Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* i915_dma.c -- DMA support for the I915 -*- linux-c -*- |
| 2 | */ |
Dave Airlie | 0d6aa60 | 2006-01-02 20:14:23 +1100 | [diff] [blame] | 3 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas. |
| 5 | * All Rights Reserved. |
Dave Airlie | bc54fd1 | 2005-06-23 22:46:46 +1000 | [diff] [blame] | 6 | * |
| 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: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice (including the |
| 16 | * next paragraph) shall be included in all copies or substantial portions |
| 17 | * of the Software. |
| 18 | * |
| 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. |
| 26 | * |
Dave Airlie | 0d6aa60 | 2006-01-02 20:14:23 +1100 | [diff] [blame] | 27 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include "drmP.h" |
| 30 | #include "drm.h" |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 31 | #include "drm_crtc_helper.h" |
| 32 | #include "intel_drv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "i915_drm.h" |
| 34 | #include "i915_drv.h" |
| 35 | |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 36 | #define I915_DRV "i915_drv" |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | /* Really want an OS-independent resettable timer. Would like to have |
| 39 | * this loop run for (eg) 3 sec, but have the timer reset every time |
| 40 | * the head pointer changes, so that EBUSY only happens if the ring |
| 41 | * actually stalls for (eg) 3 seconds. |
| 42 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 43 | int i915_wait_ring(struct drm_device * dev, int n, const char *caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 46 | drm_i915_ring_buffer_t *ring = &(dev_priv->ring); |
Keith Packard | d3a6d44 | 2008-07-30 12:21:20 -0700 | [diff] [blame] | 47 | u32 acthd_reg = IS_I965G(dev) ? ACTHD_I965 : ACTHD; |
| 48 | u32 last_acthd = I915_READ(acthd_reg); |
| 49 | u32 acthd; |
Jesse Barnes | 585fb11 | 2008-07-29 11:54:06 -0700 | [diff] [blame] | 50 | u32 last_head = I915_READ(PRB0_HEAD) & HEAD_ADDR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | int i; |
| 52 | |
Keith Packard | d3a6d44 | 2008-07-30 12:21:20 -0700 | [diff] [blame] | 53 | for (i = 0; i < 100000; i++) { |
Jesse Barnes | 585fb11 | 2008-07-29 11:54:06 -0700 | [diff] [blame] | 54 | ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR; |
Keith Packard | d3a6d44 | 2008-07-30 12:21:20 -0700 | [diff] [blame] | 55 | acthd = I915_READ(acthd_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | ring->space = ring->head - (ring->tail + 8); |
| 57 | if (ring->space < 0) |
| 58 | ring->space += ring->Size; |
| 59 | if (ring->space >= n) |
| 60 | return 0; |
| 61 | |
Chris Wilson | 98787c0 | 2009-03-06 23:27:52 +0000 | [diff] [blame] | 62 | if (dev->primary->master) { |
| 63 | struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv; |
| 64 | if (master_priv->sarea_priv) |
| 65 | master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT; |
| 66 | } |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | if (ring->head != last_head) |
| 70 | i = 0; |
Keith Packard | d3a6d44 | 2008-07-30 12:21:20 -0700 | [diff] [blame] | 71 | if (acthd != last_acthd) |
| 72 | i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | last_head = ring->head; |
Keith Packard | d3a6d44 | 2008-07-30 12:21:20 -0700 | [diff] [blame] | 75 | last_acthd = acthd; |
| 76 | msleep_interruptible(10); |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 80 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Keith Packard | 398c9cb | 2008-07-30 13:03:43 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Sets up the hardware status page for devices that need a physical address |
| 85 | * in the register. |
| 86 | */ |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 87 | static int i915_init_phys_hws(struct drm_device *dev) |
Keith Packard | 398c9cb | 2008-07-30 13:03:43 -0700 | [diff] [blame] | 88 | { |
| 89 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 90 | /* Program Hardware Status Page */ |
| 91 | dev_priv->status_page_dmah = |
| 92 | drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff); |
| 93 | |
| 94 | if (!dev_priv->status_page_dmah) { |
| 95 | DRM_ERROR("Can not allocate hardware status page\n"); |
| 96 | return -ENOMEM; |
| 97 | } |
| 98 | dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr; |
| 99 | dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr; |
| 100 | |
| 101 | memset(dev_priv->hw_status_page, 0, PAGE_SIZE); |
| 102 | |
| 103 | I915_WRITE(HWS_PGA, dev_priv->dma_status_page); |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 104 | DRM_DEBUG_DRIVER(I915_DRV, "Enabled hardware status page\n"); |
Keith Packard | 398c9cb | 2008-07-30 13:03:43 -0700 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Frees the hardware status page, whether it's a physical address or a virtual |
| 110 | * address set up by the X Server. |
| 111 | */ |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 112 | static void i915_free_hws(struct drm_device *dev) |
Keith Packard | 398c9cb | 2008-07-30 13:03:43 -0700 | [diff] [blame] | 113 | { |
| 114 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 115 | if (dev_priv->status_page_dmah) { |
| 116 | drm_pci_free(dev, dev_priv->status_page_dmah); |
| 117 | dev_priv->status_page_dmah = NULL; |
| 118 | } |
| 119 | |
| 120 | if (dev_priv->status_gfx_addr) { |
| 121 | dev_priv->status_gfx_addr = 0; |
| 122 | drm_core_ioremapfree(&dev_priv->hws_map, dev); |
| 123 | } |
| 124 | |
| 125 | /* Need to rewrite hardware status page */ |
| 126 | I915_WRITE(HWS_PGA, 0x1ffff000); |
| 127 | } |
| 128 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 129 | void i915_kernel_lost_context(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
| 131 | drm_i915_private_t *dev_priv = dev->dev_private; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 132 | struct drm_i915_master_private *master_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | drm_i915_ring_buffer_t *ring = &(dev_priv->ring); |
| 134 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 135 | /* |
| 136 | * We should never lose context on the ring with modesetting |
| 137 | * as we don't expose it to userspace |
| 138 | */ |
| 139 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 140 | return; |
| 141 | |
Jesse Barnes | 585fb11 | 2008-07-29 11:54:06 -0700 | [diff] [blame] | 142 | ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR; |
| 143 | ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | ring->space = ring->head - (ring->tail + 8); |
| 145 | if (ring->space < 0) |
| 146 | ring->space += ring->Size; |
| 147 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 148 | if (!dev->primary->master) |
| 149 | return; |
| 150 | |
| 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 156 | static int i915_dma_cleanup(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 158 | drm_i915_private_t *dev_priv = dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | /* Make sure interrupts are disabled here because the uninstall ioctl |
| 160 | * may not have been called from userspace and after dev_private |
| 161 | * is freed, it's too late. |
| 162 | */ |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 163 | if (dev->irq_enabled) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 164 | drm_irq_uninstall(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 166 | if (dev_priv->ring.virtual_start) { |
| 167 | drm_core_ioremapfree(&dev_priv->ring.map, dev); |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 168 | dev_priv->ring.virtual_start = NULL; |
| 169 | dev_priv->ring.map.handle = NULL; |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 170 | dev_priv->ring.map.size = 0; |
| 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Keith Packard | 398c9cb | 2008-07-30 13:03:43 -0700 | [diff] [blame] | 173 | /* Clear the HWS virtual address at teardown */ |
| 174 | if (I915_NEED_GFX_HWS(dev)) |
| 175 | i915_free_hws(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 180 | static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 182 | drm_i915_private_t *dev_priv = dev->dev_private; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 183 | struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Dave Airlie | 3a03ac1 | 2009-01-11 09:03:49 +1000 | [diff] [blame] | 185 | master_priv->sarea = drm_getsarea(dev); |
| 186 | if (master_priv->sarea) { |
| 187 | master_priv->sarea_priv = (drm_i915_sarea_t *) |
| 188 | ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset); |
| 189 | } else { |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 190 | DRM_DEBUG_DRIVER(I915_DRV, |
| 191 | "sarea not found assuming DRI2 userspace\n"); |
Dave Airlie | 3a03ac1 | 2009-01-11 09:03:49 +1000 | [diff] [blame] | 192 | } |
| 193 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 194 | if (init->ring_size != 0) { |
| 195 | if (dev_priv->ring.ring_obj != NULL) { |
| 196 | i915_dma_cleanup(dev); |
| 197 | DRM_ERROR("Client tried to initialize ringbuffer in " |
| 198 | "GEM mode\n"); |
| 199 | return -EINVAL; |
| 200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 202 | dev_priv->ring.Size = init->ring_size; |
| 203 | dev_priv->ring.tail_mask = dev_priv->ring.Size - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 205 | dev_priv->ring.map.offset = init->ring_start; |
| 206 | dev_priv->ring.map.size = init->ring_size; |
| 207 | dev_priv->ring.map.type = 0; |
| 208 | dev_priv->ring.map.flags = 0; |
| 209 | dev_priv->ring.map.mtrr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Jesse Barnes | 6fb8858 | 2009-02-23 10:08:21 +1000 | [diff] [blame] | 211 | drm_core_ioremap_wc(&dev_priv->ring.map, dev); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 212 | |
| 213 | if (dev_priv->ring.map.handle == NULL) { |
| 214 | i915_dma_cleanup(dev); |
| 215 | DRM_ERROR("can not ioremap virtual address for" |
| 216 | " ring buffer\n"); |
| 217 | return -ENOMEM; |
| 218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | dev_priv->ring.virtual_start = dev_priv->ring.map.handle; |
| 222 | |
=?utf-8?q?Michel_D=C3=A4nzer?= | a6b54f3 | 2006-10-24 23:37:43 +1000 | [diff] [blame] | 223 | dev_priv->cpp = init->cpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | dev_priv->back_offset = init->back_offset; |
| 225 | dev_priv->front_offset = init->front_offset; |
| 226 | dev_priv->current_page = 0; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 227 | if (master_priv->sarea_priv) |
| 228 | master_priv->sarea_priv->pf_current_page = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | /* Allow hardware batchbuffers unless told otherwise. |
| 231 | */ |
| 232 | dev_priv->allow_batchbuffer = 1; |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 237 | static int i915_dma_resume(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
| 239 | drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; |
| 240 | |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 241 | DRM_DEBUG_DRIVER(I915_DRV, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | if (dev_priv->ring.map.handle == NULL) { |
| 244 | DRM_ERROR("can not ioremap virtual address for" |
| 245 | " ring buffer\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 246 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* Program Hardware Status Page */ |
| 250 | if (!dev_priv->hw_status_page) { |
| 251 | DRM_ERROR("Can not find hardware status page\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 252 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 254 | DRM_DEBUG_DRIVER(I915_DRV, "hw status page @ %p\n", |
| 255 | dev_priv->hw_status_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 257 | if (dev_priv->status_gfx_addr != 0) |
Jesse Barnes | 585fb11 | 2008-07-29 11:54:06 -0700 | [diff] [blame] | 258 | I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr); |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 259 | else |
Jesse Barnes | 585fb11 | 2008-07-29 11:54:06 -0700 | [diff] [blame] | 260 | I915_WRITE(HWS_PGA, dev_priv->dma_status_page); |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 261 | DRM_DEBUG_DRIVER(I915_DRV, "Enabled hardware status page\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 266 | static int i915_dma_init(struct drm_device *dev, void *data, |
| 267 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 269 | drm_i915_init_t *init = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | int retcode = 0; |
| 271 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 272 | switch (init->func) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | case I915_INIT_DMA: |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 274 | retcode = i915_initialize(dev, init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | break; |
| 276 | case I915_CLEANUP_DMA: |
| 277 | retcode = i915_dma_cleanup(dev); |
| 278 | break; |
| 279 | case I915_RESUME_DMA: |
Dave Airlie | 0d6aa60 | 2006-01-02 20:14:23 +1100 | [diff] [blame] | 280 | retcode = i915_dma_resume(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | break; |
| 282 | default: |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 283 | retcode = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | |
| 287 | return retcode; |
| 288 | } |
| 289 | |
| 290 | /* Implement basically the same security restrictions as hardware does |
| 291 | * for MI_BATCH_NON_SECURE. These can be made stricter at any time. |
| 292 | * |
| 293 | * Most of the calculations below involve calculating the size of a |
| 294 | * particular instruction. It's important to get the size right as |
| 295 | * that tells us where the next instruction to check is. Any illegal |
| 296 | * instruction detected will be given a size of zero, which is a |
| 297 | * signal to abort the rest of the buffer. |
| 298 | */ |
| 299 | static int do_validate_cmd(int cmd) |
| 300 | { |
| 301 | switch (((cmd >> 29) & 0x7)) { |
| 302 | case 0x0: |
| 303 | switch ((cmd >> 23) & 0x3f) { |
| 304 | case 0x0: |
| 305 | return 1; /* MI_NOOP */ |
| 306 | case 0x4: |
| 307 | return 1; /* MI_FLUSH */ |
| 308 | default: |
| 309 | return 0; /* disallow everything else */ |
| 310 | } |
| 311 | break; |
| 312 | case 0x1: |
| 313 | return 0; /* reserved */ |
| 314 | case 0x2: |
| 315 | return (cmd & 0xff) + 2; /* 2d commands */ |
| 316 | case 0x3: |
| 317 | if (((cmd >> 24) & 0x1f) <= 0x18) |
| 318 | return 1; |
| 319 | |
| 320 | switch ((cmd >> 24) & 0x1f) { |
| 321 | case 0x1c: |
| 322 | return 1; |
| 323 | case 0x1d: |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 324 | switch ((cmd >> 16) & 0xff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | case 0x3: |
| 326 | return (cmd & 0x1f) + 2; |
| 327 | case 0x4: |
| 328 | return (cmd & 0xf) + 2; |
| 329 | default: |
| 330 | return (cmd & 0xffff) + 2; |
| 331 | } |
| 332 | case 0x1e: |
| 333 | if (cmd & (1 << 23)) |
| 334 | return (cmd & 0xffff) + 1; |
| 335 | else |
| 336 | return 1; |
| 337 | case 0x1f: |
| 338 | if ((cmd & (1 << 23)) == 0) /* inline vertices */ |
| 339 | return (cmd & 0x1ffff) + 2; |
| 340 | else if (cmd & (1 << 17)) /* indirect random */ |
| 341 | if ((cmd & 0xffff) == 0) |
| 342 | return 0; /* unknown length, too hard */ |
| 343 | else |
| 344 | return (((cmd & 0xffff) + 1) / 2) + 1; |
| 345 | else |
| 346 | return 2; /* indirect sequential */ |
| 347 | default: |
| 348 | return 0; |
| 349 | } |
| 350 | default: |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static int validate_cmd(int cmd) |
| 358 | { |
| 359 | int ret = do_validate_cmd(cmd); |
| 360 | |
Dave Airlie | bc5f452 | 2007-11-05 12:50:58 +1000 | [diff] [blame] | 361 | /* printk("validate_cmd( %x ): %d\n", cmd, ret); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 366 | static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
| 368 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 369 | int i; |
| 370 | RING_LOCALS; |
| 371 | |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 372 | if ((dwords+1) * sizeof(int) >= dev_priv->ring.Size - 8) |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 373 | return -EINVAL; |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 374 | |
Alan Hourihane | c29b669 | 2006-08-12 16:29:24 +1000 | [diff] [blame] | 375 | BEGIN_LP_RING((dwords+1)&~1); |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | for (i = 0; i < dwords;) { |
| 378 | int cmd, sz; |
| 379 | |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 380 | cmd = buffer[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords) |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 383 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | OUT_RING(cmd); |
| 386 | |
| 387 | while (++i, --sz) { |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 388 | OUT_RING(buffer[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 392 | if (dwords & 1) |
| 393 | OUT_RING(0); |
| 394 | |
| 395 | ADVANCE_LP_RING(); |
| 396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 400 | int |
| 401 | i915_emit_box(struct drm_device *dev, |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 402 | struct drm_clip_rect *boxes, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 403 | int i, int DR1, int DR4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | { |
| 405 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 406 | struct drm_clip_rect box = boxes[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | RING_LOCALS; |
| 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) { |
| 410 | DRM_ERROR("Bad box %d,%d..%d,%d\n", |
| 411 | box.x1, box.y1, box.x2, box.y2); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 412 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Alan Hourihane | c29b669 | 2006-08-12 16:29:24 +1000 | [diff] [blame] | 415 | if (IS_I965G(dev)) { |
| 416 | BEGIN_LP_RING(4); |
| 417 | OUT_RING(GFX_OP_DRAWRECT_INFO_I965); |
| 418 | OUT_RING((box.x1 & 0xffff) | (box.y1 << 16)); |
Andrew Morton | 78eca43 | 2006-08-16 09:15:51 +1000 | [diff] [blame] | 419 | OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16)); |
Alan Hourihane | c29b669 | 2006-08-12 16:29:24 +1000 | [diff] [blame] | 420 | OUT_RING(DR4); |
| 421 | ADVANCE_LP_RING(); |
| 422 | } else { |
| 423 | BEGIN_LP_RING(6); |
| 424 | OUT_RING(GFX_OP_DRAWRECT_INFO); |
| 425 | OUT_RING(DR1); |
| 426 | OUT_RING((box.x1 & 0xffff) | (box.y1 << 16)); |
| 427 | OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16)); |
| 428 | OUT_RING(DR4); |
| 429 | OUT_RING(0); |
| 430 | ADVANCE_LP_RING(); |
| 431 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
Alan Hourihane | c29b669 | 2006-08-12 16:29:24 +1000 | [diff] [blame] | 436 | /* XXX: Emitting the counter should really be moved to part of the IRQ |
| 437 | * emit. For now, do it in both places: |
| 438 | */ |
| 439 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 440 | static void i915_emit_breadcrumb(struct drm_device *dev) |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 441 | { |
| 442 | drm_i915_private_t *dev_priv = dev->dev_private; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 443 | struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv; |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 444 | RING_LOCALS; |
| 445 | |
Kristian Høgsberg | c99b058 | 2008-08-20 11:20:13 -0400 | [diff] [blame] | 446 | dev_priv->counter++; |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 447 | if (dev_priv->counter > 0x7FFFFFFFUL) |
Kristian Høgsberg | c99b058 | 2008-08-20 11:20:13 -0400 | [diff] [blame] | 448 | dev_priv->counter = 0; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 449 | if (master_priv->sarea_priv) |
| 450 | master_priv->sarea_priv->last_enqueue = dev_priv->counter; |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 451 | |
| 452 | BEGIN_LP_RING(4); |
Jesse Barnes | 585fb11 | 2008-07-29 11:54:06 -0700 | [diff] [blame] | 453 | OUT_RING(MI_STORE_DWORD_INDEX); |
Keith Packard | 0baf823 | 2008-11-08 11:44:14 +1000 | [diff] [blame] | 454 | OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT); |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 455 | OUT_RING(dev_priv->counter); |
| 456 | OUT_RING(0); |
| 457 | ADVANCE_LP_RING(); |
| 458 | } |
| 459 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 460 | static int i915_dispatch_cmdbuffer(struct drm_device * dev, |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 461 | drm_i915_cmdbuffer_t *cmd, |
| 462 | struct drm_clip_rect *cliprects, |
| 463 | void *cmdbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
| 465 | int nbox = cmd->num_cliprects; |
| 466 | int i = 0, count, ret; |
| 467 | |
| 468 | if (cmd->sz & 0x3) { |
| 469 | DRM_ERROR("alignment"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 470 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | i915_kernel_lost_context(dev); |
| 474 | |
| 475 | count = nbox ? nbox : 1; |
| 476 | |
| 477 | for (i = 0; i < count; i++) { |
| 478 | if (i < nbox) { |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 479 | ret = i915_emit_box(dev, cliprects, i, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | cmd->DR1, cmd->DR4); |
| 481 | if (ret) |
| 482 | return ret; |
| 483 | } |
| 484 | |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 485 | ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | if (ret) |
| 487 | return ret; |
| 488 | } |
| 489 | |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 490 | i915_emit_breadcrumb(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 494 | static int i915_dispatch_batchbuffer(struct drm_device * dev, |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 495 | drm_i915_batchbuffer_t * batch, |
| 496 | struct drm_clip_rect *cliprects) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | { |
| 498 | drm_i915_private_t *dev_priv = dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | int nbox = batch->num_cliprects; |
| 500 | int i = 0, count; |
| 501 | RING_LOCALS; |
| 502 | |
| 503 | if ((batch->start | batch->used) & 0x7) { |
| 504 | DRM_ERROR("alignment"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 505 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | i915_kernel_lost_context(dev); |
| 509 | |
| 510 | count = nbox ? nbox : 1; |
| 511 | |
| 512 | for (i = 0; i < count; i++) { |
| 513 | if (i < nbox) { |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 514 | int ret = i915_emit_box(dev, cliprects, i, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | batch->DR1, batch->DR4); |
| 516 | if (ret) |
| 517 | return ret; |
| 518 | } |
| 519 | |
Keith Packard | 0790d5e | 2008-07-30 12:28:47 -0700 | [diff] [blame] | 520 | if (!IS_I830(dev) && !IS_845G(dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | BEGIN_LP_RING(2); |
Dave Airlie | 21f1628 | 2007-08-07 09:09:51 +1000 | [diff] [blame] | 522 | if (IS_I965G(dev)) { |
| 523 | OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965); |
| 524 | OUT_RING(batch->start); |
| 525 | } else { |
| 526 | OUT_RING(MI_BATCH_BUFFER_START | (2 << 6)); |
| 527 | OUT_RING(batch->start | MI_BATCH_NON_SECURE); |
| 528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | ADVANCE_LP_RING(); |
| 530 | } else { |
| 531 | BEGIN_LP_RING(4); |
| 532 | OUT_RING(MI_BATCH_BUFFER); |
| 533 | OUT_RING(batch->start | MI_BATCH_NON_SECURE); |
| 534 | OUT_RING(batch->start + batch->used - 4); |
| 535 | OUT_RING(0); |
| 536 | ADVANCE_LP_RING(); |
| 537 | } |
| 538 | } |
| 539 | |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 540 | i915_emit_breadcrumb(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 545 | static int i915_dispatch_flip(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | { |
| 547 | drm_i915_private_t *dev_priv = dev->dev_private; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 548 | struct drm_i915_master_private *master_priv = |
| 549 | dev->primary->master->driver_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | RING_LOCALS; |
| 551 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 552 | if (!master_priv->sarea_priv) |
Kristian Høgsberg | c99b058 | 2008-08-20 11:20:13 -0400 | [diff] [blame] | 553 | return -EINVAL; |
| 554 | |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 555 | DRM_DEBUG_DRIVER(I915_DRV, "%s: page=%d pfCurrentPage=%d\n", |
| 556 | __func__, |
| 557 | dev_priv->current_page, |
| 558 | master_priv->sarea_priv->pf_current_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 560 | i915_kernel_lost_context(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 562 | BEGIN_LP_RING(2); |
Jesse Barnes | 585fb11 | 2008-07-29 11:54:06 -0700 | [diff] [blame] | 563 | OUT_RING(MI_FLUSH | MI_READ_FLUSH); |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 564 | OUT_RING(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | ADVANCE_LP_RING(); |
| 566 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 567 | BEGIN_LP_RING(6); |
| 568 | OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP); |
| 569 | OUT_RING(0); |
| 570 | if (dev_priv->current_page == 0) { |
| 571 | OUT_RING(dev_priv->back_offset); |
| 572 | dev_priv->current_page = 1; |
| 573 | } else { |
| 574 | OUT_RING(dev_priv->front_offset); |
| 575 | dev_priv->current_page = 0; |
| 576 | } |
| 577 | OUT_RING(0); |
| 578 | ADVANCE_LP_RING(); |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 579 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 580 | BEGIN_LP_RING(2); |
| 581 | OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP); |
| 582 | OUT_RING(0); |
| 583 | ADVANCE_LP_RING(); |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 584 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 585 | master_priv->sarea_priv->last_enqueue = dev_priv->counter++; |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 586 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 587 | BEGIN_LP_RING(4); |
Jesse Barnes | 585fb11 | 2008-07-29 11:54:06 -0700 | [diff] [blame] | 588 | OUT_RING(MI_STORE_DWORD_INDEX); |
Keith Packard | 0baf823 | 2008-11-08 11:44:14 +1000 | [diff] [blame] | 589 | OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT); |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 590 | OUT_RING(dev_priv->counter); |
| 591 | OUT_RING(0); |
| 592 | ADVANCE_LP_RING(); |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 593 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 594 | master_priv->sarea_priv->pf_current_page = dev_priv->current_page; |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 595 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 598 | static int i915_quiescent(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | { |
| 600 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 601 | |
| 602 | i915_kernel_lost_context(dev); |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 603 | return i915_wait_ring(dev, dev_priv->ring.Size - 8, __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 606 | static int i915_flush_ioctl(struct drm_device *dev, void *data, |
| 607 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | { |
Eric Anholt | 546b097 | 2008-09-01 16:45:29 -0700 | [diff] [blame] | 609 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
Eric Anholt | 546b097 | 2008-09-01 16:45:29 -0700 | [diff] [blame] | 611 | RING_LOCK_TEST_WITH_RETURN(dev, file_priv); |
| 612 | |
| 613 | mutex_lock(&dev->struct_mutex); |
| 614 | ret = i915_quiescent(dev); |
| 615 | mutex_unlock(&dev->struct_mutex); |
| 616 | |
| 617 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 620 | static int i915_batchbuffer(struct drm_device *dev, void *data, |
| 621 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 624 | struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *) |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 626 | master_priv->sarea_priv; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 627 | drm_i915_batchbuffer_t *batch = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | int ret; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 629 | struct drm_clip_rect *cliprects = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
| 631 | if (!dev_priv->allow_batchbuffer) { |
| 632 | DRM_ERROR("Batchbuffer ioctl disabled\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 633 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 636 | DRM_DEBUG_DRIVER(I915_DRV, |
| 637 | "i915 batchbuffer, start %x used %d cliprects %d\n", |
| 638 | batch->start, batch->used, batch->num_cliprects); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Eric Anholt | 546b097 | 2008-09-01 16:45:29 -0700 | [diff] [blame] | 640 | RING_LOCK_TEST_WITH_RETURN(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 642 | if (batch->num_cliprects < 0) |
| 643 | return -EINVAL; |
| 644 | |
| 645 | if (batch->num_cliprects) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 646 | cliprects = kcalloc(batch->num_cliprects, |
| 647 | sizeof(struct drm_clip_rect), |
| 648 | GFP_KERNEL); |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 649 | if (cliprects == NULL) |
| 650 | return -ENOMEM; |
| 651 | |
| 652 | ret = copy_from_user(cliprects, batch->cliprects, |
| 653 | batch->num_cliprects * |
| 654 | sizeof(struct drm_clip_rect)); |
| 655 | if (ret != 0) |
| 656 | goto fail_free; |
| 657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
Eric Anholt | 546b097 | 2008-09-01 16:45:29 -0700 | [diff] [blame] | 659 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 660 | ret = i915_dispatch_batchbuffer(dev, batch, cliprects); |
Eric Anholt | 546b097 | 2008-09-01 16:45:29 -0700 | [diff] [blame] | 661 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Kristian Høgsberg | c99b058 | 2008-08-20 11:20:13 -0400 | [diff] [blame] | 663 | if (sarea_priv) |
Keith Packard | 0baf823 | 2008-11-08 11:44:14 +1000 | [diff] [blame] | 664 | sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv); |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 665 | |
| 666 | fail_free: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 667 | kfree(cliprects); |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | return ret; |
| 670 | } |
| 671 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 672 | static int i915_cmdbuffer(struct drm_device *dev, void *data, |
| 673 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 676 | struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *) |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 678 | master_priv->sarea_priv; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 679 | drm_i915_cmdbuffer_t *cmdbuf = data; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 680 | struct drm_clip_rect *cliprects = NULL; |
| 681 | void *batch_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | int ret; |
| 683 | |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 684 | DRM_DEBUG_DRIVER(I915_DRV, |
| 685 | "i915 cmdbuffer, buf %p sz %d cliprects %d\n", |
| 686 | cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Eric Anholt | 546b097 | 2008-09-01 16:45:29 -0700 | [diff] [blame] | 688 | RING_LOCK_TEST_WITH_RETURN(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 690 | if (cmdbuf->num_cliprects < 0) |
| 691 | return -EINVAL; |
| 692 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 693 | batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL); |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 694 | if (batch_data == NULL) |
| 695 | return -ENOMEM; |
| 696 | |
| 697 | ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz); |
| 698 | if (ret != 0) |
| 699 | goto fail_batch_free; |
| 700 | |
| 701 | if (cmdbuf->num_cliprects) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 702 | cliprects = kcalloc(cmdbuf->num_cliprects, |
| 703 | sizeof(struct drm_clip_rect), GFP_KERNEL); |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 704 | if (cliprects == NULL) |
| 705 | goto fail_batch_free; |
| 706 | |
| 707 | ret = copy_from_user(cliprects, cmdbuf->cliprects, |
| 708 | cmdbuf->num_cliprects * |
| 709 | sizeof(struct drm_clip_rect)); |
| 710 | if (ret != 0) |
| 711 | goto fail_clip_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Eric Anholt | 546b097 | 2008-09-01 16:45:29 -0700 | [diff] [blame] | 714 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 715 | ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data); |
Eric Anholt | 546b097 | 2008-09-01 16:45:29 -0700 | [diff] [blame] | 716 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | if (ret) { |
| 718 | DRM_ERROR("i915_dispatch_cmdbuffer failed\n"); |
Chris Wright | 355d7f3 | 2009-04-17 01:18:55 +0000 | [diff] [blame] | 719 | goto fail_clip_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Kristian Høgsberg | c99b058 | 2008-08-20 11:20:13 -0400 | [diff] [blame] | 722 | if (sarea_priv) |
Keith Packard | 0baf823 | 2008-11-08 11:44:14 +1000 | [diff] [blame] | 723 | sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv); |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 724 | |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 725 | fail_clip_free: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 726 | kfree(cliprects); |
Chris Wright | 355d7f3 | 2009-04-17 01:18:55 +0000 | [diff] [blame] | 727 | fail_batch_free: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 728 | kfree(batch_data); |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 729 | |
| 730 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 733 | static int i915_flip_bufs(struct drm_device *dev, void *data, |
| 734 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | { |
Eric Anholt | 546b097 | 2008-09-01 16:45:29 -0700 | [diff] [blame] | 736 | int ret; |
| 737 | |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 738 | DRM_DEBUG_DRIVER(I915_DRV, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
Eric Anholt | 546b097 | 2008-09-01 16:45:29 -0700 | [diff] [blame] | 740 | RING_LOCK_TEST_WITH_RETURN(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Eric Anholt | 546b097 | 2008-09-01 16:45:29 -0700 | [diff] [blame] | 742 | mutex_lock(&dev->struct_mutex); |
| 743 | ret = i915_dispatch_flip(dev); |
| 744 | mutex_unlock(&dev->struct_mutex); |
| 745 | |
| 746 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 749 | static int i915_getparam(struct drm_device *dev, void *data, |
| 750 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 753 | drm_i915_getparam_t *param = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | int value; |
| 755 | |
| 756 | if (!dev_priv) { |
Márton Németh | 3e684ea | 2008-01-24 15:58:57 +1000 | [diff] [blame] | 757 | DRM_ERROR("called with no initialization\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 758 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 761 | switch (param->param) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | case I915_PARAM_IRQ_ACTIVE: |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 763 | value = dev->pdev->irq ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | break; |
| 765 | case I915_PARAM_ALLOW_BATCHBUFFER: |
| 766 | value = dev_priv->allow_batchbuffer ? 1 : 0; |
| 767 | break; |
Dave Airlie | 0d6aa60 | 2006-01-02 20:14:23 +1100 | [diff] [blame] | 768 | case I915_PARAM_LAST_DISPATCH: |
| 769 | value = READ_BREADCRUMB(dev_priv); |
| 770 | break; |
Kristian Høgsberg | ed4c9c4 | 2008-08-20 11:08:52 -0400 | [diff] [blame] | 771 | case I915_PARAM_CHIPSET_ID: |
| 772 | value = dev->pci_device; |
| 773 | break; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 774 | case I915_PARAM_HAS_GEM: |
Dave Airlie | ac5c4e7 | 2008-12-19 15:38:34 +1000 | [diff] [blame] | 775 | value = dev_priv->has_gem; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 776 | break; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 777 | case I915_PARAM_NUM_FENCES_AVAIL: |
| 778 | value = dev_priv->num_fence_regs - dev_priv->fence_reg_start; |
| 779 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | default: |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 781 | DRM_DEBUG_DRIVER(I915_DRV, "Unknown parameter %d\n", |
| 782 | param->param); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 783 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 786 | if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | DRM_ERROR("DRM_COPY_TO_USER failed\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 788 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 794 | static int i915_setparam(struct drm_device *dev, void *data, |
| 795 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 798 | drm_i915_setparam_t *param = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
| 800 | if (!dev_priv) { |
Márton Németh | 3e684ea | 2008-01-24 15:58:57 +1000 | [diff] [blame] | 801 | DRM_ERROR("called with no initialization\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 802 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 805 | switch (param->param) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | case I915_SETPARAM_USE_MI_BATCHBUFFER_START: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | break; |
| 808 | case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY: |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 809 | dev_priv->tex_lru_log_granularity = param->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | break; |
| 811 | case I915_SETPARAM_ALLOW_BATCHBUFFER: |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 812 | dev_priv->allow_batchbuffer = param->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | break; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 814 | case I915_SETPARAM_NUM_USED_FENCES: |
| 815 | if (param->value > dev_priv->num_fence_regs || |
| 816 | param->value < 0) |
| 817 | return -EINVAL; |
| 818 | /* Userspace can use first N regs */ |
| 819 | dev_priv->fence_reg_start = param->value; |
| 820 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | default: |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 822 | DRM_DEBUG_DRIVER(I915_DRV, "unknown parameter %d\n", |
| 823 | param->param); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 824 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 830 | static int i915_set_status_page(struct drm_device *dev, void *data, |
| 831 | struct drm_file *file_priv) |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 832 | { |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 833 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 834 | drm_i915_hws_addr_t *hws = data; |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 835 | |
Zhenyu Wang | b39d50e | 2008-02-19 20:59:09 +1000 | [diff] [blame] | 836 | if (!I915_NEED_GFX_HWS(dev)) |
| 837 | return -EINVAL; |
| 838 | |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 839 | if (!dev_priv) { |
Márton Németh | 3e684ea | 2008-01-24 15:58:57 +1000 | [diff] [blame] | 840 | DRM_ERROR("called with no initialization\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 841 | return -EINVAL; |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 842 | } |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 843 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 844 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { |
| 845 | WARN(1, "tried to set status page when mode setting active\n"); |
| 846 | return 0; |
| 847 | } |
| 848 | |
Keith Packard | 1ae8c0a | 2009-06-28 15:42:17 -0700 | [diff] [blame^] | 849 | DRM_DEBUG("set status page addr 0x%08x\n", (u32)hws->addr); |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 850 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 851 | dev_priv->status_gfx_addr = hws->addr & (0x1ffff<<12); |
| 852 | |
Eric Anholt | 8b40958 | 2007-11-22 16:40:37 +1000 | [diff] [blame] | 853 | dev_priv->hws_map.offset = dev->agp->base + hws->addr; |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 854 | dev_priv->hws_map.size = 4*1024; |
| 855 | dev_priv->hws_map.type = 0; |
| 856 | dev_priv->hws_map.flags = 0; |
| 857 | dev_priv->hws_map.mtrr = 0; |
| 858 | |
Dave Airlie | dd0910b | 2009-02-25 14:49:21 +1000 | [diff] [blame] | 859 | drm_core_ioremap_wc(&dev_priv->hws_map, dev); |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 860 | if (dev_priv->hws_map.handle == NULL) { |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 861 | i915_dma_cleanup(dev); |
| 862 | dev_priv->status_gfx_addr = 0; |
| 863 | DRM_ERROR("can not ioremap virtual address for" |
| 864 | " G33 hw status page\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 865 | return -ENOMEM; |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 866 | } |
| 867 | dev_priv->hw_status_page = dev_priv->hws_map.handle; |
| 868 | |
| 869 | memset(dev_priv->hw_status_page, 0, PAGE_SIZE); |
Jesse Barnes | 585fb11 | 2008-07-29 11:54:06 -0700 | [diff] [blame] | 870 | I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr); |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 871 | DRM_DEBUG_DRIVER(I915_DRV, "load hws HWS_PGA with gfx mem 0x%x\n", |
| 872 | dev_priv->status_gfx_addr); |
| 873 | DRM_DEBUG_DRIVER(I915_DRV, "load hws at %p\n", |
| 874 | dev_priv->hw_status_page); |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 875 | return 0; |
| 876 | } |
| 877 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 878 | /** |
| 879 | * i915_probe_agp - get AGP bootup configuration |
| 880 | * @pdev: PCI device |
| 881 | * @aperture_size: returns AGP aperture configured size |
| 882 | * @preallocated_size: returns size of BIOS preallocated AGP space |
| 883 | * |
| 884 | * Since Intel integrated graphics are UMA, the BIOS has to set aside |
| 885 | * some RAM for the framebuffer at early boot. This code figures out |
| 886 | * how much was set aside so we can use it for our own purposes. |
| 887 | */ |
Hannes Eder | b358d0a | 2008-12-18 21:18:47 +0100 | [diff] [blame] | 888 | static int i915_probe_agp(struct drm_device *dev, unsigned long *aperture_size, |
| 889 | unsigned long *preallocated_size) |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 890 | { |
| 891 | struct pci_dev *bridge_dev; |
| 892 | u16 tmp = 0; |
| 893 | unsigned long overhead; |
Eric Anholt | 241fa85 | 2009-01-02 18:05:51 -0800 | [diff] [blame] | 894 | unsigned long stolen; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 895 | |
| 896 | bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0)); |
| 897 | if (!bridge_dev) { |
| 898 | DRM_ERROR("bridge device not found\n"); |
| 899 | return -1; |
| 900 | } |
| 901 | |
| 902 | /* Get the fb aperture size and "stolen" memory amount. */ |
| 903 | pci_read_config_word(bridge_dev, INTEL_GMCH_CTRL, &tmp); |
| 904 | pci_dev_put(bridge_dev); |
| 905 | |
| 906 | *aperture_size = 1024 * 1024; |
| 907 | *preallocated_size = 1024 * 1024; |
| 908 | |
Eric Anholt | 60fd99e | 2008-12-03 22:50:02 -0800 | [diff] [blame] | 909 | switch (dev->pdev->device) { |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 910 | case PCI_DEVICE_ID_INTEL_82830_CGC: |
| 911 | case PCI_DEVICE_ID_INTEL_82845G_IG: |
| 912 | case PCI_DEVICE_ID_INTEL_82855GM_IG: |
| 913 | case PCI_DEVICE_ID_INTEL_82865_IG: |
| 914 | if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M) |
| 915 | *aperture_size *= 64; |
| 916 | else |
| 917 | *aperture_size *= 128; |
| 918 | break; |
| 919 | default: |
| 920 | /* 9xx supports large sizes, just look at the length */ |
Eric Anholt | 60fd99e | 2008-12-03 22:50:02 -0800 | [diff] [blame] | 921 | *aperture_size = pci_resource_len(dev->pdev, 2); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 922 | break; |
| 923 | } |
| 924 | |
| 925 | /* |
| 926 | * Some of the preallocated space is taken by the GTT |
| 927 | * and popup. GTT is 1K per MB of aperture size, and popup is 4K. |
| 928 | */ |
Zhenyu Wang | 2c07245 | 2009-06-05 15:38:42 +0800 | [diff] [blame] | 929 | if (IS_G4X(dev) || IS_IGD(dev) || IS_IGDNG(dev)) |
Eric Anholt | 60fd99e | 2008-12-03 22:50:02 -0800 | [diff] [blame] | 930 | overhead = 4096; |
| 931 | else |
| 932 | overhead = (*aperture_size / 1024) + 4096; |
| 933 | |
Eric Anholt | 241fa85 | 2009-01-02 18:05:51 -0800 | [diff] [blame] | 934 | switch (tmp & INTEL_GMCH_GMS_MASK) { |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 935 | case INTEL_855_GMCH_GMS_DISABLED: |
| 936 | DRM_ERROR("video memory is disabled\n"); |
| 937 | return -1; |
Eric Anholt | 241fa85 | 2009-01-02 18:05:51 -0800 | [diff] [blame] | 938 | case INTEL_855_GMCH_GMS_STOLEN_1M: |
| 939 | stolen = 1 * 1024 * 1024; |
| 940 | break; |
| 941 | case INTEL_855_GMCH_GMS_STOLEN_4M: |
| 942 | stolen = 4 * 1024 * 1024; |
| 943 | break; |
| 944 | case INTEL_855_GMCH_GMS_STOLEN_8M: |
| 945 | stolen = 8 * 1024 * 1024; |
| 946 | break; |
| 947 | case INTEL_855_GMCH_GMS_STOLEN_16M: |
| 948 | stolen = 16 * 1024 * 1024; |
| 949 | break; |
| 950 | case INTEL_855_GMCH_GMS_STOLEN_32M: |
| 951 | stolen = 32 * 1024 * 1024; |
| 952 | break; |
| 953 | case INTEL_915G_GMCH_GMS_STOLEN_48M: |
| 954 | stolen = 48 * 1024 * 1024; |
| 955 | break; |
| 956 | case INTEL_915G_GMCH_GMS_STOLEN_64M: |
| 957 | stolen = 64 * 1024 * 1024; |
| 958 | break; |
| 959 | case INTEL_GMCH_GMS_STOLEN_128M: |
| 960 | stolen = 128 * 1024 * 1024; |
| 961 | break; |
| 962 | case INTEL_GMCH_GMS_STOLEN_256M: |
| 963 | stolen = 256 * 1024 * 1024; |
| 964 | break; |
| 965 | case INTEL_GMCH_GMS_STOLEN_96M: |
| 966 | stolen = 96 * 1024 * 1024; |
| 967 | break; |
| 968 | case INTEL_GMCH_GMS_STOLEN_160M: |
| 969 | stolen = 160 * 1024 * 1024; |
| 970 | break; |
| 971 | case INTEL_GMCH_GMS_STOLEN_224M: |
| 972 | stolen = 224 * 1024 * 1024; |
| 973 | break; |
| 974 | case INTEL_GMCH_GMS_STOLEN_352M: |
| 975 | stolen = 352 * 1024 * 1024; |
| 976 | break; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 977 | default: |
| 978 | DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n", |
Eric Anholt | 241fa85 | 2009-01-02 18:05:51 -0800 | [diff] [blame] | 979 | tmp & INTEL_GMCH_GMS_MASK); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 980 | return -1; |
| 981 | } |
Eric Anholt | 241fa85 | 2009-01-02 18:05:51 -0800 | [diff] [blame] | 982 | *preallocated_size = stolen - overhead; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 983 | |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | static int i915_load_modeset_init(struct drm_device *dev) |
| 988 | { |
| 989 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 990 | unsigned long agp_size, prealloc_size; |
| 991 | int fb_bar = IS_I9XX(dev) ? 2 : 0; |
| 992 | int ret = 0; |
| 993 | |
| 994 | dev->mode_config.fb_base = drm_get_resource_start(dev, fb_bar) & |
| 995 | 0xff000000; |
| 996 | |
Jesse Barnes | 2906f02 | 2009-01-20 19:10:54 -0800 | [diff] [blame] | 997 | if (IS_MOBILE(dev) || IS_I9XX(dev)) |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 998 | dev_priv->cursor_needs_physical = true; |
| 999 | else |
| 1000 | dev_priv->cursor_needs_physical = false; |
| 1001 | |
Jesse Barnes | 2906f02 | 2009-01-20 19:10:54 -0800 | [diff] [blame] | 1002 | if (IS_I965G(dev) || IS_G33(dev)) |
| 1003 | dev_priv->cursor_needs_physical = false; |
| 1004 | |
Dave Airlie | aa59662 | 2008-12-29 16:35:02 +1000 | [diff] [blame] | 1005 | ret = i915_probe_agp(dev, &agp_size, &prealloc_size); |
| 1006 | if (ret) |
Dave Airlie | b8da7de | 2009-06-02 16:50:35 +1000 | [diff] [blame] | 1007 | goto out; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1008 | |
| 1009 | /* Basic memrange allocator for stolen space (aka vram) */ |
| 1010 | drm_mm_init(&dev_priv->vram, 0, prealloc_size); |
| 1011 | |
Eric Anholt | 13f4c43 | 2009-05-12 15:27:36 -0700 | [diff] [blame] | 1012 | /* Let GEM Manage from end of prealloc space to end of aperture. |
| 1013 | * |
| 1014 | * However, leave one page at the end still bound to the scratch page. |
| 1015 | * There are a number of places where the hardware apparently |
| 1016 | * prefetches past the end of the object, and we've seen multiple |
| 1017 | * hangs with the GPU head pointer stuck in a batchbuffer bound |
| 1018 | * at the last page of the aperture. One page should be enough to |
| 1019 | * keep any prefetching inside of the aperture. |
| 1020 | */ |
| 1021 | i915_gem_do_init(dev, prealloc_size, agp_size - 4096); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1022 | |
| 1023 | ret = i915_gem_init_ringbuffer(dev); |
| 1024 | if (ret) |
Dave Airlie | b8da7de | 2009-06-02 16:50:35 +1000 | [diff] [blame] | 1025 | goto out; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1026 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1027 | /* Allow hardware batchbuffers unless told otherwise. |
| 1028 | */ |
| 1029 | dev_priv->allow_batchbuffer = 1; |
| 1030 | |
| 1031 | ret = intel_init_bios(dev); |
| 1032 | if (ret) |
| 1033 | DRM_INFO("failed to find VBIOS tables\n"); |
| 1034 | |
| 1035 | ret = drm_irq_install(dev); |
| 1036 | if (ret) |
| 1037 | goto destroy_ringbuffer; |
| 1038 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1039 | /* Always safe in the mode setting case. */ |
| 1040 | /* FIXME: do pre/post-mode set stuff in core KMS code */ |
| 1041 | dev->vblank_disable_allowed = 1; |
| 1042 | |
| 1043 | /* |
| 1044 | * Initialize the hardware status page IRQ location. |
| 1045 | */ |
| 1046 | |
| 1047 | I915_WRITE(INSTPM, (1 << 5) | (1 << 21)); |
| 1048 | |
| 1049 | intel_modeset_init(dev); |
| 1050 | |
Jesse Barnes | 7a1fb5d | 2009-03-27 13:05:19 -0700 | [diff] [blame] | 1051 | drm_helper_initial_config(dev); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1052 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1053 | return 0; |
| 1054 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1055 | destroy_ringbuffer: |
| 1056 | i915_gem_cleanup_ringbuffer(dev); |
| 1057 | out: |
| 1058 | return ret; |
| 1059 | } |
| 1060 | |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 1061 | int i915_master_create(struct drm_device *dev, struct drm_master *master) |
| 1062 | { |
| 1063 | struct drm_i915_master_private *master_priv; |
| 1064 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1065 | master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 1066 | if (!master_priv) |
| 1067 | return -ENOMEM; |
| 1068 | |
| 1069 | master->driver_priv = master_priv; |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | void i915_master_destroy(struct drm_device *dev, struct drm_master *master) |
| 1074 | { |
| 1075 | struct drm_i915_master_private *master_priv = master->driver_priv; |
| 1076 | |
| 1077 | if (!master_priv) |
| 1078 | return; |
| 1079 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1080 | kfree(master_priv); |
Dave Airlie | 7c1c287 | 2008-11-28 14:22:24 +1000 | [diff] [blame] | 1081 | |
| 1082 | master->driver_priv = NULL; |
| 1083 | } |
| 1084 | |
Shaohua Li | 7662c8b | 2009-06-26 11:23:55 +0800 | [diff] [blame] | 1085 | static void i915_get_mem_freq(struct drm_device *dev) |
| 1086 | { |
| 1087 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1088 | u32 tmp; |
| 1089 | |
| 1090 | if (!IS_IGD(dev)) |
| 1091 | return; |
| 1092 | |
| 1093 | tmp = I915_READ(CLKCFG); |
| 1094 | |
| 1095 | switch (tmp & CLKCFG_FSB_MASK) { |
| 1096 | case CLKCFG_FSB_533: |
| 1097 | dev_priv->fsb_freq = 533; /* 133*4 */ |
| 1098 | break; |
| 1099 | case CLKCFG_FSB_800: |
| 1100 | dev_priv->fsb_freq = 800; /* 200*4 */ |
| 1101 | break; |
| 1102 | case CLKCFG_FSB_667: |
| 1103 | dev_priv->fsb_freq = 667; /* 167*4 */ |
| 1104 | break; |
| 1105 | case CLKCFG_FSB_400: |
| 1106 | dev_priv->fsb_freq = 400; /* 100*4 */ |
| 1107 | break; |
| 1108 | } |
| 1109 | |
| 1110 | switch (tmp & CLKCFG_MEM_MASK) { |
| 1111 | case CLKCFG_MEM_533: |
| 1112 | dev_priv->mem_freq = 533; |
| 1113 | break; |
| 1114 | case CLKCFG_MEM_667: |
| 1115 | dev_priv->mem_freq = 667; |
| 1116 | break; |
| 1117 | case CLKCFG_MEM_800: |
| 1118 | dev_priv->mem_freq = 800; |
| 1119 | break; |
| 1120 | } |
| 1121 | } |
| 1122 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1123 | /** |
| 1124 | * i915_driver_load - setup chip and create an initial config |
| 1125 | * @dev: DRM device |
| 1126 | * @flags: startup flags |
| 1127 | * |
| 1128 | * The driver load routine has to do several things: |
| 1129 | * - drive output discovery via intel_modeset_init() |
| 1130 | * - initialize the memory manager |
| 1131 | * - allocate initial config memory |
| 1132 | * - setup the DRM framebuffer with the allocated memory |
| 1133 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 1134 | int i915_driver_load(struct drm_device *dev, unsigned long flags) |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 1135 | { |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 1136 | struct drm_i915_private *dev_priv = dev->dev_private; |
Benjamin Herrenschmidt | d883f7f | 2009-02-02 16:55:45 +1100 | [diff] [blame] | 1137 | resource_size_t base, size; |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 1138 | int ret = 0, mmio_bar = IS_I9XX(dev) ? 0 : 1; |
| 1139 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 1140 | /* i915 has 4 more counters */ |
| 1141 | dev->counters += 4; |
| 1142 | dev->types[6] = _DRM_STAT_IRQ; |
| 1143 | dev->types[7] = _DRM_STAT_PRIMARY; |
| 1144 | dev->types[8] = _DRM_STAT_SECONDARY; |
| 1145 | dev->types[9] = _DRM_STAT_DMA; |
| 1146 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1147 | dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL); |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 1148 | if (dev_priv == NULL) |
| 1149 | return -ENOMEM; |
| 1150 | |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 1151 | dev->dev_private = (void *)dev_priv; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1152 | dev_priv->dev = dev; |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 1153 | |
| 1154 | /* Add register map (needed for suspend/resume) */ |
| 1155 | base = drm_get_resource_start(dev, mmio_bar); |
| 1156 | size = drm_get_resource_len(dev, mmio_bar); |
| 1157 | |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 1158 | dev_priv->regs = ioremap(base, size); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1159 | if (!dev_priv->regs) { |
| 1160 | DRM_ERROR("failed to map registers\n"); |
| 1161 | ret = -EIO; |
| 1162 | goto free_priv; |
| 1163 | } |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 1164 | |
Eric Anholt | ab657db1 | 2009-01-23 12:57:47 -0800 | [diff] [blame] | 1165 | dev_priv->mm.gtt_mapping = |
| 1166 | io_mapping_create_wc(dev->agp->base, |
| 1167 | dev->agp->agp_info.aper_size * 1024*1024); |
Venkatesh Pallipadi | 6644107d | 2009-02-24 17:35:11 -0800 | [diff] [blame] | 1168 | if (dev_priv->mm.gtt_mapping == NULL) { |
| 1169 | ret = -EIO; |
| 1170 | goto out_rmmap; |
| 1171 | } |
| 1172 | |
Eric Anholt | ab657db1 | 2009-01-23 12:57:47 -0800 | [diff] [blame] | 1173 | /* Set up a WC MTRR for non-PAT systems. This is more common than |
| 1174 | * one would think, because the kernel disables PAT on first |
| 1175 | * generation Core chips because WC PAT gets overridden by a UC |
| 1176 | * MTRR if present. Even if a UC MTRR isn't present. |
| 1177 | */ |
| 1178 | dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base, |
| 1179 | dev->agp->agp_info.aper_size * |
| 1180 | 1024 * 1024, |
| 1181 | MTRR_TYPE_WRCOMB, 1); |
| 1182 | if (dev_priv->mm.gtt_mtrr < 0) { |
Eric Anholt | 040aefa | 2009-03-10 12:31:12 -0700 | [diff] [blame] | 1183 | DRM_INFO("MTRR allocation failed. Graphics " |
Eric Anholt | ab657db1 | 2009-01-23 12:57:47 -0800 | [diff] [blame] | 1184 | "performance may suffer.\n"); |
| 1185 | } |
| 1186 | |
Dave Airlie | ac5c4e7 | 2008-12-19 15:38:34 +1000 | [diff] [blame] | 1187 | /* enable GEM by default */ |
| 1188 | dev_priv->has_gem = 1; |
Dave Airlie | ac5c4e7 | 2008-12-19 15:38:34 +1000 | [diff] [blame] | 1189 | |
Jesse Barnes | 9880b7a | 2009-02-06 10:22:41 -0800 | [diff] [blame] | 1190 | dev->driver->get_vblank_counter = i915_get_vblank_counter; |
Jesse Barnes | 42c2798 | 2009-05-05 13:13:16 -0700 | [diff] [blame] | 1191 | dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */ |
Zhenyu Wang | 036a4a7 | 2009-06-08 14:40:19 +0800 | [diff] [blame] | 1192 | if (IS_G4X(dev) || IS_IGDNG(dev)) { |
Jesse Barnes | 42c2798 | 2009-05-05 13:13:16 -0700 | [diff] [blame] | 1193 | dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */ |
Jesse Barnes | 9880b7a | 2009-02-06 10:22:41 -0800 | [diff] [blame] | 1194 | dev->driver->get_vblank_counter = gm45_get_vblank_counter; |
Jesse Barnes | 42c2798 | 2009-05-05 13:13:16 -0700 | [diff] [blame] | 1195 | } |
Jesse Barnes | 9880b7a | 2009-02-06 10:22:41 -0800 | [diff] [blame] | 1196 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1197 | i915_gem_load(dev); |
| 1198 | |
Keith Packard | 398c9cb | 2008-07-30 13:03:43 -0700 | [diff] [blame] | 1199 | /* Init HWS */ |
| 1200 | if (!I915_NEED_GFX_HWS(dev)) { |
| 1201 | ret = i915_init_phys_hws(dev); |
| 1202 | if (ret != 0) |
Venkatesh Pallipadi | 6644107d | 2009-02-24 17:35:11 -0800 | [diff] [blame] | 1203 | goto out_iomapfree; |
Keith Packard | 398c9cb | 2008-07-30 13:03:43 -0700 | [diff] [blame] | 1204 | } |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 1205 | |
Shaohua Li | 7662c8b | 2009-06-26 11:23:55 +0800 | [diff] [blame] | 1206 | i915_get_mem_freq(dev); |
| 1207 | |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 1208 | /* On the 945G/GM, the chipset reports the MSI capability on the |
| 1209 | * integrated graphics even though the support isn't actually there |
| 1210 | * according to the published specs. It doesn't appear to function |
| 1211 | * correctly in testing on 945G. |
| 1212 | * This may be a side effect of MSI having been made available for PEG |
| 1213 | * and the registers being closely associated. |
Keith Packard | d1ed629 | 2008-10-17 00:44:42 -0700 | [diff] [blame] | 1214 | * |
| 1215 | * According to chipset errata, on the 965GM, MSI interrupts may |
Keith Packard | b60678a | 2008-12-08 11:12:28 -0800 | [diff] [blame] | 1216 | * be lost or delayed, but we use them anyways to avoid |
| 1217 | * stuck interrupts on some machines. |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 1218 | */ |
Keith Packard | b60678a | 2008-12-08 11:12:28 -0800 | [diff] [blame] | 1219 | if (!IS_I945G(dev) && !IS_I945GM(dev)) |
Eric Anholt | d3e74d0 | 2008-11-03 14:46:17 -0800 | [diff] [blame] | 1220 | pci_enable_msi(dev->pdev); |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 1221 | |
| 1222 | spin_lock_init(&dev_priv->user_irq_lock); |
Jesse Barnes | 63eeaf3 | 2009-06-18 16:56:52 -0700 | [diff] [blame] | 1223 | spin_lock_init(&dev_priv->error_lock); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1224 | dev_priv->user_irq_refcount = 0; |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 1225 | |
Keith Packard | 5244021 | 2008-11-18 09:30:25 -0800 | [diff] [blame] | 1226 | ret = drm_vblank_init(dev, I915_NUM_PIPE); |
| 1227 | |
| 1228 | if (ret) { |
| 1229 | (void) i915_driver_unload(dev); |
| 1230 | return ret; |
| 1231 | } |
| 1232 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1233 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { |
| 1234 | ret = i915_load_modeset_init(dev); |
| 1235 | if (ret < 0) { |
| 1236 | DRM_ERROR("failed to init modeset\n"); |
| 1237 | goto out_rmmap; |
| 1238 | } |
| 1239 | } |
| 1240 | |
Matthew Garrett | 74a365b | 2009-03-19 21:35:39 +0000 | [diff] [blame] | 1241 | /* Must be done after probing outputs */ |
Zhenyu Wang | e170b03 | 2009-06-05 15:38:40 +0800 | [diff] [blame] | 1242 | /* FIXME: verify on IGDNG */ |
| 1243 | if (!IS_IGDNG(dev)) |
| 1244 | intel_opregion_init(dev, 0); |
Matthew Garrett | 74a365b | 2009-03-19 21:35:39 +0000 | [diff] [blame] | 1245 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1246 | return 0; |
| 1247 | |
Venkatesh Pallipadi | 6644107d | 2009-02-24 17:35:11 -0800 | [diff] [blame] | 1248 | out_iomapfree: |
| 1249 | io_mapping_free(dev_priv->mm.gtt_mapping); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1250 | out_rmmap: |
| 1251 | iounmap(dev_priv->regs); |
| 1252 | free_priv: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1253 | kfree(dev_priv); |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 1254 | return ret; |
| 1255 | } |
| 1256 | |
| 1257 | int i915_driver_unload(struct drm_device *dev) |
| 1258 | { |
| 1259 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1260 | |
Eric Anholt | ab657db1 | 2009-01-23 12:57:47 -0800 | [diff] [blame] | 1261 | io_mapping_free(dev_priv->mm.gtt_mapping); |
| 1262 | if (dev_priv->mm.gtt_mtrr >= 0) { |
| 1263 | mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base, |
| 1264 | dev->agp->agp_info.aper_size * 1024 * 1024); |
| 1265 | dev_priv->mm.gtt_mtrr = -1; |
| 1266 | } |
| 1267 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1268 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1269 | drm_irq_uninstall(dev); |
| 1270 | } |
| 1271 | |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 1272 | if (dev->pdev->msi_enabled) |
| 1273 | pci_disable_msi(dev->pdev); |
| 1274 | |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 1275 | if (dev_priv->regs != NULL) |
| 1276 | iounmap(dev_priv->regs); |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 1277 | |
Zhenyu Wang | e170b03 | 2009-06-05 15:38:40 +0800 | [diff] [blame] | 1278 | if (!IS_IGDNG(dev)) |
| 1279 | intel_opregion_free(dev, 0); |
Matthew Garrett | 8ee1c3d | 2008-08-05 19:37:25 +0100 | [diff] [blame] | 1280 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1281 | if (drm_core_check_feature(dev, DRIVER_MODESET)) { |
| 1282 | intel_modeset_cleanup(dev); |
| 1283 | |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 1284 | i915_gem_free_all_phys_object(dev); |
| 1285 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1286 | mutex_lock(&dev->struct_mutex); |
| 1287 | i915_gem_cleanup_ringbuffer(dev); |
| 1288 | mutex_unlock(&dev->struct_mutex); |
| 1289 | drm_mm_takedown(&dev_priv->vram); |
| 1290 | i915_gem_lastclose(dev); |
| 1291 | } |
| 1292 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1293 | kfree(dev->dev_private); |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 1294 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 1295 | return 0; |
| 1296 | } |
| 1297 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1298 | int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv) |
| 1299 | { |
| 1300 | struct drm_i915_file_private *i915_file_priv; |
| 1301 | |
yakui_zhao | be25ed9 | 2009-06-02 14:13:55 +0800 | [diff] [blame] | 1302 | DRM_DEBUG_DRIVER(I915_DRV, "\n"); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1303 | i915_file_priv = (struct drm_i915_file_private *) |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1304 | kmalloc(sizeof(*i915_file_priv), GFP_KERNEL); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1305 | |
| 1306 | if (!i915_file_priv) |
| 1307 | return -ENOMEM; |
| 1308 | |
| 1309 | file_priv->driver_priv = i915_file_priv; |
| 1310 | |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1311 | INIT_LIST_HEAD(&i915_file_priv->mm.request_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1312 | |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1316 | /** |
| 1317 | * i915_driver_lastclose - clean up after all DRM clients have exited |
| 1318 | * @dev: DRM device |
| 1319 | * |
| 1320 | * Take care of cleaning up after all DRM clients have exited. In the |
| 1321 | * mode setting case, we want to restore the kernel's initial mode (just |
| 1322 | * in case the last client left us in a bad state). |
| 1323 | * |
| 1324 | * Additionally, in the non-mode setting case, we'll tear down the AGP |
| 1325 | * and DMA structures, since the kernel won't be using them, and clea |
| 1326 | * up any GEM state. |
| 1327 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 1328 | void i915_driver_lastclose(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | { |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 1330 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1331 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1332 | if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) { |
| 1333 | intelfb_restore(); |
Dave Airlie | 144a75f | 2008-03-30 07:53:58 +1000 | [diff] [blame] | 1334 | return; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1335 | } |
Dave Airlie | 144a75f | 2008-03-30 07:53:58 +1000 | [diff] [blame] | 1336 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1337 | i915_gem_lastclose(dev); |
| 1338 | |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 1339 | if (dev_priv->agp_heap) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1340 | i915_mem_takedown(&(dev_priv->agp_heap)); |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 1341 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 1342 | i915_dma_cleanup(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1345 | void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | { |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 1347 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1348 | i915_gem_release(dev, file_priv); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1349 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1350 | i915_mem_release(dev, file_priv, dev_priv->agp_heap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1353 | void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv) |
| 1354 | { |
| 1355 | struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv; |
| 1356 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1357 | kfree(i915_file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1360 | struct drm_ioctl_desc i915_ioctls[] = { |
| 1361 | DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), |
| 1362 | DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH), |
| 1363 | DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH), |
| 1364 | DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH), |
| 1365 | DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH), |
| 1366 | DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH), |
| 1367 | DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH), |
| 1368 | DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), |
| 1369 | DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH), |
| 1370 | DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH), |
| 1371 | DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), |
| 1372 | DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH), |
| 1373 | DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ), |
| 1374 | DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ), |
| 1375 | DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ), |
| 1376 | DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH), |
Matthias Hopf | 4b40893 | 2008-10-18 07:18:05 +1000 | [diff] [blame] | 1377 | DRM_IOCTL_DEF(DRM_I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), |
Dave Airlie | 2bdf00b | 2008-10-07 13:40:10 +1000 | [diff] [blame] | 1378 | DRM_IOCTL_DEF(DRM_I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1379 | DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH), |
| 1380 | DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY), |
| 1381 | DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY), |
| 1382 | DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH), |
| 1383 | DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH), |
Dave Airlie | 2bdf00b | 2008-10-07 13:40:10 +1000 | [diff] [blame] | 1384 | DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), |
| 1385 | DRM_IOCTL_DEF(DRM_I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1386 | DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, 0), |
| 1387 | DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, 0), |
| 1388 | DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, 0), |
| 1389 | DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, 0), |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1390 | DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, 0), |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1391 | DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, 0), |
| 1392 | DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, 0), |
| 1393 | DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, 0), |
| 1394 | DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, 0), |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 1395 | DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, 0), |
Carl Worth | 08d7b3d | 2009-04-29 14:43:54 -0700 | [diff] [blame] | 1396 | DRM_IOCTL_DEF(DRM_I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, 0), |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 1397 | }; |
| 1398 | |
| 1399 | int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls); |
Dave Airlie | cda1738 | 2005-07-10 17:31:26 +1000 | [diff] [blame] | 1400 | |
| 1401 | /** |
| 1402 | * Determine if the device really is AGP or not. |
| 1403 | * |
| 1404 | * All Intel graphics chipsets are treated as AGP, even if they are really |
| 1405 | * PCI-e. |
| 1406 | * |
| 1407 | * \param dev The device to be tested. |
| 1408 | * |
| 1409 | * \returns |
| 1410 | * A value of 1 is always retured to indictate every i9x5 is AGP. |
| 1411 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 1412 | int i915_driver_device_is_agp(struct drm_device * dev) |
Dave Airlie | cda1738 | 2005-07-10 17:31:26 +1000 | [diff] [blame] | 1413 | { |
| 1414 | return 1; |
| 1415 | } |