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" |
| 31 | #include "i915_drm.h" |
| 32 | #include "i915_drv.h" |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | /* Really want an OS-independent resettable timer. Would like to have |
| 35 | * this loop run for (eg) 3 sec, but have the timer reset every time |
| 36 | * the head pointer changes, so that EBUSY only happens if the ring |
| 37 | * actually stalls for (eg) 3 seconds. |
| 38 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 39 | 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] | 40 | { |
| 41 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 42 | drm_i915_ring_buffer_t *ring = &(dev_priv->ring); |
| 43 | u32 last_head = I915_READ(LP_RING + RING_HEAD) & HEAD_ADDR; |
| 44 | int i; |
| 45 | |
| 46 | for (i = 0; i < 10000; i++) { |
| 47 | ring->head = I915_READ(LP_RING + RING_HEAD) & HEAD_ADDR; |
| 48 | ring->space = ring->head - (ring->tail + 8); |
| 49 | if (ring->space < 0) |
| 50 | ring->space += ring->Size; |
| 51 | if (ring->space >= n) |
| 52 | return 0; |
| 53 | |
| 54 | dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT; |
| 55 | |
| 56 | if (ring->head != last_head) |
| 57 | i = 0; |
| 58 | |
| 59 | last_head = ring->head; |
| 60 | } |
| 61 | |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 62 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 65 | void i915_kernel_lost_context(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
| 67 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 68 | drm_i915_ring_buffer_t *ring = &(dev_priv->ring); |
| 69 | |
| 70 | ring->head = I915_READ(LP_RING + RING_HEAD) & HEAD_ADDR; |
| 71 | ring->tail = I915_READ(LP_RING + RING_TAIL) & TAIL_ADDR; |
| 72 | ring->space = ring->head - (ring->tail + 8); |
| 73 | if (ring->space < 0) |
| 74 | ring->space += ring->Size; |
| 75 | |
| 76 | if (ring->head == ring->tail) |
| 77 | dev_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY; |
| 78 | } |
| 79 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 80 | static int i915_dma_cleanup(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 82 | drm_i915_private_t *dev_priv = dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* Make sure interrupts are disabled here because the uninstall ioctl |
| 84 | * may not have been called from userspace and after dev_private |
| 85 | * is freed, it's too late. |
| 86 | */ |
| 87 | if (dev->irq) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 88 | drm_irq_uninstall(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 90 | if (dev_priv->ring.virtual_start) { |
| 91 | drm_core_ioremapfree(&dev_priv->ring.map, dev); |
| 92 | dev_priv->ring.virtual_start = 0; |
| 93 | dev_priv->ring.map.handle = 0; |
| 94 | dev_priv->ring.map.size = 0; |
| 95 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 97 | if (dev_priv->status_page_dmah) { |
| 98 | drm_pci_free(dev, dev_priv->status_page_dmah); |
| 99 | dev_priv->status_page_dmah = NULL; |
| 100 | /* Need to rewrite hardware status page */ |
| 101 | I915_WRITE(0x02080, 0x1ffff000); |
| 102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 104 | if (dev_priv->status_gfx_addr) { |
| 105 | dev_priv->status_gfx_addr = 0; |
| 106 | drm_core_ioremapfree(&dev_priv->hws_map, dev); |
| 107 | I915_WRITE(0x2080, 0x1ffff000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 113 | 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] | 114 | { |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 115 | drm_i915_private_t *dev_priv = dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Dave Airlie | da509d7 | 2007-05-26 05:04:51 +1000 | [diff] [blame] | 117 | dev_priv->sarea = drm_getsarea(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | if (!dev_priv->sarea) { |
| 119 | DRM_ERROR("can not find sarea!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | i915_dma_cleanup(dev); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 121 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset); |
| 125 | if (!dev_priv->mmio_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | i915_dma_cleanup(dev); |
| 127 | DRM_ERROR("can not find mmio map!\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 128 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | dev_priv->sarea_priv = (drm_i915_sarea_t *) |
| 132 | ((u8 *) dev_priv->sarea->handle + init->sarea_priv_offset); |
| 133 | |
| 134 | dev_priv->ring.Start = init->ring_start; |
| 135 | dev_priv->ring.End = init->ring_end; |
| 136 | dev_priv->ring.Size = init->ring_size; |
| 137 | dev_priv->ring.tail_mask = dev_priv->ring.Size - 1; |
| 138 | |
| 139 | dev_priv->ring.map.offset = init->ring_start; |
| 140 | dev_priv->ring.map.size = init->ring_size; |
| 141 | dev_priv->ring.map.type = 0; |
| 142 | dev_priv->ring.map.flags = 0; |
| 143 | dev_priv->ring.map.mtrr = 0; |
| 144 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 145 | drm_core_ioremap(&dev_priv->ring.map, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | if (dev_priv->ring.map.handle == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | i915_dma_cleanup(dev); |
| 149 | DRM_ERROR("can not ioremap virtual address for" |
| 150 | " ring buffer\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 151 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | dev_priv->ring.virtual_start = dev_priv->ring.map.handle; |
| 155 | |
=?utf-8?q?Michel_D=C3=A4nzer?= | a6b54f3 | 2006-10-24 23:37:43 +1000 | [diff] [blame] | 156 | dev_priv->cpp = init->cpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | dev_priv->back_offset = init->back_offset; |
| 158 | dev_priv->front_offset = init->front_offset; |
| 159 | dev_priv->current_page = 0; |
| 160 | dev_priv->sarea_priv->pf_current_page = dev_priv->current_page; |
| 161 | |
| 162 | /* We are using separate values as placeholders for mechanisms for |
| 163 | * private backbuffer/depthbuffer usage. |
| 164 | */ |
| 165 | dev_priv->use_mi_batchbuffer_start = 0; |
Dave Airlie | 21f1628 | 2007-08-07 09:09:51 +1000 | [diff] [blame] | 166 | if (IS_I965G(dev)) /* 965 doesn't support older method */ |
| 167 | dev_priv->use_mi_batchbuffer_start = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | /* Allow hardware batchbuffers unless told otherwise. |
| 170 | */ |
| 171 | dev_priv->allow_batchbuffer = 1; |
| 172 | |
| 173 | /* Program Hardware Status Page */ |
Zhenyu Wang | b39d50e | 2008-02-19 20:59:09 +1000 | [diff] [blame^] | 174 | if (!I915_NEED_GFX_HWS(dev)) { |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 175 | dev_priv->status_page_dmah = |
| 176 | drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 178 | if (!dev_priv->status_page_dmah) { |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 179 | i915_dma_cleanup(dev); |
| 180 | DRM_ERROR("Can not allocate hardware status page\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 181 | return -ENOMEM; |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 182 | } |
| 183 | dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr; |
| 184 | dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr; |
| 185 | |
| 186 | memset(dev_priv->hw_status_page, 0, PAGE_SIZE); |
| 187 | I915_WRITE(0x02080, dev_priv->dma_status_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | DRM_DEBUG("Enabled hardware status page\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 193 | static int i915_dma_resume(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
| 195 | drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; |
| 196 | |
| 197 | DRM_DEBUG("%s\n", __FUNCTION__); |
| 198 | |
| 199 | if (!dev_priv->sarea) { |
| 200 | DRM_ERROR("can not find sarea!\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 201 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if (!dev_priv->mmio_map) { |
| 205 | DRM_ERROR("can not find mmio map!\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 206 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | if (dev_priv->ring.map.handle == NULL) { |
| 210 | DRM_ERROR("can not ioremap virtual address for" |
| 211 | " ring buffer\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 212 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* Program Hardware Status Page */ |
| 216 | if (!dev_priv->hw_status_page) { |
| 217 | DRM_ERROR("Can not find hardware status page\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 218 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page); |
| 221 | |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 222 | if (dev_priv->status_gfx_addr != 0) |
| 223 | I915_WRITE(0x02080, dev_priv->status_gfx_addr); |
| 224 | else |
| 225 | I915_WRITE(0x02080, dev_priv->dma_status_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | DRM_DEBUG("Enabled hardware status page\n"); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 231 | static int i915_dma_init(struct drm_device *dev, void *data, |
| 232 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 234 | drm_i915_init_t *init = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | int retcode = 0; |
| 236 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 237 | switch (init->func) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | case I915_INIT_DMA: |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 239 | retcode = i915_initialize(dev, init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | break; |
| 241 | case I915_CLEANUP_DMA: |
| 242 | retcode = i915_dma_cleanup(dev); |
| 243 | break; |
| 244 | case I915_RESUME_DMA: |
Dave Airlie | 0d6aa60 | 2006-01-02 20:14:23 +1100 | [diff] [blame] | 245 | retcode = i915_dma_resume(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | break; |
| 247 | default: |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 248 | retcode = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | break; |
| 250 | } |
| 251 | |
| 252 | return retcode; |
| 253 | } |
| 254 | |
| 255 | /* Implement basically the same security restrictions as hardware does |
| 256 | * for MI_BATCH_NON_SECURE. These can be made stricter at any time. |
| 257 | * |
| 258 | * Most of the calculations below involve calculating the size of a |
| 259 | * particular instruction. It's important to get the size right as |
| 260 | * that tells us where the next instruction to check is. Any illegal |
| 261 | * instruction detected will be given a size of zero, which is a |
| 262 | * signal to abort the rest of the buffer. |
| 263 | */ |
| 264 | static int do_validate_cmd(int cmd) |
| 265 | { |
| 266 | switch (((cmd >> 29) & 0x7)) { |
| 267 | case 0x0: |
| 268 | switch ((cmd >> 23) & 0x3f) { |
| 269 | case 0x0: |
| 270 | return 1; /* MI_NOOP */ |
| 271 | case 0x4: |
| 272 | return 1; /* MI_FLUSH */ |
| 273 | default: |
| 274 | return 0; /* disallow everything else */ |
| 275 | } |
| 276 | break; |
| 277 | case 0x1: |
| 278 | return 0; /* reserved */ |
| 279 | case 0x2: |
| 280 | return (cmd & 0xff) + 2; /* 2d commands */ |
| 281 | case 0x3: |
| 282 | if (((cmd >> 24) & 0x1f) <= 0x18) |
| 283 | return 1; |
| 284 | |
| 285 | switch ((cmd >> 24) & 0x1f) { |
| 286 | case 0x1c: |
| 287 | return 1; |
| 288 | case 0x1d: |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 289 | switch ((cmd >> 16) & 0xff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | case 0x3: |
| 291 | return (cmd & 0x1f) + 2; |
| 292 | case 0x4: |
| 293 | return (cmd & 0xf) + 2; |
| 294 | default: |
| 295 | return (cmd & 0xffff) + 2; |
| 296 | } |
| 297 | case 0x1e: |
| 298 | if (cmd & (1 << 23)) |
| 299 | return (cmd & 0xffff) + 1; |
| 300 | else |
| 301 | return 1; |
| 302 | case 0x1f: |
| 303 | if ((cmd & (1 << 23)) == 0) /* inline vertices */ |
| 304 | return (cmd & 0x1ffff) + 2; |
| 305 | else if (cmd & (1 << 17)) /* indirect random */ |
| 306 | if ((cmd & 0xffff) == 0) |
| 307 | return 0; /* unknown length, too hard */ |
| 308 | else |
| 309 | return (((cmd & 0xffff) + 1) / 2) + 1; |
| 310 | else |
| 311 | return 2; /* indirect sequential */ |
| 312 | default: |
| 313 | return 0; |
| 314 | } |
| 315 | default: |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static int validate_cmd(int cmd) |
| 323 | { |
| 324 | int ret = do_validate_cmd(cmd); |
| 325 | |
Dave Airlie | bc5f452 | 2007-11-05 12:50:58 +1000 | [diff] [blame] | 326 | /* printk("validate_cmd( %x ): %d\n", cmd, ret); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
| 328 | return ret; |
| 329 | } |
| 330 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 331 | static int i915_emit_cmds(struct drm_device * dev, int __user * buffer, int dwords) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
| 333 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 334 | int i; |
| 335 | RING_LOCALS; |
| 336 | |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 337 | if ((dwords+1) * sizeof(int) >= dev_priv->ring.Size - 8) |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 338 | return -EINVAL; |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 339 | |
Alan Hourihane | c29b669 | 2006-08-12 16:29:24 +1000 | [diff] [blame] | 340 | BEGIN_LP_RING((dwords+1)&~1); |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | for (i = 0; i < dwords;) { |
| 343 | int cmd, sz; |
| 344 | |
| 345 | if (DRM_COPY_FROM_USER_UNCHECKED(&cmd, &buffer[i], sizeof(cmd))) |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 346 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords) |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 349 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | OUT_RING(cmd); |
| 352 | |
| 353 | while (++i, --sz) { |
| 354 | if (DRM_COPY_FROM_USER_UNCHECKED(&cmd, &buffer[i], |
| 355 | sizeof(cmd))) { |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 356 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | OUT_RING(cmd); |
| 359 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 362 | if (dwords & 1) |
| 363 | OUT_RING(0); |
| 364 | |
| 365 | ADVANCE_LP_RING(); |
| 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return 0; |
| 368 | } |
| 369 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 370 | static int i915_emit_box(struct drm_device * dev, |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 371 | struct drm_clip_rect __user * boxes, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | int i, int DR1, int DR4) |
| 373 | { |
| 374 | drm_i915_private_t *dev_priv = dev->dev_private; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 375 | struct drm_clip_rect box; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | RING_LOCALS; |
| 377 | |
| 378 | if (DRM_COPY_FROM_USER_UNCHECKED(&box, &boxes[i], sizeof(box))) { |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 379 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) { |
| 383 | DRM_ERROR("Bad box %d,%d..%d,%d\n", |
| 384 | box.x1, box.y1, box.x2, box.y2); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 385 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Alan Hourihane | c29b669 | 2006-08-12 16:29:24 +1000 | [diff] [blame] | 388 | if (IS_I965G(dev)) { |
| 389 | BEGIN_LP_RING(4); |
| 390 | OUT_RING(GFX_OP_DRAWRECT_INFO_I965); |
| 391 | OUT_RING((box.x1 & 0xffff) | (box.y1 << 16)); |
Andrew Morton | 78eca43 | 2006-08-16 09:15:51 +1000 | [diff] [blame] | 392 | OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16)); |
Alan Hourihane | c29b669 | 2006-08-12 16:29:24 +1000 | [diff] [blame] | 393 | OUT_RING(DR4); |
| 394 | ADVANCE_LP_RING(); |
| 395 | } else { |
| 396 | BEGIN_LP_RING(6); |
| 397 | OUT_RING(GFX_OP_DRAWRECT_INFO); |
| 398 | OUT_RING(DR1); |
| 399 | OUT_RING((box.x1 & 0xffff) | (box.y1 << 16)); |
| 400 | OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16)); |
| 401 | OUT_RING(DR4); |
| 402 | OUT_RING(0); |
| 403 | ADVANCE_LP_RING(); |
| 404 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
Alan Hourihane | c29b669 | 2006-08-12 16:29:24 +1000 | [diff] [blame] | 409 | /* XXX: Emitting the counter should really be moved to part of the IRQ |
| 410 | * emit. For now, do it in both places: |
| 411 | */ |
| 412 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 413 | static void i915_emit_breadcrumb(struct drm_device *dev) |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 414 | { |
| 415 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 416 | RING_LOCALS; |
| 417 | |
Alan Hourihane | c29b669 | 2006-08-12 16:29:24 +1000 | [diff] [blame] | 418 | dev_priv->sarea_priv->last_enqueue = ++dev_priv->counter; |
| 419 | |
| 420 | if (dev_priv->counter > 0x7FFFFFFFUL) |
| 421 | dev_priv->sarea_priv->last_enqueue = dev_priv->counter = 1; |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 422 | |
| 423 | BEGIN_LP_RING(4); |
| 424 | OUT_RING(CMD_STORE_DWORD_IDX); |
| 425 | OUT_RING(20); |
| 426 | OUT_RING(dev_priv->counter); |
| 427 | OUT_RING(0); |
| 428 | ADVANCE_LP_RING(); |
| 429 | } |
| 430 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 431 | static int i915_dispatch_cmdbuffer(struct drm_device * dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | drm_i915_cmdbuffer_t * cmd) |
| 433 | { |
| 434 | int nbox = cmd->num_cliprects; |
| 435 | int i = 0, count, ret; |
| 436 | |
| 437 | if (cmd->sz & 0x3) { |
| 438 | DRM_ERROR("alignment"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 439 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | i915_kernel_lost_context(dev); |
| 443 | |
| 444 | count = nbox ? nbox : 1; |
| 445 | |
| 446 | for (i = 0; i < count; i++) { |
| 447 | if (i < nbox) { |
| 448 | ret = i915_emit_box(dev, cmd->cliprects, i, |
| 449 | cmd->DR1, cmd->DR4); |
| 450 | if (ret) |
| 451 | return ret; |
| 452 | } |
| 453 | |
| 454 | ret = i915_emit_cmds(dev, (int __user *)cmd->buf, cmd->sz / 4); |
| 455 | if (ret) |
| 456 | return ret; |
| 457 | } |
| 458 | |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 459 | i915_emit_breadcrumb(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 463 | static int i915_dispatch_batchbuffer(struct drm_device * dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | drm_i915_batchbuffer_t * batch) |
| 465 | { |
| 466 | drm_i915_private_t *dev_priv = dev->dev_private; |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 467 | struct drm_clip_rect __user *boxes = batch->cliprects; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | int nbox = batch->num_cliprects; |
| 469 | int i = 0, count; |
| 470 | RING_LOCALS; |
| 471 | |
| 472 | if ((batch->start | batch->used) & 0x7) { |
| 473 | DRM_ERROR("alignment"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 474 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | i915_kernel_lost_context(dev); |
| 478 | |
| 479 | count = nbox ? nbox : 1; |
| 480 | |
| 481 | for (i = 0; i < count; i++) { |
| 482 | if (i < nbox) { |
| 483 | int ret = i915_emit_box(dev, boxes, i, |
| 484 | batch->DR1, batch->DR4); |
| 485 | if (ret) |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | if (dev_priv->use_mi_batchbuffer_start) { |
| 490 | BEGIN_LP_RING(2); |
Dave Airlie | 21f1628 | 2007-08-07 09:09:51 +1000 | [diff] [blame] | 491 | if (IS_I965G(dev)) { |
| 492 | OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965); |
| 493 | OUT_RING(batch->start); |
| 494 | } else { |
| 495 | OUT_RING(MI_BATCH_BUFFER_START | (2 << 6)); |
| 496 | OUT_RING(batch->start | MI_BATCH_NON_SECURE); |
| 497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | ADVANCE_LP_RING(); |
| 499 | } else { |
| 500 | BEGIN_LP_RING(4); |
| 501 | OUT_RING(MI_BATCH_BUFFER); |
| 502 | OUT_RING(batch->start | MI_BATCH_NON_SECURE); |
| 503 | OUT_RING(batch->start + batch->used - 4); |
| 504 | OUT_RING(0); |
| 505 | ADVANCE_LP_RING(); |
| 506 | } |
| 507 | } |
| 508 | |
Dave Airlie | de227f5 | 2006-01-25 15:31:43 +1100 | [diff] [blame] | 509 | i915_emit_breadcrumb(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 514 | static int i915_dispatch_flip(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
| 516 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 517 | RING_LOCALS; |
| 518 | |
| 519 | DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n", |
| 520 | __FUNCTION__, |
| 521 | dev_priv->current_page, |
| 522 | dev_priv->sarea_priv->pf_current_page); |
| 523 | |
| 524 | i915_kernel_lost_context(dev); |
| 525 | |
| 526 | BEGIN_LP_RING(2); |
| 527 | OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE); |
| 528 | OUT_RING(0); |
| 529 | ADVANCE_LP_RING(); |
| 530 | |
| 531 | BEGIN_LP_RING(6); |
| 532 | OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP); |
| 533 | OUT_RING(0); |
| 534 | if (dev_priv->current_page == 0) { |
| 535 | OUT_RING(dev_priv->back_offset); |
| 536 | dev_priv->current_page = 1; |
| 537 | } else { |
| 538 | OUT_RING(dev_priv->front_offset); |
| 539 | dev_priv->current_page = 0; |
| 540 | } |
| 541 | OUT_RING(0); |
| 542 | ADVANCE_LP_RING(); |
| 543 | |
| 544 | BEGIN_LP_RING(2); |
| 545 | OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP); |
| 546 | OUT_RING(0); |
| 547 | ADVANCE_LP_RING(); |
| 548 | |
| 549 | dev_priv->sarea_priv->last_enqueue = dev_priv->counter++; |
| 550 | |
| 551 | BEGIN_LP_RING(4); |
| 552 | OUT_RING(CMD_STORE_DWORD_IDX); |
| 553 | OUT_RING(20); |
| 554 | OUT_RING(dev_priv->counter); |
| 555 | OUT_RING(0); |
| 556 | ADVANCE_LP_RING(); |
| 557 | |
| 558 | dev_priv->sarea_priv->pf_current_page = dev_priv->current_page; |
| 559 | return 0; |
| 560 | } |
| 561 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 562 | static int i915_quiescent(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
| 564 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 565 | |
| 566 | i915_kernel_lost_context(dev); |
| 567 | return i915_wait_ring(dev, dev_priv->ring.Size - 8, __FUNCTION__); |
| 568 | } |
| 569 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 570 | static int i915_flush_ioctl(struct drm_device *dev, void *data, |
| 571 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | { |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 573 | LOCK_TEST_WITH_RETURN(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
| 575 | return i915_quiescent(dev); |
| 576 | } |
| 577 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 578 | static int i915_batchbuffer(struct drm_device *dev, void *data, |
| 579 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; |
| 582 | u32 *hw_status = dev_priv->hw_status_page; |
| 583 | drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *) |
| 584 | dev_priv->sarea_priv; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 585 | drm_i915_batchbuffer_t *batch = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | int ret; |
| 587 | |
| 588 | if (!dev_priv->allow_batchbuffer) { |
| 589 | DRM_ERROR("Batchbuffer ioctl disabled\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 590 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | DRM_DEBUG("i915 batchbuffer, start %x used %d cliprects %d\n", |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 594 | batch->start, batch->used, batch->num_cliprects); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 596 | LOCK_TEST_WITH_RETURN(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 598 | if (batch->num_cliprects && DRM_VERIFYAREA_READ(batch->cliprects, |
| 599 | batch->num_cliprects * |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 600 | sizeof(struct drm_clip_rect))) |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 601 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 603 | ret = i915_dispatch_batchbuffer(dev, batch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | sarea_priv->last_dispatch = (int)hw_status[5]; |
| 606 | return ret; |
| 607 | } |
| 608 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 609 | static int i915_cmdbuffer(struct drm_device *dev, void *data, |
| 610 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; |
| 613 | u32 *hw_status = dev_priv->hw_status_page; |
| 614 | drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *) |
| 615 | dev_priv->sarea_priv; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 616 | drm_i915_cmdbuffer_t *cmdbuf = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | int ret; |
| 618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | DRM_DEBUG("i915 cmdbuffer, buf %p sz %d cliprects %d\n", |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 620 | cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 622 | LOCK_TEST_WITH_RETURN(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 624 | if (cmdbuf->num_cliprects && |
| 625 | DRM_VERIFYAREA_READ(cmdbuf->cliprects, |
| 626 | cmdbuf->num_cliprects * |
Dave Airlie | c60ce62 | 2007-07-11 15:27:12 +1000 | [diff] [blame] | 627 | sizeof(struct drm_clip_rect))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | DRM_ERROR("Fault accessing cliprects\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 629 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 632 | ret = i915_dispatch_cmdbuffer(dev, cmdbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | if (ret) { |
| 634 | DRM_ERROR("i915_dispatch_cmdbuffer failed\n"); |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | sarea_priv->last_dispatch = (int)hw_status[5]; |
| 639 | return 0; |
| 640 | } |
| 641 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 642 | static int i915_flip_bufs(struct drm_device *dev, void *data, |
| 643 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | DRM_DEBUG("%s\n", __FUNCTION__); |
| 646 | |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 647 | LOCK_TEST_WITH_RETURN(dev, file_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
| 649 | return i915_dispatch_flip(dev); |
| 650 | } |
| 651 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 652 | static int i915_getparam(struct drm_device *dev, void *data, |
| 653 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 656 | drm_i915_getparam_t *param = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | int value; |
| 658 | |
| 659 | if (!dev_priv) { |
Márton Németh | 3e684ea | 2008-01-24 15:58:57 +1000 | [diff] [blame] | 660 | DRM_ERROR("called with no initialization\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 661 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 664 | switch (param->param) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | case I915_PARAM_IRQ_ACTIVE: |
| 666 | value = dev->irq ? 1 : 0; |
| 667 | break; |
| 668 | case I915_PARAM_ALLOW_BATCHBUFFER: |
| 669 | value = dev_priv->allow_batchbuffer ? 1 : 0; |
| 670 | break; |
Dave Airlie | 0d6aa60 | 2006-01-02 20:14:23 +1100 | [diff] [blame] | 671 | case I915_PARAM_LAST_DISPATCH: |
| 672 | value = READ_BREADCRUMB(dev_priv); |
| 673 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | default: |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 675 | DRM_ERROR("Unknown parameter %d\n", param->param); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 676 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 679 | if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | DRM_ERROR("DRM_COPY_TO_USER failed\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 681 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 687 | static int i915_setparam(struct drm_device *dev, void *data, |
| 688 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 691 | drm_i915_setparam_t *param = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
| 693 | if (!dev_priv) { |
Márton Németh | 3e684ea | 2008-01-24 15:58:57 +1000 | [diff] [blame] | 694 | DRM_ERROR("called with no initialization\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 695 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 698 | switch (param->param) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | case I915_SETPARAM_USE_MI_BATCHBUFFER_START: |
Dave Airlie | 21f1628 | 2007-08-07 09:09:51 +1000 | [diff] [blame] | 700 | if (!IS_I965G(dev)) |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 701 | dev_priv->use_mi_batchbuffer_start = param->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | break; |
| 703 | case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY: |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 704 | dev_priv->tex_lru_log_granularity = param->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | break; |
| 706 | case I915_SETPARAM_ALLOW_BATCHBUFFER: |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 707 | dev_priv->allow_batchbuffer = param->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | break; |
| 709 | default: |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 710 | DRM_ERROR("unknown parameter %d\n", param->param); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 711 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 717 | static int i915_set_status_page(struct drm_device *dev, void *data, |
| 718 | struct drm_file *file_priv) |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 719 | { |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 720 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 721 | drm_i915_hws_addr_t *hws = data; |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 722 | |
Zhenyu Wang | b39d50e | 2008-02-19 20:59:09 +1000 | [diff] [blame^] | 723 | if (!I915_NEED_GFX_HWS(dev)) |
| 724 | return -EINVAL; |
| 725 | |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 726 | if (!dev_priv) { |
Márton Németh | 3e684ea | 2008-01-24 15:58:57 +1000 | [diff] [blame] | 727 | DRM_ERROR("called with no initialization\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 728 | return -EINVAL; |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 729 | } |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 730 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 731 | printk(KERN_DEBUG "set status page addr 0x%08x\n", (u32)hws->addr); |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 732 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 733 | dev_priv->status_gfx_addr = hws->addr & (0x1ffff<<12); |
| 734 | |
Eric Anholt | 8b40958 | 2007-11-22 16:40:37 +1000 | [diff] [blame] | 735 | dev_priv->hws_map.offset = dev->agp->base + hws->addr; |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 736 | dev_priv->hws_map.size = 4*1024; |
| 737 | dev_priv->hws_map.type = 0; |
| 738 | dev_priv->hws_map.flags = 0; |
| 739 | dev_priv->hws_map.mtrr = 0; |
| 740 | |
| 741 | drm_core_ioremap(&dev_priv->hws_map, dev); |
| 742 | if (dev_priv->hws_map.handle == NULL) { |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 743 | i915_dma_cleanup(dev); |
| 744 | dev_priv->status_gfx_addr = 0; |
| 745 | DRM_ERROR("can not ioremap virtual address for" |
| 746 | " G33 hw status page\n"); |
Eric Anholt | 20caafa | 2007-08-25 19:22:43 +1000 | [diff] [blame] | 747 | return -ENOMEM; |
Wang Zhenyu | dc7a931 | 2007-06-10 15:58:19 +1000 | [diff] [blame] | 748 | } |
| 749 | dev_priv->hw_status_page = dev_priv->hws_map.handle; |
| 750 | |
| 751 | memset(dev_priv->hw_status_page, 0, PAGE_SIZE); |
| 752 | I915_WRITE(0x02080, dev_priv->status_gfx_addr); |
| 753 | DRM_DEBUG("load hws 0x2080 with gfx mem 0x%x\n", |
| 754 | dev_priv->status_gfx_addr); |
| 755 | DRM_DEBUG("load hws at %p\n", dev_priv->hw_status_page); |
| 756 | return 0; |
| 757 | } |
| 758 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 759 | int i915_driver_load(struct drm_device *dev, unsigned long flags) |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 760 | { |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 761 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 762 | unsigned long base, size; |
| 763 | int ret = 0, mmio_bar = IS_I9XX(dev) ? 0 : 1; |
| 764 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 765 | /* i915 has 4 more counters */ |
| 766 | dev->counters += 4; |
| 767 | dev->types[6] = _DRM_STAT_IRQ; |
| 768 | dev->types[7] = _DRM_STAT_PRIMARY; |
| 769 | dev->types[8] = _DRM_STAT_SECONDARY; |
| 770 | dev->types[9] = _DRM_STAT_DMA; |
| 771 | |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 772 | dev_priv = drm_alloc(sizeof(drm_i915_private_t), DRM_MEM_DRIVER); |
| 773 | if (dev_priv == NULL) |
| 774 | return -ENOMEM; |
| 775 | |
| 776 | memset(dev_priv, 0, sizeof(drm_i915_private_t)); |
| 777 | |
| 778 | dev->dev_private = (void *)dev_priv; |
| 779 | |
| 780 | /* Add register map (needed for suspend/resume) */ |
| 781 | base = drm_get_resource_start(dev, mmio_bar); |
| 782 | size = drm_get_resource_len(dev, mmio_bar); |
| 783 | |
Dave Airlie | e3236a1 | 2007-12-17 09:41:56 +1000 | [diff] [blame] | 784 | ret = drm_addmap(dev, base, size, _DRM_REGISTERS, |
| 785 | _DRM_KERNEL | _DRM_DRIVER, |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 786 | &dev_priv->mmio_map); |
| 787 | return ret; |
| 788 | } |
| 789 | |
| 790 | int i915_driver_unload(struct drm_device *dev) |
| 791 | { |
| 792 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 793 | |
| 794 | if (dev_priv->mmio_map) |
| 795 | drm_rmmap(dev, dev_priv->mmio_map); |
| 796 | |
| 797 | drm_free(dev->dev_private, sizeof(drm_i915_private_t), |
| 798 | DRM_MEM_DRIVER); |
| 799 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame] | 800 | return 0; |
| 801 | } |
| 802 | |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 803 | void i915_driver_lastclose(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | { |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 805 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 806 | |
| 807 | if (dev_priv->agp_heap) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 808 | i915_mem_takedown(&(dev_priv->agp_heap)); |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 809 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 810 | i915_dma_cleanup(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 813 | 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] | 814 | { |
Jesse Barnes | ba8bbcf | 2007-11-22 14:14:14 +1000 | [diff] [blame] | 815 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 816 | i915_mem_release(dev, file_priv, dev_priv->agp_heap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 819 | struct drm_ioctl_desc i915_ioctls[] = { |
| 820 | DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), |
| 821 | DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH), |
| 822 | DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH), |
| 823 | DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH), |
| 824 | DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH), |
| 825 | DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH), |
| 826 | DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH), |
| 827 | DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), |
| 828 | DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH), |
| 829 | DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH), |
| 830 | DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), |
| 831 | DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH), |
| 832 | DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ), |
| 833 | DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ), |
| 834 | DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ), |
| 835 | DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH), |
| 836 | DRM_IOCTL_DEF(DRM_I915_HWS_ADDR, i915_set_status_page, DRM_AUTH), |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 837 | }; |
| 838 | |
| 839 | int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls); |
Dave Airlie | cda1738 | 2005-07-10 17:31:26 +1000 | [diff] [blame] | 840 | |
| 841 | /** |
| 842 | * Determine if the device really is AGP or not. |
| 843 | * |
| 844 | * All Intel graphics chipsets are treated as AGP, even if they are really |
| 845 | * PCI-e. |
| 846 | * |
| 847 | * \param dev The device to be tested. |
| 848 | * |
| 849 | * \returns |
| 850 | * A value of 1 is always retured to indictate every i9x5 is AGP. |
| 851 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 852 | int i915_driver_device_is_agp(struct drm_device * dev) |
Dave Airlie | cda1738 | 2005-07-10 17:31:26 +1000 | [diff] [blame] | 853 | { |
| 854 | return 1; |
| 855 | } |