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>
30 #include <drm/i915_drm.h>
32 #include "i915_trace.h"
33 #include "intel_drv.h"
34 #include <linux/dma_remapping.h>
37 struct list_head vmas;
40 struct i915_vma *lut[0];
41 struct hlist_head buckets[0];
45 static struct eb_vmas *
46 eb_create(struct drm_i915_gem_execbuffer2 *args, struct i915_address_space *vm)
48 struct eb_vmas *eb = NULL;
50 if (args->flags & I915_EXEC_HANDLE_LUT) {
51 int size = args->buffer_count;
52 size *= sizeof(struct i915_vma *);
53 size += sizeof(struct eb_vmas);
54 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
58 int size = args->buffer_count;
59 int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
60 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
61 while (count > 2*size)
63 eb = kzalloc(count*sizeof(struct hlist_head) +
64 sizeof(struct eb_vmas),
71 eb->and = -args->buffer_count;
73 INIT_LIST_HEAD(&eb->vmas);
78 eb_reset(struct eb_vmas *eb)
81 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
85 eb_lookup_vmas(struct eb_vmas *eb,
86 struct drm_i915_gem_exec_object2 *exec,
87 const struct drm_i915_gem_execbuffer2 *args,
88 struct i915_address_space *vm,
89 struct drm_file *file)
91 struct drm_i915_gem_object *obj;
92 struct list_head objects;
95 INIT_LIST_HEAD(&objects);
96 spin_lock(&file->table_lock);
97 /* Grab a reference to the object and release the lock so we can lookup
98 * or create the VMA without using GFP_ATOMIC */
99 for (i = 0; i < args->buffer_count; i++) {
100 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
102 spin_unlock(&file->table_lock);
103 DRM_DEBUG("Invalid object handle %d at index %d\n",
109 if (!list_empty(&obj->obj_exec_link)) {
110 spin_unlock(&file->table_lock);
111 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
112 obj, exec[i].handle, i);
117 drm_gem_object_reference(&obj->base);
118 list_add_tail(&obj->obj_exec_link, &objects);
120 spin_unlock(&file->table_lock);
123 list_for_each_entry(obj, &objects, obj_exec_link) {
124 struct i915_vma *vma;
127 * NOTE: We can leak any vmas created here when something fails
128 * later on. But that's no issue since vma_unbind can deal with
129 * vmas which are not actually bound. And since only
130 * lookup_or_create exists as an interface to get at the vma
131 * from the (obj, vm) we don't run the risk of creating
132 * duplicated vmas for the same vm.
134 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
136 DRM_DEBUG("Failed to lookup VMA\n");
141 list_add_tail(&vma->exec_list, &eb->vmas);
143 vma->exec_entry = &exec[i];
147 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
148 vma->exec_handle = handle;
149 hlist_add_head(&vma->exec_node,
150 &eb->buckets[handle & eb->and]);
157 while (!list_empty(&objects)) {
158 obj = list_first_entry(&objects,
159 struct drm_i915_gem_object,
161 list_del_init(&obj->obj_exec_link);
163 drm_gem_object_unreference(&obj->base);
168 static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
171 if (handle >= -eb->and)
173 return eb->lut[handle];
175 struct hlist_head *head;
176 struct hlist_node *node;
178 head = &eb->buckets[handle & eb->and];
179 hlist_for_each(node, head) {
180 struct i915_vma *vma;
182 vma = hlist_entry(node, struct i915_vma, exec_node);
183 if (vma->exec_handle == handle)
190 static void eb_destroy(struct eb_vmas *eb) {
191 while (!list_empty(&eb->vmas)) {
192 struct i915_vma *vma;
194 vma = list_first_entry(&eb->vmas,
197 list_del_init(&vma->exec_list);
198 drm_gem_object_unreference(&vma->obj->base);
203 static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
205 return (HAS_LLC(obj->base.dev) ||
206 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
207 !obj->map_and_fenceable ||
208 obj->cache_level != I915_CACHE_NONE);
212 relocate_entry_cpu(struct drm_i915_gem_object *obj,
213 struct drm_i915_gem_relocation_entry *reloc)
215 uint32_t page_offset = offset_in_page(reloc->offset);
219 ret = i915_gem_object_set_to_cpu_domain(obj, true);
223 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
224 reloc->offset >> PAGE_SHIFT));
225 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
226 kunmap_atomic(vaddr);
232 relocate_entry_gtt(struct drm_i915_gem_object *obj,
233 struct drm_i915_gem_relocation_entry *reloc)
235 struct drm_device *dev = obj->base.dev;
236 struct drm_i915_private *dev_priv = dev->dev_private;
237 uint32_t __iomem *reloc_entry;
238 void __iomem *reloc_page;
241 ret = i915_gem_object_set_to_gtt_domain(obj, true);
245 ret = i915_gem_object_put_fence(obj);
249 /* Map the page containing the relocation we're going to perform. */
250 reloc->offset += i915_gem_obj_ggtt_offset(obj);
251 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
252 reloc->offset & PAGE_MASK);
253 reloc_entry = (uint32_t __iomem *)
254 (reloc_page + offset_in_page(reloc->offset));
255 iowrite32(reloc->delta, reloc_entry);
256 io_mapping_unmap_atomic(reloc_page);
262 i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
264 struct drm_i915_gem_relocation_entry *reloc,
265 struct i915_address_space *vm)
267 struct drm_device *dev = obj->base.dev;
268 struct drm_gem_object *target_obj;
269 struct drm_i915_gem_object *target_i915_obj;
270 struct i915_vma *target_vma;
271 uint32_t target_offset;
274 /* we've already hold a reference to all valid objects */
275 target_vma = eb_get_vma(eb, reloc->target_handle);
276 if (unlikely(target_vma == NULL))
278 target_i915_obj = target_vma->obj;
279 target_obj = &target_vma->obj->base;
281 target_offset = i915_gem_obj_ggtt_offset(target_i915_obj);
283 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
284 * pipe_control writes because the gpu doesn't properly redirect them
285 * through the ppgtt for non_secure batchbuffers. */
286 if (unlikely(IS_GEN6(dev) &&
287 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
288 !target_i915_obj->has_global_gtt_mapping)) {
289 i915_gem_gtt_bind_object(target_i915_obj,
290 target_i915_obj->cache_level);
293 /* Validate that the target is in a valid r/w GPU domain */
294 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
295 DRM_DEBUG("reloc with multiple write domains: "
296 "obj %p target %d offset %d "
297 "read %08x write %08x",
298 obj, reloc->target_handle,
301 reloc->write_domain);
304 if (unlikely((reloc->write_domain | reloc->read_domains)
305 & ~I915_GEM_GPU_DOMAINS)) {
306 DRM_DEBUG("reloc with read/write non-GPU domains: "
307 "obj %p target %d offset %d "
308 "read %08x write %08x",
309 obj, reloc->target_handle,
312 reloc->write_domain);
316 target_obj->pending_read_domains |= reloc->read_domains;
317 target_obj->pending_write_domain |= reloc->write_domain;
319 /* If the relocation already has the right value in it, no
320 * more work needs to be done.
322 if (target_offset == reloc->presumed_offset)
325 /* Check that the relocation address is valid... */
326 if (unlikely(reloc->offset > obj->base.size - 4)) {
327 DRM_DEBUG("Relocation beyond object bounds: "
328 "obj %p target %d offset %d size %d.\n",
329 obj, reloc->target_handle,
331 (int) obj->base.size);
334 if (unlikely(reloc->offset & 3)) {
335 DRM_DEBUG("Relocation not 4-byte aligned: "
336 "obj %p target %d offset %d.\n",
337 obj, reloc->target_handle,
338 (int) reloc->offset);
342 /* We can't wait for rendering with pagefaults disabled */
343 if (obj->active && in_atomic())
346 reloc->delta += target_offset;
347 if (use_cpu_reloc(obj))
348 ret = relocate_entry_cpu(obj, reloc);
350 ret = relocate_entry_gtt(obj, reloc);
355 /* and update the user's relocation entry */
356 reloc->presumed_offset = target_offset;
362 i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
365 #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
366 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
367 struct drm_i915_gem_relocation_entry __user *user_relocs;
368 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
371 user_relocs = to_user_ptr(entry->relocs_ptr);
373 remain = entry->relocation_count;
375 struct drm_i915_gem_relocation_entry *r = stack_reloc;
377 if (count > ARRAY_SIZE(stack_reloc))
378 count = ARRAY_SIZE(stack_reloc);
381 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
385 u64 offset = r->presumed_offset;
387 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r,
392 if (r->presumed_offset != offset &&
393 __copy_to_user_inatomic(&user_relocs->presumed_offset,
395 sizeof(r->presumed_offset))) {
409 i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
411 struct drm_i915_gem_relocation_entry *relocs)
413 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
416 for (i = 0; i < entry->relocation_count; i++) {
417 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i],
427 i915_gem_execbuffer_relocate(struct eb_vmas *eb,
428 struct i915_address_space *vm)
430 struct i915_vma *vma;
433 /* This is the fast path and we cannot handle a pagefault whilst
434 * holding the struct mutex lest the user pass in the relocations
435 * contained within a mmaped bo. For in such a case we, the page
436 * fault handler would call i915_gem_fault() and we would try to
437 * acquire the struct mutex again. Obviously this is bad and so
438 * lockdep complains vehemently.
441 list_for_each_entry(vma, &eb->vmas, exec_list) {
442 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
451 #define __EXEC_OBJECT_HAS_PIN (1<<31)
452 #define __EXEC_OBJECT_HAS_FENCE (1<<30)
455 need_reloc_mappable(struct i915_vma *vma)
457 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
458 return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
459 i915_is_ggtt(vma->vm);
463 i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
464 struct intel_ring_buffer *ring,
467 struct drm_i915_private *dev_priv = ring->dev->dev_private;
468 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
469 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
470 bool need_fence, need_mappable;
471 struct drm_i915_gem_object *obj = vma->obj;
475 has_fenced_gpu_access &&
476 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
477 obj->tiling_mode != I915_TILING_NONE;
478 need_mappable = need_fence || need_reloc_mappable(vma);
480 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, need_mappable,
485 entry->flags |= __EXEC_OBJECT_HAS_PIN;
487 if (has_fenced_gpu_access) {
488 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
489 ret = i915_gem_object_get_fence(obj);
493 if (i915_gem_object_pin_fence(obj))
494 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
496 obj->pending_fenced_gpu_access = true;
500 /* Ensure ppgtt mapping exists if needed */
501 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
502 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
503 obj, obj->cache_level);
505 obj->has_aliasing_ppgtt_mapping = 1;
508 if (entry->offset != vma->node.start) {
509 entry->offset = vma->node.start;
513 if (entry->flags & EXEC_OBJECT_WRITE) {
514 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
515 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
518 if (entry->flags & EXEC_OBJECT_NEEDS_GTT &&
519 !obj->has_global_gtt_mapping)
520 i915_gem_gtt_bind_object(obj, obj->cache_level);
526 i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
528 struct drm_i915_gem_exec_object2 *entry;
529 struct drm_i915_gem_object *obj = vma->obj;
531 if (!drm_mm_node_allocated(&vma->node))
534 entry = vma->exec_entry;
536 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
537 i915_gem_object_unpin_fence(obj);
539 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
540 i915_gem_object_unpin(obj);
542 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
546 i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
547 struct list_head *vmas,
550 struct drm_i915_gem_object *obj;
551 struct i915_vma *vma;
552 struct list_head ordered_vmas;
553 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
556 INIT_LIST_HEAD(&ordered_vmas);
557 while (!list_empty(vmas)) {
558 struct drm_i915_gem_exec_object2 *entry;
559 bool need_fence, need_mappable;
561 vma = list_first_entry(vmas, struct i915_vma, exec_list);
563 entry = vma->exec_entry;
566 has_fenced_gpu_access &&
567 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
568 obj->tiling_mode != I915_TILING_NONE;
569 need_mappable = need_fence || need_reloc_mappable(vma);
572 list_move(&vma->exec_list, &ordered_vmas);
574 list_move_tail(&vma->exec_list, &ordered_vmas);
576 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
577 obj->base.pending_write_domain = 0;
578 obj->pending_fenced_gpu_access = false;
580 list_splice(&ordered_vmas, vmas);
582 /* Attempt to pin all of the buffers into the GTT.
583 * This is done in 3 phases:
585 * 1a. Unbind all objects that do not match the GTT constraints for
586 * the execbuffer (fenceable, mappable, alignment etc).
587 * 1b. Increment pin count for already bound objects.
588 * 2. Bind new objects.
589 * 3. Decrement pin count.
591 * This avoid unnecessary unbinding of later objects in order to make
592 * room for the earlier objects *unless* we need to defragment.
598 /* Unbind any ill-fitting objects or pin. */
599 list_for_each_entry(vma, vmas, exec_list) {
600 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
601 bool need_fence, need_mappable;
605 if (!drm_mm_node_allocated(&vma->node))
609 has_fenced_gpu_access &&
610 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
611 obj->tiling_mode != I915_TILING_NONE;
612 need_mappable = need_fence || need_reloc_mappable(vma);
614 WARN_ON((need_mappable || need_fence) &&
615 !i915_is_ggtt(vma->vm));
617 if ((entry->alignment &&
618 vma->node.start & (entry->alignment - 1)) ||
619 (need_mappable && !obj->map_and_fenceable))
620 ret = i915_vma_unbind(vma);
622 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
627 /* Bind fresh objects */
628 list_for_each_entry(vma, vmas, exec_list) {
629 if (drm_mm_node_allocated(&vma->node))
632 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
637 err: /* Decrement pin count for bound objects */
638 list_for_each_entry(vma, vmas, exec_list)
639 i915_gem_execbuffer_unreserve_vma(vma);
641 if (ret != -ENOSPC || retry++)
644 ret = i915_gem_evict_everything(ring->dev);
651 i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
652 struct drm_i915_gem_execbuffer2 *args,
653 struct drm_file *file,
654 struct intel_ring_buffer *ring,
656 struct drm_i915_gem_exec_object2 *exec)
658 struct drm_i915_gem_relocation_entry *reloc;
659 struct i915_address_space *vm;
660 struct i915_vma *vma;
664 int count = args->buffer_count;
666 if (WARN_ON(list_empty(&eb->vmas)))
669 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
671 /* We may process another execbuffer during the unlock... */
672 while (!list_empty(&eb->vmas)) {
673 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
674 list_del_init(&vma->exec_list);
675 drm_gem_object_unreference(&vma->obj->base);
678 mutex_unlock(&dev->struct_mutex);
681 for (i = 0; i < count; i++)
682 total += exec[i].relocation_count;
684 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
685 reloc = drm_malloc_ab(total, sizeof(*reloc));
686 if (reloc == NULL || reloc_offset == NULL) {
687 drm_free_large(reloc);
688 drm_free_large(reloc_offset);
689 mutex_lock(&dev->struct_mutex);
694 for (i = 0; i < count; i++) {
695 struct drm_i915_gem_relocation_entry __user *user_relocs;
696 u64 invalid_offset = (u64)-1;
699 user_relocs = to_user_ptr(exec[i].relocs_ptr);
701 if (copy_from_user(reloc+total, user_relocs,
702 exec[i].relocation_count * sizeof(*reloc))) {
704 mutex_lock(&dev->struct_mutex);
708 /* As we do not update the known relocation offsets after
709 * relocating (due to the complexities in lock handling),
710 * we need to mark them as invalid now so that we force the
711 * relocation processing next time. Just in case the target
712 * object is evicted and then rebound into its old
713 * presumed_offset before the next execbuffer - if that
714 * happened we would make the mistake of assuming that the
715 * relocations were valid.
717 for (j = 0; j < exec[i].relocation_count; j++) {
718 if (copy_to_user(&user_relocs[j].presumed_offset,
720 sizeof(invalid_offset))) {
722 mutex_lock(&dev->struct_mutex);
727 reloc_offset[i] = total;
728 total += exec[i].relocation_count;
731 ret = i915_mutex_lock_interruptible(dev);
733 mutex_lock(&dev->struct_mutex);
737 /* reacquire the objects */
739 ret = eb_lookup_vmas(eb, exec, args, vm, file);
743 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
744 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
748 list_for_each_entry(vma, &eb->vmas, exec_list) {
749 int offset = vma->exec_entry - exec;
750 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
751 reloc + reloc_offset[offset]);
756 /* Leave the user relocations as are, this is the painfully slow path,
757 * and we want to avoid the complication of dropping the lock whilst
758 * having buffers reserved in the aperture and so causing spurious
759 * ENOSPC for random operations.
763 drm_free_large(reloc);
764 drm_free_large(reloc_offset);
769 i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
770 struct list_head *vmas)
772 struct i915_vma *vma;
773 uint32_t flush_domains = 0;
774 bool flush_chipset = false;
777 list_for_each_entry(vma, vmas, exec_list) {
778 struct drm_i915_gem_object *obj = vma->obj;
779 ret = i915_gem_object_sync(obj, ring);
783 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
784 flush_chipset |= i915_gem_clflush_object(obj, false);
786 flush_domains |= obj->base.write_domain;
790 i915_gem_chipset_flush(ring->dev);
792 if (flush_domains & I915_GEM_DOMAIN_GTT)
795 /* Unconditionally invalidate gpu caches and ensure that we do flush
796 * any residual writes from the previous batch.
798 return intel_ring_invalidate_all_caches(ring);
802 i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
804 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
807 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
811 validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
815 int relocs_total = 0;
816 int relocs_max = INT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
818 for (i = 0; i < count; i++) {
819 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
820 int length; /* limited by fault_in_pages_readable() */
822 if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
825 /* First check for malicious input causing overflow in
826 * the worst case where we need to allocate the entire
827 * relocation tree as a single array.
829 if (exec[i].relocation_count > relocs_max - relocs_total)
831 relocs_total += exec[i].relocation_count;
833 length = exec[i].relocation_count *
834 sizeof(struct drm_i915_gem_relocation_entry);
836 * We must check that the entire relocation array is safe
837 * to read, but since we may need to update the presumed
838 * offsets during execution, check for full write access.
840 if (!access_ok(VERIFY_WRITE, ptr, length))
843 if (likely(!i915_prefault_disable)) {
844 if (fault_in_multipages_readable(ptr, length))
853 i915_gem_execbuffer_move_to_active(struct list_head *vmas,
854 struct intel_ring_buffer *ring)
856 struct i915_vma *vma;
858 list_for_each_entry(vma, vmas, exec_list) {
859 struct drm_i915_gem_object *obj = vma->obj;
860 u32 old_read = obj->base.read_domains;
861 u32 old_write = obj->base.write_domain;
863 obj->base.write_domain = obj->base.pending_write_domain;
864 if (obj->base.write_domain == 0)
865 obj->base.pending_read_domains |= obj->base.read_domains;
866 obj->base.read_domains = obj->base.pending_read_domains;
867 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
869 list_move_tail(&vma->mm_list, &vma->vm->active_list);
870 i915_gem_object_move_to_active(obj, ring);
871 if (obj->base.write_domain) {
873 obj->last_write_seqno = intel_ring_get_seqno(ring);
874 if (obj->pin_count) /* check for potential scanout */
875 intel_mark_fb_busy(obj, ring);
878 trace_i915_gem_object_change_domain(obj, old_read, old_write);
883 i915_gem_execbuffer_retire_commands(struct drm_device *dev,
884 struct drm_file *file,
885 struct intel_ring_buffer *ring,
886 struct drm_i915_gem_object *obj)
888 /* Unconditionally force add_request to emit a full flush. */
889 ring->gpu_caches_dirty = true;
891 /* Add a breadcrumb for the completion of the batch buffer */
892 (void)__i915_add_request(ring, file, obj, NULL);
896 i915_reset_gen7_sol_offsets(struct drm_device *dev,
897 struct intel_ring_buffer *ring)
899 drm_i915_private_t *dev_priv = dev->dev_private;
902 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
905 ret = intel_ring_begin(ring, 4 * 3);
909 for (i = 0; i < 4; i++) {
910 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
911 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
912 intel_ring_emit(ring, 0);
915 intel_ring_advance(ring);
921 i915_gem_do_execbuffer(struct drm_device *dev, void *data,
922 struct drm_file *file,
923 struct drm_i915_gem_execbuffer2 *args,
924 struct drm_i915_gem_exec_object2 *exec,
925 struct i915_address_space *vm)
927 drm_i915_private_t *dev_priv = dev->dev_private;
929 struct drm_i915_gem_object *batch_obj;
930 struct drm_clip_rect *cliprects = NULL;
931 struct intel_ring_buffer *ring;
932 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
933 u32 exec_start, exec_len;
938 if (!i915_gem_check_execbuffer(args))
941 ret = validate_exec_list(exec, args->buffer_count);
946 if (args->flags & I915_EXEC_SECURE) {
947 if (!file->is_master || !capable(CAP_SYS_ADMIN))
950 flags |= I915_DISPATCH_SECURE;
952 if (args->flags & I915_EXEC_IS_PINNED)
953 flags |= I915_DISPATCH_PINNED;
955 switch (args->flags & I915_EXEC_RING_MASK) {
956 case I915_EXEC_DEFAULT:
957 case I915_EXEC_RENDER:
958 ring = &dev_priv->ring[RCS];
961 ring = &dev_priv->ring[VCS];
962 if (ctx_id != DEFAULT_CONTEXT_ID) {
963 DRM_DEBUG("Ring %s doesn't support contexts\n",
969 ring = &dev_priv->ring[BCS];
970 if (ctx_id != DEFAULT_CONTEXT_ID) {
971 DRM_DEBUG("Ring %s doesn't support contexts\n",
976 case I915_EXEC_VEBOX:
977 ring = &dev_priv->ring[VECS];
978 if (ctx_id != DEFAULT_CONTEXT_ID) {
979 DRM_DEBUG("Ring %s doesn't support contexts\n",
986 DRM_DEBUG("execbuf with unknown ring: %d\n",
987 (int)(args->flags & I915_EXEC_RING_MASK));
990 if (!intel_ring_initialized(ring)) {
991 DRM_DEBUG("execbuf with invalid ring: %d\n",
992 (int)(args->flags & I915_EXEC_RING_MASK));
996 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
997 mask = I915_EXEC_CONSTANTS_MASK;
999 case I915_EXEC_CONSTANTS_REL_GENERAL:
1000 case I915_EXEC_CONSTANTS_ABSOLUTE:
1001 case I915_EXEC_CONSTANTS_REL_SURFACE:
1002 if (ring == &dev_priv->ring[RCS] &&
1003 mode != dev_priv->relative_constants_mode) {
1004 if (INTEL_INFO(dev)->gen < 4)
1007 if (INTEL_INFO(dev)->gen > 5 &&
1008 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1011 /* The HW changed the meaning on this bit on gen6 */
1012 if (INTEL_INFO(dev)->gen >= 6)
1013 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1017 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
1021 if (args->buffer_count < 1) {
1022 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
1026 if (args->num_cliprects != 0) {
1027 if (ring != &dev_priv->ring[RCS]) {
1028 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
1032 if (INTEL_INFO(dev)->gen >= 5) {
1033 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1037 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1038 DRM_DEBUG("execbuf with %u cliprects\n",
1039 args->num_cliprects);
1043 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
1045 if (cliprects == NULL) {
1050 if (copy_from_user(cliprects,
1051 to_user_ptr(args->cliprects_ptr),
1052 sizeof(*cliprects)*args->num_cliprects)) {
1058 ret = i915_mutex_lock_interruptible(dev);
1062 if (dev_priv->ums.mm_suspended) {
1063 mutex_unlock(&dev->struct_mutex);
1068 eb = eb_create(args, vm);
1070 mutex_unlock(&dev->struct_mutex);
1075 /* Look up object handles */
1076 ret = eb_lookup_vmas(eb, exec, args, vm, file);
1080 /* take note of the batch buffer before we might reorder the lists */
1081 batch_obj = list_entry(eb->vmas.prev, struct i915_vma, exec_list)->obj;
1083 /* Move the objects en-masse into the GTT, evicting if necessary. */
1084 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
1085 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
1089 /* The objects are in their final locations, apply the relocations. */
1091 ret = i915_gem_execbuffer_relocate(eb, vm);
1093 if (ret == -EFAULT) {
1094 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
1096 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1102 /* Set the pending read domains for the batch buffer to COMMAND */
1103 if (batch_obj->base.pending_write_domain) {
1104 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
1108 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1110 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1111 * batch" bit. Hence we need to pin secure batches into the global gtt.
1112 * hsw should have this fixed, but let's be paranoid and do it
1113 * unconditionally for now. */
1114 if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
1115 i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
1117 ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
1121 ret = i915_switch_context(ring, file, ctx_id);
1125 if (ring == &dev_priv->ring[RCS] &&
1126 mode != dev_priv->relative_constants_mode) {
1127 ret = intel_ring_begin(ring, 4);
1131 intel_ring_emit(ring, MI_NOOP);
1132 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1133 intel_ring_emit(ring, INSTPM);
1134 intel_ring_emit(ring, mask << 16 | mode);
1135 intel_ring_advance(ring);
1137 dev_priv->relative_constants_mode = mode;
1140 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1141 ret = i915_reset_gen7_sol_offsets(dev, ring);
1146 exec_start = i915_gem_obj_offset(batch_obj, vm) +
1147 args->batch_start_offset;
1148 exec_len = args->batch_len;
1150 for (i = 0; i < args->num_cliprects; i++) {
1151 ret = i915_emit_box(dev, &cliprects[i],
1152 args->DR1, args->DR4);
1156 ret = ring->dispatch_execbuffer(ring,
1157 exec_start, exec_len,
1163 ret = ring->dispatch_execbuffer(ring,
1164 exec_start, exec_len,
1170 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1172 i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
1173 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
1178 mutex_unlock(&dev->struct_mutex);
1186 * Legacy execbuffer just creates an exec2 list from the original exec object
1187 * list array and passes it to the real function.
1190 i915_gem_execbuffer(struct drm_device *dev, void *data,
1191 struct drm_file *file)
1193 struct drm_i915_private *dev_priv = dev->dev_private;
1194 struct drm_i915_gem_execbuffer *args = data;
1195 struct drm_i915_gem_execbuffer2 exec2;
1196 struct drm_i915_gem_exec_object *exec_list = NULL;
1197 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1200 if (args->buffer_count < 1) {
1201 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
1205 /* Copy in the exec list from userland */
1206 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1207 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1208 if (exec_list == NULL || exec2_list == NULL) {
1209 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
1210 args->buffer_count);
1211 drm_free_large(exec_list);
1212 drm_free_large(exec2_list);
1215 ret = copy_from_user(exec_list,
1216 to_user_ptr(args->buffers_ptr),
1217 sizeof(*exec_list) * args->buffer_count);
1219 DRM_DEBUG("copy %d exec entries failed %d\n",
1220 args->buffer_count, ret);
1221 drm_free_large(exec_list);
1222 drm_free_large(exec2_list);
1226 for (i = 0; i < args->buffer_count; i++) {
1227 exec2_list[i].handle = exec_list[i].handle;
1228 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1229 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1230 exec2_list[i].alignment = exec_list[i].alignment;
1231 exec2_list[i].offset = exec_list[i].offset;
1232 if (INTEL_INFO(dev)->gen < 4)
1233 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1235 exec2_list[i].flags = 0;
1238 exec2.buffers_ptr = args->buffers_ptr;
1239 exec2.buffer_count = args->buffer_count;
1240 exec2.batch_start_offset = args->batch_start_offset;
1241 exec2.batch_len = args->batch_len;
1242 exec2.DR1 = args->DR1;
1243 exec2.DR4 = args->DR4;
1244 exec2.num_cliprects = args->num_cliprects;
1245 exec2.cliprects_ptr = args->cliprects_ptr;
1246 exec2.flags = I915_EXEC_RENDER;
1247 i915_execbuffer2_set_context_id(exec2, 0);
1249 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list,
1250 &dev_priv->gtt.base);
1252 /* Copy the new buffer offsets back to the user's exec list. */
1253 for (i = 0; i < args->buffer_count; i++)
1254 exec_list[i].offset = exec2_list[i].offset;
1255 /* ... and back out to userspace */
1256 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
1258 sizeof(*exec_list) * args->buffer_count);
1261 DRM_DEBUG("failed to copy %d exec entries "
1262 "back to user (%d)\n",
1263 args->buffer_count, ret);
1267 drm_free_large(exec_list);
1268 drm_free_large(exec2_list);
1273 i915_gem_execbuffer2(struct drm_device *dev, void *data,
1274 struct drm_file *file)
1276 struct drm_i915_private *dev_priv = dev->dev_private;
1277 struct drm_i915_gem_execbuffer2 *args = data;
1278 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1281 if (args->buffer_count < 1 ||
1282 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
1283 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
1287 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1288 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
1289 if (exec2_list == NULL)
1290 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1291 args->buffer_count);
1292 if (exec2_list == NULL) {
1293 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
1294 args->buffer_count);
1297 ret = copy_from_user(exec2_list,
1298 to_user_ptr(args->buffers_ptr),
1299 sizeof(*exec2_list) * args->buffer_count);
1301 DRM_DEBUG("copy %d exec entries failed %d\n",
1302 args->buffer_count, ret);
1303 drm_free_large(exec2_list);
1307 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list,
1308 &dev_priv->gtt.base);
1310 /* Copy the new buffer offsets back to the user's exec list. */
1311 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
1313 sizeof(*exec2_list) * args->buffer_count);
1316 DRM_DEBUG("failed to copy %d exec entries "
1317 "back to user (%d)\n",
1318 args->buffer_count, ret);
1322 drm_free_large(exec2_list);