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