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>
35 #include <linux/uaccess.h>
37 #define __EXEC_OBJECT_HAS_PIN (1<<31)
38 #define __EXEC_OBJECT_HAS_FENCE (1<<30)
39 #define __EXEC_OBJECT_NEEDS_MAP (1<<29)
40 #define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
41 #define __EXEC_OBJECT_INTERNAL_FLAGS (0xf<<28) /* all of the above */
43 #define BATCH_OFFSET_BIAS (256*1024)
46 struct list_head vmas
;
49 struct i915_vma
*lut
[0];
50 struct hlist_head buckets
[0];
54 static struct eb_vmas
*
55 eb_create(struct drm_i915_gem_execbuffer2
*args
)
57 struct eb_vmas
*eb
= NULL
;
59 if (args
->flags
& I915_EXEC_HANDLE_LUT
) {
60 unsigned size
= args
->buffer_count
;
61 size
*= sizeof(struct i915_vma
*);
62 size
+= sizeof(struct eb_vmas
);
63 eb
= kmalloc(size
, GFP_TEMPORARY
| __GFP_NOWARN
| __GFP_NORETRY
);
67 unsigned size
= args
->buffer_count
;
68 unsigned count
= PAGE_SIZE
/ sizeof(struct hlist_head
) / 2;
69 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE
/ sizeof(struct hlist_head
));
70 while (count
> 2*size
)
72 eb
= kzalloc(count
*sizeof(struct hlist_head
) +
73 sizeof(struct eb_vmas
),
80 eb
->and = -args
->buffer_count
;
82 INIT_LIST_HEAD(&eb
->vmas
);
87 eb_reset(struct eb_vmas
*eb
)
90 memset(eb
->buckets
, 0, (eb
->and+1)*sizeof(struct hlist_head
));
94 eb_lookup_vmas(struct eb_vmas
*eb
,
95 struct drm_i915_gem_exec_object2
*exec
,
96 const struct drm_i915_gem_execbuffer2
*args
,
97 struct i915_address_space
*vm
,
98 struct drm_file
*file
)
100 struct drm_i915_gem_object
*obj
;
101 struct list_head objects
;
104 INIT_LIST_HEAD(&objects
);
105 spin_lock(&file
->table_lock
);
106 /* Grab a reference to the object and release the lock so we can lookup
107 * or create the VMA without using GFP_ATOMIC */
108 for (i
= 0; i
< args
->buffer_count
; i
++) {
109 obj
= to_intel_bo(idr_find(&file
->object_idr
, exec
[i
].handle
));
111 spin_unlock(&file
->table_lock
);
112 DRM_DEBUG("Invalid object handle %d at index %d\n",
118 if (!list_empty(&obj
->obj_exec_link
)) {
119 spin_unlock(&file
->table_lock
);
120 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
121 obj
, exec
[i
].handle
, i
);
126 i915_gem_object_get(obj
);
127 list_add_tail(&obj
->obj_exec_link
, &objects
);
129 spin_unlock(&file
->table_lock
);
132 while (!list_empty(&objects
)) {
133 struct i915_vma
*vma
;
135 obj
= list_first_entry(&objects
,
136 struct drm_i915_gem_object
,
140 * NOTE: We can leak any vmas created here when something fails
141 * later on. But that's no issue since vma_unbind can deal with
142 * vmas which are not actually bound. And since only
143 * lookup_or_create exists as an interface to get at the vma
144 * from the (obj, vm) we don't run the risk of creating
145 * duplicated vmas for the same vm.
147 vma
= i915_gem_obj_lookup_or_create_vma(obj
, vm
);
149 DRM_DEBUG("Failed to lookup VMA\n");
154 /* Transfer ownership from the objects list to the vmas list. */
155 list_add_tail(&vma
->exec_list
, &eb
->vmas
);
156 list_del_init(&obj
->obj_exec_link
);
158 vma
->exec_entry
= &exec
[i
];
162 uint32_t handle
= args
->flags
& I915_EXEC_HANDLE_LUT
? i
: exec
[i
].handle
;
163 vma
->exec_handle
= handle
;
164 hlist_add_head(&vma
->exec_node
,
165 &eb
->buckets
[handle
& eb
->and]);
174 while (!list_empty(&objects
)) {
175 obj
= list_first_entry(&objects
,
176 struct drm_i915_gem_object
,
178 list_del_init(&obj
->obj_exec_link
);
179 i915_gem_object_put(obj
);
182 * Objects already transfered to the vmas list will be unreferenced by
189 static inline struct i915_vma
*
190 eb_get_batch_vma(struct eb_vmas
*eb
)
192 /* The batch is always the LAST item in the VMA list */
193 struct i915_vma
*vma
= list_last_entry(&eb
->vmas
, typeof(*vma
), exec_list
);
198 static struct drm_i915_gem_object
*
199 eb_get_batch(struct eb_vmas
*eb
)
201 struct i915_vma
*vma
= eb_get_batch_vma(eb
);
204 * SNA is doing fancy tricks with compressing batch buffers, which leads
205 * to negative relocation deltas. Usually that works out ok since the
206 * relocate address is still positive, except when the batch is placed
207 * very low in the GTT. Ensure this doesn't happen.
209 * Note that actual hangs have only been observed on gen7, but for
210 * paranoia do it everywhere.
212 if ((vma
->exec_entry
->flags
& EXEC_OBJECT_PINNED
) == 0)
213 vma
->exec_entry
->flags
|= __EXEC_OBJECT_NEEDS_BIAS
;
218 static struct i915_vma
*eb_get_vma(struct eb_vmas
*eb
, unsigned long handle
)
221 if (handle
>= -eb
->and)
223 return eb
->lut
[handle
];
225 struct hlist_head
*head
;
226 struct i915_vma
*vma
;
228 head
= &eb
->buckets
[handle
& eb
->and];
229 hlist_for_each_entry(vma
, head
, exec_node
) {
230 if (vma
->exec_handle
== handle
)
238 i915_gem_execbuffer_unreserve_vma(struct i915_vma
*vma
)
240 struct drm_i915_gem_exec_object2
*entry
;
241 struct drm_i915_gem_object
*obj
= vma
->obj
;
243 if (!drm_mm_node_allocated(&vma
->node
))
246 entry
= vma
->exec_entry
;
248 if (entry
->flags
& __EXEC_OBJECT_HAS_FENCE
)
249 i915_gem_object_unpin_fence(obj
);
251 if (entry
->flags
& __EXEC_OBJECT_HAS_PIN
)
254 entry
->flags
&= ~(__EXEC_OBJECT_HAS_FENCE
| __EXEC_OBJECT_HAS_PIN
);
257 static void eb_destroy(struct eb_vmas
*eb
)
259 while (!list_empty(&eb
->vmas
)) {
260 struct i915_vma
*vma
;
262 vma
= list_first_entry(&eb
->vmas
,
265 list_del_init(&vma
->exec_list
);
266 i915_gem_execbuffer_unreserve_vma(vma
);
267 i915_gem_object_put(vma
->obj
);
272 static inline int use_cpu_reloc(struct drm_i915_gem_object
*obj
)
274 return (HAS_LLC(obj
->base
.dev
) ||
275 obj
->base
.write_domain
== I915_GEM_DOMAIN_CPU
||
276 obj
->cache_level
!= I915_CACHE_NONE
);
279 /* Used to convert any address to canonical form.
280 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
281 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
282 * addresses to be in a canonical form:
283 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
284 * canonical form [63:48] == [47]."
286 #define GEN8_HIGH_ADDRESS_BIT 47
287 static inline uint64_t gen8_canonical_addr(uint64_t address
)
289 return sign_extend64(address
, GEN8_HIGH_ADDRESS_BIT
);
292 static inline uint64_t gen8_noncanonical_addr(uint64_t address
)
294 return address
& ((1ULL << (GEN8_HIGH_ADDRESS_BIT
+ 1)) - 1);
297 static inline uint64_t
298 relocation_target(struct drm_i915_gem_relocation_entry
*reloc
,
299 uint64_t target_offset
)
301 return gen8_canonical_addr((int)reloc
->delta
+ target_offset
);
305 relocate_entry_cpu(struct drm_i915_gem_object
*obj
,
306 struct drm_i915_gem_relocation_entry
*reloc
,
307 uint64_t target_offset
)
309 struct drm_device
*dev
= obj
->base
.dev
;
310 uint32_t page_offset
= offset_in_page(reloc
->offset
);
311 uint64_t delta
= relocation_target(reloc
, target_offset
);
315 ret
= i915_gem_object_set_to_cpu_domain(obj
, true);
319 vaddr
= kmap_atomic(i915_gem_object_get_dirty_page(obj
,
320 reloc
->offset
>> PAGE_SHIFT
));
321 *(uint32_t *)(vaddr
+ page_offset
) = lower_32_bits(delta
);
323 if (INTEL_INFO(dev
)->gen
>= 8) {
324 page_offset
= offset_in_page(page_offset
+ sizeof(uint32_t));
326 if (page_offset
== 0) {
327 kunmap_atomic(vaddr
);
328 vaddr
= kmap_atomic(i915_gem_object_get_dirty_page(obj
,
329 (reloc
->offset
+ sizeof(uint32_t)) >> PAGE_SHIFT
));
332 *(uint32_t *)(vaddr
+ page_offset
) = upper_32_bits(delta
);
335 kunmap_atomic(vaddr
);
341 relocate_entry_gtt(struct drm_i915_gem_object
*obj
,
342 struct drm_i915_gem_relocation_entry
*reloc
,
343 uint64_t target_offset
)
345 struct drm_device
*dev
= obj
->base
.dev
;
346 struct drm_i915_private
*dev_priv
= to_i915(dev
);
347 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
348 uint64_t delta
= relocation_target(reloc
, target_offset
);
350 void __iomem
*reloc_page
;
353 ret
= i915_gem_object_set_to_gtt_domain(obj
, true);
357 ret
= i915_gem_object_put_fence(obj
);
361 /* Map the page containing the relocation we're going to perform. */
362 offset
= i915_gem_obj_ggtt_offset(obj
);
363 offset
+= reloc
->offset
;
364 reloc_page
= io_mapping_map_atomic_wc(ggtt
->mappable
,
366 iowrite32(lower_32_bits(delta
), reloc_page
+ offset_in_page(offset
));
368 if (INTEL_INFO(dev
)->gen
>= 8) {
369 offset
+= sizeof(uint32_t);
371 if (offset_in_page(offset
) == 0) {
372 io_mapping_unmap_atomic(reloc_page
);
374 io_mapping_map_atomic_wc(ggtt
->mappable
,
378 iowrite32(upper_32_bits(delta
),
379 reloc_page
+ offset_in_page(offset
));
382 io_mapping_unmap_atomic(reloc_page
);
388 clflush_write32(void *addr
, uint32_t value
)
390 /* This is not a fast path, so KISS. */
391 drm_clflush_virt_range(addr
, sizeof(uint32_t));
392 *(uint32_t *)addr
= value
;
393 drm_clflush_virt_range(addr
, sizeof(uint32_t));
397 relocate_entry_clflush(struct drm_i915_gem_object
*obj
,
398 struct drm_i915_gem_relocation_entry
*reloc
,
399 uint64_t target_offset
)
401 struct drm_device
*dev
= obj
->base
.dev
;
402 uint32_t page_offset
= offset_in_page(reloc
->offset
);
403 uint64_t delta
= relocation_target(reloc
, target_offset
);
407 ret
= i915_gem_object_set_to_gtt_domain(obj
, true);
411 vaddr
= kmap_atomic(i915_gem_object_get_dirty_page(obj
,
412 reloc
->offset
>> PAGE_SHIFT
));
413 clflush_write32(vaddr
+ page_offset
, lower_32_bits(delta
));
415 if (INTEL_INFO(dev
)->gen
>= 8) {
416 page_offset
= offset_in_page(page_offset
+ sizeof(uint32_t));
418 if (page_offset
== 0) {
419 kunmap_atomic(vaddr
);
420 vaddr
= kmap_atomic(i915_gem_object_get_dirty_page(obj
,
421 (reloc
->offset
+ sizeof(uint32_t)) >> PAGE_SHIFT
));
424 clflush_write32(vaddr
+ page_offset
, upper_32_bits(delta
));
427 kunmap_atomic(vaddr
);
433 i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object
*obj
,
435 struct drm_i915_gem_relocation_entry
*reloc
)
437 struct drm_device
*dev
= obj
->base
.dev
;
438 struct drm_gem_object
*target_obj
;
439 struct drm_i915_gem_object
*target_i915_obj
;
440 struct i915_vma
*target_vma
;
441 uint64_t target_offset
;
444 /* we've already hold a reference to all valid objects */
445 target_vma
= eb_get_vma(eb
, reloc
->target_handle
);
446 if (unlikely(target_vma
== NULL
))
448 target_i915_obj
= target_vma
->obj
;
449 target_obj
= &target_vma
->obj
->base
;
451 target_offset
= gen8_canonical_addr(target_vma
->node
.start
);
453 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
454 * pipe_control writes because the gpu doesn't properly redirect them
455 * through the ppgtt for non_secure batchbuffers. */
456 if (unlikely(IS_GEN6(dev
) &&
457 reloc
->write_domain
== I915_GEM_DOMAIN_INSTRUCTION
)) {
458 ret
= i915_vma_bind(target_vma
, target_i915_obj
->cache_level
,
460 if (WARN_ONCE(ret
, "Unexpected failure to bind target VMA!"))
464 /* Validate that the target is in a valid r/w GPU domain */
465 if (unlikely(reloc
->write_domain
& (reloc
->write_domain
- 1))) {
466 DRM_DEBUG("reloc with multiple write domains: "
467 "obj %p target %d offset %d "
468 "read %08x write %08x",
469 obj
, reloc
->target_handle
,
472 reloc
->write_domain
);
475 if (unlikely((reloc
->write_domain
| reloc
->read_domains
)
476 & ~I915_GEM_GPU_DOMAINS
)) {
477 DRM_DEBUG("reloc with read/write non-GPU domains: "
478 "obj %p target %d offset %d "
479 "read %08x write %08x",
480 obj
, reloc
->target_handle
,
483 reloc
->write_domain
);
487 target_obj
->pending_read_domains
|= reloc
->read_domains
;
488 target_obj
->pending_write_domain
|= reloc
->write_domain
;
490 /* If the relocation already has the right value in it, no
491 * more work needs to be done.
493 if (target_offset
== reloc
->presumed_offset
)
496 /* Check that the relocation address is valid... */
497 if (unlikely(reloc
->offset
>
498 obj
->base
.size
- (INTEL_INFO(dev
)->gen
>= 8 ? 8 : 4))) {
499 DRM_DEBUG("Relocation beyond object bounds: "
500 "obj %p target %d offset %d size %d.\n",
501 obj
, reloc
->target_handle
,
503 (int) obj
->base
.size
);
506 if (unlikely(reloc
->offset
& 3)) {
507 DRM_DEBUG("Relocation not 4-byte aligned: "
508 "obj %p target %d offset %d.\n",
509 obj
, reloc
->target_handle
,
510 (int) reloc
->offset
);
514 /* We can't wait for rendering with pagefaults disabled */
515 if (obj
->active
&& pagefault_disabled())
518 if (use_cpu_reloc(obj
))
519 ret
= relocate_entry_cpu(obj
, reloc
, target_offset
);
520 else if (obj
->map_and_fenceable
)
521 ret
= relocate_entry_gtt(obj
, reloc
, target_offset
);
522 else if (static_cpu_has(X86_FEATURE_CLFLUSH
))
523 ret
= relocate_entry_clflush(obj
, reloc
, target_offset
);
525 WARN_ONCE(1, "Impossible case in relocation handling\n");
532 /* and update the user's relocation entry */
533 reloc
->presumed_offset
= target_offset
;
539 i915_gem_execbuffer_relocate_vma(struct i915_vma
*vma
,
542 #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
543 struct drm_i915_gem_relocation_entry stack_reloc
[N_RELOC(512)];
544 struct drm_i915_gem_relocation_entry __user
*user_relocs
;
545 struct drm_i915_gem_exec_object2
*entry
= vma
->exec_entry
;
548 user_relocs
= u64_to_user_ptr(entry
->relocs_ptr
);
550 remain
= entry
->relocation_count
;
552 struct drm_i915_gem_relocation_entry
*r
= stack_reloc
;
554 if (count
> ARRAY_SIZE(stack_reloc
))
555 count
= ARRAY_SIZE(stack_reloc
);
558 if (__copy_from_user_inatomic(r
, user_relocs
, count
*sizeof(r
[0])))
562 u64 offset
= r
->presumed_offset
;
564 ret
= i915_gem_execbuffer_relocate_entry(vma
->obj
, eb
, r
);
568 if (r
->presumed_offset
!= offset
&&
569 __put_user(r
->presumed_offset
, &user_relocs
->presumed_offset
)) {
583 i915_gem_execbuffer_relocate_vma_slow(struct i915_vma
*vma
,
585 struct drm_i915_gem_relocation_entry
*relocs
)
587 const struct drm_i915_gem_exec_object2
*entry
= vma
->exec_entry
;
590 for (i
= 0; i
< entry
->relocation_count
; i
++) {
591 ret
= i915_gem_execbuffer_relocate_entry(vma
->obj
, eb
, &relocs
[i
]);
600 i915_gem_execbuffer_relocate(struct eb_vmas
*eb
)
602 struct i915_vma
*vma
;
605 /* This is the fast path and we cannot handle a pagefault whilst
606 * holding the struct mutex lest the user pass in the relocations
607 * contained within a mmaped bo. For in such a case we, the page
608 * fault handler would call i915_gem_fault() and we would try to
609 * acquire the struct mutex again. Obviously this is bad and so
610 * lockdep complains vehemently.
613 list_for_each_entry(vma
, &eb
->vmas
, exec_list
) {
614 ret
= i915_gem_execbuffer_relocate_vma(vma
, eb
);
623 static bool only_mappable_for_reloc(unsigned int flags
)
625 return (flags
& (EXEC_OBJECT_NEEDS_FENCE
| __EXEC_OBJECT_NEEDS_MAP
)) ==
626 __EXEC_OBJECT_NEEDS_MAP
;
630 i915_gem_execbuffer_reserve_vma(struct i915_vma
*vma
,
631 struct intel_engine_cs
*engine
,
634 struct drm_i915_gem_object
*obj
= vma
->obj
;
635 struct drm_i915_gem_exec_object2
*entry
= vma
->exec_entry
;
640 if (entry
->flags
& EXEC_OBJECT_NEEDS_GTT
)
643 if (!drm_mm_node_allocated(&vma
->node
)) {
644 /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
645 * limit address to the first 4GBs for unflagged objects.
647 if ((entry
->flags
& EXEC_OBJECT_SUPPORTS_48B_ADDRESS
) == 0)
648 flags
|= PIN_ZONE_4G
;
649 if (entry
->flags
& __EXEC_OBJECT_NEEDS_MAP
)
650 flags
|= PIN_GLOBAL
| PIN_MAPPABLE
;
651 if (entry
->flags
& __EXEC_OBJECT_NEEDS_BIAS
)
652 flags
|= BATCH_OFFSET_BIAS
| PIN_OFFSET_BIAS
;
653 if (entry
->flags
& EXEC_OBJECT_PINNED
)
654 flags
|= entry
->offset
| PIN_OFFSET_FIXED
;
655 if ((flags
& PIN_MAPPABLE
) == 0)
659 ret
= i915_gem_object_pin(obj
, vma
->vm
, entry
->alignment
, flags
);
660 if ((ret
== -ENOSPC
|| ret
== -E2BIG
) &&
661 only_mappable_for_reloc(entry
->flags
))
662 ret
= i915_gem_object_pin(obj
, vma
->vm
,
664 flags
& ~PIN_MAPPABLE
);
668 entry
->flags
|= __EXEC_OBJECT_HAS_PIN
;
670 if (entry
->flags
& EXEC_OBJECT_NEEDS_FENCE
) {
671 ret
= i915_gem_object_get_fence(obj
);
675 if (i915_gem_object_pin_fence(obj
))
676 entry
->flags
|= __EXEC_OBJECT_HAS_FENCE
;
679 if (entry
->offset
!= vma
->node
.start
) {
680 entry
->offset
= vma
->node
.start
;
684 if (entry
->flags
& EXEC_OBJECT_WRITE
) {
685 obj
->base
.pending_read_domains
= I915_GEM_DOMAIN_RENDER
;
686 obj
->base
.pending_write_domain
= I915_GEM_DOMAIN_RENDER
;
693 need_reloc_mappable(struct i915_vma
*vma
)
695 struct drm_i915_gem_exec_object2
*entry
= vma
->exec_entry
;
697 if (entry
->relocation_count
== 0)
703 /* See also use_cpu_reloc() */
704 if (HAS_LLC(vma
->obj
->base
.dev
))
707 if (vma
->obj
->base
.write_domain
== I915_GEM_DOMAIN_CPU
)
714 eb_vma_misplaced(struct i915_vma
*vma
)
716 struct drm_i915_gem_exec_object2
*entry
= vma
->exec_entry
;
717 struct drm_i915_gem_object
*obj
= vma
->obj
;
719 WARN_ON(entry
->flags
& __EXEC_OBJECT_NEEDS_MAP
&& !vma
->is_ggtt
);
721 if (entry
->alignment
&&
722 vma
->node
.start
& (entry
->alignment
- 1))
725 if (entry
->flags
& EXEC_OBJECT_PINNED
&&
726 vma
->node
.start
!= entry
->offset
)
729 if (entry
->flags
& __EXEC_OBJECT_NEEDS_BIAS
&&
730 vma
->node
.start
< BATCH_OFFSET_BIAS
)
733 /* avoid costly ping-pong once a batch bo ended up non-mappable */
734 if (entry
->flags
& __EXEC_OBJECT_NEEDS_MAP
&& !obj
->map_and_fenceable
)
735 return !only_mappable_for_reloc(entry
->flags
);
737 if ((entry
->flags
& EXEC_OBJECT_SUPPORTS_48B_ADDRESS
) == 0 &&
738 (vma
->node
.start
+ vma
->node
.size
- 1) >> 32)
745 i915_gem_execbuffer_reserve(struct intel_engine_cs
*engine
,
746 struct list_head
*vmas
,
747 struct i915_gem_context
*ctx
,
750 struct drm_i915_gem_object
*obj
;
751 struct i915_vma
*vma
;
752 struct i915_address_space
*vm
;
753 struct list_head ordered_vmas
;
754 struct list_head pinned_vmas
;
755 bool has_fenced_gpu_access
= INTEL_GEN(engine
->i915
) < 4;
758 i915_gem_retire_requests_ring(engine
);
760 vm
= list_first_entry(vmas
, struct i915_vma
, exec_list
)->vm
;
762 INIT_LIST_HEAD(&ordered_vmas
);
763 INIT_LIST_HEAD(&pinned_vmas
);
764 while (!list_empty(vmas
)) {
765 struct drm_i915_gem_exec_object2
*entry
;
766 bool need_fence
, need_mappable
;
768 vma
= list_first_entry(vmas
, struct i915_vma
, exec_list
);
770 entry
= vma
->exec_entry
;
772 if (ctx
->flags
& CONTEXT_NO_ZEROMAP
)
773 entry
->flags
|= __EXEC_OBJECT_NEEDS_BIAS
;
775 if (!has_fenced_gpu_access
)
776 entry
->flags
&= ~EXEC_OBJECT_NEEDS_FENCE
;
778 entry
->flags
& EXEC_OBJECT_NEEDS_FENCE
&&
779 obj
->tiling_mode
!= I915_TILING_NONE
;
780 need_mappable
= need_fence
|| need_reloc_mappable(vma
);
782 if (entry
->flags
& EXEC_OBJECT_PINNED
)
783 list_move_tail(&vma
->exec_list
, &pinned_vmas
);
784 else if (need_mappable
) {
785 entry
->flags
|= __EXEC_OBJECT_NEEDS_MAP
;
786 list_move(&vma
->exec_list
, &ordered_vmas
);
788 list_move_tail(&vma
->exec_list
, &ordered_vmas
);
790 obj
->base
.pending_read_domains
= I915_GEM_GPU_DOMAINS
& ~I915_GEM_DOMAIN_COMMAND
;
791 obj
->base
.pending_write_domain
= 0;
793 list_splice(&ordered_vmas
, vmas
);
794 list_splice(&pinned_vmas
, vmas
);
796 /* Attempt to pin all of the buffers into the GTT.
797 * This is done in 3 phases:
799 * 1a. Unbind all objects that do not match the GTT constraints for
800 * the execbuffer (fenceable, mappable, alignment etc).
801 * 1b. Increment pin count for already bound objects.
802 * 2. Bind new objects.
803 * 3. Decrement pin count.
805 * This avoid unnecessary unbinding of later objects in order to make
806 * room for the earlier objects *unless* we need to defragment.
812 /* Unbind any ill-fitting objects or pin. */
813 list_for_each_entry(vma
, vmas
, exec_list
) {
814 if (!drm_mm_node_allocated(&vma
->node
))
817 if (eb_vma_misplaced(vma
))
818 ret
= i915_vma_unbind(vma
);
820 ret
= i915_gem_execbuffer_reserve_vma(vma
,
827 /* Bind fresh objects */
828 list_for_each_entry(vma
, vmas
, exec_list
) {
829 if (drm_mm_node_allocated(&vma
->node
))
832 ret
= i915_gem_execbuffer_reserve_vma(vma
, engine
,
839 if (ret
!= -ENOSPC
|| retry
++)
842 /* Decrement pin count for bound objects */
843 list_for_each_entry(vma
, vmas
, exec_list
)
844 i915_gem_execbuffer_unreserve_vma(vma
);
846 ret
= i915_gem_evict_vm(vm
, true);
853 i915_gem_execbuffer_relocate_slow(struct drm_device
*dev
,
854 struct drm_i915_gem_execbuffer2
*args
,
855 struct drm_file
*file
,
856 struct intel_engine_cs
*engine
,
858 struct drm_i915_gem_exec_object2
*exec
,
859 struct i915_gem_context
*ctx
)
861 struct drm_i915_gem_relocation_entry
*reloc
;
862 struct i915_address_space
*vm
;
863 struct i915_vma
*vma
;
867 unsigned count
= args
->buffer_count
;
869 vm
= list_first_entry(&eb
->vmas
, struct i915_vma
, exec_list
)->vm
;
871 /* We may process another execbuffer during the unlock... */
872 while (!list_empty(&eb
->vmas
)) {
873 vma
= list_first_entry(&eb
->vmas
, struct i915_vma
, exec_list
);
874 list_del_init(&vma
->exec_list
);
875 i915_gem_execbuffer_unreserve_vma(vma
);
876 i915_gem_object_put(vma
->obj
);
879 mutex_unlock(&dev
->struct_mutex
);
882 for (i
= 0; i
< count
; i
++)
883 total
+= exec
[i
].relocation_count
;
885 reloc_offset
= drm_malloc_ab(count
, sizeof(*reloc_offset
));
886 reloc
= drm_malloc_ab(total
, sizeof(*reloc
));
887 if (reloc
== NULL
|| reloc_offset
== NULL
) {
888 drm_free_large(reloc
);
889 drm_free_large(reloc_offset
);
890 mutex_lock(&dev
->struct_mutex
);
895 for (i
= 0; i
< count
; i
++) {
896 struct drm_i915_gem_relocation_entry __user
*user_relocs
;
897 u64 invalid_offset
= (u64
)-1;
900 user_relocs
= u64_to_user_ptr(exec
[i
].relocs_ptr
);
902 if (copy_from_user(reloc
+total
, user_relocs
,
903 exec
[i
].relocation_count
* sizeof(*reloc
))) {
905 mutex_lock(&dev
->struct_mutex
);
909 /* As we do not update the known relocation offsets after
910 * relocating (due to the complexities in lock handling),
911 * we need to mark them as invalid now so that we force the
912 * relocation processing next time. Just in case the target
913 * object is evicted and then rebound into its old
914 * presumed_offset before the next execbuffer - if that
915 * happened we would make the mistake of assuming that the
916 * relocations were valid.
918 for (j
= 0; j
< exec
[i
].relocation_count
; j
++) {
919 if (__copy_to_user(&user_relocs
[j
].presumed_offset
,
921 sizeof(invalid_offset
))) {
923 mutex_lock(&dev
->struct_mutex
);
928 reloc_offset
[i
] = total
;
929 total
+= exec
[i
].relocation_count
;
932 ret
= i915_mutex_lock_interruptible(dev
);
934 mutex_lock(&dev
->struct_mutex
);
938 /* reacquire the objects */
940 ret
= eb_lookup_vmas(eb
, exec
, args
, vm
, file
);
944 need_relocs
= (args
->flags
& I915_EXEC_NO_RELOC
) == 0;
945 ret
= i915_gem_execbuffer_reserve(engine
, &eb
->vmas
, ctx
,
950 list_for_each_entry(vma
, &eb
->vmas
, exec_list
) {
951 int offset
= vma
->exec_entry
- exec
;
952 ret
= i915_gem_execbuffer_relocate_vma_slow(vma
, eb
,
953 reloc
+ reloc_offset
[offset
]);
958 /* Leave the user relocations as are, this is the painfully slow path,
959 * and we want to avoid the complication of dropping the lock whilst
960 * having buffers reserved in the aperture and so causing spurious
961 * ENOSPC for random operations.
965 drm_free_large(reloc
);
966 drm_free_large(reloc_offset
);
971 i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request
*req
,
972 struct list_head
*vmas
)
974 const unsigned other_rings
= ~intel_engine_flag(req
->engine
);
975 struct i915_vma
*vma
;
976 uint32_t flush_domains
= 0;
977 bool flush_chipset
= false;
980 list_for_each_entry(vma
, vmas
, exec_list
) {
981 struct drm_i915_gem_object
*obj
= vma
->obj
;
983 if (obj
->active
& other_rings
) {
984 ret
= i915_gem_object_sync(obj
, req
->engine
, &req
);
989 if (obj
->base
.write_domain
& I915_GEM_DOMAIN_CPU
)
990 flush_chipset
|= i915_gem_clflush_object(obj
, false);
992 flush_domains
|= obj
->base
.write_domain
;
996 i915_gem_chipset_flush(req
->engine
->i915
);
998 if (flush_domains
& I915_GEM_DOMAIN_GTT
)
1001 /* Unconditionally invalidate GPU caches and TLBs. */
1002 return req
->engine
->emit_flush(req
, I915_GEM_GPU_DOMAINS
, 0);
1006 i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2
*exec
)
1008 if (exec
->flags
& __I915_EXEC_UNKNOWN_FLAGS
)
1011 /* Kernel clipping was a DRI1 misfeature */
1012 if (exec
->num_cliprects
|| exec
->cliprects_ptr
)
1015 if (exec
->DR4
== 0xffffffff) {
1016 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1019 if (exec
->DR1
|| exec
->DR4
)
1022 if ((exec
->batch_start_offset
| exec
->batch_len
) & 0x7)
1029 validate_exec_list(struct drm_device
*dev
,
1030 struct drm_i915_gem_exec_object2
*exec
,
1033 unsigned relocs_total
= 0;
1034 unsigned relocs_max
= UINT_MAX
/ sizeof(struct drm_i915_gem_relocation_entry
);
1035 unsigned invalid_flags
;
1038 /* INTERNAL flags must not overlap with external ones */
1039 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS
& ~__EXEC_OBJECT_UNKNOWN_FLAGS
);
1041 invalid_flags
= __EXEC_OBJECT_UNKNOWN_FLAGS
;
1042 if (USES_FULL_PPGTT(dev
))
1043 invalid_flags
|= EXEC_OBJECT_NEEDS_GTT
;
1045 for (i
= 0; i
< count
; i
++) {
1046 char __user
*ptr
= u64_to_user_ptr(exec
[i
].relocs_ptr
);
1047 int length
; /* limited by fault_in_pages_readable() */
1049 if (exec
[i
].flags
& invalid_flags
)
1052 /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
1053 * any non-page-aligned or non-canonical addresses.
1055 if (exec
[i
].flags
& EXEC_OBJECT_PINNED
) {
1056 if (exec
[i
].offset
!=
1057 gen8_canonical_addr(exec
[i
].offset
& PAGE_MASK
))
1060 /* From drm_mm perspective address space is continuous,
1061 * so from this point we're always using non-canonical
1064 exec
[i
].offset
= gen8_noncanonical_addr(exec
[i
].offset
);
1067 if (exec
[i
].alignment
&& !is_power_of_2(exec
[i
].alignment
))
1070 /* First check for malicious input causing overflow in
1071 * the worst case where we need to allocate the entire
1072 * relocation tree as a single array.
1074 if (exec
[i
].relocation_count
> relocs_max
- relocs_total
)
1076 relocs_total
+= exec
[i
].relocation_count
;
1078 length
= exec
[i
].relocation_count
*
1079 sizeof(struct drm_i915_gem_relocation_entry
);
1081 * We must check that the entire relocation array is safe
1082 * to read, but since we may need to update the presumed
1083 * offsets during execution, check for full write access.
1085 if (!access_ok(VERIFY_WRITE
, ptr
, length
))
1088 if (likely(!i915
.prefault_disable
)) {
1089 if (fault_in_multipages_readable(ptr
, length
))
1097 static struct i915_gem_context
*
1098 i915_gem_validate_context(struct drm_device
*dev
, struct drm_file
*file
,
1099 struct intel_engine_cs
*engine
, const u32 ctx_id
)
1101 struct i915_gem_context
*ctx
= NULL
;
1102 struct i915_ctx_hang_stats
*hs
;
1104 if (engine
->id
!= RCS
&& ctx_id
!= DEFAULT_CONTEXT_HANDLE
)
1105 return ERR_PTR(-EINVAL
);
1107 ctx
= i915_gem_context_lookup(file
->driver_priv
, ctx_id
);
1111 hs
= &ctx
->hang_stats
;
1113 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id
);
1114 return ERR_PTR(-EIO
);
1121 i915_gem_execbuffer_move_to_active(struct list_head
*vmas
,
1122 struct drm_i915_gem_request
*req
)
1124 struct intel_engine_cs
*engine
= i915_gem_request_get_engine(req
);
1125 struct i915_vma
*vma
;
1127 list_for_each_entry(vma
, vmas
, exec_list
) {
1128 struct drm_i915_gem_exec_object2
*entry
= vma
->exec_entry
;
1129 struct drm_i915_gem_object
*obj
= vma
->obj
;
1130 u32 old_read
= obj
->base
.read_domains
;
1131 u32 old_write
= obj
->base
.write_domain
;
1133 obj
->dirty
= 1; /* be paranoid */
1134 obj
->base
.write_domain
= obj
->base
.pending_write_domain
;
1135 if (obj
->base
.write_domain
== 0)
1136 obj
->base
.pending_read_domains
|= obj
->base
.read_domains
;
1137 obj
->base
.read_domains
= obj
->base
.pending_read_domains
;
1139 i915_vma_move_to_active(vma
, req
);
1140 if (obj
->base
.write_domain
) {
1141 i915_gem_request_assign(&obj
->last_write_req
, req
);
1143 intel_fb_obj_invalidate(obj
, ORIGIN_CS
);
1145 /* update for the implicit flush after a batch */
1146 obj
->base
.write_domain
&= ~I915_GEM_GPU_DOMAINS
;
1148 if (entry
->flags
& EXEC_OBJECT_NEEDS_FENCE
) {
1149 i915_gem_request_assign(&obj
->last_fenced_req
, req
);
1150 if (entry
->flags
& __EXEC_OBJECT_HAS_FENCE
) {
1151 struct drm_i915_private
*dev_priv
= engine
->i915
;
1152 list_move_tail(&dev_priv
->fence_regs
[obj
->fence_reg
].lru_list
,
1153 &dev_priv
->mm
.fence_list
);
1157 trace_i915_gem_object_change_domain(obj
, old_read
, old_write
);
1162 i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params
*params
)
1164 /* Add a breadcrumb for the completion of the batch buffer */
1165 __i915_add_request(params
->request
, params
->batch_obj
, true);
1169 i915_reset_gen7_sol_offsets(struct drm_i915_gem_request
*req
)
1171 struct intel_ring
*ring
= req
->ring
;
1174 if (!IS_GEN7(req
->i915
) || req
->engine
->id
!= RCS
) {
1175 DRM_DEBUG("sol reset is gen7/rcs only\n");
1179 ret
= intel_ring_begin(req
, 4 * 3);
1183 for (i
= 0; i
< 4; i
++) {
1184 intel_ring_emit(ring
, MI_LOAD_REGISTER_IMM(1));
1185 intel_ring_emit_reg(ring
, GEN7_SO_WRITE_OFFSET(i
));
1186 intel_ring_emit(ring
, 0);
1189 intel_ring_advance(ring
);
1194 static struct drm_i915_gem_object
*
1195 i915_gem_execbuffer_parse(struct intel_engine_cs
*engine
,
1196 struct drm_i915_gem_exec_object2
*shadow_exec_entry
,
1198 struct drm_i915_gem_object
*batch_obj
,
1199 u32 batch_start_offset
,
1203 struct drm_i915_gem_object
*shadow_batch_obj
;
1204 struct i915_vma
*vma
;
1207 shadow_batch_obj
= i915_gem_batch_pool_get(&engine
->batch_pool
,
1208 PAGE_ALIGN(batch_len
));
1209 if (IS_ERR(shadow_batch_obj
))
1210 return shadow_batch_obj
;
1212 ret
= intel_engine_cmd_parser(engine
,
1221 ret
= i915_gem_obj_ggtt_pin(shadow_batch_obj
, 0, 0);
1225 i915_gem_object_unpin_pages(shadow_batch_obj
);
1227 memset(shadow_exec_entry
, 0, sizeof(*shadow_exec_entry
));
1229 vma
= i915_gem_obj_to_ggtt(shadow_batch_obj
);
1230 vma
->exec_entry
= shadow_exec_entry
;
1231 vma
->exec_entry
->flags
= __EXEC_OBJECT_HAS_PIN
;
1232 i915_gem_object_get(shadow_batch_obj
);
1233 list_add_tail(&vma
->exec_list
, &eb
->vmas
);
1235 shadow_batch_obj
->base
.pending_read_domains
= I915_GEM_DOMAIN_COMMAND
;
1237 return shadow_batch_obj
;
1240 i915_gem_object_unpin_pages(shadow_batch_obj
);
1241 if (ret
== -EACCES
) /* unhandled chained batch */
1244 return ERR_PTR(ret
);
1248 i915_gem_ringbuffer_submission(struct i915_execbuffer_params
*params
,
1249 struct drm_i915_gem_execbuffer2
*args
,
1250 struct list_head
*vmas
)
1252 struct drm_i915_private
*dev_priv
= params
->request
->i915
;
1253 u64 exec_start
, exec_len
;
1258 ret
= i915_gem_execbuffer_move_to_gpu(params
->request
, vmas
);
1262 ret
= i915_switch_context(params
->request
);
1266 instp_mode
= args
->flags
& I915_EXEC_CONSTANTS_MASK
;
1267 instp_mask
= I915_EXEC_CONSTANTS_MASK
;
1268 switch (instp_mode
) {
1269 case I915_EXEC_CONSTANTS_REL_GENERAL
:
1270 case I915_EXEC_CONSTANTS_ABSOLUTE
:
1271 case I915_EXEC_CONSTANTS_REL_SURFACE
:
1272 if (instp_mode
!= 0 && params
->engine
->id
!= RCS
) {
1273 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
1277 if (instp_mode
!= dev_priv
->relative_constants_mode
) {
1278 if (INTEL_INFO(dev_priv
)->gen
< 4) {
1279 DRM_DEBUG("no rel constants on pre-gen4\n");
1283 if (INTEL_INFO(dev_priv
)->gen
> 5 &&
1284 instp_mode
== I915_EXEC_CONSTANTS_REL_SURFACE
) {
1285 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
1289 /* The HW changed the meaning on this bit on gen6 */
1290 if (INTEL_INFO(dev_priv
)->gen
>= 6)
1291 instp_mask
&= ~I915_EXEC_CONSTANTS_REL_SURFACE
;
1295 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode
);
1299 if (params
->engine
->id
== RCS
&&
1300 instp_mode
!= dev_priv
->relative_constants_mode
) {
1301 struct intel_ring
*ring
= params
->request
->ring
;
1303 ret
= intel_ring_begin(params
->request
, 4);
1307 intel_ring_emit(ring
, MI_NOOP
);
1308 intel_ring_emit(ring
, MI_LOAD_REGISTER_IMM(1));
1309 intel_ring_emit_reg(ring
, INSTPM
);
1310 intel_ring_emit(ring
, instp_mask
<< 16 | instp_mode
);
1311 intel_ring_advance(ring
);
1313 dev_priv
->relative_constants_mode
= instp_mode
;
1316 if (args
->flags
& I915_EXEC_GEN7_SOL_RESET
) {
1317 ret
= i915_reset_gen7_sol_offsets(params
->request
);
1322 exec_len
= args
->batch_len
;
1323 exec_start
= params
->batch_obj_vm_offset
+
1324 params
->args_batch_start_offset
;
1327 exec_len
= params
->batch_obj
->base
.size
;
1329 ret
= params
->engine
->dispatch_execbuffer(params
->request
,
1330 exec_start
, exec_len
,
1331 params
->dispatch_flags
);
1335 trace_i915_gem_ring_dispatch(params
->request
, params
->dispatch_flags
);
1337 i915_gem_execbuffer_move_to_active(vmas
, params
->request
);
1343 * Find one BSD ring to dispatch the corresponding BSD command.
1344 * The engine index is returned.
1347 gen8_dispatch_bsd_engine(struct drm_i915_private
*dev_priv
,
1348 struct drm_file
*file
)
1350 struct drm_i915_file_private
*file_priv
= file
->driver_priv
;
1352 /* Check whether the file_priv has already selected one ring. */
1353 if ((int)file_priv
->bsd_engine
< 0) {
1354 /* If not, use the ping-pong mechanism to select one. */
1355 mutex_lock(&dev_priv
->drm
.struct_mutex
);
1356 file_priv
->bsd_engine
= dev_priv
->mm
.bsd_engine_dispatch_index
;
1357 dev_priv
->mm
.bsd_engine_dispatch_index
^= 1;
1358 mutex_unlock(&dev_priv
->drm
.struct_mutex
);
1361 return file_priv
->bsd_engine
;
1364 #define I915_USER_RINGS (4)
1366 static const enum intel_engine_id user_ring_map
[I915_USER_RINGS
+ 1] = {
1367 [I915_EXEC_DEFAULT
] = RCS
,
1368 [I915_EXEC_RENDER
] = RCS
,
1369 [I915_EXEC_BLT
] = BCS
,
1370 [I915_EXEC_BSD
] = VCS
,
1371 [I915_EXEC_VEBOX
] = VECS
1374 static struct intel_engine_cs
*
1375 eb_select_engine(struct drm_i915_private
*dev_priv
,
1376 struct drm_file
*file
,
1377 struct drm_i915_gem_execbuffer2
*args
)
1379 unsigned int user_ring_id
= args
->flags
& I915_EXEC_RING_MASK
;
1380 struct intel_engine_cs
*engine
;
1382 if (user_ring_id
> I915_USER_RINGS
) {
1383 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id
);
1387 if ((user_ring_id
!= I915_EXEC_BSD
) &&
1388 ((args
->flags
& I915_EXEC_BSD_MASK
) != 0)) {
1389 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1390 "bsd dispatch flags: %d\n", (int)(args
->flags
));
1394 if (user_ring_id
== I915_EXEC_BSD
&& HAS_BSD2(dev_priv
)) {
1395 unsigned int bsd_idx
= args
->flags
& I915_EXEC_BSD_MASK
;
1397 if (bsd_idx
== I915_EXEC_BSD_DEFAULT
) {
1398 bsd_idx
= gen8_dispatch_bsd_engine(dev_priv
, file
);
1399 } else if (bsd_idx
>= I915_EXEC_BSD_RING1
&&
1400 bsd_idx
<= I915_EXEC_BSD_RING2
) {
1401 bsd_idx
>>= I915_EXEC_BSD_SHIFT
;
1404 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
1409 engine
= &dev_priv
->engine
[_VCS(bsd_idx
)];
1411 engine
= &dev_priv
->engine
[user_ring_map
[user_ring_id
]];
1414 if (!intel_engine_initialized(engine
)) {
1415 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id
);
1423 i915_gem_do_execbuffer(struct drm_device
*dev
, void *data
,
1424 struct drm_file
*file
,
1425 struct drm_i915_gem_execbuffer2
*args
,
1426 struct drm_i915_gem_exec_object2
*exec
)
1428 struct drm_i915_private
*dev_priv
= to_i915(dev
);
1429 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
1430 struct drm_i915_gem_request
*req
= NULL
;
1432 struct drm_i915_gem_object
*batch_obj
;
1433 struct drm_i915_gem_exec_object2 shadow_exec_entry
;
1434 struct intel_engine_cs
*engine
;
1435 struct i915_gem_context
*ctx
;
1436 struct i915_address_space
*vm
;
1437 struct i915_execbuffer_params params_master
; /* XXX: will be removed later */
1438 struct i915_execbuffer_params
*params
= ¶ms_master
;
1439 const u32 ctx_id
= i915_execbuffer2_get_context_id(*args
);
1444 if (!i915_gem_check_execbuffer(args
))
1447 ret
= validate_exec_list(dev
, exec
, args
->buffer_count
);
1452 if (args
->flags
& I915_EXEC_SECURE
) {
1453 if (!drm_is_current_master(file
) || !capable(CAP_SYS_ADMIN
))
1456 dispatch_flags
|= I915_DISPATCH_SECURE
;
1458 if (args
->flags
& I915_EXEC_IS_PINNED
)
1459 dispatch_flags
|= I915_DISPATCH_PINNED
;
1461 engine
= eb_select_engine(dev_priv
, file
, args
);
1465 if (args
->buffer_count
< 1) {
1466 DRM_DEBUG("execbuf with %d buffers\n", args
->buffer_count
);
1470 if (args
->flags
& I915_EXEC_RESOURCE_STREAMER
) {
1471 if (!HAS_RESOURCE_STREAMER(dev
)) {
1472 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
1475 if (engine
->id
!= RCS
) {
1476 DRM_DEBUG("RS is not available on %s\n",
1481 dispatch_flags
|= I915_DISPATCH_RS
;
1484 /* Take a local wakeref for preparing to dispatch the execbuf as
1485 * we expect to access the hardware fairly frequently in the
1486 * process. Upon first dispatch, we acquire another prolonged
1487 * wakeref that we hold until the GPU has been idle for at least
1490 intel_runtime_pm_get(dev_priv
);
1492 ret
= i915_mutex_lock_interruptible(dev
);
1496 ctx
= i915_gem_validate_context(dev
, file
, engine
, ctx_id
);
1498 mutex_unlock(&dev
->struct_mutex
);
1503 i915_gem_context_get(ctx
);
1506 vm
= &ctx
->ppgtt
->base
;
1510 memset(¶ms_master
, 0x00, sizeof(params_master
));
1512 eb
= eb_create(args
);
1514 i915_gem_context_put(ctx
);
1515 mutex_unlock(&dev
->struct_mutex
);
1520 /* Look up object handles */
1521 ret
= eb_lookup_vmas(eb
, exec
, args
, vm
, file
);
1525 /* take note of the batch buffer before we might reorder the lists */
1526 batch_obj
= eb_get_batch(eb
);
1528 /* Move the objects en-masse into the GTT, evicting if necessary. */
1529 need_relocs
= (args
->flags
& I915_EXEC_NO_RELOC
) == 0;
1530 ret
= i915_gem_execbuffer_reserve(engine
, &eb
->vmas
, ctx
,
1535 /* The objects are in their final locations, apply the relocations. */
1537 ret
= i915_gem_execbuffer_relocate(eb
);
1539 if (ret
== -EFAULT
) {
1540 ret
= i915_gem_execbuffer_relocate_slow(dev
, args
, file
,
1543 BUG_ON(!mutex_is_locked(&dev
->struct_mutex
));
1549 /* Set the pending read domains for the batch buffer to COMMAND */
1550 if (batch_obj
->base
.pending_write_domain
) {
1551 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
1556 params
->args_batch_start_offset
= args
->batch_start_offset
;
1557 if (intel_engine_needs_cmd_parser(engine
) && args
->batch_len
) {
1558 struct drm_i915_gem_object
*parsed_batch_obj
;
1560 parsed_batch_obj
= i915_gem_execbuffer_parse(engine
,
1564 args
->batch_start_offset
,
1566 drm_is_current_master(file
));
1567 if (IS_ERR(parsed_batch_obj
)) {
1568 ret
= PTR_ERR(parsed_batch_obj
);
1573 * parsed_batch_obj == batch_obj means batch not fully parsed:
1574 * Accept, but don't promote to secure.
1577 if (parsed_batch_obj
!= batch_obj
) {
1579 * Batch parsed and accepted:
1581 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1582 * bit from MI_BATCH_BUFFER_START commands issued in
1583 * the dispatch_execbuffer implementations. We
1584 * specifically don't want that set on batches the
1585 * command parser has accepted.
1587 dispatch_flags
|= I915_DISPATCH_SECURE
;
1588 params
->args_batch_start_offset
= 0;
1589 batch_obj
= parsed_batch_obj
;
1593 batch_obj
->base
.pending_read_domains
|= I915_GEM_DOMAIN_COMMAND
;
1595 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1596 * batch" bit. Hence we need to pin secure batches into the global gtt.
1597 * hsw should have this fixed, but bdw mucks it up again. */
1598 if (dispatch_flags
& I915_DISPATCH_SECURE
) {
1600 * So on first glance it looks freaky that we pin the batch here
1601 * outside of the reservation loop. But:
1602 * - The batch is already pinned into the relevant ppgtt, so we
1603 * already have the backing storage fully allocated.
1604 * - No other BO uses the global gtt (well contexts, but meh),
1605 * so we don't really have issues with multiple objects not
1606 * fitting due to fragmentation.
1607 * So this is actually safe.
1609 ret
= i915_gem_obj_ggtt_pin(batch_obj
, 0, 0);
1613 params
->batch_obj_vm_offset
= i915_gem_obj_ggtt_offset(batch_obj
);
1615 params
->batch_obj_vm_offset
= i915_gem_obj_offset(batch_obj
, vm
);
1617 /* Allocate a request for this batch buffer nice and early. */
1618 req
= i915_gem_request_alloc(engine
, ctx
);
1621 goto err_batch_unpin
;
1624 ret
= i915_gem_request_add_to_client(req
, file
);
1629 * Save assorted stuff away to pass through to *_submission().
1630 * NB: This data should be 'persistent' and not local as it will
1631 * kept around beyond the duration of the IOCTL once the GPU
1632 * scheduler arrives.
1635 params
->file
= file
;
1636 params
->engine
= engine
;
1637 params
->dispatch_flags
= dispatch_flags
;
1638 params
->batch_obj
= batch_obj
;
1640 params
->request
= req
;
1642 ret
= dev_priv
->gt
.execbuf_submit(params
, args
, &eb
->vmas
);
1644 i915_gem_execbuffer_retire_commands(params
);
1648 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1649 * batch vma for correctness. For less ugly and less fragility this
1650 * needs to be adjusted to also track the ggtt batch vma properly as
1653 if (dispatch_flags
& I915_DISPATCH_SECURE
)
1654 i915_gem_object_ggtt_unpin(batch_obj
);
1657 /* the request owns the ref now */
1658 i915_gem_context_put(ctx
);
1661 mutex_unlock(&dev
->struct_mutex
);
1664 /* intel_gpu_busy should also get a ref, so it will free when the device
1665 * is really idle. */
1666 intel_runtime_pm_put(dev_priv
);
1671 * Legacy execbuffer just creates an exec2 list from the original exec object
1672 * list array and passes it to the real function.
1675 i915_gem_execbuffer(struct drm_device
*dev
, void *data
,
1676 struct drm_file
*file
)
1678 struct drm_i915_gem_execbuffer
*args
= data
;
1679 struct drm_i915_gem_execbuffer2 exec2
;
1680 struct drm_i915_gem_exec_object
*exec_list
= NULL
;
1681 struct drm_i915_gem_exec_object2
*exec2_list
= NULL
;
1684 if (args
->buffer_count
< 1) {
1685 DRM_DEBUG("execbuf with %d buffers\n", args
->buffer_count
);
1689 /* Copy in the exec list from userland */
1690 exec_list
= drm_malloc_ab(sizeof(*exec_list
), args
->buffer_count
);
1691 exec2_list
= drm_malloc_ab(sizeof(*exec2_list
), args
->buffer_count
);
1692 if (exec_list
== NULL
|| exec2_list
== NULL
) {
1693 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
1694 args
->buffer_count
);
1695 drm_free_large(exec_list
);
1696 drm_free_large(exec2_list
);
1699 ret
= copy_from_user(exec_list
,
1700 u64_to_user_ptr(args
->buffers_ptr
),
1701 sizeof(*exec_list
) * args
->buffer_count
);
1703 DRM_DEBUG("copy %d exec entries failed %d\n",
1704 args
->buffer_count
, ret
);
1705 drm_free_large(exec_list
);
1706 drm_free_large(exec2_list
);
1710 for (i
= 0; i
< args
->buffer_count
; i
++) {
1711 exec2_list
[i
].handle
= exec_list
[i
].handle
;
1712 exec2_list
[i
].relocation_count
= exec_list
[i
].relocation_count
;
1713 exec2_list
[i
].relocs_ptr
= exec_list
[i
].relocs_ptr
;
1714 exec2_list
[i
].alignment
= exec_list
[i
].alignment
;
1715 exec2_list
[i
].offset
= exec_list
[i
].offset
;
1716 if (INTEL_INFO(dev
)->gen
< 4)
1717 exec2_list
[i
].flags
= EXEC_OBJECT_NEEDS_FENCE
;
1719 exec2_list
[i
].flags
= 0;
1722 exec2
.buffers_ptr
= args
->buffers_ptr
;
1723 exec2
.buffer_count
= args
->buffer_count
;
1724 exec2
.batch_start_offset
= args
->batch_start_offset
;
1725 exec2
.batch_len
= args
->batch_len
;
1726 exec2
.DR1
= args
->DR1
;
1727 exec2
.DR4
= args
->DR4
;
1728 exec2
.num_cliprects
= args
->num_cliprects
;
1729 exec2
.cliprects_ptr
= args
->cliprects_ptr
;
1730 exec2
.flags
= I915_EXEC_RENDER
;
1731 i915_execbuffer2_set_context_id(exec2
, 0);
1733 ret
= i915_gem_do_execbuffer(dev
, data
, file
, &exec2
, exec2_list
);
1735 struct drm_i915_gem_exec_object __user
*user_exec_list
=
1736 u64_to_user_ptr(args
->buffers_ptr
);
1738 /* Copy the new buffer offsets back to the user's exec list. */
1739 for (i
= 0; i
< args
->buffer_count
; i
++) {
1740 exec2_list
[i
].offset
=
1741 gen8_canonical_addr(exec2_list
[i
].offset
);
1742 ret
= __copy_to_user(&user_exec_list
[i
].offset
,
1743 &exec2_list
[i
].offset
,
1744 sizeof(user_exec_list
[i
].offset
));
1747 DRM_DEBUG("failed to copy %d exec entries "
1748 "back to user (%d)\n",
1749 args
->buffer_count
, ret
);
1755 drm_free_large(exec_list
);
1756 drm_free_large(exec2_list
);
1761 i915_gem_execbuffer2(struct drm_device
*dev
, void *data
,
1762 struct drm_file
*file
)
1764 struct drm_i915_gem_execbuffer2
*args
= data
;
1765 struct drm_i915_gem_exec_object2
*exec2_list
= NULL
;
1768 if (args
->buffer_count
< 1 ||
1769 args
->buffer_count
> UINT_MAX
/ sizeof(*exec2_list
)) {
1770 DRM_DEBUG("execbuf2 with %d buffers\n", args
->buffer_count
);
1774 if (args
->rsvd2
!= 0) {
1775 DRM_DEBUG("dirty rvsd2 field\n");
1779 exec2_list
= drm_malloc_gfp(args
->buffer_count
,
1780 sizeof(*exec2_list
),
1782 if (exec2_list
== NULL
) {
1783 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
1784 args
->buffer_count
);
1787 ret
= copy_from_user(exec2_list
,
1788 u64_to_user_ptr(args
->buffers_ptr
),
1789 sizeof(*exec2_list
) * args
->buffer_count
);
1791 DRM_DEBUG("copy %d exec entries failed %d\n",
1792 args
->buffer_count
, ret
);
1793 drm_free_large(exec2_list
);
1797 ret
= i915_gem_do_execbuffer(dev
, data
, file
, args
, exec2_list
);
1799 /* Copy the new buffer offsets back to the user's exec list. */
1800 struct drm_i915_gem_exec_object2 __user
*user_exec_list
=
1801 u64_to_user_ptr(args
->buffers_ptr
);
1804 for (i
= 0; i
< args
->buffer_count
; i
++) {
1805 exec2_list
[i
].offset
=
1806 gen8_canonical_addr(exec2_list
[i
].offset
);
1807 ret
= __copy_to_user(&user_exec_list
[i
].offset
,
1808 &exec2_list
[i
].offset
,
1809 sizeof(user_exec_list
[i
].offset
));
1812 DRM_DEBUG("failed to copy %d exec entries "
1814 args
->buffer_count
);
1820 drm_free_large(exec2_list
);