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_vblank_pipe_get(struct drm_device
*dev
, void *data
,
725 struct drm_file
*file_priv
)
727 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
728 drm_i915_vblank_pipe_t
*pipe
= data
;
730 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
734 DRM_ERROR("called with no initialization\n");
738 pipe
->pipe
= DRM_I915_VBLANK_PIPE_A
| DRM_I915_VBLANK_PIPE_B
;
744 * Schedule buffer swap at given vertical blank.
746 static int i915_vblank_swap(struct drm_device
*dev
, void *data
,
747 struct drm_file
*file_priv
)
749 /* The delayed swap mechanism was fundamentally racy, and has been
750 * removed. The model was that the client requested a delayed flip/swap
751 * from the kernel, then waited for vblank before continuing to perform
752 * rendering. The problem was that the kernel might wake the client
753 * up before it dispatched the vblank swap (since the lock has to be
754 * held while touching the ringbuffer), in which case the client would
755 * clear and start the next frame before the swap occurred, and
756 * flicker would occur in addition to likely missing the vblank.
758 * In the absence of this ioctl, userland falls back to a correct path
759 * of waiting for a vblank, then dispatching the swap on its own.
760 * Context switching to userland and back is plenty fast enough for
761 * meeting the requirements of vblank swapping.
766 static int i915_flip_bufs(struct drm_device
*dev
, void *data
,
767 struct drm_file
*file_priv
)
771 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
774 DRM_DEBUG_DRIVER("%s\n", __func__
);
776 RING_LOCK_TEST_WITH_RETURN(dev
, file_priv
);
778 mutex_lock(&dev
->struct_mutex
);
779 ret
= i915_dispatch_flip(dev
);
780 mutex_unlock(&dev
->struct_mutex
);
785 static int i915_getparam(struct drm_device
*dev
, void *data
,
786 struct drm_file
*file_priv
)
788 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
789 drm_i915_getparam_t
*param
= data
;
793 DRM_ERROR("called with no initialization\n");
797 switch (param
->param
) {
798 case I915_PARAM_IRQ_ACTIVE
:
799 value
= dev
->pdev
->irq
? 1 : 0;
801 case I915_PARAM_ALLOW_BATCHBUFFER
:
802 value
= dev_priv
->allow_batchbuffer
? 1 : 0;
804 case I915_PARAM_LAST_DISPATCH
:
805 value
= READ_BREADCRUMB(dev_priv
);
807 case I915_PARAM_CHIPSET_ID
:
808 value
= dev
->pci_device
;
810 case I915_PARAM_HAS_GEM
:
813 case I915_PARAM_NUM_FENCES_AVAIL
:
814 value
= dev_priv
->num_fence_regs
- dev_priv
->fence_reg_start
;
816 case I915_PARAM_HAS_OVERLAY
:
817 value
= dev_priv
->overlay
? 1 : 0;
819 case I915_PARAM_HAS_PAGEFLIPPING
:
822 case I915_PARAM_HAS_EXECBUF2
:
826 case I915_PARAM_HAS_BSD
:
827 value
= HAS_BSD(dev
);
829 case I915_PARAM_HAS_BLT
:
830 value
= HAS_BLT(dev
);
832 case I915_PARAM_HAS_RELAXED_FENCING
:
835 case I915_PARAM_HAS_COHERENT_RINGS
:
838 case I915_PARAM_HAS_EXEC_CONSTANTS
:
839 value
= INTEL_INFO(dev
)->gen
>= 4;
841 case I915_PARAM_HAS_RELAXED_DELTA
:
844 case I915_PARAM_HAS_GEN7_SOL_RESET
:
847 case I915_PARAM_HAS_LLC
:
848 value
= HAS_LLC(dev
);
850 case I915_PARAM_HAS_ALIASING_PPGTT
:
851 value
= dev_priv
->mm
.aliasing_ppgtt
? 1 : 0;
854 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
859 if (DRM_COPY_TO_USER(param
->value
, &value
, sizeof(int))) {
860 DRM_ERROR("DRM_COPY_TO_USER failed\n");
867 static int i915_setparam(struct drm_device
*dev
, void *data
,
868 struct drm_file
*file_priv
)
870 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
871 drm_i915_setparam_t
*param
= data
;
874 DRM_ERROR("called with no initialization\n");
878 switch (param
->param
) {
879 case I915_SETPARAM_USE_MI_BATCHBUFFER_START
:
881 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY
:
882 dev_priv
->tex_lru_log_granularity
= param
->value
;
884 case I915_SETPARAM_ALLOW_BATCHBUFFER
:
885 dev_priv
->allow_batchbuffer
= param
->value
;
887 case I915_SETPARAM_NUM_USED_FENCES
:
888 if (param
->value
> dev_priv
->num_fence_regs
||
891 /* Userspace can use first N regs */
892 dev_priv
->fence_reg_start
= param
->value
;
895 DRM_DEBUG_DRIVER("unknown parameter %d\n",
903 static int i915_set_status_page(struct drm_device
*dev
, void *data
,
904 struct drm_file
*file_priv
)
906 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
907 drm_i915_hws_addr_t
*hws
= data
;
908 struct intel_ring_buffer
*ring
= LP_RING(dev_priv
);
910 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
913 if (!I915_NEED_GFX_HWS(dev
))
917 DRM_ERROR("called with no initialization\n");
921 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
922 WARN(1, "tried to set status page when mode setting active\n");
926 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32
)hws
->addr
);
928 ring
->status_page
.gfx_addr
= hws
->addr
& (0x1ffff<<12);
930 dev_priv
->hws_map
.offset
= dev
->agp
->base
+ hws
->addr
;
931 dev_priv
->hws_map
.size
= 4*1024;
932 dev_priv
->hws_map
.type
= 0;
933 dev_priv
->hws_map
.flags
= 0;
934 dev_priv
->hws_map
.mtrr
= 0;
936 drm_core_ioremap_wc(&dev_priv
->hws_map
, dev
);
937 if (dev_priv
->hws_map
.handle
== NULL
) {
938 i915_dma_cleanup(dev
);
939 ring
->status_page
.gfx_addr
= 0;
940 DRM_ERROR("can not ioremap virtual address for"
941 " G33 hw status page\n");
944 ring
->status_page
.page_addr
=
945 (void __force __iomem
*)dev_priv
->hws_map
.handle
;
946 memset_io(ring
->status_page
.page_addr
, 0, PAGE_SIZE
);
947 I915_WRITE(HWS_PGA
, ring
->status_page
.gfx_addr
);
949 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
950 ring
->status_page
.gfx_addr
);
951 DRM_DEBUG_DRIVER("load hws at %p\n",
952 ring
->status_page
.page_addr
);
956 static int i915_get_bridge_dev(struct drm_device
*dev
)
958 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
960 dev_priv
->bridge_dev
= pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
961 if (!dev_priv
->bridge_dev
) {
962 DRM_ERROR("bridge device not found\n");
968 #define MCHBAR_I915 0x44
969 #define MCHBAR_I965 0x48
970 #define MCHBAR_SIZE (4*4096)
972 #define DEVEN_REG 0x54
973 #define DEVEN_MCHBAR_EN (1 << 28)
975 /* Allocate space for the MCH regs if needed, return nonzero on error */
977 intel_alloc_mchbar_resource(struct drm_device
*dev
)
979 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
980 int reg
= INTEL_INFO(dev
)->gen
>= 4 ? MCHBAR_I965
: MCHBAR_I915
;
981 u32 temp_lo
, temp_hi
= 0;
985 if (INTEL_INFO(dev
)->gen
>= 4)
986 pci_read_config_dword(dev_priv
->bridge_dev
, reg
+ 4, &temp_hi
);
987 pci_read_config_dword(dev_priv
->bridge_dev
, reg
, &temp_lo
);
988 mchbar_addr
= ((u64
)temp_hi
<< 32) | temp_lo
;
990 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
993 pnp_range_reserved(mchbar_addr
, mchbar_addr
+ MCHBAR_SIZE
))
997 /* Get some space for it */
998 dev_priv
->mch_res
.name
= "i915 MCHBAR";
999 dev_priv
->mch_res
.flags
= IORESOURCE_MEM
;
1000 ret
= pci_bus_alloc_resource(dev_priv
->bridge_dev
->bus
,
1002 MCHBAR_SIZE
, MCHBAR_SIZE
,
1004 0, pcibios_align_resource
,
1005 dev_priv
->bridge_dev
);
1007 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret
);
1008 dev_priv
->mch_res
.start
= 0;
1012 if (INTEL_INFO(dev
)->gen
>= 4)
1013 pci_write_config_dword(dev_priv
->bridge_dev
, reg
+ 4,
1014 upper_32_bits(dev_priv
->mch_res
.start
));
1016 pci_write_config_dword(dev_priv
->bridge_dev
, reg
,
1017 lower_32_bits(dev_priv
->mch_res
.start
));
1021 /* Setup MCHBAR if possible, return true if we should disable it again */
1023 intel_setup_mchbar(struct drm_device
*dev
)
1025 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1026 int mchbar_reg
= INTEL_INFO(dev
)->gen
>= 4 ? MCHBAR_I965
: MCHBAR_I915
;
1030 dev_priv
->mchbar_need_disable
= false;
1032 if (IS_I915G(dev
) || IS_I915GM(dev
)) {
1033 pci_read_config_dword(dev_priv
->bridge_dev
, DEVEN_REG
, &temp
);
1034 enabled
= !!(temp
& DEVEN_MCHBAR_EN
);
1036 pci_read_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, &temp
);
1040 /* If it's already enabled, don't have to do anything */
1044 if (intel_alloc_mchbar_resource(dev
))
1047 dev_priv
->mchbar_need_disable
= true;
1049 /* Space is allocated or reserved, so enable it. */
1050 if (IS_I915G(dev
) || IS_I915GM(dev
)) {
1051 pci_write_config_dword(dev_priv
->bridge_dev
, DEVEN_REG
,
1052 temp
| DEVEN_MCHBAR_EN
);
1054 pci_read_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, &temp
);
1055 pci_write_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, temp
| 1);
1060 intel_teardown_mchbar(struct drm_device
*dev
)
1062 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1063 int mchbar_reg
= INTEL_INFO(dev
)->gen
>= 4 ? MCHBAR_I965
: MCHBAR_I915
;
1066 if (dev_priv
->mchbar_need_disable
) {
1067 if (IS_I915G(dev
) || IS_I915GM(dev
)) {
1068 pci_read_config_dword(dev_priv
->bridge_dev
, DEVEN_REG
, &temp
);
1069 temp
&= ~DEVEN_MCHBAR_EN
;
1070 pci_write_config_dword(dev_priv
->bridge_dev
, DEVEN_REG
, temp
);
1072 pci_read_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, &temp
);
1074 pci_write_config_dword(dev_priv
->bridge_dev
, mchbar_reg
, temp
);
1078 if (dev_priv
->mch_res
.start
)
1079 release_resource(&dev_priv
->mch_res
);
1082 /* true = enable decode, false = disable decoder */
1083 static unsigned int i915_vga_set_decode(void *cookie
, bool state
)
1085 struct drm_device
*dev
= cookie
;
1087 intel_modeset_vga_set_state(dev
, state
);
1089 return VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
|
1090 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
1092 return VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
1095 static void i915_switcheroo_set_state(struct pci_dev
*pdev
, enum vga_switcheroo_state state
)
1097 struct drm_device
*dev
= pci_get_drvdata(pdev
);
1098 pm_message_t pmm
= { .event
= PM_EVENT_SUSPEND
};
1099 if (state
== VGA_SWITCHEROO_ON
) {
1100 pr_info("switched on\n");
1101 dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
1102 /* i915 resume handler doesn't set to D0 */
1103 pci_set_power_state(dev
->pdev
, PCI_D0
);
1105 dev
->switch_power_state
= DRM_SWITCH_POWER_ON
;
1107 pr_err("switched off\n");
1108 dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
1109 i915_suspend(dev
, pmm
);
1110 dev
->switch_power_state
= DRM_SWITCH_POWER_OFF
;
1114 static bool i915_switcheroo_can_switch(struct pci_dev
*pdev
)
1116 struct drm_device
*dev
= pci_get_drvdata(pdev
);
1119 spin_lock(&dev
->count_lock
);
1120 can_switch
= (dev
->open_count
== 0);
1121 spin_unlock(&dev
->count_lock
);
1125 static int i915_load_modeset_init(struct drm_device
*dev
)
1127 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1130 ret
= intel_parse_bios(dev
);
1132 DRM_INFO("failed to find VBIOS tables\n");
1134 /* If we have > 1 VGA cards, then we need to arbitrate access
1135 * to the common VGA resources.
1137 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1138 * then we do not take part in VGA arbitration and the
1139 * vga_client_register() fails with -ENODEV.
1141 ret
= vga_client_register(dev
->pdev
, dev
, NULL
, i915_vga_set_decode
);
1142 if (ret
&& ret
!= -ENODEV
)
1145 intel_register_dsm_handler();
1147 ret
= vga_switcheroo_register_client(dev
->pdev
,
1148 i915_switcheroo_set_state
,
1150 i915_switcheroo_can_switch
);
1152 goto cleanup_vga_client
;
1154 /* Initialise stolen first so that we may reserve preallocated
1155 * objects for the BIOS to KMS transition.
1157 ret
= i915_gem_init_stolen(dev
);
1159 goto cleanup_vga_switcheroo
;
1161 intel_modeset_init(dev
);
1163 ret
= i915_gem_init(dev
);
1165 goto cleanup_gem_stolen
;
1167 intel_modeset_gem_init(dev
);
1169 ret
= drm_irq_install(dev
);
1173 /* Always safe in the mode setting case. */
1174 /* FIXME: do pre/post-mode set stuff in core KMS code */
1175 dev
->vblank_disable_allowed
= 1;
1177 ret
= intel_fbdev_init(dev
);
1181 drm_kms_helper_poll_init(dev
);
1183 /* We're off and running w/KMS */
1184 dev_priv
->mm
.suspended
= 0;
1189 drm_irq_uninstall(dev
);
1191 mutex_lock(&dev
->struct_mutex
);
1192 i915_gem_cleanup_ringbuffer(dev
);
1193 mutex_unlock(&dev
->struct_mutex
);
1194 i915_gem_cleanup_aliasing_ppgtt(dev
);
1196 i915_gem_cleanup_stolen(dev
);
1197 cleanup_vga_switcheroo
:
1198 vga_switcheroo_unregister_client(dev
->pdev
);
1200 vga_client_register(dev
->pdev
, NULL
, NULL
, NULL
);
1205 int i915_master_create(struct drm_device
*dev
, struct drm_master
*master
)
1207 struct drm_i915_master_private
*master_priv
;
1209 master_priv
= kzalloc(sizeof(*master_priv
), GFP_KERNEL
);
1213 master
->driver_priv
= master_priv
;
1217 void i915_master_destroy(struct drm_device
*dev
, struct drm_master
*master
)
1219 struct drm_i915_master_private
*master_priv
= master
->driver_priv
;
1226 master
->driver_priv
= NULL
;
1229 static void i915_pineview_get_mem_freq(struct drm_device
*dev
)
1231 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1234 tmp
= I915_READ(CLKCFG
);
1236 switch (tmp
& CLKCFG_FSB_MASK
) {
1237 case CLKCFG_FSB_533
:
1238 dev_priv
->fsb_freq
= 533; /* 133*4 */
1240 case CLKCFG_FSB_800
:
1241 dev_priv
->fsb_freq
= 800; /* 200*4 */
1243 case CLKCFG_FSB_667
:
1244 dev_priv
->fsb_freq
= 667; /* 167*4 */
1246 case CLKCFG_FSB_400
:
1247 dev_priv
->fsb_freq
= 400; /* 100*4 */
1251 switch (tmp
& CLKCFG_MEM_MASK
) {
1252 case CLKCFG_MEM_533
:
1253 dev_priv
->mem_freq
= 533;
1255 case CLKCFG_MEM_667
:
1256 dev_priv
->mem_freq
= 667;
1258 case CLKCFG_MEM_800
:
1259 dev_priv
->mem_freq
= 800;
1263 /* detect pineview DDR3 setting */
1264 tmp
= I915_READ(CSHRDDR3CTL
);
1265 dev_priv
->is_ddr3
= (tmp
& CSHRDDR3CTL_DDR3
) ? 1 : 0;
1268 static void i915_ironlake_get_mem_freq(struct drm_device
*dev
)
1270 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1273 ddrpll
= I915_READ16(DDRMPLL1
);
1274 csipll
= I915_READ16(CSIPLL0
);
1276 switch (ddrpll
& 0xff) {
1278 dev_priv
->mem_freq
= 800;
1281 dev_priv
->mem_freq
= 1066;
1284 dev_priv
->mem_freq
= 1333;
1287 dev_priv
->mem_freq
= 1600;
1290 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
1292 dev_priv
->mem_freq
= 0;
1296 dev_priv
->r_t
= dev_priv
->mem_freq
;
1298 switch (csipll
& 0x3ff) {
1300 dev_priv
->fsb_freq
= 3200;
1303 dev_priv
->fsb_freq
= 3733;
1306 dev_priv
->fsb_freq
= 4266;
1309 dev_priv
->fsb_freq
= 4800;
1312 dev_priv
->fsb_freq
= 5333;
1315 dev_priv
->fsb_freq
= 5866;
1318 dev_priv
->fsb_freq
= 6400;
1321 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
1323 dev_priv
->fsb_freq
= 0;
1327 if (dev_priv
->fsb_freq
== 3200) {
1329 } else if (dev_priv
->fsb_freq
> 3200 && dev_priv
->fsb_freq
<= 4800) {
1336 static const struct cparams
{
1342 { 1, 1333, 301, 28664 },
1343 { 1, 1066, 294, 24460 },
1344 { 1, 800, 294, 25192 },
1345 { 0, 1333, 276, 27605 },
1346 { 0, 1066, 276, 27605 },
1347 { 0, 800, 231, 23784 },
1350 unsigned long i915_chipset_val(struct drm_i915_private
*dev_priv
)
1352 u64 total_count
, diff
, ret
;
1353 u32 count1
, count2
, count3
, m
= 0, c
= 0;
1354 unsigned long now
= jiffies_to_msecs(jiffies
), diff1
;
1357 diff1
= now
- dev_priv
->last_time1
;
1359 /* Prevent division-by-zero if we are asking too fast.
1360 * Also, we don't get interesting results if we are polling
1361 * faster than once in 10ms, so just return the saved value
1365 return dev_priv
->chipset_power
;
1367 count1
= I915_READ(DMIEC
);
1368 count2
= I915_READ(DDREC
);
1369 count3
= I915_READ(CSIEC
);
1371 total_count
= count1
+ count2
+ count3
;
1373 /* FIXME: handle per-counter overflow */
1374 if (total_count
< dev_priv
->last_count1
) {
1375 diff
= ~0UL - dev_priv
->last_count1
;
1376 diff
+= total_count
;
1378 diff
= total_count
- dev_priv
->last_count1
;
1381 for (i
= 0; i
< ARRAY_SIZE(cparams
); i
++) {
1382 if (cparams
[i
].i
== dev_priv
->c_m
&&
1383 cparams
[i
].t
== dev_priv
->r_t
) {
1390 diff
= div_u64(diff
, diff1
);
1391 ret
= ((m
* diff
) + c
);
1392 ret
= div_u64(ret
, 10);
1394 dev_priv
->last_count1
= total_count
;
1395 dev_priv
->last_time1
= now
;
1397 dev_priv
->chipset_power
= ret
;
1402 unsigned long i915_mch_val(struct drm_i915_private
*dev_priv
)
1404 unsigned long m
, x
, b
;
1407 tsfs
= I915_READ(TSFS
);
1409 m
= ((tsfs
& TSFS_SLOPE_MASK
) >> TSFS_SLOPE_SHIFT
);
1410 x
= I915_READ8(TR1
);
1412 b
= tsfs
& TSFS_INTR_MASK
;
1414 return ((m
* x
) / 127) - b
;
1417 static u16
pvid_to_extvid(struct drm_i915_private
*dev_priv
, u8 pxvid
)
1419 static const struct v_table
{
1420 u16 vd
; /* in .1 mil */
1421 u16 vm
; /* in .1 mil */
1552 if (dev_priv
->info
->is_mobile
)
1553 return v_table
[pxvid
].vm
;
1555 return v_table
[pxvid
].vd
;
1558 void i915_update_gfx_val(struct drm_i915_private
*dev_priv
)
1560 struct timespec now
, diff1
;
1562 unsigned long diffms
;
1565 if (dev_priv
->info
->gen
!= 5)
1568 getrawmonotonic(&now
);
1569 diff1
= timespec_sub(now
, dev_priv
->last_time2
);
1571 /* Don't divide by 0 */
1572 diffms
= diff1
.tv_sec
* 1000 + diff1
.tv_nsec
/ 1000000;
1576 count
= I915_READ(GFXEC
);
1578 if (count
< dev_priv
->last_count2
) {
1579 diff
= ~0UL - dev_priv
->last_count2
;
1582 diff
= count
- dev_priv
->last_count2
;
1585 dev_priv
->last_count2
= count
;
1586 dev_priv
->last_time2
= now
;
1588 /* More magic constants... */
1590 diff
= div_u64(diff
, diffms
* 10);
1591 dev_priv
->gfx_power
= diff
;
1594 unsigned long i915_gfx_val(struct drm_i915_private
*dev_priv
)
1596 unsigned long t
, corr
, state1
, corr2
, state2
;
1599 pxvid
= I915_READ(PXVFREQ_BASE
+ (dev_priv
->cur_delay
* 4));
1600 pxvid
= (pxvid
>> 24) & 0x7f;
1601 ext_v
= pvid_to_extvid(dev_priv
, pxvid
);
1605 t
= i915_mch_val(dev_priv
);
1607 /* Revel in the empirically derived constants */
1609 /* Correction factor in 1/100000 units */
1611 corr
= ((t
* 2349) + 135940);
1613 corr
= ((t
* 964) + 29317);
1615 corr
= ((t
* 301) + 1004);
1617 corr
= corr
* ((150142 * state1
) / 10000 - 78642);
1619 corr2
= (corr
* dev_priv
->corr
);
1621 state2
= (corr2
* state1
) / 10000;
1622 state2
/= 100; /* convert to mW */
1624 i915_update_gfx_val(dev_priv
);
1626 return dev_priv
->gfx_power
+ state2
;
1629 /* Global for IPS driver to get at the current i915 device */
1630 static struct drm_i915_private
*i915_mch_dev
;
1632 * Lock protecting IPS related data structures
1634 * - dev_priv->max_delay
1635 * - dev_priv->min_delay
1637 * - dev_priv->gpu_busy
1639 static DEFINE_SPINLOCK(mchdev_lock
);
1642 * i915_read_mch_val - return value for IPS use
1644 * Calculate and return a value for the IPS driver to use when deciding whether
1645 * we have thermal and power headroom to increase CPU or GPU power budget.
1647 unsigned long i915_read_mch_val(void)
1649 struct drm_i915_private
*dev_priv
;
1650 unsigned long chipset_val
, graphics_val
, ret
= 0;
1652 spin_lock(&mchdev_lock
);
1655 dev_priv
= i915_mch_dev
;
1657 chipset_val
= i915_chipset_val(dev_priv
);
1658 graphics_val
= i915_gfx_val(dev_priv
);
1660 ret
= chipset_val
+ graphics_val
;
1663 spin_unlock(&mchdev_lock
);
1667 EXPORT_SYMBOL_GPL(i915_read_mch_val
);
1670 * i915_gpu_raise - raise GPU frequency limit
1672 * Raise the limit; IPS indicates we have thermal headroom.
1674 bool i915_gpu_raise(void)
1676 struct drm_i915_private
*dev_priv
;
1679 spin_lock(&mchdev_lock
);
1680 if (!i915_mch_dev
) {
1684 dev_priv
= i915_mch_dev
;
1686 if (dev_priv
->max_delay
> dev_priv
->fmax
)
1687 dev_priv
->max_delay
--;
1690 spin_unlock(&mchdev_lock
);
1694 EXPORT_SYMBOL_GPL(i915_gpu_raise
);
1697 * i915_gpu_lower - lower GPU frequency limit
1699 * IPS indicates we're close to a thermal limit, so throttle back the GPU
1700 * frequency maximum.
1702 bool i915_gpu_lower(void)
1704 struct drm_i915_private
*dev_priv
;
1707 spin_lock(&mchdev_lock
);
1708 if (!i915_mch_dev
) {
1712 dev_priv
= i915_mch_dev
;
1714 if (dev_priv
->max_delay
< dev_priv
->min_delay
)
1715 dev_priv
->max_delay
++;
1718 spin_unlock(&mchdev_lock
);
1722 EXPORT_SYMBOL_GPL(i915_gpu_lower
);
1725 * i915_gpu_busy - indicate GPU business to IPS
1727 * Tell the IPS driver whether or not the GPU is busy.
1729 bool i915_gpu_busy(void)
1731 struct drm_i915_private
*dev_priv
;
1734 spin_lock(&mchdev_lock
);
1737 dev_priv
= i915_mch_dev
;
1739 ret
= dev_priv
->busy
;
1742 spin_unlock(&mchdev_lock
);
1746 EXPORT_SYMBOL_GPL(i915_gpu_busy
);
1749 * i915_gpu_turbo_disable - disable graphics turbo
1751 * Disable graphics turbo by resetting the max frequency and setting the
1752 * current frequency to the default.
1754 bool i915_gpu_turbo_disable(void)
1756 struct drm_i915_private
*dev_priv
;
1759 spin_lock(&mchdev_lock
);
1760 if (!i915_mch_dev
) {
1764 dev_priv
= i915_mch_dev
;
1766 dev_priv
->max_delay
= dev_priv
->fstart
;
1768 if (!ironlake_set_drps(dev_priv
->dev
, dev_priv
->fstart
))
1772 spin_unlock(&mchdev_lock
);
1776 EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable
);
1779 * Tells the intel_ips driver that the i915 driver is now loaded, if
1780 * IPS got loaded first.
1782 * This awkward dance is so that neither module has to depend on the
1783 * other in order for IPS to do the appropriate communication of
1784 * GPU turbo limits to i915.
1787 ips_ping_for_i915_load(void)
1791 link
= symbol_get(ips_link_to_i915_driver
);
1794 symbol_put(ips_link_to_i915_driver
);
1799 i915_mtrr_setup(struct drm_i915_private
*dev_priv
, unsigned long base
,
1802 dev_priv
->mm
.gtt_mtrr
= -1;
1804 #if defined(CONFIG_X86_PAT)
1809 /* Set up a WC MTRR for non-PAT systems. This is more common than
1810 * one would think, because the kernel disables PAT on first
1811 * generation Core chips because WC PAT gets overridden by a UC
1812 * MTRR if present. Even if a UC MTRR isn't present.
1814 dev_priv
->mm
.gtt_mtrr
= mtrr_add(base
, size
, MTRR_TYPE_WRCOMB
, 1);
1815 if (dev_priv
->mm
.gtt_mtrr
< 0) {
1816 DRM_INFO("MTRR allocation failed. Graphics "
1817 "performance may suffer.\n");
1822 * i915_driver_load - setup chip and create an initial config
1824 * @flags: startup flags
1826 * The driver load routine has to do several things:
1827 * - drive output discovery via intel_modeset_init()
1828 * - initialize the memory manager
1829 * - allocate initial config memory
1830 * - setup the DRM framebuffer with the allocated memory
1832 int i915_driver_load(struct drm_device
*dev
, unsigned long flags
)
1834 struct drm_i915_private
*dev_priv
;
1835 struct intel_device_info
*info
;
1836 int ret
= 0, mmio_bar
;
1837 uint32_t aperture_size
;
1839 info
= (struct intel_device_info
*) flags
;
1841 /* Refuse to load on gen6+ without kms enabled. */
1842 if (info
->gen
>= 6 && !drm_core_check_feature(dev
, DRIVER_MODESET
))
1846 /* i915 has 4 more counters */
1848 dev
->types
[6] = _DRM_STAT_IRQ
;
1849 dev
->types
[7] = _DRM_STAT_PRIMARY
;
1850 dev
->types
[8] = _DRM_STAT_SECONDARY
;
1851 dev
->types
[9] = _DRM_STAT_DMA
;
1853 dev_priv
= kzalloc(sizeof(drm_i915_private_t
), GFP_KERNEL
);
1854 if (dev_priv
== NULL
)
1857 dev
->dev_private
= (void *)dev_priv
;
1858 dev_priv
->dev
= dev
;
1859 dev_priv
->info
= info
;
1861 if (i915_get_bridge_dev(dev
)) {
1866 pci_set_master(dev
->pdev
);
1868 /* overlay on gen2 is broken and can't address above 1G */
1870 dma_set_coherent_mask(&dev
->pdev
->dev
, DMA_BIT_MASK(30));
1872 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1873 * using 32bit addressing, overwriting memory if HWS is located
1876 * The documentation also mentions an issue with undefined
1877 * behaviour if any general state is accessed within a page above 4GB,
1878 * which also needs to be handled carefully.
1880 if (IS_BROADWATER(dev
) || IS_CRESTLINE(dev
))
1881 dma_set_coherent_mask(&dev
->pdev
->dev
, DMA_BIT_MASK(32));
1883 mmio_bar
= IS_GEN2(dev
) ? 1 : 0;
1884 dev_priv
->regs
= pci_iomap(dev
->pdev
, mmio_bar
, 0);
1885 if (!dev_priv
->regs
) {
1886 DRM_ERROR("failed to map registers\n");
1891 dev_priv
->mm
.gtt
= intel_gtt_get();
1892 if (!dev_priv
->mm
.gtt
) {
1893 DRM_ERROR("Failed to initialize GTT\n");
1898 aperture_size
= dev_priv
->mm
.gtt
->gtt_mappable_entries
<< PAGE_SHIFT
;
1900 dev_priv
->mm
.gtt_mapping
=
1901 io_mapping_create_wc(dev
->agp
->base
, aperture_size
);
1902 if (dev_priv
->mm
.gtt_mapping
== NULL
) {
1907 i915_mtrr_setup(dev_priv
, dev
->agp
->base
, aperture_size
);
1909 /* The i915 workqueue is primarily used for batched retirement of
1910 * requests (and thus managing bo) once the task has been completed
1911 * by the GPU. i915_gem_retire_requests() is called directly when we
1912 * need high-priority retirement, such as waiting for an explicit
1915 * It is also used for periodic low-priority events, such as
1916 * idle-timers and recording error state.
1918 * All tasks on the workqueue are expected to acquire the dev mutex
1919 * so there is no point in running more than one instance of the
1920 * workqueue at any time: max_active = 1 and NON_REENTRANT.
1922 dev_priv
->wq
= alloc_workqueue("i915",
1923 WQ_UNBOUND
| WQ_NON_REENTRANT
,
1925 if (dev_priv
->wq
== NULL
) {
1926 DRM_ERROR("Failed to create our workqueue.\n");
1931 intel_irq_init(dev
);
1933 /* Try to make sure MCHBAR is enabled before poking at it */
1934 intel_setup_mchbar(dev
);
1935 intel_setup_gmbus(dev
);
1936 intel_opregion_setup(dev
);
1938 /* Make sure the bios did its job and set up vital registers */
1939 intel_setup_bios(dev
);
1944 if (!I915_NEED_GFX_HWS(dev
)) {
1945 ret
= i915_init_phys_hws(dev
);
1947 goto out_gem_unload
;
1950 if (IS_PINEVIEW(dev
))
1951 i915_pineview_get_mem_freq(dev
);
1952 else if (IS_GEN5(dev
))
1953 i915_ironlake_get_mem_freq(dev
);
1955 /* On the 945G/GM, the chipset reports the MSI capability on the
1956 * integrated graphics even though the support isn't actually there
1957 * according to the published specs. It doesn't appear to function
1958 * correctly in testing on 945G.
1959 * This may be a side effect of MSI having been made available for PEG
1960 * and the registers being closely associated.
1962 * According to chipset errata, on the 965GM, MSI interrupts may
1963 * be lost or delayed, but we use them anyways to avoid
1964 * stuck interrupts on some machines.
1966 if (!IS_I945G(dev
) && !IS_I945GM(dev
))
1967 pci_enable_msi(dev
->pdev
);
1969 spin_lock_init(&dev_priv
->gt_lock
);
1970 spin_lock_init(&dev_priv
->irq_lock
);
1971 spin_lock_init(&dev_priv
->error_lock
);
1972 spin_lock_init(&dev_priv
->rps_lock
);
1974 if (IS_IVYBRIDGE(dev
) || IS_HASWELL(dev
))
1975 dev_priv
->num_pipe
= 3;
1976 else if (IS_MOBILE(dev
) || !IS_GEN2(dev
))
1977 dev_priv
->num_pipe
= 2;
1979 dev_priv
->num_pipe
= 1;
1981 ret
= drm_vblank_init(dev
, dev_priv
->num_pipe
);
1983 goto out_gem_unload
;
1985 /* Start out suspended */
1986 dev_priv
->mm
.suspended
= 1;
1988 intel_detect_pch(dev
);
1990 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
1991 ret
= i915_load_modeset_init(dev
);
1993 DRM_ERROR("failed to init modeset\n");
1994 goto out_gem_unload
;
1998 i915_setup_sysfs(dev
);
2000 /* Must be done after probing outputs */
2001 intel_opregion_init(dev
);
2002 acpi_video_register();
2004 setup_timer(&dev_priv
->hangcheck_timer
, i915_hangcheck_elapsed
,
2005 (unsigned long) dev
);
2008 spin_lock(&mchdev_lock
);
2009 i915_mch_dev
= dev_priv
;
2010 dev_priv
->mchdev_lock
= &mchdev_lock
;
2011 spin_unlock(&mchdev_lock
);
2013 ips_ping_for_i915_load();
2019 if (dev_priv
->mm
.inactive_shrinker
.shrink
)
2020 unregister_shrinker(&dev_priv
->mm
.inactive_shrinker
);
2022 if (dev
->pdev
->msi_enabled
)
2023 pci_disable_msi(dev
->pdev
);
2025 intel_teardown_gmbus(dev
);
2026 intel_teardown_mchbar(dev
);
2027 destroy_workqueue(dev_priv
->wq
);
2029 if (dev_priv
->mm
.gtt_mtrr
>= 0) {
2030 mtrr_del(dev_priv
->mm
.gtt_mtrr
, dev
->agp
->base
,
2031 dev
->agp
->agp_info
.aper_size
* 1024 * 1024);
2032 dev_priv
->mm
.gtt_mtrr
= -1;
2034 io_mapping_free(dev_priv
->mm
.gtt_mapping
);
2036 pci_iounmap(dev
->pdev
, dev_priv
->regs
);
2038 pci_dev_put(dev_priv
->bridge_dev
);
2044 int i915_driver_unload(struct drm_device
*dev
)
2046 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
2049 spin_lock(&mchdev_lock
);
2050 i915_mch_dev
= NULL
;
2051 spin_unlock(&mchdev_lock
);
2053 i915_teardown_sysfs(dev
);
2055 if (dev_priv
->mm
.inactive_shrinker
.shrink
)
2056 unregister_shrinker(&dev_priv
->mm
.inactive_shrinker
);
2058 mutex_lock(&dev
->struct_mutex
);
2059 ret
= i915_gpu_idle(dev
);
2061 DRM_ERROR("failed to idle hardware: %d\n", ret
);
2062 i915_gem_retire_requests(dev
);
2063 mutex_unlock(&dev
->struct_mutex
);
2065 /* Cancel the retire work handler, which should be idle now. */
2066 cancel_delayed_work_sync(&dev_priv
->mm
.retire_work
);
2068 io_mapping_free(dev_priv
->mm
.gtt_mapping
);
2069 if (dev_priv
->mm
.gtt_mtrr
>= 0) {
2070 mtrr_del(dev_priv
->mm
.gtt_mtrr
, dev
->agp
->base
,
2071 dev
->agp
->agp_info
.aper_size
* 1024 * 1024);
2072 dev_priv
->mm
.gtt_mtrr
= -1;
2075 acpi_video_unregister();
2077 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
2078 intel_fbdev_fini(dev
);
2079 intel_modeset_cleanup(dev
);
2082 * free the memory space allocated for the child device
2083 * config parsed from VBT
2085 if (dev_priv
->child_dev
&& dev_priv
->child_dev_num
) {
2086 kfree(dev_priv
->child_dev
);
2087 dev_priv
->child_dev
= NULL
;
2088 dev_priv
->child_dev_num
= 0;
2091 vga_switcheroo_unregister_client(dev
->pdev
);
2092 vga_client_register(dev
->pdev
, NULL
, NULL
, NULL
);
2095 /* Free error state after interrupts are fully disabled. */
2096 del_timer_sync(&dev_priv
->hangcheck_timer
);
2097 cancel_work_sync(&dev_priv
->error_work
);
2098 i915_destroy_error_state(dev
);
2100 if (dev
->pdev
->msi_enabled
)
2101 pci_disable_msi(dev
->pdev
);
2103 intel_opregion_fini(dev
);
2105 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
2106 /* Flush any outstanding unpin_work. */
2107 flush_workqueue(dev_priv
->wq
);
2109 mutex_lock(&dev
->struct_mutex
);
2110 i915_gem_free_all_phys_object(dev
);
2111 i915_gem_cleanup_ringbuffer(dev
);
2112 mutex_unlock(&dev
->struct_mutex
);
2113 i915_gem_cleanup_aliasing_ppgtt(dev
);
2114 i915_gem_cleanup_stolen(dev
);
2115 drm_mm_takedown(&dev_priv
->mm
.stolen
);
2117 intel_cleanup_overlay(dev
);
2119 if (!I915_NEED_GFX_HWS(dev
))
2123 if (dev_priv
->regs
!= NULL
)
2124 pci_iounmap(dev
->pdev
, dev_priv
->regs
);
2126 intel_teardown_gmbus(dev
);
2127 intel_teardown_mchbar(dev
);
2129 destroy_workqueue(dev_priv
->wq
);
2131 pci_dev_put(dev_priv
->bridge_dev
);
2132 kfree(dev
->dev_private
);
2137 int i915_driver_open(struct drm_device
*dev
, struct drm_file
*file
)
2139 struct drm_i915_file_private
*file_priv
;
2141 DRM_DEBUG_DRIVER("\n");
2142 file_priv
= kmalloc(sizeof(*file_priv
), GFP_KERNEL
);
2146 file
->driver_priv
= file_priv
;
2148 spin_lock_init(&file_priv
->mm
.lock
);
2149 INIT_LIST_HEAD(&file_priv
->mm
.request_list
);
2155 * i915_driver_lastclose - clean up after all DRM clients have exited
2158 * Take care of cleaning up after all DRM clients have exited. In the
2159 * mode setting case, we want to restore the kernel's initial mode (just
2160 * in case the last client left us in a bad state).
2162 * Additionally, in the non-mode setting case, we'll tear down the GTT
2163 * and DMA structures, since the kernel won't be using them, and clea
2166 void i915_driver_lastclose(struct drm_device
* dev
)
2168 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2170 if (!dev_priv
|| drm_core_check_feature(dev
, DRIVER_MODESET
)) {
2171 intel_fb_restore_mode(dev
);
2172 vga_switcheroo_process_delayed_switch();
2176 i915_gem_lastclose(dev
);
2178 i915_dma_cleanup(dev
);
2181 void i915_driver_preclose(struct drm_device
* dev
, struct drm_file
*file_priv
)
2183 i915_gem_release(dev
, file_priv
);
2186 void i915_driver_postclose(struct drm_device
*dev
, struct drm_file
*file
)
2188 struct drm_i915_file_private
*file_priv
= file
->driver_priv
;
2193 struct drm_ioctl_desc i915_ioctls
[] = {
2194 DRM_IOCTL_DEF_DRV(I915_INIT
, i915_dma_init
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
2195 DRM_IOCTL_DEF_DRV(I915_FLUSH
, i915_flush_ioctl
, DRM_AUTH
),
2196 DRM_IOCTL_DEF_DRV(I915_FLIP
, i915_flip_bufs
, DRM_AUTH
),
2197 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER
, i915_batchbuffer
, DRM_AUTH
),
2198 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT
, i915_irq_emit
, DRM_AUTH
),
2199 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT
, i915_irq_wait
, DRM_AUTH
),
2200 DRM_IOCTL_DEF_DRV(I915_GETPARAM
, i915_getparam
, DRM_AUTH
),
2201 DRM_IOCTL_DEF_DRV(I915_SETPARAM
, i915_setparam
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
2202 DRM_IOCTL_DEF_DRV(I915_ALLOC
, drm_noop
, DRM_AUTH
),
2203 DRM_IOCTL_DEF_DRV(I915_FREE
, drm_noop
, DRM_AUTH
),
2204 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP
, drm_noop
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
2205 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER
, i915_cmdbuffer
, DRM_AUTH
),
2206 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP
, drm_noop
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
2207 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE
, drm_noop
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
2208 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE
, i915_vblank_pipe_get
, DRM_AUTH
),
2209 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP
, i915_vblank_swap
, DRM_AUTH
),
2210 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR
, i915_set_status_page
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
),
2211 DRM_IOCTL_DEF_DRV(I915_GEM_INIT
, i915_gem_init_ioctl
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
2212 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER
, i915_gem_execbuffer
, DRM_AUTH
|DRM_UNLOCKED
),
2213 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2
, i915_gem_execbuffer2
, DRM_AUTH
|DRM_UNLOCKED
),
2214 DRM_IOCTL_DEF_DRV(I915_GEM_PIN
, i915_gem_pin_ioctl
, DRM_AUTH
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
2215 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN
, i915_gem_unpin_ioctl
, DRM_AUTH
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
2216 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY
, i915_gem_busy_ioctl
, DRM_AUTH
|DRM_UNLOCKED
),
2217 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE
, i915_gem_throttle_ioctl
, DRM_AUTH
|DRM_UNLOCKED
),
2218 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT
, i915_gem_entervt_ioctl
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
2219 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT
, i915_gem_leavevt_ioctl
, DRM_AUTH
|DRM_MASTER
|DRM_ROOT_ONLY
|DRM_UNLOCKED
),
2220 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE
, i915_gem_create_ioctl
, DRM_UNLOCKED
),
2221 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD
, i915_gem_pread_ioctl
, DRM_UNLOCKED
),
2222 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE
, i915_gem_pwrite_ioctl
, DRM_UNLOCKED
),
2223 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP
, i915_gem_mmap_ioctl
, DRM_UNLOCKED
),
2224 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT
, i915_gem_mmap_gtt_ioctl
, DRM_UNLOCKED
),
2225 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN
, i915_gem_set_domain_ioctl
, DRM_UNLOCKED
),
2226 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH
, i915_gem_sw_finish_ioctl
, DRM_UNLOCKED
),
2227 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING
, i915_gem_set_tiling
, DRM_UNLOCKED
),
2228 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING
, i915_gem_get_tiling
, DRM_UNLOCKED
),
2229 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE
, i915_gem_get_aperture_ioctl
, DRM_UNLOCKED
),
2230 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID
, intel_get_pipe_from_crtc_id
, DRM_UNLOCKED
),
2231 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE
, i915_gem_madvise_ioctl
, DRM_UNLOCKED
),
2232 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE
, intel_overlay_put_image
, DRM_MASTER
|DRM_CONTROL_ALLOW
|DRM_UNLOCKED
),
2233 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS
, intel_overlay_attrs
, DRM_MASTER
|DRM_CONTROL_ALLOW
|DRM_UNLOCKED
),
2234 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY
, intel_sprite_set_colorkey
, DRM_MASTER
|DRM_CONTROL_ALLOW
|DRM_UNLOCKED
),
2235 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY
, intel_sprite_get_colorkey
, DRM_MASTER
|DRM_CONTROL_ALLOW
|DRM_UNLOCKED
),
2238 int i915_max_ioctl
= DRM_ARRAY_SIZE(i915_ioctls
);
2241 * This is really ugly: Because old userspace abused the linux agp interface to
2242 * manage the gtt, we need to claim that all intel devices are agp. For
2243 * otherwise the drm core refuses to initialize the agp support code.
2245 int i915_driver_device_is_agp(struct drm_device
* dev
)