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