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.
31 #include "drm_crtc_helper.h"
32 #include "intel_drv.h"
36 /* Really want an OS-independent resettable timer. Would like to have
37 * this loop run for (eg) 3 sec, but have the timer reset every time
38 * the head pointer changes, so that EBUSY only happens if the ring
39 * actually stalls for (eg) 3 seconds.
41 int i915_wait_ring(struct drm_device * dev, int n, const char *caller)
43 drm_i915_private_t *dev_priv = dev->dev_private;
44 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
45 u32 acthd_reg = IS_I965G(dev) ? ACTHD_I965 : ACTHD;
46 u32 last_acthd = I915_READ(acthd_reg);
48 u32 last_head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
51 for (i = 0; i < 100000; i++) {
52 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
53 acthd = I915_READ(acthd_reg);
54 ring->space = ring->head - (ring->tail + 8);
56 ring->space += ring->Size;
60 if (dev->primary->master) {
61 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
62 if (master_priv->sarea_priv)
63 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
67 if (ring->head != last_head)
69 if (acthd != last_acthd)
72 last_head = ring->head;
74 msleep_interruptible(10);
82 * Sets up the hardware status page for devices that need a physical address
85 static int i915_init_phys_hws(struct drm_device *dev)
87 drm_i915_private_t *dev_priv = dev->dev_private;
88 /* Program Hardware Status Page */
89 dev_priv->status_page_dmah =
90 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff);
92 if (!dev_priv->status_page_dmah) {
93 DRM_ERROR("Can not allocate hardware status page\n");
96 dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr;
97 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
99 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
101 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
102 DRM_DEBUG("Enabled hardware status page\n");
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 if (dev_priv->status_page_dmah) {
114 drm_pci_free(dev, dev_priv->status_page_dmah);
115 dev_priv->status_page_dmah = NULL;
118 if (dev_priv->status_gfx_addr) {
119 dev_priv->status_gfx_addr = 0;
120 drm_core_ioremapfree(&dev_priv->hws_map, dev);
123 /* Need to rewrite hardware status page */
124 I915_WRITE(HWS_PGA, 0x1ffff000);
127 void i915_kernel_lost_context(struct drm_device * dev)
129 drm_i915_private_t *dev_priv = dev->dev_private;
130 struct drm_i915_master_private *master_priv;
131 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
134 * We should never lose context on the ring with modesetting
135 * as we don't expose it to userspace
137 if (drm_core_check_feature(dev, DRIVER_MODESET))
140 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
141 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
142 ring->space = ring->head - (ring->tail + 8);
144 ring->space += ring->Size;
146 if (!dev->primary->master)
149 master_priv = dev->primary->master->driver_priv;
150 if (ring->head == ring->tail && master_priv->sarea_priv)
151 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
154 static int i915_dma_cleanup(struct drm_device * dev)
156 drm_i915_private_t *dev_priv = dev->dev_private;
157 /* Make sure interrupts are disabled here because the uninstall ioctl
158 * may not have been called from userspace and after dev_private
159 * is freed, it's too late.
161 if (dev->irq_enabled)
162 drm_irq_uninstall(dev);
164 if (dev_priv->ring.virtual_start) {
165 drm_core_ioremapfree(&dev_priv->ring.map, dev);
166 dev_priv->ring.virtual_start = NULL;
167 dev_priv->ring.map.handle = NULL;
168 dev_priv->ring.map.size = 0;
171 /* Clear the HWS virtual address at teardown */
172 if (I915_NEED_GFX_HWS(dev))
178 static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
180 drm_i915_private_t *dev_priv = dev->dev_private;
181 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
183 master_priv->sarea = drm_getsarea(dev);
184 if (master_priv->sarea) {
185 master_priv->sarea_priv = (drm_i915_sarea_t *)
186 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
188 DRM_DEBUG("sarea not found assuming DRI2 userspace\n");
191 if (init->ring_size != 0) {
192 if (dev_priv->ring.ring_obj != NULL) {
193 i915_dma_cleanup(dev);
194 DRM_ERROR("Client tried to initialize ringbuffer in "
199 dev_priv->ring.Size = init->ring_size;
200 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
202 dev_priv->ring.map.offset = init->ring_start;
203 dev_priv->ring.map.size = init->ring_size;
204 dev_priv->ring.map.type = 0;
205 dev_priv->ring.map.flags = 0;
206 dev_priv->ring.map.mtrr = 0;
208 drm_core_ioremap_wc(&dev_priv->ring.map, dev);
210 if (dev_priv->ring.map.handle == NULL) {
211 i915_dma_cleanup(dev);
212 DRM_ERROR("can not ioremap virtual address for"
218 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
220 dev_priv->cpp = init->cpp;
221 dev_priv->back_offset = init->back_offset;
222 dev_priv->front_offset = init->front_offset;
223 dev_priv->current_page = 0;
224 if (master_priv->sarea_priv)
225 master_priv->sarea_priv->pf_current_page = 0;
227 /* Allow hardware batchbuffers unless told otherwise.
229 dev_priv->allow_batchbuffer = 1;
234 static int i915_dma_resume(struct drm_device * dev)
236 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
238 DRM_DEBUG("%s\n", __func__);
240 if (dev_priv->ring.map.handle == NULL) {
241 DRM_ERROR("can not ioremap virtual address for"
246 /* Program Hardware Status Page */
247 if (!dev_priv->hw_status_page) {
248 DRM_ERROR("Can not find hardware status page\n");
251 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
253 if (dev_priv->status_gfx_addr != 0)
254 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
256 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
257 DRM_DEBUG("Enabled hardware status page\n");
262 static int i915_dma_init(struct drm_device *dev, void *data,
263 struct drm_file *file_priv)
265 drm_i915_init_t *init = data;
268 switch (init->func) {
270 retcode = i915_initialize(dev, init);
272 case I915_CLEANUP_DMA:
273 retcode = i915_dma_cleanup(dev);
275 case I915_RESUME_DMA:
276 retcode = i915_dma_resume(dev);
286 /* Implement basically the same security restrictions as hardware does
287 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
289 * Most of the calculations below involve calculating the size of a
290 * particular instruction. It's important to get the size right as
291 * that tells us where the next instruction to check is. Any illegal
292 * instruction detected will be given a size of zero, which is a
293 * signal to abort the rest of the buffer.
295 static int do_validate_cmd(int cmd)
297 switch (((cmd >> 29) & 0x7)) {
299 switch ((cmd >> 23) & 0x3f) {
301 return 1; /* MI_NOOP */
303 return 1; /* MI_FLUSH */
305 return 0; /* disallow everything else */
309 return 0; /* reserved */
311 return (cmd & 0xff) + 2; /* 2d commands */
313 if (((cmd >> 24) & 0x1f) <= 0x18)
316 switch ((cmd >> 24) & 0x1f) {
320 switch ((cmd >> 16) & 0xff) {
322 return (cmd & 0x1f) + 2;
324 return (cmd & 0xf) + 2;
326 return (cmd & 0xffff) + 2;
330 return (cmd & 0xffff) + 1;
334 if ((cmd & (1 << 23)) == 0) /* inline vertices */
335 return (cmd & 0x1ffff) + 2;
336 else if (cmd & (1 << 17)) /* indirect random */
337 if ((cmd & 0xffff) == 0)
338 return 0; /* unknown length, too hard */
340 return (((cmd & 0xffff) + 1) / 2) + 1;
342 return 2; /* indirect sequential */
353 static int validate_cmd(int cmd)
355 int ret = do_validate_cmd(cmd);
357 /* printk("validate_cmd( %x ): %d\n", cmd, ret); */
362 static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
364 drm_i915_private_t *dev_priv = dev->dev_private;
368 if ((dwords+1) * sizeof(int) >= dev_priv->ring.Size - 8)
371 BEGIN_LP_RING((dwords+1)&~1);
373 for (i = 0; i < dwords;) {
378 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
397 i915_emit_box(struct drm_device *dev,
398 struct drm_clip_rect *boxes,
399 int i, int DR1, int DR4)
401 drm_i915_private_t *dev_priv = dev->dev_private;
402 struct drm_clip_rect box = boxes[i];
405 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
406 DRM_ERROR("Bad box %d,%d..%d,%d\n",
407 box.x1, box.y1, box.x2, box.y2);
413 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
414 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
415 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
420 OUT_RING(GFX_OP_DRAWRECT_INFO);
422 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
423 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
432 /* XXX: Emitting the counter should really be moved to part of the IRQ
433 * emit. For now, do it in both places:
436 static void i915_emit_breadcrumb(struct drm_device *dev)
438 drm_i915_private_t *dev_priv = dev->dev_private;
439 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
443 if (dev_priv->counter > 0x7FFFFFFFUL)
444 dev_priv->counter = 0;
445 if (master_priv->sarea_priv)
446 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
449 OUT_RING(MI_STORE_DWORD_INDEX);
450 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
451 OUT_RING(dev_priv->counter);
456 static int i915_dispatch_cmdbuffer(struct drm_device * dev,
457 drm_i915_cmdbuffer_t *cmd,
458 struct drm_clip_rect *cliprects,
461 int nbox = cmd->num_cliprects;
462 int i = 0, count, ret;
465 DRM_ERROR("alignment");
469 i915_kernel_lost_context(dev);
471 count = nbox ? nbox : 1;
473 for (i = 0; i < count; i++) {
475 ret = i915_emit_box(dev, cliprects, i,
481 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
486 i915_emit_breadcrumb(dev);
490 static int i915_dispatch_batchbuffer(struct drm_device * dev,
491 drm_i915_batchbuffer_t * batch,
492 struct drm_clip_rect *cliprects)
494 drm_i915_private_t *dev_priv = dev->dev_private;
495 int nbox = batch->num_cliprects;
499 if ((batch->start | batch->used) & 0x7) {
500 DRM_ERROR("alignment");
504 i915_kernel_lost_context(dev);
506 count = nbox ? nbox : 1;
508 for (i = 0; i < count; i++) {
510 int ret = i915_emit_box(dev, cliprects, i,
511 batch->DR1, batch->DR4);
516 if (!IS_I830(dev) && !IS_845G(dev)) {
519 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
520 OUT_RING(batch->start);
522 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
523 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
528 OUT_RING(MI_BATCH_BUFFER);
529 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
530 OUT_RING(batch->start + batch->used - 4);
536 i915_emit_breadcrumb(dev);
541 static int i915_dispatch_flip(struct drm_device * dev)
543 drm_i915_private_t *dev_priv = dev->dev_private;
544 struct drm_i915_master_private *master_priv =
545 dev->primary->master->driver_priv;
548 if (!master_priv->sarea_priv)
551 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
553 dev_priv->current_page,
554 master_priv->sarea_priv->pf_current_page);
556 i915_kernel_lost_context(dev);
559 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
564 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
566 if (dev_priv->current_page == 0) {
567 OUT_RING(dev_priv->back_offset);
568 dev_priv->current_page = 1;
570 OUT_RING(dev_priv->front_offset);
571 dev_priv->current_page = 0;
577 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
581 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
584 OUT_RING(MI_STORE_DWORD_INDEX);
585 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
586 OUT_RING(dev_priv->counter);
590 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
594 static int i915_quiescent(struct drm_device * dev)
596 drm_i915_private_t *dev_priv = dev->dev_private;
598 i915_kernel_lost_context(dev);
599 return i915_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
602 static int i915_flush_ioctl(struct drm_device *dev, void *data,
603 struct drm_file *file_priv)
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 (!dev_priv->allow_batchbuffer) {
628 DRM_ERROR("Batchbuffer ioctl disabled\n");
632 DRM_DEBUG("i915 batchbuffer, start %x used %d cliprects %d\n",
633 batch->start, batch->used, batch->num_cliprects);
635 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
637 if (batch->num_cliprects < 0)
640 if (batch->num_cliprects) {
641 cliprects = drm_calloc(batch->num_cliprects,
642 sizeof(struct drm_clip_rect),
644 if (cliprects == NULL)
647 ret = copy_from_user(cliprects, batch->cliprects,
648 batch->num_cliprects *
649 sizeof(struct drm_clip_rect));
654 mutex_lock(&dev->struct_mutex);
655 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
656 mutex_unlock(&dev->struct_mutex);
659 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
663 batch->num_cliprects * sizeof(struct drm_clip_rect),
669 static int i915_cmdbuffer(struct drm_device *dev, void *data,
670 struct drm_file *file_priv)
672 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
673 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
674 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
675 master_priv->sarea_priv;
676 drm_i915_cmdbuffer_t *cmdbuf = data;
677 struct drm_clip_rect *cliprects = NULL;
681 DRM_DEBUG("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
682 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
684 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
686 if (cmdbuf->num_cliprects < 0)
689 batch_data = drm_alloc(cmdbuf->sz, DRM_MEM_DRIVER);
690 if (batch_data == NULL)
693 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
695 goto fail_batch_free;
697 if (cmdbuf->num_cliprects) {
698 cliprects = drm_calloc(cmdbuf->num_cliprects,
699 sizeof(struct drm_clip_rect),
701 if (cliprects == NULL)
702 goto fail_batch_free;
704 ret = copy_from_user(cliprects, cmdbuf->cliprects,
705 cmdbuf->num_cliprects *
706 sizeof(struct drm_clip_rect));
711 mutex_lock(&dev->struct_mutex);
712 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
713 mutex_unlock(&dev->struct_mutex);
715 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
716 goto fail_batch_free;
720 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
723 drm_free(batch_data, cmdbuf->sz, DRM_MEM_DRIVER);
726 cmdbuf->num_cliprects * sizeof(struct drm_clip_rect),
732 static int i915_flip_bufs(struct drm_device *dev, void *data,
733 struct drm_file *file_priv)
737 DRM_DEBUG("%s\n", __func__);
739 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
741 mutex_lock(&dev->struct_mutex);
742 ret = i915_dispatch_flip(dev);
743 mutex_unlock(&dev->struct_mutex);
748 static int i915_getparam(struct drm_device *dev, void *data,
749 struct drm_file *file_priv)
751 drm_i915_private_t *dev_priv = dev->dev_private;
752 drm_i915_getparam_t *param = data;
756 DRM_ERROR("called with no initialization\n");
760 switch (param->param) {
761 case I915_PARAM_IRQ_ACTIVE:
762 value = dev->pdev->irq ? 1 : 0;
764 case I915_PARAM_ALLOW_BATCHBUFFER:
765 value = dev_priv->allow_batchbuffer ? 1 : 0;
767 case I915_PARAM_LAST_DISPATCH:
768 value = READ_BREADCRUMB(dev_priv);
770 case I915_PARAM_CHIPSET_ID:
771 value = dev->pci_device;
773 case I915_PARAM_HAS_GEM:
774 value = dev_priv->has_gem;
776 case I915_PARAM_NUM_FENCES_AVAIL:
777 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
780 DRM_DEBUG("Unknown parameter %d\n", param->param);
784 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
785 DRM_ERROR("DRM_COPY_TO_USER failed\n");
792 static int i915_setparam(struct drm_device *dev, void *data,
793 struct drm_file *file_priv)
795 drm_i915_private_t *dev_priv = dev->dev_private;
796 drm_i915_setparam_t *param = data;
799 DRM_ERROR("called with no initialization\n");
803 switch (param->param) {
804 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
806 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
807 dev_priv->tex_lru_log_granularity = param->value;
809 case I915_SETPARAM_ALLOW_BATCHBUFFER:
810 dev_priv->allow_batchbuffer = param->value;
812 case I915_SETPARAM_NUM_USED_FENCES:
813 if (param->value > dev_priv->num_fence_regs ||
816 /* Userspace can use first N regs */
817 dev_priv->fence_reg_start = param->value;
820 DRM_DEBUG("unknown parameter %d\n", param->param);
827 static int i915_set_status_page(struct drm_device *dev, void *data,
828 struct drm_file *file_priv)
830 drm_i915_private_t *dev_priv = dev->dev_private;
831 drm_i915_hws_addr_t *hws = data;
833 if (!I915_NEED_GFX_HWS(dev))
837 DRM_ERROR("called with no initialization\n");
841 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
842 WARN(1, "tried to set status page when mode setting active\n");
846 printk(KERN_DEBUG "set status page addr 0x%08x\n", (u32)hws->addr);
848 dev_priv->status_gfx_addr = hws->addr & (0x1ffff<<12);
850 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
851 dev_priv->hws_map.size = 4*1024;
852 dev_priv->hws_map.type = 0;
853 dev_priv->hws_map.flags = 0;
854 dev_priv->hws_map.mtrr = 0;
856 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
857 if (dev_priv->hws_map.handle == NULL) {
858 i915_dma_cleanup(dev);
859 dev_priv->status_gfx_addr = 0;
860 DRM_ERROR("can not ioremap virtual address for"
861 " G33 hw status page\n");
864 dev_priv->hw_status_page = dev_priv->hws_map.handle;
866 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
867 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
868 DRM_DEBUG("load hws HWS_PGA with gfx mem 0x%x\n",
869 dev_priv->status_gfx_addr);
870 DRM_DEBUG("load hws at %p\n", dev_priv->hw_status_page);
875 * i915_probe_agp - get AGP bootup configuration
877 * @aperture_size: returns AGP aperture configured size
878 * @preallocated_size: returns size of BIOS preallocated AGP space
880 * Since Intel integrated graphics are UMA, the BIOS has to set aside
881 * some RAM for the framebuffer at early boot. This code figures out
882 * how much was set aside so we can use it for our own purposes.
884 static int i915_probe_agp(struct drm_device *dev, unsigned long *aperture_size,
885 unsigned long *preallocated_size)
887 struct pci_dev *bridge_dev;
889 unsigned long overhead;
890 unsigned long stolen;
892 bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
894 DRM_ERROR("bridge device not found\n");
898 /* Get the fb aperture size and "stolen" memory amount. */
899 pci_read_config_word(bridge_dev, INTEL_GMCH_CTRL, &tmp);
900 pci_dev_put(bridge_dev);
902 *aperture_size = 1024 * 1024;
903 *preallocated_size = 1024 * 1024;
905 switch (dev->pdev->device) {
906 case PCI_DEVICE_ID_INTEL_82830_CGC:
907 case PCI_DEVICE_ID_INTEL_82845G_IG:
908 case PCI_DEVICE_ID_INTEL_82855GM_IG:
909 case PCI_DEVICE_ID_INTEL_82865_IG:
910 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
911 *aperture_size *= 64;
913 *aperture_size *= 128;
916 /* 9xx supports large sizes, just look at the length */
917 *aperture_size = pci_resource_len(dev->pdev, 2);
922 * Some of the preallocated space is taken by the GTT
923 * and popup. GTT is 1K per MB of aperture size, and popup is 4K.
925 if (IS_G4X(dev) || IS_IGD(dev))
928 overhead = (*aperture_size / 1024) + 4096;
930 switch (tmp & INTEL_GMCH_GMS_MASK) {
931 case INTEL_855_GMCH_GMS_DISABLED:
932 DRM_ERROR("video memory is disabled\n");
934 case INTEL_855_GMCH_GMS_STOLEN_1M:
935 stolen = 1 * 1024 * 1024;
937 case INTEL_855_GMCH_GMS_STOLEN_4M:
938 stolen = 4 * 1024 * 1024;
940 case INTEL_855_GMCH_GMS_STOLEN_8M:
941 stolen = 8 * 1024 * 1024;
943 case INTEL_855_GMCH_GMS_STOLEN_16M:
944 stolen = 16 * 1024 * 1024;
946 case INTEL_855_GMCH_GMS_STOLEN_32M:
947 stolen = 32 * 1024 * 1024;
949 case INTEL_915G_GMCH_GMS_STOLEN_48M:
950 stolen = 48 * 1024 * 1024;
952 case INTEL_915G_GMCH_GMS_STOLEN_64M:
953 stolen = 64 * 1024 * 1024;
955 case INTEL_GMCH_GMS_STOLEN_128M:
956 stolen = 128 * 1024 * 1024;
958 case INTEL_GMCH_GMS_STOLEN_256M:
959 stolen = 256 * 1024 * 1024;
961 case INTEL_GMCH_GMS_STOLEN_96M:
962 stolen = 96 * 1024 * 1024;
964 case INTEL_GMCH_GMS_STOLEN_160M:
965 stolen = 160 * 1024 * 1024;
967 case INTEL_GMCH_GMS_STOLEN_224M:
968 stolen = 224 * 1024 * 1024;
970 case INTEL_GMCH_GMS_STOLEN_352M:
971 stolen = 352 * 1024 * 1024;
974 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
975 tmp & INTEL_GMCH_GMS_MASK);
978 *preallocated_size = stolen - overhead;
983 static int i915_load_modeset_init(struct drm_device *dev)
985 struct drm_i915_private *dev_priv = dev->dev_private;
986 unsigned long agp_size, prealloc_size;
987 int fb_bar = IS_I9XX(dev) ? 2 : 0;
990 dev->devname = kstrdup(DRIVER_NAME, GFP_KERNEL);
996 dev->mode_config.fb_base = drm_get_resource_start(dev, fb_bar) &
999 if (IS_MOBILE(dev) || IS_I9XX(dev))
1000 dev_priv->cursor_needs_physical = true;
1002 dev_priv->cursor_needs_physical = false;
1004 if (IS_I965G(dev) || IS_G33(dev))
1005 dev_priv->cursor_needs_physical = false;
1007 ret = i915_probe_agp(dev, &agp_size, &prealloc_size);
1011 /* Basic memrange allocator for stolen space (aka vram) */
1012 drm_mm_init(&dev_priv->vram, 0, prealloc_size);
1014 /* Let GEM Manage from end of prealloc space to end of aperture */
1015 i915_gem_do_init(dev, prealloc_size, agp_size);
1017 ret = i915_gem_init_ringbuffer(dev);
1021 /* Allow hardware batchbuffers unless told otherwise.
1023 dev_priv->allow_batchbuffer = 1;
1025 ret = intel_init_bios(dev);
1027 DRM_INFO("failed to find VBIOS tables\n");
1029 ret = drm_irq_install(dev);
1031 goto destroy_ringbuffer;
1033 /* Always safe in the mode setting case. */
1034 /* FIXME: do pre/post-mode set stuff in core KMS code */
1035 dev->vblank_disable_allowed = 1;
1038 * Initialize the hardware status page IRQ location.
1041 I915_WRITE(INSTPM, (1 << 5) | (1 << 21));
1043 intel_modeset_init(dev);
1045 drm_helper_initial_config(dev);
1050 i915_gem_cleanup_ringbuffer(dev);
1052 kfree(dev->devname);
1057 int i915_master_create(struct drm_device *dev, struct drm_master *master)
1059 struct drm_i915_master_private *master_priv;
1061 master_priv = drm_calloc(1, sizeof(*master_priv), DRM_MEM_DRIVER);
1065 master->driver_priv = master_priv;
1069 void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1071 struct drm_i915_master_private *master_priv = master->driver_priv;
1076 drm_free(master_priv, sizeof(*master_priv), DRM_MEM_DRIVER);
1078 master->driver_priv = NULL;
1082 * i915_driver_load - setup chip and create an initial config
1084 * @flags: startup flags
1086 * The driver load routine has to do several things:
1087 * - drive output discovery via intel_modeset_init()
1088 * - initialize the memory manager
1089 * - allocate initial config memory
1090 * - setup the DRM framebuffer with the allocated memory
1092 int i915_driver_load(struct drm_device *dev, unsigned long flags)
1094 struct drm_i915_private *dev_priv = dev->dev_private;
1095 resource_size_t base, size;
1096 int ret = 0, mmio_bar = IS_I9XX(dev) ? 0 : 1;
1098 /* i915 has 4 more counters */
1100 dev->types[6] = _DRM_STAT_IRQ;
1101 dev->types[7] = _DRM_STAT_PRIMARY;
1102 dev->types[8] = _DRM_STAT_SECONDARY;
1103 dev->types[9] = _DRM_STAT_DMA;
1105 dev_priv = drm_alloc(sizeof(drm_i915_private_t), DRM_MEM_DRIVER);
1106 if (dev_priv == NULL)
1109 memset(dev_priv, 0, sizeof(drm_i915_private_t));
1111 dev->dev_private = (void *)dev_priv;
1112 dev_priv->dev = dev;
1114 /* Add register map (needed for suspend/resume) */
1115 base = drm_get_resource_start(dev, mmio_bar);
1116 size = drm_get_resource_len(dev, mmio_bar);
1118 dev_priv->regs = ioremap(base, size);
1119 if (!dev_priv->regs) {
1120 DRM_ERROR("failed to map registers\n");
1125 dev_priv->mm.gtt_mapping =
1126 io_mapping_create_wc(dev->agp->base,
1127 dev->agp->agp_info.aper_size * 1024*1024);
1128 if (dev_priv->mm.gtt_mapping == NULL) {
1133 /* Set up a WC MTRR for non-PAT systems. This is more common than
1134 * one would think, because the kernel disables PAT on first
1135 * generation Core chips because WC PAT gets overridden by a UC
1136 * MTRR if present. Even if a UC MTRR isn't present.
1138 dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base,
1139 dev->agp->agp_info.aper_size *
1141 MTRR_TYPE_WRCOMB, 1);
1142 if (dev_priv->mm.gtt_mtrr < 0) {
1143 DRM_INFO("MTRR allocation failed. Graphics "
1144 "performance may suffer.\n");
1147 #ifdef CONFIG_HIGHMEM64G
1148 /* don't enable GEM on PAE - needs agp + set_memory_* interface fixes */
1149 dev_priv->has_gem = 0;
1151 /* enable GEM by default */
1152 dev_priv->has_gem = 1;
1155 dev->driver->get_vblank_counter = i915_get_vblank_counter;
1157 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
1162 if (!I915_NEED_GFX_HWS(dev)) {
1163 ret = i915_init_phys_hws(dev);
1168 /* On the 945G/GM, the chipset reports the MSI capability on the
1169 * integrated graphics even though the support isn't actually there
1170 * according to the published specs. It doesn't appear to function
1171 * correctly in testing on 945G.
1172 * This may be a side effect of MSI having been made available for PEG
1173 * and the registers being closely associated.
1175 * According to chipset errata, on the 965GM, MSI interrupts may
1176 * be lost or delayed, but we use them anyways to avoid
1177 * stuck interrupts on some machines.
1179 if (!IS_I945G(dev) && !IS_I945GM(dev))
1180 pci_enable_msi(dev->pdev);
1182 intel_opregion_init(dev);
1184 spin_lock_init(&dev_priv->user_irq_lock);
1185 dev_priv->user_irq_refcount = 0;
1187 ret = drm_vblank_init(dev, I915_NUM_PIPE);
1190 (void) i915_driver_unload(dev);
1194 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1195 ret = i915_load_modeset_init(dev);
1197 DRM_ERROR("failed to init modeset\n");
1205 io_mapping_free(dev_priv->mm.gtt_mapping);
1207 iounmap(dev_priv->regs);
1209 drm_free(dev_priv, sizeof(struct drm_i915_private), DRM_MEM_DRIVER);
1213 int i915_driver_unload(struct drm_device *dev)
1215 struct drm_i915_private *dev_priv = dev->dev_private;
1217 io_mapping_free(dev_priv->mm.gtt_mapping);
1218 if (dev_priv->mm.gtt_mtrr >= 0) {
1219 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
1220 dev->agp->agp_info.aper_size * 1024 * 1024);
1221 dev_priv->mm.gtt_mtrr = -1;
1224 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1225 drm_irq_uninstall(dev);
1228 if (dev->pdev->msi_enabled)
1229 pci_disable_msi(dev->pdev);
1231 if (dev_priv->regs != NULL)
1232 iounmap(dev_priv->regs);
1234 intel_opregion_free(dev);
1236 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1237 intel_modeset_cleanup(dev);
1239 i915_gem_free_all_phys_object(dev);
1241 mutex_lock(&dev->struct_mutex);
1242 i915_gem_cleanup_ringbuffer(dev);
1243 mutex_unlock(&dev->struct_mutex);
1244 drm_mm_takedown(&dev_priv->vram);
1245 i915_gem_lastclose(dev);
1248 drm_free(dev->dev_private, sizeof(drm_i915_private_t),
1254 int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1256 struct drm_i915_file_private *i915_file_priv;
1259 i915_file_priv = (struct drm_i915_file_private *)
1260 drm_alloc(sizeof(*i915_file_priv), DRM_MEM_FILES);
1262 if (!i915_file_priv)
1265 file_priv->driver_priv = i915_file_priv;
1267 i915_file_priv->mm.last_gem_seqno = 0;
1268 i915_file_priv->mm.last_gem_throttle_seqno = 0;
1274 * i915_driver_lastclose - clean up after all DRM clients have exited
1277 * Take care of cleaning up after all DRM clients have exited. In the
1278 * mode setting case, we want to restore the kernel's initial mode (just
1279 * in case the last client left us in a bad state).
1281 * Additionally, in the non-mode setting case, we'll tear down the AGP
1282 * and DMA structures, since the kernel won't be using them, and clea
1285 void i915_driver_lastclose(struct drm_device * dev)
1287 drm_i915_private_t *dev_priv = dev->dev_private;
1289 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
1294 i915_gem_lastclose(dev);
1296 if (dev_priv->agp_heap)
1297 i915_mem_takedown(&(dev_priv->agp_heap));
1299 i915_dma_cleanup(dev);
1302 void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
1304 drm_i915_private_t *dev_priv = dev->dev_private;
1305 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1306 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
1309 void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
1311 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1313 drm_free(i915_file_priv, sizeof(*i915_file_priv), DRM_MEM_FILES);
1316 struct drm_ioctl_desc i915_ioctls[] = {
1317 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1318 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1319 DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
1320 DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1321 DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1322 DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1323 DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
1324 DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1325 DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH),
1326 DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH),
1327 DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1328 DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
1329 DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1330 DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1331 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
1332 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
1333 DRM_IOCTL_DEF(DRM_I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1334 DRM_IOCTL_DEF(DRM_I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1335 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
1336 DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1337 DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1338 DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH),
1339 DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH),
1340 DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1341 DRM_IOCTL_DEF(DRM_I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1342 DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, 0),
1343 DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, 0),
1344 DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, 0),
1345 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, 0),
1346 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, 0),
1347 DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, 0),
1348 DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, 0),
1349 DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, 0),
1350 DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, 0),
1351 DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, 0),
1354 int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
1357 * Determine if the device really is AGP or not.
1359 * All Intel graphics chipsets are treated as AGP, even if they are really
1362 * \param dev The device to be tested.
1365 * A value of 1 is always retured to indictate every i9x5 is AGP.
1367 int i915_driver_device_is_agp(struct drm_device * dev)