2 * Copyright © 2008,2010 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
27 * $FreeBSD: src/sys/dev/drm2/i915/i915_gem_execbuffer.c,v 1.3 2012/05/28 13:58:08 kib Exp $
30 #include <sys/limits.h>
31 #include <sys/sfbuf.h>
34 #include <drm/i915_drm.h>
36 #include "intel_drv.h"
38 struct change_domains {
39 uint32_t invalidate_domains;
40 uint32_t flush_domains;
46 * Set the next domain for the specified object. This
47 * may not actually perform the necessary flushing/invaliding though,
48 * as that may want to be batched with other set_domain operations
50 * This is (we hope) the only really tricky part of gem. The goal
51 * is fairly simple -- track which caches hold bits of the object
52 * and make sure they remain coherent. A few concrete examples may
53 * help to explain how it works. For shorthand, we use the notation
54 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
55 * a pair of read and write domain masks.
57 * Case 1: the batch buffer
63 * 5. Unmapped from GTT
66 * Let's take these a step at a time
69 * Pages allocated from the kernel may still have
70 * cache contents, so we set them to (CPU, CPU) always.
71 * 2. Written by CPU (using pwrite)
72 * The pwrite function calls set_domain (CPU, CPU) and
73 * this function does nothing (as nothing changes)
75 * This function asserts that the object is not
76 * currently in any GPU-based read or write domains
78 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
79 * As write_domain is zero, this function adds in the
80 * current read domains (CPU+COMMAND, 0).
81 * flush_domains is set to CPU.
82 * invalidate_domains is set to COMMAND
83 * clflush is run to get data out of the CPU caches
84 * then i915_dev_set_domain calls i915_gem_flush to
85 * emit an MI_FLUSH and drm_agp_chipset_flush
86 * 5. Unmapped from GTT
87 * i915_gem_object_unbind calls set_domain (CPU, CPU)
88 * flush_domains and invalidate_domains end up both zero
89 * so no flushing/invalidating happens
93 * Case 2: The shared render buffer
97 * 3. Read/written by GPU
98 * 4. set_domain to (CPU,CPU)
99 * 5. Read/written by CPU
100 * 6. Read/written by GPU
103 * Same as last example, (CPU, CPU)
105 * Nothing changes (assertions find that it is not in the GPU)
106 * 3. Read/written by GPU
107 * execbuffer calls set_domain (RENDER, RENDER)
108 * flush_domains gets CPU
109 * invalidate_domains gets GPU
111 * MI_FLUSH and drm_agp_chipset_flush
112 * 4. set_domain (CPU, CPU)
113 * flush_domains gets GPU
114 * invalidate_domains gets CPU
115 * wait_rendering (obj) to make sure all drawing is complete.
116 * This will include an MI_FLUSH to get the data from GPU
118 * clflush (obj) to invalidate the CPU cache
119 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
120 * 5. Read/written by CPU
121 * cache lines are loaded and dirtied
122 * 6. Read written by GPU
123 * Same as last GPU access
125 * Case 3: The constant buffer
130 * 4. Updated (written) by CPU again
139 * flush_domains = CPU
140 * invalidate_domains = RENDER
143 * drm_agp_chipset_flush
144 * 4. Updated (written) by CPU again
146 * flush_domains = 0 (no previous write domain)
147 * invalidate_domains = 0 (no new read domains)
150 * flush_domains = CPU
151 * invalidate_domains = RENDER
154 * drm_agp_chipset_flush
157 i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
158 struct intel_ring_buffer *ring,
159 struct change_domains *cd)
161 uint32_t invalidate_domains = 0, flush_domains = 0;
164 * If the object isn't moving to a new write domain,
165 * let the object stay in multiple read domains
167 if (obj->base.pending_write_domain == 0)
168 obj->base.pending_read_domains |= obj->base.read_domains;
171 * Flush the current write domain if
172 * the new read domains don't match. Invalidate
173 * any read domains which differ from the old
176 if (obj->base.write_domain &&
177 (((obj->base.write_domain != obj->base.pending_read_domains ||
178 obj->ring != ring)) ||
179 (obj->fenced_gpu_access && !obj->pending_fenced_gpu_access))) {
180 flush_domains |= obj->base.write_domain;
181 invalidate_domains |=
182 obj->base.pending_read_domains & ~obj->base.write_domain;
185 * Invalidate any read caches which may have
186 * stale data. That is, any new read domains.
188 invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
189 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
190 i915_gem_clflush_object(obj);
192 if (obj->base.pending_write_domain)
193 cd->flips |= atomic_read(&obj->pending_flip);
195 /* The actual obj->write_domain will be updated with
196 * pending_write_domain after we emit the accumulated flush for all
197 * of our domain changes in execbuffers (which clears objects'
198 * write_domains). So if we have a current write domain that we
199 * aren't changing, set pending_write_domain to that.
201 if (flush_domains == 0 && obj->base.pending_write_domain == 0)
202 obj->base.pending_write_domain = obj->base.write_domain;
204 cd->invalidate_domains |= invalidate_domains;
205 cd->flush_domains |= flush_domains;
206 if (flush_domains & I915_GEM_GPU_DOMAINS)
207 cd->flush_rings |= intel_ring_flag(obj->ring);
208 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
209 cd->flush_rings |= intel_ring_flag(ring);
214 struct hlist_head buckets[0];
217 static struct eb_objects *
220 struct eb_objects *eb;
221 int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
225 eb = kzalloc(count*sizeof(struct hlist_head) +
226 sizeof(struct eb_objects),
229 eb = kmalloc(count*sizeof(struct hlist_head) +
230 sizeof(struct eb_objects),
231 DRM_I915_GEM, M_WAITOK | M_ZERO);
241 eb_reset(struct eb_objects *eb)
243 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
247 eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj)
249 hlist_add_head(&obj->exec_node,
250 &eb->buckets[obj->exec_handle & eb->and]);
253 static struct drm_i915_gem_object *
254 eb_get_object(struct eb_objects *eb, unsigned long handle)
256 struct hlist_head *head;
257 struct hlist_node *node;
258 struct drm_i915_gem_object *obj;
260 head = &eb->buckets[handle & eb->and];
261 hlist_for_each(node, head) {
262 obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
263 if (obj->exec_handle == handle)
271 eb_destroy(struct eb_objects *eb)
273 drm_free(eb, DRM_I915_GEM);
277 i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
278 struct eb_objects *eb,
279 struct drm_i915_gem_relocation_entry *reloc)
281 struct drm_device *dev = obj->base.dev;
282 struct drm_gem_object *target_obj;
283 uint32_t target_offset;
286 /* we've already hold a reference to all valid objects */
287 target_obj = &eb_get_object(eb, reloc->target_handle)->base;
288 if (unlikely(target_obj == NULL))
291 target_offset = to_intel_bo(target_obj)->gtt_offset;
294 DRM_INFO("%s: obj %p offset %08x target %d "
295 "read %08x write %08x gtt %08x "
296 "presumed %08x delta %08x\n",
300 (int) reloc->target_handle,
301 (int) reloc->read_domains,
302 (int) reloc->write_domain,
304 (int) reloc->presumed_offset,
308 /* The target buffer should have appeared before us in the
309 * exec_object list, so it should have a GTT space bound by now.
311 if (unlikely(target_offset == 0)) {
312 DRM_DEBUG("No GTT space found for object %d\n",
313 reloc->target_handle);
317 /* Validate that the target is in a valid r/w GPU domain */
318 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
319 DRM_DEBUG("reloc with multiple write domains: "
320 "obj %p target %d offset %d "
321 "read %08x write %08x",
322 obj, reloc->target_handle,
325 reloc->write_domain);
328 if (unlikely((reloc->write_domain | reloc->read_domains)
329 & ~I915_GEM_GPU_DOMAINS)) {
330 DRM_DEBUG("reloc with read/write non-GPU domains: "
331 "obj %p target %d offset %d "
332 "read %08x write %08x",
333 obj, reloc->target_handle,
336 reloc->write_domain);
339 if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
340 reloc->write_domain != target_obj->pending_write_domain)) {
341 DRM_DEBUG("Write domain conflict: "
342 "obj %p target %d offset %d "
343 "new %08x old %08x\n",
344 obj, reloc->target_handle,
347 target_obj->pending_write_domain);
351 target_obj->pending_read_domains |= reloc->read_domains;
352 target_obj->pending_write_domain |= reloc->write_domain;
354 /* If the relocation already has the right value in it, no
355 * more work needs to be done.
357 if (target_offset == reloc->presumed_offset)
360 /* Check that the relocation address is valid... */
361 if (unlikely(reloc->offset > obj->base.size - 4)) {
362 DRM_DEBUG("Relocation beyond object bounds: "
363 "obj %p target %d offset %d size %d.\n",
364 obj, reloc->target_handle,
366 (int) obj->base.size);
369 if (unlikely(reloc->offset & 3)) {
370 DRM_DEBUG("Relocation not 4-byte aligned: "
371 "obj %p target %d offset %d.\n",
372 obj, reloc->target_handle,
373 (int) reloc->offset);
377 /* We can't wait for rendering with pagefaults disabled */
378 if (obj->active && (curthread->td_flags & TDF_NOFAULT))
381 reloc->delta += target_offset;
382 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
383 uint32_t page_offset = reloc->offset & PAGE_MASK;
387 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
391 sf = sf_buf_alloc(obj->pages[OFF_TO_IDX(reloc->offset)]);
394 vaddr = (void *)sf_buf_kva(sf);
396 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
399 uint32_t *reloc_entry;
402 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
407 * Map the page containing the relocation we're going
410 reloc->offset += obj->gtt_offset;
411 reloc_page = pmap_mapdev_attr(dev->agp->base + (reloc->offset &
412 ~PAGE_MASK), PAGE_SIZE, PAT_WRITE_COMBINING);
413 reloc_entry = (uint32_t *)(reloc_page + (reloc->offset &
415 *(volatile uint32_t *)reloc_entry = reloc->delta;
416 pmap_unmapdev((vm_offset_t)reloc_page, PAGE_SIZE);
419 /* and update the user's relocation entry */
420 reloc->presumed_offset = target_offset;
426 i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
427 struct eb_objects *eb)
429 struct drm_i915_gem_relocation_entry *user_relocs;
430 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
431 struct drm_i915_gem_relocation_entry reloc;
434 user_relocs = (void *)(uintptr_t)entry->relocs_ptr;
435 for (i = 0; i < entry->relocation_count; i++) {
436 ret = -copyin_nofault(user_relocs + i, &reloc, sizeof(reloc));
440 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &reloc);
444 ret = -copyout_nofault(&reloc.presumed_offset,
445 &user_relocs[i].presumed_offset,
446 sizeof(reloc.presumed_offset));
455 i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
456 struct eb_objects *eb, struct drm_i915_gem_relocation_entry *relocs)
458 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
461 for (i = 0; i < entry->relocation_count; i++) {
462 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
471 i915_gem_execbuffer_relocate(struct drm_device *dev,
472 struct eb_objects *eb,
473 struct list_head *objects)
475 struct drm_i915_gem_object *obj;
476 thread_t td = curthread;
480 /* Try to move as many of the relocation targets off the active list
481 * to avoid unnecessary fallbacks to the slow path, as we cannot wait
482 * for the retirement with pagefaults disabled.
484 i915_gem_retire_requests(dev);
487 pflags = td->td_flags & TDF_NOFAULT;
488 atomic_set_int(&td->td_flags, TDF_NOFAULT);
490 /* This is the fast path and we cannot handle a pagefault whilst
491 * holding the device lock lest the user pass in the relocations
492 * contained within a mmaped bo. For in such a case we, the page
493 * fault handler would call i915_gem_fault() and we would try to
494 * acquire the device lock again. Obviously this is bad.
497 list_for_each_entry(obj, objects, exec_list) {
498 ret = i915_gem_execbuffer_relocate_object(obj, eb);
503 if ((pflags & TDF_NOFAULT) == 0)
504 atomic_clear_int(&td->td_flags, TDF_NOFAULT);
509 #define __EXEC_OBJECT_HAS_FENCE (1<<31)
512 pin_and_fence_object(struct drm_i915_gem_object *obj,
513 struct intel_ring_buffer *ring)
515 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
516 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
517 bool need_fence, need_mappable;
521 has_fenced_gpu_access &&
522 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
523 obj->tiling_mode != I915_TILING_NONE;
525 entry->relocation_count ? true : need_fence;
527 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable);
531 if (has_fenced_gpu_access) {
532 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
533 if (obj->tiling_mode) {
534 ret = i915_gem_object_get_fence(obj);
538 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
539 i915_gem_object_pin_fence(obj);
541 ret = i915_gem_object_put_fence(obj);
545 obj->pending_fenced_gpu_access = true;
549 entry->offset = obj->gtt_offset;
553 i915_gem_object_unpin(obj);
558 i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
559 struct drm_file *file,
560 struct list_head *objects)
562 drm_i915_private_t *dev_priv;
563 struct drm_i915_gem_object *obj;
565 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
566 struct list_head ordered_objects;
568 dev_priv = ring->dev->dev_private;
569 INIT_LIST_HEAD(&ordered_objects);
570 while (!list_empty(objects)) {
571 struct drm_i915_gem_exec_object2 *entry;
572 bool need_fence, need_mappable;
574 obj = list_first_entry(objects,
575 struct drm_i915_gem_object,
577 entry = obj->exec_entry;
580 has_fenced_gpu_access &&
581 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
582 obj->tiling_mode != I915_TILING_NONE;
584 entry->relocation_count ? true : need_fence;
587 list_move(&obj->exec_list, &ordered_objects);
589 list_move_tail(&obj->exec_list, &ordered_objects);
591 obj->base.pending_read_domains = 0;
592 obj->base.pending_write_domain = 0;
594 list_splice(&ordered_objects, objects);
596 /* Attempt to pin all of the buffers into the GTT.
597 * This is done in 3 phases:
599 * 1a. Unbind all objects that do not match the GTT constraints for
600 * the execbuffer (fenceable, mappable, alignment etc).
601 * 1b. Increment pin count for already bound objects and obtain
602 * a fence register if required.
603 * 2. Bind new objects.
604 * 3. Decrement pin count.
606 * This avoid unnecessary unbinding of later objects in order to makr
607 * room for the earlier objects *unless* we need to defragment.
613 /* Unbind any ill-fitting objects or pin. */
614 list_for_each_entry(obj, objects, exec_list) {
615 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
616 bool need_fence, need_mappable;
622 has_fenced_gpu_access &&
623 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
624 obj->tiling_mode != I915_TILING_NONE;
626 entry->relocation_count ? true : need_fence;
628 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
629 (need_mappable && !obj->map_and_fenceable))
630 ret = i915_gem_object_unbind(obj);
632 ret = pin_and_fence_object(obj, ring);
637 /* Bind fresh objects */
638 list_for_each_entry(obj, objects, exec_list) {
642 ret = pin_and_fence_object(obj, ring);
646 /* This can potentially raise a harmless
647 * -EINVAL if we failed to bind in the above
648 * call. It cannot raise -EINTR since we know
649 * that the bo is freshly bound and so will
650 * not need to be flushed or waited upon.
652 ret_ignore = i915_gem_object_unbind(obj);
654 if (obj->gtt_space != NULL)
655 kprintf("%s: gtt_space\n", __func__);
660 /* Decrement pin count for bound objects */
661 list_for_each_entry(obj, objects, exec_list) {
662 struct drm_i915_gem_exec_object2 *entry;
667 entry = obj->exec_entry;
668 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
669 i915_gem_object_unpin_fence(obj);
670 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
673 i915_gem_object_unpin(obj);
675 /* ... and ensure ppgtt mapping exist if needed. */
676 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
677 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
678 obj, obj->cache_level);
680 obj->has_aliasing_ppgtt_mapping = 1;
684 if (ret != -ENOSPC || retry > 1)
687 /* First attempt, just clear anything that is purgeable.
688 * Second attempt, clear the entire GTT.
690 ret = i915_gem_evict_everything(ring->dev);
698 list_for_each_entry_continue_reverse(obj, objects, exec_list) {
699 struct drm_i915_gem_exec_object2 *entry;
704 entry = obj->exec_entry;
705 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
706 i915_gem_object_unpin_fence(obj);
707 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
710 i915_gem_object_unpin(obj);
717 i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
718 struct drm_file *file, struct intel_ring_buffer *ring,
719 struct list_head *objects, struct eb_objects *eb,
720 struct drm_i915_gem_exec_object2 *exec, int count)
722 struct drm_i915_gem_relocation_entry *reloc;
723 struct drm_i915_gem_object *obj;
727 /* We may process another execbuffer during the unlock... */
728 while (!list_empty(objects)) {
729 obj = list_first_entry(objects,
730 struct drm_i915_gem_object,
732 list_del_init(&obj->exec_list);
733 drm_gem_object_unreference(&obj->base);
739 for (i = 0; i < count; i++)
740 total += exec[i].relocation_count;
742 reloc_offset = kmalloc(count * sizeof(*reloc_offset), DRM_I915_GEM,
744 reloc = kmalloc(total * sizeof(*reloc), DRM_I915_GEM, M_WAITOK | M_ZERO);
747 for (i = 0; i < count; i++) {
748 struct drm_i915_gem_relocation_entry *user_relocs;
750 user_relocs = (void *)(uintptr_t)exec[i].relocs_ptr;
751 ret = -copyin(user_relocs, reloc + total,
752 exec[i].relocation_count * sizeof(*reloc));
758 reloc_offset[i] = total;
759 total += exec[i].relocation_count;
762 ret = i915_mutex_lock_interruptible(dev);
768 /* reacquire the objects */
770 for (i = 0; i < count; i++) {
771 struct drm_i915_gem_object *obj;
773 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
775 if (&obj->base == NULL) {
776 DRM_DEBUG("Invalid object handle %d at index %d\n",
782 list_add_tail(&obj->exec_list, objects);
783 obj->exec_handle = exec[i].handle;
784 obj->exec_entry = &exec[i];
785 eb_add_object(eb, obj);
788 ret = i915_gem_execbuffer_reserve(ring, file, objects);
792 list_for_each_entry(obj, objects, exec_list) {
793 int offset = obj->exec_entry - exec;
794 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
795 reloc + reloc_offset[offset]);
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.
807 drm_free(reloc, DRM_I915_GEM);
808 drm_free(reloc_offset, DRM_I915_GEM);
813 i915_gem_execbuffer_flush(struct drm_device *dev,
814 uint32_t invalidate_domains,
815 uint32_t flush_domains,
816 uint32_t flush_rings)
818 drm_i915_private_t *dev_priv = dev->dev_private;
821 if (flush_domains & I915_GEM_DOMAIN_CPU)
822 intel_gtt_chipset_flush();
824 if (flush_domains & I915_GEM_DOMAIN_GTT)
827 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
828 for (i = 0; i < I915_NUM_RINGS; i++)
829 if (flush_rings & (1 << i)) {
830 ret = i915_gem_flush_ring(&dev_priv->ring[i],
831 invalidate_domains, flush_domains);
841 i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
843 u32 plane, flip_mask;
846 /* Check for any pending flips. As we only maintain a flip queue depth
847 * of 1, we can simply insert a WAIT for the next display flip prior
848 * to executing the batch and avoid stalling the CPU.
851 for (plane = 0; flips >> plane; plane++) {
852 if (((flips >> plane) & 1) == 0)
856 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
858 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
860 ret = intel_ring_begin(ring, 2);
864 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
865 intel_ring_emit(ring, MI_NOOP);
866 intel_ring_advance(ring);
873 i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
874 struct list_head *objects)
876 struct drm_i915_gem_object *obj;
877 struct change_domains cd;
880 memset(&cd, 0, sizeof(cd));
881 list_for_each_entry(obj, objects, exec_list)
882 i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
884 if (cd.invalidate_domains | cd.flush_domains) {
886 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
888 cd.invalidate_domains,
891 ret = i915_gem_execbuffer_flush(ring->dev,
892 cd.invalidate_domains,
900 ret = i915_gem_execbuffer_wait_for_flips(ring, cd.flips);
905 list_for_each_entry(obj, objects, exec_list) {
906 ret = i915_gem_object_sync(obj, ring);
915 i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
917 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
921 validate_exec_list(struct drm_i915_gem_exec_object2 *exec, int count,
925 int i, length, page_count;
927 /* XXXKIB various limits checking is missing there */
928 *map = kmalloc(count * sizeof(*ma), DRM_I915_GEM, M_WAITOK | M_ZERO);
929 for (i = 0; i < count; i++) {
930 /* First check for malicious input causing overflow */
931 if (exec[i].relocation_count >
932 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
935 length = exec[i].relocation_count *
936 sizeof(struct drm_i915_gem_relocation_entry);
942 * Since both start and end of the relocation region
943 * may be not aligned on the page boundary, be
944 * conservative and request a page slot for each
945 * partial page. Thus +2.
947 page_count = howmany(length, PAGE_SIZE) + 2;
948 ma = (*map)[i] = kmalloc(page_count * sizeof(vm_page_t),
949 DRM_I915_GEM, M_WAITOK | M_ZERO);
950 if (vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
951 exec[i].relocs_ptr, length, VM_PROT_READ | VM_PROT_WRITE,
952 ma, page_count) == -1) {
953 drm_free(ma, DRM_I915_GEM);
963 i915_gem_execbuffer_move_to_active(struct list_head *objects,
964 struct intel_ring_buffer *ring)
966 struct drm_i915_gem_object *obj;
967 uint32_t old_read, old_write;
969 list_for_each_entry(obj, objects, exec_list) {
970 old_read = obj->base.read_domains;
971 old_write = obj->base.write_domain;
973 obj->base.read_domains = obj->base.pending_read_domains;
974 obj->base.write_domain = obj->base.pending_write_domain;
975 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
977 i915_gem_object_move_to_active(obj, ring);
978 if (obj->base.write_domain) {
980 obj->last_write_seqno = intel_ring_get_seqno(ring);
981 list_move_tail(&obj->gpu_write_list,
982 &ring->gpu_write_list);
983 intel_mark_busy(ring->dev);
988 int i915_gem_sync_exec_requests;
991 i915_gem_execbuffer_retire_commands(struct drm_device *dev,
992 struct drm_file *file,
993 struct intel_ring_buffer *ring)
995 /* Unconditionally force add_request to emit a full flush. */
996 ring->gpu_caches_dirty = true;
998 /* Add a breadcrumb for the completion of the batch buffer */
999 (void)i915_add_request(ring, file, NULL);
1003 i915_gem_fix_mi_batchbuffer_end(struct drm_i915_gem_object *batch_obj,
1004 uint32_t batch_start_offset, uint32_t batch_len)
1007 uint64_t po_r, po_w;
1010 po_r = batch_obj->base.dev->agp->base + batch_obj->gtt_offset +
1011 batch_start_offset + batch_len;
1014 mkva = pmap_mapdev_attr(trunc_page(po_r), 2 * PAGE_SIZE,
1015 PAT_WRITE_COMBINING);
1017 cmd = *(uint32_t *)(mkva + po_r);
1019 if (cmd != MI_BATCH_BUFFER_END) {
1021 * batch_len != 0 due to the check at the start of
1022 * i915_gem_do_execbuffer
1024 if (batch_obj->base.size > batch_start_offset + batch_len) {
1026 /* DRM_DEBUG("batchbuffer does not end by MI_BATCH_BUFFER_END !\n"); */
1029 DRM_DEBUG("batchbuffer does not end by MI_BATCH_BUFFER_END, overwriting last bo cmd !\n");
1031 *(uint32_t *)(mkva + po_w) = MI_BATCH_BUFFER_END;
1034 pmap_unmapdev((vm_offset_t)mkva, 2 * PAGE_SIZE);
1037 int i915_fix_mi_batchbuffer_end = 0;
1040 i915_reset_gen7_sol_offsets(struct drm_device *dev,
1041 struct intel_ring_buffer *ring)
1043 drm_i915_private_t *dev_priv = dev->dev_private;
1046 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
1049 ret = intel_ring_begin(ring, 4 * 3);
1053 for (i = 0; i < 4; i++) {
1054 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1055 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
1056 intel_ring_emit(ring, 0);
1059 intel_ring_advance(ring);
1065 i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1066 struct drm_file *file,
1067 struct drm_i915_gem_execbuffer2 *args,
1068 struct drm_i915_gem_exec_object2 *exec)
1070 drm_i915_private_t *dev_priv = dev->dev_private;
1071 struct list_head objects;
1072 struct eb_objects *eb;
1073 struct drm_i915_gem_object *batch_obj;
1074 struct drm_clip_rect *cliprects = NULL;
1075 struct intel_ring_buffer *ring;
1076 vm_page_t **relocs_ma;
1077 u32 exec_start, exec_len;
1082 if (!i915_gem_check_execbuffer(args)) {
1083 DRM_DEBUG("execbuf with invalid offset/length\n");
1087 if (args->batch_len == 0)
1090 ret = validate_exec_list(exec, args->buffer_count, &relocs_ma);
1095 if (args->flags & I915_EXEC_SECURE) {
1096 flags |= I915_DISPATCH_SECURE;
1098 if (args->flags & I915_EXEC_IS_PINNED)
1099 flags |= I915_DISPATCH_PINNED;
1101 switch (args->flags & I915_EXEC_RING_MASK) {
1102 case I915_EXEC_DEFAULT:
1103 case I915_EXEC_RENDER:
1104 ring = &dev_priv->ring[RCS];
1107 if (!HAS_BSD(dev)) {
1108 DRM_DEBUG("execbuf with invalid ring (BSD)\n");
1111 ring = &dev_priv->ring[VCS];
1114 if (!HAS_BLT(dev)) {
1115 DRM_DEBUG("execbuf with invalid ring (BLT)\n");
1118 ring = &dev_priv->ring[BCS];
1121 DRM_DEBUG("execbuf with unknown ring: %d\n",
1122 (int)(args->flags & I915_EXEC_RING_MASK));
1124 goto pre_struct_lock_err;
1126 if (!intel_ring_initialized(ring)) {
1127 DRM_DEBUG("execbuf with invalid ring: %d\n",
1128 (int)(args->flags & I915_EXEC_RING_MASK));
1132 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1133 mask = I915_EXEC_CONSTANTS_MASK;
1135 case I915_EXEC_CONSTANTS_REL_GENERAL:
1136 case I915_EXEC_CONSTANTS_ABSOLUTE:
1137 case I915_EXEC_CONSTANTS_REL_SURFACE:
1138 if (ring == &dev_priv->ring[RCS] &&
1139 mode != dev_priv->relative_constants_mode) {
1140 if (INTEL_INFO(dev)->gen < 4) {
1142 goto pre_struct_lock_err;
1145 if (INTEL_INFO(dev)->gen > 5 &&
1146 mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1148 goto pre_struct_lock_err;
1151 /* The HW changed the meaning on this bit on gen6 */
1152 if (INTEL_INFO(dev)->gen >= 6)
1153 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1157 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
1159 goto pre_struct_lock_err;
1162 if (args->buffer_count < 1) {
1163 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
1165 goto pre_struct_lock_err;
1168 if (args->num_cliprects != 0) {
1169 if (ring != &dev_priv->ring[RCS]) {
1170 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
1172 goto pre_struct_lock_err;
1175 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1176 DRM_DEBUG("execbuf with %u cliprects\n",
1177 args->num_cliprects);
1179 goto pre_struct_lock_err;
1181 cliprects = kmalloc( sizeof(*cliprects) * args->num_cliprects,
1182 DRM_I915_GEM, M_WAITOK | M_ZERO);
1183 ret = -copyin((void *)(uintptr_t)args->cliprects_ptr, cliprects,
1184 sizeof(*cliprects) * args->num_cliprects);
1186 goto pre_struct_lock_err;
1189 ret = i915_mutex_lock_interruptible(dev);
1191 goto pre_struct_lock_err;
1193 if (dev_priv->mm.suspended) {
1195 goto struct_lock_err;
1198 eb = eb_create(args->buffer_count);
1201 goto struct_lock_err;
1204 /* Look up object handles */
1205 INIT_LIST_HEAD(&objects);
1206 for (i = 0; i < args->buffer_count; i++) {
1207 struct drm_i915_gem_object *obj;
1208 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
1210 if (&obj->base == NULL) {
1211 DRM_DEBUG("Invalid object handle %d at index %d\n",
1213 /* prevent error path from reading uninitialized data */
1218 if (!list_empty(&obj->exec_list)) {
1219 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
1220 obj, exec[i].handle, i);
1225 list_add_tail(&obj->exec_list, &objects);
1226 obj->exec_handle = exec[i].handle;
1227 obj->exec_entry = &exec[i];
1228 eb_add_object(eb, obj);
1231 /* take note of the batch buffer before we might reorder the lists */
1232 batch_obj = list_entry(objects.prev,
1233 struct drm_i915_gem_object,
1236 /* Move the objects en-masse into the GTT, evicting if necessary. */
1237 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
1241 /* The objects are in their final locations, apply the relocations. */
1242 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
1244 if (ret == -EFAULT) {
1245 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
1246 &objects, eb, exec, args->buffer_count);
1247 DRM_LOCK_ASSERT(dev);
1253 /* Set the pending read domains for the batch buffer to COMMAND */
1254 if (batch_obj->base.pending_write_domain) {
1255 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
1259 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1261 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1262 * batch" bit. Hence we need to pin secure batches into the global gtt.
1263 * hsw should have this fixed, but let's be paranoid and do it
1264 * unconditionally for now. */
1265 if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
1266 i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
1268 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
1272 if (ring == &dev_priv->ring[RCS] &&
1273 mode != dev_priv->relative_constants_mode) {
1274 ret = intel_ring_begin(ring, 4);
1278 intel_ring_emit(ring, MI_NOOP);
1279 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1280 intel_ring_emit(ring, INSTPM);
1281 intel_ring_emit(ring, mask << 16 | mode);
1282 intel_ring_advance(ring);
1284 dev_priv->relative_constants_mode = mode;
1287 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1288 ret = i915_reset_gen7_sol_offsets(dev, ring);
1293 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1294 exec_len = args->batch_len;
1296 if (i915_fix_mi_batchbuffer_end) {
1297 i915_gem_fix_mi_batchbuffer_end(batch_obj,
1298 args->batch_start_offset, args->batch_len);
1302 for (i = 0; i < args->num_cliprects; i++) {
1303 ret = i915_emit_box_p(dev, &cliprects[i],
1304 args->DR1, args->DR4);
1308 ret = ring->dispatch_execbuffer(ring,
1309 exec_start, exec_len,
1315 ret = ring->dispatch_execbuffer(ring,
1316 exec_start, exec_len,
1322 i915_gem_execbuffer_move_to_active(&objects, ring);
1323 i915_gem_execbuffer_retire_commands(dev, file, ring);
1327 while (!list_empty(&objects)) {
1328 struct drm_i915_gem_object *obj;
1330 obj = list_first_entry(&objects, struct drm_i915_gem_object,
1332 list_del_init(&obj->exec_list);
1333 drm_gem_object_unreference(&obj->base);
1338 pre_struct_lock_err:
1339 for (i = 0; i < args->buffer_count; i++) {
1340 if (relocs_ma[i] != NULL) {
1341 vm_page_unhold_pages(relocs_ma[i], howmany(
1342 exec[i].relocation_count *
1343 sizeof(struct drm_i915_gem_relocation_entry),
1345 drm_free(relocs_ma[i], DRM_I915_GEM);
1348 drm_free(relocs_ma, DRM_I915_GEM);
1349 drm_free(cliprects, DRM_I915_GEM);
1354 * Legacy execbuffer just creates an exec2 list from the original exec object
1355 * list array and passes it to the real function.
1358 i915_gem_execbuffer(struct drm_device *dev, void *data,
1359 struct drm_file *file)
1361 struct drm_i915_gem_execbuffer *args = data;
1362 struct drm_i915_gem_execbuffer2 exec2;
1363 struct drm_i915_gem_exec_object *exec_list = NULL;
1364 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1367 DRM_DEBUG("buffers_ptr %d buffer_count %d len %08x\n",
1368 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
1370 if (args->buffer_count < 1) {
1371 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
1375 /* Copy in the exec list from userland */
1376 /* XXXKIB user-controlled malloc size */
1377 exec_list = kmalloc(sizeof(*exec_list) * args->buffer_count,
1378 DRM_I915_GEM, M_WAITOK);
1379 exec2_list = kmalloc(sizeof(*exec2_list) * args->buffer_count,
1380 DRM_I915_GEM, M_WAITOK);
1381 ret = -copyin((void *)(uintptr_t)args->buffers_ptr, exec_list,
1382 sizeof(*exec_list) * args->buffer_count);
1384 DRM_DEBUG("copy %d exec entries failed %d\n",
1385 args->buffer_count, ret);
1386 drm_free(exec_list, DRM_I915_GEM);
1387 drm_free(exec2_list, DRM_I915_GEM);
1391 for (i = 0; i < args->buffer_count; i++) {
1392 exec2_list[i].handle = exec_list[i].handle;
1393 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1394 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1395 exec2_list[i].alignment = exec_list[i].alignment;
1396 exec2_list[i].offset = exec_list[i].offset;
1397 if (INTEL_INFO(dev)->gen < 4)
1398 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1400 exec2_list[i].flags = 0;
1403 exec2.buffers_ptr = args->buffers_ptr;
1404 exec2.buffer_count = args->buffer_count;
1405 exec2.batch_start_offset = args->batch_start_offset;
1406 exec2.batch_len = args->batch_len;
1407 exec2.DR1 = args->DR1;
1408 exec2.DR4 = args->DR4;
1409 exec2.num_cliprects = args->num_cliprects;
1410 exec2.cliprects_ptr = args->cliprects_ptr;
1411 exec2.flags = I915_EXEC_RENDER;
1413 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1415 /* Copy the new buffer offsets back to the user's exec list. */
1416 for (i = 0; i < args->buffer_count; i++)
1417 exec_list[i].offset = exec2_list[i].offset;
1418 /* ... and back out to userspace */
1419 ret = -copyout(exec_list, (void *)(uintptr_t)args->buffers_ptr,
1420 sizeof(*exec_list) * args->buffer_count);
1422 DRM_DEBUG("failed to copy %d exec entries "
1423 "back to user (%d)\n",
1424 args->buffer_count, ret);
1428 drm_free(exec_list, DRM_I915_GEM);
1429 drm_free(exec2_list, DRM_I915_GEM);
1434 i915_gem_execbuffer2(struct drm_device *dev, void *data,
1435 struct drm_file *file)
1437 struct drm_i915_gem_execbuffer2 *args = data;
1438 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1441 DRM_DEBUG("buffers_ptr %jx buffer_count %d len %08x\n",
1442 (uintmax_t)args->buffers_ptr, args->buffer_count, args->batch_len);
1444 if (args->buffer_count < 1 ||
1445 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
1446 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
1450 /* XXXKIB user-controllable kmalloc size */
1451 exec2_list = kmalloc(sizeof(*exec2_list) * args->buffer_count,
1452 DRM_I915_GEM, M_WAITOK);
1453 ret = -copyin((void *)(uintptr_t)args->buffers_ptr, exec2_list,
1454 sizeof(*exec2_list) * args->buffer_count);
1456 DRM_DEBUG("copy %d exec entries failed %d\n",
1457 args->buffer_count, ret);
1458 drm_free(exec2_list, DRM_I915_GEM);
1462 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1464 /* Copy the new buffer offsets back to the user's exec list. */
1465 ret = -copyout(exec2_list, (void *)(uintptr_t)args->buffers_ptr,
1466 sizeof(*exec2_list) * args->buffer_count);
1468 DRM_DEBUG("failed to copy %d exec entries "
1469 "back to user (%d)\n",
1470 args->buffer_count, ret);
1474 drm_free(exec2_list, DRM_I915_GEM);