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) |
Daniel Vetter | 96154f2 | 2011-12-14 13:57:00 +0100 | [diff] [blame] | 206 | cd->flush_rings |= intel_ring_flag(obj->ring); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 207 | if (invalidate_domains & I915_GEM_GPU_DOMAINS) |
Daniel Vetter | 96154f2 | 2011-12-14 13:57:00 +0100 | [diff] [blame] | 208 | cd->flush_rings |= intel_ring_flag(ring); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 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 | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 269 | static inline int use_cpu_reloc(struct drm_i915_gem_object *obj) |
| 270 | { |
| 271 | return (obj->base.write_domain == I915_GEM_DOMAIN_CPU || |
| 272 | obj->cache_level != I915_CACHE_NONE); |
| 273 | } |
| 274 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 275 | static int |
| 276 | i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 277 | struct eb_objects *eb, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 278 | struct drm_i915_gem_relocation_entry *reloc) |
| 279 | { |
| 280 | struct drm_device *dev = obj->base.dev; |
| 281 | struct drm_gem_object *target_obj; |
Daniel Vetter | 149c840 | 2012-02-15 23:50:23 +0100 | [diff] [blame] | 282 | struct drm_i915_gem_object *target_i915_obj; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 283 | uint32_t target_offset; |
| 284 | int ret = -EINVAL; |
| 285 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 286 | /* we've already hold a reference to all valid objects */ |
| 287 | target_obj = &eb_get_object(eb, reloc->target_handle)->base; |
| 288 | if (unlikely(target_obj == NULL)) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 289 | return -ENOENT; |
| 290 | |
Daniel Vetter | 149c840 | 2012-02-15 23:50:23 +0100 | [diff] [blame] | 291 | target_i915_obj = to_intel_bo(target_obj); |
| 292 | target_offset = target_i915_obj->gtt_offset; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 293 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 294 | /* The target buffer should have appeared before us in the |
| 295 | * exec_object list, so it should have a GTT space bound by now. |
| 296 | */ |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 297 | if (unlikely(target_offset == 0)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 298 | DRM_DEBUG("No GTT space found for object %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 299 | reloc->target_handle); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 300 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Validate that the target is in a valid r/w GPU domain */ |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 304 | if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 305 | DRM_DEBUG("reloc with multiple write domains: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 306 | "obj %p target %d offset %d " |
| 307 | "read %08x write %08x", |
| 308 | obj, reloc->target_handle, |
| 309 | (int) reloc->offset, |
| 310 | reloc->read_domains, |
| 311 | reloc->write_domain); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 312 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 313 | } |
Daniel Vetter | 4ca4a25 | 2011-12-14 13:57:27 +0100 | [diff] [blame] | 314 | if (unlikely((reloc->write_domain | reloc->read_domains) |
| 315 | & ~I915_GEM_GPU_DOMAINS)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 316 | DRM_DEBUG("reloc with read/write non-GPU domains: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 317 | "obj %p target %d offset %d " |
| 318 | "read %08x write %08x", |
| 319 | obj, reloc->target_handle, |
| 320 | (int) reloc->offset, |
| 321 | reloc->read_domains, |
| 322 | reloc->write_domain); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 323 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 324 | } |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 325 | if (unlikely(reloc->write_domain && target_obj->pending_write_domain && |
| 326 | reloc->write_domain != target_obj->pending_write_domain)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 327 | DRM_DEBUG("Write domain conflict: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 328 | "obj %p target %d offset %d " |
| 329 | "new %08x old %08x\n", |
| 330 | obj, reloc->target_handle, |
| 331 | (int) reloc->offset, |
| 332 | reloc->write_domain, |
| 333 | target_obj->pending_write_domain); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 334 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | target_obj->pending_read_domains |= reloc->read_domains; |
| 338 | target_obj->pending_write_domain |= reloc->write_domain; |
| 339 | |
| 340 | /* If the relocation already has the right value in it, no |
| 341 | * more work needs to be done. |
| 342 | */ |
| 343 | if (target_offset == reloc->presumed_offset) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 344 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 345 | |
| 346 | /* Check that the relocation address is valid... */ |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 347 | if (unlikely(reloc->offset > obj->base.size - 4)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 348 | DRM_DEBUG("Relocation beyond object bounds: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 349 | "obj %p target %d offset %d size %d.\n", |
| 350 | obj, reloc->target_handle, |
| 351 | (int) reloc->offset, |
| 352 | (int) obj->base.size); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 353 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 354 | } |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 355 | if (unlikely(reloc->offset & 3)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 356 | DRM_DEBUG("Relocation not 4-byte aligned: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 357 | "obj %p target %d offset %d.\n", |
| 358 | obj, reloc->target_handle, |
| 359 | (int) reloc->offset); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 360 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 363 | /* We can't wait for rendering with pagefaults disabled */ |
| 364 | if (obj->active && in_atomic()) |
| 365 | return -EFAULT; |
| 366 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 367 | reloc->delta += target_offset; |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 368 | if (use_cpu_reloc(obj)) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 369 | uint32_t page_offset = reloc->offset & ~PAGE_MASK; |
| 370 | char *vaddr; |
| 371 | |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 372 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
| 373 | if (ret) |
| 374 | return ret; |
| 375 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 376 | vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]); |
| 377 | *(uint32_t *)(vaddr + page_offset) = reloc->delta; |
| 378 | kunmap_atomic(vaddr); |
| 379 | } else { |
| 380 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 381 | uint32_t __iomem *reloc_entry; |
| 382 | void __iomem *reloc_page; |
| 383 | |
| 384 | ret = i915_gem_object_set_to_gtt_domain(obj, 1); |
| 385 | if (ret) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 386 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 387 | |
| 388 | /* Map the page containing the relocation we're going to perform. */ |
| 389 | reloc->offset += obj->gtt_offset; |
| 390 | reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping, |
| 391 | reloc->offset & PAGE_MASK); |
| 392 | reloc_entry = (uint32_t __iomem *) |
| 393 | (reloc_page + (reloc->offset & ~PAGE_MASK)); |
| 394 | iowrite32(reloc->delta, reloc_entry); |
| 395 | io_mapping_unmap_atomic(reloc_page); |
| 396 | } |
| 397 | |
Daniel Vetter | 149c840 | 2012-02-15 23:50:23 +0100 | [diff] [blame] | 398 | /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and |
| 399 | * pipe_control writes because the gpu doesn't properly redirect them |
| 400 | * through the ppgtt for non_secure batchbuffers. */ |
| 401 | if (unlikely(IS_GEN6(dev) && |
| 402 | reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION && |
| 403 | !target_i915_obj->has_global_gtt_mapping)) { |
| 404 | i915_gem_gtt_bind_object(target_i915_obj, |
| 405 | target_i915_obj->cache_level); |
| 406 | } |
| 407 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 408 | /* and update the user's relocation entry */ |
| 409 | reloc->presumed_offset = target_offset; |
| 410 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 411 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static int |
| 415 | i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj, |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 416 | struct eb_objects *eb) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 417 | { |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 418 | #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry)) |
| 419 | struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)]; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 420 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 421 | struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 422 | int remain, ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 423 | |
| 424 | user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 425 | |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 426 | remain = entry->relocation_count; |
| 427 | while (remain) { |
| 428 | struct drm_i915_gem_relocation_entry *r = stack_reloc; |
| 429 | int count = remain; |
| 430 | if (count > ARRAY_SIZE(stack_reloc)) |
| 431 | count = ARRAY_SIZE(stack_reloc); |
| 432 | remain -= count; |
| 433 | |
| 434 | if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0]))) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 435 | return -EFAULT; |
| 436 | |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 437 | do { |
| 438 | u64 offset = r->presumed_offset; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 439 | |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 440 | ret = i915_gem_execbuffer_relocate_entry(obj, eb, r); |
| 441 | if (ret) |
| 442 | return ret; |
| 443 | |
| 444 | if (r->presumed_offset != offset && |
| 445 | __copy_to_user_inatomic(&user_relocs->presumed_offset, |
| 446 | &r->presumed_offset, |
| 447 | sizeof(r->presumed_offset))) { |
| 448 | return -EFAULT; |
| 449 | } |
| 450 | |
| 451 | user_relocs++; |
| 452 | r++; |
| 453 | } while (--count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | return 0; |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 457 | #undef N_RELOC |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static int |
| 461 | i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 462 | struct eb_objects *eb, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 463 | struct drm_i915_gem_relocation_entry *relocs) |
| 464 | { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 465 | const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 466 | int i, ret; |
| 467 | |
| 468 | for (i = 0; i < entry->relocation_count; i++) { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 469 | ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 470 | if (ret) |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static int |
| 478 | i915_gem_execbuffer_relocate(struct drm_device *dev, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 479 | struct eb_objects *eb, |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 480 | struct list_head *objects) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 481 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 482 | struct drm_i915_gem_object *obj; |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 483 | int ret = 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 484 | |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 485 | /* This is the fast path and we cannot handle a pagefault whilst |
| 486 | * holding the struct mutex lest the user pass in the relocations |
| 487 | * contained within a mmaped bo. For in such a case we, the page |
| 488 | * fault handler would call i915_gem_fault() and we would try to |
| 489 | * acquire the struct mutex again. Obviously this is bad and so |
| 490 | * lockdep complains vehemently. |
| 491 | */ |
| 492 | pagefault_disable(); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 493 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 494 | ret = i915_gem_execbuffer_relocate_object(obj, eb); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 495 | if (ret) |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 496 | break; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 497 | } |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 498 | pagefault_enable(); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 499 | |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 500 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 503 | #define __EXEC_OBJECT_HAS_FENCE (1<<31) |
| 504 | |
| 505 | static int |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 506 | need_reloc_mappable(struct drm_i915_gem_object *obj) |
| 507 | { |
| 508 | struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
| 509 | return entry->relocation_count && !use_cpu_reloc(obj); |
| 510 | } |
| 511 | |
| 512 | static int |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 513 | pin_and_fence_object(struct drm_i915_gem_object *obj, |
| 514 | struct intel_ring_buffer *ring) |
| 515 | { |
| 516 | struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
| 517 | bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4; |
| 518 | bool need_fence, need_mappable; |
| 519 | int ret; |
| 520 | |
| 521 | need_fence = |
| 522 | has_fenced_gpu_access && |
| 523 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 524 | obj->tiling_mode != I915_TILING_NONE; |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 525 | need_mappable = need_fence || need_reloc_mappable(obj); |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 526 | |
| 527 | ret = i915_gem_object_pin(obj, entry->alignment, need_mappable); |
| 528 | if (ret) |
| 529 | return ret; |
| 530 | |
| 531 | if (has_fenced_gpu_access) { |
| 532 | if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) { |
Chris Wilson | 9a5a53b | 2012-03-22 15:10:00 +0000 | [diff] [blame] | 533 | ret = i915_gem_object_get_fence(obj, ring); |
| 534 | if (ret) |
| 535 | goto err_unpin; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 536 | |
Chris Wilson | 9a5a53b | 2012-03-22 15:10:00 +0000 | [diff] [blame] | 537 | if (i915_gem_object_pin_fence(obj)) |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 538 | entry->flags |= __EXEC_OBJECT_HAS_FENCE; |
Chris Wilson | 9a5a53b | 2012-03-22 15:10:00 +0000 | [diff] [blame] | 539 | |
Chris Wilson | 7dd4906 | 2012-03-21 10:48:18 +0000 | [diff] [blame] | 540 | obj->pending_fenced_gpu_access = true; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 541 | } |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | entry->offset = obj->gtt_offset; |
| 545 | return 0; |
| 546 | |
| 547 | err_unpin: |
| 548 | i915_gem_object_unpin(obj); |
| 549 | return ret; |
| 550 | } |
| 551 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 552 | static int |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 553 | i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 554 | struct drm_file *file, |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 555 | struct list_head *objects) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 556 | { |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 557 | drm_i915_private_t *dev_priv = ring->dev->dev_private; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 558 | struct drm_i915_gem_object *obj; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 559 | int ret, retry; |
Chris Wilson | 9b3826b | 2010-12-05 17:11:54 +0000 | [diff] [blame] | 560 | bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 561 | struct list_head ordered_objects; |
| 562 | |
| 563 | INIT_LIST_HEAD(&ordered_objects); |
| 564 | while (!list_empty(objects)) { |
| 565 | struct drm_i915_gem_exec_object2 *entry; |
| 566 | bool need_fence, need_mappable; |
| 567 | |
| 568 | obj = list_first_entry(objects, |
| 569 | struct drm_i915_gem_object, |
| 570 | exec_list); |
| 571 | entry = obj->exec_entry; |
| 572 | |
| 573 | need_fence = |
| 574 | has_fenced_gpu_access && |
| 575 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 576 | obj->tiling_mode != I915_TILING_NONE; |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 577 | need_mappable = need_fence || need_reloc_mappable(obj); |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 578 | |
| 579 | if (need_mappable) |
| 580 | list_move(&obj->exec_list, &ordered_objects); |
| 581 | else |
| 582 | list_move_tail(&obj->exec_list, &ordered_objects); |
Chris Wilson | 595dad7 | 2011-01-13 11:03:48 +0000 | [diff] [blame] | 583 | |
| 584 | obj->base.pending_read_domains = 0; |
| 585 | obj->base.pending_write_domain = 0; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 586 | } |
| 587 | list_splice(&ordered_objects, objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 588 | |
| 589 | /* Attempt to pin all of the buffers into the GTT. |
| 590 | * This is done in 3 phases: |
| 591 | * |
| 592 | * 1a. Unbind all objects that do not match the GTT constraints for |
| 593 | * the execbuffer (fenceable, mappable, alignment etc). |
| 594 | * 1b. Increment pin count for already bound objects. |
| 595 | * 2. Bind new objects. |
| 596 | * 3. Decrement pin count. |
| 597 | * |
| 598 | * This avoid unnecessary unbinding of later objects in order to makr |
| 599 | * room for the earlier objects *unless* we need to defragment. |
| 600 | */ |
| 601 | retry = 0; |
| 602 | do { |
| 603 | ret = 0; |
| 604 | |
| 605 | /* Unbind any ill-fitting objects or pin. */ |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 606 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 607 | struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 608 | bool need_fence, need_mappable; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 609 | |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 610 | if (!obj->gtt_space) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 611 | continue; |
| 612 | |
| 613 | need_fence = |
Chris Wilson | 9b3826b | 2010-12-05 17:11:54 +0000 | [diff] [blame] | 614 | has_fenced_gpu_access && |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 615 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 616 | obj->tiling_mode != I915_TILING_NONE; |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 617 | need_mappable = need_fence || need_reloc_mappable(obj); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 618 | |
| 619 | if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) || |
| 620 | (need_mappable && !obj->map_and_fenceable)) |
| 621 | ret = i915_gem_object_unbind(obj); |
| 622 | else |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 623 | ret = pin_and_fence_object(obj, ring); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 624 | if (ret) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 625 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /* Bind fresh objects */ |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 629 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 630 | if (obj->gtt_space) |
| 631 | continue; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 632 | |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 633 | ret = pin_and_fence_object(obj, ring); |
| 634 | if (ret) { |
| 635 | int ret_ignore; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 636 | |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 637 | /* This can potentially raise a harmless |
| 638 | * -EINVAL if we failed to bind in the above |
| 639 | * call. It cannot raise -EINTR since we know |
| 640 | * that the bo is freshly bound and so will |
| 641 | * not need to be flushed or waited upon. |
| 642 | */ |
| 643 | ret_ignore = i915_gem_object_unbind(obj); |
| 644 | (void)ret_ignore; |
| 645 | WARN_ON(obj->gtt_space); |
| 646 | break; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 647 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 650 | /* Decrement pin count for bound objects */ |
| 651 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 652 | struct drm_i915_gem_exec_object2 *entry; |
| 653 | |
| 654 | if (!obj->gtt_space) |
| 655 | continue; |
| 656 | |
| 657 | entry = obj->exec_entry; |
| 658 | if (entry->flags & __EXEC_OBJECT_HAS_FENCE) { |
| 659 | i915_gem_object_unpin_fence(obj); |
| 660 | entry->flags &= ~__EXEC_OBJECT_HAS_FENCE; |
| 661 | } |
| 662 | |
| 663 | i915_gem_object_unpin(obj); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 664 | |
| 665 | /* ... and ensure ppgtt mapping exist if needed. */ |
| 666 | if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) { |
| 667 | i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt, |
| 668 | obj, obj->cache_level); |
| 669 | |
| 670 | obj->has_aliasing_ppgtt_mapping = 1; |
| 671 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | if (ret != -ENOSPC || retry > 1) |
| 675 | return ret; |
| 676 | |
| 677 | /* First attempt, just clear anything that is purgeable. |
| 678 | * Second attempt, clear the entire GTT. |
| 679 | */ |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 680 | ret = i915_gem_evict_everything(ring->dev, retry == 0); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 681 | if (ret) |
| 682 | return ret; |
| 683 | |
| 684 | retry++; |
| 685 | } while (1); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 686 | |
| 687 | err: |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 688 | list_for_each_entry_continue_reverse(obj, objects, exec_list) { |
| 689 | struct drm_i915_gem_exec_object2 *entry; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 690 | |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 691 | if (!obj->gtt_space) |
| 692 | continue; |
| 693 | |
| 694 | entry = obj->exec_entry; |
| 695 | if (entry->flags & __EXEC_OBJECT_HAS_FENCE) { |
| 696 | i915_gem_object_unpin_fence(obj); |
| 697 | entry->flags &= ~__EXEC_OBJECT_HAS_FENCE; |
| 698 | } |
| 699 | |
| 700 | i915_gem_object_unpin(obj); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static int |
| 707 | i915_gem_execbuffer_relocate_slow(struct drm_device *dev, |
| 708 | struct drm_file *file, |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 709 | struct intel_ring_buffer *ring, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 710 | struct list_head *objects, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 711 | struct eb_objects *eb, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 712 | struct drm_i915_gem_exec_object2 *exec, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 713 | int count) |
| 714 | { |
| 715 | struct drm_i915_gem_relocation_entry *reloc; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 716 | struct drm_i915_gem_object *obj; |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 717 | int *reloc_offset; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 718 | int i, total, ret; |
| 719 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 720 | /* We may process another execbuffer during the unlock... */ |
Chris Wilson | 36cf174 | 2011-01-10 12:09:12 +0000 | [diff] [blame] | 721 | while (!list_empty(objects)) { |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 722 | obj = list_first_entry(objects, |
| 723 | struct drm_i915_gem_object, |
| 724 | exec_list); |
| 725 | list_del_init(&obj->exec_list); |
| 726 | drm_gem_object_unreference(&obj->base); |
| 727 | } |
| 728 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 729 | mutex_unlock(&dev->struct_mutex); |
| 730 | |
| 731 | total = 0; |
| 732 | for (i = 0; i < count; i++) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 733 | total += exec[i].relocation_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 734 | |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 735 | reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset)); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 736 | reloc = drm_malloc_ab(total, sizeof(*reloc)); |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 737 | if (reloc == NULL || reloc_offset == NULL) { |
| 738 | drm_free_large(reloc); |
| 739 | drm_free_large(reloc_offset); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 740 | mutex_lock(&dev->struct_mutex); |
| 741 | return -ENOMEM; |
| 742 | } |
| 743 | |
| 744 | total = 0; |
| 745 | for (i = 0; i < count; i++) { |
| 746 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
| 747 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 748 | user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 749 | |
| 750 | if (copy_from_user(reloc+total, user_relocs, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 751 | exec[i].relocation_count * sizeof(*reloc))) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 752 | ret = -EFAULT; |
| 753 | mutex_lock(&dev->struct_mutex); |
| 754 | goto err; |
| 755 | } |
| 756 | |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 757 | reloc_offset[i] = total; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 758 | total += exec[i].relocation_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | ret = i915_mutex_lock_interruptible(dev); |
| 762 | if (ret) { |
| 763 | mutex_lock(&dev->struct_mutex); |
| 764 | goto err; |
| 765 | } |
| 766 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 767 | /* reacquire the objects */ |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 768 | eb_reset(eb); |
| 769 | for (i = 0; i < count; i++) { |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 770 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, |
| 771 | exec[i].handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 772 | if (&obj->base == NULL) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 773 | DRM_DEBUG("Invalid object handle %d at index %d\n", |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 774 | exec[i].handle, i); |
| 775 | ret = -ENOENT; |
| 776 | goto err; |
| 777 | } |
| 778 | |
| 779 | list_add_tail(&obj->exec_list, objects); |
| 780 | obj->exec_handle = exec[i].handle; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 781 | obj->exec_entry = &exec[i]; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 782 | eb_add_object(eb, obj); |
| 783 | } |
| 784 | |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 785 | ret = i915_gem_execbuffer_reserve(ring, file, objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 786 | if (ret) |
| 787 | goto err; |
| 788 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 789 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 790 | int offset = obj->exec_entry - exec; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 791 | ret = i915_gem_execbuffer_relocate_object_slow(obj, eb, |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 792 | reloc + reloc_offset[offset]); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 793 | if (ret) |
| 794 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /* Leave the user relocations as are, this is the painfully slow path, |
| 798 | * and we want to avoid the complication of dropping the lock whilst |
| 799 | * having buffers reserved in the aperture and so causing spurious |
| 800 | * ENOSPC for random operations. |
| 801 | */ |
| 802 | |
| 803 | err: |
| 804 | drm_free_large(reloc); |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 805 | drm_free_large(reloc_offset); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 806 | return ret; |
| 807 | } |
| 808 | |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 809 | static int |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 810 | i915_gem_execbuffer_flush(struct drm_device *dev, |
| 811 | uint32_t invalidate_domains, |
| 812 | uint32_t flush_domains, |
| 813 | uint32_t flush_rings) |
| 814 | { |
| 815 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 816 | int i, ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 817 | |
| 818 | if (flush_domains & I915_GEM_DOMAIN_CPU) |
| 819 | intel_gtt_chipset_flush(); |
| 820 | |
Chris Wilson | 63256ec | 2011-01-04 18:42:07 +0000 | [diff] [blame] | 821 | if (flush_domains & I915_GEM_DOMAIN_GTT) |
| 822 | wmb(); |
| 823 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 824 | if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) { |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 825 | for (i = 0; i < I915_NUM_RINGS; i++) |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 826 | if (flush_rings & (1 << i)) { |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 827 | ret = i915_gem_flush_ring(&dev_priv->ring[i], |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 828 | invalidate_domains, |
| 829 | flush_domains); |
| 830 | if (ret) |
| 831 | return ret; |
| 832 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 833 | } |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 834 | |
| 835 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 838 | static int |
Chris Wilson | c59a333 | 2011-03-06 13:51:29 +0000 | [diff] [blame] | 839 | i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips) |
| 840 | { |
| 841 | u32 plane, flip_mask; |
| 842 | int ret; |
| 843 | |
| 844 | /* Check for any pending flips. As we only maintain a flip queue depth |
| 845 | * of 1, we can simply insert a WAIT for the next display flip prior |
| 846 | * to executing the batch and avoid stalling the CPU. |
| 847 | */ |
| 848 | |
| 849 | for (plane = 0; flips >> plane; plane++) { |
| 850 | if (((flips >> plane) & 1) == 0) |
| 851 | continue; |
| 852 | |
| 853 | if (plane) |
| 854 | flip_mask = MI_WAIT_FOR_PLANE_B_FLIP; |
| 855 | else |
| 856 | flip_mask = MI_WAIT_FOR_PLANE_A_FLIP; |
| 857 | |
| 858 | ret = intel_ring_begin(ring, 2); |
| 859 | if (ret) |
| 860 | return ret; |
| 861 | |
| 862 | intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask); |
| 863 | intel_ring_emit(ring, MI_NOOP); |
| 864 | intel_ring_advance(ring); |
| 865 | } |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | |
| 871 | static int |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 872 | i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring, |
| 873 | struct list_head *objects) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 874 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 875 | struct drm_i915_gem_object *obj; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 876 | struct change_domains cd; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 877 | int ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 878 | |
Chris Wilson | c59a333 | 2011-03-06 13:51:29 +0000 | [diff] [blame] | 879 | memset(&cd, 0, sizeof(cd)); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 880 | list_for_each_entry(obj, objects, exec_list) |
| 881 | i915_gem_object_set_to_gpu_domain(obj, ring, &cd); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 882 | |
| 883 | if (cd.invalidate_domains | cd.flush_domains) { |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 884 | ret = i915_gem_execbuffer_flush(ring->dev, |
| 885 | cd.invalidate_domains, |
| 886 | cd.flush_domains, |
| 887 | cd.flush_rings); |
| 888 | if (ret) |
| 889 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Chris Wilson | c59a333 | 2011-03-06 13:51:29 +0000 | [diff] [blame] | 892 | if (cd.flips) { |
| 893 | ret = i915_gem_execbuffer_wait_for_flips(ring, cd.flips); |
| 894 | if (ret) |
| 895 | return ret; |
| 896 | } |
| 897 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 898 | list_for_each_entry(obj, objects, exec_list) { |
Ben Widawsky | 2911a35 | 2012-04-05 14:47:36 -0700 | [diff] [blame^] | 899 | ret = i915_gem_object_sync(obj, ring); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 900 | if (ret) |
| 901 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | return 0; |
| 905 | } |
| 906 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 907 | static bool |
| 908 | i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 909 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 910 | return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | static int |
| 914 | validate_exec_list(struct drm_i915_gem_exec_object2 *exec, |
| 915 | int count) |
| 916 | { |
| 917 | int i; |
| 918 | |
| 919 | for (i = 0; i < count; i++) { |
| 920 | char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr; |
| 921 | int length; /* limited by fault_in_pages_readable() */ |
| 922 | |
| 923 | /* First check for malicious input causing overflow */ |
| 924 | if (exec[i].relocation_count > |
| 925 | INT_MAX / sizeof(struct drm_i915_gem_relocation_entry)) |
| 926 | return -EINVAL; |
| 927 | |
| 928 | length = exec[i].relocation_count * |
| 929 | sizeof(struct drm_i915_gem_relocation_entry); |
| 930 | if (!access_ok(VERIFY_READ, ptr, length)) |
| 931 | return -EFAULT; |
| 932 | |
| 933 | /* we may also need to update the presumed offsets */ |
| 934 | if (!access_ok(VERIFY_WRITE, ptr, length)) |
| 935 | return -EFAULT; |
| 936 | |
Daniel Vetter | f56f821 | 2012-03-25 19:47:41 +0200 | [diff] [blame] | 937 | if (fault_in_multipages_readable(ptr, length)) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 938 | return -EFAULT; |
| 939 | } |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 944 | static void |
| 945 | i915_gem_execbuffer_move_to_active(struct list_head *objects, |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 946 | struct intel_ring_buffer *ring, |
| 947 | u32 seqno) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 948 | { |
| 949 | struct drm_i915_gem_object *obj; |
| 950 | |
| 951 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 952 | u32 old_read = obj->base.read_domains; |
| 953 | u32 old_write = obj->base.write_domain; |
| 954 | |
| 955 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 956 | obj->base.read_domains = obj->base.pending_read_domains; |
| 957 | obj->base.write_domain = obj->base.pending_write_domain; |
| 958 | obj->fenced_gpu_access = obj->pending_fenced_gpu_access; |
| 959 | |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 960 | i915_gem_object_move_to_active(obj, ring, seqno); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 961 | if (obj->base.write_domain) { |
| 962 | obj->dirty = 1; |
Chris Wilson | 87ca9c8 | 2010-12-02 09:42:56 +0000 | [diff] [blame] | 963 | obj->pending_gpu_write = true; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 964 | list_move_tail(&obj->gpu_write_list, |
| 965 | &ring->gpu_write_list); |
| 966 | intel_mark_busy(ring->dev, obj); |
| 967 | } |
| 968 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 969 | trace_i915_gem_object_change_domain(obj, old_read, old_write); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 973 | static void |
| 974 | i915_gem_execbuffer_retire_commands(struct drm_device *dev, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 975 | struct drm_file *file, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 976 | struct intel_ring_buffer *ring) |
| 977 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 978 | struct drm_i915_gem_request *request; |
Chris Wilson | b72f3ac | 2011-01-04 17:34:02 +0000 | [diff] [blame] | 979 | u32 invalidate; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 980 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 981 | /* |
| 982 | * Ensure that the commands in the batch buffer are |
| 983 | * finished before the interrupt fires. |
| 984 | * |
| 985 | * The sampler always gets flushed on i965 (sigh). |
| 986 | */ |
Chris Wilson | b72f3ac | 2011-01-04 17:34:02 +0000 | [diff] [blame] | 987 | invalidate = I915_GEM_DOMAIN_COMMAND; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 988 | if (INTEL_INFO(dev)->gen >= 4) |
Chris Wilson | b72f3ac | 2011-01-04 17:34:02 +0000 | [diff] [blame] | 989 | invalidate |= I915_GEM_DOMAIN_SAMPLER; |
| 990 | if (ring->flush(ring, invalidate, 0)) { |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 991 | i915_gem_next_request_seqno(ring); |
Chris Wilson | b72f3ac | 2011-01-04 17:34:02 +0000 | [diff] [blame] | 992 | return; |
| 993 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 994 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 995 | /* Add a breadcrumb for the completion of the batch buffer */ |
| 996 | request = kzalloc(sizeof(*request), GFP_KERNEL); |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 997 | if (request == NULL || i915_add_request(ring, file, request)) { |
| 998 | i915_gem_next_request_seqno(ring); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 999 | kfree(request); |
| 1000 | } |
| 1001 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1002 | |
| 1003 | static int |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1004 | i915_reset_gen7_sol_offsets(struct drm_device *dev, |
| 1005 | struct intel_ring_buffer *ring) |
| 1006 | { |
| 1007 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1008 | int ret, i; |
| 1009 | |
| 1010 | if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) |
| 1011 | return 0; |
| 1012 | |
| 1013 | ret = intel_ring_begin(ring, 4 * 3); |
| 1014 | if (ret) |
| 1015 | return ret; |
| 1016 | |
| 1017 | for (i = 0; i < 4; i++) { |
| 1018 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 1019 | intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i)); |
| 1020 | intel_ring_emit(ring, 0); |
| 1021 | } |
| 1022 | |
| 1023 | intel_ring_advance(ring); |
| 1024 | |
| 1025 | return 0; |
| 1026 | } |
| 1027 | |
| 1028 | static int |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1029 | i915_gem_do_execbuffer(struct drm_device *dev, void *data, |
| 1030 | struct drm_file *file, |
| 1031 | struct drm_i915_gem_execbuffer2 *args, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1032 | struct drm_i915_gem_exec_object2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1033 | { |
| 1034 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1035 | struct list_head objects; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1036 | struct eb_objects *eb; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1037 | struct drm_i915_gem_object *batch_obj; |
| 1038 | struct drm_clip_rect *cliprects = NULL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1039 | struct intel_ring_buffer *ring; |
Chris Wilson | c4e7a41 | 2010-11-30 14:10:25 +0000 | [diff] [blame] | 1040 | u32 exec_start, exec_len; |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1041 | u32 seqno; |
Ben Widawsky | 84f9f93 | 2011-12-12 19:21:58 -0800 | [diff] [blame] | 1042 | u32 mask; |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 1043 | int ret, mode, i; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1044 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1045 | if (!i915_gem_check_execbuffer(args)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1046 | DRM_DEBUG("execbuf with invalid offset/length\n"); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1047 | return -EINVAL; |
| 1048 | } |
| 1049 | |
| 1050 | ret = validate_exec_list(exec, args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1051 | if (ret) |
| 1052 | return ret; |
| 1053 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1054 | switch (args->flags & I915_EXEC_RING_MASK) { |
| 1055 | case I915_EXEC_DEFAULT: |
| 1056 | case I915_EXEC_RENDER: |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1057 | ring = &dev_priv->ring[RCS]; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1058 | break; |
| 1059 | case I915_EXEC_BSD: |
| 1060 | if (!HAS_BSD(dev)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1061 | DRM_DEBUG("execbuf with invalid ring (BSD)\n"); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1062 | return -EINVAL; |
| 1063 | } |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1064 | ring = &dev_priv->ring[VCS]; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1065 | break; |
| 1066 | case I915_EXEC_BLT: |
| 1067 | if (!HAS_BLT(dev)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1068 | DRM_DEBUG("execbuf with invalid ring (BLT)\n"); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1069 | return -EINVAL; |
| 1070 | } |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1071 | ring = &dev_priv->ring[BCS]; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1072 | break; |
| 1073 | default: |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1074 | DRM_DEBUG("execbuf with unknown ring: %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1075 | (int)(args->flags & I915_EXEC_RING_MASK)); |
| 1076 | return -EINVAL; |
| 1077 | } |
| 1078 | |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 1079 | mode = args->flags & I915_EXEC_CONSTANTS_MASK; |
Ben Widawsky | 84f9f93 | 2011-12-12 19:21:58 -0800 | [diff] [blame] | 1080 | mask = I915_EXEC_CONSTANTS_MASK; |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 1081 | switch (mode) { |
| 1082 | case I915_EXEC_CONSTANTS_REL_GENERAL: |
| 1083 | case I915_EXEC_CONSTANTS_ABSOLUTE: |
| 1084 | case I915_EXEC_CONSTANTS_REL_SURFACE: |
| 1085 | if (ring == &dev_priv->ring[RCS] && |
| 1086 | mode != dev_priv->relative_constants_mode) { |
| 1087 | if (INTEL_INFO(dev)->gen < 4) |
| 1088 | return -EINVAL; |
| 1089 | |
| 1090 | if (INTEL_INFO(dev)->gen > 5 && |
| 1091 | mode == I915_EXEC_CONSTANTS_REL_SURFACE) |
| 1092 | return -EINVAL; |
Ben Widawsky | 84f9f93 | 2011-12-12 19:21:58 -0800 | [diff] [blame] | 1093 | |
| 1094 | /* The HW changed the meaning on this bit on gen6 */ |
| 1095 | if (INTEL_INFO(dev)->gen >= 6) |
| 1096 | mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE; |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 1097 | } |
| 1098 | break; |
| 1099 | default: |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1100 | DRM_DEBUG("execbuf with unknown constants: %d\n", mode); |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 1101 | return -EINVAL; |
| 1102 | } |
| 1103 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1104 | if (args->buffer_count < 1) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1105 | DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1106 | return -EINVAL; |
| 1107 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1108 | |
| 1109 | if (args->num_cliprects != 0) { |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1110 | if (ring != &dev_priv->ring[RCS]) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1111 | DRM_DEBUG("clip rectangles are only valid with the render ring\n"); |
Chris Wilson | c4e7a41 | 2010-11-30 14:10:25 +0000 | [diff] [blame] | 1112 | return -EINVAL; |
| 1113 | } |
| 1114 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1115 | cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects), |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1116 | GFP_KERNEL); |
| 1117 | if (cliprects == NULL) { |
| 1118 | ret = -ENOMEM; |
| 1119 | goto pre_mutex_err; |
| 1120 | } |
| 1121 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1122 | if (copy_from_user(cliprects, |
| 1123 | (struct drm_clip_rect __user *)(uintptr_t) |
| 1124 | args->cliprects_ptr, |
| 1125 | sizeof(*cliprects)*args->num_cliprects)) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1126 | ret = -EFAULT; |
| 1127 | goto pre_mutex_err; |
| 1128 | } |
| 1129 | } |
| 1130 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1131 | ret = i915_mutex_lock_interruptible(dev); |
| 1132 | if (ret) |
| 1133 | goto pre_mutex_err; |
| 1134 | |
| 1135 | if (dev_priv->mm.suspended) { |
| 1136 | mutex_unlock(&dev->struct_mutex); |
| 1137 | ret = -EBUSY; |
| 1138 | goto pre_mutex_err; |
| 1139 | } |
| 1140 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1141 | eb = eb_create(args->buffer_count); |
| 1142 | if (eb == NULL) { |
| 1143 | mutex_unlock(&dev->struct_mutex); |
| 1144 | ret = -ENOMEM; |
| 1145 | goto pre_mutex_err; |
| 1146 | } |
| 1147 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1148 | /* Look up object handles */ |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1149 | INIT_LIST_HEAD(&objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1150 | for (i = 0; i < args->buffer_count; i++) { |
| 1151 | struct drm_i915_gem_object *obj; |
| 1152 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1153 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, |
| 1154 | exec[i].handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 1155 | if (&obj->base == NULL) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1156 | DRM_DEBUG("Invalid object handle %d at index %d\n", |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1157 | exec[i].handle, i); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1158 | /* prevent error path from reading uninitialized data */ |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1159 | ret = -ENOENT; |
| 1160 | goto err; |
| 1161 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1162 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1163 | if (!list_empty(&obj->exec_list)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1164 | DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n", |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1165 | obj, exec[i].handle, i); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1166 | ret = -EINVAL; |
| 1167 | goto err; |
| 1168 | } |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1169 | |
| 1170 | list_add_tail(&obj->exec_list, &objects); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1171 | obj->exec_handle = exec[i].handle; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 1172 | obj->exec_entry = &exec[i]; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1173 | eb_add_object(eb, obj); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 1176 | /* take note of the batch buffer before we might reorder the lists */ |
| 1177 | batch_obj = list_entry(objects.prev, |
| 1178 | struct drm_i915_gem_object, |
| 1179 | exec_list); |
| 1180 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1181 | /* Move the objects en-masse into the GTT, evicting if necessary. */ |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 1182 | ret = i915_gem_execbuffer_reserve(ring, file, &objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1183 | if (ret) |
| 1184 | goto err; |
| 1185 | |
| 1186 | /* The objects are in their final locations, apply the relocations. */ |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 1187 | ret = i915_gem_execbuffer_relocate(dev, eb, &objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1188 | if (ret) { |
| 1189 | if (ret == -EFAULT) { |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 1190 | ret = i915_gem_execbuffer_relocate_slow(dev, file, ring, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1191 | &objects, eb, |
| 1192 | exec, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1193 | args->buffer_count); |
| 1194 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 1195 | } |
| 1196 | if (ret) |
| 1197 | goto err; |
| 1198 | } |
| 1199 | |
| 1200 | /* Set the pending read domains for the batch buffer to COMMAND */ |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1201 | if (batch_obj->base.pending_write_domain) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1202 | DRM_DEBUG("Attempting to use self-modifying batch buffer\n"); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1203 | ret = -EINVAL; |
| 1204 | goto err; |
| 1205 | } |
| 1206 | batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND; |
| 1207 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1208 | ret = i915_gem_execbuffer_move_to_gpu(ring, &objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1209 | if (ret) |
| 1210 | goto err; |
| 1211 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1212 | seqno = i915_gem_next_request_seqno(ring); |
Chris Wilson | 076e2c0 | 2011-01-21 10:07:18 +0000 | [diff] [blame] | 1213 | for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) { |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1214 | if (seqno < ring->sync_seqno[i]) { |
| 1215 | /* The GPU can not handle its semaphore value wrapping, |
| 1216 | * so every billion or so execbuffers, we need to stall |
| 1217 | * the GPU in order to reset the counters. |
| 1218 | */ |
Ben Widawsky | b93f9cf | 2012-01-25 15:39:34 -0800 | [diff] [blame] | 1219 | ret = i915_gpu_idle(dev, true); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1220 | if (ret) |
| 1221 | goto err; |
| 1222 | |
| 1223 | BUG_ON(ring->sync_seqno[i]); |
| 1224 | } |
| 1225 | } |
| 1226 | |
Ben Widawsky | e2971bd | 2011-12-12 19:21:57 -0800 | [diff] [blame] | 1227 | if (ring == &dev_priv->ring[RCS] && |
| 1228 | mode != dev_priv->relative_constants_mode) { |
| 1229 | ret = intel_ring_begin(ring, 4); |
| 1230 | if (ret) |
| 1231 | goto err; |
| 1232 | |
| 1233 | intel_ring_emit(ring, MI_NOOP); |
| 1234 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 1235 | intel_ring_emit(ring, INSTPM); |
Ben Widawsky | 84f9f93 | 2011-12-12 19:21:58 -0800 | [diff] [blame] | 1236 | intel_ring_emit(ring, mask << 16 | mode); |
Ben Widawsky | e2971bd | 2011-12-12 19:21:57 -0800 | [diff] [blame] | 1237 | intel_ring_advance(ring); |
| 1238 | |
| 1239 | dev_priv->relative_constants_mode = mode; |
| 1240 | } |
| 1241 | |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1242 | if (args->flags & I915_EXEC_GEN7_SOL_RESET) { |
| 1243 | ret = i915_reset_gen7_sol_offsets(dev, ring); |
| 1244 | if (ret) |
| 1245 | goto err; |
| 1246 | } |
| 1247 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1248 | trace_i915_gem_ring_dispatch(ring, seqno); |
| 1249 | |
Chris Wilson | c4e7a41 | 2010-11-30 14:10:25 +0000 | [diff] [blame] | 1250 | exec_start = batch_obj->gtt_offset + args->batch_start_offset; |
| 1251 | exec_len = args->batch_len; |
| 1252 | if (cliprects) { |
| 1253 | for (i = 0; i < args->num_cliprects; i++) { |
| 1254 | ret = i915_emit_box(dev, &cliprects[i], |
| 1255 | args->DR1, args->DR4); |
| 1256 | if (ret) |
| 1257 | goto err; |
| 1258 | |
| 1259 | ret = ring->dispatch_execbuffer(ring, |
| 1260 | exec_start, exec_len); |
| 1261 | if (ret) |
| 1262 | goto err; |
| 1263 | } |
| 1264 | } else { |
| 1265 | ret = ring->dispatch_execbuffer(ring, exec_start, exec_len); |
| 1266 | if (ret) |
| 1267 | goto err; |
| 1268 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1269 | |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 1270 | i915_gem_execbuffer_move_to_active(&objects, ring, seqno); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1271 | i915_gem_execbuffer_retire_commands(dev, file, ring); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1272 | |
| 1273 | err: |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1274 | eb_destroy(eb); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1275 | while (!list_empty(&objects)) { |
| 1276 | struct drm_i915_gem_object *obj; |
| 1277 | |
| 1278 | obj = list_first_entry(&objects, |
| 1279 | struct drm_i915_gem_object, |
| 1280 | exec_list); |
| 1281 | list_del_init(&obj->exec_list); |
| 1282 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | mutex_unlock(&dev->struct_mutex); |
| 1286 | |
| 1287 | pre_mutex_err: |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1288 | kfree(cliprects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1289 | return ret; |
| 1290 | } |
| 1291 | |
| 1292 | /* |
| 1293 | * Legacy execbuffer just creates an exec2 list from the original exec object |
| 1294 | * list array and passes it to the real function. |
| 1295 | */ |
| 1296 | int |
| 1297 | i915_gem_execbuffer(struct drm_device *dev, void *data, |
| 1298 | struct drm_file *file) |
| 1299 | { |
| 1300 | struct drm_i915_gem_execbuffer *args = data; |
| 1301 | struct drm_i915_gem_execbuffer2 exec2; |
| 1302 | struct drm_i915_gem_exec_object *exec_list = NULL; |
| 1303 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 1304 | int ret, i; |
| 1305 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1306 | if (args->buffer_count < 1) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1307 | DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1308 | return -EINVAL; |
| 1309 | } |
| 1310 | |
| 1311 | /* Copy in the exec list from userland */ |
| 1312 | exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count); |
| 1313 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count); |
| 1314 | if (exec_list == NULL || exec2_list == NULL) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1315 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1316 | args->buffer_count); |
| 1317 | drm_free_large(exec_list); |
| 1318 | drm_free_large(exec2_list); |
| 1319 | return -ENOMEM; |
| 1320 | } |
| 1321 | ret = copy_from_user(exec_list, |
| 1322 | (struct drm_i915_relocation_entry __user *) |
| 1323 | (uintptr_t) args->buffers_ptr, |
| 1324 | sizeof(*exec_list) * args->buffer_count); |
| 1325 | if (ret != 0) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1326 | DRM_DEBUG("copy %d exec entries failed %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1327 | args->buffer_count, ret); |
| 1328 | drm_free_large(exec_list); |
| 1329 | drm_free_large(exec2_list); |
| 1330 | return -EFAULT; |
| 1331 | } |
| 1332 | |
| 1333 | for (i = 0; i < args->buffer_count; i++) { |
| 1334 | exec2_list[i].handle = exec_list[i].handle; |
| 1335 | exec2_list[i].relocation_count = exec_list[i].relocation_count; |
| 1336 | exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; |
| 1337 | exec2_list[i].alignment = exec_list[i].alignment; |
| 1338 | exec2_list[i].offset = exec_list[i].offset; |
| 1339 | if (INTEL_INFO(dev)->gen < 4) |
| 1340 | exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; |
| 1341 | else |
| 1342 | exec2_list[i].flags = 0; |
| 1343 | } |
| 1344 | |
| 1345 | exec2.buffers_ptr = args->buffers_ptr; |
| 1346 | exec2.buffer_count = args->buffer_count; |
| 1347 | exec2.batch_start_offset = args->batch_start_offset; |
| 1348 | exec2.batch_len = args->batch_len; |
| 1349 | exec2.DR1 = args->DR1; |
| 1350 | exec2.DR4 = args->DR4; |
| 1351 | exec2.num_cliprects = args->num_cliprects; |
| 1352 | exec2.cliprects_ptr = args->cliprects_ptr; |
| 1353 | exec2.flags = I915_EXEC_RENDER; |
| 1354 | |
| 1355 | ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list); |
| 1356 | if (!ret) { |
| 1357 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 1358 | for (i = 0; i < args->buffer_count; i++) |
| 1359 | exec_list[i].offset = exec2_list[i].offset; |
| 1360 | /* ... and back out to userspace */ |
| 1361 | ret = copy_to_user((struct drm_i915_relocation_entry __user *) |
| 1362 | (uintptr_t) args->buffers_ptr, |
| 1363 | exec_list, |
| 1364 | sizeof(*exec_list) * args->buffer_count); |
| 1365 | if (ret) { |
| 1366 | ret = -EFAULT; |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1367 | DRM_DEBUG("failed to copy %d exec entries " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1368 | "back to user (%d)\n", |
| 1369 | args->buffer_count, ret); |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | drm_free_large(exec_list); |
| 1374 | drm_free_large(exec2_list); |
| 1375 | return ret; |
| 1376 | } |
| 1377 | |
| 1378 | int |
| 1379 | i915_gem_execbuffer2(struct drm_device *dev, void *data, |
| 1380 | struct drm_file *file) |
| 1381 | { |
| 1382 | struct drm_i915_gem_execbuffer2 *args = data; |
| 1383 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 1384 | int ret; |
| 1385 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1386 | if (args->buffer_count < 1) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1387 | DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1388 | return -EINVAL; |
| 1389 | } |
| 1390 | |
Chris Wilson | 8408c28 | 2011-02-21 12:54:48 +0000 | [diff] [blame] | 1391 | exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count, |
| 1392 | GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); |
| 1393 | if (exec2_list == NULL) |
| 1394 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), |
| 1395 | args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1396 | if (exec2_list == NULL) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1397 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1398 | args->buffer_count); |
| 1399 | return -ENOMEM; |
| 1400 | } |
| 1401 | ret = copy_from_user(exec2_list, |
| 1402 | (struct drm_i915_relocation_entry __user *) |
| 1403 | (uintptr_t) args->buffers_ptr, |
| 1404 | sizeof(*exec2_list) * args->buffer_count); |
| 1405 | if (ret != 0) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1406 | DRM_DEBUG("copy %d exec entries failed %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1407 | args->buffer_count, ret); |
| 1408 | drm_free_large(exec2_list); |
| 1409 | return -EFAULT; |
| 1410 | } |
| 1411 | |
| 1412 | ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list); |
| 1413 | if (!ret) { |
| 1414 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 1415 | ret = copy_to_user((struct drm_i915_relocation_entry __user *) |
| 1416 | (uintptr_t) args->buffers_ptr, |
| 1417 | exec2_list, |
| 1418 | sizeof(*exec2_list) * args->buffer_count); |
| 1419 | if (ret) { |
| 1420 | ret = -EFAULT; |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1421 | DRM_DEBUG("failed to copy %d exec entries " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1422 | "back to user (%d)\n", |
| 1423 | args->buffer_count, ret); |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | drm_free_large(exec2_list); |
| 1428 | return ret; |
| 1429 | } |