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>
36 #define __EXEC_OBJECT_HAS_PIN (1<<31)
37 #define __EXEC_OBJECT_HAS_FENCE (1<<30)
40 struct list_head vmas
;
43 struct i915_vma
*lut
[0];
44 struct hlist_head buckets
[0];
48 static struct eb_vmas
*
49 eb_create(struct drm_i915_gem_execbuffer2
*args
, struct i915_address_space
*vm
)
51 struct eb_vmas
*eb
= NULL
;
53 if (args
->flags
& I915_EXEC_HANDLE_LUT
) {
54 unsigned size
= args
->buffer_count
;
55 size
*= sizeof(struct i915_vma
*);
56 size
+= sizeof(struct eb_vmas
);
57 eb
= kmalloc(size
, GFP_TEMPORARY
| __GFP_NOWARN
| __GFP_NORETRY
);
61 unsigned size
= args
->buffer_count
;
62 unsigned count
= PAGE_SIZE
/ sizeof(struct hlist_head
) / 2;
63 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE
/ sizeof(struct hlist_head
));
64 while (count
> 2*size
)
66 eb
= kzalloc(count
*sizeof(struct hlist_head
) +
67 sizeof(struct eb_vmas
),
74 eb
->and = -args
->buffer_count
;
76 INIT_LIST_HEAD(&eb
->vmas
);
81 eb_reset(struct eb_vmas
*eb
)
84 memset(eb
->buckets
, 0, (eb
->and+1)*sizeof(struct hlist_head
));
88 eb_lookup_vmas(struct eb_vmas
*eb
,
89 struct drm_i915_gem_exec_object2
*exec
,
90 const struct drm_i915_gem_execbuffer2
*args
,
91 struct i915_address_space
*vm
,
92 struct drm_file
*file
)
94 struct drm_i915_gem_object
*obj
;
95 struct list_head objects
;
98 INIT_LIST_HEAD(&objects
);
99 spin_lock(&file
->table_lock
);
100 /* Grab a reference to the object and release the lock so we can lookup
101 * or create the VMA without using GFP_ATOMIC */
102 for (i
= 0; i
< args
->buffer_count
; i
++) {
103 obj
= to_intel_bo(idr_find(&file
->object_idr
, exec
[i
].handle
));
105 spin_unlock(&file
->table_lock
);
106 DRM_DEBUG("Invalid object handle %d at index %d\n",
112 if (!list_empty(&obj
->obj_exec_link
)) {
113 spin_unlock(&file
->table_lock
);
114 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
115 obj
, exec
[i
].handle
, i
);
120 drm_gem_object_reference(&obj
->base
);
121 list_add_tail(&obj
->obj_exec_link
, &objects
);
123 spin_unlock(&file
->table_lock
);
126 while (!list_empty(&objects
)) {
127 struct i915_vma
*vma
;
129 obj
= list_first_entry(&objects
,
130 struct drm_i915_gem_object
,
134 * NOTE: We can leak any vmas created here when something fails
135 * later on. But that's no issue since vma_unbind can deal with
136 * vmas which are not actually bound. And since only
137 * lookup_or_create exists as an interface to get at the vma
138 * from the (obj, vm) we don't run the risk of creating
139 * duplicated vmas for the same vm.
141 vma
= i915_gem_obj_lookup_or_create_vma(obj
, vm
);
143 DRM_DEBUG("Failed to lookup VMA\n");
148 /* Transfer ownership from the objects list to the vmas list. */
149 list_add_tail(&vma
->exec_list
, &eb
->vmas
);
150 list_del_init(&obj
->obj_exec_link
);
152 vma
->exec_entry
= &exec
[i
];
156 uint32_t handle
= args
->flags
& I915_EXEC_HANDLE_LUT
? i
: exec
[i
].handle
;
157 vma
->exec_handle
= handle
;
158 hlist_add_head(&vma
->exec_node
,
159 &eb
->buckets
[handle
& eb
->and]);
168 while (!list_empty(&objects
)) {
169 obj
= list_first_entry(&objects
,
170 struct drm_i915_gem_object
,
172 list_del_init(&obj
->obj_exec_link
);
173 drm_gem_object_unreference(&obj
->base
);
176 * Objects already transfered to the vmas list will be unreferenced by
183 static struct i915_vma
*eb_get_vma(struct eb_vmas
*eb
, unsigned long handle
)
186 if (handle
>= -eb
->and)
188 return eb
->lut
[handle
];
190 struct hlist_head
*head
;
191 struct hlist_node
*node
;
193 head
= &eb
->buckets
[handle
& eb
->and];
194 hlist_for_each(node
, head
) {
195 struct i915_vma
*vma
;
197 vma
= hlist_entry(node
, struct i915_vma
, exec_node
);
198 if (vma
->exec_handle
== handle
)
206 i915_gem_execbuffer_unreserve_vma(struct i915_vma
*vma
)
208 struct drm_i915_gem_exec_object2
*entry
;
209 struct drm_i915_gem_object
*obj
= vma
->obj
;
211 if (!drm_mm_node_allocated(&vma
->node
))
214 entry
= vma
->exec_entry
;
216 if (entry
->flags
& __EXEC_OBJECT_HAS_FENCE
)
217 i915_gem_object_unpin_fence(obj
);
219 if (entry
->flags
& __EXEC_OBJECT_HAS_PIN
)
220 i915_gem_object_unpin(obj
);
222 entry
->flags
&= ~(__EXEC_OBJECT_HAS_FENCE
| __EXEC_OBJECT_HAS_PIN
);
225 static void eb_destroy(struct eb_vmas
*eb
)
227 while (!list_empty(&eb
->vmas
)) {
228 struct i915_vma
*vma
;
230 vma
= list_first_entry(&eb
->vmas
,
233 list_del_init(&vma
->exec_list
);
234 i915_gem_execbuffer_unreserve_vma(vma
);
235 drm_gem_object_unreference(&vma
->obj
->base
);
240 static inline int use_cpu_reloc(struct drm_i915_gem_object
*obj
)
242 return (HAS_LLC(obj
->base
.dev
) ||
243 obj
->base
.write_domain
== I915_GEM_DOMAIN_CPU
||
244 !obj
->map_and_fenceable
||
245 obj
->cache_level
!= I915_CACHE_NONE
);
249 relocate_entry_cpu(struct drm_i915_gem_object
*obj
,
250 struct drm_i915_gem_relocation_entry
*reloc
)
252 struct drm_device
*dev
= obj
->base
.dev
;
253 uint32_t page_offset
= offset_in_page(reloc
->offset
);
257 ret
= i915_gem_object_set_to_cpu_domain(obj
, true);
261 vaddr
= kmap_atomic(i915_gem_object_get_page(obj
,
262 reloc
->offset
>> PAGE_SHIFT
));
263 *(uint32_t *)(vaddr
+ page_offset
) = reloc
->delta
;
265 if (INTEL_INFO(dev
)->gen
>= 8) {
266 page_offset
= offset_in_page(page_offset
+ sizeof(uint32_t));
268 if (page_offset
== 0) {
269 kunmap_atomic(vaddr
);
270 vaddr
= kmap_atomic(i915_gem_object_get_page(obj
,
271 (reloc
->offset
+ sizeof(uint32_t)) >> PAGE_SHIFT
));
274 *(uint32_t *)(vaddr
+ page_offset
) = 0;
277 kunmap_atomic(vaddr
);
283 relocate_entry_gtt(struct drm_i915_gem_object
*obj
,
284 struct drm_i915_gem_relocation_entry
*reloc
)
286 struct drm_device
*dev
= obj
->base
.dev
;
287 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
288 uint32_t __iomem
*reloc_entry
;
289 void __iomem
*reloc_page
;
292 ret
= i915_gem_object_set_to_gtt_domain(obj
, true);
296 ret
= i915_gem_object_put_fence(obj
);
300 /* Map the page containing the relocation we're going to perform. */
301 reloc
->offset
+= i915_gem_obj_ggtt_offset(obj
);
302 reloc_page
= io_mapping_map_atomic_wc(dev_priv
->gtt
.mappable
,
303 reloc
->offset
& PAGE_MASK
);
304 reloc_entry
= (uint32_t __iomem
*)
305 (reloc_page
+ offset_in_page(reloc
->offset
));
306 iowrite32(reloc
->delta
, reloc_entry
);
308 if (INTEL_INFO(dev
)->gen
>= 8) {
311 if (offset_in_page(reloc
->offset
+ sizeof(uint32_t)) == 0) {
312 io_mapping_unmap_atomic(reloc_page
);
313 reloc_page
= io_mapping_map_atomic_wc(
314 dev_priv
->gtt
.mappable
,
315 reloc
->offset
+ sizeof(uint32_t));
316 reloc_entry
= reloc_page
;
319 iowrite32(0, reloc_entry
);
322 io_mapping_unmap_atomic(reloc_page
);
328 i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object
*obj
,
330 struct drm_i915_gem_relocation_entry
*reloc
,
331 struct i915_address_space
*vm
)
333 struct drm_device
*dev
= obj
->base
.dev
;
334 struct drm_gem_object
*target_obj
;
335 struct drm_i915_gem_object
*target_i915_obj
;
336 struct i915_vma
*target_vma
;
337 uint32_t target_offset
;
340 /* we've already hold a reference to all valid objects */
341 target_vma
= eb_get_vma(eb
, reloc
->target_handle
);
342 if (unlikely(target_vma
== NULL
))
344 target_i915_obj
= target_vma
->obj
;
345 target_obj
= &target_vma
->obj
->base
;
347 target_offset
= i915_gem_obj_ggtt_offset(target_i915_obj
);
349 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
350 * pipe_control writes because the gpu doesn't properly redirect them
351 * through the ppgtt for non_secure batchbuffers. */
352 if (unlikely(IS_GEN6(dev
) &&
353 reloc
->write_domain
== I915_GEM_DOMAIN_INSTRUCTION
&&
354 !target_i915_obj
->has_global_gtt_mapping
)) {
355 i915_gem_gtt_bind_object(target_i915_obj
,
356 target_i915_obj
->cache_level
);
359 /* Validate that the target is in a valid r/w GPU domain */
360 if (unlikely(reloc
->write_domain
& (reloc
->write_domain
- 1))) {
361 DRM_DEBUG("reloc with multiple write domains: "
362 "obj %p target %d offset %d "
363 "read %08x write %08x",
364 obj
, reloc
->target_handle
,
367 reloc
->write_domain
);
370 if (unlikely((reloc
->write_domain
| reloc
->read_domains
)
371 & ~I915_GEM_GPU_DOMAINS
)) {
372 DRM_DEBUG("reloc with read/write non-GPU domains: "
373 "obj %p target %d offset %d "
374 "read %08x write %08x",
375 obj
, reloc
->target_handle
,
378 reloc
->write_domain
);
382 target_obj
->pending_read_domains
|= reloc
->read_domains
;
383 target_obj
->pending_write_domain
|= reloc
->write_domain
;
385 /* If the relocation already has the right value in it, no
386 * more work needs to be done.
388 if (target_offset
== reloc
->presumed_offset
)
391 /* Check that the relocation address is valid... */
392 if (unlikely(reloc
->offset
>
393 obj
->base
.size
- (INTEL_INFO(dev
)->gen
>= 8 ? 8 : 4))) {
394 DRM_DEBUG("Relocation beyond object bounds: "
395 "obj %p target %d offset %d size %d.\n",
396 obj
, reloc
->target_handle
,
398 (int) obj
->base
.size
);
401 if (unlikely(reloc
->offset
& 3)) {
402 DRM_DEBUG("Relocation not 4-byte aligned: "
403 "obj %p target %d offset %d.\n",
404 obj
, reloc
->target_handle
,
405 (int) reloc
->offset
);
409 /* We can't wait for rendering with pagefaults disabled */
410 if (obj
->active
&& in_atomic())
413 reloc
->delta
+= target_offset
;
414 if (use_cpu_reloc(obj
))
415 ret
= relocate_entry_cpu(obj
, reloc
);
417 ret
= relocate_entry_gtt(obj
, reloc
);
422 /* and update the user's relocation entry */
423 reloc
->presumed_offset
= target_offset
;
429 i915_gem_execbuffer_relocate_vma(struct i915_vma
*vma
,
432 #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
433 struct drm_i915_gem_relocation_entry stack_reloc
[N_RELOC(512)];
434 struct drm_i915_gem_relocation_entry __user
*user_relocs
;
435 struct drm_i915_gem_exec_object2
*entry
= vma
->exec_entry
;
438 user_relocs
= to_user_ptr(entry
->relocs_ptr
);
440 remain
= entry
->relocation_count
;
442 struct drm_i915_gem_relocation_entry
*r
= stack_reloc
;
444 if (count
> ARRAY_SIZE(stack_reloc
))
445 count
= ARRAY_SIZE(stack_reloc
);
448 if (__copy_from_user_inatomic(r
, user_relocs
, count
*sizeof(r
[0])))
452 u64 offset
= r
->presumed_offset
;
454 ret
= i915_gem_execbuffer_relocate_entry(vma
->obj
, eb
, r
,
459 if (r
->presumed_offset
!= offset
&&
460 __copy_to_user_inatomic(&user_relocs
->presumed_offset
,
462 sizeof(r
->presumed_offset
))) {
476 i915_gem_execbuffer_relocate_vma_slow(struct i915_vma
*vma
,
478 struct drm_i915_gem_relocation_entry
*relocs
)
480 const struct drm_i915_gem_exec_object2
*entry
= vma
->exec_entry
;
483 for (i
= 0; i
< entry
->relocation_count
; i
++) {
484 ret
= i915_gem_execbuffer_relocate_entry(vma
->obj
, eb
, &relocs
[i
],
494 i915_gem_execbuffer_relocate(struct eb_vmas
*eb
,
495 struct i915_address_space
*vm
)
497 struct i915_vma
*vma
;
500 /* This is the fast path and we cannot handle a pagefault whilst
501 * holding the struct mutex lest the user pass in the relocations
502 * contained within a mmaped bo. For in such a case we, the page
503 * fault handler would call i915_gem_fault() and we would try to
504 * acquire the struct mutex again. Obviously this is bad and so
505 * lockdep complains vehemently.
508 list_for_each_entry(vma
, &eb
->vmas
, exec_list
) {
509 ret
= i915_gem_execbuffer_relocate_vma(vma
, eb
);
519 need_reloc_mappable(struct i915_vma
*vma
)
521 struct drm_i915_gem_exec_object2
*entry
= vma
->exec_entry
;
522 return entry
->relocation_count
&& !use_cpu_reloc(vma
->obj
) &&
523 i915_is_ggtt(vma
->vm
);
527 i915_gem_execbuffer_reserve_vma(struct i915_vma
*vma
,
528 struct intel_ring_buffer
*ring
,
531 struct drm_i915_private
*dev_priv
= ring
->dev
->dev_private
;
532 struct drm_i915_gem_exec_object2
*entry
= vma
->exec_entry
;
533 bool has_fenced_gpu_access
= INTEL_INFO(ring
->dev
)->gen
< 4;
534 bool need_fence
, need_mappable
;
535 struct drm_i915_gem_object
*obj
= vma
->obj
;
539 has_fenced_gpu_access
&&
540 entry
->flags
& EXEC_OBJECT_NEEDS_FENCE
&&
541 obj
->tiling_mode
!= I915_TILING_NONE
;
542 need_mappable
= need_fence
|| need_reloc_mappable(vma
);
544 ret
= i915_gem_object_pin(obj
, vma
->vm
, entry
->alignment
, need_mappable
,
549 entry
->flags
|= __EXEC_OBJECT_HAS_PIN
;
551 if (has_fenced_gpu_access
) {
552 if (entry
->flags
& EXEC_OBJECT_NEEDS_FENCE
) {
553 ret
= i915_gem_object_get_fence(obj
);
557 if (i915_gem_object_pin_fence(obj
))
558 entry
->flags
|= __EXEC_OBJECT_HAS_FENCE
;
560 obj
->pending_fenced_gpu_access
= true;
564 /* Ensure ppgtt mapping exists if needed */
565 if (dev_priv
->mm
.aliasing_ppgtt
&& !obj
->has_aliasing_ppgtt_mapping
) {
566 i915_ppgtt_bind_object(dev_priv
->mm
.aliasing_ppgtt
,
567 obj
, obj
->cache_level
);
569 obj
->has_aliasing_ppgtt_mapping
= 1;
572 if (entry
->offset
!= vma
->node
.start
) {
573 entry
->offset
= vma
->node
.start
;
577 if (entry
->flags
& EXEC_OBJECT_WRITE
) {
578 obj
->base
.pending_read_domains
= I915_GEM_DOMAIN_RENDER
;
579 obj
->base
.pending_write_domain
= I915_GEM_DOMAIN_RENDER
;
582 if (entry
->flags
& EXEC_OBJECT_NEEDS_GTT
&&
583 !obj
->has_global_gtt_mapping
)
584 i915_gem_gtt_bind_object(obj
, obj
->cache_level
);
590 i915_gem_execbuffer_reserve(struct intel_ring_buffer
*ring
,
591 struct list_head
*vmas
,
594 struct drm_i915_gem_object
*obj
;
595 struct i915_vma
*vma
;
596 struct i915_address_space
*vm
;
597 struct list_head ordered_vmas
;
598 bool has_fenced_gpu_access
= INTEL_INFO(ring
->dev
)->gen
< 4;
601 if (list_empty(vmas
))
604 vm
= list_first_entry(vmas
, struct i915_vma
, exec_list
)->vm
;
606 INIT_LIST_HEAD(&ordered_vmas
);
607 while (!list_empty(vmas
)) {
608 struct drm_i915_gem_exec_object2
*entry
;
609 bool need_fence
, need_mappable
;
611 vma
= list_first_entry(vmas
, struct i915_vma
, exec_list
);
613 entry
= vma
->exec_entry
;
616 has_fenced_gpu_access
&&
617 entry
->flags
& EXEC_OBJECT_NEEDS_FENCE
&&
618 obj
->tiling_mode
!= I915_TILING_NONE
;
619 need_mappable
= need_fence
|| need_reloc_mappable(vma
);
622 list_move(&vma
->exec_list
, &ordered_vmas
);
624 list_move_tail(&vma
->exec_list
, &ordered_vmas
);
626 obj
->base
.pending_read_domains
= I915_GEM_GPU_DOMAINS
& ~I915_GEM_DOMAIN_COMMAND
;
627 obj
->base
.pending_write_domain
= 0;
628 obj
->pending_fenced_gpu_access
= false;
630 list_splice(&ordered_vmas
, vmas
);
632 /* Attempt to pin all of the buffers into the GTT.
633 * This is done in 3 phases:
635 * 1a. Unbind all objects that do not match the GTT constraints for
636 * the execbuffer (fenceable, mappable, alignment etc).
637 * 1b. Increment pin count for already bound objects.
638 * 2. Bind new objects.
639 * 3. Decrement pin count.
641 * This avoid unnecessary unbinding of later objects in order to make
642 * room for the earlier objects *unless* we need to defragment.
648 /* Unbind any ill-fitting objects or pin. */
649 list_for_each_entry(vma
, vmas
, exec_list
) {
650 struct drm_i915_gem_exec_object2
*entry
= vma
->exec_entry
;
651 bool need_fence
, need_mappable
;
655 if (!drm_mm_node_allocated(&vma
->node
))
659 has_fenced_gpu_access
&&
660 entry
->flags
& EXEC_OBJECT_NEEDS_FENCE
&&
661 obj
->tiling_mode
!= I915_TILING_NONE
;
662 need_mappable
= need_fence
|| need_reloc_mappable(vma
);
664 WARN_ON((need_mappable
|| need_fence
) &&
665 !i915_is_ggtt(vma
->vm
));
667 if ((entry
->alignment
&&
668 vma
->node
.start
& (entry
->alignment
- 1)) ||
669 (need_mappable
&& !obj
->map_and_fenceable
))
670 ret
= i915_vma_unbind(vma
);
672 ret
= i915_gem_execbuffer_reserve_vma(vma
, ring
, need_relocs
);
677 /* Bind fresh objects */
678 list_for_each_entry(vma
, vmas
, exec_list
) {
679 if (drm_mm_node_allocated(&vma
->node
))
682 ret
= i915_gem_execbuffer_reserve_vma(vma
, ring
, need_relocs
);
688 if (ret
!= -ENOSPC
|| retry
++)
691 /* Decrement pin count for bound objects */
692 list_for_each_entry(vma
, vmas
, exec_list
)
693 i915_gem_execbuffer_unreserve_vma(vma
);
695 ret
= i915_gem_evict_vm(vm
, true);
702 i915_gem_execbuffer_relocate_slow(struct drm_device
*dev
,
703 struct drm_i915_gem_execbuffer2
*args
,
704 struct drm_file
*file
,
705 struct intel_ring_buffer
*ring
,
707 struct drm_i915_gem_exec_object2
*exec
)
709 struct drm_i915_gem_relocation_entry
*reloc
;
710 struct i915_address_space
*vm
;
711 struct i915_vma
*vma
;
715 unsigned count
= args
->buffer_count
;
717 if (WARN_ON(list_empty(&eb
->vmas
)))
720 vm
= list_first_entry(&eb
->vmas
, struct i915_vma
, exec_list
)->vm
;
722 /* We may process another execbuffer during the unlock... */
723 while (!list_empty(&eb
->vmas
)) {
724 vma
= list_first_entry(&eb
->vmas
, struct i915_vma
, exec_list
);
725 list_del_init(&vma
->exec_list
);
726 i915_gem_execbuffer_unreserve_vma(vma
);
727 drm_gem_object_unreference(&vma
->obj
->base
);
730 mutex_unlock(&dev
->struct_mutex
);
733 for (i
= 0; i
< count
; i
++)
734 total
+= exec
[i
].relocation_count
;
736 reloc_offset
= drm_malloc_ab(count
, sizeof(*reloc_offset
));
737 reloc
= drm_malloc_ab(total
, sizeof(*reloc
));
738 if (reloc
== NULL
|| reloc_offset
== NULL
) {
739 drm_free_large(reloc
);
740 drm_free_large(reloc_offset
);
741 mutex_lock(&dev
->struct_mutex
);
746 for (i
= 0; i
< count
; i
++) {
747 struct drm_i915_gem_relocation_entry __user
*user_relocs
;
748 u64 invalid_offset
= (u64
)-1;
751 user_relocs
= to_user_ptr(exec
[i
].relocs_ptr
);
753 if (copy_from_user(reloc
+total
, user_relocs
,
754 exec
[i
].relocation_count
* sizeof(*reloc
))) {
756 mutex_lock(&dev
->struct_mutex
);
760 /* As we do not update the known relocation offsets after
761 * relocating (due to the complexities in lock handling),
762 * we need to mark them as invalid now so that we force the
763 * relocation processing next time. Just in case the target
764 * object is evicted and then rebound into its old
765 * presumed_offset before the next execbuffer - if that
766 * happened we would make the mistake of assuming that the
767 * relocations were valid.
769 for (j
= 0; j
< exec
[i
].relocation_count
; j
++) {
770 if (copy_to_user(&user_relocs
[j
].presumed_offset
,
772 sizeof(invalid_offset
))) {
774 mutex_lock(&dev
->struct_mutex
);
779 reloc_offset
[i
] = total
;
780 total
+= exec
[i
].relocation_count
;
783 ret
= i915_mutex_lock_interruptible(dev
);
785 mutex_lock(&dev
->struct_mutex
);
789 /* reacquire the objects */
791 ret
= eb_lookup_vmas(eb
, exec
, args
, vm
, file
);
795 need_relocs
= (args
->flags
& I915_EXEC_NO_RELOC
) == 0;
796 ret
= i915_gem_execbuffer_reserve(ring
, &eb
->vmas
, &need_relocs
);
800 list_for_each_entry(vma
, &eb
->vmas
, exec_list
) {
801 int offset
= vma
->exec_entry
- exec
;
802 ret
= i915_gem_execbuffer_relocate_vma_slow(vma
, eb
,
803 reloc
+ reloc_offset
[offset
]);
808 /* Leave the user relocations as are, this is the painfully slow path,
809 * and we want to avoid the complication of dropping the lock whilst
810 * having buffers reserved in the aperture and so causing spurious
811 * ENOSPC for random operations.
815 drm_free_large(reloc
);
816 drm_free_large(reloc_offset
);
821 i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer
*ring
,
822 struct list_head
*vmas
)
824 struct i915_vma
*vma
;
825 uint32_t flush_domains
= 0;
826 bool flush_chipset
= false;
829 list_for_each_entry(vma
, vmas
, exec_list
) {
830 struct drm_i915_gem_object
*obj
= vma
->obj
;
831 ret
= i915_gem_object_sync(obj
, ring
);
835 if (obj
->base
.write_domain
& I915_GEM_DOMAIN_CPU
)
836 flush_chipset
|= i915_gem_clflush_object(obj
, false);
838 flush_domains
|= obj
->base
.write_domain
;
842 i915_gem_chipset_flush(ring
->dev
);
844 if (flush_domains
& I915_GEM_DOMAIN_GTT
)
847 /* Unconditionally invalidate gpu caches and ensure that we do flush
848 * any residual writes from the previous batch.
850 return intel_ring_invalidate_all_caches(ring
);
854 i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2
*exec
)
856 if (exec
->flags
& __I915_EXEC_UNKNOWN_FLAGS
)
859 return ((exec
->batch_start_offset
| exec
->batch_len
) & 0x7) == 0;
863 validate_exec_list(struct drm_i915_gem_exec_object2
*exec
,
867 unsigned relocs_total
= 0;
868 unsigned relocs_max
= UINT_MAX
/ sizeof(struct drm_i915_gem_relocation_entry
);
870 for (i
= 0; i
< count
; i
++) {
871 char __user
*ptr
= to_user_ptr(exec
[i
].relocs_ptr
);
872 int length
; /* limited by fault_in_pages_readable() */
874 if (exec
[i
].flags
& __EXEC_OBJECT_UNKNOWN_FLAGS
)
877 /* First check for malicious input causing overflow in
878 * the worst case where we need to allocate the entire
879 * relocation tree as a single array.
881 if (exec
[i
].relocation_count
> relocs_max
- relocs_total
)
883 relocs_total
+= exec
[i
].relocation_count
;
885 length
= exec
[i
].relocation_count
*
886 sizeof(struct drm_i915_gem_relocation_entry
);
888 * We must check that the entire relocation array is safe
889 * to read, but since we may need to update the presumed
890 * offsets during execution, check for full write access.
892 if (!access_ok(VERIFY_WRITE
, ptr
, length
))
895 if (likely(!i915_prefault_disable
)) {
896 if (fault_in_multipages_readable(ptr
, length
))
905 i915_gem_execbuffer_move_to_active(struct list_head
*vmas
,
906 struct intel_ring_buffer
*ring
)
908 struct i915_vma
*vma
;
910 list_for_each_entry(vma
, vmas
, exec_list
) {
911 struct drm_i915_gem_object
*obj
= vma
->obj
;
912 u32 old_read
= obj
->base
.read_domains
;
913 u32 old_write
= obj
->base
.write_domain
;
915 obj
->base
.write_domain
= obj
->base
.pending_write_domain
;
916 if (obj
->base
.write_domain
== 0)
917 obj
->base
.pending_read_domains
|= obj
->base
.read_domains
;
918 obj
->base
.read_domains
= obj
->base
.pending_read_domains
;
919 obj
->fenced_gpu_access
= obj
->pending_fenced_gpu_access
;
921 i915_vma_move_to_active(vma
, ring
);
922 if (obj
->base
.write_domain
) {
924 obj
->last_write_seqno
= intel_ring_get_seqno(ring
);
925 if (obj
->pin_count
) /* check for potential scanout */
926 intel_mark_fb_busy(obj
, ring
);
929 trace_i915_gem_object_change_domain(obj
, old_read
, old_write
);
934 i915_gem_execbuffer_retire_commands(struct drm_device
*dev
,
935 struct drm_file
*file
,
936 struct intel_ring_buffer
*ring
,
937 struct drm_i915_gem_object
*obj
)
939 /* Unconditionally force add_request to emit a full flush. */
940 ring
->gpu_caches_dirty
= true;
942 /* Add a breadcrumb for the completion of the batch buffer */
943 (void)__i915_add_request(ring
, file
, obj
, NULL
);
947 i915_reset_gen7_sol_offsets(struct drm_device
*dev
,
948 struct intel_ring_buffer
*ring
)
950 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
953 if (!IS_GEN7(dev
) || ring
!= &dev_priv
->ring
[RCS
])
956 ret
= intel_ring_begin(ring
, 4 * 3);
960 for (i
= 0; i
< 4; i
++) {
961 intel_ring_emit(ring
, MI_LOAD_REGISTER_IMM(1));
962 intel_ring_emit(ring
, GEN7_SO_WRITE_OFFSET(i
));
963 intel_ring_emit(ring
, 0);
966 intel_ring_advance(ring
);
972 i915_gem_do_execbuffer(struct drm_device
*dev
, void *data
,
973 struct drm_file
*file
,
974 struct drm_i915_gem_execbuffer2
*args
,
975 struct drm_i915_gem_exec_object2
*exec
,
976 struct i915_address_space
*vm
)
978 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
980 struct drm_i915_gem_object
*batch_obj
;
981 struct drm_clip_rect
*cliprects
= NULL
;
982 struct intel_ring_buffer
*ring
;
983 struct i915_ctx_hang_stats
*hs
;
984 u32 ctx_id
= i915_execbuffer2_get_context_id(*args
);
985 u32 exec_start
, exec_len
;
990 if (!i915_gem_check_execbuffer(args
))
993 ret
= validate_exec_list(exec
, args
->buffer_count
);
998 if (args
->flags
& I915_EXEC_SECURE
) {
999 if (!file
->is_master
|| !capable(CAP_SYS_ADMIN
))
1002 flags
|= I915_DISPATCH_SECURE
;
1004 if (args
->flags
& I915_EXEC_IS_PINNED
)
1005 flags
|= I915_DISPATCH_PINNED
;
1007 switch (args
->flags
& I915_EXEC_RING_MASK
) {
1008 case I915_EXEC_DEFAULT
:
1009 case I915_EXEC_RENDER
:
1010 ring
= &dev_priv
->ring
[RCS
];
1013 ring
= &dev_priv
->ring
[VCS
];
1014 if (ctx_id
!= DEFAULT_CONTEXT_ID
) {
1015 DRM_DEBUG("Ring %s doesn't support contexts\n",
1021 ring
= &dev_priv
->ring
[BCS
];
1022 if (ctx_id
!= DEFAULT_CONTEXT_ID
) {
1023 DRM_DEBUG("Ring %s doesn't support contexts\n",
1028 case I915_EXEC_VEBOX
:
1029 ring
= &dev_priv
->ring
[VECS
];
1030 if (ctx_id
!= DEFAULT_CONTEXT_ID
) {
1031 DRM_DEBUG("Ring %s doesn't support contexts\n",
1038 DRM_DEBUG("execbuf with unknown ring: %d\n",
1039 (int)(args
->flags
& I915_EXEC_RING_MASK
));
1042 if (!intel_ring_initialized(ring
)) {
1043 DRM_DEBUG("execbuf with invalid ring: %d\n",
1044 (int)(args
->flags
& I915_EXEC_RING_MASK
));
1048 mode
= args
->flags
& I915_EXEC_CONSTANTS_MASK
;
1049 mask
= I915_EXEC_CONSTANTS_MASK
;
1051 case I915_EXEC_CONSTANTS_REL_GENERAL
:
1052 case I915_EXEC_CONSTANTS_ABSOLUTE
:
1053 case I915_EXEC_CONSTANTS_REL_SURFACE
:
1054 if (ring
== &dev_priv
->ring
[RCS
] &&
1055 mode
!= dev_priv
->relative_constants_mode
) {
1056 if (INTEL_INFO(dev
)->gen
< 4)
1059 if (INTEL_INFO(dev
)->gen
> 5 &&
1060 mode
== I915_EXEC_CONSTANTS_REL_SURFACE
)
1063 /* The HW changed the meaning on this bit on gen6 */
1064 if (INTEL_INFO(dev
)->gen
>= 6)
1065 mask
&= ~I915_EXEC_CONSTANTS_REL_SURFACE
;
1069 DRM_DEBUG("execbuf with unknown constants: %d\n", mode
);
1073 if (args
->buffer_count
< 1) {
1074 DRM_DEBUG("execbuf with %d buffers\n", args
->buffer_count
);
1078 if (args
->num_cliprects
!= 0) {
1079 if (ring
!= &dev_priv
->ring
[RCS
]) {
1080 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
1084 if (INTEL_INFO(dev
)->gen
>= 5) {
1085 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1089 if (args
->num_cliprects
> UINT_MAX
/ sizeof(*cliprects
)) {
1090 DRM_DEBUG("execbuf with %u cliprects\n",
1091 args
->num_cliprects
);
1095 cliprects
= kcalloc(args
->num_cliprects
,
1098 if (cliprects
== NULL
) {
1103 if (copy_from_user(cliprects
,
1104 to_user_ptr(args
->cliprects_ptr
),
1105 sizeof(*cliprects
)*args
->num_cliprects
)) {
1111 ret
= i915_mutex_lock_interruptible(dev
);
1115 if (dev_priv
->ums
.mm_suspended
) {
1116 mutex_unlock(&dev
->struct_mutex
);
1121 eb
= eb_create(args
, vm
);
1123 mutex_unlock(&dev
->struct_mutex
);
1128 /* Look up object handles */
1129 ret
= eb_lookup_vmas(eb
, exec
, args
, vm
, file
);
1133 /* take note of the batch buffer before we might reorder the lists */
1134 batch_obj
= list_entry(eb
->vmas
.prev
, struct i915_vma
, exec_list
)->obj
;
1136 /* Move the objects en-masse into the GTT, evicting if necessary. */
1137 need_relocs
= (args
->flags
& I915_EXEC_NO_RELOC
) == 0;
1138 ret
= i915_gem_execbuffer_reserve(ring
, &eb
->vmas
, &need_relocs
);
1142 /* The objects are in their final locations, apply the relocations. */
1144 ret
= i915_gem_execbuffer_relocate(eb
, vm
);
1146 if (ret
== -EFAULT
) {
1147 ret
= i915_gem_execbuffer_relocate_slow(dev
, args
, file
, ring
,
1149 BUG_ON(!mutex_is_locked(&dev
->struct_mutex
));
1155 /* Set the pending read domains for the batch buffer to COMMAND */
1156 if (batch_obj
->base
.pending_write_domain
) {
1157 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
1161 batch_obj
->base
.pending_read_domains
|= I915_GEM_DOMAIN_COMMAND
;
1163 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1164 * batch" bit. Hence we need to pin secure batches into the global gtt.
1165 * hsw should have this fixed, but bdw mucks it up again. */
1166 if (flags
& I915_DISPATCH_SECURE
&& !batch_obj
->has_global_gtt_mapping
)
1167 i915_gem_gtt_bind_object(batch_obj
, batch_obj
->cache_level
);
1169 ret
= i915_gem_execbuffer_move_to_gpu(ring
, &eb
->vmas
);
1173 hs
= i915_gem_context_get_hang_stats(dev
, file
, ctx_id
);
1184 ret
= i915_switch_context(ring
, file
, ctx_id
);
1188 if (ring
== &dev_priv
->ring
[RCS
] &&
1189 mode
!= dev_priv
->relative_constants_mode
) {
1190 ret
= intel_ring_begin(ring
, 4);
1194 intel_ring_emit(ring
, MI_NOOP
);
1195 intel_ring_emit(ring
, MI_LOAD_REGISTER_IMM(1));
1196 intel_ring_emit(ring
, INSTPM
);
1197 intel_ring_emit(ring
, mask
<< 16 | mode
);
1198 intel_ring_advance(ring
);
1200 dev_priv
->relative_constants_mode
= mode
;
1203 if (args
->flags
& I915_EXEC_GEN7_SOL_RESET
) {
1204 ret
= i915_reset_gen7_sol_offsets(dev
, ring
);
1209 exec_start
= i915_gem_obj_offset(batch_obj
, vm
) +
1210 args
->batch_start_offset
;
1211 exec_len
= args
->batch_len
;
1213 for (i
= 0; i
< args
->num_cliprects
; i
++) {
1214 ret
= i915_emit_box(dev
, &cliprects
[i
],
1215 args
->DR1
, args
->DR4
);
1219 ret
= ring
->dispatch_execbuffer(ring
,
1220 exec_start
, exec_len
,
1226 ret
= ring
->dispatch_execbuffer(ring
,
1227 exec_start
, exec_len
,
1233 trace_i915_gem_ring_dispatch(ring
, intel_ring_get_seqno(ring
), flags
);
1235 i915_gem_execbuffer_move_to_active(&eb
->vmas
, ring
);
1236 i915_gem_execbuffer_retire_commands(dev
, file
, ring
, batch_obj
);
1241 mutex_unlock(&dev
->struct_mutex
);
1249 * Legacy execbuffer just creates an exec2 list from the original exec object
1250 * list array and passes it to the real function.
1253 i915_gem_execbuffer(struct drm_device
*dev
, void *data
,
1254 struct drm_file
*file
)
1256 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1257 struct drm_i915_gem_execbuffer
*args
= data
;
1258 struct drm_i915_gem_execbuffer2 exec2
;
1259 struct drm_i915_gem_exec_object
*exec_list
= NULL
;
1260 struct drm_i915_gem_exec_object2
*exec2_list
= NULL
;
1263 if (args
->buffer_count
< 1) {
1264 DRM_DEBUG("execbuf with %d buffers\n", args
->buffer_count
);
1268 /* Copy in the exec list from userland */
1269 exec_list
= drm_malloc_ab(sizeof(*exec_list
), args
->buffer_count
);
1270 exec2_list
= drm_malloc_ab(sizeof(*exec2_list
), args
->buffer_count
);
1271 if (exec_list
== NULL
|| exec2_list
== NULL
) {
1272 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
1273 args
->buffer_count
);
1274 drm_free_large(exec_list
);
1275 drm_free_large(exec2_list
);
1278 ret
= copy_from_user(exec_list
,
1279 to_user_ptr(args
->buffers_ptr
),
1280 sizeof(*exec_list
) * args
->buffer_count
);
1282 DRM_DEBUG("copy %d exec entries failed %d\n",
1283 args
->buffer_count
, ret
);
1284 drm_free_large(exec_list
);
1285 drm_free_large(exec2_list
);
1289 for (i
= 0; i
< args
->buffer_count
; i
++) {
1290 exec2_list
[i
].handle
= exec_list
[i
].handle
;
1291 exec2_list
[i
].relocation_count
= exec_list
[i
].relocation_count
;
1292 exec2_list
[i
].relocs_ptr
= exec_list
[i
].relocs_ptr
;
1293 exec2_list
[i
].alignment
= exec_list
[i
].alignment
;
1294 exec2_list
[i
].offset
= exec_list
[i
].offset
;
1295 if (INTEL_INFO(dev
)->gen
< 4)
1296 exec2_list
[i
].flags
= EXEC_OBJECT_NEEDS_FENCE
;
1298 exec2_list
[i
].flags
= 0;
1301 exec2
.buffers_ptr
= args
->buffers_ptr
;
1302 exec2
.buffer_count
= args
->buffer_count
;
1303 exec2
.batch_start_offset
= args
->batch_start_offset
;
1304 exec2
.batch_len
= args
->batch_len
;
1305 exec2
.DR1
= args
->DR1
;
1306 exec2
.DR4
= args
->DR4
;
1307 exec2
.num_cliprects
= args
->num_cliprects
;
1308 exec2
.cliprects_ptr
= args
->cliprects_ptr
;
1309 exec2
.flags
= I915_EXEC_RENDER
;
1310 i915_execbuffer2_set_context_id(exec2
, 0);
1312 ret
= i915_gem_do_execbuffer(dev
, data
, file
, &exec2
, exec2_list
,
1313 &dev_priv
->gtt
.base
);
1315 /* Copy the new buffer offsets back to the user's exec list. */
1316 for (i
= 0; i
< args
->buffer_count
; i
++)
1317 exec_list
[i
].offset
= exec2_list
[i
].offset
;
1318 /* ... and back out to userspace */
1319 ret
= copy_to_user(to_user_ptr(args
->buffers_ptr
),
1321 sizeof(*exec_list
) * args
->buffer_count
);
1324 DRM_DEBUG("failed to copy %d exec entries "
1325 "back to user (%d)\n",
1326 args
->buffer_count
, ret
);
1330 drm_free_large(exec_list
);
1331 drm_free_large(exec2_list
);
1336 i915_gem_execbuffer2(struct drm_device
*dev
, void *data
,
1337 struct drm_file
*file
)
1339 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1340 struct drm_i915_gem_execbuffer2
*args
= data
;
1341 struct drm_i915_gem_exec_object2
*exec2_list
= NULL
;
1344 if (args
->buffer_count
< 1 ||
1345 args
->buffer_count
> UINT_MAX
/ sizeof(*exec2_list
)) {
1346 DRM_DEBUG("execbuf2 with %d buffers\n", args
->buffer_count
);
1350 exec2_list
= kmalloc(sizeof(*exec2_list
)*args
->buffer_count
,
1351 GFP_TEMPORARY
| __GFP_NOWARN
| __GFP_NORETRY
);
1352 if (exec2_list
== NULL
)
1353 exec2_list
= drm_malloc_ab(sizeof(*exec2_list
),
1354 args
->buffer_count
);
1355 if (exec2_list
== NULL
) {
1356 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
1357 args
->buffer_count
);
1360 ret
= copy_from_user(exec2_list
,
1361 to_user_ptr(args
->buffers_ptr
),
1362 sizeof(*exec2_list
) * args
->buffer_count
);
1364 DRM_DEBUG("copy %d exec entries failed %d\n",
1365 args
->buffer_count
, ret
);
1366 drm_free_large(exec2_list
);
1370 ret
= i915_gem_do_execbuffer(dev
, data
, file
, args
, exec2_list
,
1371 &dev_priv
->gtt
.base
);
1373 /* Copy the new buffer offsets back to the user's exec list. */
1374 ret
= copy_to_user(to_user_ptr(args
->buffers_ptr
),
1376 sizeof(*exec2_list
) * args
->buffer_count
);
1379 DRM_DEBUG("failed to copy %d exec entries "
1380 "back to user (%d)\n",
1381 args
->buffer_count
, ret
);
1385 drm_free_large(exec2_list
);