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
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/drm_fb_helper.h>
34 #include "intel_drv.h"
35 #include <drm/i915_drm.h>
37 #include "i915_trace.h"
38 #include <linux/pci.h>
39 #include <linux/vgaarb.h>
40 #include <linux/acpi.h>
41 #include <linux/pnp.h>
42 #include <linux/vga_switcheroo.h>
43 #include <linux/slab.h>
44 #include <acpi/video.h>
47 #define LP_RING(d) (&((struct drm_i915_private *)(d))->ring[RCS])
49 #define BEGIN_LP_RING(n) \
50 intel_ring_begin(LP_RING(dev_priv), (n))
53 intel_ring_emit(LP_RING(dev_priv), x)
55 #define ADVANCE_LP_RING() \
56 intel_ring_advance(LP_RING(dev_priv))
59 * Lock test for when it's just for synchronization of ring access.
61 * In that case, we don't need to do it when GEM is initialized as nobody else
62 * has access to the ring.
64 #define RING_LOCK_TEST_WITH_RETURN(dev, file) do { \
65 if (LP_RING(dev->dev_private)->obj == NULL) \
66 LOCK_TEST_WITH_RETURN(dev, file); \
70 intel_read_legacy_status_page(struct drm_i915_private
*dev_priv
, int reg
)
72 if (I915_NEED_GFX_HWS(dev_priv
->dev
))
73 return ioread32(dev_priv
->dri1
.gfx_hws_cpu_addr
+ reg
);
75 return intel_read_status_page(LP_RING(dev_priv
), reg
);
78 #define READ_HWSP(dev_priv, reg) intel_read_legacy_status_page(dev_priv, reg)
79 #define READ_BREADCRUMB(dev_priv) READ_HWSP(dev_priv, I915_BREADCRUMB_INDEX)
80 #define I915_BREADCRUMB_INDEX 0x21
82 void i915_update_dri1_breadcrumb(struct drm_device
*dev
)
84 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
85 struct drm_i915_master_private
*master_priv
;
87 if (dev
->primary
->master
) {
88 master_priv
= dev
->primary
->master
->driver_priv
;
89 if (master_priv
->sarea_priv
)
90 master_priv
->sarea_priv
->last_dispatch
=
91 READ_BREADCRUMB(dev_priv
);
95 static void i915_write_hws_pga(struct drm_device
*dev
)
97 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
100 addr
= dev_priv
->status_page_dmah
->busaddr
;
101 if (INTEL_INFO(dev
)->gen
>= 4)
102 addr
|= (dev_priv
->status_page_dmah
->busaddr
>> 28) & 0xf0;
103 I915_WRITE(HWS_PGA
, addr
);
107 * Frees the hardware status page, whether it's a physical address or a virtual
108 * address set up by the X Server.
110 static void i915_free_hws(struct drm_device
*dev
)
112 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
113 struct intel_ring_buffer
*ring
= LP_RING(dev_priv
);
115 if (dev_priv
->status_page_dmah
) {
116 drm_pci_free(dev
, dev_priv
->status_page_dmah
);
117 dev_priv
->status_page_dmah
= NULL
;
120 if (ring
->status_page
.gfx_addr
) {
121 ring
->status_page
.gfx_addr
= 0;
122 iounmap(dev_priv
->dri1
.gfx_hws_cpu_addr
);
125 /* Need to rewrite hardware status page */
126 I915_WRITE(HWS_PGA
, 0x1ffff000);
129 void i915_kernel_lost_context(struct drm_device
* dev
)
131 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
132 struct drm_i915_master_private
*master_priv
;
133 struct intel_ring_buffer
*ring
= LP_RING(dev_priv
);
136 * We should never lose context on the ring with modesetting
137 * as we don't expose it to userspace
139 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
142 ring
->head
= I915_READ_HEAD(ring
) & HEAD_ADDR
;
143 ring
->tail
= I915_READ_TAIL(ring
) & TAIL_ADDR
;
144 ring
->space
= ring
->head
- (ring
->tail
+ I915_RING_FREE_SPACE
);
146 ring
->space
+= ring
->size
;
148 if (!dev
->primary
->master
)
151 master_priv
= dev
->primary
->master
->driver_priv
;
152 if (ring
->head
== ring
->tail
&& master_priv
->sarea_priv
)
153 master_priv
->sarea_priv
->perf_boxes
|= I915_BOX_RING_EMPTY
;
156 static int i915_dma_cleanup(struct drm_device
* dev
)
158 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
161 /* Make sure interrupts are disabled here because the uninstall ioctl
162 * may not have been called from userspace and after dev_private
163 * is freed, it's too late.
165 if (dev
->irq_enabled
)
166 drm_irq_uninstall(dev
);
168 mutex_lock(&dev
->struct_mutex
);
169 for (i
= 0; i
< I915_NUM_RINGS
; i
++)
170 intel_cleanup_ring_buffer(&dev_priv
->ring
[i
]);
171 mutex_unlock(&dev
->struct_mutex
);
173 /* Clear the HWS virtual address at teardown */
174 if (I915_NEED_GFX_HWS(dev
))
180 static int i915_initialize(struct drm_device
* dev
, drm_i915_init_t
* init
)
182 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
183 struct drm_i915_master_private
*master_priv
= dev
->primary
->master
->driver_priv
;
186 master_priv
->sarea
= drm_getsarea(dev
);
187 if (master_priv
->sarea
) {
188 master_priv
->sarea_priv
= (drm_i915_sarea_t
*)
189 ((u8
*)master_priv
->sarea
->handle
+ init
->sarea_priv_offset
);
191 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
194 if (init
->ring_size
!= 0) {
195 if (LP_RING(dev_priv
)->obj
!= NULL
) {
196 i915_dma_cleanup(dev
);
197 DRM_ERROR("Client tried to initialize ringbuffer in "
202 ret
= intel_render_ring_init_dri(dev
,
206 i915_dma_cleanup(dev
);
211 dev_priv
->dri1
.cpp
= init
->cpp
;
212 dev_priv
->dri1
.back_offset
= init
->back_offset
;
213 dev_priv
->dri1
.front_offset
= init
->front_offset
;
214 dev_priv
->dri1
.current_page
= 0;
215 if (master_priv
->sarea_priv
)
216 master_priv
->sarea_priv
->pf_current_page
= 0;
218 /* Allow hardware batchbuffers unless told otherwise.
220 dev_priv
->dri1
.allow_batchbuffer
= 1;
225 static int i915_dma_resume(struct drm_device
* dev
)
227 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
228 struct intel_ring_buffer
*ring
= LP_RING(dev_priv
);
230 DRM_DEBUG_DRIVER("%s\n", __func__
);
232 if (ring
->virtual_start
== NULL
) {
233 DRM_ERROR("can not ioremap virtual address for"
238 /* Program Hardware Status Page */
239 if (!ring
->status_page
.page_addr
) {
240 DRM_ERROR("Can not find hardware status page\n");
243 DRM_DEBUG_DRIVER("hw status page @ %p\n",
244 ring
->status_page
.page_addr
);
245 if (ring
->status_page
.gfx_addr
!= 0)
246 intel_ring_setup_status_page(ring
);
248 i915_write_hws_pga(dev
);
250 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
255 static int i915_dma_init(struct drm_device
*dev
, void *data
,
256 struct drm_file
*file_priv
)
258 drm_i915_init_t
*init
= data
;
261 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
264 switch (init
->func
) {
266 retcode
= i915_initialize(dev
, init
);
268 case I915_CLEANUP_DMA
:
269 retcode
= i915_dma_cleanup(dev
);
271 case I915_RESUME_DMA
:
272 retcode
= i915_dma_resume(dev
);
282 /* Implement basically the same security restrictions as hardware does
283 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
285 * Most of the calculations below involve calculating the size of a
286 * particular instruction. It's important to get the size right as
287 * that tells us where the next instruction to check is. Any illegal
288 * instruction detected will be given a size of zero, which is a
289 * signal to abort the rest of the buffer.
291 static int validate_cmd(int cmd
)
293 switch (((cmd
>> 29) & 0x7)) {
295 switch ((cmd
>> 23) & 0x3f) {
297 return 1; /* MI_NOOP */
299 return 1; /* MI_FLUSH */
301 return 0; /* disallow everything else */
305 return 0; /* reserved */
307 return (cmd
& 0xff) + 2; /* 2d commands */
309 if (((cmd
>> 24) & 0x1f) <= 0x18)
312 switch ((cmd
>> 24) & 0x1f) {
316 switch ((cmd
>> 16) & 0xff) {
318 return (cmd
& 0x1f) + 2;
320 return (cmd
& 0xf) + 2;
322 return (cmd
& 0xffff) + 2;
326 return (cmd
& 0xffff) + 1;
330 if ((cmd
& (1 << 23)) == 0) /* inline vertices */
331 return (cmd
& 0x1ffff) + 2;
332 else if (cmd
& (1 << 17)) /* indirect random */
333 if ((cmd
& 0xffff) == 0)
334 return 0; /* unknown length, too hard */
336 return (((cmd
& 0xffff) + 1) / 2) + 1;
338 return 2; /* indirect sequential */
349 static int i915_emit_cmds(struct drm_device
* dev
, int *buffer
, int dwords
)
351 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
354 if ((dwords
+1) * sizeof(int) >= LP_RING(dev_priv
)->size
- 8)
357 for (i
= 0; i
< dwords
;) {
358 int sz
= validate_cmd(buffer
[i
]);
359 if (sz
== 0 || i
+ sz
> dwords
)
364 ret
= BEGIN_LP_RING((dwords
+1)&~1);
368 for (i
= 0; i
< dwords
; i
++)
379 i915_emit_box(struct drm_device
*dev
,
380 struct drm_clip_rect
*box
,
383 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
386 if (box
->y2
<= box
->y1
|| box
->x2
<= box
->x1
||
387 box
->y2
<= 0 || box
->x2
<= 0) {
388 DRM_ERROR("Bad box %d,%d..%d,%d\n",
389 box
->x1
, box
->y1
, box
->x2
, box
->y2
);
393 if (INTEL_INFO(dev
)->gen
>= 4) {
394 ret
= BEGIN_LP_RING(4);
398 OUT_RING(GFX_OP_DRAWRECT_INFO_I965
);
399 OUT_RING((box
->x1
& 0xffff) | (box
->y1
<< 16));
400 OUT_RING(((box
->x2
- 1) & 0xffff) | ((box
->y2
- 1) << 16));
403 ret
= BEGIN_LP_RING(6);
407 OUT_RING(GFX_OP_DRAWRECT_INFO
);
409 OUT_RING((box
->x1
& 0xffff) | (box
->y1
<< 16));
410 OUT_RING(((box
->x2
- 1) & 0xffff) | ((box
->y2
- 1) << 16));
419 /* XXX: Emitting the counter should really be moved to part of the IRQ
420 * emit. For now, do it in both places:
423 static void i915_emit_breadcrumb(struct drm_device
*dev
)
425 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
426 struct drm_i915_master_private
*master_priv
= dev
->primary
->master
->driver_priv
;
428 dev_priv
->dri1
.counter
++;
429 if (dev_priv
->dri1
.counter
> 0x7FFFFFFFUL
)
430 dev_priv
->dri1
.counter
= 0;
431 if (master_priv
->sarea_priv
)
432 master_priv
->sarea_priv
->last_enqueue
= dev_priv
->dri1
.counter
;
434 if (BEGIN_LP_RING(4) == 0) {
435 OUT_RING(MI_STORE_DWORD_INDEX
);
436 OUT_RING(I915_BREADCRUMB_INDEX
<< MI_STORE_DWORD_INDEX_SHIFT
);
437 OUT_RING(dev_priv
->dri1
.counter
);
443 static int i915_dispatch_cmdbuffer(struct drm_device
* dev
,
444 drm_i915_cmdbuffer_t
*cmd
,
445 struct drm_clip_rect
*cliprects
,
448 int nbox
= cmd
->num_cliprects
;
449 int i
= 0, count
, ret
;
452 DRM_ERROR("alignment");
456 i915_kernel_lost_context(dev
);
458 count
= nbox
? nbox
: 1;
460 for (i
= 0; i
< count
; i
++) {
462 ret
= i915_emit_box(dev
, &cliprects
[i
],
468 ret
= i915_emit_cmds(dev
, cmdbuf
, cmd
->sz
/ 4);
473 i915_emit_breadcrumb(dev
);
477 static int i915_dispatch_batchbuffer(struct drm_device
* dev
,
478 drm_i915_batchbuffer_t
* batch
,
479 struct drm_clip_rect
*cliprects
)
481 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
482 int nbox
= batch
->num_cliprects
;
485 if ((batch
->start
| batch
->used
) & 0x7) {
486 DRM_ERROR("alignment");
490 i915_kernel_lost_context(dev
);
492 count
= nbox
? nbox
: 1;
493 for (i
= 0; i
< count
; i
++) {
495 ret
= i915_emit_box(dev
, &cliprects
[i
],
496 batch
->DR1
, batch
->DR4
);
501 if (!IS_I830(dev
) && !IS_845G(dev
)) {
502 ret
= BEGIN_LP_RING(2);
506 if (INTEL_INFO(dev
)->gen
>= 4) {
507 OUT_RING(MI_BATCH_BUFFER_START
| (2 << 6) | MI_BATCH_NON_SECURE_I965
);
508 OUT_RING(batch
->start
);
510 OUT_RING(MI_BATCH_BUFFER_START
| (2 << 6));
511 OUT_RING(batch
->start
| MI_BATCH_NON_SECURE
);
514 ret
= BEGIN_LP_RING(4);
518 OUT_RING(MI_BATCH_BUFFER
);
519 OUT_RING(batch
->start
| MI_BATCH_NON_SECURE
);
520 OUT_RING(batch
->start
+ batch
->used
- 4);
527 if (IS_G4X(dev
) || IS_GEN5(dev
)) {
528 if (BEGIN_LP_RING(2) == 0) {
529 OUT_RING(MI_FLUSH
| MI_NO_WRITE_FLUSH
| MI_INVALIDATE_ISP
);
535 i915_emit_breadcrumb(dev
);
539 static int i915_dispatch_flip(struct drm_device
* dev
)
541 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
542 struct drm_i915_master_private
*master_priv
=
543 dev
->primary
->master
->driver_priv
;
546 if (!master_priv
->sarea_priv
)
549 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
551 dev_priv
->dri1
.current_page
,
552 master_priv
->sarea_priv
->pf_current_page
);
554 i915_kernel_lost_context(dev
);
556 ret
= BEGIN_LP_RING(10);
560 OUT_RING(MI_FLUSH
| MI_READ_FLUSH
);
563 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO
| ASYNC_FLIP
);
565 if (dev_priv
->dri1
.current_page
== 0) {
566 OUT_RING(dev_priv
->dri1
.back_offset
);
567 dev_priv
->dri1
.current_page
= 1;
569 OUT_RING(dev_priv
->dri1
.front_offset
);
570 dev_priv
->dri1
.current_page
= 0;
574 OUT_RING(MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_A_FLIP
);
579 master_priv
->sarea_priv
->last_enqueue
= dev_priv
->dri1
.counter
++;
581 if (BEGIN_LP_RING(4) == 0) {
582 OUT_RING(MI_STORE_DWORD_INDEX
);
583 OUT_RING(I915_BREADCRUMB_INDEX
<< MI_STORE_DWORD_INDEX_SHIFT
);
584 OUT_RING(dev_priv
->dri1
.counter
);
589 master_priv
->sarea_priv
->pf_current_page
= dev_priv
->dri1
.current_page
;
593 static int i915_quiescent(struct drm_device
*dev
)
595 i915_kernel_lost_context(dev
);
596 return intel_ring_idle(LP_RING(dev
->dev_private
));
599 static int i915_flush_ioctl(struct drm_device
*dev
, void *data
,
600 struct drm_file
*file_priv
)
604 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
607 RING_LOCK_TEST_WITH_RETURN(dev
, file_priv
);
609 mutex_lock(&dev
->struct_mutex
);
610 ret
= i915_quiescent(dev
);
611 mutex_unlock(&dev
->struct_mutex
);
616 static int i915_batchbuffer(struct drm_device
*dev
, void *data
,
617 struct drm_file
*file_priv
)
619 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
620 struct drm_i915_master_private
*master_priv
= dev
->primary
->master
->driver_priv
;
621 drm_i915_sarea_t
*sarea_priv
= (drm_i915_sarea_t
*)
622 master_priv
->sarea_priv
;
623 drm_i915_batchbuffer_t
*batch
= data
;
625 struct drm_clip_rect
*cliprects
= NULL
;
627 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
630 if (!dev_priv
->dri1
.allow_batchbuffer
) {
631 DRM_ERROR("Batchbuffer ioctl disabled\n");
635 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
636 batch
->start
, batch
->used
, batch
->num_cliprects
);
638 RING_LOCK_TEST_WITH_RETURN(dev
, file_priv
);
640 if (batch
->num_cliprects
< 0)
643 if (batch
->num_cliprects
) {
644 cliprects
= kcalloc(batch
->num_cliprects
,
645 sizeof(struct drm_clip_rect
),
647 if (cliprects
== NULL
)
650 ret
= copy_from_user(cliprects
, batch
->cliprects
,
651 batch
->num_cliprects
*
652 sizeof(struct drm_clip_rect
));
659 mutex_lock(&dev
->struct_mutex
);
660 ret
= i915_dispatch_batchbuffer(dev
, batch
, cliprects
);
661 mutex_unlock(&dev
->struct_mutex
);
664 sarea_priv
->last_dispatch
= READ_BREADCRUMB(dev_priv
);
672 static int i915_cmdbuffer(struct drm_device
*dev
, void *data
,
673 struct drm_file
*file_priv
)
675 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
676 struct drm_i915_master_private
*master_priv
= dev
->primary
->master
->driver_priv
;
677 drm_i915_sarea_t
*sarea_priv
= (drm_i915_sarea_t
*)
678 master_priv
->sarea_priv
;
679 drm_i915_cmdbuffer_t
*cmdbuf
= data
;
680 struct drm_clip_rect
*cliprects
= NULL
;
684 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
685 cmdbuf
->buf
, cmdbuf
->sz
, cmdbuf
->num_cliprects
);
687 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
690 RING_LOCK_TEST_WITH_RETURN(dev
, file_priv
);
692 if (cmdbuf
->num_cliprects
< 0)
695 batch_data
= kmalloc(cmdbuf
->sz
, GFP_KERNEL
);
696 if (batch_data
== NULL
)
699 ret
= copy_from_user(batch_data
, cmdbuf
->buf
, cmdbuf
->sz
);
702 goto fail_batch_free
;
705 if (cmdbuf
->num_cliprects
) {
706 cliprects
= kcalloc(cmdbuf
->num_cliprects
,
707 sizeof(struct drm_clip_rect
), GFP_KERNEL
);
708 if (cliprects
== NULL
) {
710 goto fail_batch_free
;
713 ret
= copy_from_user(cliprects
, cmdbuf
->cliprects
,
714 cmdbuf
->num_cliprects
*
715 sizeof(struct drm_clip_rect
));
722 mutex_lock(&dev
->struct_mutex
);
723 ret
= i915_dispatch_cmdbuffer(dev
, cmdbuf
, cliprects
, batch_data
);
724 mutex_unlock(&dev
->struct_mutex
);
726 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
731 sarea_priv
->last_dispatch
= READ_BREADCRUMB(dev_priv
);
741 static int i915_emit_irq(struct drm_device
* dev
)
743 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
744 struct drm_i915_master_private
*master_priv
= dev
->primary
->master
->driver_priv
;
746 i915_kernel_lost_context(dev
);
748 DRM_DEBUG_DRIVER("\n");
750 dev_priv
->dri1
.counter
++;
751 if (dev_priv
->dri1
.counter
> 0x7FFFFFFFUL
)
752 dev_priv
->dri1
.counter
= 1;
753 if (master_priv
->sarea_priv
)
754 master_priv
->sarea_priv
->last_enqueue
= dev_priv
->dri1
.counter
;
756 if (BEGIN_LP_RING(4) == 0) {
757 OUT_RING(MI_STORE_DWORD_INDEX
);
758 OUT_RING(I915_BREADCRUMB_INDEX
<< MI_STORE_DWORD_INDEX_SHIFT
);
759 OUT_RING(dev_priv
->dri1
.counter
);
760 OUT_RING(MI_USER_INTERRUPT
);
764 return dev_priv
->dri1
.counter
;
767 static int i915_wait_irq(struct drm_device
* dev
, int irq_nr
)
769 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
770 struct drm_i915_master_private
*master_priv
= dev
->primary
->master
->driver_priv
;
772 struct intel_ring_buffer
*ring
= LP_RING(dev_priv
);
774 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr
,
775 READ_BREADCRUMB(dev_priv
));
777 if (READ_BREADCRUMB(dev_priv
) >= irq_nr
) {
778 if (master_priv
->sarea_priv
)
779 master_priv
->sarea_priv
->last_dispatch
= READ_BREADCRUMB(dev_priv
);
783 if (master_priv
->sarea_priv
)
784 master_priv
->sarea_priv
->perf_boxes
|= I915_BOX_WAIT
;
786 if (ring
->irq_get(ring
)) {
787 DRM_WAIT_ON(ret
, ring
->irq_queue
, 3 * DRM_HZ
,
788 READ_BREADCRUMB(dev_priv
) >= irq_nr
);
790 } else if (wait_for(READ_BREADCRUMB(dev_priv
) >= irq_nr
, 3000))
794 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
795 READ_BREADCRUMB(dev_priv
), (int)dev_priv
->dri1
.counter
);
801 /* Needs the lock as it touches the ring.
803 static int i915_irq_emit(struct drm_device
*dev
, void *data
,
804 struct drm_file
*file_priv
)
806 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
807 drm_i915_irq_emit_t
*emit
= data
;
810 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
813 if (!dev_priv
|| !LP_RING(dev_priv
)->virtual_start
) {
814 DRM_ERROR("called with no initialization\n");
818 RING_LOCK_TEST_WITH_RETURN(dev
, file_priv
);
820 mutex_lock(&dev
->struct_mutex
);
821 result
= i915_emit_irq(dev
);
822 mutex_unlock(&dev
->struct_mutex
);
824 if (DRM_COPY_TO_USER(emit
->irq_seq
, &result
, sizeof(int))) {
825 DRM_ERROR("copy_to_user\n");
832 /* Doesn't need the hardware lock.
834 static int i915_irq_wait(struct drm_device
*dev
, void *data
,
835 struct drm_file
*file_priv
)
837 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
838 drm_i915_irq_wait_t
*irqwait
= data
;
840 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
844 DRM_ERROR("called with no initialization\n");
848 return i915_wait_irq(dev
, irqwait
->irq_seq
);
851 static int i915_vblank_pipe_get(struct drm_device
*dev
, void *data
,
852 struct drm_file
*file_priv
)
854 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
855 drm_i915_vblank_pipe_t
*pipe
= data
;
857 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
861 DRM_ERROR("called with no initialization\n");
865 pipe
->pipe
= DRM_I915_VBLANK_PIPE_A
| DRM_I915_VBLANK_PIPE_B
;
871 * Schedule buffer swap at given vertical blank.
873 static int i915_vblank_swap(struct drm_device
*dev
, void *data
,
874 struct drm_file
*file_priv
)
876 /* The delayed swap mechanism was fundamentally racy, and has been
877 * removed. The model was that the client requested a delayed flip/swap
878 * from the kernel, then waited for vblank before continuing to perform
879 * rendering. The problem was that the kernel might wake the client
880 * up before it dispatched the vblank swap (since the lock has to be
881 * held while touching the ringbuffer), in which case the client would
882 * clear and start the next frame before the swap occurred, and
883 * flicker would occur in addition to likely missing the vblank.
885 * In the absence of this ioctl, userland falls back to a correct path
886 * of waiting for a vblank, then dispatching the swap on its own.
887 * Context switching to userland and back is plenty fast enough for
888 * meeting the requirements of vblank swapping.
893 static int i915_flip_bufs(struct drm_device
*dev
, void *data
,
894 struct drm_file
*file_priv
)
898 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
901 DRM_DEBUG_DRIVER("%s\n", __func__
);
903 RING_LOCK_TEST_WITH_RETURN(dev
, file_priv
);
905 mutex_lock(&dev
->struct_mutex
);
906 ret
= i915_dispatch_flip(dev
);
907 mutex_unlock(&dev
->struct_mutex
);
912 static int i915_getparam(struct drm_device
*dev
, void *data
,
913 struct drm_file
*file_priv
)
915 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
916 drm_i915_getparam_t
*param
= data
;
920 DRM_ERROR("called with no initialization\n");
924 switch (param
->param
) {
925 case I915_PARAM_IRQ_ACTIVE
:
926 value
= dev
->pdev
->irq
? 1 : 0;
928 case I915_PARAM_ALLOW_BATCHBUFFER
:
929 value
= dev_priv
->dri1
.allow_batchbuffer
? 1 : 0;
931 case I915_PARAM_LAST_DISPATCH
:
932 value
= READ_BREADCRUMB(dev_priv
);
934 case I915_PARAM_CHIPSET_ID
:
935 value
= dev
->pci_device
;
937 case I915_PARAM_HAS_GEM
:
940 case I915_PARAM_NUM_FENCES_AVAIL
:
941 value
= dev_priv
->num_fence_regs
- dev_priv
->fence_reg_start
;
943 case I915_PARAM_HAS_OVERLAY
:
944 value
= dev_priv
->overlay
? 1 : 0;
946 case I915_PARAM_HAS_PAGEFLIPPING
:
949 case I915_PARAM_HAS_EXECBUF2
:
953 case I915_PARAM_HAS_BSD
:
954 value
= intel_ring_initialized(&dev_priv
->ring
[VCS
]);
956 case I915_PARAM_HAS_BLT
:
957 value
= intel_ring_initialized(&dev_priv
->ring
[BCS
]);
959 case I915_PARAM_HAS_VEBOX
:
960 value
= intel_ring_initialized(&dev_priv
->ring
[VECS
]);
962 case I915_PARAM_HAS_RELAXED_FENCING
:
965 case I915_PARAM_HAS_COHERENT_RINGS
:
968 case I915_PARAM_HAS_EXEC_CONSTANTS
:
969 value
= INTEL_INFO(dev
)->gen
>= 4;
971 case I915_PARAM_HAS_RELAXED_DELTA
:
974 case I915_PARAM_HAS_GEN7_SOL_RESET
:
977 case I915_PARAM_HAS_LLC
:
978 value
= HAS_LLC(dev
);
980 case I915_PARAM_HAS_ALIASING_PPGTT
:
981 value
= dev_priv
->mm
.aliasing_ppgtt
? 1 : 0;
983 case I915_PARAM_HAS_WAIT_TIMEOUT
:
986 case I915_PARAM_HAS_SEMAPHORES
:
987 value
= i915_semaphore_is_enabled(dev
);
989 case I915_PARAM_HAS_PRIME_VMAP_FLUSH
:
992 case I915_PARAM_HAS_SECURE_BATCHES
:
993 value
= capable(CAP_SYS_ADMIN
);
995 case I915_PARAM_HAS_PINNED_BATCHES
:
998 case I915_PARAM_HAS_EXEC_NO_RELOC
:
1001 case I915_PARAM_HAS_EXEC_HANDLE_LUT
:
1005 DRM_DEBUG("Unknown parameter %d\n", param
->param
);
1009 if (DRM_COPY_TO_USER(param
->value
, &value
, sizeof(int))) {
1010 DRM_ERROR("DRM_COPY_TO_USER failed\n");
1017 static int i915_setparam(struct drm_device
*dev
, void *data
,
1018 struct drm_file
*file_priv
)
1020 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1021 drm_i915_setparam_t
*param
= data
;
1024 DRM_ERROR("called with no initialization\n");
1028 switch (param
->param
) {
1029 case I915_SETPARAM_USE_MI_BATCHBUFFER_START
:
1031 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY
:
1033 case I915_SETPARAM_ALLOW_BATCHBUFFER
:
1034 dev_priv
->dri1
.allow_batchbuffer
= param
->value
? 1 : 0;
1036 case I915_SETPARAM_NUM_USED_FENCES
:
1037 if (param
->value
> dev_priv
->num_fence_regs
||
1040 /* Userspace can use first N regs */
1041 dev_priv
->fence_reg_start
= param
->value
;
1044 DRM_DEBUG_DRIVER("unknown parameter %d\n",
1052 static int i915_set_status_page(struct drm_device
*dev
, void *data
,
1053 struct drm_file
*file_priv
)
1055 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1056 drm_i915_hws_addr_t
*hws
= data
;
1057 struct intel_ring_buffer
*ring
;
1059 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
1062 if (!I915_NEED_GFX_HWS(dev
))
1066 DRM_ERROR("called with no initialization\n");
1070 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
1071 WARN(1, "tried to set status page when mode setting active\n");
1075 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32
)hws
->addr
);
1077 ring
= LP_RING(dev_priv
);
1078 ring
->status_page
.gfx_addr
= hws
->addr
& (0x1ffff<<12);
1080 dev_priv
->dri1
.gfx_hws_cpu_addr
=
1081 ioremap_wc(dev_priv
->gtt
.mappable_base
+ hws
->addr
, 4096);
1082 if (dev_priv
->dri1
.gfx_hws_cpu_addr
== NULL
) {
1083 i915_dma_cleanup(dev
);
1084 ring
->status_page
.gfx_addr
= 0;
1085 DRM_ERROR("can not ioremap virtual address for"
1086 " G33 hw status page\n");
1090 memset_io(dev_priv
->dri1
.gfx_hws_cpu_addr
, 0, PAGE_SIZE
);
1091 I915_WRITE(HWS_PGA
, ring
->status_page
.gfx_addr
);
1093 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
1094 ring
->status_page
.gfx_addr
);
1095 DRM_DEBUG_DRIVER("load hws at %p\n",
1096 ring
->status_page
.page_addr
);
1100 static int i915_get_bridge_dev(struct drm_device
*dev
)
1102 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1104 dev_priv
->bridge_dev
= pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
1105 if (!dev_priv
->bridge_dev
) {
1106 DRM_ERROR("bridge device not found\n");
1112 #define MCHBAR_I915 0x44
1113 #define MCHBAR_I965 0x48
1114 #define MCHBAR_SIZE (4*4096)
1116 #define DEVEN_REG 0x54
1117 #define DEVEN_MCHBAR_EN (1 << 28)
1119 /* Allocate space for the MCH regs if needed, return nonzero on error */
1121 intel_alloc_mchbar_resource(struct drm_device
*dev
)
1123 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1124 int reg
= INTEL_INFO(dev
)->gen
>= 4 ? MCHBAR_I965
: MCHBAR_I915
;
1125 u32 temp_lo
, temp_hi
= 0;
1129 if (INTEL_INFO(dev
)->gen
>= 4)
1130 pci_read_config_dword(dev_priv
->bridge_dev
, reg
+ 4, &temp_hi
);
1131 pci_read_config_dword(dev_priv
->bridge_dev
, reg
, &temp_lo
);
1132 mchbar_addr
= ((u64
)temp_hi
<< 32) | temp_lo
;
1134 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
1137 pnp_range_reserved(mchbar_addr
, mchbar_addr
+ MCHBAR_SIZE
))
1141 /* Get some space for it */
1142 dev_priv
->mch_res
.name
= "i915 MCHBAR";
1143 dev_priv
->mch_res
.flags
= IORESOURCE_MEM
;
1144 ret
= pci_bus_alloc_resource(dev_priv
->bridge_dev
->bus
,
1146 MCHBAR_SIZE
, MCHBAR_SIZE
,
1148 0, pcibios_align_resource
,
1149 dev_priv
->bridge_dev
);
1151 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret
);
1152 dev_priv
->mch_res
.start
= 0;
1156 if (INTEL_INFO(dev
)->gen
>= 4)
1157 pci_write_config_dword(dev_priv
->bridge_dev
, reg
+ 4,
1158 upper_32_bits(dev_priv
->mch_res
.start
));
1160 pci_write_config_dword(dev_priv
->bridge_dev
, reg
,
1161 lower_32_bits(dev_priv
->mch_res
.start
));
1165 /* Setup MCHBAR if possible, return true if we should disable it again */
1167 intel_setup_mchbar(struct drm_device
*dev
)
1169 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1170 int mchbar_reg
= INTEL_INFO(dev
)->gen
>= 4 ? MCHBAR_I965
: MCHBAR_I915
;
1174 dev_priv
->mchbar_need_disable
= false;
1176 if (IS_I915G(dev
) || IS_I915GM(dev
)) {
1177 pci_read_config_dword(dev_priv
->bridge_dev
, DEVEN_REG
, &temp
);
1178 enabled
= !!(temp
& DEVEN_MCHBAR_EN
);
1180 pci_read_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, &temp
);
1184 /* If it's already enabled, don't have to do anything */
1188 if (intel_alloc_mchbar_resource(dev
))
1191 dev_priv
->mchbar_need_disable
= true;
1193 /* Space is allocated or reserved, so enable it. */
1194 if (IS_I915G(dev
) || IS_I915GM(dev
)) {
1195 pci_write_config_dword(dev_priv
->bridge_dev
, DEVEN_REG
,
1196 temp
| DEVEN_MCHBAR_EN
);
1198 pci_read_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, &temp
);
1199 pci_write_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, temp
| 1);
1204 intel_teardown_mchbar(struct drm_device
*dev
)
1206 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1207 int mchbar_reg
= INTEL_INFO(dev
)->gen
>= 4 ? MCHBAR_I965
: MCHBAR_I915
;
1210 if (dev_priv
->mchbar_need_disable
) {
1211 if (IS_I915G(dev
) || IS_I915GM(dev
)) {
1212 pci_read_config_dword(dev_priv
->bridge_dev
, DEVEN_REG
, &temp
);
1213 temp
&= ~DEVEN_MCHBAR_EN
;
1214 pci_write_config_dword(dev_priv
->bridge_dev
, DEVEN_REG
, temp
);
1216 pci_read_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, &temp
);
1218 pci_write_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, temp
);
1222 if (dev_priv
->mch_res
.start
)
1223 release_resource(&dev_priv
->mch_res
);
1226 /* true = enable decode, false = disable decoder */
1227 static unsigned int i915_vga_set_decode(void *cookie
, bool state
)
1229 struct drm_device
*dev
= cookie
;
1231 intel_modeset_vga_set_state(dev
, state
);
1233 return VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
|
1234 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
1236 return VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
1239 static void i915_switcheroo_set_state(struct pci_dev
*pdev
, enum vga_switcheroo_state state
)
1241 struct drm_device
*dev
= pci_get_drvdata(pdev
);
1242 pm_message_t pmm
= { .event
= PM_EVENT_SUSPEND
};
1243 if (state
== VGA_SWITCHEROO_ON
) {
1244 pr_info("switched on\n");
1245 dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
1246 /* i915 resume handler doesn't set to D0 */
1247 pci_set_power_state(dev
->pdev
, PCI_D0
);
1249 dev
->switch_power_state
= DRM_SWITCH_POWER_ON
;
1251 pr_err("switched off\n");
1252 dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
1253 i915_suspend(dev
, pmm
);
1254 dev
->switch_power_state
= DRM_SWITCH_POWER_OFF
;
1258 static bool i915_switcheroo_can_switch(struct pci_dev
*pdev
)
1260 struct drm_device
*dev
= pci_get_drvdata(pdev
);
1263 spin_lock(&dev
->count_lock
);
1264 can_switch
= (dev
->open_count
== 0);
1265 spin_unlock(&dev
->count_lock
);
1269 static const struct vga_switcheroo_client_ops i915_switcheroo_ops
= {
1270 .set_gpu_state
= i915_switcheroo_set_state
,
1272 .can_switch
= i915_switcheroo_can_switch
,
1275 static int i915_load_modeset_init(struct drm_device
*dev
)
1277 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1280 ret
= intel_parse_bios(dev
);
1282 DRM_INFO("failed to find VBIOS tables\n");
1284 /* If we have > 1 VGA cards, then we need to arbitrate access
1285 * to the common VGA resources.
1287 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1288 * then we do not take part in VGA arbitration and the
1289 * vga_client_register() fails with -ENODEV.
1291 ret
= vga_client_register(dev
->pdev
, dev
, NULL
, i915_vga_set_decode
);
1292 if (ret
&& ret
!= -ENODEV
)
1295 intel_register_dsm_handler();
1297 ret
= vga_switcheroo_register_client(dev
->pdev
, &i915_switcheroo_ops
);
1299 goto cleanup_vga_client
;
1301 /* Initialise stolen first so that we may reserve preallocated
1302 * objects for the BIOS to KMS transition.
1304 ret
= i915_gem_init_stolen(dev
);
1306 goto cleanup_vga_switcheroo
;
1308 ret
= drm_irq_install(dev
);
1310 goto cleanup_gem_stolen
;
1312 /* Important: The output setup functions called by modeset_init need
1313 * working irqs for e.g. gmbus and dp aux transfers. */
1314 intel_modeset_init(dev
);
1316 ret
= i915_gem_init(dev
);
1320 INIT_WORK(&dev_priv
->console_resume_work
, intel_console_resume
);
1322 intel_modeset_gem_init(dev
);
1324 /* Always safe in the mode setting case. */
1325 /* FIXME: do pre/post-mode set stuff in core KMS code */
1326 dev
->vblank_disable_allowed
= 1;
1327 if (INTEL_INFO(dev
)->num_pipes
== 0) {
1328 dev_priv
->mm
.suspended
= 0;
1332 ret
= intel_fbdev_init(dev
);
1336 /* Only enable hotplug handling once the fbdev is fully set up. */
1337 intel_hpd_init(dev
);
1340 * Some ports require correctly set-up hpd registers for detection to
1341 * work properly (leading to ghost connected connector status), e.g. VGA
1342 * on gm45. Hence we can only set up the initial fbdev config after hpd
1343 * irqs are fully enabled. Now we should scan for the initial config
1344 * only once hotplug handling is enabled, but due to screwed-up locking
1345 * around kms/fbdev init we can't protect the fdbev initial config
1346 * scanning against hotplug events. Hence do this first and ignore the
1347 * tiny window where we will loose hotplug notifactions.
1349 intel_fbdev_initial_config(dev
);
1351 /* Only enable hotplug handling once the fbdev is fully set up. */
1352 dev_priv
->enable_hotplug_processing
= true;
1354 drm_kms_helper_poll_init(dev
);
1356 /* We're off and running w/KMS */
1357 dev_priv
->mm
.suspended
= 0;
1362 mutex_lock(&dev
->struct_mutex
);
1363 i915_gem_cleanup_ringbuffer(dev
);
1364 i915_gem_context_fini(dev
);
1365 mutex_unlock(&dev
->struct_mutex
);
1366 i915_gem_cleanup_aliasing_ppgtt(dev
);
1367 drm_mm_takedown(&dev_priv
->mm
.gtt_space
);
1369 drm_irq_uninstall(dev
);
1371 i915_gem_cleanup_stolen(dev
);
1372 cleanup_vga_switcheroo
:
1373 vga_switcheroo_unregister_client(dev
->pdev
);
1375 vga_client_register(dev
->pdev
, NULL
, NULL
, NULL
);
1380 int i915_master_create(struct drm_device
*dev
, struct drm_master
*master
)
1382 struct drm_i915_master_private
*master_priv
;
1384 master_priv
= kzalloc(sizeof(*master_priv
), GFP_KERNEL
);
1388 master
->driver_priv
= master_priv
;
1392 void i915_master_destroy(struct drm_device
*dev
, struct drm_master
*master
)
1394 struct drm_i915_master_private
*master_priv
= master
->driver_priv
;
1401 master
->driver_priv
= NULL
;
1405 i915_mtrr_setup(struct drm_i915_private
*dev_priv
, unsigned long base
,
1408 dev_priv
->mm
.gtt_mtrr
= -1;
1410 #if defined(CONFIG_X86_PAT)
1415 /* Set up a WC MTRR for non-PAT systems. This is more common than
1416 * one would think, because the kernel disables PAT on first
1417 * generation Core chips because WC PAT gets overridden by a UC
1418 * MTRR if present. Even if a UC MTRR isn't present.
1420 dev_priv
->mm
.gtt_mtrr
= mtrr_add(base
, size
, MTRR_TYPE_WRCOMB
, 1);
1421 if (dev_priv
->mm
.gtt_mtrr
< 0) {
1422 DRM_INFO("MTRR allocation failed. Graphics "
1423 "performance may suffer.\n");
1427 static void i915_kick_out_firmware_fb(struct drm_i915_private
*dev_priv
)
1429 struct apertures_struct
*ap
;
1430 struct pci_dev
*pdev
= dev_priv
->dev
->pdev
;
1433 ap
= alloc_apertures(1);
1437 ap
->ranges
[0].base
= dev_priv
->gtt
.mappable_base
;
1438 ap
->ranges
[0].size
= dev_priv
->gtt
.mappable_end
;
1441 pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
;
1443 remove_conflicting_framebuffers(ap
, "inteldrmfb", primary
);
1448 static void i915_dump_device_info(struct drm_i915_private
*dev_priv
)
1450 const struct intel_device_info
*info
= dev_priv
->info
;
1452 #define PRINT_S(name) "%s"
1454 #define PRINT_FLAG(name) info->name ? #name "," : ""
1456 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x flags="
1457 DEV_INFO_FOR_EACH_FLAG(PRINT_S
, SEP_EMPTY
),
1459 dev_priv
->dev
->pdev
->device
,
1460 DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG
, SEP_COMMA
));
1468 * intel_early_sanitize_regs - clean up BIOS state
1471 * This function must be called before we do any I915_READ or I915_WRITE. Its
1472 * purpose is to clean up any state left by the BIOS that may affect us when
1473 * reading and/or writing registers.
1475 static void intel_early_sanitize_regs(struct drm_device
*dev
)
1477 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1479 if (HAS_FPGA_DBG_UNCLAIMED(dev
))
1480 I915_WRITE_NOTRACE(FPGA_DBG
, FPGA_DBG_RM_NOCLAIM
);
1484 * i915_driver_load - setup chip and create an initial config
1486 * @flags: startup flags
1488 * The driver load routine has to do several things:
1489 * - drive output discovery via intel_modeset_init()
1490 * - initialize the memory manager
1491 * - allocate initial config memory
1492 * - setup the DRM framebuffer with the allocated memory
1494 int i915_driver_load(struct drm_device
*dev
, unsigned long flags
)
1496 struct drm_i915_private
*dev_priv
;
1497 struct intel_device_info
*info
;
1498 int ret
= 0, mmio_bar
, mmio_size
;
1499 uint32_t aperture_size
;
1501 info
= (struct intel_device_info
*) flags
;
1503 /* Refuse to load on gen6+ without kms enabled. */
1504 if (info
->gen
>= 6 && !drm_core_check_feature(dev
, DRIVER_MODESET
))
1507 /* i915 has 4 more counters */
1509 dev
->types
[6] = _DRM_STAT_IRQ
;
1510 dev
->types
[7] = _DRM_STAT_PRIMARY
;
1511 dev
->types
[8] = _DRM_STAT_SECONDARY
;
1512 dev
->types
[9] = _DRM_STAT_DMA
;
1514 dev_priv
= kzalloc(sizeof(drm_i915_private_t
), GFP_KERNEL
);
1515 if (dev_priv
== NULL
)
1518 dev
->dev_private
= (void *)dev_priv
;
1519 dev_priv
->dev
= dev
;
1520 dev_priv
->info
= info
;
1522 i915_dump_device_info(dev_priv
);
1524 if (i915_get_bridge_dev(dev
)) {
1529 mmio_bar
= IS_GEN2(dev
) ? 1 : 0;
1530 /* Before gen4, the registers and the GTT are behind different BARs.
1531 * However, from gen4 onwards, the registers and the GTT are shared
1532 * in the same BAR, so we want to restrict this ioremap from
1533 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
1534 * the register BAR remains the same size for all the earlier
1535 * generations up to Ironlake.
1538 mmio_size
= 512*1024;
1540 mmio_size
= 2*1024*1024;
1542 dev_priv
->regs
= pci_iomap(dev
->pdev
, mmio_bar
, mmio_size
);
1543 if (!dev_priv
->regs
) {
1544 DRM_ERROR("failed to map registers\n");
1549 intel_early_sanitize_regs(dev
);
1551 ret
= i915_gem_gtt_init(dev
);
1555 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
1556 i915_kick_out_firmware_fb(dev_priv
);
1558 pci_set_master(dev
->pdev
);
1560 /* overlay on gen2 is broken and can't address above 1G */
1562 dma_set_coherent_mask(&dev
->pdev
->dev
, DMA_BIT_MASK(30));
1564 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1565 * using 32bit addressing, overwriting memory if HWS is located
1568 * The documentation also mentions an issue with undefined
1569 * behaviour if any general state is accessed within a page above 4GB,
1570 * which also needs to be handled carefully.
1572 if (IS_BROADWATER(dev
) || IS_CRESTLINE(dev
))
1573 dma_set_coherent_mask(&dev
->pdev
->dev
, DMA_BIT_MASK(32));
1575 aperture_size
= dev_priv
->gtt
.mappable_end
;
1577 dev_priv
->gtt
.mappable
=
1578 io_mapping_create_wc(dev_priv
->gtt
.mappable_base
,
1580 if (dev_priv
->gtt
.mappable
== NULL
) {
1585 i915_mtrr_setup(dev_priv
, dev_priv
->gtt
.mappable_base
,
1588 /* The i915 workqueue is primarily used for batched retirement of
1589 * requests (and thus managing bo) once the task has been completed
1590 * by the GPU. i915_gem_retire_requests() is called directly when we
1591 * need high-priority retirement, such as waiting for an explicit
1594 * It is also used for periodic low-priority events, such as
1595 * idle-timers and recording error state.
1597 * All tasks on the workqueue are expected to acquire the dev mutex
1598 * so there is no point in running more than one instance of the
1599 * workqueue at any time. Use an ordered one.
1601 dev_priv
->wq
= alloc_ordered_workqueue("i915", 0);
1602 if (dev_priv
->wq
== NULL
) {
1603 DRM_ERROR("Failed to create our workqueue.\n");
1608 /* This must be called before any calls to HAS_PCH_* */
1609 intel_detect_pch(dev
);
1611 intel_irq_init(dev
);
1614 /* Try to make sure MCHBAR is enabled before poking at it */
1615 intel_setup_mchbar(dev
);
1616 intel_setup_gmbus(dev
);
1617 intel_opregion_setup(dev
);
1619 intel_setup_bios(dev
);
1623 /* On the 945G/GM, the chipset reports the MSI capability on the
1624 * integrated graphics even though the support isn't actually there
1625 * according to the published specs. It doesn't appear to function
1626 * correctly in testing on 945G.
1627 * This may be a side effect of MSI having been made available for PEG
1628 * and the registers being closely associated.
1630 * According to chipset errata, on the 965GM, MSI interrupts may
1631 * be lost or delayed, but we use them anyways to avoid
1632 * stuck interrupts on some machines.
1634 if (!IS_I945G(dev
) && !IS_I945GM(dev
))
1635 pci_enable_msi(dev
->pdev
);
1637 spin_lock_init(&dev_priv
->irq_lock
);
1638 spin_lock_init(&dev_priv
->gpu_error
.lock
);
1639 spin_lock_init(&dev_priv
->rps
.lock
);
1640 spin_lock_init(&dev_priv
->backlight
.lock
);
1641 mutex_init(&dev_priv
->dpio_lock
);
1643 mutex_init(&dev_priv
->rps
.hw_lock
);
1644 mutex_init(&dev_priv
->modeset_restore_lock
);
1646 dev_priv
->num_plane
= 1;
1647 if (IS_VALLEYVIEW(dev
))
1648 dev_priv
->num_plane
= 2;
1650 if (INTEL_INFO(dev
)->num_pipes
) {
1651 ret
= drm_vblank_init(dev
, INTEL_INFO(dev
)->num_pipes
);
1653 goto out_gem_unload
;
1656 /* Start out suspended */
1657 dev_priv
->mm
.suspended
= 1;
1659 if (HAS_POWER_WELL(dev
))
1660 i915_init_power_well(dev
);
1662 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
1663 ret
= i915_load_modeset_init(dev
);
1665 DRM_ERROR("failed to init modeset\n");
1666 goto out_gem_unload
;
1670 i915_setup_sysfs(dev
);
1672 if (INTEL_INFO(dev
)->num_pipes
) {
1673 /* Must be done after probing outputs */
1674 intel_opregion_init(dev
);
1675 acpi_video_register();
1679 intel_gpu_ips_init(dev_priv
);
1684 if (dev_priv
->mm
.inactive_shrinker
.shrink
)
1685 unregister_shrinker(&dev_priv
->mm
.inactive_shrinker
);
1687 if (dev
->pdev
->msi_enabled
)
1688 pci_disable_msi(dev
->pdev
);
1690 intel_teardown_gmbus(dev
);
1691 intel_teardown_mchbar(dev
);
1692 destroy_workqueue(dev_priv
->wq
);
1694 if (dev_priv
->mm
.gtt_mtrr
>= 0) {
1695 mtrr_del(dev_priv
->mm
.gtt_mtrr
,
1696 dev_priv
->gtt
.mappable_base
,
1698 dev_priv
->mm
.gtt_mtrr
= -1;
1700 io_mapping_free(dev_priv
->gtt
.mappable
);
1701 dev_priv
->gtt
.gtt_remove(dev
);
1703 pci_iounmap(dev
->pdev
, dev_priv
->regs
);
1705 pci_dev_put(dev_priv
->bridge_dev
);
1711 int i915_driver_unload(struct drm_device
*dev
)
1713 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1716 intel_gpu_ips_teardown();
1718 if (HAS_POWER_WELL(dev
))
1719 i915_remove_power_well(dev
);
1721 i915_teardown_sysfs(dev
);
1723 if (dev_priv
->mm
.inactive_shrinker
.shrink
)
1724 unregister_shrinker(&dev_priv
->mm
.inactive_shrinker
);
1726 mutex_lock(&dev
->struct_mutex
);
1727 ret
= i915_gpu_idle(dev
);
1729 DRM_ERROR("failed to idle hardware: %d\n", ret
);
1730 i915_gem_retire_requests(dev
);
1731 mutex_unlock(&dev
->struct_mutex
);
1733 /* Cancel the retire work handler, which should be idle now. */
1734 cancel_delayed_work_sync(&dev_priv
->mm
.retire_work
);
1736 io_mapping_free(dev_priv
->gtt
.mappable
);
1737 if (dev_priv
->mm
.gtt_mtrr
>= 0) {
1738 mtrr_del(dev_priv
->mm
.gtt_mtrr
,
1739 dev_priv
->gtt
.mappable_base
,
1740 dev_priv
->gtt
.mappable_end
);
1741 dev_priv
->mm
.gtt_mtrr
= -1;
1744 acpi_video_unregister();
1746 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
1747 intel_fbdev_fini(dev
);
1748 intel_modeset_cleanup(dev
);
1749 cancel_work_sync(&dev_priv
->console_resume_work
);
1752 * free the memory space allocated for the child device
1753 * config parsed from VBT
1755 if (dev_priv
->vbt
.child_dev
&& dev_priv
->vbt
.child_dev_num
) {
1756 kfree(dev_priv
->vbt
.child_dev
);
1757 dev_priv
->vbt
.child_dev
= NULL
;
1758 dev_priv
->vbt
.child_dev_num
= 0;
1761 vga_switcheroo_unregister_client(dev
->pdev
);
1762 vga_client_register(dev
->pdev
, NULL
, NULL
, NULL
);
1765 /* Free error state after interrupts are fully disabled. */
1766 del_timer_sync(&dev_priv
->gpu_error
.hangcheck_timer
);
1767 cancel_work_sync(&dev_priv
->gpu_error
.work
);
1768 i915_destroy_error_state(dev
);
1770 if (dev
->pdev
->msi_enabled
)
1771 pci_disable_msi(dev
->pdev
);
1773 intel_opregion_fini(dev
);
1775 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
1776 /* Flush any outstanding unpin_work. */
1777 flush_workqueue(dev_priv
->wq
);
1779 mutex_lock(&dev
->struct_mutex
);
1780 i915_gem_free_all_phys_object(dev
);
1781 i915_gem_cleanup_ringbuffer(dev
);
1782 i915_gem_context_fini(dev
);
1783 mutex_unlock(&dev
->struct_mutex
);
1784 i915_gem_cleanup_aliasing_ppgtt(dev
);
1785 i915_gem_cleanup_stolen(dev
);
1787 if (!I915_NEED_GFX_HWS(dev
))
1791 drm_mm_takedown(&dev_priv
->mm
.gtt_space
);
1792 if (dev_priv
->regs
!= NULL
)
1793 pci_iounmap(dev
->pdev
, dev_priv
->regs
);
1795 intel_teardown_gmbus(dev
);
1796 intel_teardown_mchbar(dev
);
1798 destroy_workqueue(dev_priv
->wq
);
1799 pm_qos_remove_request(&dev_priv
->pm_qos
);
1801 dev_priv
->gtt
.gtt_remove(dev
);
1804 kmem_cache_destroy(dev_priv
->slab
);
1806 pci_dev_put(dev_priv
->bridge_dev
);
1807 kfree(dev
->dev_private
);
1812 int i915_driver_open(struct drm_device
*dev
, struct drm_file
*file
)
1814 struct drm_i915_file_private
*file_priv
;
1816 DRM_DEBUG_DRIVER("\n");
1817 file_priv
= kmalloc(sizeof(*file_priv
), GFP_KERNEL
);
1821 file
->driver_priv
= file_priv
;
1823 spin_lock_init(&file_priv
->mm
.lock
);
1824 INIT_LIST_HEAD(&file_priv
->mm
.request_list
);
1826 idr_init(&file_priv
->context_idr
);
1832 * i915_driver_lastclose - clean up after all DRM clients have exited
1835 * Take care of cleaning up after all DRM clients have exited. In the
1836 * mode setting case, we want to restore the kernel's initial mode (just
1837 * in case the last client left us in a bad state).
1839 * Additionally, in the non-mode setting case, we'll tear down the GTT
1840 * and DMA structures, since the kernel won't be using them, and clea
1843 void i915_driver_lastclose(struct drm_device
* dev
)
1845 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1847 /* On gen6+ we refuse to init without kms enabled, but then the drm core
1848 * goes right around and calls lastclose. Check for this and don't clean
1853 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
1854 intel_fb_restore_mode(dev
);
1855 vga_switcheroo_process_delayed_switch();
1859 i915_gem_lastclose(dev
);
1861 i915_dma_cleanup(dev
);
1864 void i915_driver_preclose(struct drm_device
* dev
, struct drm_file
*file_priv
)
1866 i915_gem_context_close(dev
, file_priv
);
1867 i915_gem_release(dev
, file_priv
);
1870 void i915_driver_postclose(struct drm_device
*dev
, struct drm_file
*file
)
1872 struct drm_i915_file_private
*file_priv
= file
->driver_priv
;
1877 struct drm_ioctl_desc i915_ioctls
[] = {
1878 DRM_IOCTL_DEF_DRV(I915_INIT
, i915_dma_init
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
1879 DRM_IOCTL_DEF_DRV(I915_FLUSH
, i915_flush_ioctl
, DRM_AUTH
),
1880 DRM_IOCTL_DEF_DRV(I915_FLIP
, i915_flip_bufs
, DRM_AUTH
),
1881 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER
, i915_batchbuffer
, DRM_AUTH
),
1882 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT
, i915_irq_emit
, DRM_AUTH
),
1883 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT
, i915_irq_wait
, DRM_AUTH
),
1884 DRM_IOCTL_DEF_DRV(I915_GETPARAM
, i915_getparam
, DRM_AUTH
),
1885 DRM_IOCTL_DEF_DRV(I915_SETPARAM
, i915_setparam
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
1886 DRM_IOCTL_DEF_DRV(I915_ALLOC
, drm_noop
, DRM_AUTH
),
1887 DRM_IOCTL_DEF_DRV(I915_FREE
, drm_noop
, DRM_AUTH
),
1888 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP
, drm_noop
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
1889 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER
, i915_cmdbuffer
, DRM_AUTH
),
1890 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP
, drm_noop
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
1891 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE
, drm_noop
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
1892 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE
, i915_vblank_pipe_get
, DRM_AUTH
),
1893 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP
, i915_vblank_swap
, DRM_AUTH
),
1894 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR
, i915_set_status_page
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
1895 DRM_IOCTL_DEF_DRV(I915_GEM_INIT
, i915_gem_init_ioctl
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
1896 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER
, i915_gem_execbuffer
, DRM_AUTH
|DRM_UNLOCKED
),
1897 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2
, i915_gem_execbuffer2
, DRM_AUTH
|DRM_UNLOCKED
),
1898 DRM_IOCTL_DEF_DRV(I915_GEM_PIN
, i915_gem_pin_ioctl
, DRM_AUTH
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
1899 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN
, i915_gem_unpin_ioctl
, DRM_AUTH
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
1900 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY
, i915_gem_busy_ioctl
, DRM_AUTH
|DRM_UNLOCKED
),
1901 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING
, i915_gem_set_caching_ioctl
, DRM_UNLOCKED
),
1902 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING
, i915_gem_get_caching_ioctl
, DRM_UNLOCKED
),
1903 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE
, i915_gem_throttle_ioctl
, DRM_AUTH
|DRM_UNLOCKED
),
1904 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT
, i915_gem_entervt_ioctl
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
1905 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT
, i915_gem_leavevt_ioctl
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
1906 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE
, i915_gem_create_ioctl
, DRM_UNLOCKED
),
1907 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD
, i915_gem_pread_ioctl
, DRM_UNLOCKED
),
1908 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE
, i915_gem_pwrite_ioctl
, DRM_UNLOCKED
),
1909 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP
, i915_gem_mmap_ioctl
, DRM_UNLOCKED
),
1910 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT
, i915_gem_mmap_gtt_ioctl
, DRM_UNLOCKED
),
1911 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN
, i915_gem_set_domain_ioctl
, DRM_UNLOCKED
),
1912 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH
, i915_gem_sw_finish_ioctl
, DRM_UNLOCKED
),
1913 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING
, i915_gem_set_tiling
, DRM_UNLOCKED
),
1914 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING
, i915_gem_get_tiling
, DRM_UNLOCKED
),
1915 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE
, i915_gem_get_aperture_ioctl
, DRM_UNLOCKED
),
1916 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID
, intel_get_pipe_from_crtc_id
, DRM_UNLOCKED
),
1917 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE
, i915_gem_madvise_ioctl
, DRM_UNLOCKED
),
1918 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE
, intel_overlay_put_image
, DRM_MASTER
|DRM_CONTROL_ALLOW
|DRM_UNLOCKED
),
1919 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS
, intel_overlay_attrs
, DRM_MASTER
|DRM_CONTROL_ALLOW
|DRM_UNLOCKED
),
1920 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY
, intel_sprite_set_colorkey
, DRM_MASTER
|DRM_CONTROL_ALLOW
|DRM_UNLOCKED
),
1921 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY
, intel_sprite_get_colorkey
, DRM_MASTER
|DRM_CONTROL_ALLOW
|DRM_UNLOCKED
),
1922 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT
, i915_gem_wait_ioctl
, DRM_AUTH
|DRM_UNLOCKED
),
1923 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE
, i915_gem_context_create_ioctl
, DRM_UNLOCKED
),
1924 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY
, i915_gem_context_destroy_ioctl
, DRM_UNLOCKED
),
1925 DRM_IOCTL_DEF_DRV(I915_REG_READ
, i915_reg_read_ioctl
, DRM_UNLOCKED
),
1928 int i915_max_ioctl
= DRM_ARRAY_SIZE(i915_ioctls
);
1931 * This is really ugly: Because old userspace abused the linux agp interface to
1932 * manage the gtt, we need to claim that all intel devices are agp. For
1933 * otherwise the drm core refuses to initialize the agp support code.
1935 int i915_driver_device_is_agp(struct drm_device
* dev
)