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