1 /* i915_dma.c -- DMA support for the I915 -*- linux-c -*-
4 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include "drm_crtc_helper.h"
34 #include "drm_fb_helper.h"
35 #include "intel_drv.h"
38 #include "i915_trace.h"
39 #include "../../../platform/x86/intel_ips.h"
40 #include <linux/pci.h>
41 #include <linux/vgaarb.h>
42 #include <linux/acpi.h>
43 #include <linux/pnp.h>
44 #include <linux/vga_switcheroo.h>
45 #include <linux/slab.h>
46 #include <linux/module.h>
47 #include <acpi/video.h>
50 static void i915_write_hws_pga(struct drm_device
*dev
)
52 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
55 addr
= dev_priv
->status_page_dmah
->busaddr
;
56 if (INTEL_INFO(dev
)->gen
>= 4)
57 addr
|= (dev_priv
->status_page_dmah
->busaddr
>> 28) & 0xf0;
58 I915_WRITE(HWS_PGA
, addr
);
62 * Sets up the hardware status page for devices that need a physical address
65 static int i915_init_phys_hws(struct drm_device
*dev
)
67 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
69 /* Program Hardware Status Page */
70 dev_priv
->status_page_dmah
=
71 drm_pci_alloc(dev
, PAGE_SIZE
, PAGE_SIZE
);
73 if (!dev_priv
->status_page_dmah
) {
74 DRM_ERROR("Can not allocate hardware status page\n");
78 memset_io((void __force __iomem
*)dev_priv
->status_page_dmah
->vaddr
,
81 i915_write_hws_pga(dev
);
83 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
88 * Frees the hardware status page, whether it's a physical address or a virtual
89 * address set up by the X Server.
91 static void i915_free_hws(struct drm_device
*dev
)
93 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
94 struct intel_ring_buffer
*ring
= LP_RING(dev_priv
);
96 if (dev_priv
->status_page_dmah
) {
97 drm_pci_free(dev
, dev_priv
->status_page_dmah
);
98 dev_priv
->status_page_dmah
= NULL
;
101 if (ring
->status_page
.gfx_addr
) {
102 ring
->status_page
.gfx_addr
= 0;
103 drm_core_ioremapfree(&dev_priv
->hws_map
, dev
);
106 /* Need to rewrite hardware status page */
107 I915_WRITE(HWS_PGA
, 0x1ffff000);
110 void i915_kernel_lost_context(struct drm_device
* dev
)
112 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
113 struct drm_i915_master_private
*master_priv
;
114 struct intel_ring_buffer
*ring
= LP_RING(dev_priv
);
117 * We should never lose context on the ring with modesetting
118 * as we don't expose it to userspace
120 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
123 ring
->head
= I915_READ_HEAD(ring
) & HEAD_ADDR
;
124 ring
->tail
= I915_READ_TAIL(ring
) & TAIL_ADDR
;
125 ring
->space
= ring
->head
- (ring
->tail
+ 8);
127 ring
->space
+= ring
->size
;
129 if (!dev
->primary
->master
)
132 master_priv
= dev
->primary
->master
->driver_priv
;
133 if (ring
->head
== ring
->tail
&& master_priv
->sarea_priv
)
134 master_priv
->sarea_priv
->perf_boxes
|= I915_BOX_RING_EMPTY
;
137 static int i915_dma_cleanup(struct drm_device
* dev
)
139 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
142 /* Make sure interrupts are disabled here because the uninstall ioctl
143 * may not have been called from userspace and after dev_private
144 * is freed, it's too late.
146 if (dev
->irq_enabled
)
147 drm_irq_uninstall(dev
);
149 mutex_lock(&dev
->struct_mutex
);
150 for (i
= 0; i
< I915_NUM_RINGS
; i
++)
151 intel_cleanup_ring_buffer(&dev_priv
->ring
[i
]);
152 mutex_unlock(&dev
->struct_mutex
);
154 /* Clear the HWS virtual address at teardown */
155 if (I915_NEED_GFX_HWS(dev
))
161 static int i915_initialize(struct drm_device
* dev
, drm_i915_init_t
* init
)
163 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
164 struct drm_i915_master_private
*master_priv
= dev
->primary
->master
->driver_priv
;
167 master_priv
->sarea
= drm_getsarea(dev
);
168 if (master_priv
->sarea
) {
169 master_priv
->sarea_priv
= (drm_i915_sarea_t
*)
170 ((u8
*)master_priv
->sarea
->handle
+ init
->sarea_priv_offset
);
172 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
175 if (init
->ring_size
!= 0) {
176 if (LP_RING(dev_priv
)->obj
!= NULL
) {
177 i915_dma_cleanup(dev
);
178 DRM_ERROR("Client tried to initialize ringbuffer in "
183 ret
= intel_render_ring_init_dri(dev
,
187 i915_dma_cleanup(dev
);
192 dev_priv
->cpp
= init
->cpp
;
193 dev_priv
->back_offset
= init
->back_offset
;
194 dev_priv
->front_offset
= init
->front_offset
;
195 dev_priv
->current_page
= 0;
196 if (master_priv
->sarea_priv
)
197 master_priv
->sarea_priv
->pf_current_page
= 0;
199 /* Allow hardware batchbuffers unless told otherwise.
201 dev_priv
->allow_batchbuffer
= 1;
206 static int i915_dma_resume(struct drm_device
* dev
)
208 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
209 struct intel_ring_buffer
*ring
= LP_RING(dev_priv
);
211 DRM_DEBUG_DRIVER("%s\n", __func__
);
213 if (ring
->map
.handle
== NULL
) {
214 DRM_ERROR("can not ioremap virtual address for"
219 /* Program Hardware Status Page */
220 if (!ring
->status_page
.page_addr
) {
221 DRM_ERROR("Can not find hardware status page\n");
224 DRM_DEBUG_DRIVER("hw status page @ %p\n",
225 ring
->status_page
.page_addr
);
226 if (ring
->status_page
.gfx_addr
!= 0)
227 intel_ring_setup_status_page(ring
);
229 i915_write_hws_pga(dev
);
231 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
236 static int i915_dma_init(struct drm_device
*dev
, void *data
,
237 struct drm_file
*file_priv
)
239 drm_i915_init_t
*init
= data
;
242 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
245 switch (init
->func
) {
247 retcode
= i915_initialize(dev
, init
);
249 case I915_CLEANUP_DMA
:
250 retcode
= i915_dma_cleanup(dev
);
252 case I915_RESUME_DMA
:
253 retcode
= i915_dma_resume(dev
);
263 /* Implement basically the same security restrictions as hardware does
264 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
266 * Most of the calculations below involve calculating the size of a
267 * particular instruction. It's important to get the size right as
268 * that tells us where the next instruction to check is. Any illegal
269 * instruction detected will be given a size of zero, which is a
270 * signal to abort the rest of the buffer.
272 static int validate_cmd(int cmd
)
274 switch (((cmd
>> 29) & 0x7)) {
276 switch ((cmd
>> 23) & 0x3f) {
278 return 1; /* MI_NOOP */
280 return 1; /* MI_FLUSH */
282 return 0; /* disallow everything else */
286 return 0; /* reserved */
288 return (cmd
& 0xff) + 2; /* 2d commands */
290 if (((cmd
>> 24) & 0x1f) <= 0x18)
293 switch ((cmd
>> 24) & 0x1f) {
297 switch ((cmd
>> 16) & 0xff) {
299 return (cmd
& 0x1f) + 2;
301 return (cmd
& 0xf) + 2;
303 return (cmd
& 0xffff) + 2;
307 return (cmd
& 0xffff) + 1;
311 if ((cmd
& (1 << 23)) == 0) /* inline vertices */
312 return (cmd
& 0x1ffff) + 2;
313 else if (cmd
& (1 << 17)) /* indirect random */
314 if ((cmd
& 0xffff) == 0)
315 return 0; /* unknown length, too hard */
317 return (((cmd
& 0xffff) + 1) / 2) + 1;
319 return 2; /* indirect sequential */
330 static int i915_emit_cmds(struct drm_device
* dev
, int *buffer
, int dwords
)
332 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
335 if ((dwords
+1) * sizeof(int) >= LP_RING(dev_priv
)->size
- 8)
338 for (i
= 0; i
< dwords
;) {
339 int sz
= validate_cmd(buffer
[i
]);
340 if (sz
== 0 || i
+ sz
> dwords
)
345 ret
= BEGIN_LP_RING((dwords
+1)&~1);
349 for (i
= 0; i
< dwords
; i
++)
360 i915_emit_box(struct drm_device
*dev
,
361 struct drm_clip_rect
*box
,
364 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
367 if (box
->y2
<= box
->y1
|| box
->x2
<= box
->x1
||
368 box
->y2
<= 0 || box
->x2
<= 0) {
369 DRM_ERROR("Bad box %d,%d..%d,%d\n",
370 box
->x1
, box
->y1
, box
->x2
, box
->y2
);
374 if (INTEL_INFO(dev
)->gen
>= 4) {
375 ret
= BEGIN_LP_RING(4);
379 OUT_RING(GFX_OP_DRAWRECT_INFO_I965
);
380 OUT_RING((box
->x1
& 0xffff) | (box
->y1
<< 16));
381 OUT_RING(((box
->x2
- 1) & 0xffff) | ((box
->y2
- 1) << 16));
384 ret
= BEGIN_LP_RING(6);
388 OUT_RING(GFX_OP_DRAWRECT_INFO
);
390 OUT_RING((box
->x1
& 0xffff) | (box
->y1
<< 16));
391 OUT_RING(((box
->x2
- 1) & 0xffff) | ((box
->y2
- 1) << 16));
400 /* XXX: Emitting the counter should really be moved to part of the IRQ
401 * emit. For now, do it in both places:
404 static void i915_emit_breadcrumb(struct drm_device
*dev
)
406 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
407 struct drm_i915_master_private
*master_priv
= dev
->primary
->master
->driver_priv
;
410 if (dev_priv
->counter
> 0x7FFFFFFFUL
)
411 dev_priv
->counter
= 0;
412 if (master_priv
->sarea_priv
)
413 master_priv
->sarea_priv
->last_enqueue
= dev_priv
->counter
;
415 if (BEGIN_LP_RING(4) == 0) {
416 OUT_RING(MI_STORE_DWORD_INDEX
);
417 OUT_RING(I915_BREADCRUMB_INDEX
<< MI_STORE_DWORD_INDEX_SHIFT
);
418 OUT_RING(dev_priv
->counter
);
424 static int i915_dispatch_cmdbuffer(struct drm_device
* dev
,
425 drm_i915_cmdbuffer_t
*cmd
,
426 struct drm_clip_rect
*cliprects
,
429 int nbox
= cmd
->num_cliprects
;
430 int i
= 0, count
, ret
;
433 DRM_ERROR("alignment");
437 i915_kernel_lost_context(dev
);
439 count
= nbox
? nbox
: 1;
441 for (i
= 0; i
< count
; i
++) {
443 ret
= i915_emit_box(dev
, &cliprects
[i
],
449 ret
= i915_emit_cmds(dev
, cmdbuf
, cmd
->sz
/ 4);
454 i915_emit_breadcrumb(dev
);
458 static int i915_dispatch_batchbuffer(struct drm_device
* dev
,
459 drm_i915_batchbuffer_t
* batch
,
460 struct drm_clip_rect
*cliprects
)
462 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
463 int nbox
= batch
->num_cliprects
;
466 if ((batch
->start
| batch
->used
) & 0x7) {
467 DRM_ERROR("alignment");
471 i915_kernel_lost_context(dev
);
473 count
= nbox
? nbox
: 1;
474 for (i
= 0; i
< count
; i
++) {
476 ret
= i915_emit_box(dev
, &cliprects
[i
],
477 batch
->DR1
, batch
->DR4
);
482 if (!IS_I830(dev
) && !IS_845G(dev
)) {
483 ret
= BEGIN_LP_RING(2);
487 if (INTEL_INFO(dev
)->gen
>= 4) {
488 OUT_RING(MI_BATCH_BUFFER_START
| (2 << 6) | MI_BATCH_NON_SECURE_I965
);
489 OUT_RING(batch
->start
);
491 OUT_RING(MI_BATCH_BUFFER_START
| (2 << 6));
492 OUT_RING(batch
->start
| MI_BATCH_NON_SECURE
);
495 ret
= BEGIN_LP_RING(4);
499 OUT_RING(MI_BATCH_BUFFER
);
500 OUT_RING(batch
->start
| MI_BATCH_NON_SECURE
);
501 OUT_RING(batch
->start
+ batch
->used
- 4);
508 if (IS_G4X(dev
) || IS_GEN5(dev
)) {
509 if (BEGIN_LP_RING(2) == 0) {
510 OUT_RING(MI_FLUSH
| MI_NO_WRITE_FLUSH
| MI_INVALIDATE_ISP
);
516 i915_emit_breadcrumb(dev
);
520 static int i915_dispatch_flip(struct drm_device
* dev
)
522 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
523 struct drm_i915_master_private
*master_priv
=
524 dev
->primary
->master
->driver_priv
;
527 if (!master_priv
->sarea_priv
)
530 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
532 dev_priv
->current_page
,
533 master_priv
->sarea_priv
->pf_current_page
);
535 i915_kernel_lost_context(dev
);
537 ret
= BEGIN_LP_RING(10);
541 OUT_RING(MI_FLUSH
| MI_READ_FLUSH
);
544 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO
| ASYNC_FLIP
);
546 if (dev_priv
->current_page
== 0) {
547 OUT_RING(dev_priv
->back_offset
);
548 dev_priv
->current_page
= 1;
550 OUT_RING(dev_priv
->front_offset
);
551 dev_priv
->current_page
= 0;
555 OUT_RING(MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_A_FLIP
);
560 master_priv
->sarea_priv
->last_enqueue
= dev_priv
->counter
++;
562 if (BEGIN_LP_RING(4) == 0) {
563 OUT_RING(MI_STORE_DWORD_INDEX
);
564 OUT_RING(I915_BREADCRUMB_INDEX
<< MI_STORE_DWORD_INDEX_SHIFT
);
565 OUT_RING(dev_priv
->counter
);
570 master_priv
->sarea_priv
->pf_current_page
= dev_priv
->current_page
;
574 static int i915_quiescent(struct drm_device
*dev
)
576 struct intel_ring_buffer
*ring
= LP_RING(dev
->dev_private
);
578 i915_kernel_lost_context(dev
);
579 return intel_wait_ring_idle(ring
);
582 static int i915_flush_ioctl(struct drm_device
*dev
, void *data
,
583 struct drm_file
*file_priv
)
587 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
590 RING_LOCK_TEST_WITH_RETURN(dev
, file_priv
);
592 mutex_lock(&dev
->struct_mutex
);
593 ret
= i915_quiescent(dev
);
594 mutex_unlock(&dev
->struct_mutex
);
599 static int i915_batchbuffer(struct drm_device
*dev
, void *data
,
600 struct drm_file
*file_priv
)
602 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
603 struct drm_i915_master_private
*master_priv
= dev
->primary
->master
->driver_priv
;
604 drm_i915_sarea_t
*sarea_priv
= (drm_i915_sarea_t
*)
605 master_priv
->sarea_priv
;
606 drm_i915_batchbuffer_t
*batch
= data
;
608 struct drm_clip_rect
*cliprects
= NULL
;
610 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
613 if (!dev_priv
->allow_batchbuffer
) {
614 DRM_ERROR("Batchbuffer ioctl disabled\n");
618 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
619 batch
->start
, batch
->used
, batch
->num_cliprects
);
621 RING_LOCK_TEST_WITH_RETURN(dev
, file_priv
);
623 if (batch
->num_cliprects
< 0)
626 if (batch
->num_cliprects
) {
627 cliprects
= kcalloc(batch
->num_cliprects
,
628 sizeof(struct drm_clip_rect
),
630 if (cliprects
== NULL
)
633 ret
= copy_from_user(cliprects
, batch
->cliprects
,
634 batch
->num_cliprects
*
635 sizeof(struct drm_clip_rect
));
642 mutex_lock(&dev
->struct_mutex
);
643 ret
= i915_dispatch_batchbuffer(dev
, batch
, cliprects
);
644 mutex_unlock(&dev
->struct_mutex
);
647 sarea_priv
->last_dispatch
= READ_BREADCRUMB(dev_priv
);
655 static int i915_cmdbuffer(struct drm_device
*dev
, void *data
,
656 struct drm_file
*file_priv
)
658 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
659 struct drm_i915_master_private
*master_priv
= dev
->primary
->master
->driver_priv
;
660 drm_i915_sarea_t
*sarea_priv
= (drm_i915_sarea_t
*)
661 master_priv
->sarea_priv
;
662 drm_i915_cmdbuffer_t
*cmdbuf
= data
;
663 struct drm_clip_rect
*cliprects
= NULL
;
667 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
668 cmdbuf
->buf
, cmdbuf
->sz
, cmdbuf
->num_cliprects
);
670 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
673 RING_LOCK_TEST_WITH_RETURN(dev
, file_priv
);
675 if (cmdbuf
->num_cliprects
< 0)
678 batch_data
= kmalloc(cmdbuf
->sz
, GFP_KERNEL
);
679 if (batch_data
== NULL
)
682 ret
= copy_from_user(batch_data
, cmdbuf
->buf
, cmdbuf
->sz
);
685 goto fail_batch_free
;
688 if (cmdbuf
->num_cliprects
) {
689 cliprects
= kcalloc(cmdbuf
->num_cliprects
,
690 sizeof(struct drm_clip_rect
), GFP_KERNEL
);
691 if (cliprects
== NULL
) {
693 goto fail_batch_free
;
696 ret
= copy_from_user(cliprects
, cmdbuf
->cliprects
,
697 cmdbuf
->num_cliprects
*
698 sizeof(struct drm_clip_rect
));
705 mutex_lock(&dev
->struct_mutex
);
706 ret
= i915_dispatch_cmdbuffer(dev
, cmdbuf
, cliprects
, batch_data
);
707 mutex_unlock(&dev
->struct_mutex
);
709 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
714 sarea_priv
->last_dispatch
= READ_BREADCRUMB(dev_priv
);
724 static int i915_flip_bufs(struct drm_device
*dev
, void *data
,
725 struct drm_file
*file_priv
)
729 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
732 DRM_DEBUG_DRIVER("%s\n", __func__
);
734 RING_LOCK_TEST_WITH_RETURN(dev
, file_priv
);
736 mutex_lock(&dev
->struct_mutex
);
737 ret
= i915_dispatch_flip(dev
);
738 mutex_unlock(&dev
->struct_mutex
);
743 static int i915_getparam(struct drm_device
*dev
, void *data
,
744 struct drm_file
*file_priv
)
746 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
747 drm_i915_getparam_t
*param
= data
;
751 DRM_ERROR("called with no initialization\n");
755 switch (param
->param
) {
756 case I915_PARAM_IRQ_ACTIVE
:
757 value
= dev
->pdev
->irq
? 1 : 0;
759 case I915_PARAM_ALLOW_BATCHBUFFER
:
760 value
= dev_priv
->allow_batchbuffer
? 1 : 0;
762 case I915_PARAM_LAST_DISPATCH
:
763 value
= READ_BREADCRUMB(dev_priv
);
765 case I915_PARAM_CHIPSET_ID
:
766 value
= dev
->pci_device
;
768 case I915_PARAM_HAS_GEM
:
771 case I915_PARAM_NUM_FENCES_AVAIL
:
772 value
= dev_priv
->num_fence_regs
- dev_priv
->fence_reg_start
;
774 case I915_PARAM_HAS_OVERLAY
:
775 value
= dev_priv
->overlay
? 1 : 0;
777 case I915_PARAM_HAS_PAGEFLIPPING
:
780 case I915_PARAM_HAS_EXECBUF2
:
784 case I915_PARAM_HAS_BSD
:
785 value
= HAS_BSD(dev
);
787 case I915_PARAM_HAS_BLT
:
788 value
= HAS_BLT(dev
);
790 case I915_PARAM_HAS_RELAXED_FENCING
:
793 case I915_PARAM_HAS_COHERENT_RINGS
:
796 case I915_PARAM_HAS_EXEC_CONSTANTS
:
797 value
= INTEL_INFO(dev
)->gen
>= 4;
799 case I915_PARAM_HAS_RELAXED_DELTA
:
802 case I915_PARAM_HAS_GEN7_SOL_RESET
:
805 case I915_PARAM_HAS_LLC
:
806 value
= HAS_LLC(dev
);
808 case I915_PARAM_HAS_ALIASING_PPGTT
:
809 value
= dev_priv
->mm
.aliasing_ppgtt
? 1 : 0;
812 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
817 if (DRM_COPY_TO_USER(param
->value
, &value
, sizeof(int))) {
818 DRM_ERROR("DRM_COPY_TO_USER failed\n");
825 static int i915_setparam(struct drm_device
*dev
, void *data
,
826 struct drm_file
*file_priv
)
828 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
829 drm_i915_setparam_t
*param
= data
;
832 DRM_ERROR("called with no initialization\n");
836 switch (param
->param
) {
837 case I915_SETPARAM_USE_MI_BATCHBUFFER_START
:
839 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY
:
840 dev_priv
->tex_lru_log_granularity
= param
->value
;
842 case I915_SETPARAM_ALLOW_BATCHBUFFER
:
843 dev_priv
->allow_batchbuffer
= param
->value
;
845 case I915_SETPARAM_NUM_USED_FENCES
:
846 if (param
->value
> dev_priv
->num_fence_regs
||
849 /* Userspace can use first N regs */
850 dev_priv
->fence_reg_start
= param
->value
;
853 DRM_DEBUG_DRIVER("unknown parameter %d\n",
861 static int i915_set_status_page(struct drm_device
*dev
, void *data
,
862 struct drm_file
*file_priv
)
864 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
865 drm_i915_hws_addr_t
*hws
= data
;
866 struct intel_ring_buffer
*ring
= LP_RING(dev_priv
);
868 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
871 if (!I915_NEED_GFX_HWS(dev
))
875 DRM_ERROR("called with no initialization\n");
879 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
880 WARN(1, "tried to set status page when mode setting active\n");
884 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32
)hws
->addr
);
886 ring
->status_page
.gfx_addr
= hws
->addr
& (0x1ffff<<12);
888 dev_priv
->hws_map
.offset
= dev
->agp
->base
+ hws
->addr
;
889 dev_priv
->hws_map
.size
= 4*1024;
890 dev_priv
->hws_map
.type
= 0;
891 dev_priv
->hws_map
.flags
= 0;
892 dev_priv
->hws_map
.mtrr
= 0;
894 drm_core_ioremap_wc(&dev_priv
->hws_map
, dev
);
895 if (dev_priv
->hws_map
.handle
== NULL
) {
896 i915_dma_cleanup(dev
);
897 ring
->status_page
.gfx_addr
= 0;
898 DRM_ERROR("can not ioremap virtual address for"
899 " G33 hw status page\n");
902 ring
->status_page
.page_addr
=
903 (void __force __iomem
*)dev_priv
->hws_map
.handle
;
904 memset_io(ring
->status_page
.page_addr
, 0, PAGE_SIZE
);
905 I915_WRITE(HWS_PGA
, ring
->status_page
.gfx_addr
);
907 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
908 ring
->status_page
.gfx_addr
);
909 DRM_DEBUG_DRIVER("load hws at %p\n",
910 ring
->status_page
.page_addr
);
914 static int i915_get_bridge_dev(struct drm_device
*dev
)
916 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
918 dev_priv
->bridge_dev
= pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
919 if (!dev_priv
->bridge_dev
) {
920 DRM_ERROR("bridge device not found\n");
926 #define MCHBAR_I915 0x44
927 #define MCHBAR_I965 0x48
928 #define MCHBAR_SIZE (4*4096)
930 #define DEVEN_REG 0x54
931 #define DEVEN_MCHBAR_EN (1 << 28)
933 /* Allocate space for the MCH regs if needed, return nonzero on error */
935 intel_alloc_mchbar_resource(struct drm_device
*dev
)
937 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
938 int reg
= INTEL_INFO(dev
)->gen
>= 4 ? MCHBAR_I965
: MCHBAR_I915
;
939 u32 temp_lo
, temp_hi
= 0;
943 if (INTEL_INFO(dev
)->gen
>= 4)
944 pci_read_config_dword(dev_priv
->bridge_dev
, reg
+ 4, &temp_hi
);
945 pci_read_config_dword(dev_priv
->bridge_dev
, reg
, &temp_lo
);
946 mchbar_addr
= ((u64
)temp_hi
<< 32) | temp_lo
;
948 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
951 pnp_range_reserved(mchbar_addr
, mchbar_addr
+ MCHBAR_SIZE
))
955 /* Get some space for it */
956 dev_priv
->mch_res
.name
= "i915 MCHBAR";
957 dev_priv
->mch_res
.flags
= IORESOURCE_MEM
;
958 ret
= pci_bus_alloc_resource(dev_priv
->bridge_dev
->bus
,
960 MCHBAR_SIZE
, MCHBAR_SIZE
,
962 0, pcibios_align_resource
,
963 dev_priv
->bridge_dev
);
965 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret
);
966 dev_priv
->mch_res
.start
= 0;
970 if (INTEL_INFO(dev
)->gen
>= 4)
971 pci_write_config_dword(dev_priv
->bridge_dev
, reg
+ 4,
972 upper_32_bits(dev_priv
->mch_res
.start
));
974 pci_write_config_dword(dev_priv
->bridge_dev
, reg
,
975 lower_32_bits(dev_priv
->mch_res
.start
));
979 /* Setup MCHBAR if possible, return true if we should disable it again */
981 intel_setup_mchbar(struct drm_device
*dev
)
983 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
984 int mchbar_reg
= INTEL_INFO(dev
)->gen
>= 4 ? MCHBAR_I965
: MCHBAR_I915
;
988 dev_priv
->mchbar_need_disable
= false;
990 if (IS_I915G(dev
) || IS_I915GM(dev
)) {
991 pci_read_config_dword(dev_priv
->bridge_dev
, DEVEN_REG
, &temp
);
992 enabled
= !!(temp
& DEVEN_MCHBAR_EN
);
994 pci_read_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, &temp
);
998 /* If it's already enabled, don't have to do anything */
1002 if (intel_alloc_mchbar_resource(dev
))
1005 dev_priv
->mchbar_need_disable
= true;
1007 /* Space is allocated or reserved, so enable it. */
1008 if (IS_I915G(dev
) || IS_I915GM(dev
)) {
1009 pci_write_config_dword(dev_priv
->bridge_dev
, DEVEN_REG
,
1010 temp
| DEVEN_MCHBAR_EN
);
1012 pci_read_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, &temp
);
1013 pci_write_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, temp
| 1);
1018 intel_teardown_mchbar(struct drm_device
*dev
)
1020 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1021 int mchbar_reg
= INTEL_INFO(dev
)->gen
>= 4 ? MCHBAR_I965
: MCHBAR_I915
;
1024 if (dev_priv
->mchbar_need_disable
) {
1025 if (IS_I915G(dev
) || IS_I915GM(dev
)) {
1026 pci_read_config_dword(dev_priv
->bridge_dev
, DEVEN_REG
, &temp
);
1027 temp
&= ~DEVEN_MCHBAR_EN
;
1028 pci_write_config_dword(dev_priv
->bridge_dev
, DEVEN_REG
, temp
);
1030 pci_read_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, &temp
);
1032 pci_write_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, temp
);
1036 if (dev_priv
->mch_res
.start
)
1037 release_resource(&dev_priv
->mch_res
);
1040 /* true = enable decode, false = disable decoder */
1041 static unsigned int i915_vga_set_decode(void *cookie
, bool state
)
1043 struct drm_device
*dev
= cookie
;
1045 intel_modeset_vga_set_state(dev
, state
);
1047 return VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
|
1048 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
1050 return VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
1053 static void i915_switcheroo_set_state(struct pci_dev
*pdev
, enum vga_switcheroo_state state
)
1055 struct drm_device
*dev
= pci_get_drvdata(pdev
);
1056 pm_message_t pmm
= { .event
= PM_EVENT_SUSPEND
};
1057 if (state
== VGA_SWITCHEROO_ON
) {
1058 pr_info("switched on\n");
1059 dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
1060 /* i915 resume handler doesn't set to D0 */
1061 pci_set_power_state(dev
->pdev
, PCI_D0
);
1063 dev
->switch_power_state
= DRM_SWITCH_POWER_ON
;
1065 pr_err("switched off\n");
1066 dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
1067 i915_suspend(dev
, pmm
);
1068 dev
->switch_power_state
= DRM_SWITCH_POWER_OFF
;
1072 static bool i915_switcheroo_can_switch(struct pci_dev
*pdev
)
1074 struct drm_device
*dev
= pci_get_drvdata(pdev
);
1077 spin_lock(&dev
->count_lock
);
1078 can_switch
= (dev
->open_count
== 0);
1079 spin_unlock(&dev
->count_lock
);
1083 static int i915_load_modeset_init(struct drm_device
*dev
)
1085 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1088 ret
= intel_parse_bios(dev
);
1090 DRM_INFO("failed to find VBIOS tables\n");
1092 /* If we have > 1 VGA cards, then we need to arbitrate access
1093 * to the common VGA resources.
1095 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1096 * then we do not take part in VGA arbitration and the
1097 * vga_client_register() fails with -ENODEV.
1099 ret
= vga_client_register(dev
->pdev
, dev
, NULL
, i915_vga_set_decode
);
1100 if (ret
&& ret
!= -ENODEV
)
1103 intel_register_dsm_handler();
1105 ret
= vga_switcheroo_register_client(dev
->pdev
,
1106 i915_switcheroo_set_state
,
1108 i915_switcheroo_can_switch
);
1110 goto cleanup_vga_client
;
1112 /* Initialise stolen first so that we may reserve preallocated
1113 * objects for the BIOS to KMS transition.
1115 ret
= i915_gem_init_stolen(dev
);
1117 goto cleanup_vga_switcheroo
;
1119 intel_modeset_init(dev
);
1121 ret
= i915_gem_init(dev
);
1123 goto cleanup_gem_stolen
;
1125 intel_modeset_gem_init(dev
);
1127 ret
= drm_irq_install(dev
);
1131 /* Always safe in the mode setting case. */
1132 /* FIXME: do pre/post-mode set stuff in core KMS code */
1133 dev
->vblank_disable_allowed
= 1;
1135 ret
= intel_fbdev_init(dev
);
1139 drm_kms_helper_poll_init(dev
);
1141 /* We're off and running w/KMS */
1142 dev_priv
->mm
.suspended
= 0;
1147 drm_irq_uninstall(dev
);
1149 mutex_lock(&dev
->struct_mutex
);
1150 i915_gem_cleanup_ringbuffer(dev
);
1151 mutex_unlock(&dev
->struct_mutex
);
1152 i915_gem_cleanup_aliasing_ppgtt(dev
);
1154 i915_gem_cleanup_stolen(dev
);
1155 cleanup_vga_switcheroo
:
1156 vga_switcheroo_unregister_client(dev
->pdev
);
1158 vga_client_register(dev
->pdev
, NULL
, NULL
, NULL
);
1163 int i915_master_create(struct drm_device
*dev
, struct drm_master
*master
)
1165 struct drm_i915_master_private
*master_priv
;
1167 master_priv
= kzalloc(sizeof(*master_priv
), GFP_KERNEL
);
1171 master
->driver_priv
= master_priv
;
1175 void i915_master_destroy(struct drm_device
*dev
, struct drm_master
*master
)
1177 struct drm_i915_master_private
*master_priv
= master
->driver_priv
;
1184 master
->driver_priv
= NULL
;
1187 static void i915_pineview_get_mem_freq(struct drm_device
*dev
)
1189 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1192 tmp
= I915_READ(CLKCFG
);
1194 switch (tmp
& CLKCFG_FSB_MASK
) {
1195 case CLKCFG_FSB_533
:
1196 dev_priv
->fsb_freq
= 533; /* 133*4 */
1198 case CLKCFG_FSB_800
:
1199 dev_priv
->fsb_freq
= 800; /* 200*4 */
1201 case CLKCFG_FSB_667
:
1202 dev_priv
->fsb_freq
= 667; /* 167*4 */
1204 case CLKCFG_FSB_400
:
1205 dev_priv
->fsb_freq
= 400; /* 100*4 */
1209 switch (tmp
& CLKCFG_MEM_MASK
) {
1210 case CLKCFG_MEM_533
:
1211 dev_priv
->mem_freq
= 533;
1213 case CLKCFG_MEM_667
:
1214 dev_priv
->mem_freq
= 667;
1216 case CLKCFG_MEM_800
:
1217 dev_priv
->mem_freq
= 800;
1221 /* detect pineview DDR3 setting */
1222 tmp
= I915_READ(CSHRDDR3CTL
);
1223 dev_priv
->is_ddr3
= (tmp
& CSHRDDR3CTL_DDR3
) ? 1 : 0;
1226 static void i915_ironlake_get_mem_freq(struct drm_device
*dev
)
1228 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1231 ddrpll
= I915_READ16(DDRMPLL1
);
1232 csipll
= I915_READ16(CSIPLL0
);
1234 switch (ddrpll
& 0xff) {
1236 dev_priv
->mem_freq
= 800;
1239 dev_priv
->mem_freq
= 1066;
1242 dev_priv
->mem_freq
= 1333;
1245 dev_priv
->mem_freq
= 1600;
1248 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
1250 dev_priv
->mem_freq
= 0;
1254 dev_priv
->r_t
= dev_priv
->mem_freq
;
1256 switch (csipll
& 0x3ff) {
1258 dev_priv
->fsb_freq
= 3200;
1261 dev_priv
->fsb_freq
= 3733;
1264 dev_priv
->fsb_freq
= 4266;
1267 dev_priv
->fsb_freq
= 4800;
1270 dev_priv
->fsb_freq
= 5333;
1273 dev_priv
->fsb_freq
= 5866;
1276 dev_priv
->fsb_freq
= 6400;
1279 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
1281 dev_priv
->fsb_freq
= 0;
1285 if (dev_priv
->fsb_freq
== 3200) {
1287 } else if (dev_priv
->fsb_freq
> 3200 && dev_priv
->fsb_freq
<= 4800) {
1294 static const struct cparams
{
1300 { 1, 1333, 301, 28664 },
1301 { 1, 1066, 294, 24460 },
1302 { 1, 800, 294, 25192 },
1303 { 0, 1333, 276, 27605 },
1304 { 0, 1066, 276, 27605 },
1305 { 0, 800, 231, 23784 },
1308 unsigned long i915_chipset_val(struct drm_i915_private
*dev_priv
)
1310 u64 total_count
, diff
, ret
;
1311 u32 count1
, count2
, count3
, m
= 0, c
= 0;
1312 unsigned long now
= jiffies_to_msecs(jiffies
), diff1
;
1315 diff1
= now
- dev_priv
->last_time1
;
1317 /* Prevent division-by-zero if we are asking too fast.
1318 * Also, we don't get interesting results if we are polling
1319 * faster than once in 10ms, so just return the saved value
1323 return dev_priv
->chipset_power
;
1325 count1
= I915_READ(DMIEC
);
1326 count2
= I915_READ(DDREC
);
1327 count3
= I915_READ(CSIEC
);
1329 total_count
= count1
+ count2
+ count3
;
1331 /* FIXME: handle per-counter overflow */
1332 if (total_count
< dev_priv
->last_count1
) {
1333 diff
= ~0UL - dev_priv
->last_count1
;
1334 diff
+= total_count
;
1336 diff
= total_count
- dev_priv
->last_count1
;
1339 for (i
= 0; i
< ARRAY_SIZE(cparams
); i
++) {
1340 if (cparams
[i
].i
== dev_priv
->c_m
&&
1341 cparams
[i
].t
== dev_priv
->r_t
) {
1348 diff
= div_u64(diff
, diff1
);
1349 ret
= ((m
* diff
) + c
);
1350 ret
= div_u64(ret
, 10);
1352 dev_priv
->last_count1
= total_count
;
1353 dev_priv
->last_time1
= now
;
1355 dev_priv
->chipset_power
= ret
;
1360 unsigned long i915_mch_val(struct drm_i915_private
*dev_priv
)
1362 unsigned long m
, x
, b
;
1365 tsfs
= I915_READ(TSFS
);
1367 m
= ((tsfs
& TSFS_SLOPE_MASK
) >> TSFS_SLOPE_SHIFT
);
1368 x
= I915_READ8(TR1
);
1370 b
= tsfs
& TSFS_INTR_MASK
;
1372 return ((m
* x
) / 127) - b
;
1375 static u16
pvid_to_extvid(struct drm_i915_private
*dev_priv
, u8 pxvid
)
1377 static const struct v_table
{
1378 u16 vd
; /* in .1 mil */
1379 u16 vm
; /* in .1 mil */
1510 if (dev_priv
->info
->is_mobile
)
1511 return v_table
[pxvid
].vm
;
1513 return v_table
[pxvid
].vd
;
1516 void i915_update_gfx_val(struct drm_i915_private
*dev_priv
)
1518 struct timespec now
, diff1
;
1520 unsigned long diffms
;
1523 getrawmonotonic(&now
);
1524 diff1
= timespec_sub(now
, dev_priv
->last_time2
);
1526 /* Don't divide by 0 */
1527 diffms
= diff1
.tv_sec
* 1000 + diff1
.tv_nsec
/ 1000000;
1531 count
= I915_READ(GFXEC
);
1533 if (count
< dev_priv
->last_count2
) {
1534 diff
= ~0UL - dev_priv
->last_count2
;
1537 diff
= count
- dev_priv
->last_count2
;
1540 dev_priv
->last_count2
= count
;
1541 dev_priv
->last_time2
= now
;
1543 /* More magic constants... */
1545 diff
= div_u64(diff
, diffms
* 10);
1546 dev_priv
->gfx_power
= diff
;
1549 unsigned long i915_gfx_val(struct drm_i915_private
*dev_priv
)
1551 unsigned long t
, corr
, state1
, corr2
, state2
;
1554 pxvid
= I915_READ(PXVFREQ_BASE
+ (dev_priv
->cur_delay
* 4));
1555 pxvid
= (pxvid
>> 24) & 0x7f;
1556 ext_v
= pvid_to_extvid(dev_priv
, pxvid
);
1560 t
= i915_mch_val(dev_priv
);
1562 /* Revel in the empirically derived constants */
1564 /* Correction factor in 1/100000 units */
1566 corr
= ((t
* 2349) + 135940);
1568 corr
= ((t
* 964) + 29317);
1570 corr
= ((t
* 301) + 1004);
1572 corr
= corr
* ((150142 * state1
) / 10000 - 78642);
1574 corr2
= (corr
* dev_priv
->corr
);
1576 state2
= (corr2
* state1
) / 10000;
1577 state2
/= 100; /* convert to mW */
1579 i915_update_gfx_val(dev_priv
);
1581 return dev_priv
->gfx_power
+ state2
;
1584 /* Global for IPS driver to get at the current i915 device */
1585 static struct drm_i915_private
*i915_mch_dev
;
1587 * Lock protecting IPS related data structures
1589 * - dev_priv->max_delay
1590 * - dev_priv->min_delay
1592 * - dev_priv->gpu_busy
1594 static DEFINE_SPINLOCK(mchdev_lock
);
1597 * i915_read_mch_val - return value for IPS use
1599 * Calculate and return a value for the IPS driver to use when deciding whether
1600 * we have thermal and power headroom to increase CPU or GPU power budget.
1602 unsigned long i915_read_mch_val(void)
1604 struct drm_i915_private
*dev_priv
;
1605 unsigned long chipset_val
, graphics_val
, ret
= 0;
1607 spin_lock(&mchdev_lock
);
1610 dev_priv
= i915_mch_dev
;
1612 chipset_val
= i915_chipset_val(dev_priv
);
1613 graphics_val
= i915_gfx_val(dev_priv
);
1615 ret
= chipset_val
+ graphics_val
;
1618 spin_unlock(&mchdev_lock
);
1622 EXPORT_SYMBOL_GPL(i915_read_mch_val
);
1625 * i915_gpu_raise - raise GPU frequency limit
1627 * Raise the limit; IPS indicates we have thermal headroom.
1629 bool i915_gpu_raise(void)
1631 struct drm_i915_private
*dev_priv
;
1634 spin_lock(&mchdev_lock
);
1635 if (!i915_mch_dev
) {
1639 dev_priv
= i915_mch_dev
;
1641 if (dev_priv
->max_delay
> dev_priv
->fmax
)
1642 dev_priv
->max_delay
--;
1645 spin_unlock(&mchdev_lock
);
1649 EXPORT_SYMBOL_GPL(i915_gpu_raise
);
1652 * i915_gpu_lower - lower GPU frequency limit
1654 * IPS indicates we're close to a thermal limit, so throttle back the GPU
1655 * frequency maximum.
1657 bool i915_gpu_lower(void)
1659 struct drm_i915_private
*dev_priv
;
1662 spin_lock(&mchdev_lock
);
1663 if (!i915_mch_dev
) {
1667 dev_priv
= i915_mch_dev
;
1669 if (dev_priv
->max_delay
< dev_priv
->min_delay
)
1670 dev_priv
->max_delay
++;
1673 spin_unlock(&mchdev_lock
);
1677 EXPORT_SYMBOL_GPL(i915_gpu_lower
);
1680 * i915_gpu_busy - indicate GPU business to IPS
1682 * Tell the IPS driver whether or not the GPU is busy.
1684 bool i915_gpu_busy(void)
1686 struct drm_i915_private
*dev_priv
;
1689 spin_lock(&mchdev_lock
);
1692 dev_priv
= i915_mch_dev
;
1694 ret
= dev_priv
->busy
;
1697 spin_unlock(&mchdev_lock
);
1701 EXPORT_SYMBOL_GPL(i915_gpu_busy
);
1704 * i915_gpu_turbo_disable - disable graphics turbo
1706 * Disable graphics turbo by resetting the max frequency and setting the
1707 * current frequency to the default.
1709 bool i915_gpu_turbo_disable(void)
1711 struct drm_i915_private
*dev_priv
;
1714 spin_lock(&mchdev_lock
);
1715 if (!i915_mch_dev
) {
1719 dev_priv
= i915_mch_dev
;
1721 dev_priv
->max_delay
= dev_priv
->fstart
;
1723 if (!ironlake_set_drps(dev_priv
->dev
, dev_priv
->fstart
))
1727 spin_unlock(&mchdev_lock
);
1731 EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable
);
1734 * Tells the intel_ips driver that the i915 driver is now loaded, if
1735 * IPS got loaded first.
1737 * This awkward dance is so that neither module has to depend on the
1738 * other in order for IPS to do the appropriate communication of
1739 * GPU turbo limits to i915.
1742 ips_ping_for_i915_load(void)
1746 link
= symbol_get(ips_link_to_i915_driver
);
1749 symbol_put(ips_link_to_i915_driver
);
1754 i915_mtrr_setup(struct drm_i915_private
*dev_priv
, unsigned long base
,
1757 dev_priv
->mm
.gtt_mtrr
= -1;
1759 #if defined(CONFIG_X86_PAT)
1764 /* Set up a WC MTRR for non-PAT systems. This is more common than
1765 * one would think, because the kernel disables PAT on first
1766 * generation Core chips because WC PAT gets overridden by a UC
1767 * MTRR if present. Even if a UC MTRR isn't present.
1769 dev_priv
->mm
.gtt_mtrr
= mtrr_add(base
, size
, MTRR_TYPE_WRCOMB
, 1);
1770 if (dev_priv
->mm
.gtt_mtrr
< 0) {
1771 DRM_INFO("MTRR allocation failed. Graphics "
1772 "performance may suffer.\n");
1777 * i915_driver_load - setup chip and create an initial config
1779 * @flags: startup flags
1781 * The driver load routine has to do several things:
1782 * - drive output discovery via intel_modeset_init()
1783 * - initialize the memory manager
1784 * - allocate initial config memory
1785 * - setup the DRM framebuffer with the allocated memory
1787 int i915_driver_load(struct drm_device
*dev
, unsigned long flags
)
1789 struct drm_i915_private
*dev_priv
;
1790 struct intel_device_info
*info
;
1791 int ret
= 0, mmio_bar
;
1792 uint32_t aperture_size
;
1794 info
= (struct intel_device_info
*) flags
;
1796 /* Refuse to load on gen6+ without kms enabled. */
1797 if (info
->gen
>= 6 && !drm_core_check_feature(dev
, DRIVER_MODESET
))
1801 /* i915 has 4 more counters */
1803 dev
->types
[6] = _DRM_STAT_IRQ
;
1804 dev
->types
[7] = _DRM_STAT_PRIMARY
;
1805 dev
->types
[8] = _DRM_STAT_SECONDARY
;
1806 dev
->types
[9] = _DRM_STAT_DMA
;
1808 dev_priv
= kzalloc(sizeof(drm_i915_private_t
), GFP_KERNEL
);
1809 if (dev_priv
== NULL
)
1812 dev
->dev_private
= (void *)dev_priv
;
1813 dev_priv
->dev
= dev
;
1814 dev_priv
->info
= info
;
1816 if (i915_get_bridge_dev(dev
)) {
1821 pci_set_master(dev
->pdev
);
1823 /* overlay on gen2 is broken and can't address above 1G */
1825 dma_set_coherent_mask(&dev
->pdev
->dev
, DMA_BIT_MASK(30));
1827 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1828 * using 32bit addressing, overwriting memory if HWS is located
1831 * The documentation also mentions an issue with undefined
1832 * behaviour if any general state is accessed within a page above 4GB,
1833 * which also needs to be handled carefully.
1835 if (IS_BROADWATER(dev
) || IS_CRESTLINE(dev
))
1836 dma_set_coherent_mask(&dev
->pdev
->dev
, DMA_BIT_MASK(32));
1838 mmio_bar
= IS_GEN2(dev
) ? 1 : 0;
1839 dev_priv
->regs
= pci_iomap(dev
->pdev
, mmio_bar
, 0);
1840 if (!dev_priv
->regs
) {
1841 DRM_ERROR("failed to map registers\n");
1846 dev_priv
->mm
.gtt
= intel_gtt_get();
1847 if (!dev_priv
->mm
.gtt
) {
1848 DRM_ERROR("Failed to initialize GTT\n");
1853 aperture_size
= dev_priv
->mm
.gtt
->gtt_mappable_entries
<< PAGE_SHIFT
;
1855 dev_priv
->mm
.gtt_mapping
=
1856 io_mapping_create_wc(dev
->agp
->base
, aperture_size
);
1857 if (dev_priv
->mm
.gtt_mapping
== NULL
) {
1862 i915_mtrr_setup(dev_priv
, dev
->agp
->base
, aperture_size
);
1864 /* The i915 workqueue is primarily used for batched retirement of
1865 * requests (and thus managing bo) once the task has been completed
1866 * by the GPU. i915_gem_retire_requests() is called directly when we
1867 * need high-priority retirement, such as waiting for an explicit
1870 * It is also used for periodic low-priority events, such as
1871 * idle-timers and recording error state.
1873 * All tasks on the workqueue are expected to acquire the dev mutex
1874 * so there is no point in running more than one instance of the
1875 * workqueue at any time: max_active = 1 and NON_REENTRANT.
1877 dev_priv
->wq
= alloc_workqueue("i915",
1878 WQ_UNBOUND
| WQ_NON_REENTRANT
,
1880 if (dev_priv
->wq
== NULL
) {
1881 DRM_ERROR("Failed to create our workqueue.\n");
1886 intel_irq_init(dev
);
1888 /* Try to make sure MCHBAR is enabled before poking at it */
1889 intel_setup_mchbar(dev
);
1890 intel_setup_gmbus(dev
);
1891 intel_opregion_setup(dev
);
1893 /* Make sure the bios did its job and set up vital registers */
1894 intel_setup_bios(dev
);
1899 if (!I915_NEED_GFX_HWS(dev
)) {
1900 ret
= i915_init_phys_hws(dev
);
1902 goto out_gem_unload
;
1905 if (IS_PINEVIEW(dev
))
1906 i915_pineview_get_mem_freq(dev
);
1907 else if (IS_GEN5(dev
))
1908 i915_ironlake_get_mem_freq(dev
);
1910 /* On the 945G/GM, the chipset reports the MSI capability on the
1911 * integrated graphics even though the support isn't actually there
1912 * according to the published specs. It doesn't appear to function
1913 * correctly in testing on 945G.
1914 * This may be a side effect of MSI having been made available for PEG
1915 * and the registers being closely associated.
1917 * According to chipset errata, on the 965GM, MSI interrupts may
1918 * be lost or delayed, but we use them anyways to avoid
1919 * stuck interrupts on some machines.
1921 if (!IS_I945G(dev
) && !IS_I945GM(dev
))
1922 pci_enable_msi(dev
->pdev
);
1924 spin_lock_init(&dev_priv
->gt_lock
);
1925 spin_lock_init(&dev_priv
->irq_lock
);
1926 spin_lock_init(&dev_priv
->error_lock
);
1927 spin_lock_init(&dev_priv
->rps_lock
);
1929 if (IS_IVYBRIDGE(dev
) || IS_HASWELL(dev
))
1930 dev_priv
->num_pipe
= 3;
1931 else if (IS_MOBILE(dev
) || !IS_GEN2(dev
))
1932 dev_priv
->num_pipe
= 2;
1934 dev_priv
->num_pipe
= 1;
1936 ret
= drm_vblank_init(dev
, dev_priv
->num_pipe
);
1938 goto out_gem_unload
;
1940 /* Start out suspended */
1941 dev_priv
->mm
.suspended
= 1;
1943 intel_detect_pch(dev
);
1945 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
1946 ret
= i915_load_modeset_init(dev
);
1948 DRM_ERROR("failed to init modeset\n");
1949 goto out_gem_unload
;
1953 i915_setup_sysfs(dev
);
1955 /* Must be done after probing outputs */
1956 intel_opregion_init(dev
);
1957 acpi_video_register();
1959 setup_timer(&dev_priv
->hangcheck_timer
, i915_hangcheck_elapsed
,
1960 (unsigned long) dev
);
1962 spin_lock(&mchdev_lock
);
1963 i915_mch_dev
= dev_priv
;
1964 dev_priv
->mchdev_lock
= &mchdev_lock
;
1965 spin_unlock(&mchdev_lock
);
1967 ips_ping_for_i915_load();
1972 if (dev_priv
->mm
.inactive_shrinker
.shrink
)
1973 unregister_shrinker(&dev_priv
->mm
.inactive_shrinker
);
1975 if (dev
->pdev
->msi_enabled
)
1976 pci_disable_msi(dev
->pdev
);
1978 intel_teardown_gmbus(dev
);
1979 intel_teardown_mchbar(dev
);
1980 destroy_workqueue(dev_priv
->wq
);
1982 if (dev_priv
->mm
.gtt_mtrr
>= 0) {
1983 mtrr_del(dev_priv
->mm
.gtt_mtrr
, dev
->agp
->base
,
1984 dev
->agp
->agp_info
.aper_size
* 1024 * 1024);
1985 dev_priv
->mm
.gtt_mtrr
= -1;
1987 io_mapping_free(dev_priv
->mm
.gtt_mapping
);
1989 pci_iounmap(dev
->pdev
, dev_priv
->regs
);
1991 pci_dev_put(dev_priv
->bridge_dev
);
1997 int i915_driver_unload(struct drm_device
*dev
)
1999 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
2002 spin_lock(&mchdev_lock
);
2003 i915_mch_dev
= NULL
;
2004 spin_unlock(&mchdev_lock
);
2006 i915_teardown_sysfs(dev
);
2008 if (dev_priv
->mm
.inactive_shrinker
.shrink
)
2009 unregister_shrinker(&dev_priv
->mm
.inactive_shrinker
);
2011 mutex_lock(&dev
->struct_mutex
);
2012 ret
= i915_gpu_idle(dev
, true);
2014 DRM_ERROR("failed to idle hardware: %d\n", ret
);
2015 mutex_unlock(&dev
->struct_mutex
);
2017 /* Cancel the retire work handler, which should be idle now. */
2018 cancel_delayed_work_sync(&dev_priv
->mm
.retire_work
);
2020 io_mapping_free(dev_priv
->mm
.gtt_mapping
);
2021 if (dev_priv
->mm
.gtt_mtrr
>= 0) {
2022 mtrr_del(dev_priv
->mm
.gtt_mtrr
, dev
->agp
->base
,
2023 dev
->agp
->agp_info
.aper_size
* 1024 * 1024);
2024 dev_priv
->mm
.gtt_mtrr
= -1;
2027 acpi_video_unregister();
2029 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
2030 intel_fbdev_fini(dev
);
2031 intel_modeset_cleanup(dev
);
2034 * free the memory space allocated for the child device
2035 * config parsed from VBT
2037 if (dev_priv
->child_dev
&& dev_priv
->child_dev_num
) {
2038 kfree(dev_priv
->child_dev
);
2039 dev_priv
->child_dev
= NULL
;
2040 dev_priv
->child_dev_num
= 0;
2043 vga_switcheroo_unregister_client(dev
->pdev
);
2044 vga_client_register(dev
->pdev
, NULL
, NULL
, NULL
);
2047 /* Free error state after interrupts are fully disabled. */
2048 del_timer_sync(&dev_priv
->hangcheck_timer
);
2049 cancel_work_sync(&dev_priv
->error_work
);
2050 i915_destroy_error_state(dev
);
2052 if (dev
->pdev
->msi_enabled
)
2053 pci_disable_msi(dev
->pdev
);
2055 intel_opregion_fini(dev
);
2057 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
2058 /* Flush any outstanding unpin_work. */
2059 flush_workqueue(dev_priv
->wq
);
2061 mutex_lock(&dev
->struct_mutex
);
2062 i915_gem_free_all_phys_object(dev
);
2063 i915_gem_cleanup_ringbuffer(dev
);
2064 mutex_unlock(&dev
->struct_mutex
);
2065 i915_gem_cleanup_aliasing_ppgtt(dev
);
2066 i915_gem_cleanup_stolen(dev
);
2067 drm_mm_takedown(&dev_priv
->mm
.stolen
);
2069 intel_cleanup_overlay(dev
);
2071 if (!I915_NEED_GFX_HWS(dev
))
2075 if (dev_priv
->regs
!= NULL
)
2076 pci_iounmap(dev
->pdev
, dev_priv
->regs
);
2078 intel_teardown_gmbus(dev
);
2079 intel_teardown_mchbar(dev
);
2081 destroy_workqueue(dev_priv
->wq
);
2083 pci_dev_put(dev_priv
->bridge_dev
);
2084 kfree(dev
->dev_private
);
2089 int i915_driver_open(struct drm_device
*dev
, struct drm_file
*file
)
2091 struct drm_i915_file_private
*file_priv
;
2093 DRM_DEBUG_DRIVER("\n");
2094 file_priv
= kmalloc(sizeof(*file_priv
), GFP_KERNEL
);
2098 file
->driver_priv
= file_priv
;
2100 spin_lock_init(&file_priv
->mm
.lock
);
2101 INIT_LIST_HEAD(&file_priv
->mm
.request_list
);
2107 * i915_driver_lastclose - clean up after all DRM clients have exited
2110 * Take care of cleaning up after all DRM clients have exited. In the
2111 * mode setting case, we want to restore the kernel's initial mode (just
2112 * in case the last client left us in a bad state).
2114 * Additionally, in the non-mode setting case, we'll tear down the GTT
2115 * and DMA structures, since the kernel won't be using them, and clea
2118 void i915_driver_lastclose(struct drm_device
* dev
)
2120 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2122 if (!dev_priv
|| drm_core_check_feature(dev
, DRIVER_MODESET
)) {
2123 intel_fb_restore_mode(dev
);
2124 vga_switcheroo_process_delayed_switch();
2128 i915_gem_lastclose(dev
);
2130 i915_dma_cleanup(dev
);
2133 void i915_driver_preclose(struct drm_device
* dev
, struct drm_file
*file_priv
)
2135 i915_gem_release(dev
, file_priv
);
2138 void i915_driver_postclose(struct drm_device
*dev
, struct drm_file
*file
)
2140 struct drm_i915_file_private
*file_priv
= file
->driver_priv
;
2145 struct drm_ioctl_desc i915_ioctls
[] = {
2146 DRM_IOCTL_DEF_DRV(I915_INIT
, i915_dma_init
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
2147 DRM_IOCTL_DEF_DRV(I915_FLUSH
, i915_flush_ioctl
, DRM_AUTH
),
2148 DRM_IOCTL_DEF_DRV(I915_FLIP
, i915_flip_bufs
, DRM_AUTH
),
2149 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER
, i915_batchbuffer
, DRM_AUTH
),
2150 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT
, i915_irq_emit
, DRM_AUTH
),
2151 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT
, i915_irq_wait
, DRM_AUTH
),
2152 DRM_IOCTL_DEF_DRV(I915_GETPARAM
, i915_getparam
, DRM_AUTH
),
2153 DRM_IOCTL_DEF_DRV(I915_SETPARAM
, i915_setparam
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
2154 DRM_IOCTL_DEF_DRV(I915_ALLOC
, drm_noop
, DRM_AUTH
),
2155 DRM_IOCTL_DEF_DRV(I915_FREE
, drm_noop
, DRM_AUTH
),
2156 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP
, drm_noop
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
2157 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER
, i915_cmdbuffer
, DRM_AUTH
),
2158 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP
, drm_noop
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
2159 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE
, i915_vblank_pipe_set
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
2160 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE
, i915_vblank_pipe_get
, DRM_AUTH
),
2161 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP
, i915_vblank_swap
, DRM_AUTH
),
2162 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR
, i915_set_status_page
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
2163 DRM_IOCTL_DEF_DRV(I915_GEM_INIT
, i915_gem_init_ioctl
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
2164 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER
, i915_gem_execbuffer
, DRM_AUTH
|DRM_UNLOCKED
),
2165 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2
, i915_gem_execbuffer2
, DRM_AUTH
|DRM_UNLOCKED
),
2166 DRM_IOCTL_DEF_DRV(I915_GEM_PIN
, i915_gem_pin_ioctl
, DRM_AUTH
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
2167 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN
, i915_gem_unpin_ioctl
, DRM_AUTH
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
2168 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY
, i915_gem_busy_ioctl
, DRM_AUTH
|DRM_UNLOCKED
),
2169 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE
, i915_gem_throttle_ioctl
, DRM_AUTH
|DRM_UNLOCKED
),
2170 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT
, i915_gem_entervt_ioctl
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
2171 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT
, i915_gem_leavevt_ioctl
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
2172 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE
, i915_gem_create_ioctl
, DRM_UNLOCKED
),
2173 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD
, i915_gem_pread_ioctl
, DRM_UNLOCKED
),
2174 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE
, i915_gem_pwrite_ioctl
, DRM_UNLOCKED
),
2175 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP
, i915_gem_mmap_ioctl
, DRM_UNLOCKED
),
2176 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT
, i915_gem_mmap_gtt_ioctl
, DRM_UNLOCKED
),
2177 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN
, i915_gem_set_domain_ioctl
, DRM_UNLOCKED
),
2178 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH
, i915_gem_sw_finish_ioctl
, DRM_UNLOCKED
),
2179 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING
, i915_gem_set_tiling
, DRM_UNLOCKED
),
2180 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING
, i915_gem_get_tiling
, DRM_UNLOCKED
),
2181 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE
, i915_gem_get_aperture_ioctl
, DRM_UNLOCKED
),
2182 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID
, intel_get_pipe_from_crtc_id
, DRM_UNLOCKED
),
2183 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE
, i915_gem_madvise_ioctl
, DRM_UNLOCKED
),
2184 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE
, intel_overlay_put_image
, DRM_MASTER
|DRM_CONTROL_ALLOW
|DRM_UNLOCKED
),
2185 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS
, intel_overlay_attrs
, DRM_MASTER
|DRM_CONTROL_ALLOW
|DRM_UNLOCKED
),
2186 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY
, intel_sprite_set_colorkey
, DRM_MASTER
|DRM_CONTROL_ALLOW
|DRM_UNLOCKED
),
2187 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY
, intel_sprite_get_colorkey
, DRM_MASTER
|DRM_CONTROL_ALLOW
|DRM_UNLOCKED
),
2190 int i915_max_ioctl
= DRM_ARRAY_SIZE(i915_ioctls
);
2193 * This is really ugly: Because old userspace abused the linux agp interface to
2194 * manage the gtt, we need to claim that all intel devices are agp. For
2195 * otherwise the drm core refuses to initialize the agp support code.
2197 int i915_driver_device_is_agp(struct drm_device
* dev
)