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