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>
31 #include <linux/highmem.h>
33 #include "intel_drv.h"
37 struct hlist_head buckets[0];
40 static struct eb_objects *
43 struct eb_objects *eb;
44 int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
47 eb = kmalloc(count*sizeof(struct hlist_head) +
48 sizeof(struct eb_objects),
49 M_DRM, M_WAITOK | M_ZERO);
58 eb_reset(struct eb_objects *eb)
60 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
64 eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj)
66 hlist_add_head(&obj->exec_node,
67 &eb->buckets[obj->exec_handle & eb->and]);
70 static struct drm_i915_gem_object *
71 eb_get_object(struct eb_objects *eb, unsigned long handle)
73 struct hlist_head *head;
74 struct hlist_node *node;
75 struct drm_i915_gem_object *obj;
77 head = &eb->buckets[handle & eb->and];
78 hlist_for_each(node, head) {
79 obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
80 if (obj->exec_handle == handle)
88 eb_destroy(struct eb_objects *eb)
93 static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
95 return (obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
96 !obj->map_and_fenceable ||
97 obj->cache_level != I915_CACHE_NONE);
101 i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
102 struct eb_objects *eb,
103 struct drm_i915_gem_relocation_entry *reloc)
105 struct drm_device *dev = obj->base.dev;
106 struct drm_gem_object *target_obj;
107 struct drm_i915_gem_object *target_i915_obj;
108 uint32_t target_offset;
111 /* we've already hold a reference to all valid objects */
112 target_obj = &eb_get_object(eb, reloc->target_handle)->base;
113 if (unlikely(target_obj == NULL))
116 target_i915_obj = to_intel_bo(target_obj);
117 target_offset = target_i915_obj->gtt_offset;
119 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
120 * pipe_control writes because the gpu doesn't properly redirect them
121 * through the ppgtt for non_secure batchbuffers. */
122 if (unlikely(IS_GEN6(dev) &&
123 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
124 !target_i915_obj->has_global_gtt_mapping)) {
125 i915_gem_gtt_bind_object(target_i915_obj,
126 target_i915_obj->cache_level);
129 /* Validate that the target is in a valid r/w GPU domain */
130 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
131 DRM_DEBUG("reloc with multiple write domains: "
132 "obj %p target %d offset %d "
133 "read %08x write %08x",
134 obj, reloc->target_handle,
137 reloc->write_domain);
140 if (unlikely((reloc->write_domain | reloc->read_domains)
141 & ~I915_GEM_GPU_DOMAINS)) {
142 DRM_DEBUG("reloc with read/write non-GPU domains: "
143 "obj %p target %d offset %d "
144 "read %08x write %08x",
145 obj, reloc->target_handle,
148 reloc->write_domain);
151 if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
152 reloc->write_domain != target_obj->pending_write_domain)) {
153 DRM_DEBUG("Write domain conflict: "
154 "obj %p target %d offset %d "
155 "new %08x old %08x\n",
156 obj, reloc->target_handle,
159 target_obj->pending_write_domain);
163 target_obj->pending_read_domains |= reloc->read_domains;
164 target_obj->pending_write_domain |= reloc->write_domain;
166 /* If the relocation already has the right value in it, no
167 * more work needs to be done.
169 if (target_offset == reloc->presumed_offset)
172 /* Check that the relocation address is valid... */
173 if (unlikely(reloc->offset > obj->base.size - 4)) {
174 DRM_DEBUG("Relocation beyond object bounds: "
175 "obj %p target %d offset %d size %d.\n",
176 obj, reloc->target_handle,
178 (int) obj->base.size);
181 if (unlikely(reloc->offset & 3)) {
182 DRM_DEBUG("Relocation not 4-byte aligned: "
183 "obj %p target %d offset %d.\n",
184 obj, reloc->target_handle,
185 (int) reloc->offset);
189 /* We can't wait for rendering with pagefaults disabled */
190 if (obj->active && (curthread->td_flags & TDF_NOFAULT))
193 reloc->delta += target_offset;
194 if (use_cpu_reloc(obj)) {
195 uint32_t page_offset = reloc->offset & PAGE_MASK;
198 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
202 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
203 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
204 kunmap_atomic(vaddr);
206 uint32_t __iomem *reloc_entry;
207 char __iomem *reloc_page;
209 ret = i915_gem_object_set_to_gtt_domain(obj, true);
213 ret = i915_gem_object_put_fence(obj);
217 /* Map the page containing the relocation we're going to perform. */
218 reloc->offset += obj->gtt_offset;
219 reloc_page = pmap_mapdev_attr(dev->agp->base + (reloc->offset &
220 ~PAGE_MASK), PAGE_SIZE, PAT_WRITE_COMBINING);
221 reloc_entry = (uint32_t *)(reloc_page + (reloc->offset &
224 iowrite32(reloc->delta, reloc_entry);
225 pmap_unmapdev((vm_offset_t)reloc_page, PAGE_SIZE);
228 /* and update the user's relocation entry */
229 reloc->presumed_offset = target_offset;
235 i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
236 struct eb_objects *eb)
238 #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
239 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
240 struct drm_i915_gem_relocation_entry __user *user_relocs;
241 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
244 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
246 remain = entry->relocation_count;
248 struct drm_i915_gem_relocation_entry *r = stack_reloc;
250 if (count > ARRAY_SIZE(stack_reloc))
251 count = ARRAY_SIZE(stack_reloc);
254 if (copyin_nofault(user_relocs, r, count*sizeof(r[0])))
258 u64 offset = r->presumed_offset;
260 ret = i915_gem_execbuffer_relocate_entry(obj, eb, r);
264 if (r->presumed_offset != offset &&
265 copyout_nofault(&r->presumed_offset,
266 &user_relocs->presumed_offset,
267 sizeof(r->presumed_offset))) {
281 i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
282 struct eb_objects *eb,
283 struct drm_i915_gem_relocation_entry *relocs)
285 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
288 for (i = 0; i < entry->relocation_count; i++) {
289 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
298 i915_gem_execbuffer_relocate(struct drm_device *dev,
299 struct eb_objects *eb,
300 struct list_head *objects)
302 struct drm_i915_gem_object *obj;
305 /* This is the fast path and we cannot handle a pagefault whilst
306 * holding the struct mutex lest the user pass in the relocations
307 * contained within a mmaped bo. For in such a case we, the page
308 * fault handler would call i915_gem_fault() and we would try to
309 * acquire the struct mutex again. Obviously this is bad and so
310 * lockdep complains vehemently.
315 list_for_each_entry(obj, objects, exec_list) {
316 ret = i915_gem_execbuffer_relocate_object(obj, eb);
327 #define __EXEC_OBJECT_HAS_PIN (1<<31)
328 #define __EXEC_OBJECT_HAS_FENCE (1<<30)
331 need_reloc_mappable(struct drm_i915_gem_object *obj)
333 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
334 return entry->relocation_count && !use_cpu_reloc(obj);
338 i915_gem_execbuffer_reserve_object(struct drm_i915_gem_object *obj,
339 struct intel_ring_buffer *ring)
341 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
342 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
343 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
344 bool need_fence, need_mappable;
348 has_fenced_gpu_access &&
349 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
350 obj->tiling_mode != I915_TILING_NONE;
351 need_mappable = need_fence || need_reloc_mappable(obj);
353 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable, false);
357 entry->flags |= __EXEC_OBJECT_HAS_PIN;
359 if (has_fenced_gpu_access) {
360 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
361 ret = i915_gem_object_get_fence(obj);
365 if (i915_gem_object_pin_fence(obj))
366 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
368 obj->pending_fenced_gpu_access = true;
372 /* Ensure ppgtt mapping exists if needed */
373 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
374 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
375 obj, obj->cache_level);
377 obj->has_aliasing_ppgtt_mapping = 1;
380 entry->offset = obj->gtt_offset;
385 i915_gem_execbuffer_unreserve_object(struct drm_i915_gem_object *obj)
387 struct drm_i915_gem_exec_object2 *entry;
392 entry = obj->exec_entry;
394 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
395 i915_gem_object_unpin_fence(obj);
397 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
398 i915_gem_object_unpin(obj);
400 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
404 i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
405 struct drm_file *file,
406 struct list_head *objects)
408 struct drm_i915_gem_object *obj;
409 struct list_head ordered_objects;
410 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
413 INIT_LIST_HEAD(&ordered_objects);
414 while (!list_empty(objects)) {
415 struct drm_i915_gem_exec_object2 *entry;
416 bool need_fence, need_mappable;
418 obj = list_first_entry(objects,
419 struct drm_i915_gem_object,
421 entry = obj->exec_entry;
424 has_fenced_gpu_access &&
425 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
426 obj->tiling_mode != I915_TILING_NONE;
427 need_mappable = need_fence || need_reloc_mappable(obj);
430 list_move(&obj->exec_list, &ordered_objects);
432 list_move_tail(&obj->exec_list, &ordered_objects);
434 obj->base.pending_read_domains = 0;
435 obj->base.pending_write_domain = 0;
436 obj->pending_fenced_gpu_access = false;
438 list_splice(&ordered_objects, objects);
440 /* Attempt to pin all of the buffers into the GTT.
441 * This is done in 3 phases:
443 * 1a. Unbind all objects that do not match the GTT constraints for
444 * the execbuffer (fenceable, mappable, alignment etc).
445 * 1b. Increment pin count for already bound objects.
446 * 2. Bind new objects.
447 * 3. Decrement pin count.
449 * This avoid unnecessary unbinding of later objects in order to make
450 * room for the earlier objects *unless* we need to defragment.
456 /* Unbind any ill-fitting objects or pin. */
457 list_for_each_entry(obj, objects, exec_list) {
458 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
459 bool need_fence, need_mappable;
465 has_fenced_gpu_access &&
466 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
467 obj->tiling_mode != I915_TILING_NONE;
468 need_mappable = need_fence || need_reloc_mappable(obj);
470 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
471 (need_mappable && !obj->map_and_fenceable))
472 ret = i915_gem_object_unbind(obj);
474 ret = i915_gem_execbuffer_reserve_object(obj, ring);
479 /* Bind fresh objects */
480 list_for_each_entry(obj, objects, exec_list) {
484 ret = i915_gem_execbuffer_reserve_object(obj, ring);
489 err: /* Decrement pin count for bound objects */
490 list_for_each_entry(obj, objects, exec_list)
491 i915_gem_execbuffer_unreserve_object(obj);
493 if (ret != -ENOSPC || retry++)
496 ret = i915_gem_evict_everything(ring->dev);
503 i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
504 struct drm_file *file,
505 struct intel_ring_buffer *ring,
506 struct list_head *objects,
507 struct eb_objects *eb,
508 struct drm_i915_gem_exec_object2 *exec,
511 struct drm_i915_gem_relocation_entry *reloc;
512 struct drm_i915_gem_object *obj;
516 /* We may process another execbuffer during the unlock... */
517 while (!list_empty(objects)) {
518 obj = list_first_entry(objects,
519 struct drm_i915_gem_object,
521 list_del_init(&obj->exec_list);
522 drm_gem_object_unreference(&obj->base);
528 for (i = 0; i < count; i++)
529 total += exec[i].relocation_count;
531 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
532 reloc = drm_malloc_ab(total, sizeof(*reloc));
533 if (reloc == NULL || reloc_offset == NULL) {
534 drm_free_large(reloc);
535 drm_free_large(reloc_offset);
541 for (i = 0; i < count; i++) {
542 struct drm_i915_gem_relocation_entry __user *user_relocs;
543 u64 invalid_offset = (u64)-1;
546 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
548 if (copy_from_user(reloc+total, user_relocs,
549 exec[i].relocation_count * sizeof(*reloc))) {
555 /* As we do not update the known relocation offsets after
556 * relocating (due to the complexities in lock handling),
557 * we need to mark them as invalid now so that we force the
558 * relocation processing next time. Just in case the target
559 * object is evicted and then rebound into its old
560 * presumed_offset before the next execbuffer - if that
561 * happened we would make the mistake of assuming that the
562 * relocations were valid.
564 for (j = 0; j < exec[i].relocation_count; j++) {
565 if (copy_to_user(&user_relocs[j].presumed_offset,
567 sizeof(invalid_offset))) {
574 reloc_offset[i] = total;
575 total += exec[i].relocation_count;
578 ret = i915_mutex_lock_interruptible(dev);
584 /* reacquire the objects */
586 for (i = 0; i < count; i++) {
587 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
589 if (&obj->base == NULL) {
590 DRM_DEBUG("Invalid object handle %d at index %d\n",
596 list_add_tail(&obj->exec_list, objects);
597 obj->exec_handle = exec[i].handle;
598 obj->exec_entry = &exec[i];
599 eb_add_object(eb, obj);
602 ret = i915_gem_execbuffer_reserve(ring, file, objects);
606 list_for_each_entry(obj, objects, exec_list) {
607 int offset = obj->exec_entry - exec;
608 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
609 reloc + reloc_offset[offset]);
614 /* Leave the user relocations as are, this is the painfully slow path,
615 * and we want to avoid the complication of dropping the lock whilst
616 * having buffers reserved in the aperture and so causing spurious
617 * ENOSPC for random operations.
621 drm_free_large(reloc);
622 drm_free_large(reloc_offset);
627 i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
629 u32 plane, flip_mask;
632 /* Check for any pending flips. As we only maintain a flip queue depth
633 * of 1, we can simply insert a WAIT for the next display flip prior
634 * to executing the batch and avoid stalling the CPU.
637 for (plane = 0; flips >> plane; plane++) {
638 if (((flips >> plane) & 1) == 0)
642 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
644 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
646 ret = intel_ring_begin(ring, 2);
650 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
651 intel_ring_emit(ring, MI_NOOP);
652 intel_ring_advance(ring);
659 i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
660 struct list_head *objects)
662 struct drm_i915_gem_object *obj;
663 uint32_t flush_domains = 0;
667 list_for_each_entry(obj, objects, exec_list) {
668 ret = i915_gem_object_sync(obj, ring);
672 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
673 i915_gem_clflush_object(obj);
675 if (obj->base.pending_write_domain)
676 flips |= atomic_read(&obj->pending_flip);
678 flush_domains |= obj->base.write_domain;
682 ret = i915_gem_execbuffer_wait_for_flips(ring, flips);
687 if (flush_domains & I915_GEM_DOMAIN_CPU)
688 i915_gem_chipset_flush(ring->dev);
690 if (flush_domains & I915_GEM_DOMAIN_GTT)
693 /* Unconditionally invalidate gpu caches and ensure that we do flush
694 * any residual writes from the previous batch.
696 return intel_ring_invalidate_all_caches(ring);
700 i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
702 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
706 validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
710 int relocs_total = 0;
711 int relocs_max = INT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
713 for (i = 0; i < count; i++) {
715 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
717 int length; /* limited by fault_in_pages_readable() */
719 /* First check for malicious input causing overflow in
720 * the worst case where we need to allocate the entire
721 * relocation tree as a single array.
723 if (exec[i].relocation_count > relocs_max - relocs_total)
725 relocs_total += exec[i].relocation_count;
727 length = exec[i].relocation_count *
728 sizeof(struct drm_i915_gem_relocation_entry);
730 if (!access_ok(VERIFY_READ, ptr, length))
733 /* we may also need to update the presumed offsets */
734 if (!access_ok(VERIFY_WRITE, ptr, length))
737 if (fault_in_multipages_readable(ptr, length))
746 i915_gem_execbuffer_move_to_active(struct list_head *objects,
747 struct intel_ring_buffer *ring)
749 struct drm_i915_gem_object *obj;
751 list_for_each_entry(obj, objects, exec_list) {
752 obj->base.read_domains = obj->base.pending_read_domains;
753 obj->base.write_domain = obj->base.pending_write_domain;
754 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
756 i915_gem_object_move_to_active(obj, ring);
757 if (obj->base.write_domain) {
759 obj->last_write_seqno = intel_ring_get_seqno(ring);
760 if (obj->pin_count) /* check for potential scanout */
761 intel_mark_fb_busy(obj);
767 i915_gem_execbuffer_retire_commands(struct drm_device *dev,
768 struct drm_file *file,
769 struct intel_ring_buffer *ring)
771 /* Unconditionally force add_request to emit a full flush. */
772 ring->gpu_caches_dirty = true;
774 /* Add a breadcrumb for the completion of the batch buffer */
775 (void)i915_add_request(ring, file, NULL);
779 i915_reset_gen7_sol_offsets(struct drm_device *dev,
780 struct intel_ring_buffer *ring)
782 drm_i915_private_t *dev_priv = dev->dev_private;
785 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
788 ret = intel_ring_begin(ring, 4 * 3);
792 for (i = 0; i < 4; i++) {
793 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
794 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
795 intel_ring_emit(ring, 0);
798 intel_ring_advance(ring);
804 i915_gem_do_execbuffer(struct drm_device *dev, void *data,
805 struct drm_file *file,
806 struct drm_i915_gem_execbuffer2 *args,
807 struct drm_i915_gem_exec_object2 *exec)
809 drm_i915_private_t *dev_priv = dev->dev_private;
810 struct list_head objects;
811 struct eb_objects *eb;
812 struct drm_i915_gem_object *batch_obj;
813 struct drm_clip_rect *cliprects = NULL;
814 struct intel_ring_buffer *ring;
815 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
816 u32 exec_start, exec_len;
821 if (!i915_gem_check_execbuffer(args)) {
822 DRM_DEBUG("execbuf with invalid offset/length\n");
826 ret = validate_exec_list(exec, args->buffer_count);
831 if (args->flags & I915_EXEC_SECURE) {
832 flags |= I915_DISPATCH_SECURE;
834 if (args->flags & I915_EXEC_IS_PINNED)
835 flags |= I915_DISPATCH_PINNED;
837 switch (args->flags & I915_EXEC_RING_MASK) {
838 case I915_EXEC_DEFAULT:
839 case I915_EXEC_RENDER:
840 ring = &dev_priv->ring[RCS];
843 ring = &dev_priv->ring[VCS];
845 DRM_DEBUG("Ring %s doesn't support contexts\n",
851 ring = &dev_priv->ring[BCS];
853 DRM_DEBUG("Ring %s doesn't support contexts\n",
859 DRM_DEBUG("execbuf with unknown ring: %d\n",
860 (int)(args->flags & I915_EXEC_RING_MASK));
863 if (!intel_ring_initialized(ring)) {
864 DRM_DEBUG("execbuf with invalid ring: %d\n",
865 (int)(args->flags & I915_EXEC_RING_MASK));
869 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
870 mask = I915_EXEC_CONSTANTS_MASK;
872 case I915_EXEC_CONSTANTS_REL_GENERAL:
873 case I915_EXEC_CONSTANTS_ABSOLUTE:
874 case I915_EXEC_CONSTANTS_REL_SURFACE:
875 if (ring == &dev_priv->ring[RCS] &&
876 mode != dev_priv->relative_constants_mode) {
877 if (INTEL_INFO(dev)->gen < 4)
880 if (INTEL_INFO(dev)->gen > 5 &&
881 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
884 /* The HW changed the meaning on this bit on gen6 */
885 if (INTEL_INFO(dev)->gen >= 6)
886 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
890 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
894 if (args->buffer_count < 1) {
895 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
899 if (args->num_cliprects != 0) {
900 if (ring != &dev_priv->ring[RCS]) {
901 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
905 if (INTEL_INFO(dev)->gen >= 5) {
906 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
910 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
911 DRM_DEBUG("execbuf with %u cliprects\n",
912 args->num_cliprects);
915 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
916 M_DRM, M_WAITOK | M_ZERO);
917 if (cliprects == NULL) {
922 if (copy_from_user(cliprects,
923 (struct drm_clip_rect __user *)(uintptr_t)
925 sizeof(*cliprects)*args->num_cliprects)) {
931 ret = i915_mutex_lock_interruptible(dev);
935 if (dev_priv->mm.suspended) {
941 eb = eb_create(args->buffer_count);
948 /* Look up object handles */
949 INIT_LIST_HEAD(&objects);
950 for (i = 0; i < args->buffer_count; i++) {
951 struct drm_i915_gem_object *obj;
953 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
955 if (&obj->base == NULL) {
956 DRM_DEBUG("Invalid object handle %d at index %d\n",
958 /* prevent error path from reading uninitialized data */
963 if (!list_empty(&obj->exec_list)) {
964 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
965 obj, exec[i].handle, i);
970 list_add_tail(&obj->exec_list, &objects);
971 obj->exec_handle = exec[i].handle;
972 obj->exec_entry = &exec[i];
973 eb_add_object(eb, obj);
976 /* take note of the batch buffer before we might reorder the lists */
977 batch_obj = list_entry(objects.prev,
978 struct drm_i915_gem_object,
981 /* Move the objects en-masse into the GTT, evicting if necessary. */
982 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
986 /* The objects are in their final locations, apply the relocations. */
987 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
989 if (ret == -EFAULT) {
990 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
994 DRM_LOCK_ASSERT(dev);
1000 /* Set the pending read domains for the batch buffer to COMMAND */
1001 if (batch_obj->base.pending_write_domain) {
1002 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
1006 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1008 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1009 * batch" bit. Hence we need to pin secure batches into the global gtt.
1010 * hsw should have this fixed, but let's be paranoid and do it
1011 * unconditionally for now. */
1012 if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
1013 i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
1015 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
1019 ret = i915_switch_context(ring, file, ctx_id);
1023 if (ring == &dev_priv->ring[RCS] &&
1024 mode != dev_priv->relative_constants_mode) {
1025 ret = intel_ring_begin(ring, 4);
1029 intel_ring_emit(ring, MI_NOOP);
1030 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1031 intel_ring_emit(ring, INSTPM);
1032 intel_ring_emit(ring, mask << 16 | mode);
1033 intel_ring_advance(ring);
1035 dev_priv->relative_constants_mode = mode;
1038 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1039 ret = i915_reset_gen7_sol_offsets(dev, ring);
1044 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1045 exec_len = args->batch_len;
1047 for (i = 0; i < args->num_cliprects; i++) {
1048 ret = i915_emit_box(dev, &cliprects[i],
1049 args->DR1, args->DR4);
1053 ret = ring->dispatch_execbuffer(ring,
1054 exec_start, exec_len,
1060 ret = ring->dispatch_execbuffer(ring,
1061 exec_start, exec_len,
1067 i915_gem_execbuffer_move_to_active(&objects, ring);
1068 i915_gem_execbuffer_retire_commands(dev, file, ring);
1072 while (!list_empty(&objects)) {
1073 struct drm_i915_gem_object *obj;
1075 obj = list_first_entry(&objects,
1076 struct drm_i915_gem_object,
1078 list_del_init(&obj->exec_list);
1079 drm_gem_object_unreference(&obj->base);
1085 drm_free(cliprects, M_DRM);
1090 * Legacy execbuffer just creates an exec2 list from the original exec object
1091 * list array and passes it to the real function.
1094 i915_gem_execbuffer(struct drm_device *dev, void *data,
1095 struct drm_file *file)
1097 struct drm_i915_gem_execbuffer *args = data;
1098 struct drm_i915_gem_execbuffer2 exec2;
1099 struct drm_i915_gem_exec_object *exec_list = NULL;
1100 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1103 if (args->buffer_count < 1) {
1104 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
1108 /* Copy in the exec list from userland */
1109 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1110 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1111 if (exec_list == NULL || exec2_list == NULL) {
1112 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
1113 args->buffer_count);
1114 drm_free_large(exec_list);
1115 drm_free_large(exec2_list);
1119 ret = copy_from_user(exec_list,
1120 (void __user *)(uintptr_t)args->buffers_ptr,
1121 sizeof(*exec_list) * args->buffer_count);
1123 DRM_DEBUG("copy %d exec entries failed %d\n",
1124 args->buffer_count, ret);
1125 drm_free_large(exec_list);
1126 drm_free_large(exec2_list);
1130 for (i = 0; i < args->buffer_count; i++) {
1131 exec2_list[i].handle = exec_list[i].handle;
1132 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1133 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1134 exec2_list[i].alignment = exec_list[i].alignment;
1135 exec2_list[i].offset = exec_list[i].offset;
1136 if (INTEL_INFO(dev)->gen < 4)
1137 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1139 exec2_list[i].flags = 0;
1142 exec2.buffers_ptr = args->buffers_ptr;
1143 exec2.buffer_count = args->buffer_count;
1144 exec2.batch_start_offset = args->batch_start_offset;
1145 exec2.batch_len = args->batch_len;
1146 exec2.DR1 = args->DR1;
1147 exec2.DR4 = args->DR4;
1148 exec2.num_cliprects = args->num_cliprects;
1149 exec2.cliprects_ptr = args->cliprects_ptr;
1150 exec2.flags = I915_EXEC_RENDER;
1151 i915_execbuffer2_set_context_id(exec2, 0);
1153 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1155 /* Copy the new buffer offsets back to the user's exec list. */
1156 for (i = 0; i < args->buffer_count; i++)
1157 exec_list[i].offset = exec2_list[i].offset;
1158 /* ... and back out to userspace */
1159 ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
1161 sizeof(*exec_list) * args->buffer_count);
1164 DRM_DEBUG("failed to copy %d exec entries "
1165 "back to user (%d)\n",
1166 args->buffer_count, ret);
1170 drm_free_large(exec_list);
1171 drm_free_large(exec2_list);
1176 i915_gem_execbuffer2(struct drm_device *dev, void *data,
1177 struct drm_file *file)
1179 struct drm_i915_gem_execbuffer2 *args = data;
1180 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1183 if (args->buffer_count < 1 ||
1184 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
1185 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
1189 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1191 if (exec2_list == NULL)
1192 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1193 args->buffer_count);
1194 if (exec2_list == NULL) {
1195 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
1196 args->buffer_count);
1199 ret = copy_from_user(exec2_list,
1200 (struct drm_i915_relocation_entry __user *)
1201 (uintptr_t) args->buffers_ptr,
1202 sizeof(*exec2_list) * args->buffer_count);
1204 DRM_DEBUG("copy %d exec entries failed %d\n",
1205 args->buffer_count, ret);
1206 drm_free_large(exec2_list);
1210 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1212 /* Copy the new buffer offsets back to the user's exec list. */
1213 ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
1215 sizeof(*exec2_list) * args->buffer_count);
1218 DRM_DEBUG("failed to copy %d exec entries "
1219 "back to user (%d)\n",
1220 args->buffer_count, ret);
1224 drm_free_large(exec2_list);