Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008,2010 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * Chris Wilson <chris@chris-wilson.co.uk> |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | #include "drmP.h" |
| 30 | #include "drm.h" |
| 31 | #include "i915_drm.h" |
| 32 | #include "i915_drv.h" |
| 33 | #include "i915_trace.h" |
| 34 | #include "intel_drv.h" |
Eugeni Dodonov | f45b555 | 2011-12-09 17:16:37 -0800 | [diff] [blame] | 35 | #include <linux/dma_remapping.h> |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 36 | |
| 37 | struct change_domains { |
| 38 | uint32_t invalidate_domains; |
| 39 | uint32_t flush_domains; |
| 40 | uint32_t flush_rings; |
Chris Wilson | c59a333 | 2011-03-06 13:51:29 +0000 | [diff] [blame] | 41 | uint32_t flips; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | /* |
| 45 | * Set the next domain for the specified object. This |
| 46 | * may not actually perform the necessary flushing/invaliding though, |
| 47 | * as that may want to be batched with other set_domain operations |
| 48 | * |
| 49 | * This is (we hope) the only really tricky part of gem. The goal |
| 50 | * is fairly simple -- track which caches hold bits of the object |
| 51 | * and make sure they remain coherent. A few concrete examples may |
| 52 | * help to explain how it works. For shorthand, we use the notation |
| 53 | * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the |
| 54 | * a pair of read and write domain masks. |
| 55 | * |
| 56 | * Case 1: the batch buffer |
| 57 | * |
| 58 | * 1. Allocated |
| 59 | * 2. Written by CPU |
| 60 | * 3. Mapped to GTT |
| 61 | * 4. Read by GPU |
| 62 | * 5. Unmapped from GTT |
| 63 | * 6. Freed |
| 64 | * |
| 65 | * Let's take these a step at a time |
| 66 | * |
| 67 | * 1. Allocated |
| 68 | * Pages allocated from the kernel may still have |
| 69 | * cache contents, so we set them to (CPU, CPU) always. |
| 70 | * 2. Written by CPU (using pwrite) |
| 71 | * The pwrite function calls set_domain (CPU, CPU) and |
| 72 | * this function does nothing (as nothing changes) |
| 73 | * 3. Mapped by GTT |
| 74 | * This function asserts that the object is not |
| 75 | * currently in any GPU-based read or write domains |
| 76 | * 4. Read by GPU |
| 77 | * i915_gem_execbuffer calls set_domain (COMMAND, 0). |
| 78 | * As write_domain is zero, this function adds in the |
| 79 | * current read domains (CPU+COMMAND, 0). |
| 80 | * flush_domains is set to CPU. |
| 81 | * invalidate_domains is set to COMMAND |
| 82 | * clflush is run to get data out of the CPU caches |
| 83 | * then i915_dev_set_domain calls i915_gem_flush to |
| 84 | * emit an MI_FLUSH and drm_agp_chipset_flush |
| 85 | * 5. Unmapped from GTT |
| 86 | * i915_gem_object_unbind calls set_domain (CPU, CPU) |
| 87 | * flush_domains and invalidate_domains end up both zero |
| 88 | * so no flushing/invalidating happens |
| 89 | * 6. Freed |
| 90 | * yay, done |
| 91 | * |
| 92 | * Case 2: The shared render buffer |
| 93 | * |
| 94 | * 1. Allocated |
| 95 | * 2. Mapped to GTT |
| 96 | * 3. Read/written by GPU |
| 97 | * 4. set_domain to (CPU,CPU) |
| 98 | * 5. Read/written by CPU |
| 99 | * 6. Read/written by GPU |
| 100 | * |
| 101 | * 1. Allocated |
| 102 | * Same as last example, (CPU, CPU) |
| 103 | * 2. Mapped to GTT |
| 104 | * Nothing changes (assertions find that it is not in the GPU) |
| 105 | * 3. Read/written by GPU |
| 106 | * execbuffer calls set_domain (RENDER, RENDER) |
| 107 | * flush_domains gets CPU |
| 108 | * invalidate_domains gets GPU |
| 109 | * clflush (obj) |
| 110 | * MI_FLUSH and drm_agp_chipset_flush |
| 111 | * 4. set_domain (CPU, CPU) |
| 112 | * flush_domains gets GPU |
| 113 | * invalidate_domains gets CPU |
| 114 | * wait_rendering (obj) to make sure all drawing is complete. |
| 115 | * This will include an MI_FLUSH to get the data from GPU |
| 116 | * to memory |
| 117 | * clflush (obj) to invalidate the CPU cache |
| 118 | * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?) |
| 119 | * 5. Read/written by CPU |
| 120 | * cache lines are loaded and dirtied |
| 121 | * 6. Read written by GPU |
| 122 | * Same as last GPU access |
| 123 | * |
| 124 | * Case 3: The constant buffer |
| 125 | * |
| 126 | * 1. Allocated |
| 127 | * 2. Written by CPU |
| 128 | * 3. Read by GPU |
| 129 | * 4. Updated (written) by CPU again |
| 130 | * 5. Read by GPU |
| 131 | * |
| 132 | * 1. Allocated |
| 133 | * (CPU, CPU) |
| 134 | * 2. Written by CPU |
| 135 | * (CPU, CPU) |
| 136 | * 3. Read by GPU |
| 137 | * (CPU+RENDER, 0) |
| 138 | * flush_domains = CPU |
| 139 | * invalidate_domains = RENDER |
| 140 | * clflush (obj) |
| 141 | * MI_FLUSH |
| 142 | * drm_agp_chipset_flush |
| 143 | * 4. Updated (written) by CPU again |
| 144 | * (CPU, CPU) |
| 145 | * flush_domains = 0 (no previous write domain) |
| 146 | * invalidate_domains = 0 (no new read domains) |
| 147 | * 5. Read by GPU |
| 148 | * (CPU+RENDER, 0) |
| 149 | * flush_domains = CPU |
| 150 | * invalidate_domains = RENDER |
| 151 | * clflush (obj) |
| 152 | * MI_FLUSH |
| 153 | * drm_agp_chipset_flush |
| 154 | */ |
| 155 | static void |
| 156 | i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj, |
| 157 | struct intel_ring_buffer *ring, |
| 158 | struct change_domains *cd) |
| 159 | { |
| 160 | uint32_t invalidate_domains = 0, flush_domains = 0; |
| 161 | |
| 162 | /* |
| 163 | * If the object isn't moving to a new write domain, |
| 164 | * let the object stay in multiple read domains |
| 165 | */ |
| 166 | if (obj->base.pending_write_domain == 0) |
| 167 | obj->base.pending_read_domains |= obj->base.read_domains; |
| 168 | |
| 169 | /* |
| 170 | * Flush the current write domain if |
| 171 | * the new read domains don't match. Invalidate |
| 172 | * any read domains which differ from the old |
| 173 | * write domain |
| 174 | */ |
| 175 | if (obj->base.write_domain && |
| 176 | (((obj->base.write_domain != obj->base.pending_read_domains || |
| 177 | obj->ring != ring)) || |
| 178 | (obj->fenced_gpu_access && !obj->pending_fenced_gpu_access))) { |
| 179 | flush_domains |= obj->base.write_domain; |
| 180 | invalidate_domains |= |
| 181 | obj->base.pending_read_domains & ~obj->base.write_domain; |
| 182 | } |
| 183 | /* |
| 184 | * Invalidate any read caches which may have |
| 185 | * stale data. That is, any new read domains. |
| 186 | */ |
| 187 | invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains; |
| 188 | if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) |
| 189 | i915_gem_clflush_object(obj); |
| 190 | |
Chris Wilson | c59a333 | 2011-03-06 13:51:29 +0000 | [diff] [blame] | 191 | if (obj->base.pending_write_domain) |
| 192 | cd->flips |= atomic_read(&obj->pending_flip); |
| 193 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 194 | /* The actual obj->write_domain will be updated with |
| 195 | * pending_write_domain after we emit the accumulated flush for all |
| 196 | * of our domain changes in execbuffers (which clears objects' |
| 197 | * write_domains). So if we have a current write domain that we |
| 198 | * aren't changing, set pending_write_domain to that. |
| 199 | */ |
| 200 | if (flush_domains == 0 && obj->base.pending_write_domain == 0) |
| 201 | obj->base.pending_write_domain = obj->base.write_domain; |
| 202 | |
| 203 | cd->invalidate_domains |= invalidate_domains; |
| 204 | cd->flush_domains |= flush_domains; |
| 205 | if (flush_domains & I915_GEM_GPU_DOMAINS) |
| 206 | cd->flush_rings |= obj->ring->id; |
| 207 | if (invalidate_domains & I915_GEM_GPU_DOMAINS) |
| 208 | cd->flush_rings |= ring->id; |
| 209 | } |
| 210 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 211 | struct eb_objects { |
| 212 | int and; |
| 213 | struct hlist_head buckets[0]; |
| 214 | }; |
| 215 | |
| 216 | static struct eb_objects * |
| 217 | eb_create(int size) |
| 218 | { |
| 219 | struct eb_objects *eb; |
| 220 | int count = PAGE_SIZE / sizeof(struct hlist_head) / 2; |
| 221 | while (count > size) |
| 222 | count >>= 1; |
| 223 | eb = kzalloc(count*sizeof(struct hlist_head) + |
| 224 | sizeof(struct eb_objects), |
| 225 | GFP_KERNEL); |
| 226 | if (eb == NULL) |
| 227 | return eb; |
| 228 | |
| 229 | eb->and = count - 1; |
| 230 | return eb; |
| 231 | } |
| 232 | |
| 233 | static void |
| 234 | eb_reset(struct eb_objects *eb) |
| 235 | { |
| 236 | memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head)); |
| 237 | } |
| 238 | |
| 239 | static void |
| 240 | eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj) |
| 241 | { |
| 242 | hlist_add_head(&obj->exec_node, |
| 243 | &eb->buckets[obj->exec_handle & eb->and]); |
| 244 | } |
| 245 | |
| 246 | static struct drm_i915_gem_object * |
| 247 | eb_get_object(struct eb_objects *eb, unsigned long handle) |
| 248 | { |
| 249 | struct hlist_head *head; |
| 250 | struct hlist_node *node; |
| 251 | struct drm_i915_gem_object *obj; |
| 252 | |
| 253 | head = &eb->buckets[handle & eb->and]; |
| 254 | hlist_for_each(node, head) { |
| 255 | obj = hlist_entry(node, struct drm_i915_gem_object, exec_node); |
| 256 | if (obj->exec_handle == handle) |
| 257 | return obj; |
| 258 | } |
| 259 | |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | static void |
| 264 | eb_destroy(struct eb_objects *eb) |
| 265 | { |
| 266 | kfree(eb); |
| 267 | } |
| 268 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 269 | static int |
| 270 | i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 271 | struct eb_objects *eb, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 272 | struct drm_i915_gem_relocation_entry *reloc) |
| 273 | { |
| 274 | struct drm_device *dev = obj->base.dev; |
| 275 | struct drm_gem_object *target_obj; |
| 276 | uint32_t target_offset; |
| 277 | int ret = -EINVAL; |
| 278 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 279 | /* we've already hold a reference to all valid objects */ |
| 280 | target_obj = &eb_get_object(eb, reloc->target_handle)->base; |
| 281 | if (unlikely(target_obj == NULL)) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 282 | return -ENOENT; |
| 283 | |
| 284 | target_offset = to_intel_bo(target_obj)->gtt_offset; |
| 285 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 286 | /* The target buffer should have appeared before us in the |
| 287 | * exec_object list, so it should have a GTT space bound by now. |
| 288 | */ |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 289 | if (unlikely(target_offset == 0)) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 290 | DRM_ERROR("No GTT space found for object %d\n", |
| 291 | reloc->target_handle); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 292 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* Validate that the target is in a valid r/w GPU domain */ |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 296 | if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 297 | DRM_ERROR("reloc with multiple write domains: " |
| 298 | "obj %p target %d offset %d " |
| 299 | "read %08x write %08x", |
| 300 | obj, reloc->target_handle, |
| 301 | (int) reloc->offset, |
| 302 | reloc->read_domains, |
| 303 | reloc->write_domain); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 304 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 305 | } |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 306 | if (unlikely((reloc->write_domain | reloc->read_domains) & I915_GEM_DOMAIN_CPU)) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 307 | DRM_ERROR("reloc with read/write CPU domains: " |
| 308 | "obj %p target %d offset %d " |
| 309 | "read %08x write %08x", |
| 310 | obj, reloc->target_handle, |
| 311 | (int) reloc->offset, |
| 312 | reloc->read_domains, |
| 313 | reloc->write_domain); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 314 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 315 | } |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 316 | if (unlikely(reloc->write_domain && target_obj->pending_write_domain && |
| 317 | reloc->write_domain != target_obj->pending_write_domain)) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 318 | DRM_ERROR("Write domain conflict: " |
| 319 | "obj %p target %d offset %d " |
| 320 | "new %08x old %08x\n", |
| 321 | obj, reloc->target_handle, |
| 322 | (int) reloc->offset, |
| 323 | reloc->write_domain, |
| 324 | target_obj->pending_write_domain); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 325 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | target_obj->pending_read_domains |= reloc->read_domains; |
| 329 | target_obj->pending_write_domain |= reloc->write_domain; |
| 330 | |
| 331 | /* If the relocation already has the right value in it, no |
| 332 | * more work needs to be done. |
| 333 | */ |
| 334 | if (target_offset == reloc->presumed_offset) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 335 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 336 | |
| 337 | /* Check that the relocation address is valid... */ |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 338 | if (unlikely(reloc->offset > obj->base.size - 4)) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 339 | DRM_ERROR("Relocation beyond object bounds: " |
| 340 | "obj %p target %d offset %d size %d.\n", |
| 341 | obj, reloc->target_handle, |
| 342 | (int) reloc->offset, |
| 343 | (int) obj->base.size); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 344 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 345 | } |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 346 | if (unlikely(reloc->offset & 3)) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 347 | DRM_ERROR("Relocation not 4-byte aligned: " |
| 348 | "obj %p target %d offset %d.\n", |
| 349 | obj, reloc->target_handle, |
| 350 | (int) reloc->offset); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 351 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 354 | reloc->delta += target_offset; |
| 355 | if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) { |
| 356 | uint32_t page_offset = reloc->offset & ~PAGE_MASK; |
| 357 | char *vaddr; |
| 358 | |
| 359 | vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]); |
| 360 | *(uint32_t *)(vaddr + page_offset) = reloc->delta; |
| 361 | kunmap_atomic(vaddr); |
| 362 | } else { |
| 363 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 364 | uint32_t __iomem *reloc_entry; |
| 365 | void __iomem *reloc_page; |
| 366 | |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 367 | /* We can't wait for rendering with pagefaults disabled */ |
| 368 | if (obj->active && in_atomic()) |
| 369 | return -EFAULT; |
| 370 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 371 | ret = i915_gem_object_set_to_gtt_domain(obj, 1); |
| 372 | if (ret) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 373 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 374 | |
| 375 | /* Map the page containing the relocation we're going to perform. */ |
| 376 | reloc->offset += obj->gtt_offset; |
| 377 | reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping, |
| 378 | reloc->offset & PAGE_MASK); |
| 379 | reloc_entry = (uint32_t __iomem *) |
| 380 | (reloc_page + (reloc->offset & ~PAGE_MASK)); |
| 381 | iowrite32(reloc->delta, reloc_entry); |
| 382 | io_mapping_unmap_atomic(reloc_page); |
| 383 | } |
| 384 | |
| 385 | /* and update the user's relocation entry */ |
| 386 | reloc->presumed_offset = target_offset; |
| 387 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 388 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static int |
| 392 | i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj, |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 393 | struct eb_objects *eb) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 394 | { |
| 395 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 396 | struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 397 | int i, ret; |
| 398 | |
| 399 | user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr; |
| 400 | for (i = 0; i < entry->relocation_count; i++) { |
| 401 | struct drm_i915_gem_relocation_entry reloc; |
| 402 | |
| 403 | if (__copy_from_user_inatomic(&reloc, |
| 404 | user_relocs+i, |
| 405 | sizeof(reloc))) |
| 406 | return -EFAULT; |
| 407 | |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 408 | ret = i915_gem_execbuffer_relocate_entry(obj, eb, &reloc); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 409 | if (ret) |
| 410 | return ret; |
| 411 | |
| 412 | if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset, |
| 413 | &reloc.presumed_offset, |
| 414 | sizeof(reloc.presumed_offset))) |
| 415 | return -EFAULT; |
| 416 | } |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static int |
| 422 | i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 423 | struct eb_objects *eb, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 424 | struct drm_i915_gem_relocation_entry *relocs) |
| 425 | { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 426 | const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 427 | int i, ret; |
| 428 | |
| 429 | for (i = 0; i < entry->relocation_count; i++) { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 430 | ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 431 | if (ret) |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static int |
| 439 | i915_gem_execbuffer_relocate(struct drm_device *dev, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 440 | struct eb_objects *eb, |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 441 | struct list_head *objects) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 442 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 443 | struct drm_i915_gem_object *obj; |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 444 | int ret = 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 445 | |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 446 | /* This is the fast path and we cannot handle a pagefault whilst |
| 447 | * holding the struct mutex lest the user pass in the relocations |
| 448 | * contained within a mmaped bo. For in such a case we, the page |
| 449 | * fault handler would call i915_gem_fault() and we would try to |
| 450 | * acquire the struct mutex again. Obviously this is bad and so |
| 451 | * lockdep complains vehemently. |
| 452 | */ |
| 453 | pagefault_disable(); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 454 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 455 | ret = i915_gem_execbuffer_relocate_object(obj, eb); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 456 | if (ret) |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 457 | break; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 458 | } |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 459 | pagefault_enable(); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 460 | |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 461 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static int |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 465 | i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 466 | struct drm_file *file, |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 467 | struct list_head *objects) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 468 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 469 | struct drm_i915_gem_object *obj; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 470 | int ret, retry; |
Chris Wilson | 9b3826b | 2010-12-05 17:11:54 +0000 | [diff] [blame] | 471 | bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 472 | struct list_head ordered_objects; |
| 473 | |
| 474 | INIT_LIST_HEAD(&ordered_objects); |
| 475 | while (!list_empty(objects)) { |
| 476 | struct drm_i915_gem_exec_object2 *entry; |
| 477 | bool need_fence, need_mappable; |
| 478 | |
| 479 | obj = list_first_entry(objects, |
| 480 | struct drm_i915_gem_object, |
| 481 | exec_list); |
| 482 | entry = obj->exec_entry; |
| 483 | |
| 484 | need_fence = |
| 485 | has_fenced_gpu_access && |
| 486 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 487 | obj->tiling_mode != I915_TILING_NONE; |
| 488 | need_mappable = |
| 489 | entry->relocation_count ? true : need_fence; |
| 490 | |
| 491 | if (need_mappable) |
| 492 | list_move(&obj->exec_list, &ordered_objects); |
| 493 | else |
| 494 | list_move_tail(&obj->exec_list, &ordered_objects); |
Chris Wilson | 595dad7 | 2011-01-13 11:03:48 +0000 | [diff] [blame] | 495 | |
| 496 | obj->base.pending_read_domains = 0; |
| 497 | obj->base.pending_write_domain = 0; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 498 | } |
| 499 | list_splice(&ordered_objects, objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 500 | |
| 501 | /* Attempt to pin all of the buffers into the GTT. |
| 502 | * This is done in 3 phases: |
| 503 | * |
| 504 | * 1a. Unbind all objects that do not match the GTT constraints for |
| 505 | * the execbuffer (fenceable, mappable, alignment etc). |
| 506 | * 1b. Increment pin count for already bound objects. |
| 507 | * 2. Bind new objects. |
| 508 | * 3. Decrement pin count. |
| 509 | * |
| 510 | * This avoid unnecessary unbinding of later objects in order to makr |
| 511 | * room for the earlier objects *unless* we need to defragment. |
| 512 | */ |
| 513 | retry = 0; |
| 514 | do { |
| 515 | ret = 0; |
| 516 | |
| 517 | /* Unbind any ill-fitting objects or pin. */ |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 518 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 519 | struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 520 | bool need_fence, need_mappable; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 521 | if (!obj->gtt_space) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 522 | continue; |
| 523 | |
| 524 | need_fence = |
Chris Wilson | 9b3826b | 2010-12-05 17:11:54 +0000 | [diff] [blame] | 525 | has_fenced_gpu_access && |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 526 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 527 | obj->tiling_mode != I915_TILING_NONE; |
| 528 | need_mappable = |
| 529 | entry->relocation_count ? true : need_fence; |
| 530 | |
| 531 | if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) || |
| 532 | (need_mappable && !obj->map_and_fenceable)) |
| 533 | ret = i915_gem_object_unbind(obj); |
| 534 | else |
| 535 | ret = i915_gem_object_pin(obj, |
| 536 | entry->alignment, |
| 537 | need_mappable); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 538 | if (ret) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 539 | goto err; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 540 | |
| 541 | entry++; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /* Bind fresh objects */ |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 545 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 546 | struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 547 | bool need_fence; |
| 548 | |
| 549 | need_fence = |
Chris Wilson | 9b3826b | 2010-12-05 17:11:54 +0000 | [diff] [blame] | 550 | has_fenced_gpu_access && |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 551 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 552 | obj->tiling_mode != I915_TILING_NONE; |
| 553 | |
| 554 | if (!obj->gtt_space) { |
| 555 | bool need_mappable = |
| 556 | entry->relocation_count ? true : need_fence; |
| 557 | |
| 558 | ret = i915_gem_object_pin(obj, |
| 559 | entry->alignment, |
| 560 | need_mappable); |
| 561 | if (ret) |
| 562 | break; |
| 563 | } |
| 564 | |
Chris Wilson | 9b3826b | 2010-12-05 17:11:54 +0000 | [diff] [blame] | 565 | if (has_fenced_gpu_access) { |
| 566 | if (need_fence) { |
Chris Wilson | ce453d8 | 2011-02-21 14:43:56 +0000 | [diff] [blame] | 567 | ret = i915_gem_object_get_fence(obj, ring); |
Chris Wilson | 9b3826b | 2010-12-05 17:11:54 +0000 | [diff] [blame] | 568 | if (ret) |
| 569 | break; |
| 570 | } else if (entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 571 | obj->tiling_mode == I915_TILING_NONE) { |
| 572 | /* XXX pipelined! */ |
| 573 | ret = i915_gem_object_put_fence(obj); |
| 574 | if (ret) |
| 575 | break; |
| 576 | } |
| 577 | obj->pending_fenced_gpu_access = need_fence; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | entry->offset = obj->gtt_offset; |
| 581 | } |
| 582 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 583 | /* Decrement pin count for bound objects */ |
| 584 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 585 | if (obj->gtt_space) |
| 586 | i915_gem_object_unpin(obj); |
| 587 | } |
| 588 | |
| 589 | if (ret != -ENOSPC || retry > 1) |
| 590 | return ret; |
| 591 | |
| 592 | /* First attempt, just clear anything that is purgeable. |
| 593 | * Second attempt, clear the entire GTT. |
| 594 | */ |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 595 | ret = i915_gem_evict_everything(ring->dev, retry == 0); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 596 | if (ret) |
| 597 | return ret; |
| 598 | |
| 599 | retry++; |
| 600 | } while (1); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 601 | |
| 602 | err: |
Chris Wilson | 602606a | 2010-11-28 15:31:02 +0000 | [diff] [blame] | 603 | obj = list_entry(obj->exec_list.prev, |
| 604 | struct drm_i915_gem_object, |
| 605 | exec_list); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 606 | while (objects != &obj->exec_list) { |
| 607 | if (obj->gtt_space) |
| 608 | i915_gem_object_unpin(obj); |
| 609 | |
| 610 | obj = list_entry(obj->exec_list.prev, |
| 611 | struct drm_i915_gem_object, |
| 612 | exec_list); |
| 613 | } |
| 614 | |
| 615 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static int |
| 619 | i915_gem_execbuffer_relocate_slow(struct drm_device *dev, |
| 620 | struct drm_file *file, |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 621 | struct intel_ring_buffer *ring, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 622 | struct list_head *objects, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 623 | struct eb_objects *eb, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 624 | struct drm_i915_gem_exec_object2 *exec, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 625 | int count) |
| 626 | { |
| 627 | struct drm_i915_gem_relocation_entry *reloc; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 628 | struct drm_i915_gem_object *obj; |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 629 | int *reloc_offset; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 630 | int i, total, ret; |
| 631 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 632 | /* We may process another execbuffer during the unlock... */ |
Chris Wilson | 36cf174 | 2011-01-10 12:09:12 +0000 | [diff] [blame] | 633 | while (!list_empty(objects)) { |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 634 | obj = list_first_entry(objects, |
| 635 | struct drm_i915_gem_object, |
| 636 | exec_list); |
| 637 | list_del_init(&obj->exec_list); |
| 638 | drm_gem_object_unreference(&obj->base); |
| 639 | } |
| 640 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 641 | mutex_unlock(&dev->struct_mutex); |
| 642 | |
| 643 | total = 0; |
| 644 | for (i = 0; i < count; i++) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 645 | total += exec[i].relocation_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 646 | |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 647 | reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset)); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 648 | reloc = drm_malloc_ab(total, sizeof(*reloc)); |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 649 | if (reloc == NULL || reloc_offset == NULL) { |
| 650 | drm_free_large(reloc); |
| 651 | drm_free_large(reloc_offset); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 652 | mutex_lock(&dev->struct_mutex); |
| 653 | return -ENOMEM; |
| 654 | } |
| 655 | |
| 656 | total = 0; |
| 657 | for (i = 0; i < count; i++) { |
| 658 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
| 659 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 660 | user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 661 | |
| 662 | if (copy_from_user(reloc+total, user_relocs, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 663 | exec[i].relocation_count * sizeof(*reloc))) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 664 | ret = -EFAULT; |
| 665 | mutex_lock(&dev->struct_mutex); |
| 666 | goto err; |
| 667 | } |
| 668 | |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 669 | reloc_offset[i] = total; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 670 | total += exec[i].relocation_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | ret = i915_mutex_lock_interruptible(dev); |
| 674 | if (ret) { |
| 675 | mutex_lock(&dev->struct_mutex); |
| 676 | goto err; |
| 677 | } |
| 678 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 679 | /* reacquire the objects */ |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 680 | eb_reset(eb); |
| 681 | for (i = 0; i < count; i++) { |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 682 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, |
| 683 | exec[i].handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 684 | if (&obj->base == NULL) { |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 685 | DRM_ERROR("Invalid object handle %d at index %d\n", |
| 686 | exec[i].handle, i); |
| 687 | ret = -ENOENT; |
| 688 | goto err; |
| 689 | } |
| 690 | |
| 691 | list_add_tail(&obj->exec_list, objects); |
| 692 | obj->exec_handle = exec[i].handle; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 693 | obj->exec_entry = &exec[i]; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 694 | eb_add_object(eb, obj); |
| 695 | } |
| 696 | |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 697 | ret = i915_gem_execbuffer_reserve(ring, file, objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 698 | if (ret) |
| 699 | goto err; |
| 700 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 701 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 702 | int offset = obj->exec_entry - exec; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 703 | ret = i915_gem_execbuffer_relocate_object_slow(obj, eb, |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 704 | reloc + reloc_offset[offset]); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 705 | if (ret) |
| 706 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /* Leave the user relocations as are, this is the painfully slow path, |
| 710 | * and we want to avoid the complication of dropping the lock whilst |
| 711 | * having buffers reserved in the aperture and so causing spurious |
| 712 | * ENOSPC for random operations. |
| 713 | */ |
| 714 | |
| 715 | err: |
| 716 | drm_free_large(reloc); |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 717 | drm_free_large(reloc_offset); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 718 | return ret; |
| 719 | } |
| 720 | |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 721 | static int |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 722 | i915_gem_execbuffer_flush(struct drm_device *dev, |
| 723 | uint32_t invalidate_domains, |
| 724 | uint32_t flush_domains, |
| 725 | uint32_t flush_rings) |
| 726 | { |
| 727 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 728 | int i, ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 729 | |
| 730 | if (flush_domains & I915_GEM_DOMAIN_CPU) |
| 731 | intel_gtt_chipset_flush(); |
| 732 | |
Chris Wilson | 63256ec | 2011-01-04 18:42:07 +0000 | [diff] [blame] | 733 | if (flush_domains & I915_GEM_DOMAIN_GTT) |
| 734 | wmb(); |
| 735 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 736 | if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) { |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 737 | for (i = 0; i < I915_NUM_RINGS; i++) |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 738 | if (flush_rings & (1 << i)) { |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 739 | ret = i915_gem_flush_ring(&dev_priv->ring[i], |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 740 | invalidate_domains, |
| 741 | flush_domains); |
| 742 | if (ret) |
| 743 | return ret; |
| 744 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 745 | } |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 746 | |
| 747 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Eugeni Dodonov | f45b555 | 2011-12-09 17:16:37 -0800 | [diff] [blame] | 750 | static bool |
| 751 | intel_enable_semaphores(struct drm_device *dev) |
| 752 | { |
| 753 | if (INTEL_INFO(dev)->gen < 6) |
| 754 | return 0; |
| 755 | |
| 756 | if (i915_semaphores >= 0) |
| 757 | return i915_semaphores; |
| 758 | |
| 759 | /* Enable semaphores on SNB when IO remapping is off */ |
| 760 | if (INTEL_INFO(dev)->gen == 6) |
| 761 | return !intel_iommu_enabled; |
| 762 | |
| 763 | return 1; |
| 764 | } |
| 765 | |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 766 | static int |
| 767 | i915_gem_execbuffer_sync_rings(struct drm_i915_gem_object *obj, |
| 768 | struct intel_ring_buffer *to) |
| 769 | { |
| 770 | struct intel_ring_buffer *from = obj->ring; |
| 771 | u32 seqno; |
| 772 | int ret, idx; |
| 773 | |
| 774 | if (from == NULL || to == from) |
| 775 | return 0; |
| 776 | |
Chris Wilson | a1656b9 | 2011-03-04 18:48:03 +0000 | [diff] [blame] | 777 | /* XXX gpu semaphores are implicated in various hard hangs on SNB */ |
Eugeni Dodonov | f45b555 | 2011-12-09 17:16:37 -0800 | [diff] [blame] | 778 | if (!intel_enable_semaphores(obj->base.dev)) |
Chris Wilson | ce453d8 | 2011-02-21 14:43:56 +0000 | [diff] [blame] | 779 | return i915_gem_object_wait_rendering(obj); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 780 | |
| 781 | idx = intel_ring_sync_index(from, to); |
| 782 | |
| 783 | seqno = obj->last_rendering_seqno; |
| 784 | if (seqno <= from->sync_seqno[idx]) |
| 785 | return 0; |
| 786 | |
| 787 | if (seqno == from->outstanding_lazy_request) { |
| 788 | struct drm_i915_gem_request *request; |
| 789 | |
| 790 | request = kzalloc(sizeof(*request), GFP_KERNEL); |
| 791 | if (request == NULL) |
| 792 | return -ENOMEM; |
| 793 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 794 | ret = i915_add_request(from, NULL, request); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 795 | if (ret) { |
| 796 | kfree(request); |
| 797 | return ret; |
| 798 | } |
| 799 | |
| 800 | seqno = request->seqno; |
| 801 | } |
| 802 | |
| 803 | from->sync_seqno[idx] = seqno; |
Ben Widawsky | c8c99b0 | 2011-09-14 20:32:47 -0700 | [diff] [blame] | 804 | |
| 805 | return to->sync_to(to, from, seqno - 1); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 806 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 807 | |
| 808 | static int |
Chris Wilson | c59a333 | 2011-03-06 13:51:29 +0000 | [diff] [blame] | 809 | i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips) |
| 810 | { |
| 811 | u32 plane, flip_mask; |
| 812 | int ret; |
| 813 | |
| 814 | /* Check for any pending flips. As we only maintain a flip queue depth |
| 815 | * of 1, we can simply insert a WAIT for the next display flip prior |
| 816 | * to executing the batch and avoid stalling the CPU. |
| 817 | */ |
| 818 | |
| 819 | for (plane = 0; flips >> plane; plane++) { |
| 820 | if (((flips >> plane) & 1) == 0) |
| 821 | continue; |
| 822 | |
| 823 | if (plane) |
| 824 | flip_mask = MI_WAIT_FOR_PLANE_B_FLIP; |
| 825 | else |
| 826 | flip_mask = MI_WAIT_FOR_PLANE_A_FLIP; |
| 827 | |
| 828 | ret = intel_ring_begin(ring, 2); |
| 829 | if (ret) |
| 830 | return ret; |
| 831 | |
| 832 | intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask); |
| 833 | intel_ring_emit(ring, MI_NOOP); |
| 834 | intel_ring_advance(ring); |
| 835 | } |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | |
| 841 | static int |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 842 | i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring, |
| 843 | struct list_head *objects) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 844 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 845 | struct drm_i915_gem_object *obj; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 846 | struct change_domains cd; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 847 | int ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 848 | |
Chris Wilson | c59a333 | 2011-03-06 13:51:29 +0000 | [diff] [blame] | 849 | memset(&cd, 0, sizeof(cd)); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 850 | list_for_each_entry(obj, objects, exec_list) |
| 851 | i915_gem_object_set_to_gpu_domain(obj, ring, &cd); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 852 | |
| 853 | if (cd.invalidate_domains | cd.flush_domains) { |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 854 | ret = i915_gem_execbuffer_flush(ring->dev, |
| 855 | cd.invalidate_domains, |
| 856 | cd.flush_domains, |
| 857 | cd.flush_rings); |
| 858 | if (ret) |
| 859 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Chris Wilson | c59a333 | 2011-03-06 13:51:29 +0000 | [diff] [blame] | 862 | if (cd.flips) { |
| 863 | ret = i915_gem_execbuffer_wait_for_flips(ring, cd.flips); |
| 864 | if (ret) |
| 865 | return ret; |
| 866 | } |
| 867 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 868 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 869 | ret = i915_gem_execbuffer_sync_rings(obj, ring); |
| 870 | if (ret) |
| 871 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 877 | static bool |
| 878 | i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 879 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 880 | return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | static int |
| 884 | validate_exec_list(struct drm_i915_gem_exec_object2 *exec, |
| 885 | int count) |
| 886 | { |
| 887 | int i; |
| 888 | |
| 889 | for (i = 0; i < count; i++) { |
| 890 | char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr; |
| 891 | int length; /* limited by fault_in_pages_readable() */ |
| 892 | |
| 893 | /* First check for malicious input causing overflow */ |
| 894 | if (exec[i].relocation_count > |
| 895 | INT_MAX / sizeof(struct drm_i915_gem_relocation_entry)) |
| 896 | return -EINVAL; |
| 897 | |
| 898 | length = exec[i].relocation_count * |
| 899 | sizeof(struct drm_i915_gem_relocation_entry); |
| 900 | if (!access_ok(VERIFY_READ, ptr, length)) |
| 901 | return -EFAULT; |
| 902 | |
| 903 | /* we may also need to update the presumed offsets */ |
| 904 | if (!access_ok(VERIFY_WRITE, ptr, length)) |
| 905 | return -EFAULT; |
| 906 | |
| 907 | if (fault_in_pages_readable(ptr, length)) |
| 908 | return -EFAULT; |
| 909 | } |
| 910 | |
| 911 | return 0; |
| 912 | } |
| 913 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 914 | static void |
| 915 | i915_gem_execbuffer_move_to_active(struct list_head *objects, |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 916 | struct intel_ring_buffer *ring, |
| 917 | u32 seqno) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 918 | { |
| 919 | struct drm_i915_gem_object *obj; |
| 920 | |
| 921 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 922 | u32 old_read = obj->base.read_domains; |
| 923 | u32 old_write = obj->base.write_domain; |
| 924 | |
| 925 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 926 | obj->base.read_domains = obj->base.pending_read_domains; |
| 927 | obj->base.write_domain = obj->base.pending_write_domain; |
| 928 | obj->fenced_gpu_access = obj->pending_fenced_gpu_access; |
| 929 | |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 930 | i915_gem_object_move_to_active(obj, ring, seqno); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 931 | if (obj->base.write_domain) { |
| 932 | obj->dirty = 1; |
Chris Wilson | 87ca9c8 | 2010-12-02 09:42:56 +0000 | [diff] [blame] | 933 | obj->pending_gpu_write = true; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 934 | list_move_tail(&obj->gpu_write_list, |
| 935 | &ring->gpu_write_list); |
| 936 | intel_mark_busy(ring->dev, obj); |
| 937 | } |
| 938 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 939 | trace_i915_gem_object_change_domain(obj, old_read, old_write); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 943 | static void |
| 944 | i915_gem_execbuffer_retire_commands(struct drm_device *dev, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 945 | struct drm_file *file, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 946 | struct intel_ring_buffer *ring) |
| 947 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 948 | struct drm_i915_gem_request *request; |
Chris Wilson | b72f3ac | 2011-01-04 17:34:02 +0000 | [diff] [blame] | 949 | u32 invalidate; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 950 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 951 | /* |
| 952 | * Ensure that the commands in the batch buffer are |
| 953 | * finished before the interrupt fires. |
| 954 | * |
| 955 | * The sampler always gets flushed on i965 (sigh). |
| 956 | */ |
Chris Wilson | b72f3ac | 2011-01-04 17:34:02 +0000 | [diff] [blame] | 957 | invalidate = I915_GEM_DOMAIN_COMMAND; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 958 | if (INTEL_INFO(dev)->gen >= 4) |
Chris Wilson | b72f3ac | 2011-01-04 17:34:02 +0000 | [diff] [blame] | 959 | invalidate |= I915_GEM_DOMAIN_SAMPLER; |
| 960 | if (ring->flush(ring, invalidate, 0)) { |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 961 | i915_gem_next_request_seqno(ring); |
Chris Wilson | b72f3ac | 2011-01-04 17:34:02 +0000 | [diff] [blame] | 962 | return; |
| 963 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 964 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 965 | /* Add a breadcrumb for the completion of the batch buffer */ |
| 966 | request = kzalloc(sizeof(*request), GFP_KERNEL); |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 967 | if (request == NULL || i915_add_request(ring, file, request)) { |
| 968 | i915_gem_next_request_seqno(ring); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 969 | kfree(request); |
| 970 | } |
| 971 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 972 | |
| 973 | static int |
| 974 | i915_gem_do_execbuffer(struct drm_device *dev, void *data, |
| 975 | struct drm_file *file, |
| 976 | struct drm_i915_gem_execbuffer2 *args, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 977 | struct drm_i915_gem_exec_object2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 978 | { |
| 979 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 980 | struct list_head objects; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 981 | struct eb_objects *eb; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 982 | struct drm_i915_gem_object *batch_obj; |
| 983 | struct drm_clip_rect *cliprects = NULL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 984 | struct intel_ring_buffer *ring; |
Chris Wilson | c4e7a41 | 2010-11-30 14:10:25 +0000 | [diff] [blame] | 985 | u32 exec_start, exec_len; |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 986 | u32 seqno; |
Ben Widawsky | 84f9f93 | 2011-12-12 19:21:58 -0800 | [diff] [blame^] | 987 | u32 mask; |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 988 | int ret, mode, i; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 989 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 990 | if (!i915_gem_check_execbuffer(args)) { |
| 991 | DRM_ERROR("execbuf with invalid offset/length\n"); |
| 992 | return -EINVAL; |
| 993 | } |
| 994 | |
| 995 | ret = validate_exec_list(exec, args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 996 | if (ret) |
| 997 | return ret; |
| 998 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 999 | switch (args->flags & I915_EXEC_RING_MASK) { |
| 1000 | case I915_EXEC_DEFAULT: |
| 1001 | case I915_EXEC_RENDER: |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1002 | ring = &dev_priv->ring[RCS]; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1003 | break; |
| 1004 | case I915_EXEC_BSD: |
| 1005 | if (!HAS_BSD(dev)) { |
| 1006 | DRM_ERROR("execbuf with invalid ring (BSD)\n"); |
| 1007 | return -EINVAL; |
| 1008 | } |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1009 | ring = &dev_priv->ring[VCS]; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1010 | break; |
| 1011 | case I915_EXEC_BLT: |
| 1012 | if (!HAS_BLT(dev)) { |
| 1013 | DRM_ERROR("execbuf with invalid ring (BLT)\n"); |
| 1014 | return -EINVAL; |
| 1015 | } |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1016 | ring = &dev_priv->ring[BCS]; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1017 | break; |
| 1018 | default: |
| 1019 | DRM_ERROR("execbuf with unknown ring: %d\n", |
| 1020 | (int)(args->flags & I915_EXEC_RING_MASK)); |
| 1021 | return -EINVAL; |
| 1022 | } |
| 1023 | |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 1024 | mode = args->flags & I915_EXEC_CONSTANTS_MASK; |
Ben Widawsky | 84f9f93 | 2011-12-12 19:21:58 -0800 | [diff] [blame^] | 1025 | mask = I915_EXEC_CONSTANTS_MASK; |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 1026 | switch (mode) { |
| 1027 | case I915_EXEC_CONSTANTS_REL_GENERAL: |
| 1028 | case I915_EXEC_CONSTANTS_ABSOLUTE: |
| 1029 | case I915_EXEC_CONSTANTS_REL_SURFACE: |
| 1030 | if (ring == &dev_priv->ring[RCS] && |
| 1031 | mode != dev_priv->relative_constants_mode) { |
| 1032 | if (INTEL_INFO(dev)->gen < 4) |
| 1033 | return -EINVAL; |
| 1034 | |
| 1035 | if (INTEL_INFO(dev)->gen > 5 && |
| 1036 | mode == I915_EXEC_CONSTANTS_REL_SURFACE) |
| 1037 | return -EINVAL; |
Ben Widawsky | 84f9f93 | 2011-12-12 19:21:58 -0800 | [diff] [blame^] | 1038 | |
| 1039 | /* The HW changed the meaning on this bit on gen6 */ |
| 1040 | if (INTEL_INFO(dev)->gen >= 6) |
| 1041 | mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE; |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 1042 | } |
| 1043 | break; |
| 1044 | default: |
| 1045 | DRM_ERROR("execbuf with unknown constants: %d\n", mode); |
| 1046 | return -EINVAL; |
| 1047 | } |
| 1048 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1049 | if (args->buffer_count < 1) { |
| 1050 | DRM_ERROR("execbuf with %d buffers\n", args->buffer_count); |
| 1051 | return -EINVAL; |
| 1052 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1053 | |
| 1054 | if (args->num_cliprects != 0) { |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1055 | if (ring != &dev_priv->ring[RCS]) { |
Chris Wilson | c4e7a41 | 2010-11-30 14:10:25 +0000 | [diff] [blame] | 1056 | DRM_ERROR("clip rectangles are only valid with the render ring\n"); |
| 1057 | return -EINVAL; |
| 1058 | } |
| 1059 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1060 | cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects), |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1061 | GFP_KERNEL); |
| 1062 | if (cliprects == NULL) { |
| 1063 | ret = -ENOMEM; |
| 1064 | goto pre_mutex_err; |
| 1065 | } |
| 1066 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1067 | if (copy_from_user(cliprects, |
| 1068 | (struct drm_clip_rect __user *)(uintptr_t) |
| 1069 | args->cliprects_ptr, |
| 1070 | sizeof(*cliprects)*args->num_cliprects)) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1071 | ret = -EFAULT; |
| 1072 | goto pre_mutex_err; |
| 1073 | } |
| 1074 | } |
| 1075 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1076 | ret = i915_mutex_lock_interruptible(dev); |
| 1077 | if (ret) |
| 1078 | goto pre_mutex_err; |
| 1079 | |
| 1080 | if (dev_priv->mm.suspended) { |
| 1081 | mutex_unlock(&dev->struct_mutex); |
| 1082 | ret = -EBUSY; |
| 1083 | goto pre_mutex_err; |
| 1084 | } |
| 1085 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1086 | eb = eb_create(args->buffer_count); |
| 1087 | if (eb == NULL) { |
| 1088 | mutex_unlock(&dev->struct_mutex); |
| 1089 | ret = -ENOMEM; |
| 1090 | goto pre_mutex_err; |
| 1091 | } |
| 1092 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1093 | /* Look up object handles */ |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1094 | INIT_LIST_HEAD(&objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1095 | for (i = 0; i < args->buffer_count; i++) { |
| 1096 | struct drm_i915_gem_object *obj; |
| 1097 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1098 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, |
| 1099 | exec[i].handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 1100 | if (&obj->base == NULL) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1101 | DRM_ERROR("Invalid object handle %d at index %d\n", |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1102 | exec[i].handle, i); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1103 | /* prevent error path from reading uninitialized data */ |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1104 | ret = -ENOENT; |
| 1105 | goto err; |
| 1106 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1107 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1108 | if (!list_empty(&obj->exec_list)) { |
| 1109 | DRM_ERROR("Object %p [handle %d, index %d] appears more than once in object list\n", |
| 1110 | obj, exec[i].handle, i); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1111 | ret = -EINVAL; |
| 1112 | goto err; |
| 1113 | } |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1114 | |
| 1115 | list_add_tail(&obj->exec_list, &objects); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1116 | obj->exec_handle = exec[i].handle; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 1117 | obj->exec_entry = &exec[i]; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1118 | eb_add_object(eb, obj); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 1121 | /* take note of the batch buffer before we might reorder the lists */ |
| 1122 | batch_obj = list_entry(objects.prev, |
| 1123 | struct drm_i915_gem_object, |
| 1124 | exec_list); |
| 1125 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1126 | /* Move the objects en-masse into the GTT, evicting if necessary. */ |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 1127 | ret = i915_gem_execbuffer_reserve(ring, file, &objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1128 | if (ret) |
| 1129 | goto err; |
| 1130 | |
| 1131 | /* The objects are in their final locations, apply the relocations. */ |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 1132 | ret = i915_gem_execbuffer_relocate(dev, eb, &objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1133 | if (ret) { |
| 1134 | if (ret == -EFAULT) { |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 1135 | ret = i915_gem_execbuffer_relocate_slow(dev, file, ring, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1136 | &objects, eb, |
| 1137 | exec, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1138 | args->buffer_count); |
| 1139 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 1140 | } |
| 1141 | if (ret) |
| 1142 | goto err; |
| 1143 | } |
| 1144 | |
| 1145 | /* Set the pending read domains for the batch buffer to COMMAND */ |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1146 | if (batch_obj->base.pending_write_domain) { |
| 1147 | DRM_ERROR("Attempting to use self-modifying batch buffer\n"); |
| 1148 | ret = -EINVAL; |
| 1149 | goto err; |
| 1150 | } |
| 1151 | batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND; |
| 1152 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1153 | ret = i915_gem_execbuffer_move_to_gpu(ring, &objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1154 | if (ret) |
| 1155 | goto err; |
| 1156 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1157 | seqno = i915_gem_next_request_seqno(ring); |
Chris Wilson | 076e2c0 | 2011-01-21 10:07:18 +0000 | [diff] [blame] | 1158 | for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) { |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1159 | if (seqno < ring->sync_seqno[i]) { |
| 1160 | /* The GPU can not handle its semaphore value wrapping, |
| 1161 | * so every billion or so execbuffers, we need to stall |
| 1162 | * the GPU in order to reset the counters. |
| 1163 | */ |
| 1164 | ret = i915_gpu_idle(dev); |
| 1165 | if (ret) |
| 1166 | goto err; |
| 1167 | |
| 1168 | BUG_ON(ring->sync_seqno[i]); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Ben Widawsky | e2971bd | 2011-12-12 19:21:57 -0800 | [diff] [blame] | 1172 | if (ring == &dev_priv->ring[RCS] && |
| 1173 | mode != dev_priv->relative_constants_mode) { |
| 1174 | ret = intel_ring_begin(ring, 4); |
| 1175 | if (ret) |
| 1176 | goto err; |
| 1177 | |
| 1178 | intel_ring_emit(ring, MI_NOOP); |
| 1179 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 1180 | intel_ring_emit(ring, INSTPM); |
Ben Widawsky | 84f9f93 | 2011-12-12 19:21:58 -0800 | [diff] [blame^] | 1181 | intel_ring_emit(ring, mask << 16 | mode); |
Ben Widawsky | e2971bd | 2011-12-12 19:21:57 -0800 | [diff] [blame] | 1182 | intel_ring_advance(ring); |
| 1183 | |
| 1184 | dev_priv->relative_constants_mode = mode; |
| 1185 | } |
| 1186 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1187 | trace_i915_gem_ring_dispatch(ring, seqno); |
| 1188 | |
Chris Wilson | c4e7a41 | 2010-11-30 14:10:25 +0000 | [diff] [blame] | 1189 | exec_start = batch_obj->gtt_offset + args->batch_start_offset; |
| 1190 | exec_len = args->batch_len; |
| 1191 | if (cliprects) { |
| 1192 | for (i = 0; i < args->num_cliprects; i++) { |
| 1193 | ret = i915_emit_box(dev, &cliprects[i], |
| 1194 | args->DR1, args->DR4); |
| 1195 | if (ret) |
| 1196 | goto err; |
| 1197 | |
| 1198 | ret = ring->dispatch_execbuffer(ring, |
| 1199 | exec_start, exec_len); |
| 1200 | if (ret) |
| 1201 | goto err; |
| 1202 | } |
| 1203 | } else { |
| 1204 | ret = ring->dispatch_execbuffer(ring, exec_start, exec_len); |
| 1205 | if (ret) |
| 1206 | goto err; |
| 1207 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1208 | |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1209 | i915_gem_execbuffer_move_to_active(&objects, ring, seqno); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1210 | i915_gem_execbuffer_retire_commands(dev, file, ring); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1211 | |
| 1212 | err: |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1213 | eb_destroy(eb); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1214 | while (!list_empty(&objects)) { |
| 1215 | struct drm_i915_gem_object *obj; |
| 1216 | |
| 1217 | obj = list_first_entry(&objects, |
| 1218 | struct drm_i915_gem_object, |
| 1219 | exec_list); |
| 1220 | list_del_init(&obj->exec_list); |
| 1221 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | mutex_unlock(&dev->struct_mutex); |
| 1225 | |
| 1226 | pre_mutex_err: |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1227 | kfree(cliprects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1228 | return ret; |
| 1229 | } |
| 1230 | |
| 1231 | /* |
| 1232 | * Legacy execbuffer just creates an exec2 list from the original exec object |
| 1233 | * list array and passes it to the real function. |
| 1234 | */ |
| 1235 | int |
| 1236 | i915_gem_execbuffer(struct drm_device *dev, void *data, |
| 1237 | struct drm_file *file) |
| 1238 | { |
| 1239 | struct drm_i915_gem_execbuffer *args = data; |
| 1240 | struct drm_i915_gem_execbuffer2 exec2; |
| 1241 | struct drm_i915_gem_exec_object *exec_list = NULL; |
| 1242 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 1243 | int ret, i; |
| 1244 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1245 | if (args->buffer_count < 1) { |
| 1246 | DRM_ERROR("execbuf with %d buffers\n", args->buffer_count); |
| 1247 | return -EINVAL; |
| 1248 | } |
| 1249 | |
| 1250 | /* Copy in the exec list from userland */ |
| 1251 | exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count); |
| 1252 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count); |
| 1253 | if (exec_list == NULL || exec2_list == NULL) { |
| 1254 | DRM_ERROR("Failed to allocate exec list for %d buffers\n", |
| 1255 | args->buffer_count); |
| 1256 | drm_free_large(exec_list); |
| 1257 | drm_free_large(exec2_list); |
| 1258 | return -ENOMEM; |
| 1259 | } |
| 1260 | ret = copy_from_user(exec_list, |
| 1261 | (struct drm_i915_relocation_entry __user *) |
| 1262 | (uintptr_t) args->buffers_ptr, |
| 1263 | sizeof(*exec_list) * args->buffer_count); |
| 1264 | if (ret != 0) { |
| 1265 | DRM_ERROR("copy %d exec entries failed %d\n", |
| 1266 | args->buffer_count, ret); |
| 1267 | drm_free_large(exec_list); |
| 1268 | drm_free_large(exec2_list); |
| 1269 | return -EFAULT; |
| 1270 | } |
| 1271 | |
| 1272 | for (i = 0; i < args->buffer_count; i++) { |
| 1273 | exec2_list[i].handle = exec_list[i].handle; |
| 1274 | exec2_list[i].relocation_count = exec_list[i].relocation_count; |
| 1275 | exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; |
| 1276 | exec2_list[i].alignment = exec_list[i].alignment; |
| 1277 | exec2_list[i].offset = exec_list[i].offset; |
| 1278 | if (INTEL_INFO(dev)->gen < 4) |
| 1279 | exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; |
| 1280 | else |
| 1281 | exec2_list[i].flags = 0; |
| 1282 | } |
| 1283 | |
| 1284 | exec2.buffers_ptr = args->buffers_ptr; |
| 1285 | exec2.buffer_count = args->buffer_count; |
| 1286 | exec2.batch_start_offset = args->batch_start_offset; |
| 1287 | exec2.batch_len = args->batch_len; |
| 1288 | exec2.DR1 = args->DR1; |
| 1289 | exec2.DR4 = args->DR4; |
| 1290 | exec2.num_cliprects = args->num_cliprects; |
| 1291 | exec2.cliprects_ptr = args->cliprects_ptr; |
| 1292 | exec2.flags = I915_EXEC_RENDER; |
| 1293 | |
| 1294 | ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list); |
| 1295 | if (!ret) { |
| 1296 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 1297 | for (i = 0; i < args->buffer_count; i++) |
| 1298 | exec_list[i].offset = exec2_list[i].offset; |
| 1299 | /* ... and back out to userspace */ |
| 1300 | ret = copy_to_user((struct drm_i915_relocation_entry __user *) |
| 1301 | (uintptr_t) args->buffers_ptr, |
| 1302 | exec_list, |
| 1303 | sizeof(*exec_list) * args->buffer_count); |
| 1304 | if (ret) { |
| 1305 | ret = -EFAULT; |
| 1306 | DRM_ERROR("failed to copy %d exec entries " |
| 1307 | "back to user (%d)\n", |
| 1308 | args->buffer_count, ret); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | drm_free_large(exec_list); |
| 1313 | drm_free_large(exec2_list); |
| 1314 | return ret; |
| 1315 | } |
| 1316 | |
| 1317 | int |
| 1318 | i915_gem_execbuffer2(struct drm_device *dev, void *data, |
| 1319 | struct drm_file *file) |
| 1320 | { |
| 1321 | struct drm_i915_gem_execbuffer2 *args = data; |
| 1322 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 1323 | int ret; |
| 1324 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1325 | if (args->buffer_count < 1) { |
| 1326 | DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count); |
| 1327 | return -EINVAL; |
| 1328 | } |
| 1329 | |
Chris Wilson | 8408c28 | 2011-02-21 12:54:48 +0000 | [diff] [blame] | 1330 | exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count, |
| 1331 | GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); |
| 1332 | if (exec2_list == NULL) |
| 1333 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), |
| 1334 | args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1335 | if (exec2_list == NULL) { |
| 1336 | DRM_ERROR("Failed to allocate exec list for %d buffers\n", |
| 1337 | args->buffer_count); |
| 1338 | return -ENOMEM; |
| 1339 | } |
| 1340 | ret = copy_from_user(exec2_list, |
| 1341 | (struct drm_i915_relocation_entry __user *) |
| 1342 | (uintptr_t) args->buffers_ptr, |
| 1343 | sizeof(*exec2_list) * args->buffer_count); |
| 1344 | if (ret != 0) { |
| 1345 | DRM_ERROR("copy %d exec entries failed %d\n", |
| 1346 | args->buffer_count, ret); |
| 1347 | drm_free_large(exec2_list); |
| 1348 | return -EFAULT; |
| 1349 | } |
| 1350 | |
| 1351 | ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list); |
| 1352 | if (!ret) { |
| 1353 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 1354 | ret = copy_to_user((struct drm_i915_relocation_entry __user *) |
| 1355 | (uintptr_t) args->buffers_ptr, |
| 1356 | exec2_list, |
| 1357 | sizeof(*exec2_list) * args->buffer_count); |
| 1358 | if (ret) { |
| 1359 | ret = -EFAULT; |
| 1360 | DRM_ERROR("failed to copy %d exec entries " |
| 1361 | "back to user (%d)\n", |
| 1362 | args->buffer_count, ret); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | drm_free_large(exec2_list); |
| 1367 | return ret; |
| 1368 | } |