1c349dbc7Sjsg /*
2c349dbc7Sjsg  * SPDX-License-Identifier: MIT
3c349dbc7Sjsg  *
4c349dbc7Sjsg  * Copyright © 2008,2010 Intel Corporation
5c349dbc7Sjsg  */
6c349dbc7Sjsg 
7c349dbc7Sjsg #include <linux/dma-resv.h>
81bb76ff1Sjsg #include <linux/highmem.h>
9c349dbc7Sjsg #include <linux/sync_file.h>
10c349dbc7Sjsg #include <linux/uaccess.h>
11c349dbc7Sjsg 
12c349dbc7Sjsg #include <drm/drm_syncobj.h>
13c349dbc7Sjsg 
14c349dbc7Sjsg #include "display/intel_frontbuffer.h"
15c349dbc7Sjsg 
16c349dbc7Sjsg #include "gem/i915_gem_ioctls.h"
17c349dbc7Sjsg #include "gt/intel_context.h"
185ca02815Sjsg #include "gt/intel_gpu_commands.h"
19c349dbc7Sjsg #include "gt/intel_gt.h"
20ad8b1aafSjsg #include "gt/intel_gt_buffer_pool.h"
21c349dbc7Sjsg #include "gt/intel_gt_pm.h"
22c349dbc7Sjsg #include "gt/intel_ring.h"
23c349dbc7Sjsg 
241bb76ff1Sjsg #include "pxp/intel_pxp.h"
251bb76ff1Sjsg 
261bb76ff1Sjsg #include "i915_cmd_parser.h"
27c349dbc7Sjsg #include "i915_drv.h"
281bb76ff1Sjsg #include "i915_file_private.h"
29c349dbc7Sjsg #include "i915_gem_clflush.h"
30c349dbc7Sjsg #include "i915_gem_context.h"
311bb76ff1Sjsg #include "i915_gem_evict.h"
32c349dbc7Sjsg #include "i915_gem_ioctls.h"
33*f005ef32Sjsg #include "i915_reg.h"
34c349dbc7Sjsg #include "i915_trace.h"
35ad8b1aafSjsg #include "i915_user_extensions.h"
36c349dbc7Sjsg 
37c349dbc7Sjsg struct eb_vma {
38c349dbc7Sjsg 	struct i915_vma *vma;
39c349dbc7Sjsg 	unsigned int flags;
40c349dbc7Sjsg 
41c349dbc7Sjsg 	/** This vma's place in the execbuf reservation list */
42c349dbc7Sjsg 	struct drm_i915_gem_exec_object2 *exec;
43c349dbc7Sjsg 	struct list_head bind_link;
44c349dbc7Sjsg 	struct list_head reloc_link;
45c349dbc7Sjsg 
46c349dbc7Sjsg 	struct hlist_node node;
47c349dbc7Sjsg 	u32 handle;
48c349dbc7Sjsg };
49c349dbc7Sjsg 
50c349dbc7Sjsg enum {
51c349dbc7Sjsg 	FORCE_CPU_RELOC = 1,
52c349dbc7Sjsg 	FORCE_GTT_RELOC,
53c349dbc7Sjsg 	FORCE_GPU_RELOC,
54c349dbc7Sjsg #define DBG_FORCE_RELOC 0 /* choose one of the above! */
55c349dbc7Sjsg };
56c349dbc7Sjsg 
57*f005ef32Sjsg /* __EXEC_OBJECT_ flags > BIT(29) defined in i915_vma.h */
58*f005ef32Sjsg #define __EXEC_OBJECT_HAS_PIN		BIT(29)
59*f005ef32Sjsg #define __EXEC_OBJECT_HAS_FENCE		BIT(28)
60*f005ef32Sjsg #define __EXEC_OBJECT_USERPTR_INIT	BIT(27)
61*f005ef32Sjsg #define __EXEC_OBJECT_NEEDS_MAP		BIT(26)
62*f005ef32Sjsg #define __EXEC_OBJECT_NEEDS_BIAS	BIT(25)
63*f005ef32Sjsg #define __EXEC_OBJECT_INTERNAL_FLAGS	(~0u << 25) /* all of the above + */
64c349dbc7Sjsg #define __EXEC_OBJECT_RESERVED (__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_FENCE)
65c349dbc7Sjsg 
66c349dbc7Sjsg #define __EXEC_HAS_RELOC	BIT(31)
67ad8b1aafSjsg #define __EXEC_ENGINE_PINNED	BIT(30)
685ca02815Sjsg #define __EXEC_USERPTR_USED	BIT(29)
695ca02815Sjsg #define __EXEC_INTERNAL_FLAGS	(~0u << 29)
70c349dbc7Sjsg #define UPDATE			PIN_OFFSET_FIXED
71c349dbc7Sjsg 
72c349dbc7Sjsg #define BATCH_OFFSET_BIAS (256*1024)
73c349dbc7Sjsg 
74c349dbc7Sjsg #define __I915_EXEC_ILLEGAL_FLAGS \
75c349dbc7Sjsg 	(__I915_EXEC_UNKNOWN_FLAGS | \
76c349dbc7Sjsg 	 I915_EXEC_CONSTANTS_MASK  | \
77c349dbc7Sjsg 	 I915_EXEC_RESOURCE_STREAMER)
78c349dbc7Sjsg 
79c349dbc7Sjsg /* Catch emission of unexpected errors for CI! */
80c349dbc7Sjsg #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
81c349dbc7Sjsg #undef EINVAL
82c349dbc7Sjsg #define EINVAL ({ \
83c349dbc7Sjsg 	DRM_DEBUG_DRIVER("EINVAL at %s:%d\n", __func__, __LINE__); \
84c349dbc7Sjsg 	22; \
85c349dbc7Sjsg })
86c349dbc7Sjsg #endif
87c349dbc7Sjsg 
88c349dbc7Sjsg /**
89c349dbc7Sjsg  * DOC: User command execution
90c349dbc7Sjsg  *
91c349dbc7Sjsg  * Userspace submits commands to be executed on the GPU as an instruction
92c349dbc7Sjsg  * stream within a GEM object we call a batchbuffer. This instructions may
93c349dbc7Sjsg  * refer to other GEM objects containing auxiliary state such as kernels,
94c349dbc7Sjsg  * samplers, render targets and even secondary batchbuffers. Userspace does
95c349dbc7Sjsg  * not know where in the GPU memory these objects reside and so before the
96c349dbc7Sjsg  * batchbuffer is passed to the GPU for execution, those addresses in the
97c349dbc7Sjsg  * batchbuffer and auxiliary objects are updated. This is known as relocation,
98c349dbc7Sjsg  * or patching. To try and avoid having to relocate each object on the next
99c349dbc7Sjsg  * execution, userspace is told the location of those objects in this pass,
100c349dbc7Sjsg  * but this remains just a hint as the kernel may choose a new location for
101c349dbc7Sjsg  * any object in the future.
102c349dbc7Sjsg  *
103c349dbc7Sjsg  * At the level of talking to the hardware, submitting a batchbuffer for the
104c349dbc7Sjsg  * GPU to execute is to add content to a buffer from which the HW
105c349dbc7Sjsg  * command streamer is reading.
106c349dbc7Sjsg  *
107c349dbc7Sjsg  * 1. Add a command to load the HW context. For Logical Ring Contexts, i.e.
108c349dbc7Sjsg  *    Execlists, this command is not placed on the same buffer as the
109c349dbc7Sjsg  *    remaining items.
110c349dbc7Sjsg  *
111c349dbc7Sjsg  * 2. Add a command to invalidate caches to the buffer.
112c349dbc7Sjsg  *
113c349dbc7Sjsg  * 3. Add a batchbuffer start command to the buffer; the start command is
114c349dbc7Sjsg  *    essentially a token together with the GPU address of the batchbuffer
115c349dbc7Sjsg  *    to be executed.
116c349dbc7Sjsg  *
117c349dbc7Sjsg  * 4. Add a pipeline flush to the buffer.
118c349dbc7Sjsg  *
119c349dbc7Sjsg  * 5. Add a memory write command to the buffer to record when the GPU
120c349dbc7Sjsg  *    is done executing the batchbuffer. The memory write writes the
121c349dbc7Sjsg  *    global sequence number of the request, ``i915_request::global_seqno``;
122c349dbc7Sjsg  *    the i915 driver uses the current value in the register to determine
123c349dbc7Sjsg  *    if the GPU has completed the batchbuffer.
124c349dbc7Sjsg  *
125c349dbc7Sjsg  * 6. Add a user interrupt command to the buffer. This command instructs
126c349dbc7Sjsg  *    the GPU to issue an interrupt when the command, pipeline flush and
127c349dbc7Sjsg  *    memory write are completed.
128c349dbc7Sjsg  *
129c349dbc7Sjsg  * 7. Inform the hardware of the additional commands added to the buffer
130c349dbc7Sjsg  *    (by updating the tail pointer).
131c349dbc7Sjsg  *
132c349dbc7Sjsg  * Processing an execbuf ioctl is conceptually split up into a few phases.
133c349dbc7Sjsg  *
134c349dbc7Sjsg  * 1. Validation - Ensure all the pointers, handles and flags are valid.
135c349dbc7Sjsg  * 2. Reservation - Assign GPU address space for every object
136c349dbc7Sjsg  * 3. Relocation - Update any addresses to point to the final locations
137c349dbc7Sjsg  * 4. Serialisation - Order the request with respect to its dependencies
138c349dbc7Sjsg  * 5. Construction - Construct a request to execute the batchbuffer
139c349dbc7Sjsg  * 6. Submission (at some point in the future execution)
140c349dbc7Sjsg  *
141c349dbc7Sjsg  * Reserving resources for the execbuf is the most complicated phase. We
142c349dbc7Sjsg  * neither want to have to migrate the object in the address space, nor do
143c349dbc7Sjsg  * we want to have to update any relocations pointing to this object. Ideally,
144c349dbc7Sjsg  * we want to leave the object where it is and for all the existing relocations
145c349dbc7Sjsg  * to match. If the object is given a new address, or if userspace thinks the
146c349dbc7Sjsg  * object is elsewhere, we have to parse all the relocation entries and update
147c349dbc7Sjsg  * the addresses. Userspace can set the I915_EXEC_NORELOC flag to hint that
148c349dbc7Sjsg  * all the target addresses in all of its objects match the value in the
149c349dbc7Sjsg  * relocation entries and that they all match the presumed offsets given by the
150c349dbc7Sjsg  * list of execbuffer objects. Using this knowledge, we know that if we haven't
151c349dbc7Sjsg  * moved any buffers, all the relocation entries are valid and we can skip
152c349dbc7Sjsg  * the update. (If userspace is wrong, the likely outcome is an impromptu GPU
153c349dbc7Sjsg  * hang.) The requirement for using I915_EXEC_NO_RELOC are:
154c349dbc7Sjsg  *
155c349dbc7Sjsg  *      The addresses written in the objects must match the corresponding
156c349dbc7Sjsg  *      reloc.presumed_offset which in turn must match the corresponding
157c349dbc7Sjsg  *      execobject.offset.
158c349dbc7Sjsg  *
159c349dbc7Sjsg  *      Any render targets written to in the batch must be flagged with
160c349dbc7Sjsg  *      EXEC_OBJECT_WRITE.
161c349dbc7Sjsg  *
162c349dbc7Sjsg  *      To avoid stalling, execobject.offset should match the current
163c349dbc7Sjsg  *      address of that object within the active context.
164c349dbc7Sjsg  *
165c349dbc7Sjsg  * The reservation is done is multiple phases. First we try and keep any
166c349dbc7Sjsg  * object already bound in its current location - so as long as meets the
167c349dbc7Sjsg  * constraints imposed by the new execbuffer. Any object left unbound after the
168c349dbc7Sjsg  * first pass is then fitted into any available idle space. If an object does
169c349dbc7Sjsg  * not fit, all objects are removed from the reservation and the process rerun
170c349dbc7Sjsg  * after sorting the objects into a priority order (more difficult to fit
171c349dbc7Sjsg  * objects are tried first). Failing that, the entire VM is cleared and we try
172c349dbc7Sjsg  * to fit the execbuf once last time before concluding that it simply will not
173c349dbc7Sjsg  * fit.
174c349dbc7Sjsg  *
175c349dbc7Sjsg  * A small complication to all of this is that we allow userspace not only to
176c349dbc7Sjsg  * specify an alignment and a size for the object in the address space, but
177c349dbc7Sjsg  * we also allow userspace to specify the exact offset. This objects are
178c349dbc7Sjsg  * simpler to place (the location is known a priori) all we have to do is make
179c349dbc7Sjsg  * sure the space is available.
180c349dbc7Sjsg  *
181c349dbc7Sjsg  * Once all the objects are in place, patching up the buried pointers to point
182c349dbc7Sjsg  * to the final locations is a fairly simple job of walking over the relocation
183c349dbc7Sjsg  * entry arrays, looking up the right address and rewriting the value into
184c349dbc7Sjsg  * the object. Simple! ... The relocation entries are stored in user memory
185c349dbc7Sjsg  * and so to access them we have to copy them into a local buffer. That copy
186c349dbc7Sjsg  * has to avoid taking any pagefaults as they may lead back to a GEM object
187c349dbc7Sjsg  * requiring the struct_mutex (i.e. recursive deadlock). So once again we split
188c349dbc7Sjsg  * the relocation into multiple passes. First we try to do everything within an
189c349dbc7Sjsg  * atomic context (avoid the pagefaults) which requires that we never wait. If
190c349dbc7Sjsg  * we detect that we may wait, or if we need to fault, then we have to fallback
191c349dbc7Sjsg  * to a slower path. The slowpath has to drop the mutex. (Can you hear alarm
192c349dbc7Sjsg  * bells yet?) Dropping the mutex means that we lose all the state we have
193c349dbc7Sjsg  * built up so far for the execbuf and we must reset any global data. However,
194c349dbc7Sjsg  * we do leave the objects pinned in their final locations - which is a
195c349dbc7Sjsg  * potential issue for concurrent execbufs. Once we have left the mutex, we can
196c349dbc7Sjsg  * allocate and copy all the relocation entries into a large array at our
197c349dbc7Sjsg  * leisure, reacquire the mutex, reclaim all the objects and other state and
198c349dbc7Sjsg  * then proceed to update any incorrect addresses with the objects.
199c349dbc7Sjsg  *
200c349dbc7Sjsg  * As we process the relocation entries, we maintain a record of whether the
201c349dbc7Sjsg  * object is being written to. Using NORELOC, we expect userspace to provide
202c349dbc7Sjsg  * this information instead. We also check whether we can skip the relocation
203c349dbc7Sjsg  * by comparing the expected value inside the relocation entry with the target's
204c349dbc7Sjsg  * final address. If they differ, we have to map the current object and rewrite
205c349dbc7Sjsg  * the 4 or 8 byte pointer within.
206c349dbc7Sjsg  *
207c349dbc7Sjsg  * Serialising an execbuf is quite simple according to the rules of the GEM
208c349dbc7Sjsg  * ABI. Execution within each context is ordered by the order of submission.
209c349dbc7Sjsg  * Writes to any GEM object are in order of submission and are exclusive. Reads
210c349dbc7Sjsg  * from a GEM object are unordered with respect to other reads, but ordered by
211c349dbc7Sjsg  * writes. A write submitted after a read cannot occur before the read, and
212c349dbc7Sjsg  * similarly any read submitted after a write cannot occur before the write.
213c349dbc7Sjsg  * Writes are ordered between engines such that only one write occurs at any
214c349dbc7Sjsg  * time (completing any reads beforehand) - using semaphores where available
215c349dbc7Sjsg  * and CPU serialisation otherwise. Other GEM access obey the same rules, any
216c349dbc7Sjsg  * write (either via mmaps using set-domain, or via pwrite) must flush all GPU
217c349dbc7Sjsg  * reads before starting, and any read (either using set-domain or pread) must
218c349dbc7Sjsg  * flush all GPU writes before starting. (Note we only employ a barrier before,
219c349dbc7Sjsg  * we currently rely on userspace not concurrently starting a new execution
220c349dbc7Sjsg  * whilst reading or writing to an object. This may be an advantage or not
221c349dbc7Sjsg  * depending on how much you trust userspace not to shoot themselves in the
222c349dbc7Sjsg  * foot.) Serialisation may just result in the request being inserted into
223c349dbc7Sjsg  * a DAG awaiting its turn, but most simple is to wait on the CPU until
224c349dbc7Sjsg  * all dependencies are resolved.
225c349dbc7Sjsg  *
226c349dbc7Sjsg  * After all of that, is just a matter of closing the request and handing it to
227c349dbc7Sjsg  * the hardware (well, leaving it in a queue to be executed). However, we also
228c349dbc7Sjsg  * offer the ability for batchbuffers to be run with elevated privileges so
229c349dbc7Sjsg  * that they access otherwise hidden registers. (Used to adjust L3 cache etc.)
230c349dbc7Sjsg  * Before any batch is given extra privileges we first must check that it
231c349dbc7Sjsg  * contains no nefarious instructions, we check that each instruction is from
232c349dbc7Sjsg  * our whitelist and all registers are also from an allowed list. We first
233c349dbc7Sjsg  * copy the user's batchbuffer to a shadow (so that the user doesn't have
234c349dbc7Sjsg  * access to it, either by the CPU or GPU as we scan it) and then parse each
235c349dbc7Sjsg  * instruction. If everything is ok, we set a flag telling the hardware to run
236c349dbc7Sjsg  * the batchbuffer in trusted mode, otherwise the ioctl is rejected.
237c349dbc7Sjsg  */
238c349dbc7Sjsg 
239ad8b1aafSjsg struct eb_fence {
240ad8b1aafSjsg 	struct drm_syncobj *syncobj; /* Use with ptr_mask_bits() */
241ad8b1aafSjsg 	struct dma_fence *dma_fence;
242ad8b1aafSjsg 	u64 value;
243ad8b1aafSjsg 	struct dma_fence_chain *chain_fence;
244ad8b1aafSjsg };
245ad8b1aafSjsg 
246c349dbc7Sjsg struct i915_execbuffer {
247c349dbc7Sjsg 	struct drm_i915_private *i915; /** i915 backpointer */
248c349dbc7Sjsg 	struct drm_file *file; /** per-file lookup tables and limits */
249c349dbc7Sjsg 	struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */
250c349dbc7Sjsg 	struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */
251c349dbc7Sjsg 	struct eb_vma *vma;
252c349dbc7Sjsg 
2531bb76ff1Sjsg 	struct intel_gt *gt; /* gt for the execbuf */
254c349dbc7Sjsg 	struct intel_context *context; /* logical state for the request */
255c349dbc7Sjsg 	struct i915_gem_context *gem_context; /** caller's context */
256c349dbc7Sjsg 
2571bb76ff1Sjsg 	/** our requests to build */
2581bb76ff1Sjsg 	struct i915_request *requests[MAX_ENGINE_INSTANCE + 1];
2591bb76ff1Sjsg 	/** identity of the batch obj/vma */
2601bb76ff1Sjsg 	struct eb_vma *batches[MAX_ENGINE_INSTANCE + 1];
261c349dbc7Sjsg 	struct i915_vma *trampoline; /** trampoline used for chaining */
262c349dbc7Sjsg 
2631bb76ff1Sjsg 	/** used for excl fence in dma_resv objects when > 1 BB submitted */
2641bb76ff1Sjsg 	struct dma_fence *composite_fence;
2651bb76ff1Sjsg 
266c349dbc7Sjsg 	/** actual size of execobj[] as we may extend it for the cmdparser */
267c349dbc7Sjsg 	unsigned int buffer_count;
268c349dbc7Sjsg 
2691bb76ff1Sjsg 	/* number of batches in execbuf IOCTL */
2701bb76ff1Sjsg 	unsigned int num_batches;
2711bb76ff1Sjsg 
272c349dbc7Sjsg 	/** list of vma not yet bound during reservation phase */
273c349dbc7Sjsg 	struct list_head unbound;
274c349dbc7Sjsg 
275c349dbc7Sjsg 	/** list of vma that have execobj.relocation_count */
276c349dbc7Sjsg 	struct list_head relocs;
277c349dbc7Sjsg 
278ad8b1aafSjsg 	struct i915_gem_ww_ctx ww;
279ad8b1aafSjsg 
280c349dbc7Sjsg 	/**
281c349dbc7Sjsg 	 * Track the most recently used object for relocations, as we
282c349dbc7Sjsg 	 * frequently have to perform multiple relocations within the same
283c349dbc7Sjsg 	 * obj/page
284c349dbc7Sjsg 	 */
285c349dbc7Sjsg 	struct reloc_cache {
286c349dbc7Sjsg 		struct drm_mm_node node; /** temporary GTT binding */
287c349dbc7Sjsg 		unsigned long vaddr; /** Current kmap address */
288c349dbc7Sjsg 		unsigned long page; /** Currently mapped page index */
2895ca02815Sjsg 		unsigned int graphics_ver; /** Cached value of GRAPHICS_VER */
290c349dbc7Sjsg 		bool use_64bit_reloc : 1;
291c349dbc7Sjsg 		bool has_llc : 1;
292c349dbc7Sjsg 		bool has_fence : 1;
293c349dbc7Sjsg 		bool needs_unfenced : 1;
294c349dbc7Sjsg 	} reloc_cache;
295c349dbc7Sjsg 
296c349dbc7Sjsg 	u64 invalid_flags; /** Set of execobj.flags that are invalid */
297c349dbc7Sjsg 
2981bb76ff1Sjsg 	/** Length of batch within object */
2991bb76ff1Sjsg 	u64 batch_len[MAX_ENGINE_INSTANCE + 1];
300c349dbc7Sjsg 	u32 batch_start_offset; /** Location within object of batch */
301c349dbc7Sjsg 	u32 batch_flags; /** Flags composed for emit_bb_start() */
302ad8b1aafSjsg 	struct intel_gt_buffer_pool_node *batch_pool; /** pool node for batch buffer */
303c349dbc7Sjsg 
304c349dbc7Sjsg 	/**
305c349dbc7Sjsg 	 * Indicate either the size of the hastable used to resolve
306c349dbc7Sjsg 	 * relocation handles, or if negative that we are using a direct
307c349dbc7Sjsg 	 * index into the execobj[].
308c349dbc7Sjsg 	 */
309c349dbc7Sjsg 	int lut_size;
310c349dbc7Sjsg 	struct hlist_head *buckets; /** ht for relocation handles */
311ad8b1aafSjsg 
312ad8b1aafSjsg 	struct eb_fence *fences;
313ad8b1aafSjsg 	unsigned long num_fences;
3141bb76ff1Sjsg #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
3151bb76ff1Sjsg 	struct i915_capture_list *capture_lists[MAX_ENGINE_INSTANCE + 1];
3161bb76ff1Sjsg #endif
317c349dbc7Sjsg };
318c349dbc7Sjsg 
319ad8b1aafSjsg static int eb_parse(struct i915_execbuffer *eb);
3201bb76ff1Sjsg static int eb_pin_engine(struct i915_execbuffer *eb, bool throttle);
321ad8b1aafSjsg static void eb_unpin_engine(struct i915_execbuffer *eb);
3221bb76ff1Sjsg static void eb_capture_release(struct i915_execbuffer *eb);
323ad8b1aafSjsg 
eb_use_cmdparser(const struct i915_execbuffer * eb)324c349dbc7Sjsg static inline bool eb_use_cmdparser(const struct i915_execbuffer *eb)
325c349dbc7Sjsg {
3261bb76ff1Sjsg 	return intel_engine_requires_cmd_parser(eb->context->engine) ||
3271bb76ff1Sjsg 		(intel_engine_using_cmd_parser(eb->context->engine) &&
328c349dbc7Sjsg 		 eb->args->batch_len);
329c349dbc7Sjsg }
330c349dbc7Sjsg 
eb_create(struct i915_execbuffer * eb)331c349dbc7Sjsg static int eb_create(struct i915_execbuffer *eb)
332c349dbc7Sjsg {
333c349dbc7Sjsg 	if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) {
334c349dbc7Sjsg 		unsigned int size = 1 + ilog2(eb->buffer_count);
335c349dbc7Sjsg 
336c349dbc7Sjsg 		/*
337c349dbc7Sjsg 		 * Without a 1:1 association between relocation handles and
338c349dbc7Sjsg 		 * the execobject[] index, we instead create a hashtable.
339c349dbc7Sjsg 		 * We size it dynamically based on available memory, starting
340c349dbc7Sjsg 		 * first with 1:1 assocative hash and scaling back until
341c349dbc7Sjsg 		 * the allocation succeeds.
342c349dbc7Sjsg 		 *
343c349dbc7Sjsg 		 * Later on we use a positive lut_size to indicate we are
344c349dbc7Sjsg 		 * using this hashtable, and a negative value to indicate a
345c349dbc7Sjsg 		 * direct lookup.
346c349dbc7Sjsg 		 */
347c349dbc7Sjsg 		do {
348c349dbc7Sjsg 			gfp_t flags;
349c349dbc7Sjsg 
350c349dbc7Sjsg 			/* While we can still reduce the allocation size, don't
351c349dbc7Sjsg 			 * raise a warning and allow the allocation to fail.
352c349dbc7Sjsg 			 * On the last pass though, we want to try as hard
353c349dbc7Sjsg 			 * as possible to perform the allocation and warn
354c349dbc7Sjsg 			 * if it fails.
355c349dbc7Sjsg 			 */
356c349dbc7Sjsg 			flags = GFP_KERNEL;
357c349dbc7Sjsg 			if (size > 1)
358c349dbc7Sjsg 				flags |= __GFP_NORETRY | __GFP_NOWARN;
359c349dbc7Sjsg 
360c349dbc7Sjsg 			eb->buckets = kzalloc(sizeof(struct hlist_head) << size,
361c349dbc7Sjsg 					      flags);
362c349dbc7Sjsg 			if (eb->buckets)
363c349dbc7Sjsg 				break;
364c349dbc7Sjsg 		} while (--size);
365c349dbc7Sjsg 
366c349dbc7Sjsg 		if (unlikely(!size))
367c349dbc7Sjsg 			return -ENOMEM;
368c349dbc7Sjsg 
369c349dbc7Sjsg 		eb->lut_size = size;
370c349dbc7Sjsg 	} else {
371c349dbc7Sjsg 		eb->lut_size = -eb->buffer_count;
372c349dbc7Sjsg 	}
373c349dbc7Sjsg 
374c349dbc7Sjsg 	return 0;
375c349dbc7Sjsg }
376c349dbc7Sjsg 
377c349dbc7Sjsg static bool
eb_vma_misplaced(const struct drm_i915_gem_exec_object2 * entry,const struct i915_vma * vma,unsigned int flags)378c349dbc7Sjsg eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry,
379c349dbc7Sjsg 		 const struct i915_vma *vma,
380c349dbc7Sjsg 		 unsigned int flags)
381c349dbc7Sjsg {
382*f005ef32Sjsg 	const u64 start = i915_vma_offset(vma);
383*f005ef32Sjsg 	const u64 size = i915_vma_size(vma);
384*f005ef32Sjsg 
385*f005ef32Sjsg 	if (size < entry->pad_to_size)
386c349dbc7Sjsg 		return true;
387c349dbc7Sjsg 
388*f005ef32Sjsg 	if (entry->alignment && !IS_ALIGNED(start, entry->alignment))
389c349dbc7Sjsg 		return true;
390c349dbc7Sjsg 
391c349dbc7Sjsg 	if (flags & EXEC_OBJECT_PINNED &&
392*f005ef32Sjsg 	    start != entry->offset)
393c349dbc7Sjsg 		return true;
394c349dbc7Sjsg 
395c349dbc7Sjsg 	if (flags & __EXEC_OBJECT_NEEDS_BIAS &&
396*f005ef32Sjsg 	    start < BATCH_OFFSET_BIAS)
397c349dbc7Sjsg 		return true;
398c349dbc7Sjsg 
399c349dbc7Sjsg 	if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) &&
400*f005ef32Sjsg 	    (start + size + 4095) >> 32)
401c349dbc7Sjsg 		return true;
402c349dbc7Sjsg 
403c349dbc7Sjsg 	if (flags & __EXEC_OBJECT_NEEDS_MAP &&
404c349dbc7Sjsg 	    !i915_vma_is_map_and_fenceable(vma))
405c349dbc7Sjsg 		return true;
406c349dbc7Sjsg 
407c349dbc7Sjsg 	return false;
408c349dbc7Sjsg }
409c349dbc7Sjsg 
eb_pin_flags(const struct drm_i915_gem_exec_object2 * entry,unsigned int exec_flags)410ad8b1aafSjsg static u64 eb_pin_flags(const struct drm_i915_gem_exec_object2 *entry,
411ad8b1aafSjsg 			unsigned int exec_flags)
412ad8b1aafSjsg {
413ad8b1aafSjsg 	u64 pin_flags = 0;
414ad8b1aafSjsg 
415ad8b1aafSjsg 	if (exec_flags & EXEC_OBJECT_NEEDS_GTT)
416ad8b1aafSjsg 		pin_flags |= PIN_GLOBAL;
417ad8b1aafSjsg 
418ad8b1aafSjsg 	/*
419ad8b1aafSjsg 	 * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
420ad8b1aafSjsg 	 * limit address to the first 4GBs for unflagged objects.
421ad8b1aafSjsg 	 */
422ad8b1aafSjsg 	if (!(exec_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
423ad8b1aafSjsg 		pin_flags |= PIN_ZONE_4G;
424ad8b1aafSjsg 
425ad8b1aafSjsg 	if (exec_flags & __EXEC_OBJECT_NEEDS_MAP)
426ad8b1aafSjsg 		pin_flags |= PIN_MAPPABLE;
427ad8b1aafSjsg 
428ad8b1aafSjsg 	if (exec_flags & EXEC_OBJECT_PINNED)
429ad8b1aafSjsg 		pin_flags |= entry->offset | PIN_OFFSET_FIXED;
430ad8b1aafSjsg 	else if (exec_flags & __EXEC_OBJECT_NEEDS_BIAS)
431ad8b1aafSjsg 		pin_flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
432ad8b1aafSjsg 
433ad8b1aafSjsg 	return pin_flags;
434ad8b1aafSjsg }
435ad8b1aafSjsg 
4365ca02815Sjsg static inline int
eb_pin_vma(struct i915_execbuffer * eb,const struct drm_i915_gem_exec_object2 * entry,struct eb_vma * ev)437c349dbc7Sjsg eb_pin_vma(struct i915_execbuffer *eb,
438c349dbc7Sjsg 	   const struct drm_i915_gem_exec_object2 *entry,
439c349dbc7Sjsg 	   struct eb_vma *ev)
440c349dbc7Sjsg {
441c349dbc7Sjsg 	struct i915_vma *vma = ev->vma;
442c349dbc7Sjsg 	u64 pin_flags;
4435ca02815Sjsg 	int err;
444c349dbc7Sjsg 
445c349dbc7Sjsg 	if (vma->node.size)
446*f005ef32Sjsg 		pin_flags =  __i915_vma_offset(vma);
447c349dbc7Sjsg 	else
448c349dbc7Sjsg 		pin_flags = entry->offset & PIN_OFFSET_MASK;
449c349dbc7Sjsg 
4501bb76ff1Sjsg 	pin_flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED | PIN_VALIDATE;
451c349dbc7Sjsg 	if (unlikely(ev->flags & EXEC_OBJECT_NEEDS_GTT))
452c349dbc7Sjsg 		pin_flags |= PIN_GLOBAL;
453c349dbc7Sjsg 
454ad8b1aafSjsg 	/* Attempt to reuse the current location if available */
4555ca02815Sjsg 	err = i915_vma_pin_ww(vma, &eb->ww, 0, 0, pin_flags);
4565ca02815Sjsg 	if (err == -EDEADLK)
4575ca02815Sjsg 		return err;
4585ca02815Sjsg 
4595ca02815Sjsg 	if (unlikely(err)) {
460ad8b1aafSjsg 		if (entry->flags & EXEC_OBJECT_PINNED)
4615ca02815Sjsg 			return err;
462c349dbc7Sjsg 
463ad8b1aafSjsg 		/* Failing that pick any _free_ space if suitable */
4645ca02815Sjsg 		err = i915_vma_pin_ww(vma, &eb->ww,
465ad8b1aafSjsg 					     entry->pad_to_size,
466ad8b1aafSjsg 					     entry->alignment,
467ad8b1aafSjsg 					     eb_pin_flags(entry, ev->flags) |
4681bb76ff1Sjsg 					     PIN_USER | PIN_NOEVICT | PIN_VALIDATE);
4695ca02815Sjsg 		if (unlikely(err))
4705ca02815Sjsg 			return err;
471ad8b1aafSjsg 	}
472ad8b1aafSjsg 
473c349dbc7Sjsg 	if (unlikely(ev->flags & EXEC_OBJECT_NEEDS_FENCE)) {
4745ca02815Sjsg 		err = i915_vma_pin_fence(vma);
4751bb76ff1Sjsg 		if (unlikely(err))
4765ca02815Sjsg 			return err;
477c349dbc7Sjsg 
478c349dbc7Sjsg 		if (vma->fence)
479c349dbc7Sjsg 			ev->flags |= __EXEC_OBJECT_HAS_FENCE;
480c349dbc7Sjsg 	}
481c349dbc7Sjsg 
482c349dbc7Sjsg 	ev->flags |= __EXEC_OBJECT_HAS_PIN;
4835ca02815Sjsg 	if (eb_vma_misplaced(entry, vma, ev->flags))
4845ca02815Sjsg 		return -EBADSLT;
4855ca02815Sjsg 
4865ca02815Sjsg 	return 0;
487c349dbc7Sjsg }
488c349dbc7Sjsg 
489c349dbc7Sjsg static inline void
eb_unreserve_vma(struct eb_vma * ev)490c349dbc7Sjsg eb_unreserve_vma(struct eb_vma *ev)
491c349dbc7Sjsg {
492ad8b1aafSjsg 	if (unlikely(ev->flags & __EXEC_OBJECT_HAS_FENCE))
493ad8b1aafSjsg 		__i915_vma_unpin_fence(ev->vma);
494ad8b1aafSjsg 
495c349dbc7Sjsg 	ev->flags &= ~__EXEC_OBJECT_RESERVED;
496c349dbc7Sjsg }
497c349dbc7Sjsg 
498c349dbc7Sjsg static int
eb_validate_vma(struct i915_execbuffer * eb,struct drm_i915_gem_exec_object2 * entry,struct i915_vma * vma)499c349dbc7Sjsg eb_validate_vma(struct i915_execbuffer *eb,
500c349dbc7Sjsg 		struct drm_i915_gem_exec_object2 *entry,
501c349dbc7Sjsg 		struct i915_vma *vma)
502c349dbc7Sjsg {
5035ca02815Sjsg 	/* Relocations are disallowed for all platforms after TGL-LP.  This
5045ca02815Sjsg 	 * also covers all platforms with local memory.
5055ca02815Sjsg 	 */
5065ca02815Sjsg 	if (entry->relocation_count &&
5075ca02815Sjsg 	    GRAPHICS_VER(eb->i915) >= 12 && !IS_TIGERLAKE(eb->i915))
5085ca02815Sjsg 		return -EINVAL;
5095ca02815Sjsg 
510c349dbc7Sjsg 	if (unlikely(entry->flags & eb->invalid_flags))
511c349dbc7Sjsg 		return -EINVAL;
512c349dbc7Sjsg 
513c349dbc7Sjsg 	if (unlikely(entry->alignment &&
514c349dbc7Sjsg 		     !is_power_of_2_u64(entry->alignment)))
515c349dbc7Sjsg 		return -EINVAL;
516c349dbc7Sjsg 
517c349dbc7Sjsg 	/*
518c349dbc7Sjsg 	 * Offset can be used as input (EXEC_OBJECT_PINNED), reject
519c349dbc7Sjsg 	 * any non-page-aligned or non-canonical addresses.
520c349dbc7Sjsg 	 */
521c349dbc7Sjsg 	if (unlikely(entry->flags & EXEC_OBJECT_PINNED &&
522c349dbc7Sjsg 		     entry->offset != gen8_canonical_addr(entry->offset & I915_GTT_PAGE_MASK)))
523c349dbc7Sjsg 		return -EINVAL;
524c349dbc7Sjsg 
525c349dbc7Sjsg 	/* pad_to_size was once a reserved field, so sanitize it */
526c349dbc7Sjsg 	if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) {
527c349dbc7Sjsg 		if (unlikely(offset_in_page(entry->pad_to_size)))
528c349dbc7Sjsg 			return -EINVAL;
529c349dbc7Sjsg 	} else {
530c349dbc7Sjsg 		entry->pad_to_size = 0;
531c349dbc7Sjsg 	}
532c349dbc7Sjsg 	/*
533c349dbc7Sjsg 	 * From drm_mm perspective address space is continuous,
534c349dbc7Sjsg 	 * so from this point we're always using non-canonical
535c349dbc7Sjsg 	 * form internally.
536c349dbc7Sjsg 	 */
537c349dbc7Sjsg 	entry->offset = gen8_noncanonical_addr(entry->offset);
538c349dbc7Sjsg 
539c349dbc7Sjsg 	if (!eb->reloc_cache.has_fence) {
540c349dbc7Sjsg 		entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
541c349dbc7Sjsg 	} else {
542c349dbc7Sjsg 		if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE ||
543c349dbc7Sjsg 		     eb->reloc_cache.needs_unfenced) &&
544c349dbc7Sjsg 		    i915_gem_object_is_tiled(vma->obj))
545c349dbc7Sjsg 			entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP;
546c349dbc7Sjsg 	}
547c349dbc7Sjsg 
548c349dbc7Sjsg 	return 0;
549c349dbc7Sjsg }
550c349dbc7Sjsg 
5511bb76ff1Sjsg static inline bool
is_batch_buffer(struct i915_execbuffer * eb,unsigned int buffer_idx)5521bb76ff1Sjsg is_batch_buffer(struct i915_execbuffer *eb, unsigned int buffer_idx)
5531bb76ff1Sjsg {
5541bb76ff1Sjsg 	return eb->args->flags & I915_EXEC_BATCH_FIRST ?
5551bb76ff1Sjsg 		buffer_idx < eb->num_batches :
5561bb76ff1Sjsg 		buffer_idx >= eb->args->buffer_count - eb->num_batches;
5571bb76ff1Sjsg }
5581bb76ff1Sjsg 
5591bb76ff1Sjsg static int
eb_add_vma(struct i915_execbuffer * eb,unsigned int * current_batch,unsigned int i,struct i915_vma * vma)560c349dbc7Sjsg eb_add_vma(struct i915_execbuffer *eb,
5611bb76ff1Sjsg 	   unsigned int *current_batch,
5621bb76ff1Sjsg 	   unsigned int i,
563c349dbc7Sjsg 	   struct i915_vma *vma)
564c349dbc7Sjsg {
5651bb76ff1Sjsg 	struct drm_i915_private *i915 = eb->i915;
566c349dbc7Sjsg 	struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
567c349dbc7Sjsg 	struct eb_vma *ev = &eb->vma[i];
568c349dbc7Sjsg 
569ad8b1aafSjsg 	ev->vma = vma;
570c349dbc7Sjsg 	ev->exec = entry;
571c349dbc7Sjsg 	ev->flags = entry->flags;
572c349dbc7Sjsg 
573c349dbc7Sjsg 	if (eb->lut_size > 0) {
574c349dbc7Sjsg 		ev->handle = entry->handle;
575c349dbc7Sjsg 		hlist_add_head(&ev->node,
576c349dbc7Sjsg 			       &eb->buckets[hash_32(entry->handle,
577c349dbc7Sjsg 						    eb->lut_size)]);
578c349dbc7Sjsg 	}
579c349dbc7Sjsg 
580c349dbc7Sjsg 	if (entry->relocation_count)
581c349dbc7Sjsg 		list_add_tail(&ev->reloc_link, &eb->relocs);
582c349dbc7Sjsg 
583c349dbc7Sjsg 	/*
584c349dbc7Sjsg 	 * SNA is doing fancy tricks with compressing batch buffers, which leads
585c349dbc7Sjsg 	 * to negative relocation deltas. Usually that works out ok since the
586c349dbc7Sjsg 	 * relocate address is still positive, except when the batch is placed
587c349dbc7Sjsg 	 * very low in the GTT. Ensure this doesn't happen.
588c349dbc7Sjsg 	 *
589c349dbc7Sjsg 	 * Note that actual hangs have only been observed on gen7, but for
590c349dbc7Sjsg 	 * paranoia do it everywhere.
591c349dbc7Sjsg 	 */
5921bb76ff1Sjsg 	if (is_batch_buffer(eb, i)) {
593c349dbc7Sjsg 		if (entry->relocation_count &&
594c349dbc7Sjsg 		    !(ev->flags & EXEC_OBJECT_PINNED))
595c349dbc7Sjsg 			ev->flags |= __EXEC_OBJECT_NEEDS_BIAS;
596c349dbc7Sjsg 		if (eb->reloc_cache.has_fence)
597c349dbc7Sjsg 			ev->flags |= EXEC_OBJECT_NEEDS_FENCE;
598c349dbc7Sjsg 
5991bb76ff1Sjsg 		eb->batches[*current_batch] = ev;
6001bb76ff1Sjsg 
6011bb76ff1Sjsg 		if (unlikely(ev->flags & EXEC_OBJECT_WRITE)) {
6021bb76ff1Sjsg 			drm_dbg(&i915->drm,
6031bb76ff1Sjsg 				"Attempting to use self-modifying batch buffer\n");
6041bb76ff1Sjsg 			return -EINVAL;
605c349dbc7Sjsg 		}
6061bb76ff1Sjsg 
6071bb76ff1Sjsg 		if (range_overflows_t(u64,
6081bb76ff1Sjsg 				      eb->batch_start_offset,
6091bb76ff1Sjsg 				      eb->args->batch_len,
6101bb76ff1Sjsg 				      ev->vma->size)) {
6111bb76ff1Sjsg 			drm_dbg(&i915->drm, "Attempting to use out-of-bounds batch\n");
6121bb76ff1Sjsg 			return -EINVAL;
6131bb76ff1Sjsg 		}
6141bb76ff1Sjsg 
6151bb76ff1Sjsg 		if (eb->args->batch_len == 0)
6161bb76ff1Sjsg 			eb->batch_len[*current_batch] = ev->vma->size -
6171bb76ff1Sjsg 				eb->batch_start_offset;
6181bb76ff1Sjsg 		else
6191bb76ff1Sjsg 			eb->batch_len[*current_batch] = eb->args->batch_len;
6201bb76ff1Sjsg 		if (unlikely(eb->batch_len[*current_batch] == 0)) { /* impossible! */
6211bb76ff1Sjsg 			drm_dbg(&i915->drm, "Invalid batch length\n");
6221bb76ff1Sjsg 			return -EINVAL;
6231bb76ff1Sjsg 		}
6241bb76ff1Sjsg 
6251bb76ff1Sjsg 		++*current_batch;
6261bb76ff1Sjsg 	}
6271bb76ff1Sjsg 
6281bb76ff1Sjsg 	return 0;
629c349dbc7Sjsg }
630c349dbc7Sjsg 
use_cpu_reloc(const struct reloc_cache * cache,const struct drm_i915_gem_object * obj)631c349dbc7Sjsg static inline int use_cpu_reloc(const struct reloc_cache *cache,
632c349dbc7Sjsg 				const struct drm_i915_gem_object *obj)
633c349dbc7Sjsg {
634c349dbc7Sjsg 	if (!i915_gem_object_has_struct_page(obj))
635c349dbc7Sjsg 		return false;
636c349dbc7Sjsg 
637c349dbc7Sjsg 	if (DBG_FORCE_RELOC == FORCE_CPU_RELOC)
638c349dbc7Sjsg 		return true;
639c349dbc7Sjsg 
640c349dbc7Sjsg 	if (DBG_FORCE_RELOC == FORCE_GTT_RELOC)
641c349dbc7Sjsg 		return false;
642c349dbc7Sjsg 
643*f005ef32Sjsg 	/*
644*f005ef32Sjsg 	 * For objects created by userspace through GEM_CREATE with pat_index
645*f005ef32Sjsg 	 * set by set_pat extension, i915_gem_object_has_cache_level() always
646*f005ef32Sjsg 	 * return true, otherwise the call would fall back to checking whether
647*f005ef32Sjsg 	 * the object is un-cached.
648*f005ef32Sjsg 	 */
649c349dbc7Sjsg 	return (cache->has_llc ||
650c349dbc7Sjsg 		obj->cache_dirty ||
651*f005ef32Sjsg 		!i915_gem_object_has_cache_level(obj, I915_CACHE_NONE));
652c349dbc7Sjsg }
653c349dbc7Sjsg 
eb_reserve_vma(struct i915_execbuffer * eb,struct eb_vma * ev,u64 pin_flags)654ad8b1aafSjsg static int eb_reserve_vma(struct i915_execbuffer *eb,
655c349dbc7Sjsg 			  struct eb_vma *ev,
656c349dbc7Sjsg 			  u64 pin_flags)
657c349dbc7Sjsg {
658c349dbc7Sjsg 	struct drm_i915_gem_exec_object2 *entry = ev->exec;
659c349dbc7Sjsg 	struct i915_vma *vma = ev->vma;
660c349dbc7Sjsg 	int err;
661c349dbc7Sjsg 
662c349dbc7Sjsg 	if (drm_mm_node_allocated(&vma->node) &&
663c349dbc7Sjsg 	    eb_vma_misplaced(entry, vma, ev->flags)) {
664c349dbc7Sjsg 		err = i915_vma_unbind(vma);
665c349dbc7Sjsg 		if (err)
666c349dbc7Sjsg 			return err;
667c349dbc7Sjsg 	}
668c349dbc7Sjsg 
669ad8b1aafSjsg 	err = i915_vma_pin_ww(vma, &eb->ww,
670c349dbc7Sjsg 			   entry->pad_to_size, entry->alignment,
671ad8b1aafSjsg 			   eb_pin_flags(entry, ev->flags) | pin_flags);
672c349dbc7Sjsg 	if (err)
673c349dbc7Sjsg 		return err;
674c349dbc7Sjsg 
675*f005ef32Sjsg 	if (entry->offset != i915_vma_offset(vma)) {
676*f005ef32Sjsg 		entry->offset = i915_vma_offset(vma) | UPDATE;
677c349dbc7Sjsg 		eb->args->flags |= __EXEC_HAS_RELOC;
678c349dbc7Sjsg 	}
679c349dbc7Sjsg 
680ad8b1aafSjsg 	if (unlikely(ev->flags & EXEC_OBJECT_NEEDS_FENCE)) {
681c349dbc7Sjsg 		err = i915_vma_pin_fence(vma);
6821bb76ff1Sjsg 		if (unlikely(err))
683c349dbc7Sjsg 			return err;
684c349dbc7Sjsg 
685c349dbc7Sjsg 		if (vma->fence)
686ad8b1aafSjsg 			ev->flags |= __EXEC_OBJECT_HAS_FENCE;
687c349dbc7Sjsg 	}
688c349dbc7Sjsg 
689ad8b1aafSjsg 	ev->flags |= __EXEC_OBJECT_HAS_PIN;
690c349dbc7Sjsg 	GEM_BUG_ON(eb_vma_misplaced(entry, vma, ev->flags));
691c349dbc7Sjsg 
692c349dbc7Sjsg 	return 0;
693c349dbc7Sjsg }
694c349dbc7Sjsg 
eb_unbind(struct i915_execbuffer * eb,bool force)6951bb76ff1Sjsg static bool eb_unbind(struct i915_execbuffer *eb, bool force)
696c349dbc7Sjsg {
697c349dbc7Sjsg 	const unsigned int count = eb->buffer_count;
6981bb76ff1Sjsg 	unsigned int i;
699c349dbc7Sjsg 	struct list_head last;
7001bb76ff1Sjsg 	bool unpinned = false;
701c349dbc7Sjsg 
702c349dbc7Sjsg 	/* Resort *all* the objects into priority order */
703c349dbc7Sjsg 	INIT_LIST_HEAD(&eb->unbound);
704c349dbc7Sjsg 	INIT_LIST_HEAD(&last);
705c349dbc7Sjsg 
7061bb76ff1Sjsg 	for (i = 0; i < count; i++) {
7071bb76ff1Sjsg 		struct eb_vma *ev = &eb->vma[i];
7081bb76ff1Sjsg 		unsigned int flags = ev->flags;
7091bb76ff1Sjsg 
7101bb76ff1Sjsg 		if (!force && flags & EXEC_OBJECT_PINNED &&
711c349dbc7Sjsg 		    flags & __EXEC_OBJECT_HAS_PIN)
712c349dbc7Sjsg 			continue;
713c349dbc7Sjsg 
7141bb76ff1Sjsg 		unpinned = true;
715c349dbc7Sjsg 		eb_unreserve_vma(ev);
716c349dbc7Sjsg 
717c349dbc7Sjsg 		if (flags & EXEC_OBJECT_PINNED)
718c349dbc7Sjsg 			/* Pinned must have their slot */
719c349dbc7Sjsg 			list_add(&ev->bind_link, &eb->unbound);
720c349dbc7Sjsg 		else if (flags & __EXEC_OBJECT_NEEDS_MAP)
721c349dbc7Sjsg 			/* Map require the lowest 256MiB (aperture) */
722c349dbc7Sjsg 			list_add_tail(&ev->bind_link, &eb->unbound);
723c349dbc7Sjsg 		else if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
724c349dbc7Sjsg 			/* Prioritise 4GiB region for restricted bo */
725c349dbc7Sjsg 			list_add(&ev->bind_link, &last);
726c349dbc7Sjsg 		else
727c349dbc7Sjsg 			list_add_tail(&ev->bind_link, &last);
728c349dbc7Sjsg 	}
7291bb76ff1Sjsg 
730c349dbc7Sjsg 	list_splice_tail(&last, &eb->unbound);
7311bb76ff1Sjsg 	return unpinned;
7321bb76ff1Sjsg }
733c349dbc7Sjsg 
eb_reserve(struct i915_execbuffer * eb)7341bb76ff1Sjsg static int eb_reserve(struct i915_execbuffer *eb)
7351bb76ff1Sjsg {
7361bb76ff1Sjsg 	struct eb_vma *ev;
7371bb76ff1Sjsg 	unsigned int pass;
7381bb76ff1Sjsg 	int err = 0;
739c349dbc7Sjsg 
7401bb76ff1Sjsg 	/*
7416823e11cSjsg 	 * We have one more buffers that we couldn't bind, which could be due to
7426823e11cSjsg 	 * various reasons. To resolve this we have 4 passes, with every next
7436823e11cSjsg 	 * level turning the screws tighter:
7441bb76ff1Sjsg 	 *
7456823e11cSjsg 	 * 0. Unbind all objects that do not match the GTT constraints for the
7466823e11cSjsg 	 * execbuffer (fenceable, mappable, alignment etc). Bind all new
7476823e11cSjsg 	 * objects.  This avoids unnecessary unbinding of later objects in order
7486823e11cSjsg 	 * to make room for the earlier objects *unless* we need to defragment.
7491bb76ff1Sjsg 	 *
7506823e11cSjsg 	 * 1. Reorder the buffers, where objects with the most restrictive
7516823e11cSjsg 	 * placement requirements go first (ignoring fixed location buffers for
7526823e11cSjsg 	 * now).  For example, objects needing the mappable aperture (the first
7536823e11cSjsg 	 * 256M of GTT), should go first vs objects that can be placed just
7546823e11cSjsg 	 * about anywhere. Repeat the previous pass.
7551bb76ff1Sjsg 	 *
7566823e11cSjsg 	 * 2. Consider buffers that are pinned at a fixed location. Also try to
7576823e11cSjsg 	 * evict the entire VM this time, leaving only objects that we were
7586823e11cSjsg 	 * unable to lock. Try again to bind the buffers. (still using the new
7596823e11cSjsg 	 * buffer order).
7606823e11cSjsg 	 *
7616823e11cSjsg 	 * 3. We likely have object lock contention for one or more stubborn
7626823e11cSjsg 	 * objects in the VM, for which we need to evict to make forward
7636823e11cSjsg 	 * progress (perhaps we are fighting the shrinker?). When evicting the
7646823e11cSjsg 	 * VM this time around, anything that we can't lock we now track using
7656823e11cSjsg 	 * the busy_bo, using the full lock (after dropping the vm->mutex to
7666823e11cSjsg 	 * prevent deadlocks), instead of trylock. We then continue to evict the
7676823e11cSjsg 	 * VM, this time with the stubborn object locked, which we can now
7686823e11cSjsg 	 * hopefully unbind (if still bound in the VM). Repeat until the VM is
7696823e11cSjsg 	 * evicted. Finally we should be able bind everything.
7701bb76ff1Sjsg 	 */
7716823e11cSjsg 	for (pass = 0; pass <= 3; pass++) {
7721bb76ff1Sjsg 		int pin_flags = PIN_USER | PIN_VALIDATE;
7731bb76ff1Sjsg 
7741bb76ff1Sjsg 		if (pass == 0)
7751bb76ff1Sjsg 			pin_flags |= PIN_NONBLOCK;
7761bb76ff1Sjsg 
7771bb76ff1Sjsg 		if (pass >= 1)
778*f005ef32Sjsg 			eb_unbind(eb, pass >= 2);
7791bb76ff1Sjsg 
7801bb76ff1Sjsg 		if (pass == 2) {
7811bb76ff1Sjsg 			err = mutex_lock_interruptible(&eb->context->vm->mutex);
7821bb76ff1Sjsg 			if (!err) {
7836823e11cSjsg 				err = i915_gem_evict_vm(eb->context->vm, &eb->ww, NULL);
784c349dbc7Sjsg 				mutex_unlock(&eb->context->vm->mutex);
7851bb76ff1Sjsg 			}
786c349dbc7Sjsg 			if (err)
787ad8b1aafSjsg 				return err;
7881bb76ff1Sjsg 		}
7891bb76ff1Sjsg 
7906823e11cSjsg 		if (pass == 3) {
7916823e11cSjsg retry:
7926823e11cSjsg 			err = mutex_lock_interruptible(&eb->context->vm->mutex);
7936823e11cSjsg 			if (!err) {
7946823e11cSjsg 				struct drm_i915_gem_object *busy_bo = NULL;
7956823e11cSjsg 
7966823e11cSjsg 				err = i915_gem_evict_vm(eb->context->vm, &eb->ww, &busy_bo);
7976823e11cSjsg 				mutex_unlock(&eb->context->vm->mutex);
7986823e11cSjsg 				if (err && busy_bo) {
7996823e11cSjsg 					err = i915_gem_object_lock(busy_bo, &eb->ww);
8006823e11cSjsg 					i915_gem_object_put(busy_bo);
8016823e11cSjsg 					if (!err)
8026823e11cSjsg 						goto retry;
8036823e11cSjsg 				}
8046823e11cSjsg 			}
8056823e11cSjsg 			if (err)
8066823e11cSjsg 				return err;
8076823e11cSjsg 		}
8086823e11cSjsg 
8091bb76ff1Sjsg 		list_for_each_entry(ev, &eb->unbound, bind_link) {
8101bb76ff1Sjsg 			err = eb_reserve_vma(eb, ev, pin_flags);
8111bb76ff1Sjsg 			if (err)
812c349dbc7Sjsg 				break;
813c349dbc7Sjsg 		}
814c349dbc7Sjsg 
8151bb76ff1Sjsg 		if (err != -ENOSPC)
8161bb76ff1Sjsg 			break;
817c349dbc7Sjsg 	}
818c349dbc7Sjsg 
8191bb76ff1Sjsg 	return err;
820c349dbc7Sjsg }
821c349dbc7Sjsg 
eb_select_context(struct i915_execbuffer * eb)822c349dbc7Sjsg static int eb_select_context(struct i915_execbuffer *eb)
823c349dbc7Sjsg {
824c349dbc7Sjsg 	struct i915_gem_context *ctx;
825c349dbc7Sjsg 
826c349dbc7Sjsg 	ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1);
8275ca02815Sjsg 	if (unlikely(IS_ERR(ctx)))
8285ca02815Sjsg 		return PTR_ERR(ctx);
829c349dbc7Sjsg 
830c349dbc7Sjsg 	eb->gem_context = ctx;
8311bb76ff1Sjsg 	if (i915_gem_context_has_full_ppgtt(ctx))
832c349dbc7Sjsg 		eb->invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
833c349dbc7Sjsg 
834c349dbc7Sjsg 	return 0;
835c349dbc7Sjsg }
836c349dbc7Sjsg 
__eb_add_lut(struct i915_execbuffer * eb,u32 handle,struct i915_vma * vma)837ad8b1aafSjsg static int __eb_add_lut(struct i915_execbuffer *eb,
838ad8b1aafSjsg 			u32 handle, struct i915_vma *vma)
839c349dbc7Sjsg {
840ad8b1aafSjsg 	struct i915_gem_context *ctx = eb->gem_context;
841ad8b1aafSjsg 	struct i915_lut_handle *lut;
842c349dbc7Sjsg 	int err;
843c349dbc7Sjsg 
844c349dbc7Sjsg 	lut = i915_lut_handle_alloc();
845ad8b1aafSjsg 	if (unlikely(!lut))
846ad8b1aafSjsg 		return -ENOMEM;
847c349dbc7Sjsg 
848ad8b1aafSjsg 	i915_vma_get(vma);
849c349dbc7Sjsg 	if (!atomic_fetch_inc(&vma->open_count))
850c349dbc7Sjsg 		i915_vma_reopen(vma);
851c349dbc7Sjsg 	lut->handle = handle;
852ad8b1aafSjsg 	lut->ctx = ctx;
853c349dbc7Sjsg 
854ad8b1aafSjsg 	/* Check that the context hasn't been closed in the meantime */
855ad8b1aafSjsg 	err = -EINTR;
856ad8b1aafSjsg 	if (!mutex_lock_interruptible(&ctx->lut_mutex)) {
8571bb76ff1Sjsg 		if (likely(!i915_gem_context_is_closed(ctx)))
858ad8b1aafSjsg 			err = radix_tree_insert(&ctx->handles_vma, handle, vma);
859ad8b1aafSjsg 		else
860ad8b1aafSjsg 			err = -ENOENT;
861ad8b1aafSjsg 		if (err == 0) { /* And nor has this handle */
862ad8b1aafSjsg 			struct drm_i915_gem_object *obj = vma->obj;
863ad8b1aafSjsg 
864ad8b1aafSjsg 			spin_lock(&obj->lut_lock);
865ad8b1aafSjsg 			if (idr_find(&eb->file->object_idr, handle) == obj) {
866c349dbc7Sjsg 				list_add(&lut->obj_link, &obj->lut_list);
867ad8b1aafSjsg 			} else {
868ad8b1aafSjsg 				radix_tree_delete(&ctx->handles_vma, handle);
869ad8b1aafSjsg 				err = -ENOENT;
870ad8b1aafSjsg 			}
871ad8b1aafSjsg 			spin_unlock(&obj->lut_lock);
872ad8b1aafSjsg 		}
873ad8b1aafSjsg 		mutex_unlock(&ctx->lut_mutex);
874ad8b1aafSjsg 	}
875c349dbc7Sjsg 	if (unlikely(err))
876ad8b1aafSjsg 		goto err;
877ad8b1aafSjsg 
878ad8b1aafSjsg 	return 0;
879ad8b1aafSjsg 
880ad8b1aafSjsg err:
881ad8b1aafSjsg 	i915_vma_close(vma);
882ad8b1aafSjsg 	i915_vma_put(vma);
883ad8b1aafSjsg 	i915_lut_handle_free(lut);
884ad8b1aafSjsg 	return err;
885ad8b1aafSjsg }
886ad8b1aafSjsg 
eb_lookup_vma(struct i915_execbuffer * eb,u32 handle)887ad8b1aafSjsg static struct i915_vma *eb_lookup_vma(struct i915_execbuffer *eb, u32 handle)
888ad8b1aafSjsg {
889ad8b1aafSjsg 	struct i915_address_space *vm = eb->context->vm;
890ad8b1aafSjsg 
891ad8b1aafSjsg 	do {
892ad8b1aafSjsg 		struct drm_i915_gem_object *obj;
893ad8b1aafSjsg 		struct i915_vma *vma;
894ad8b1aafSjsg 		int err;
895ad8b1aafSjsg 
896ad8b1aafSjsg 		rcu_read_lock();
897ad8b1aafSjsg 		vma = radix_tree_lookup(&eb->gem_context->handles_vma, handle);
898ad8b1aafSjsg 		if (likely(vma && vma->vm == vm))
899ad8b1aafSjsg 			vma = i915_vma_tryget(vma);
900ad8b1aafSjsg 		rcu_read_unlock();
901ad8b1aafSjsg 		if (likely(vma))
902ad8b1aafSjsg 			return vma;
903ad8b1aafSjsg 
904ad8b1aafSjsg 		obj = i915_gem_object_lookup(eb->file, handle);
905ad8b1aafSjsg 		if (unlikely(!obj))
906ad8b1aafSjsg 			return ERR_PTR(-ENOENT);
907ad8b1aafSjsg 
9081bb76ff1Sjsg 		/*
9091bb76ff1Sjsg 		 * If the user has opted-in for protected-object tracking, make
9101bb76ff1Sjsg 		 * sure the object encryption can be used.
9111bb76ff1Sjsg 		 * We only need to do this when the object is first used with
9121bb76ff1Sjsg 		 * this context, because the context itself will be banned when
9131bb76ff1Sjsg 		 * the protected objects become invalid.
9141bb76ff1Sjsg 		 */
9151bb76ff1Sjsg 		if (i915_gem_context_uses_protected_content(eb->gem_context) &&
9161bb76ff1Sjsg 		    i915_gem_object_is_protected(obj)) {
917*f005ef32Sjsg 			err = intel_pxp_key_check(eb->i915->pxp, obj, true);
9181bb76ff1Sjsg 			if (err) {
9191bb76ff1Sjsg 				i915_gem_object_put(obj);
9201bb76ff1Sjsg 				return ERR_PTR(err);
9211bb76ff1Sjsg 			}
9221bb76ff1Sjsg 		}
9231bb76ff1Sjsg 
924ad8b1aafSjsg 		vma = i915_vma_instance(obj, vm, NULL);
925ad8b1aafSjsg 		if (IS_ERR(vma)) {
926ad8b1aafSjsg 			i915_gem_object_put(obj);
927ad8b1aafSjsg 			return vma;
928ad8b1aafSjsg 		}
929ad8b1aafSjsg 
930ad8b1aafSjsg 		err = __eb_add_lut(eb, handle, vma);
931ad8b1aafSjsg 		if (likely(!err))
932ad8b1aafSjsg 			return vma;
933ad8b1aafSjsg 
934ad8b1aafSjsg 		i915_gem_object_put(obj);
935ad8b1aafSjsg 		if (err != -EEXIST)
936ad8b1aafSjsg 			return ERR_PTR(err);
937ad8b1aafSjsg 	} while (1);
938ad8b1aafSjsg }
939ad8b1aafSjsg 
eb_lookup_vmas(struct i915_execbuffer * eb)940ad8b1aafSjsg static int eb_lookup_vmas(struct i915_execbuffer *eb)
941ad8b1aafSjsg {
9421bb76ff1Sjsg 	unsigned int i, current_batch = 0;
943ad8b1aafSjsg 	int err = 0;
944ad8b1aafSjsg 
945ad8b1aafSjsg 	INIT_LIST_HEAD(&eb->relocs);
946ad8b1aafSjsg 
947ad8b1aafSjsg 	for (i = 0; i < eb->buffer_count; i++) {
948ad8b1aafSjsg 		struct i915_vma *vma;
949ad8b1aafSjsg 
950ad8b1aafSjsg 		vma = eb_lookup_vma(eb, eb->exec[i].handle);
951ad8b1aafSjsg 		if (IS_ERR(vma)) {
952ad8b1aafSjsg 			err = PTR_ERR(vma);
953ad8b1aafSjsg 			goto err;
954ad8b1aafSjsg 		}
955ad8b1aafSjsg 
956ad8b1aafSjsg 		err = eb_validate_vma(eb, &eb->exec[i], vma);
957ad8b1aafSjsg 		if (unlikely(err)) {
958ad8b1aafSjsg 			i915_vma_put(vma);
959ad8b1aafSjsg 			goto err;
960ad8b1aafSjsg 		}
961c349dbc7Sjsg 
9621bb76ff1Sjsg 		err = eb_add_vma(eb, &current_batch, i, vma);
9631bb76ff1Sjsg 		if (err)
9641bb76ff1Sjsg 			return err;
9655ca02815Sjsg 
9665ca02815Sjsg 		if (i915_gem_object_is_userptr(vma->obj)) {
9675ca02815Sjsg 			err = i915_gem_object_userptr_submit_init(vma->obj);
9685ca02815Sjsg 			if (err) {
9695ca02815Sjsg 				if (i + 1 < eb->buffer_count) {
9705ca02815Sjsg 					/*
9715ca02815Sjsg 					 * Execbuffer code expects last vma entry to be NULL,
9725ca02815Sjsg 					 * since we already initialized this entry,
9735ca02815Sjsg 					 * set the next value to NULL or we mess up
9745ca02815Sjsg 					 * cleanup handling.
9755ca02815Sjsg 					 */
9765ca02815Sjsg 					eb->vma[i + 1].vma = NULL;
9775ca02815Sjsg 				}
9785ca02815Sjsg 
9795ca02815Sjsg 				return err;
9805ca02815Sjsg 			}
9815ca02815Sjsg 
9825ca02815Sjsg 			eb->vma[i].flags |= __EXEC_OBJECT_USERPTR_INIT;
9835ca02815Sjsg 			eb->args->flags |= __EXEC_USERPTR_USED;
9845ca02815Sjsg 		}
985c349dbc7Sjsg 	}
986c349dbc7Sjsg 
987c349dbc7Sjsg 	return 0;
988c349dbc7Sjsg 
989ad8b1aafSjsg err:
990c349dbc7Sjsg 	eb->vma[i].vma = NULL;
991c349dbc7Sjsg 	return err;
992c349dbc7Sjsg }
993c349dbc7Sjsg 
eb_lock_vmas(struct i915_execbuffer * eb)9945ca02815Sjsg static int eb_lock_vmas(struct i915_execbuffer *eb)
9955ca02815Sjsg {
9965ca02815Sjsg 	unsigned int i;
9975ca02815Sjsg 	int err;
9985ca02815Sjsg 
9995ca02815Sjsg 	for (i = 0; i < eb->buffer_count; i++) {
10005ca02815Sjsg 		struct eb_vma *ev = &eb->vma[i];
10015ca02815Sjsg 		struct i915_vma *vma = ev->vma;
10025ca02815Sjsg 
10035ca02815Sjsg 		err = i915_gem_object_lock(vma->obj, &eb->ww);
10045ca02815Sjsg 		if (err)
10055ca02815Sjsg 			return err;
10065ca02815Sjsg 	}
10075ca02815Sjsg 
10085ca02815Sjsg 	return 0;
10095ca02815Sjsg }
10105ca02815Sjsg 
eb_validate_vmas(struct i915_execbuffer * eb)1011ad8b1aafSjsg static int eb_validate_vmas(struct i915_execbuffer *eb)
1012ad8b1aafSjsg {
1013ad8b1aafSjsg 	unsigned int i;
1014ad8b1aafSjsg 	int err;
1015ad8b1aafSjsg 
1016ad8b1aafSjsg 	INIT_LIST_HEAD(&eb->unbound);
1017ad8b1aafSjsg 
10185ca02815Sjsg 	err = eb_lock_vmas(eb);
10195ca02815Sjsg 	if (err)
10205ca02815Sjsg 		return err;
10215ca02815Sjsg 
1022ad8b1aafSjsg 	for (i = 0; i < eb->buffer_count; i++) {
1023ad8b1aafSjsg 		struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
1024ad8b1aafSjsg 		struct eb_vma *ev = &eb->vma[i];
1025ad8b1aafSjsg 		struct i915_vma *vma = ev->vma;
1026ad8b1aafSjsg 
10275ca02815Sjsg 		err = eb_pin_vma(eb, entry, ev);
10285ca02815Sjsg 		if (err == -EDEADLK)
1029ad8b1aafSjsg 			return err;
1030ad8b1aafSjsg 
10315ca02815Sjsg 		if (!err) {
1032*f005ef32Sjsg 			if (entry->offset != i915_vma_offset(vma)) {
1033*f005ef32Sjsg 				entry->offset = i915_vma_offset(vma) | UPDATE;
1034ad8b1aafSjsg 				eb->args->flags |= __EXEC_HAS_RELOC;
1035ad8b1aafSjsg 			}
1036ad8b1aafSjsg 		} else {
1037ad8b1aafSjsg 			eb_unreserve_vma(ev);
1038ad8b1aafSjsg 
1039ad8b1aafSjsg 			list_add_tail(&ev->bind_link, &eb->unbound);
1040ad8b1aafSjsg 			if (drm_mm_node_allocated(&vma->node)) {
1041ad8b1aafSjsg 				err = i915_vma_unbind(vma);
1042ad8b1aafSjsg 				if (err)
1043ad8b1aafSjsg 					return err;
1044ad8b1aafSjsg 			}
1045ad8b1aafSjsg 		}
1046ad8b1aafSjsg 
10471bb76ff1Sjsg 		/* Reserve enough slots to accommodate composite fences */
10481bb76ff1Sjsg 		err = dma_resv_reserve_fences(vma->obj->base.resv, eb->num_batches);
10495ca02815Sjsg 		if (err)
10505ca02815Sjsg 			return err;
10515ca02815Sjsg 
1052ad8b1aafSjsg 		GEM_BUG_ON(drm_mm_node_allocated(&vma->node) &&
1053ad8b1aafSjsg 			   eb_vma_misplaced(&eb->exec[i], vma, ev->flags));
1054ad8b1aafSjsg 	}
1055ad8b1aafSjsg 
1056ad8b1aafSjsg 	if (!list_empty(&eb->unbound))
1057ad8b1aafSjsg 		return eb_reserve(eb);
1058ad8b1aafSjsg 
1059ad8b1aafSjsg 	return 0;
1060ad8b1aafSjsg }
1061ad8b1aafSjsg 
1062c349dbc7Sjsg static struct eb_vma *
eb_get_vma(const struct i915_execbuffer * eb,unsigned long handle)1063c349dbc7Sjsg eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
1064c349dbc7Sjsg {
1065c349dbc7Sjsg 	if (eb->lut_size < 0) {
1066c349dbc7Sjsg 		if (handle >= -eb->lut_size)
1067c349dbc7Sjsg 			return NULL;
1068c349dbc7Sjsg 		return &eb->vma[handle];
1069c349dbc7Sjsg 	} else {
1070c349dbc7Sjsg 		struct hlist_head *head;
1071c349dbc7Sjsg 		struct eb_vma *ev;
1072c349dbc7Sjsg 
1073c349dbc7Sjsg 		head = &eb->buckets[hash_32(handle, eb->lut_size)];
1074c349dbc7Sjsg 		hlist_for_each_entry(ev, head, node) {
1075c349dbc7Sjsg 			if (ev->handle == handle)
1076c349dbc7Sjsg 				return ev;
1077c349dbc7Sjsg 		}
1078c349dbc7Sjsg 		return NULL;
1079c349dbc7Sjsg 	}
1080c349dbc7Sjsg }
1081c349dbc7Sjsg 
eb_release_vmas(struct i915_execbuffer * eb,bool final)1082ad8b1aafSjsg static void eb_release_vmas(struct i915_execbuffer *eb, bool final)
1083c349dbc7Sjsg {
1084c349dbc7Sjsg 	const unsigned int count = eb->buffer_count;
1085c349dbc7Sjsg 	unsigned int i;
1086c349dbc7Sjsg 
1087c349dbc7Sjsg 	for (i = 0; i < count; i++) {
1088c349dbc7Sjsg 		struct eb_vma *ev = &eb->vma[i];
1089c349dbc7Sjsg 		struct i915_vma *vma = ev->vma;
1090c349dbc7Sjsg 
1091c349dbc7Sjsg 		if (!vma)
1092c349dbc7Sjsg 			break;
1093c349dbc7Sjsg 
1094ad8b1aafSjsg 		eb_unreserve_vma(ev);
1095c349dbc7Sjsg 
1096ad8b1aafSjsg 		if (final)
1097c349dbc7Sjsg 			i915_vma_put(vma);
1098c349dbc7Sjsg 	}
1099ad8b1aafSjsg 
11001bb76ff1Sjsg 	eb_capture_release(eb);
1101ad8b1aafSjsg 	eb_unpin_engine(eb);
1102c349dbc7Sjsg }
1103c349dbc7Sjsg 
eb_destroy(const struct i915_execbuffer * eb)1104c349dbc7Sjsg static void eb_destroy(const struct i915_execbuffer *eb)
1105c349dbc7Sjsg {
1106c349dbc7Sjsg 	if (eb->lut_size > 0)
1107c349dbc7Sjsg 		kfree(eb->buckets);
1108c349dbc7Sjsg }
1109c349dbc7Sjsg 
1110c349dbc7Sjsg static inline u64
relocation_target(const struct drm_i915_gem_relocation_entry * reloc,const struct i915_vma * target)1111c349dbc7Sjsg relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
1112c349dbc7Sjsg 		  const struct i915_vma *target)
1113c349dbc7Sjsg {
1114*f005ef32Sjsg 	return gen8_canonical_addr((int)reloc->delta + i915_vma_offset(target));
1115c349dbc7Sjsg }
1116c349dbc7Sjsg 
reloc_cache_init(struct reloc_cache * cache,struct drm_i915_private * i915)1117c349dbc7Sjsg static void reloc_cache_init(struct reloc_cache *cache,
1118c349dbc7Sjsg 			     struct drm_i915_private *i915)
1119c349dbc7Sjsg {
1120c349dbc7Sjsg 	cache->page = -1;
1121c349dbc7Sjsg 	cache->vaddr = 0;
1122c349dbc7Sjsg 	/* Must be a variable in the struct to allow GCC to unroll. */
11235ca02815Sjsg 	cache->graphics_ver = GRAPHICS_VER(i915);
1124c349dbc7Sjsg 	cache->has_llc = HAS_LLC(i915);
1125c349dbc7Sjsg 	cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
11265ca02815Sjsg 	cache->has_fence = cache->graphics_ver < 4;
1127c349dbc7Sjsg 	cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
1128c349dbc7Sjsg 	cache->node.flags = 0;
1129c349dbc7Sjsg }
1130c349dbc7Sjsg 
unmask_page(unsigned long p)1131c349dbc7Sjsg static inline void *unmask_page(unsigned long p)
1132c349dbc7Sjsg {
1133ad8b1aafSjsg 	return (void *)(uintptr_t)(p & LINUX_PAGE_MASK);
1134c349dbc7Sjsg }
1135c349dbc7Sjsg 
unmask_flags(unsigned long p)1136c349dbc7Sjsg static inline unsigned int unmask_flags(unsigned long p)
1137c349dbc7Sjsg {
1138ad8b1aafSjsg 	return p & ~LINUX_PAGE_MASK;
1139c349dbc7Sjsg }
1140c349dbc7Sjsg 
1141c349dbc7Sjsg #define KMAP 0x4 /* after CLFLUSH_FLAGS */
1142c349dbc7Sjsg 
cache_to_ggtt(struct reloc_cache * cache)1143c349dbc7Sjsg static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
1144c349dbc7Sjsg {
1145c349dbc7Sjsg 	struct drm_i915_private *i915 =
1146c349dbc7Sjsg 		container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
11471bb76ff1Sjsg 	return to_gt(i915)->ggtt;
1148c349dbc7Sjsg }
1149c349dbc7Sjsg 
reloc_cache_unmap(struct reloc_cache * cache)1150543bcfd9Sjsg static void reloc_cache_unmap(struct reloc_cache *cache)
1151543bcfd9Sjsg {
1152543bcfd9Sjsg 	void *vaddr;
1153543bcfd9Sjsg 
1154543bcfd9Sjsg 	if (!cache->vaddr)
1155543bcfd9Sjsg 		return;
1156543bcfd9Sjsg 
1157543bcfd9Sjsg 	vaddr = unmask_page(cache->vaddr);
1158543bcfd9Sjsg 	if (cache->vaddr & KMAP)
1159543bcfd9Sjsg 		kunmap_atomic(vaddr);
1160543bcfd9Sjsg 	else
1161543bcfd9Sjsg 		io_mapping_unmap_atomic((void __iomem *)vaddr);
1162543bcfd9Sjsg }
1163543bcfd9Sjsg 
reloc_cache_remap(struct reloc_cache * cache,struct drm_i915_gem_object * obj)1164543bcfd9Sjsg static void reloc_cache_remap(struct reloc_cache *cache,
1165543bcfd9Sjsg 			      struct drm_i915_gem_object *obj)
1166543bcfd9Sjsg {
1167543bcfd9Sjsg 	void *vaddr;
1168543bcfd9Sjsg 
1169543bcfd9Sjsg 	if (!cache->vaddr)
1170543bcfd9Sjsg 		return;
1171543bcfd9Sjsg 
1172543bcfd9Sjsg 	if (cache->vaddr & KMAP) {
1173543bcfd9Sjsg 		struct vm_page *page = i915_gem_object_get_page(obj, cache->page);
1174543bcfd9Sjsg 
1175543bcfd9Sjsg 		vaddr = kmap_atomic(page);
1176543bcfd9Sjsg 		cache->vaddr = unmask_flags(cache->vaddr) |
1177543bcfd9Sjsg 			(unsigned long)vaddr;
1178543bcfd9Sjsg 	} else {
1179543bcfd9Sjsg 		struct i915_ggtt *ggtt = cache_to_ggtt(cache);
1180543bcfd9Sjsg 		unsigned long offset;
1181543bcfd9Sjsg 
1182543bcfd9Sjsg 		offset = cache->node.start;
1183543bcfd9Sjsg 		if (!drm_mm_node_allocated(&cache->node))
1184543bcfd9Sjsg 			offset += cache->page << PAGE_SHIFT;
1185543bcfd9Sjsg 
1186543bcfd9Sjsg 		cache->vaddr = (unsigned long)
1187543bcfd9Sjsg 			io_mapping_map_atomic_wc(&ggtt->iomap, offset);
1188543bcfd9Sjsg 	}
1189543bcfd9Sjsg }
1190543bcfd9Sjsg 
reloc_cache_reset(struct reloc_cache * cache,struct i915_execbuffer * eb)1191ad8b1aafSjsg static void reloc_cache_reset(struct reloc_cache *cache, struct i915_execbuffer *eb)
1192c349dbc7Sjsg {
1193c349dbc7Sjsg 	void *vaddr;
1194c349dbc7Sjsg 
1195c349dbc7Sjsg 	if (!cache->vaddr)
1196c349dbc7Sjsg 		return;
1197c349dbc7Sjsg 
1198c349dbc7Sjsg 	vaddr = unmask_page(cache->vaddr);
1199c349dbc7Sjsg 	if (cache->vaddr & KMAP) {
1200ad8b1aafSjsg 		struct drm_i915_gem_object *obj =
1201ad8b1aafSjsg 			(struct drm_i915_gem_object *)cache->node.mm;
1202c349dbc7Sjsg 		if (cache->vaddr & CLFLUSH_AFTER)
1203c349dbc7Sjsg 			mb();
1204c349dbc7Sjsg 
1205c349dbc7Sjsg 		kunmap_atomic(vaddr);
1206ad8b1aafSjsg 		i915_gem_object_finish_access(obj);
1207c349dbc7Sjsg 	} else {
1208c349dbc7Sjsg 		struct i915_ggtt *ggtt = cache_to_ggtt(cache);
1209c349dbc7Sjsg 
1210c349dbc7Sjsg 		intel_gt_flush_ggtt_writes(ggtt->vm.gt);
1211c349dbc7Sjsg 		io_mapping_unmap_atomic((void __iomem *)vaddr);
1212c349dbc7Sjsg 
1213c349dbc7Sjsg 		if (drm_mm_node_allocated(&cache->node)) {
1214c349dbc7Sjsg 			ggtt->vm.clear_range(&ggtt->vm,
1215c349dbc7Sjsg 					     cache->node.start,
1216c349dbc7Sjsg 					     cache->node.size);
1217c349dbc7Sjsg 			mutex_lock(&ggtt->vm.mutex);
1218c349dbc7Sjsg 			drm_mm_remove_node(&cache->node);
1219c349dbc7Sjsg 			mutex_unlock(&ggtt->vm.mutex);
1220c349dbc7Sjsg 		} else {
1221c349dbc7Sjsg 			i915_vma_unpin((struct i915_vma *)cache->node.mm);
1222c349dbc7Sjsg 		}
1223c349dbc7Sjsg 	}
1224c349dbc7Sjsg 
1225c349dbc7Sjsg 	cache->vaddr = 0;
1226c349dbc7Sjsg 	cache->page = -1;
1227c349dbc7Sjsg }
1228c349dbc7Sjsg 
reloc_kmap(struct drm_i915_gem_object * obj,struct reloc_cache * cache,unsigned long pageno)1229c349dbc7Sjsg static void *reloc_kmap(struct drm_i915_gem_object *obj,
1230c349dbc7Sjsg 			struct reloc_cache *cache,
1231ad8b1aafSjsg 			unsigned long pageno)
1232c349dbc7Sjsg {
1233c349dbc7Sjsg 	void *vaddr;
1234ad8b1aafSjsg 	struct vm_page *page;
1235c349dbc7Sjsg 
1236c349dbc7Sjsg 	if (cache->vaddr) {
1237c349dbc7Sjsg 		kunmap_atomic(unmask_page(cache->vaddr));
1238c349dbc7Sjsg 	} else {
1239c349dbc7Sjsg 		unsigned int flushes;
1240c349dbc7Sjsg 		int err;
1241c349dbc7Sjsg 
1242c349dbc7Sjsg 		err = i915_gem_object_prepare_write(obj, &flushes);
1243c349dbc7Sjsg 		if (err)
1244c349dbc7Sjsg 			return ERR_PTR(err);
1245c349dbc7Sjsg 
1246c349dbc7Sjsg 		BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
1247ad8b1aafSjsg 		BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & LINUX_PAGE_MASK);
1248c349dbc7Sjsg 
1249c349dbc7Sjsg 		cache->vaddr = flushes | KMAP;
1250c349dbc7Sjsg 		cache->node.mm = (void *)obj;
1251c349dbc7Sjsg 		if (flushes)
1252c349dbc7Sjsg 			mb();
1253c349dbc7Sjsg 	}
1254c349dbc7Sjsg 
1255ad8b1aafSjsg 	page = i915_gem_object_get_page(obj, pageno);
1256ad8b1aafSjsg 	if (!obj->mm.dirty)
1257ad8b1aafSjsg 		set_page_dirty(page);
1258ad8b1aafSjsg 
1259ad8b1aafSjsg 	vaddr = kmap_atomic(page);
1260c349dbc7Sjsg 	cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
1261ad8b1aafSjsg 	cache->page = pageno;
1262c349dbc7Sjsg 
1263c349dbc7Sjsg 	return vaddr;
1264c349dbc7Sjsg }
1265c349dbc7Sjsg 
reloc_iomap(struct i915_vma * batch,struct i915_execbuffer * eb,unsigned long page)12661bb76ff1Sjsg static void *reloc_iomap(struct i915_vma *batch,
1267ad8b1aafSjsg 			 struct i915_execbuffer *eb,
1268c349dbc7Sjsg 			 unsigned long page)
1269c349dbc7Sjsg {
12701bb76ff1Sjsg 	struct drm_i915_gem_object *obj = batch->obj;
1271ad8b1aafSjsg 	struct reloc_cache *cache = &eb->reloc_cache;
1272c349dbc7Sjsg 	struct i915_ggtt *ggtt = cache_to_ggtt(cache);
1273c349dbc7Sjsg 	unsigned long offset;
1274c349dbc7Sjsg 	void *vaddr;
1275c349dbc7Sjsg 
1276c349dbc7Sjsg 	if (cache->vaddr) {
1277c349dbc7Sjsg 		intel_gt_flush_ggtt_writes(ggtt->vm.gt);
1278c349dbc7Sjsg 		io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
1279c349dbc7Sjsg 	} else {
12801bb76ff1Sjsg 		struct i915_vma *vma = ERR_PTR(-ENODEV);
1281c349dbc7Sjsg 		int err;
1282c349dbc7Sjsg 
1283c349dbc7Sjsg 		if (i915_gem_object_is_tiled(obj))
1284c349dbc7Sjsg 			return ERR_PTR(-EINVAL);
1285c349dbc7Sjsg 
1286c349dbc7Sjsg 		if (use_cpu_reloc(cache, obj))
1287c349dbc7Sjsg 			return NULL;
1288c349dbc7Sjsg 
1289c349dbc7Sjsg 		err = i915_gem_object_set_to_gtt_domain(obj, true);
1290c349dbc7Sjsg 		if (err)
1291c349dbc7Sjsg 			return ERR_PTR(err);
1292c349dbc7Sjsg 
12931bb76ff1Sjsg 		/*
12941bb76ff1Sjsg 		 * i915_gem_object_ggtt_pin_ww may attempt to remove the batch
12951bb76ff1Sjsg 		 * VMA from the object list because we no longer pin.
12961bb76ff1Sjsg 		 *
12971bb76ff1Sjsg 		 * Only attempt to pin the batch buffer to ggtt if the current batch
12981bb76ff1Sjsg 		 * is not inside ggtt, or the batch buffer is not misplaced.
12991bb76ff1Sjsg 		 */
13001bb76ff1Sjsg 		if (!i915_is_ggtt(batch->vm) ||
13011bb76ff1Sjsg 		    !i915_vma_misplaced(batch, 0, 0, PIN_MAPPABLE)) {
1302ad8b1aafSjsg 			vma = i915_gem_object_ggtt_pin_ww(obj, &eb->ww, NULL, 0, 0,
1303c349dbc7Sjsg 							  PIN_MAPPABLE |
1304c349dbc7Sjsg 							  PIN_NONBLOCK /* NOWARN */ |
1305c349dbc7Sjsg 							  PIN_NOEVICT);
13061bb76ff1Sjsg 		}
13071bb76ff1Sjsg 
1308ad8b1aafSjsg 		if (vma == ERR_PTR(-EDEADLK))
1309ad8b1aafSjsg 			return vma;
1310ad8b1aafSjsg 
1311c349dbc7Sjsg 		if (IS_ERR(vma)) {
1312c349dbc7Sjsg 			memset(&cache->node, 0, sizeof(cache->node));
1313c349dbc7Sjsg 			mutex_lock(&ggtt->vm.mutex);
1314c349dbc7Sjsg 			err = drm_mm_insert_node_in_range
1315c349dbc7Sjsg 				(&ggtt->vm.mm, &cache->node,
1316c349dbc7Sjsg 				 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
1317c349dbc7Sjsg 				 0, ggtt->mappable_end,
1318c349dbc7Sjsg 				 DRM_MM_INSERT_LOW);
1319c349dbc7Sjsg 			mutex_unlock(&ggtt->vm.mutex);
1320c349dbc7Sjsg 			if (err) /* no inactive aperture space, use cpu reloc */
1321c349dbc7Sjsg 				return NULL;
1322c349dbc7Sjsg 		} else {
1323*f005ef32Sjsg 			cache->node.start = i915_ggtt_offset(vma);
1324c349dbc7Sjsg 			cache->node.mm = (void *)vma;
1325c349dbc7Sjsg 		}
1326c349dbc7Sjsg 	}
1327c349dbc7Sjsg 
1328c349dbc7Sjsg 	offset = cache->node.start;
1329c349dbc7Sjsg 	if (drm_mm_node_allocated(&cache->node)) {
1330c349dbc7Sjsg 		ggtt->vm.insert_page(&ggtt->vm,
1331c349dbc7Sjsg 				     i915_gem_object_get_dma_address(obj, page),
1332*f005ef32Sjsg 				     offset,
1333*f005ef32Sjsg 				     i915_gem_get_pat_index(ggtt->vm.i915,
1334*f005ef32Sjsg 							    I915_CACHE_NONE),
1335*f005ef32Sjsg 				     0);
1336c349dbc7Sjsg 	} else {
1337c349dbc7Sjsg 		offset += page << PAGE_SHIFT;
1338c349dbc7Sjsg 	}
1339c349dbc7Sjsg 
1340c349dbc7Sjsg 	vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->iomap,
1341c349dbc7Sjsg 							 offset);
1342c349dbc7Sjsg 	cache->page = page;
1343c349dbc7Sjsg 	cache->vaddr = (unsigned long)vaddr;
1344c349dbc7Sjsg 
1345c349dbc7Sjsg 	return vaddr;
1346c349dbc7Sjsg }
1347c349dbc7Sjsg 
reloc_vaddr(struct i915_vma * vma,struct i915_execbuffer * eb,unsigned long page)13481bb76ff1Sjsg static void *reloc_vaddr(struct i915_vma *vma,
1349ad8b1aafSjsg 			 struct i915_execbuffer *eb,
1350c349dbc7Sjsg 			 unsigned long page)
1351c349dbc7Sjsg {
1352ad8b1aafSjsg 	struct reloc_cache *cache = &eb->reloc_cache;
1353c349dbc7Sjsg 	void *vaddr;
1354c349dbc7Sjsg 
1355c349dbc7Sjsg 	if (cache->page == page) {
1356c349dbc7Sjsg 		vaddr = unmask_page(cache->vaddr);
1357c349dbc7Sjsg 	} else {
1358c349dbc7Sjsg 		vaddr = NULL;
1359c349dbc7Sjsg 		if ((cache->vaddr & KMAP) == 0)
13601bb76ff1Sjsg 			vaddr = reloc_iomap(vma, eb, page);
1361c349dbc7Sjsg 		if (!vaddr)
13621bb76ff1Sjsg 			vaddr = reloc_kmap(vma->obj, cache, page);
1363c349dbc7Sjsg 	}
1364c349dbc7Sjsg 
1365c349dbc7Sjsg 	return vaddr;
1366c349dbc7Sjsg }
1367c349dbc7Sjsg 
clflush_write32(u32 * addr,u32 value,unsigned int flushes)1368c349dbc7Sjsg static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
1369c349dbc7Sjsg {
1370c349dbc7Sjsg 	if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
13711bb76ff1Sjsg 		if (flushes & CLFLUSH_BEFORE)
13721bb76ff1Sjsg 			drm_clflush_virt_range(addr, sizeof(*addr));
1373c349dbc7Sjsg 
1374c349dbc7Sjsg 		*addr = value;
1375c349dbc7Sjsg 
1376c349dbc7Sjsg 		/*
1377c349dbc7Sjsg 		 * Writes to the same cacheline are serialised by the CPU
1378c349dbc7Sjsg 		 * (including clflush). On the write path, we only require
1379c349dbc7Sjsg 		 * that it hits memory in an orderly fashion and place
1380c349dbc7Sjsg 		 * mb barriers at the start and end of the relocation phase
1381c349dbc7Sjsg 		 * to ensure ordering of clflush wrt to the system.
1382c349dbc7Sjsg 		 */
1383c349dbc7Sjsg 		if (flushes & CLFLUSH_AFTER)
13841bb76ff1Sjsg 			drm_clflush_virt_range(addr, sizeof(*addr));
1385c349dbc7Sjsg 	} else
1386c349dbc7Sjsg 		*addr = value;
1387c349dbc7Sjsg }
1388c349dbc7Sjsg 
1389ad8b1aafSjsg static u64
relocate_entry(struct i915_vma * vma,const struct drm_i915_gem_relocation_entry * reloc,struct i915_execbuffer * eb,const struct i915_vma * target)1390ad8b1aafSjsg relocate_entry(struct i915_vma *vma,
1391ad8b1aafSjsg 	       const struct drm_i915_gem_relocation_entry *reloc,
1392ad8b1aafSjsg 	       struct i915_execbuffer *eb,
1393ad8b1aafSjsg 	       const struct i915_vma *target)
1394ad8b1aafSjsg {
1395ad8b1aafSjsg 	u64 target_addr = relocation_target(reloc, target);
1396ad8b1aafSjsg 	u64 offset = reloc->offset;
1397ad8b1aafSjsg 	bool wide = eb->reloc_cache.use_64bit_reloc;
1398ad8b1aafSjsg 	void *vaddr;
1399ad8b1aafSjsg 
1400c349dbc7Sjsg repeat:
14011bb76ff1Sjsg 	vaddr = reloc_vaddr(vma, eb,
1402ad8b1aafSjsg 			    offset >> PAGE_SHIFT);
1403c349dbc7Sjsg 	if (IS_ERR(vaddr))
1404c349dbc7Sjsg 		return PTR_ERR(vaddr);
1405c349dbc7Sjsg 
1406ad8b1aafSjsg 	GEM_BUG_ON(!IS_ALIGNED(offset, sizeof(u32)));
1407c349dbc7Sjsg 	clflush_write32(vaddr + offset_in_page(offset),
1408ad8b1aafSjsg 			lower_32_bits(target_addr),
1409c349dbc7Sjsg 			eb->reloc_cache.vaddr);
1410c349dbc7Sjsg 
1411c349dbc7Sjsg 	if (wide) {
1412c349dbc7Sjsg 		offset += sizeof(u32);
1413ad8b1aafSjsg 		target_addr >>= 32;
1414c349dbc7Sjsg 		wide = false;
1415c349dbc7Sjsg 		goto repeat;
1416c349dbc7Sjsg 	}
1417c349dbc7Sjsg 
1418c349dbc7Sjsg 	return target->node.start | UPDATE;
1419c349dbc7Sjsg }
1420c349dbc7Sjsg 
1421c349dbc7Sjsg static u64
eb_relocate_entry(struct i915_execbuffer * eb,struct eb_vma * ev,const struct drm_i915_gem_relocation_entry * reloc)1422c349dbc7Sjsg eb_relocate_entry(struct i915_execbuffer *eb,
1423c349dbc7Sjsg 		  struct eb_vma *ev,
1424c349dbc7Sjsg 		  const struct drm_i915_gem_relocation_entry *reloc)
1425c349dbc7Sjsg {
1426c349dbc7Sjsg 	struct drm_i915_private *i915 = eb->i915;
1427c349dbc7Sjsg 	struct eb_vma *target;
1428c349dbc7Sjsg 	int err;
1429c349dbc7Sjsg 
1430c349dbc7Sjsg 	/* we've already hold a reference to all valid objects */
1431c349dbc7Sjsg 	target = eb_get_vma(eb, reloc->target_handle);
1432c349dbc7Sjsg 	if (unlikely(!target))
1433c349dbc7Sjsg 		return -ENOENT;
1434c349dbc7Sjsg 
1435c349dbc7Sjsg 	/* Validate that the target is in a valid r/w GPU domain */
1436c349dbc7Sjsg 	if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
1437c349dbc7Sjsg 		drm_dbg(&i915->drm, "reloc with multiple write domains: "
1438c349dbc7Sjsg 			  "target %d offset %d "
1439c349dbc7Sjsg 			  "read %08x write %08x",
1440c349dbc7Sjsg 			  reloc->target_handle,
1441c349dbc7Sjsg 			  (int) reloc->offset,
1442c349dbc7Sjsg 			  reloc->read_domains,
1443c349dbc7Sjsg 			  reloc->write_domain);
1444c349dbc7Sjsg 		return -EINVAL;
1445c349dbc7Sjsg 	}
1446c349dbc7Sjsg 	if (unlikely((reloc->write_domain | reloc->read_domains)
1447c349dbc7Sjsg 		     & ~I915_GEM_GPU_DOMAINS)) {
1448c349dbc7Sjsg 		drm_dbg(&i915->drm, "reloc with read/write non-GPU domains: "
1449c349dbc7Sjsg 			  "target %d offset %d "
1450c349dbc7Sjsg 			  "read %08x write %08x",
1451c349dbc7Sjsg 			  reloc->target_handle,
1452c349dbc7Sjsg 			  (int) reloc->offset,
1453c349dbc7Sjsg 			  reloc->read_domains,
1454c349dbc7Sjsg 			  reloc->write_domain);
1455c349dbc7Sjsg 		return -EINVAL;
1456c349dbc7Sjsg 	}
1457c349dbc7Sjsg 
1458c349dbc7Sjsg 	if (reloc->write_domain) {
1459c349dbc7Sjsg 		target->flags |= EXEC_OBJECT_WRITE;
1460c349dbc7Sjsg 
1461c349dbc7Sjsg 		/*
1462c349dbc7Sjsg 		 * Sandybridge PPGTT errata: We need a global gtt mapping
1463c349dbc7Sjsg 		 * for MI and pipe_control writes because the gpu doesn't
1464c349dbc7Sjsg 		 * properly redirect them through the ppgtt for non_secure
1465c349dbc7Sjsg 		 * batchbuffers.
1466c349dbc7Sjsg 		 */
1467c349dbc7Sjsg 		if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
1468543bcfd9Sjsg 		    GRAPHICS_VER(eb->i915) == 6 &&
1469543bcfd9Sjsg 		    !i915_vma_is_bound(target->vma, I915_VMA_GLOBAL_BIND)) {
1470543bcfd9Sjsg 			struct i915_vma *vma = target->vma;
1471543bcfd9Sjsg 
1472543bcfd9Sjsg 			reloc_cache_unmap(&eb->reloc_cache);
1473543bcfd9Sjsg 			mutex_lock(&vma->vm->mutex);
1474c349dbc7Sjsg 			err = i915_vma_bind(target->vma,
1475*f005ef32Sjsg 					    target->vma->obj->pat_index,
14761bb76ff1Sjsg 					    PIN_GLOBAL, NULL, NULL);
1477543bcfd9Sjsg 			mutex_unlock(&vma->vm->mutex);
1478543bcfd9Sjsg 			reloc_cache_remap(&eb->reloc_cache, ev->vma->obj);
1479ad8b1aafSjsg 			if (err)
1480c349dbc7Sjsg 				return err;
1481c349dbc7Sjsg 		}
1482c349dbc7Sjsg 	}
1483c349dbc7Sjsg 
1484c349dbc7Sjsg 	/*
1485c349dbc7Sjsg 	 * If the relocation already has the right value in it, no
1486c349dbc7Sjsg 	 * more work needs to be done.
1487c349dbc7Sjsg 	 */
1488c349dbc7Sjsg 	if (!DBG_FORCE_RELOC &&
1489*f005ef32Sjsg 	    gen8_canonical_addr(i915_vma_offset(target->vma)) == reloc->presumed_offset)
1490c349dbc7Sjsg 		return 0;
1491c349dbc7Sjsg 
1492c349dbc7Sjsg 	/* Check that the relocation address is valid... */
1493c349dbc7Sjsg 	if (unlikely(reloc->offset >
1494c349dbc7Sjsg 		     ev->vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
1495c349dbc7Sjsg 		drm_dbg(&i915->drm, "Relocation beyond object bounds: "
1496c349dbc7Sjsg 			  "target %d offset %d size %d.\n",
1497c349dbc7Sjsg 			  reloc->target_handle,
1498c349dbc7Sjsg 			  (int)reloc->offset,
1499c349dbc7Sjsg 			  (int)ev->vma->size);
1500c349dbc7Sjsg 		return -EINVAL;
1501c349dbc7Sjsg 	}
1502c349dbc7Sjsg 	if (unlikely(reloc->offset & 3)) {
1503c349dbc7Sjsg 		drm_dbg(&i915->drm, "Relocation not 4-byte aligned: "
1504c349dbc7Sjsg 			  "target %d offset %d.\n",
1505c349dbc7Sjsg 			  reloc->target_handle,
1506c349dbc7Sjsg 			  (int)reloc->offset);
1507c349dbc7Sjsg 		return -EINVAL;
1508c349dbc7Sjsg 	}
1509c349dbc7Sjsg 
1510c349dbc7Sjsg 	/*
1511c349dbc7Sjsg 	 * If we write into the object, we need to force the synchronisation
1512c349dbc7Sjsg 	 * barrier, either with an asynchronous clflush or if we executed the
1513c349dbc7Sjsg 	 * patching using the GPU (though that should be serialised by the
1514c349dbc7Sjsg 	 * timeline). To be completely sure, and since we are required to
1515c349dbc7Sjsg 	 * do relocations we are already stalling, disable the user's opt
1516c349dbc7Sjsg 	 * out of our synchronisation.
1517c349dbc7Sjsg 	 */
1518c349dbc7Sjsg 	ev->flags &= ~EXEC_OBJECT_ASYNC;
1519c349dbc7Sjsg 
1520c349dbc7Sjsg 	/* and update the user's relocation entry */
1521c349dbc7Sjsg 	return relocate_entry(ev->vma, reloc, eb, target->vma);
1522c349dbc7Sjsg }
1523c349dbc7Sjsg 
eb_relocate_vma(struct i915_execbuffer * eb,struct eb_vma * ev)1524c349dbc7Sjsg static int eb_relocate_vma(struct i915_execbuffer *eb, struct eb_vma *ev)
1525c349dbc7Sjsg {
1526c349dbc7Sjsg #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
1527c349dbc7Sjsg 	struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
1528c349dbc7Sjsg 	const struct drm_i915_gem_exec_object2 *entry = ev->exec;
1529ad8b1aafSjsg 	struct drm_i915_gem_relocation_entry __user *urelocs =
1530ad8b1aafSjsg 		u64_to_user_ptr(entry->relocs_ptr);
1531ad8b1aafSjsg 	unsigned long remain = entry->relocation_count;
1532c349dbc7Sjsg 
1533c349dbc7Sjsg 	if (unlikely(remain > N_RELOC(ULONG_MAX)))
1534c349dbc7Sjsg 		return -EINVAL;
1535c349dbc7Sjsg 
1536c349dbc7Sjsg 	/*
1537c349dbc7Sjsg 	 * We must check that the entire relocation array is safe
1538c349dbc7Sjsg 	 * to read. However, if the array is not writable the user loses
1539c349dbc7Sjsg 	 * the updated relocation values.
1540c349dbc7Sjsg 	 */
1541c349dbc7Sjsg 	if (unlikely(!access_ok(urelocs, remain * sizeof(*urelocs))))
1542c349dbc7Sjsg 		return -EFAULT;
1543c349dbc7Sjsg 
1544c349dbc7Sjsg 	do {
1545c349dbc7Sjsg 		struct drm_i915_gem_relocation_entry *r = stack;
1546c349dbc7Sjsg 		unsigned int count =
1547ad8b1aafSjsg 			min_t(unsigned long, remain, ARRAY_SIZE(stack));
1548c349dbc7Sjsg 		unsigned int copied;
1549c349dbc7Sjsg 
1550c349dbc7Sjsg 		/*
1551c349dbc7Sjsg 		 * This is the fast path and we cannot handle a pagefault
1552c349dbc7Sjsg 		 * whilst holding the struct mutex lest the user pass in the
1553c349dbc7Sjsg 		 * relocations contained within a mmaped bo. For in such a case
1554c349dbc7Sjsg 		 * we, the page fault handler would call i915_gem_fault() and
1555c349dbc7Sjsg 		 * we would try to acquire the struct mutex again. Obviously
1556c349dbc7Sjsg 		 * this is bad and so lockdep complains vehemently.
1557c349dbc7Sjsg 		 */
1558ad8b1aafSjsg 		pagefault_disable();
1559ad8b1aafSjsg 		copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
1560ad8b1aafSjsg 		pagefault_enable();
1561c349dbc7Sjsg 		if (unlikely(copied)) {
1562c349dbc7Sjsg 			remain = -EFAULT;
1563c349dbc7Sjsg 			goto out;
1564c349dbc7Sjsg 		}
1565c349dbc7Sjsg 
1566c349dbc7Sjsg 		remain -= count;
1567c349dbc7Sjsg 		do {
1568c349dbc7Sjsg 			u64 offset = eb_relocate_entry(eb, ev, r);
1569c349dbc7Sjsg 
1570c349dbc7Sjsg 			if (likely(offset == 0)) {
1571c349dbc7Sjsg 			} else if ((s64)offset < 0) {
1572c349dbc7Sjsg 				remain = (int)offset;
1573c349dbc7Sjsg 				goto out;
1574c349dbc7Sjsg 			} else {
1575c349dbc7Sjsg 				/*
1576c349dbc7Sjsg 				 * Note that reporting an error now
1577c349dbc7Sjsg 				 * leaves everything in an inconsistent
1578c349dbc7Sjsg 				 * state as we have *already* changed
1579c349dbc7Sjsg 				 * the relocation value inside the
1580c349dbc7Sjsg 				 * object. As we have not changed the
1581c349dbc7Sjsg 				 * reloc.presumed_offset or will not
1582c349dbc7Sjsg 				 * change the execobject.offset, on the
1583c349dbc7Sjsg 				 * call we may not rewrite the value
1584c349dbc7Sjsg 				 * inside the object, leaving it
1585c349dbc7Sjsg 				 * dangling and causing a GPU hang. Unless
1586c349dbc7Sjsg 				 * userspace dynamically rebuilds the
1587c349dbc7Sjsg 				 * relocations on each execbuf rather than
1588c349dbc7Sjsg 				 * presume a static tree.
1589c349dbc7Sjsg 				 *
1590c349dbc7Sjsg 				 * We did previously check if the relocations
1591c349dbc7Sjsg 				 * were writable (access_ok), an error now
1592c349dbc7Sjsg 				 * would be a strange race with mprotect,
1593c349dbc7Sjsg 				 * having already demonstrated that we
1594c349dbc7Sjsg 				 * can read from this userspace address.
1595c349dbc7Sjsg 				 */
1596c349dbc7Sjsg 				offset = gen8_canonical_addr(offset & ~UPDATE);
1597c349dbc7Sjsg 				__put_user(offset,
1598c349dbc7Sjsg 					   &urelocs[r - stack].presumed_offset);
1599c349dbc7Sjsg 			}
1600c349dbc7Sjsg 		} while (r++, --count);
1601c349dbc7Sjsg 		urelocs += ARRAY_SIZE(stack);
1602c349dbc7Sjsg 	} while (remain);
1603c349dbc7Sjsg out:
1604ad8b1aafSjsg 	reloc_cache_reset(&eb->reloc_cache, eb);
1605c349dbc7Sjsg 	return remain;
1606c349dbc7Sjsg }
1607c349dbc7Sjsg 
1608ad8b1aafSjsg static int
eb_relocate_vma_slow(struct i915_execbuffer * eb,struct eb_vma * ev)1609ad8b1aafSjsg eb_relocate_vma_slow(struct i915_execbuffer *eb, struct eb_vma *ev)
1610c349dbc7Sjsg {
1611ad8b1aafSjsg 	const struct drm_i915_gem_exec_object2 *entry = ev->exec;
1612ad8b1aafSjsg 	struct drm_i915_gem_relocation_entry *relocs =
1613ad8b1aafSjsg 		u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1614ad8b1aafSjsg 	unsigned int i;
1615c349dbc7Sjsg 	int err;
1616c349dbc7Sjsg 
1617ad8b1aafSjsg 	for (i = 0; i < entry->relocation_count; i++) {
1618ad8b1aafSjsg 		u64 offset = eb_relocate_entry(eb, ev, &relocs[i]);
1619c349dbc7Sjsg 
1620ad8b1aafSjsg 		if ((s64)offset < 0) {
1621ad8b1aafSjsg 			err = (int)offset;
1622ad8b1aafSjsg 			goto err;
1623ad8b1aafSjsg 		}
1624ad8b1aafSjsg 	}
1625ad8b1aafSjsg 	err = 0;
1626ad8b1aafSjsg err:
1627ad8b1aafSjsg 	reloc_cache_reset(&eb->reloc_cache, eb);
1628ad8b1aafSjsg 	return err;
1629ad8b1aafSjsg }
1630ad8b1aafSjsg 
check_relocations(const struct drm_i915_gem_exec_object2 * entry)1631ad8b1aafSjsg static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
1632ad8b1aafSjsg {
1633ad8b1aafSjsg 	const char __user *addr, *end;
1634ad8b1aafSjsg 	unsigned long size;
1635ad8b1aafSjsg 	char __maybe_unused c;
1636ad8b1aafSjsg 
1637ad8b1aafSjsg 	size = entry->relocation_count;
1638ad8b1aafSjsg 	if (size == 0)
1639ad8b1aafSjsg 		return 0;
1640ad8b1aafSjsg 
1641ad8b1aafSjsg 	if (size > N_RELOC(ULONG_MAX))
1642ad8b1aafSjsg 		return -EINVAL;
1643ad8b1aafSjsg 
1644ad8b1aafSjsg 	addr = u64_to_user_ptr(entry->relocs_ptr);
1645ad8b1aafSjsg 	size *= sizeof(struct drm_i915_gem_relocation_entry);
1646ad8b1aafSjsg 	if (!access_ok(addr, size))
1647ad8b1aafSjsg 		return -EFAULT;
1648ad8b1aafSjsg 
1649ad8b1aafSjsg 	end = addr + size;
1650ad8b1aafSjsg 	for (; addr < end; addr += PAGE_SIZE) {
1651ad8b1aafSjsg 		int err = __get_user(c, addr);
1652c349dbc7Sjsg 		if (err)
1653c349dbc7Sjsg 			return err;
1654c349dbc7Sjsg 	}
1655ad8b1aafSjsg 	return __get_user(c, end - 1);
1656ad8b1aafSjsg }
1657ad8b1aafSjsg 
eb_copy_relocations(const struct i915_execbuffer * eb)1658ad8b1aafSjsg static int eb_copy_relocations(const struct i915_execbuffer *eb)
1659ad8b1aafSjsg {
1660ad8b1aafSjsg 	struct drm_i915_gem_relocation_entry *relocs;
1661ad8b1aafSjsg 	const unsigned int count = eb->buffer_count;
1662ad8b1aafSjsg 	unsigned int i;
1663ad8b1aafSjsg 	int err;
1664ad8b1aafSjsg 
1665ad8b1aafSjsg 	for (i = 0; i < count; i++) {
1666ad8b1aafSjsg 		const unsigned int nreloc = eb->exec[i].relocation_count;
1667ad8b1aafSjsg 		struct drm_i915_gem_relocation_entry __user *urelocs;
1668ad8b1aafSjsg 		unsigned long size;
1669ad8b1aafSjsg 		unsigned long copied;
1670ad8b1aafSjsg 
1671ad8b1aafSjsg 		if (nreloc == 0)
1672ad8b1aafSjsg 			continue;
1673ad8b1aafSjsg 
1674ad8b1aafSjsg 		err = check_relocations(&eb->exec[i]);
1675ad8b1aafSjsg 		if (err)
1676ad8b1aafSjsg 			goto err;
1677ad8b1aafSjsg 
1678ad8b1aafSjsg 		urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
1679ad8b1aafSjsg 		size = nreloc * sizeof(*relocs);
1680ad8b1aafSjsg 
1681ad8b1aafSjsg 		relocs = kvmalloc_array(size, 1, GFP_KERNEL);
1682ad8b1aafSjsg 		if (!relocs) {
1683ad8b1aafSjsg 			err = -ENOMEM;
1684ad8b1aafSjsg 			goto err;
1685ad8b1aafSjsg 		}
1686ad8b1aafSjsg 
1687ad8b1aafSjsg 		/* copy_from_user is limited to < 4GiB */
1688ad8b1aafSjsg 		copied = 0;
1689ad8b1aafSjsg 		do {
1690ad8b1aafSjsg 			unsigned int len =
1691ad8b1aafSjsg 				min_t(u64, BIT_ULL(31), size - copied);
1692ad8b1aafSjsg 
1693ad8b1aafSjsg 			if (__copy_from_user((char *)relocs + copied,
1694ad8b1aafSjsg 					     (char __user *)urelocs + copied,
1695ad8b1aafSjsg 					     len))
1696ad8b1aafSjsg 				goto end;
1697ad8b1aafSjsg 
1698ad8b1aafSjsg 			copied += len;
1699ad8b1aafSjsg 		} while (copied < size);
1700ad8b1aafSjsg 
1701ad8b1aafSjsg 		/*
1702ad8b1aafSjsg 		 * As we do not update the known relocation offsets after
1703ad8b1aafSjsg 		 * relocating (due to the complexities in lock handling),
1704ad8b1aafSjsg 		 * we need to mark them as invalid now so that we force the
1705ad8b1aafSjsg 		 * relocation processing next time. Just in case the target
1706ad8b1aafSjsg 		 * object is evicted and then rebound into its old
1707ad8b1aafSjsg 		 * presumed_offset before the next execbuffer - if that
1708ad8b1aafSjsg 		 * happened we would make the mistake of assuming that the
1709ad8b1aafSjsg 		 * relocations were valid.
1710ad8b1aafSjsg 		 */
1711ad8b1aafSjsg 		if (!user_access_begin(urelocs, size))
1712ad8b1aafSjsg 			goto end;
1713ad8b1aafSjsg 
1714ad8b1aafSjsg 		for (copied = 0; copied < nreloc; copied++)
1715ad8b1aafSjsg 			unsafe_put_user(-1,
1716ad8b1aafSjsg 					&urelocs[copied].presumed_offset,
1717ad8b1aafSjsg 					end_user);
1718ad8b1aafSjsg 		user_access_end();
1719ad8b1aafSjsg 
1720ad8b1aafSjsg 		eb->exec[i].relocs_ptr = (uintptr_t)relocs;
1721ad8b1aafSjsg 	}
1722ad8b1aafSjsg 
1723ad8b1aafSjsg 	return 0;
1724ad8b1aafSjsg 
1725ad8b1aafSjsg end_user:
1726ad8b1aafSjsg 	user_access_end();
1727ad8b1aafSjsg end:
1728ad8b1aafSjsg 	kvfree(relocs);
1729ad8b1aafSjsg 	err = -EFAULT;
1730ad8b1aafSjsg err:
1731ad8b1aafSjsg 	while (i--) {
1732ad8b1aafSjsg 		relocs = u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr);
1733ad8b1aafSjsg 		if (eb->exec[i].relocation_count)
1734ad8b1aafSjsg 			kvfree(relocs);
1735ad8b1aafSjsg 	}
1736ad8b1aafSjsg 	return err;
1737ad8b1aafSjsg }
1738ad8b1aafSjsg 
eb_prefault_relocations(const struct i915_execbuffer * eb)1739ad8b1aafSjsg static int eb_prefault_relocations(const struct i915_execbuffer *eb)
1740ad8b1aafSjsg {
1741ad8b1aafSjsg 	const unsigned int count = eb->buffer_count;
1742ad8b1aafSjsg 	unsigned int i;
1743ad8b1aafSjsg 
1744ad8b1aafSjsg 	for (i = 0; i < count; i++) {
1745ad8b1aafSjsg 		int err;
1746ad8b1aafSjsg 
1747ad8b1aafSjsg 		err = check_relocations(&eb->exec[i]);
1748ad8b1aafSjsg 		if (err)
1749ad8b1aafSjsg 			return err;
1750ad8b1aafSjsg 	}
1751ad8b1aafSjsg 
1752ad8b1aafSjsg 	return 0;
1753ad8b1aafSjsg }
1754ad8b1aafSjsg 
eb_reinit_userptr(struct i915_execbuffer * eb)17555ca02815Sjsg static int eb_reinit_userptr(struct i915_execbuffer *eb)
17565ca02815Sjsg {
17575ca02815Sjsg 	const unsigned int count = eb->buffer_count;
17585ca02815Sjsg 	unsigned int i;
17595ca02815Sjsg 	int ret;
17605ca02815Sjsg 
17615ca02815Sjsg 	if (likely(!(eb->args->flags & __EXEC_USERPTR_USED)))
17625ca02815Sjsg 		return 0;
17635ca02815Sjsg 
17645ca02815Sjsg 	for (i = 0; i < count; i++) {
17655ca02815Sjsg 		struct eb_vma *ev = &eb->vma[i];
17665ca02815Sjsg 
17675ca02815Sjsg 		if (!i915_gem_object_is_userptr(ev->vma->obj))
17685ca02815Sjsg 			continue;
17695ca02815Sjsg 
17705ca02815Sjsg 		ret = i915_gem_object_userptr_submit_init(ev->vma->obj);
17715ca02815Sjsg 		if (ret)
17725ca02815Sjsg 			return ret;
17735ca02815Sjsg 
17745ca02815Sjsg 		ev->flags |= __EXEC_OBJECT_USERPTR_INIT;
17755ca02815Sjsg 	}
17765ca02815Sjsg 
17775ca02815Sjsg 	return 0;
17785ca02815Sjsg }
17795ca02815Sjsg 
eb_relocate_parse_slow(struct i915_execbuffer * eb)17801bb76ff1Sjsg static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb)
1781ad8b1aafSjsg {
1782ad8b1aafSjsg 	bool have_copy = false;
1783ad8b1aafSjsg 	struct eb_vma *ev;
1784ad8b1aafSjsg 	int err = 0;
1785ad8b1aafSjsg 
1786ad8b1aafSjsg repeat:
1787ad8b1aafSjsg 	if (signal_pending(current)) {
1788ad8b1aafSjsg 		err = -ERESTARTSYS;
1789ad8b1aafSjsg 		goto out;
1790ad8b1aafSjsg 	}
1791ad8b1aafSjsg 
1792ad8b1aafSjsg 	/* We may process another execbuffer during the unlock... */
1793ad8b1aafSjsg 	eb_release_vmas(eb, false);
1794ad8b1aafSjsg 	i915_gem_ww_ctx_fini(&eb->ww);
1795ad8b1aafSjsg 
1796ad8b1aafSjsg 	/*
1797ad8b1aafSjsg 	 * We take 3 passes through the slowpatch.
1798ad8b1aafSjsg 	 *
1799ad8b1aafSjsg 	 * 1 - we try to just prefault all the user relocation entries and
1800ad8b1aafSjsg 	 * then attempt to reuse the atomic pagefault disabled fast path again.
1801ad8b1aafSjsg 	 *
1802ad8b1aafSjsg 	 * 2 - we copy the user entries to a local buffer here outside of the
1803ad8b1aafSjsg 	 * local and allow ourselves to wait upon any rendering before
1804ad8b1aafSjsg 	 * relocations
1805ad8b1aafSjsg 	 *
1806ad8b1aafSjsg 	 * 3 - we already have a local copy of the relocation entries, but
1807ad8b1aafSjsg 	 * were interrupted (EAGAIN) whilst waiting for the objects, try again.
1808ad8b1aafSjsg 	 */
1809ad8b1aafSjsg 	if (!err) {
1810ad8b1aafSjsg 		err = eb_prefault_relocations(eb);
1811ad8b1aafSjsg 	} else if (!have_copy) {
1812ad8b1aafSjsg 		err = eb_copy_relocations(eb);
1813ad8b1aafSjsg 		have_copy = err == 0;
1814ad8b1aafSjsg 	} else {
1815ad8b1aafSjsg 		cond_resched();
1816ad8b1aafSjsg 		err = 0;
1817ad8b1aafSjsg 	}
1818ad8b1aafSjsg 
1819ad8b1aafSjsg 	if (!err)
18205ca02815Sjsg 		err = eb_reinit_userptr(eb);
1821ad8b1aafSjsg 
1822ad8b1aafSjsg 	i915_gem_ww_ctx_init(&eb->ww, true);
1823ad8b1aafSjsg 	if (err)
1824ad8b1aafSjsg 		goto out;
1825ad8b1aafSjsg 
1826ad8b1aafSjsg 	/* reacquire the objects */
1827ad8b1aafSjsg repeat_validate:
18281bb76ff1Sjsg 	err = eb_pin_engine(eb, false);
18291bb76ff1Sjsg 	if (err)
1830ad8b1aafSjsg 		goto err;
1831ad8b1aafSjsg 
1832ad8b1aafSjsg 	err = eb_validate_vmas(eb);
1833ad8b1aafSjsg 	if (err)
1834ad8b1aafSjsg 		goto err;
1835ad8b1aafSjsg 
18361bb76ff1Sjsg 	GEM_BUG_ON(!eb->batches[0]);
1837ad8b1aafSjsg 
1838ad8b1aafSjsg 	list_for_each_entry(ev, &eb->relocs, reloc_link) {
1839ad8b1aafSjsg 		if (!have_copy) {
1840ad8b1aafSjsg 			err = eb_relocate_vma(eb, ev);
1841ad8b1aafSjsg 			if (err)
1842ad8b1aafSjsg 				break;
1843ad8b1aafSjsg 		} else {
1844ad8b1aafSjsg 			err = eb_relocate_vma_slow(eb, ev);
1845ad8b1aafSjsg 			if (err)
1846ad8b1aafSjsg 				break;
1847ad8b1aafSjsg 		}
1848ad8b1aafSjsg 	}
1849ad8b1aafSjsg 
1850ad8b1aafSjsg 	if (err == -EDEADLK)
1851ad8b1aafSjsg 		goto err;
1852ad8b1aafSjsg 
1853ad8b1aafSjsg 	if (err && !have_copy)
1854ad8b1aafSjsg 		goto repeat;
1855ad8b1aafSjsg 
1856ad8b1aafSjsg 	if (err)
1857ad8b1aafSjsg 		goto err;
1858ad8b1aafSjsg 
1859ad8b1aafSjsg 	/* as last step, parse the command buffer */
1860ad8b1aafSjsg 	err = eb_parse(eb);
1861ad8b1aafSjsg 	if (err)
1862ad8b1aafSjsg 		goto err;
1863ad8b1aafSjsg 
1864ad8b1aafSjsg 	/*
1865ad8b1aafSjsg 	 * Leave the user relocations as are, this is the painfully slow path,
1866ad8b1aafSjsg 	 * and we want to avoid the complication of dropping the lock whilst
1867ad8b1aafSjsg 	 * having buffers reserved in the aperture and so causing spurious
1868ad8b1aafSjsg 	 * ENOSPC for random operations.
1869ad8b1aafSjsg 	 */
1870ad8b1aafSjsg 
1871ad8b1aafSjsg err:
1872ad8b1aafSjsg 	if (err == -EDEADLK) {
1873ad8b1aafSjsg 		eb_release_vmas(eb, false);
1874ad8b1aafSjsg 		err = i915_gem_ww_ctx_backoff(&eb->ww);
1875ad8b1aafSjsg 		if (!err)
1876ad8b1aafSjsg 			goto repeat_validate;
1877ad8b1aafSjsg 	}
1878ad8b1aafSjsg 
1879ad8b1aafSjsg 	if (err == -EAGAIN)
1880ad8b1aafSjsg 		goto repeat;
1881ad8b1aafSjsg 
1882ad8b1aafSjsg out:
1883ad8b1aafSjsg 	if (have_copy) {
1884ad8b1aafSjsg 		const unsigned int count = eb->buffer_count;
1885ad8b1aafSjsg 		unsigned int i;
1886ad8b1aafSjsg 
1887ad8b1aafSjsg 		for (i = 0; i < count; i++) {
1888ad8b1aafSjsg 			const struct drm_i915_gem_exec_object2 *entry =
1889ad8b1aafSjsg 				&eb->exec[i];
1890ad8b1aafSjsg 			struct drm_i915_gem_relocation_entry *relocs;
1891ad8b1aafSjsg 
1892ad8b1aafSjsg 			if (!entry->relocation_count)
1893ad8b1aafSjsg 				continue;
1894ad8b1aafSjsg 
1895ad8b1aafSjsg 			relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1896ad8b1aafSjsg 			kvfree(relocs);
1897ad8b1aafSjsg 		}
1898ad8b1aafSjsg 	}
1899ad8b1aafSjsg 
1900ad8b1aafSjsg 	return err;
1901ad8b1aafSjsg }
1902ad8b1aafSjsg 
eb_relocate_parse(struct i915_execbuffer * eb)1903ad8b1aafSjsg static int eb_relocate_parse(struct i915_execbuffer *eb)
1904ad8b1aafSjsg {
1905ad8b1aafSjsg 	int err;
1906ad8b1aafSjsg 	bool throttle = true;
1907ad8b1aafSjsg 
1908ad8b1aafSjsg retry:
19091bb76ff1Sjsg 	err = eb_pin_engine(eb, throttle);
19101bb76ff1Sjsg 	if (err) {
1911ad8b1aafSjsg 		if (err != -EDEADLK)
1912ad8b1aafSjsg 			return err;
1913ad8b1aafSjsg 
1914ad8b1aafSjsg 		goto err;
1915ad8b1aafSjsg 	}
1916ad8b1aafSjsg 
1917ad8b1aafSjsg 	/* only throttle once, even if we didn't need to throttle */
1918ad8b1aafSjsg 	throttle = false;
1919ad8b1aafSjsg 
1920ad8b1aafSjsg 	err = eb_validate_vmas(eb);
1921ad8b1aafSjsg 	if (err == -EAGAIN)
1922ad8b1aafSjsg 		goto slow;
1923ad8b1aafSjsg 	else if (err)
1924ad8b1aafSjsg 		goto err;
1925c349dbc7Sjsg 
1926c349dbc7Sjsg 	/* The objects are in their final locations, apply the relocations. */
1927c349dbc7Sjsg 	if (eb->args->flags & __EXEC_HAS_RELOC) {
1928c349dbc7Sjsg 		struct eb_vma *ev;
1929c349dbc7Sjsg 
1930c349dbc7Sjsg 		list_for_each_entry(ev, &eb->relocs, reloc_link) {
1931c349dbc7Sjsg 			err = eb_relocate_vma(eb, ev);
1932c349dbc7Sjsg 			if (err)
1933ad8b1aafSjsg 				break;
1934c349dbc7Sjsg 		}
1935c349dbc7Sjsg 
1936ad8b1aafSjsg 		if (err == -EDEADLK)
1937ad8b1aafSjsg 			goto err;
1938ad8b1aafSjsg 		else if (err)
1939ad8b1aafSjsg 			goto slow;
1940ad8b1aafSjsg 	}
1941ad8b1aafSjsg 
1942ad8b1aafSjsg 	if (!err)
1943ad8b1aafSjsg 		err = eb_parse(eb);
1944ad8b1aafSjsg 
1945ad8b1aafSjsg err:
1946ad8b1aafSjsg 	if (err == -EDEADLK) {
1947ad8b1aafSjsg 		eb_release_vmas(eb, false);
1948ad8b1aafSjsg 		err = i915_gem_ww_ctx_backoff(&eb->ww);
1949ad8b1aafSjsg 		if (!err)
1950ad8b1aafSjsg 			goto retry;
1951ad8b1aafSjsg 	}
1952ad8b1aafSjsg 
1953ad8b1aafSjsg 	return err;
1954ad8b1aafSjsg 
1955ad8b1aafSjsg slow:
19561bb76ff1Sjsg 	err = eb_relocate_parse_slow(eb);
1957ad8b1aafSjsg 	if (err)
1958ad8b1aafSjsg 		/*
1959ad8b1aafSjsg 		 * If the user expects the execobject.offset and
1960ad8b1aafSjsg 		 * reloc.presumed_offset to be an exact match,
1961ad8b1aafSjsg 		 * as for using NO_RELOC, then we cannot update
1962ad8b1aafSjsg 		 * the execobject.offset until we have completed
1963ad8b1aafSjsg 		 * relocation.
1964ad8b1aafSjsg 		 */
1965ad8b1aafSjsg 		eb->args->flags &= ~__EXEC_HAS_RELOC;
1966ad8b1aafSjsg 
1967ad8b1aafSjsg 	return err;
1968c349dbc7Sjsg }
1969c349dbc7Sjsg 
19701bb76ff1Sjsg /*
19711bb76ff1Sjsg  * Using two helper loops for the order of which requests / batches are created
19721bb76ff1Sjsg  * and added the to backend. Requests are created in order from the parent to
19731bb76ff1Sjsg  * the last child. Requests are added in the reverse order, from the last child
19741bb76ff1Sjsg  * to parent. This is done for locking reasons as the timeline lock is acquired
19751bb76ff1Sjsg  * during request creation and released when the request is added to the
19761bb76ff1Sjsg  * backend. To make lockdep happy (see intel_context_timeline_lock) this must be
19771bb76ff1Sjsg  * the ordering.
19781bb76ff1Sjsg  */
19791bb76ff1Sjsg #define for_each_batch_create_order(_eb, _i) \
19801bb76ff1Sjsg 	for ((_i) = 0; (_i) < (_eb)->num_batches; ++(_i))
19811bb76ff1Sjsg #define for_each_batch_add_order(_eb, _i) \
19821bb76ff1Sjsg 	BUILD_BUG_ON(!typecheck(int, _i)); \
19831bb76ff1Sjsg 	for ((_i) = (_eb)->num_batches - 1; (_i) >= 0; --(_i))
19841bb76ff1Sjsg 
19851bb76ff1Sjsg static struct i915_request *
eb_find_first_request_added(struct i915_execbuffer * eb)19861bb76ff1Sjsg eb_find_first_request_added(struct i915_execbuffer *eb)
19871bb76ff1Sjsg {
19881bb76ff1Sjsg 	int i;
19891bb76ff1Sjsg 
19901bb76ff1Sjsg 	for_each_batch_add_order(eb, i)
19911bb76ff1Sjsg 		if (eb->requests[i])
19921bb76ff1Sjsg 			return eb->requests[i];
19931bb76ff1Sjsg 
19941bb76ff1Sjsg 	GEM_BUG_ON("Request not found");
19951bb76ff1Sjsg 
19961bb76ff1Sjsg 	return NULL;
19971bb76ff1Sjsg }
19981bb76ff1Sjsg 
19991bb76ff1Sjsg #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
20001bb76ff1Sjsg 
20011bb76ff1Sjsg /* Stage with GFP_KERNEL allocations before we enter the signaling critical path */
eb_capture_stage(struct i915_execbuffer * eb)20021bb76ff1Sjsg static int eb_capture_stage(struct i915_execbuffer *eb)
20031bb76ff1Sjsg {
20041bb76ff1Sjsg 	const unsigned int count = eb->buffer_count;
20051bb76ff1Sjsg 	unsigned int i = count, j;
20061bb76ff1Sjsg 
20071bb76ff1Sjsg 	while (i--) {
20081bb76ff1Sjsg 		struct eb_vma *ev = &eb->vma[i];
20091bb76ff1Sjsg 		struct i915_vma *vma = ev->vma;
20101bb76ff1Sjsg 		unsigned int flags = ev->flags;
20111bb76ff1Sjsg 
20121bb76ff1Sjsg 		if (!(flags & EXEC_OBJECT_CAPTURE))
20131bb76ff1Sjsg 			continue;
20141bb76ff1Sjsg 
20151bb76ff1Sjsg 		if (i915_gem_context_is_recoverable(eb->gem_context) &&
20161bb76ff1Sjsg 		    (IS_DGFX(eb->i915) || GRAPHICS_VER_FULL(eb->i915) > IP_VER(12, 0)))
20171bb76ff1Sjsg 			return -EINVAL;
20181bb76ff1Sjsg 
20191bb76ff1Sjsg 		for_each_batch_create_order(eb, j) {
20201bb76ff1Sjsg 			struct i915_capture_list *capture;
20211bb76ff1Sjsg 
20221bb76ff1Sjsg 			capture = kmalloc(sizeof(*capture), GFP_KERNEL);
20231bb76ff1Sjsg 			if (!capture)
20241bb76ff1Sjsg 				continue;
20251bb76ff1Sjsg 
20261bb76ff1Sjsg 			capture->next = eb->capture_lists[j];
20271bb76ff1Sjsg 			capture->vma_res = i915_vma_resource_get(vma->resource);
20281bb76ff1Sjsg 			eb->capture_lists[j] = capture;
20291bb76ff1Sjsg 		}
20301bb76ff1Sjsg 	}
20311bb76ff1Sjsg 
20321bb76ff1Sjsg 	return 0;
20331bb76ff1Sjsg }
20341bb76ff1Sjsg 
20351bb76ff1Sjsg /* Commit once we're in the critical path */
eb_capture_commit(struct i915_execbuffer * eb)20361bb76ff1Sjsg static void eb_capture_commit(struct i915_execbuffer *eb)
20371bb76ff1Sjsg {
20381bb76ff1Sjsg 	unsigned int j;
20391bb76ff1Sjsg 
20401bb76ff1Sjsg 	for_each_batch_create_order(eb, j) {
20411bb76ff1Sjsg 		struct i915_request *rq = eb->requests[j];
20421bb76ff1Sjsg 
20431bb76ff1Sjsg 		if (!rq)
20441bb76ff1Sjsg 			break;
20451bb76ff1Sjsg 
20461bb76ff1Sjsg 		rq->capture_list = eb->capture_lists[j];
20471bb76ff1Sjsg 		eb->capture_lists[j] = NULL;
20481bb76ff1Sjsg 	}
20491bb76ff1Sjsg }
20501bb76ff1Sjsg 
20511bb76ff1Sjsg /*
20521bb76ff1Sjsg  * Release anything that didn't get committed due to errors.
20531bb76ff1Sjsg  * The capture_list will otherwise be freed at request retire.
20541bb76ff1Sjsg  */
eb_capture_release(struct i915_execbuffer * eb)20551bb76ff1Sjsg static void eb_capture_release(struct i915_execbuffer *eb)
20561bb76ff1Sjsg {
20571bb76ff1Sjsg 	unsigned int j;
20581bb76ff1Sjsg 
20591bb76ff1Sjsg 	for_each_batch_create_order(eb, j) {
20601bb76ff1Sjsg 		if (eb->capture_lists[j]) {
20611bb76ff1Sjsg 			i915_request_free_capture_list(eb->capture_lists[j]);
20621bb76ff1Sjsg 			eb->capture_lists[j] = NULL;
20631bb76ff1Sjsg 		}
20641bb76ff1Sjsg 	}
20651bb76ff1Sjsg }
20661bb76ff1Sjsg 
eb_capture_list_clear(struct i915_execbuffer * eb)20671bb76ff1Sjsg static void eb_capture_list_clear(struct i915_execbuffer *eb)
20681bb76ff1Sjsg {
20691bb76ff1Sjsg 	memset(eb->capture_lists, 0, sizeof(eb->capture_lists));
20701bb76ff1Sjsg }
20711bb76ff1Sjsg 
20721bb76ff1Sjsg #else
20731bb76ff1Sjsg 
eb_capture_stage(struct i915_execbuffer * eb)20741bb76ff1Sjsg static int eb_capture_stage(struct i915_execbuffer *eb)
20751bb76ff1Sjsg {
20761bb76ff1Sjsg 	return 0;
20771bb76ff1Sjsg }
20781bb76ff1Sjsg 
eb_capture_commit(struct i915_execbuffer * eb)20791bb76ff1Sjsg static void eb_capture_commit(struct i915_execbuffer *eb)
20801bb76ff1Sjsg {
20811bb76ff1Sjsg }
20821bb76ff1Sjsg 
eb_capture_release(struct i915_execbuffer * eb)20831bb76ff1Sjsg static void eb_capture_release(struct i915_execbuffer *eb)
20841bb76ff1Sjsg {
20851bb76ff1Sjsg }
20861bb76ff1Sjsg 
eb_capture_list_clear(struct i915_execbuffer * eb)20871bb76ff1Sjsg static void eb_capture_list_clear(struct i915_execbuffer *eb)
20881bb76ff1Sjsg {
20891bb76ff1Sjsg }
20901bb76ff1Sjsg 
20911bb76ff1Sjsg #endif
20921bb76ff1Sjsg 
eb_move_to_gpu(struct i915_execbuffer * eb)2093c349dbc7Sjsg static int eb_move_to_gpu(struct i915_execbuffer *eb)
2094c349dbc7Sjsg {
2095c349dbc7Sjsg 	const unsigned int count = eb->buffer_count;
2096ad8b1aafSjsg 	unsigned int i = count;
20971bb76ff1Sjsg 	int err = 0, j;
2098c349dbc7Sjsg 
2099c349dbc7Sjsg 	while (i--) {
2100c349dbc7Sjsg 		struct eb_vma *ev = &eb->vma[i];
2101c349dbc7Sjsg 		struct i915_vma *vma = ev->vma;
2102c349dbc7Sjsg 		unsigned int flags = ev->flags;
2103c349dbc7Sjsg 		struct drm_i915_gem_object *obj = vma->obj;
2104c349dbc7Sjsg 
2105c349dbc7Sjsg 		assert_vma_held(vma);
2106c349dbc7Sjsg 
2107c349dbc7Sjsg 		/*
2108c349dbc7Sjsg 		 * If the GPU is not _reading_ through the CPU cache, we need
2109c349dbc7Sjsg 		 * to make sure that any writes (both previous GPU writes from
2110c349dbc7Sjsg 		 * before a change in snooping levels and normal CPU writes)
2111c349dbc7Sjsg 		 * caught in that cache are flushed to main memory.
2112c349dbc7Sjsg 		 *
2113c349dbc7Sjsg 		 * We want to say
2114c349dbc7Sjsg 		 *   obj->cache_dirty &&
2115c349dbc7Sjsg 		 *   !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
2116c349dbc7Sjsg 		 * but gcc's optimiser doesn't handle that as well and emits
2117c349dbc7Sjsg 		 * two jumps instead of one. Maybe one day...
21181bb76ff1Sjsg 		 *
21191bb76ff1Sjsg 		 * FIXME: There is also sync flushing in set_pages(), which
21201bb76ff1Sjsg 		 * serves a different purpose(some of the time at least).
21211bb76ff1Sjsg 		 *
21221bb76ff1Sjsg 		 * We should consider:
21231bb76ff1Sjsg 		 *
21241bb76ff1Sjsg 		 *   1. Rip out the async flush code.
21251bb76ff1Sjsg 		 *
21261bb76ff1Sjsg 		 *   2. Or make the sync flushing use the async clflush path
21271bb76ff1Sjsg 		 *   using mandatory fences underneath. Currently the below
21281bb76ff1Sjsg 		 *   async flush happens after we bind the object.
2129c349dbc7Sjsg 		 */
2130c349dbc7Sjsg 		if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) {
2131c349dbc7Sjsg 			if (i915_gem_clflush_object(obj, 0))
2132c349dbc7Sjsg 				flags &= ~EXEC_OBJECT_ASYNC;
2133c349dbc7Sjsg 		}
2134c349dbc7Sjsg 
21351bb76ff1Sjsg 		/* We only need to await on the first request */
2136c349dbc7Sjsg 		if (err == 0 && !(flags & EXEC_OBJECT_ASYNC)) {
2137c349dbc7Sjsg 			err = i915_request_await_object
21381bb76ff1Sjsg 				(eb_find_first_request_added(eb), obj,
21391bb76ff1Sjsg 				 flags & EXEC_OBJECT_WRITE);
2140c349dbc7Sjsg 		}
2141c349dbc7Sjsg 
21421bb76ff1Sjsg 		for_each_batch_add_order(eb, j) {
21431bb76ff1Sjsg 			if (err)
21441bb76ff1Sjsg 				break;
21451bb76ff1Sjsg 			if (!eb->requests[j])
21461bb76ff1Sjsg 				continue;
21471bb76ff1Sjsg 
21481bb76ff1Sjsg 			err = _i915_vma_move_to_active(vma, eb->requests[j],
21491bb76ff1Sjsg 						       j ? NULL :
21501bb76ff1Sjsg 						       eb->composite_fence ?
21511bb76ff1Sjsg 						       eb->composite_fence :
21521bb76ff1Sjsg 						       &eb->requests[j]->fence,
2153*f005ef32Sjsg 						       flags | __EXEC_OBJECT_NO_RESERVE |
2154*f005ef32Sjsg 						       __EXEC_OBJECT_NO_REQUEST_AWAIT);
2155c349dbc7Sjsg 		}
21561bb76ff1Sjsg 	}
2157c349dbc7Sjsg 
21585ca02815Sjsg #ifdef CONFIG_MMU_NOTIFIER
21595ca02815Sjsg 	if (!err && (eb->args->flags & __EXEC_USERPTR_USED)) {
21605ca02815Sjsg 		read_lock(&eb->i915->mm.notifier_lock);
21615ca02815Sjsg 
21625ca02815Sjsg 		/*
21635ca02815Sjsg 		 * count is always at least 1, otherwise __EXEC_USERPTR_USED
21645ca02815Sjsg 		 * could not have been set
21655ca02815Sjsg 		 */
21665ca02815Sjsg 		for (i = 0; i < count; i++) {
21675ca02815Sjsg 			struct eb_vma *ev = &eb->vma[i];
21685ca02815Sjsg 			struct drm_i915_gem_object *obj = ev->vma->obj;
21695ca02815Sjsg 
21705ca02815Sjsg 			if (!i915_gem_object_is_userptr(obj))
21715ca02815Sjsg 				continue;
21725ca02815Sjsg 
21735ca02815Sjsg 			err = i915_gem_object_userptr_submit_done(obj);
21745ca02815Sjsg 			if (err)
21755ca02815Sjsg 				break;
21765ca02815Sjsg 		}
21775ca02815Sjsg 
21785ca02815Sjsg 		read_unlock(&eb->i915->mm.notifier_lock);
21795ca02815Sjsg 	}
21805ca02815Sjsg #endif
21815ca02815Sjsg 
2182c349dbc7Sjsg 	if (unlikely(err))
2183c349dbc7Sjsg 		goto err_skip;
2184c349dbc7Sjsg 
2185c349dbc7Sjsg 	/* Unconditionally flush any chipset caches (for streaming writes). */
21861bb76ff1Sjsg 	intel_gt_chipset_flush(eb->gt);
21871bb76ff1Sjsg 	eb_capture_commit(eb);
21881bb76ff1Sjsg 
2189c349dbc7Sjsg 	return 0;
2190c349dbc7Sjsg 
2191c349dbc7Sjsg err_skip:
21921bb76ff1Sjsg 	for_each_batch_create_order(eb, j) {
21931bb76ff1Sjsg 		if (!eb->requests[j])
21941bb76ff1Sjsg 			break;
21951bb76ff1Sjsg 
21961bb76ff1Sjsg 		i915_request_set_error_once(eb->requests[j], err);
21971bb76ff1Sjsg 	}
2198c349dbc7Sjsg 	return err;
2199c349dbc7Sjsg }
2200c349dbc7Sjsg 
i915_gem_check_execbuffer(struct drm_i915_private * i915,struct drm_i915_gem_execbuffer2 * exec)2201*f005ef32Sjsg static int i915_gem_check_execbuffer(struct drm_i915_private *i915,
2202*f005ef32Sjsg 				     struct drm_i915_gem_execbuffer2 *exec)
2203c349dbc7Sjsg {
2204c349dbc7Sjsg 	if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
2205c349dbc7Sjsg 		return -EINVAL;
2206c349dbc7Sjsg 
2207c349dbc7Sjsg 	/* Kernel clipping was a DRI1 misfeature */
2208ad8b1aafSjsg 	if (!(exec->flags & (I915_EXEC_FENCE_ARRAY |
2209ad8b1aafSjsg 			     I915_EXEC_USE_EXTENSIONS))) {
2210c349dbc7Sjsg 		if (exec->num_cliprects || exec->cliprects_ptr)
2211c349dbc7Sjsg 			return -EINVAL;
2212c349dbc7Sjsg 	}
2213c349dbc7Sjsg 
2214c349dbc7Sjsg 	if (exec->DR4 == 0xffffffff) {
2215*f005ef32Sjsg 		drm_dbg(&i915->drm, "UXA submitting garbage DR4, fixing up\n");
2216c349dbc7Sjsg 		exec->DR4 = 0;
2217c349dbc7Sjsg 	}
2218c349dbc7Sjsg 	if (exec->DR1 || exec->DR4)
2219c349dbc7Sjsg 		return -EINVAL;
2220c349dbc7Sjsg 
2221c349dbc7Sjsg 	if ((exec->batch_start_offset | exec->batch_len) & 0x7)
2222c349dbc7Sjsg 		return -EINVAL;
2223c349dbc7Sjsg 
2224c349dbc7Sjsg 	return 0;
2225c349dbc7Sjsg }
2226c349dbc7Sjsg 
i915_reset_gen7_sol_offsets(struct i915_request * rq)2227c349dbc7Sjsg static int i915_reset_gen7_sol_offsets(struct i915_request *rq)
2228c349dbc7Sjsg {
2229c349dbc7Sjsg 	u32 *cs;
2230c349dbc7Sjsg 	int i;
2231c349dbc7Sjsg 
2232*f005ef32Sjsg 	if (GRAPHICS_VER(rq->i915) != 7 || rq->engine->id != RCS0) {
2233*f005ef32Sjsg 		drm_dbg(&rq->i915->drm, "sol reset is gen7/rcs only\n");
2234c349dbc7Sjsg 		return -EINVAL;
2235c349dbc7Sjsg 	}
2236c349dbc7Sjsg 
2237c349dbc7Sjsg 	cs = intel_ring_begin(rq, 4 * 2 + 2);
2238c349dbc7Sjsg 	if (IS_ERR(cs))
2239c349dbc7Sjsg 		return PTR_ERR(cs);
2240c349dbc7Sjsg 
2241c349dbc7Sjsg 	*cs++ = MI_LOAD_REGISTER_IMM(4);
2242c349dbc7Sjsg 	for (i = 0; i < 4; i++) {
2243c349dbc7Sjsg 		*cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
2244c349dbc7Sjsg 		*cs++ = 0;
2245c349dbc7Sjsg 	}
2246c349dbc7Sjsg 	*cs++ = MI_NOOP;
2247c349dbc7Sjsg 	intel_ring_advance(rq, cs);
2248c349dbc7Sjsg 
2249c349dbc7Sjsg 	return 0;
2250c349dbc7Sjsg }
2251c349dbc7Sjsg 
2252c349dbc7Sjsg static struct i915_vma *
shadow_batch_pin(struct i915_execbuffer * eb,struct drm_i915_gem_object * obj,struct i915_address_space * vm,unsigned int flags)2253ad8b1aafSjsg shadow_batch_pin(struct i915_execbuffer *eb,
2254ad8b1aafSjsg 		 struct drm_i915_gem_object *obj,
2255c349dbc7Sjsg 		 struct i915_address_space *vm,
2256c349dbc7Sjsg 		 unsigned int flags)
2257c349dbc7Sjsg {
2258c349dbc7Sjsg 	struct i915_vma *vma;
2259c349dbc7Sjsg 	int err;
2260c349dbc7Sjsg 
2261c349dbc7Sjsg 	vma = i915_vma_instance(obj, vm, NULL);
2262c349dbc7Sjsg 	if (IS_ERR(vma))
2263c349dbc7Sjsg 		return vma;
2264c349dbc7Sjsg 
22651bb76ff1Sjsg 	err = i915_vma_pin_ww(vma, &eb->ww, 0, 0, flags | PIN_VALIDATE);
2266c349dbc7Sjsg 	if (err)
2267c349dbc7Sjsg 		return ERR_PTR(err);
2268c349dbc7Sjsg 
2269c349dbc7Sjsg 	return vma;
2270c349dbc7Sjsg }
2271c349dbc7Sjsg 
eb_dispatch_secure(struct i915_execbuffer * eb,struct i915_vma * vma)2272ad8b1aafSjsg static struct i915_vma *eb_dispatch_secure(struct i915_execbuffer *eb, struct i915_vma *vma)
2273ad8b1aafSjsg {
2274ad8b1aafSjsg 	/*
2275ad8b1aafSjsg 	 * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
2276ad8b1aafSjsg 	 * batch" bit. Hence we need to pin secure batches into the global gtt.
2277ad8b1aafSjsg 	 * hsw should have this fixed, but bdw mucks it up again. */
2278ad8b1aafSjsg 	if (eb->batch_flags & I915_DISPATCH_SECURE)
22791bb76ff1Sjsg 		return i915_gem_object_ggtt_pin_ww(vma->obj, &eb->ww, NULL, 0, 0, PIN_VALIDATE);
2280ad8b1aafSjsg 
2281ad8b1aafSjsg 	return NULL;
2282ad8b1aafSjsg }
2283ad8b1aafSjsg 
eb_parse(struct i915_execbuffer * eb)2284c349dbc7Sjsg static int eb_parse(struct i915_execbuffer *eb)
2285c349dbc7Sjsg {
2286c349dbc7Sjsg 	struct drm_i915_private *i915 = eb->i915;
2287ad8b1aafSjsg 	struct intel_gt_buffer_pool_node *pool = eb->batch_pool;
2288ad8b1aafSjsg 	struct i915_vma *shadow, *trampoline, *batch;
2289ad8b1aafSjsg 	unsigned long len;
2290c349dbc7Sjsg 	int err;
2291c349dbc7Sjsg 
2292ad8b1aafSjsg 	if (!eb_use_cmdparser(eb)) {
22931bb76ff1Sjsg 		batch = eb_dispatch_secure(eb, eb->batches[0]->vma);
2294ad8b1aafSjsg 		if (IS_ERR(batch))
2295ad8b1aafSjsg 			return PTR_ERR(batch);
2296ad8b1aafSjsg 
2297ad8b1aafSjsg 		goto secure_batch;
2298ad8b1aafSjsg 	}
2299c349dbc7Sjsg 
23001bb76ff1Sjsg 	if (intel_context_is_parallel(eb->context))
23011bb76ff1Sjsg 		return -EINVAL;
23021bb76ff1Sjsg 
23031bb76ff1Sjsg 	len = eb->batch_len[0];
2304c349dbc7Sjsg 	if (!CMDPARSER_USES_GGTT(eb->i915)) {
2305c349dbc7Sjsg 		/*
2306c349dbc7Sjsg 		 * ppGTT backed shadow buffers must be mapped RO, to prevent
2307c349dbc7Sjsg 		 * post-scan tampering
2308c349dbc7Sjsg 		 */
2309c349dbc7Sjsg 		if (!eb->context->vm->has_read_only) {
2310c349dbc7Sjsg 			drm_dbg(&i915->drm,
2311c349dbc7Sjsg 				"Cannot prevent post-scan tampering without RO capable vm\n");
2312c349dbc7Sjsg 			return -EINVAL;
2313c349dbc7Sjsg 		}
2314c349dbc7Sjsg 	} else {
2315c349dbc7Sjsg 		len += I915_CMD_PARSER_TRAMPOLINE_SIZE;
2316c349dbc7Sjsg 	}
23171bb76ff1Sjsg 	if (unlikely(len < eb->batch_len[0])) /* last paranoid check of overflow */
2318ad8b1aafSjsg 		return -EINVAL;
2319c349dbc7Sjsg 
2320ad8b1aafSjsg 	if (!pool) {
23211bb76ff1Sjsg 		pool = intel_gt_get_buffer_pool(eb->gt, len,
23225ca02815Sjsg 						I915_MAP_WB);
2323c349dbc7Sjsg 		if (IS_ERR(pool))
2324c349dbc7Sjsg 			return PTR_ERR(pool);
2325ad8b1aafSjsg 		eb->batch_pool = pool;
2326ad8b1aafSjsg 	}
2327c349dbc7Sjsg 
2328ad8b1aafSjsg 	err = i915_gem_object_lock(pool->obj, &eb->ww);
2329ad8b1aafSjsg 	if (err)
23301bb76ff1Sjsg 		return err;
2331ad8b1aafSjsg 
2332ad8b1aafSjsg 	shadow = shadow_batch_pin(eb, pool->obj, eb->context->vm, PIN_USER);
23331bb76ff1Sjsg 	if (IS_ERR(shadow))
23341bb76ff1Sjsg 		return PTR_ERR(shadow);
23351bb76ff1Sjsg 
23365ca02815Sjsg 	intel_gt_buffer_pool_mark_used(pool);
2337c349dbc7Sjsg 	i915_gem_object_set_readonly(shadow->obj);
2338ad8b1aafSjsg 	shadow->private = pool;
2339c349dbc7Sjsg 
2340c349dbc7Sjsg 	trampoline = NULL;
2341c349dbc7Sjsg 	if (CMDPARSER_USES_GGTT(eb->i915)) {
2342c349dbc7Sjsg 		trampoline = shadow;
2343c349dbc7Sjsg 
2344ad8b1aafSjsg 		shadow = shadow_batch_pin(eb, pool->obj,
23451bb76ff1Sjsg 					  &eb->gt->ggtt->vm,
2346c349dbc7Sjsg 					  PIN_GLOBAL);
23471bb76ff1Sjsg 		if (IS_ERR(shadow))
23481bb76ff1Sjsg 			return PTR_ERR(shadow);
23491bb76ff1Sjsg 
2350ad8b1aafSjsg 		shadow->private = pool;
2351c349dbc7Sjsg 
2352c349dbc7Sjsg 		eb->batch_flags |= I915_DISPATCH_SECURE;
2353c349dbc7Sjsg 	}
2354c349dbc7Sjsg 
2355a37a8dbaSjsg 	batch = eb_dispatch_secure(eb, shadow);
23561bb76ff1Sjsg 	if (IS_ERR(batch))
23571bb76ff1Sjsg 		return PTR_ERR(batch);
2358a37a8dbaSjsg 
23591bb76ff1Sjsg 	err = dma_resv_reserve_fences(shadow->obj->base.resv, 1);
23605ca02815Sjsg 	if (err)
23611bb76ff1Sjsg 		return err;
23625ca02815Sjsg 
23631bb76ff1Sjsg 	err = intel_engine_cmd_parser(eb->context->engine,
23641bb76ff1Sjsg 				      eb->batches[0]->vma,
2365d9ace711Sjsg 				      eb->batch_start_offset,
23661bb76ff1Sjsg 				      eb->batch_len[0],
2367d9ace711Sjsg 				      shadow, trampoline);
2368c349dbc7Sjsg 	if (err)
23691bb76ff1Sjsg 		return err;
2370c349dbc7Sjsg 
23711bb76ff1Sjsg 	eb->batches[0] = &eb->vma[eb->buffer_count++];
23721bb76ff1Sjsg 	eb->batches[0]->vma = i915_vma_get(shadow);
23731bb76ff1Sjsg 	eb->batches[0]->flags = __EXEC_OBJECT_HAS_PIN;
2374c349dbc7Sjsg 
2375c349dbc7Sjsg 	eb->trampoline = trampoline;
2376c349dbc7Sjsg 	eb->batch_start_offset = 0;
2377c349dbc7Sjsg 
2378ad8b1aafSjsg secure_batch:
2379ad8b1aafSjsg 	if (batch) {
23801bb76ff1Sjsg 		if (intel_context_is_parallel(eb->context))
23811bb76ff1Sjsg 			return -EINVAL;
23821bb76ff1Sjsg 
23831bb76ff1Sjsg 		eb->batches[0] = &eb->vma[eb->buffer_count++];
23841bb76ff1Sjsg 		eb->batches[0]->flags = __EXEC_OBJECT_HAS_PIN;
23851bb76ff1Sjsg 		eb->batches[0]->vma = i915_vma_get(batch);
2386ad8b1aafSjsg 	}
2387c349dbc7Sjsg 	return 0;
2388c349dbc7Sjsg }
2389c349dbc7Sjsg 
eb_request_submit(struct i915_execbuffer * eb,struct i915_request * rq,struct i915_vma * batch,u64 batch_len)23901bb76ff1Sjsg static int eb_request_submit(struct i915_execbuffer *eb,
23911bb76ff1Sjsg 			     struct i915_request *rq,
23921bb76ff1Sjsg 			     struct i915_vma *batch,
23931bb76ff1Sjsg 			     u64 batch_len)
2394c349dbc7Sjsg {
2395c349dbc7Sjsg 	int err;
2396c349dbc7Sjsg 
23971bb76ff1Sjsg 	if (intel_context_nopreempt(rq->context))
23981bb76ff1Sjsg 		__set_bit(I915_FENCE_FLAG_NOPREEMPT, &rq->fence.flags);
2399c349dbc7Sjsg 
2400c349dbc7Sjsg 	if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
24011bb76ff1Sjsg 		err = i915_reset_gen7_sol_offsets(rq);
2402c349dbc7Sjsg 		if (err)
2403c349dbc7Sjsg 			return err;
2404c349dbc7Sjsg 	}
2405c349dbc7Sjsg 
2406c349dbc7Sjsg 	/*
2407c349dbc7Sjsg 	 * After we completed waiting for other engines (using HW semaphores)
2408c349dbc7Sjsg 	 * then we can signal that this request/batch is ready to run. This
2409c349dbc7Sjsg 	 * allows us to determine if the batch is still waiting on the GPU
2410c349dbc7Sjsg 	 * or actually running by checking the breadcrumb.
2411c349dbc7Sjsg 	 */
24121bb76ff1Sjsg 	if (rq->context->engine->emit_init_breadcrumb) {
24131bb76ff1Sjsg 		err = rq->context->engine->emit_init_breadcrumb(rq);
2414c349dbc7Sjsg 		if (err)
2415c349dbc7Sjsg 			return err;
2416c349dbc7Sjsg 	}
2417c349dbc7Sjsg 
24181bb76ff1Sjsg 	err = rq->context->engine->emit_bb_start(rq,
2419*f005ef32Sjsg 						 i915_vma_offset(batch) +
2420c349dbc7Sjsg 						 eb->batch_start_offset,
24211bb76ff1Sjsg 						 batch_len,
2422c349dbc7Sjsg 						 eb->batch_flags);
2423c349dbc7Sjsg 	if (err)
2424c349dbc7Sjsg 		return err;
2425c349dbc7Sjsg 
2426c349dbc7Sjsg 	if (eb->trampoline) {
24271bb76ff1Sjsg 		GEM_BUG_ON(intel_context_is_parallel(rq->context));
2428c349dbc7Sjsg 		GEM_BUG_ON(eb->batch_start_offset);
24291bb76ff1Sjsg 		err = rq->context->engine->emit_bb_start(rq,
2430*f005ef32Sjsg 							 i915_vma_offset(eb->trampoline) +
24311bb76ff1Sjsg 							 batch_len, 0, 0);
2432c349dbc7Sjsg 		if (err)
2433c349dbc7Sjsg 			return err;
2434c349dbc7Sjsg 	}
2435c349dbc7Sjsg 
2436c349dbc7Sjsg 	return 0;
2437c349dbc7Sjsg }
2438c349dbc7Sjsg 
eb_submit(struct i915_execbuffer * eb)24391bb76ff1Sjsg static int eb_submit(struct i915_execbuffer *eb)
2440c349dbc7Sjsg {
24411bb76ff1Sjsg 	unsigned int i;
24421bb76ff1Sjsg 	int err;
24431bb76ff1Sjsg 
24441bb76ff1Sjsg 	err = eb_move_to_gpu(eb);
24451bb76ff1Sjsg 
24461bb76ff1Sjsg 	for_each_batch_create_order(eb, i) {
24471bb76ff1Sjsg 		if (!eb->requests[i])
24481bb76ff1Sjsg 			break;
24491bb76ff1Sjsg 
24501bb76ff1Sjsg 		trace_i915_request_queue(eb->requests[i], eb->batch_flags);
24511bb76ff1Sjsg 		if (!err)
24521bb76ff1Sjsg 			err = eb_request_submit(eb, eb->requests[i],
24531bb76ff1Sjsg 						eb->batches[i]->vma,
24541bb76ff1Sjsg 						eb->batch_len[i]);
24551bb76ff1Sjsg 	}
24561bb76ff1Sjsg 
24571bb76ff1Sjsg 	return err;
24581bb76ff1Sjsg }
24591bb76ff1Sjsg 
2460c349dbc7Sjsg /*
2461c349dbc7Sjsg  * Find one BSD ring to dispatch the corresponding BSD command.
2462c349dbc7Sjsg  * The engine index is returned.
2463c349dbc7Sjsg  */
2464c349dbc7Sjsg static unsigned int
gen8_dispatch_bsd_engine(struct drm_i915_private * dev_priv,struct drm_file * file)2465c349dbc7Sjsg gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
2466c349dbc7Sjsg 			 struct drm_file *file)
2467c349dbc7Sjsg {
2468c349dbc7Sjsg 	struct drm_i915_file_private *file_priv = file->driver_priv;
2469c349dbc7Sjsg 
2470c349dbc7Sjsg 	/* Check whether the file_priv has already selected one ring. */
2471c349dbc7Sjsg 	if ((int)file_priv->bsd_engine < 0)
2472c349dbc7Sjsg 		file_priv->bsd_engine =
2473*f005ef32Sjsg 			get_random_u32_below(dev_priv->engine_uabi_class_count[I915_ENGINE_CLASS_VIDEO]);
2474c349dbc7Sjsg 
2475c349dbc7Sjsg 	return file_priv->bsd_engine;
2476c349dbc7Sjsg }
2477c349dbc7Sjsg 
2478c349dbc7Sjsg static const enum intel_engine_id user_ring_map[] = {
2479c349dbc7Sjsg 	[I915_EXEC_DEFAULT]	= RCS0,
2480c349dbc7Sjsg 	[I915_EXEC_RENDER]	= RCS0,
2481c349dbc7Sjsg 	[I915_EXEC_BLT]		= BCS0,
2482c349dbc7Sjsg 	[I915_EXEC_BSD]		= VCS0,
2483c349dbc7Sjsg 	[I915_EXEC_VEBOX]	= VECS0
2484c349dbc7Sjsg };
2485c349dbc7Sjsg 
eb_throttle(struct i915_execbuffer * eb,struct intel_context * ce)2486ad8b1aafSjsg static struct i915_request *eb_throttle(struct i915_execbuffer *eb, struct intel_context *ce)
2487c349dbc7Sjsg {
2488c349dbc7Sjsg 	struct intel_ring *ring = ce->ring;
2489c349dbc7Sjsg 	struct intel_timeline *tl = ce->timeline;
2490c349dbc7Sjsg 	struct i915_request *rq;
2491c349dbc7Sjsg 
2492c349dbc7Sjsg 	/*
2493c349dbc7Sjsg 	 * Completely unscientific finger-in-the-air estimates for suitable
2494c349dbc7Sjsg 	 * maximum user request size (to avoid blocking) and then backoff.
2495c349dbc7Sjsg 	 */
2496c349dbc7Sjsg 	if (intel_ring_update_space(ring) >= PAGE_SIZE)
2497c349dbc7Sjsg 		return NULL;
2498c349dbc7Sjsg 
2499c349dbc7Sjsg 	/*
2500c349dbc7Sjsg 	 * Find a request that after waiting upon, there will be at least half
2501c349dbc7Sjsg 	 * the ring available. The hysteresis allows us to compete for the
2502c349dbc7Sjsg 	 * shared ring and should mean that we sleep less often prior to
2503c349dbc7Sjsg 	 * claiming our resources, but not so long that the ring completely
2504c349dbc7Sjsg 	 * drains before we can submit our next request.
2505c349dbc7Sjsg 	 */
2506c349dbc7Sjsg 	list_for_each_entry(rq, &tl->requests, link) {
2507c349dbc7Sjsg 		if (rq->ring != ring)
2508c349dbc7Sjsg 			continue;
2509c349dbc7Sjsg 
2510c349dbc7Sjsg 		if (__intel_ring_space(rq->postfix,
2511c349dbc7Sjsg 				       ring->emit, ring->size) > ring->size / 2)
2512c349dbc7Sjsg 			break;
2513c349dbc7Sjsg 	}
2514c349dbc7Sjsg 	if (&rq->link == &tl->requests)
2515c349dbc7Sjsg 		return NULL; /* weird, we will check again later for real */
2516c349dbc7Sjsg 
2517c349dbc7Sjsg 	return i915_request_get(rq);
2518c349dbc7Sjsg }
2519c349dbc7Sjsg 
eb_pin_timeline(struct i915_execbuffer * eb,struct intel_context * ce,bool throttle)25201bb76ff1Sjsg static int eb_pin_timeline(struct i915_execbuffer *eb, struct intel_context *ce,
25211bb76ff1Sjsg 			   bool throttle)
2522c349dbc7Sjsg {
2523c349dbc7Sjsg 	struct intel_timeline *tl;
2524ad8b1aafSjsg 	struct i915_request *rq = NULL;
2525c349dbc7Sjsg 
2526c349dbc7Sjsg 	/*
2527c349dbc7Sjsg 	 * Take a local wakeref for preparing to dispatch the execbuf as
2528c349dbc7Sjsg 	 * we expect to access the hardware fairly frequently in the
2529c349dbc7Sjsg 	 * process, and require the engine to be kept awake between accesses.
2530c349dbc7Sjsg 	 * Upon dispatch, we acquire another prolonged wakeref that we hold
2531c349dbc7Sjsg 	 * until the timeline is idle, which in turn releases the wakeref
2532c349dbc7Sjsg 	 * taken on the engine, and the parent device.
2533c349dbc7Sjsg 	 */
2534c349dbc7Sjsg 	tl = intel_context_timeline_lock(ce);
25351bb76ff1Sjsg 	if (IS_ERR(tl))
25361bb76ff1Sjsg 		return PTR_ERR(tl);
2537c349dbc7Sjsg 
2538c349dbc7Sjsg 	intel_context_enter(ce);
2539ad8b1aafSjsg 	if (throttle)
2540ad8b1aafSjsg 		rq = eb_throttle(eb, ce);
2541c349dbc7Sjsg 	intel_context_timeline_unlock(tl);
2542c349dbc7Sjsg 
25431bb76ff1Sjsg 	if (rq) {
25441bb76ff1Sjsg #ifdef __linux__
25451bb76ff1Sjsg 		bool nonblock = eb->file->filp->f_flags & O_NONBLOCK;
25461bb76ff1Sjsg #else
25471bb76ff1Sjsg 		bool nonblock = eb->file->filp->f_flag & FNONBLOCK;
25481bb76ff1Sjsg #endif
25491bb76ff1Sjsg 		long timeout = nonblock ? 0 : MAX_SCHEDULE_TIMEOUT;
25501bb76ff1Sjsg 
25511bb76ff1Sjsg 		if (i915_request_wait(rq, I915_WAIT_INTERRUPTIBLE,
25521bb76ff1Sjsg 				      timeout) < 0) {
25531bb76ff1Sjsg 			i915_request_put(rq);
25541bb76ff1Sjsg 
25551bb76ff1Sjsg 			/*
25561bb76ff1Sjsg 			 * Error path, cannot use intel_context_timeline_lock as
25571bb76ff1Sjsg 			 * that is user interruptable and this clean up step
25581bb76ff1Sjsg 			 * must be done.
25591bb76ff1Sjsg 			 */
25601bb76ff1Sjsg 			mutex_lock(&ce->timeline->mutex);
25611bb76ff1Sjsg 			intel_context_exit(ce);
25621bb76ff1Sjsg 			mutex_unlock(&ce->timeline->mutex);
25631bb76ff1Sjsg 
25641bb76ff1Sjsg 			if (nonblock)
25651bb76ff1Sjsg 				return -EWOULDBLOCK;
25661bb76ff1Sjsg 			else
25671bb76ff1Sjsg 				return -EINTR;
25681bb76ff1Sjsg 		}
25691bb76ff1Sjsg 		i915_request_put(rq);
25701bb76ff1Sjsg 	}
25711bb76ff1Sjsg 
25721bb76ff1Sjsg 	return 0;
25731bb76ff1Sjsg }
25741bb76ff1Sjsg 
eb_pin_engine(struct i915_execbuffer * eb,bool throttle)25751bb76ff1Sjsg static int eb_pin_engine(struct i915_execbuffer *eb, bool throttle)
25761bb76ff1Sjsg {
25771bb76ff1Sjsg 	struct intel_context *ce = eb->context, *child;
25781bb76ff1Sjsg 	int err;
25791bb76ff1Sjsg 	int i = 0, j = 0;
25801bb76ff1Sjsg 
25811bb76ff1Sjsg 	GEM_BUG_ON(eb->args->flags & __EXEC_ENGINE_PINNED);
25821bb76ff1Sjsg 
25831bb76ff1Sjsg 	if (unlikely(intel_context_is_banned(ce)))
25841bb76ff1Sjsg 		return -EIO;
25851bb76ff1Sjsg 
25861bb76ff1Sjsg 	/*
25871bb76ff1Sjsg 	 * Pinning the contexts may generate requests in order to acquire
25881bb76ff1Sjsg 	 * GGTT space, so do this first before we reserve a seqno for
25891bb76ff1Sjsg 	 * ourselves.
25901bb76ff1Sjsg 	 */
25911bb76ff1Sjsg 	err = intel_context_pin_ww(ce, &eb->ww);
25921bb76ff1Sjsg 	if (err)
25931bb76ff1Sjsg 		return err;
25941bb76ff1Sjsg 	for_each_child(ce, child) {
25951bb76ff1Sjsg 		err = intel_context_pin_ww(child, &eb->ww);
25961bb76ff1Sjsg 		GEM_BUG_ON(err);	/* perma-pinned should incr a counter */
25971bb76ff1Sjsg 	}
25981bb76ff1Sjsg 
25991bb76ff1Sjsg 	for_each_child(ce, child) {
26001bb76ff1Sjsg 		err = eb_pin_timeline(eb, child, throttle);
26011bb76ff1Sjsg 		if (err)
26021bb76ff1Sjsg 			goto unwind;
26031bb76ff1Sjsg 		++i;
26041bb76ff1Sjsg 	}
26051bb76ff1Sjsg 	err = eb_pin_timeline(eb, ce, throttle);
26061bb76ff1Sjsg 	if (err)
26071bb76ff1Sjsg 		goto unwind;
26081bb76ff1Sjsg 
2609ad8b1aafSjsg 	eb->args->flags |= __EXEC_ENGINE_PINNED;
26101bb76ff1Sjsg 	return 0;
26111bb76ff1Sjsg 
26121bb76ff1Sjsg unwind:
26131bb76ff1Sjsg 	for_each_child(ce, child) {
26141bb76ff1Sjsg 		if (j++ < i) {
26151bb76ff1Sjsg 			mutex_lock(&child->timeline->mutex);
26161bb76ff1Sjsg 			intel_context_exit(child);
26171bb76ff1Sjsg 			mutex_unlock(&child->timeline->mutex);
26181bb76ff1Sjsg 		}
26191bb76ff1Sjsg 	}
26201bb76ff1Sjsg 	for_each_child(ce, child)
26211bb76ff1Sjsg 		intel_context_unpin(child);
26221bb76ff1Sjsg 	intel_context_unpin(ce);
26231bb76ff1Sjsg 	return err;
2624c349dbc7Sjsg }
2625c349dbc7Sjsg 
eb_unpin_engine(struct i915_execbuffer * eb)2626c349dbc7Sjsg static void eb_unpin_engine(struct i915_execbuffer *eb)
2627c349dbc7Sjsg {
26281bb76ff1Sjsg 	struct intel_context *ce = eb->context, *child;
2629c349dbc7Sjsg 
2630ad8b1aafSjsg 	if (!(eb->args->flags & __EXEC_ENGINE_PINNED))
2631ad8b1aafSjsg 		return;
2632ad8b1aafSjsg 
2633ad8b1aafSjsg 	eb->args->flags &= ~__EXEC_ENGINE_PINNED;
2634ad8b1aafSjsg 
26351bb76ff1Sjsg 	for_each_child(ce, child) {
26361bb76ff1Sjsg 		mutex_lock(&child->timeline->mutex);
26371bb76ff1Sjsg 		intel_context_exit(child);
26381bb76ff1Sjsg 		mutex_unlock(&child->timeline->mutex);
26391bb76ff1Sjsg 
26401bb76ff1Sjsg 		intel_context_unpin(child);
26411bb76ff1Sjsg 	}
26421bb76ff1Sjsg 
26431bb76ff1Sjsg 	mutex_lock(&ce->timeline->mutex);
2644c349dbc7Sjsg 	intel_context_exit(ce);
26451bb76ff1Sjsg 	mutex_unlock(&ce->timeline->mutex);
2646c349dbc7Sjsg 
2647c349dbc7Sjsg 	intel_context_unpin(ce);
2648c349dbc7Sjsg }
2649c349dbc7Sjsg 
2650c349dbc7Sjsg static unsigned int
eb_select_legacy_ring(struct i915_execbuffer * eb)2651ad8b1aafSjsg eb_select_legacy_ring(struct i915_execbuffer *eb)
2652c349dbc7Sjsg {
2653c349dbc7Sjsg 	struct drm_i915_private *i915 = eb->i915;
2654ad8b1aafSjsg 	struct drm_i915_gem_execbuffer2 *args = eb->args;
2655c349dbc7Sjsg 	unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
2656c349dbc7Sjsg 
2657c349dbc7Sjsg 	if (user_ring_id != I915_EXEC_BSD &&
2658c349dbc7Sjsg 	    (args->flags & I915_EXEC_BSD_MASK)) {
2659c349dbc7Sjsg 		drm_dbg(&i915->drm,
2660c349dbc7Sjsg 			"execbuf with non bsd ring but with invalid "
2661c349dbc7Sjsg 			"bsd dispatch flags: %d\n", (int)(args->flags));
2662c349dbc7Sjsg 		return -1;
2663c349dbc7Sjsg 	}
2664c349dbc7Sjsg 
2665*f005ef32Sjsg 	if (user_ring_id == I915_EXEC_BSD &&
2666*f005ef32Sjsg 	    i915->engine_uabi_class_count[I915_ENGINE_CLASS_VIDEO] > 1) {
2667c349dbc7Sjsg 		unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
2668c349dbc7Sjsg 
2669c349dbc7Sjsg 		if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
2670ad8b1aafSjsg 			bsd_idx = gen8_dispatch_bsd_engine(i915, eb->file);
2671c349dbc7Sjsg 		} else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
2672c349dbc7Sjsg 			   bsd_idx <= I915_EXEC_BSD_RING2) {
2673c349dbc7Sjsg 			bsd_idx >>= I915_EXEC_BSD_SHIFT;
2674c349dbc7Sjsg 			bsd_idx--;
2675c349dbc7Sjsg 		} else {
2676c349dbc7Sjsg 			drm_dbg(&i915->drm,
2677c349dbc7Sjsg 				"execbuf with unknown bsd ring: %u\n",
2678c349dbc7Sjsg 				bsd_idx);
2679c349dbc7Sjsg 			return -1;
2680c349dbc7Sjsg 		}
2681c349dbc7Sjsg 
2682c349dbc7Sjsg 		return _VCS(bsd_idx);
2683c349dbc7Sjsg 	}
2684c349dbc7Sjsg 
2685c349dbc7Sjsg 	if (user_ring_id >= ARRAY_SIZE(user_ring_map)) {
2686c349dbc7Sjsg 		drm_dbg(&i915->drm, "execbuf with unknown ring: %u\n",
2687c349dbc7Sjsg 			user_ring_id);
2688c349dbc7Sjsg 		return -1;
2689c349dbc7Sjsg 	}
2690c349dbc7Sjsg 
2691c349dbc7Sjsg 	return user_ring_map[user_ring_id];
2692c349dbc7Sjsg }
2693c349dbc7Sjsg 
2694c349dbc7Sjsg static int
eb_select_engine(struct i915_execbuffer * eb)2695ad8b1aafSjsg eb_select_engine(struct i915_execbuffer *eb)
2696c349dbc7Sjsg {
26971bb76ff1Sjsg 	struct intel_context *ce, *child;
2698*f005ef32Sjsg 	struct intel_gt *gt;
2699c349dbc7Sjsg 	unsigned int idx;
2700c349dbc7Sjsg 	int err;
2701c349dbc7Sjsg 
2702c349dbc7Sjsg 	if (i915_gem_context_user_engines(eb->gem_context))
2703ad8b1aafSjsg 		idx = eb->args->flags & I915_EXEC_RING_MASK;
2704c349dbc7Sjsg 	else
2705ad8b1aafSjsg 		idx = eb_select_legacy_ring(eb);
2706c349dbc7Sjsg 
2707c349dbc7Sjsg 	ce = i915_gem_context_get_engine(eb->gem_context, idx);
2708c349dbc7Sjsg 	if (IS_ERR(ce))
2709c349dbc7Sjsg 		return PTR_ERR(ce);
2710c349dbc7Sjsg 
27111bb76ff1Sjsg 	if (intel_context_is_parallel(ce)) {
27121bb76ff1Sjsg 		if (eb->buffer_count < ce->parallel.number_children + 1) {
27131bb76ff1Sjsg 			intel_context_put(ce);
27141bb76ff1Sjsg 			return -EINVAL;
27151bb76ff1Sjsg 		}
27161bb76ff1Sjsg 		if (eb->batch_start_offset || eb->args->batch_len) {
27171bb76ff1Sjsg 			intel_context_put(ce);
27181bb76ff1Sjsg 			return -EINVAL;
27191bb76ff1Sjsg 		}
27201bb76ff1Sjsg 	}
27211bb76ff1Sjsg 	eb->num_batches = ce->parallel.number_children + 1;
2722*f005ef32Sjsg 	gt = ce->engine->gt;
27231bb76ff1Sjsg 
27241bb76ff1Sjsg 	for_each_child(ce, child)
27251bb76ff1Sjsg 		intel_context_get(child);
2726*f005ef32Sjsg 	intel_gt_pm_get(gt);
2727*f005ef32Sjsg 	/*
2728*f005ef32Sjsg 	 * Keep GT0 active on MTL so that i915_vma_parked() doesn't
2729*f005ef32Sjsg 	 * free VMAs while execbuf ioctl is validating VMAs.
2730*f005ef32Sjsg 	 */
2731*f005ef32Sjsg 	if (gt->info.id)
2732*f005ef32Sjsg 		intel_gt_pm_get(to_gt(gt->i915));
2733c349dbc7Sjsg 
2734ad8b1aafSjsg 	if (!test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) {
2735ad8b1aafSjsg 		err = intel_context_alloc_state(ce);
2736ad8b1aafSjsg 		if (err)
2737ad8b1aafSjsg 			goto err;
2738ad8b1aafSjsg 	}
27391bb76ff1Sjsg 	for_each_child(ce, child) {
27401bb76ff1Sjsg 		if (!test_bit(CONTEXT_ALLOC_BIT, &child->flags)) {
27411bb76ff1Sjsg 			err = intel_context_alloc_state(child);
27421bb76ff1Sjsg 			if (err)
27431bb76ff1Sjsg 				goto err;
27441bb76ff1Sjsg 		}
27451bb76ff1Sjsg 	}
2746ad8b1aafSjsg 
2747ad8b1aafSjsg 	/*
2748ad8b1aafSjsg 	 * ABI: Before userspace accesses the GPU (e.g. execbuffer), report
2749ad8b1aafSjsg 	 * EIO if the GPU is already wedged.
2750ad8b1aafSjsg 	 */
2751ad8b1aafSjsg 	err = intel_gt_terminally_wedged(ce->engine->gt);
2752ad8b1aafSjsg 	if (err)
2753ad8b1aafSjsg 		goto err;
2754ad8b1aafSjsg 
27551bb76ff1Sjsg 	if (!i915_vm_tryget(ce->vm)) {
27561bb76ff1Sjsg 		err = -ENOENT;
27571bb76ff1Sjsg 		goto err;
27581bb76ff1Sjsg 	}
27591bb76ff1Sjsg 
2760ad8b1aafSjsg 	eb->context = ce;
27611bb76ff1Sjsg 	eb->gt = ce->engine->gt;
2762ad8b1aafSjsg 
2763ad8b1aafSjsg 	/*
2764ad8b1aafSjsg 	 * Make sure engine pool stays alive even if we call intel_context_put
2765ad8b1aafSjsg 	 * during ww handling. The pool is destroyed when last pm reference
2766ad8b1aafSjsg 	 * is dropped, which breaks our -EDEADLK handling.
2767ad8b1aafSjsg 	 */
2768ad8b1aafSjsg 	return err;
2769ad8b1aafSjsg 
2770ad8b1aafSjsg err:
2771*f005ef32Sjsg 	if (gt->info.id)
2772*f005ef32Sjsg 		intel_gt_pm_put(to_gt(gt->i915));
2773*f005ef32Sjsg 
2774*f005ef32Sjsg 	intel_gt_pm_put(gt);
27751bb76ff1Sjsg 	for_each_child(ce, child)
27761bb76ff1Sjsg 		intel_context_put(child);
2777ad8b1aafSjsg 	intel_context_put(ce);
2778c349dbc7Sjsg 	return err;
2779c349dbc7Sjsg }
2780c349dbc7Sjsg 
2781c349dbc7Sjsg static void
eb_put_engine(struct i915_execbuffer * eb)2782ad8b1aafSjsg eb_put_engine(struct i915_execbuffer *eb)
2783c349dbc7Sjsg {
27841bb76ff1Sjsg 	struct intel_context *child;
27851bb76ff1Sjsg 
27861bb76ff1Sjsg 	i915_vm_put(eb->context->vm);
2787*f005ef32Sjsg 	/*
2788*f005ef32Sjsg 	 * This works in conjunction with eb_select_engine() to prevent
2789*f005ef32Sjsg 	 * i915_vma_parked() from interfering while execbuf validates vmas.
2790*f005ef32Sjsg 	 */
2791*f005ef32Sjsg 	if (eb->gt->info.id)
2792*f005ef32Sjsg 		intel_gt_pm_put(to_gt(eb->gt->i915));
27931bb76ff1Sjsg 	intel_gt_pm_put(eb->gt);
27941bb76ff1Sjsg 	for_each_child(eb->context, child)
27951bb76ff1Sjsg 		intel_context_put(child);
2796ad8b1aafSjsg 	intel_context_put(eb->context);
2797ad8b1aafSjsg }
2798ad8b1aafSjsg 
2799ad8b1aafSjsg static void
__free_fence_array(struct eb_fence * fences,unsigned int n)2800ad8b1aafSjsg __free_fence_array(struct eb_fence *fences, unsigned int n)
2801ad8b1aafSjsg {
2802ad8b1aafSjsg 	while (n--) {
2803ad8b1aafSjsg 		drm_syncobj_put(ptr_mask_bits(fences[n].syncobj, 2));
2804ad8b1aafSjsg 		dma_fence_put(fences[n].dma_fence);
28055ca02815Sjsg 		dma_fence_chain_free(fences[n].chain_fence);
2806ad8b1aafSjsg 	}
2807c349dbc7Sjsg 	kvfree(fences);
2808c349dbc7Sjsg }
2809c349dbc7Sjsg 
2810ad8b1aafSjsg static int
add_timeline_fence_array(struct i915_execbuffer * eb,const struct drm_i915_gem_execbuffer_ext_timeline_fences * timeline_fences)2811ad8b1aafSjsg add_timeline_fence_array(struct i915_execbuffer *eb,
2812ad8b1aafSjsg 			 const struct drm_i915_gem_execbuffer_ext_timeline_fences *timeline_fences)
2813c349dbc7Sjsg {
2814ad8b1aafSjsg 	struct drm_i915_gem_exec_fence __user *user_fences;
2815ad8b1aafSjsg 	u64 __user *user_values;
2816ad8b1aafSjsg 	struct eb_fence *f;
2817ad8b1aafSjsg 	u64 nfences;
2818ad8b1aafSjsg 	int err = 0;
2819c349dbc7Sjsg 
2820ad8b1aafSjsg 	nfences = timeline_fences->fence_count;
2821ad8b1aafSjsg 	if (!nfences)
2822ad8b1aafSjsg 		return 0;
2823c349dbc7Sjsg 
2824c349dbc7Sjsg 	/* Check multiplication overflow for access_ok() and kvmalloc_array() */
2825c349dbc7Sjsg 	BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long));
2826c349dbc7Sjsg 	if (nfences > min_t(unsigned long,
2827ad8b1aafSjsg 			    ULONG_MAX / sizeof(*user_fences),
2828ad8b1aafSjsg 			    SIZE_MAX / sizeof(*f)) - eb->num_fences)
2829ad8b1aafSjsg 		return -EINVAL;
2830c349dbc7Sjsg 
2831ad8b1aafSjsg 	user_fences = u64_to_user_ptr(timeline_fences->handles_ptr);
2832ad8b1aafSjsg 	if (!access_ok(user_fences, nfences * sizeof(*user_fences)))
2833ad8b1aafSjsg 		return -EFAULT;
2834c349dbc7Sjsg 
2835ad8b1aafSjsg 	user_values = u64_to_user_ptr(timeline_fences->values_ptr);
2836ad8b1aafSjsg 	if (!access_ok(user_values, nfences * sizeof(*user_values)))
2837ad8b1aafSjsg 		return -EFAULT;
2838ad8b1aafSjsg 
2839ad8b1aafSjsg #ifdef __linux__
2840ad8b1aafSjsg 	f = krealloc(eb->fences,
2841ad8b1aafSjsg 		     (eb->num_fences + nfences) * sizeof(*f),
2842c349dbc7Sjsg 		     __GFP_NOWARN | GFP_KERNEL);
2843ad8b1aafSjsg 	if (!f)
2844ad8b1aafSjsg 		return -ENOMEM;
2845ad8b1aafSjsg #else
2846ad8b1aafSjsg 	f = kmalloc((eb->num_fences + nfences) * sizeof(*f),
2847ad8b1aafSjsg 		     __GFP_NOWARN | GFP_KERNEL);
2848ad8b1aafSjsg 	if (!f)
2849ad8b1aafSjsg 		return -ENOMEM;
2850ad8b1aafSjsg 	memcpy(f, eb->fences, eb->num_fences * sizeof(*f));
2851ad8b1aafSjsg 	kfree(eb->fences);
2852ad8b1aafSjsg #endif
2853c349dbc7Sjsg 
2854ad8b1aafSjsg 	eb->fences = f;
2855ad8b1aafSjsg 	f += eb->num_fences;
2856ad8b1aafSjsg 
2857ad8b1aafSjsg #ifdef notyet
2858ad8b1aafSjsg 	BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) &
2859ad8b1aafSjsg 		     ~__I915_EXEC_FENCE_UNKNOWN_FLAGS);
2860ad8b1aafSjsg #endif
2861ad8b1aafSjsg 
2862ad8b1aafSjsg 	while (nfences--) {
2863ad8b1aafSjsg 		struct drm_i915_gem_exec_fence user_fence;
2864c349dbc7Sjsg 		struct drm_syncobj *syncobj;
2865ad8b1aafSjsg 		struct dma_fence *fence = NULL;
2866ad8b1aafSjsg 		u64 point;
2867c349dbc7Sjsg 
2868ad8b1aafSjsg 		if (__copy_from_user(&user_fence,
2869ad8b1aafSjsg 				     user_fences++,
2870ad8b1aafSjsg 				     sizeof(user_fence)))
2871ad8b1aafSjsg 			return -EFAULT;
2872c349dbc7Sjsg 
2873ad8b1aafSjsg 		if (user_fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS)
2874ad8b1aafSjsg 			return -EINVAL;
2875c349dbc7Sjsg 
2876ad8b1aafSjsg 		if (__get_user(point, user_values++))
2877ad8b1aafSjsg 			return -EFAULT;
2878ad8b1aafSjsg 
2879ad8b1aafSjsg 		syncobj = drm_syncobj_find(eb->file, user_fence.handle);
2880c349dbc7Sjsg 		if (!syncobj) {
2881*f005ef32Sjsg 			drm_dbg(&eb->i915->drm,
2882*f005ef32Sjsg 				"Invalid syncobj handle provided\n");
2883ad8b1aafSjsg 			return -ENOENT;
2884ad8b1aafSjsg 		}
2885ad8b1aafSjsg 
2886ad8b1aafSjsg 		fence = drm_syncobj_fence_get(syncobj);
2887ad8b1aafSjsg 
2888ad8b1aafSjsg 		if (!fence && user_fence.flags &&
2889ad8b1aafSjsg 		    !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) {
2890*f005ef32Sjsg 			drm_dbg(&eb->i915->drm,
2891*f005ef32Sjsg 				"Syncobj handle has no fence\n");
2892ad8b1aafSjsg 			drm_syncobj_put(syncobj);
2893ad8b1aafSjsg 			return -EINVAL;
2894ad8b1aafSjsg 		}
2895ad8b1aafSjsg 
2896ad8b1aafSjsg 		if (fence)
2897ad8b1aafSjsg 			err = dma_fence_chain_find_seqno(&fence, point);
2898ad8b1aafSjsg 
2899ad8b1aafSjsg 		if (err && !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) {
2900*f005ef32Sjsg 			drm_dbg(&eb->i915->drm,
2901*f005ef32Sjsg 				"Syncobj handle missing requested point %llu\n",
2902*f005ef32Sjsg 				point);
2903ad8b1aafSjsg 			dma_fence_put(fence);
2904ad8b1aafSjsg 			drm_syncobj_put(syncobj);
2905ad8b1aafSjsg 			return err;
2906ad8b1aafSjsg 		}
2907ad8b1aafSjsg 
2908ad8b1aafSjsg 		/*
2909ad8b1aafSjsg 		 * A point might have been signaled already and
2910ad8b1aafSjsg 		 * garbage collected from the timeline. In this case
2911ad8b1aafSjsg 		 * just ignore the point and carry on.
2912ad8b1aafSjsg 		 */
2913ad8b1aafSjsg 		if (!fence && !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) {
2914ad8b1aafSjsg 			drm_syncobj_put(syncobj);
2915ad8b1aafSjsg 			continue;
2916ad8b1aafSjsg 		}
2917ad8b1aafSjsg 
2918ad8b1aafSjsg 		/*
2919ad8b1aafSjsg 		 * For timeline syncobjs we need to preallocate chains for
2920ad8b1aafSjsg 		 * later signaling.
2921ad8b1aafSjsg 		 */
2922ad8b1aafSjsg 		if (point != 0 && user_fence.flags & I915_EXEC_FENCE_SIGNAL) {
2923ad8b1aafSjsg 			/*
2924ad8b1aafSjsg 			 * Waiting and signaling the same point (when point !=
2925ad8b1aafSjsg 			 * 0) would break the timeline.
2926ad8b1aafSjsg 			 */
2927ad8b1aafSjsg 			if (user_fence.flags & I915_EXEC_FENCE_WAIT) {
2928*f005ef32Sjsg 				drm_dbg(&eb->i915->drm,
2929*f005ef32Sjsg 					"Trying to wait & signal the same timeline point.\n");
2930ad8b1aafSjsg 				dma_fence_put(fence);
2931ad8b1aafSjsg 				drm_syncobj_put(syncobj);
2932ad8b1aafSjsg 				return -EINVAL;
2933ad8b1aafSjsg 			}
2934ad8b1aafSjsg 
29355ca02815Sjsg 			f->chain_fence = dma_fence_chain_alloc();
2936ad8b1aafSjsg 			if (!f->chain_fence) {
2937ad8b1aafSjsg 				drm_syncobj_put(syncobj);
2938ad8b1aafSjsg 				dma_fence_put(fence);
2939ad8b1aafSjsg 				return -ENOMEM;
2940ad8b1aafSjsg 			}
2941ad8b1aafSjsg 		} else {
2942ad8b1aafSjsg 			f->chain_fence = NULL;
2943ad8b1aafSjsg 		}
2944ad8b1aafSjsg 
2945ad8b1aafSjsg 		f->syncobj = ptr_pack_bits(syncobj, user_fence.flags, 2);
2946ad8b1aafSjsg 		f->dma_fence = fence;
2947ad8b1aafSjsg 		f->value = point;
2948ad8b1aafSjsg 		f++;
2949ad8b1aafSjsg 		eb->num_fences++;
2950ad8b1aafSjsg 	}
2951ad8b1aafSjsg 
2952ad8b1aafSjsg 	return 0;
2953ad8b1aafSjsg }
2954ad8b1aafSjsg 
add_fence_array(struct i915_execbuffer * eb)2955ad8b1aafSjsg static int add_fence_array(struct i915_execbuffer *eb)
2956ad8b1aafSjsg {
2957ad8b1aafSjsg 	struct drm_i915_gem_execbuffer2 *args = eb->args;
2958ad8b1aafSjsg 	struct drm_i915_gem_exec_fence __user *user;
2959ad8b1aafSjsg 	unsigned long num_fences = args->num_cliprects;
2960ad8b1aafSjsg 	struct eb_fence *f;
2961ad8b1aafSjsg 
2962ad8b1aafSjsg 	if (!(args->flags & I915_EXEC_FENCE_ARRAY))
2963ad8b1aafSjsg 		return 0;
2964ad8b1aafSjsg 
2965ad8b1aafSjsg 	if (!num_fences)
2966ad8b1aafSjsg 		return 0;
2967ad8b1aafSjsg 
2968ad8b1aafSjsg 	/* Check multiplication overflow for access_ok() and kvmalloc_array() */
2969ad8b1aafSjsg 	BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long));
2970ad8b1aafSjsg 	if (num_fences > min_t(unsigned long,
2971ad8b1aafSjsg 			       ULONG_MAX / sizeof(*user),
2972ad8b1aafSjsg 			       SIZE_MAX / sizeof(*f) - eb->num_fences))
2973ad8b1aafSjsg 		return -EINVAL;
2974ad8b1aafSjsg 
2975ad8b1aafSjsg 	user = u64_to_user_ptr(args->cliprects_ptr);
2976ad8b1aafSjsg 	if (!access_ok(user, num_fences * sizeof(*user)))
2977ad8b1aafSjsg 		return -EFAULT;
2978ad8b1aafSjsg 
2979ad8b1aafSjsg #ifdef __linux__
2980ad8b1aafSjsg 	f = krealloc(eb->fences,
2981ad8b1aafSjsg 		     (eb->num_fences + num_fences) * sizeof(*f),
2982ad8b1aafSjsg 		     __GFP_NOWARN | GFP_KERNEL);
2983ad8b1aafSjsg 	if (!f)
2984ad8b1aafSjsg 		return -ENOMEM;
2985ad8b1aafSjsg #else
2986ad8b1aafSjsg 	f = kmalloc((eb->num_fences + num_fences) * sizeof(*f),
2987ad8b1aafSjsg 		     __GFP_NOWARN | GFP_KERNEL);
2988ad8b1aafSjsg 	if (!f)
2989ad8b1aafSjsg 		return -ENOMEM;
2990ad8b1aafSjsg 	memcpy(f, eb->fences, eb->num_fences * sizeof(*f));
2991ad8b1aafSjsg 	kfree(eb->fences);
2992ad8b1aafSjsg #endif
2993ad8b1aafSjsg 
2994ad8b1aafSjsg 	eb->fences = f;
2995ad8b1aafSjsg 	f += eb->num_fences;
2996ad8b1aafSjsg 	while (num_fences--) {
2997ad8b1aafSjsg 		struct drm_i915_gem_exec_fence user_fence;
2998ad8b1aafSjsg 		struct drm_syncobj *syncobj;
2999ad8b1aafSjsg 		struct dma_fence *fence = NULL;
3000ad8b1aafSjsg 
3001ad8b1aafSjsg 		if (__copy_from_user(&user_fence, user++, sizeof(user_fence)))
3002ad8b1aafSjsg 			return -EFAULT;
3003ad8b1aafSjsg 
3004ad8b1aafSjsg 		if (user_fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS)
3005ad8b1aafSjsg 			return -EINVAL;
3006ad8b1aafSjsg 
3007ad8b1aafSjsg 		syncobj = drm_syncobj_find(eb->file, user_fence.handle);
3008ad8b1aafSjsg 		if (!syncobj) {
3009*f005ef32Sjsg 			drm_dbg(&eb->i915->drm,
3010*f005ef32Sjsg 				"Invalid syncobj handle provided\n");
3011ad8b1aafSjsg 			return -ENOENT;
3012ad8b1aafSjsg 		}
3013ad8b1aafSjsg 
3014ad8b1aafSjsg 		if (user_fence.flags & I915_EXEC_FENCE_WAIT) {
3015ad8b1aafSjsg 			fence = drm_syncobj_fence_get(syncobj);
3016ad8b1aafSjsg 			if (!fence) {
3017*f005ef32Sjsg 				drm_dbg(&eb->i915->drm,
3018*f005ef32Sjsg 					"Syncobj handle has no fence\n");
3019ad8b1aafSjsg 				drm_syncobj_put(syncobj);
3020ad8b1aafSjsg 				return -EINVAL;
3021ad8b1aafSjsg 			}
3022c349dbc7Sjsg 		}
3023c349dbc7Sjsg 
3024c349dbc7Sjsg #ifdef notyet
3025c349dbc7Sjsg 		BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) &
3026c349dbc7Sjsg 			     ~__I915_EXEC_FENCE_UNKNOWN_FLAGS);
3027c349dbc7Sjsg #endif
3028c349dbc7Sjsg 
3029ad8b1aafSjsg 		f->syncobj = ptr_pack_bits(syncobj, user_fence.flags, 2);
3030ad8b1aafSjsg 		f->dma_fence = fence;
3031ad8b1aafSjsg 		f->value = 0;
3032ad8b1aafSjsg 		f->chain_fence = NULL;
3033ad8b1aafSjsg 		f++;
3034ad8b1aafSjsg 		eb->num_fences++;
3035c349dbc7Sjsg 	}
3036c349dbc7Sjsg 
3037ad8b1aafSjsg 	return 0;
3038c349dbc7Sjsg }
3039c349dbc7Sjsg 
put_fence_array(struct eb_fence * fences,int num_fences)3040ad8b1aafSjsg static void put_fence_array(struct eb_fence *fences, int num_fences)
3041c349dbc7Sjsg {
3042c349dbc7Sjsg 	if (fences)
3043ad8b1aafSjsg 		__free_fence_array(fences, num_fences);
3044c349dbc7Sjsg }
3045c349dbc7Sjsg 
3046c349dbc7Sjsg static int
await_fence_array(struct i915_execbuffer * eb,struct i915_request * rq)30471bb76ff1Sjsg await_fence_array(struct i915_execbuffer *eb,
30481bb76ff1Sjsg 		  struct i915_request *rq)
3049c349dbc7Sjsg {
3050c349dbc7Sjsg 	unsigned int n;
3051c349dbc7Sjsg 	int err;
3052c349dbc7Sjsg 
3053ad8b1aafSjsg 	for (n = 0; n < eb->num_fences; n++) {
3054ad8b1aafSjsg 		if (!eb->fences[n].dma_fence)
3055c349dbc7Sjsg 			continue;
3056c349dbc7Sjsg 
30571bb76ff1Sjsg 		err = i915_request_await_dma_fence(rq, eb->fences[n].dma_fence);
3058c349dbc7Sjsg 		if (err < 0)
3059c349dbc7Sjsg 			return err;
3060c349dbc7Sjsg 	}
3061c349dbc7Sjsg 
3062c349dbc7Sjsg 	return 0;
3063c349dbc7Sjsg }
3064c349dbc7Sjsg 
signal_fence_array(const struct i915_execbuffer * eb,struct dma_fence * const fence)30651bb76ff1Sjsg static void signal_fence_array(const struct i915_execbuffer *eb,
30661bb76ff1Sjsg 			       struct dma_fence * const fence)
3067c349dbc7Sjsg {
3068c349dbc7Sjsg 	unsigned int n;
3069c349dbc7Sjsg 
3070ad8b1aafSjsg 	for (n = 0; n < eb->num_fences; n++) {
3071c349dbc7Sjsg 		struct drm_syncobj *syncobj;
3072c349dbc7Sjsg 		unsigned int flags;
3073c349dbc7Sjsg 
3074ad8b1aafSjsg 		syncobj = ptr_unpack_bits(eb->fences[n].syncobj, &flags, 2);
3075c349dbc7Sjsg 		if (!(flags & I915_EXEC_FENCE_SIGNAL))
3076c349dbc7Sjsg 			continue;
3077c349dbc7Sjsg 
3078ad8b1aafSjsg 		if (eb->fences[n].chain_fence) {
3079ad8b1aafSjsg 			drm_syncobj_add_point(syncobj,
3080ad8b1aafSjsg 					      eb->fences[n].chain_fence,
3081ad8b1aafSjsg 					      fence,
3082ad8b1aafSjsg 					      eb->fences[n].value);
3083ad8b1aafSjsg 			/*
3084ad8b1aafSjsg 			 * The chain's ownership is transferred to the
3085ad8b1aafSjsg 			 * timeline.
3086ad8b1aafSjsg 			 */
3087ad8b1aafSjsg 			eb->fences[n].chain_fence = NULL;
3088ad8b1aafSjsg 		} else {
3089c349dbc7Sjsg 			drm_syncobj_replace_fence(syncobj, fence);
3090c349dbc7Sjsg 		}
3091c349dbc7Sjsg 	}
3092ad8b1aafSjsg }
3093ad8b1aafSjsg 
3094ad8b1aafSjsg static int
parse_timeline_fences(struct i915_user_extension __user * ext,void * data)3095ad8b1aafSjsg parse_timeline_fences(struct i915_user_extension __user *ext, void *data)
3096ad8b1aafSjsg {
3097ad8b1aafSjsg 	struct i915_execbuffer *eb = data;
3098ad8b1aafSjsg 	struct drm_i915_gem_execbuffer_ext_timeline_fences timeline_fences;
3099ad8b1aafSjsg 
3100ad8b1aafSjsg 	if (copy_from_user(&timeline_fences, ext, sizeof(timeline_fences)))
3101ad8b1aafSjsg 		return -EFAULT;
3102ad8b1aafSjsg 
3103ad8b1aafSjsg 	return add_timeline_fence_array(eb, &timeline_fences);
3104ad8b1aafSjsg }
3105c349dbc7Sjsg 
retire_requests(struct intel_timeline * tl,struct i915_request * end)3106c349dbc7Sjsg static void retire_requests(struct intel_timeline *tl, struct i915_request *end)
3107c349dbc7Sjsg {
3108c349dbc7Sjsg 	struct i915_request *rq, *rn;
3109c349dbc7Sjsg 
3110c349dbc7Sjsg 	list_for_each_entry_safe(rq, rn, &tl->requests, link)
3111c349dbc7Sjsg 		if (rq == end || !i915_request_retire(rq))
3112c349dbc7Sjsg 			break;
3113c349dbc7Sjsg }
3114c349dbc7Sjsg 
eb_request_add(struct i915_execbuffer * eb,struct i915_request * rq,int err,bool last_parallel)31151bb76ff1Sjsg static int eb_request_add(struct i915_execbuffer *eb, struct i915_request *rq,
31161bb76ff1Sjsg 			  int err, bool last_parallel)
3117c349dbc7Sjsg {
3118c349dbc7Sjsg 	struct intel_timeline * const tl = i915_request_timeline(rq);
3119c349dbc7Sjsg 	struct i915_sched_attr attr = {};
3120c349dbc7Sjsg 	struct i915_request *prev;
3121c349dbc7Sjsg 
3122c349dbc7Sjsg 	lockdep_assert_held(&tl->mutex);
3123c349dbc7Sjsg 	lockdep_unpin_lock(&tl->mutex, rq->cookie);
3124c349dbc7Sjsg 
3125c349dbc7Sjsg 	trace_i915_request_add(rq);
3126c349dbc7Sjsg 
3127c349dbc7Sjsg 	prev = __i915_request_commit(rq);
3128c349dbc7Sjsg 
3129c349dbc7Sjsg 	/* Check that the context wasn't destroyed before submission */
3130c349dbc7Sjsg 	if (likely(!intel_context_is_closed(eb->context))) {
3131c349dbc7Sjsg 		attr = eb->gem_context->sched;
3132c349dbc7Sjsg 	} else {
3133c349dbc7Sjsg 		/* Serialise with context_close via the add_to_timeline */
3134c349dbc7Sjsg 		i915_request_set_error_once(rq, -ENOENT);
3135c349dbc7Sjsg 		__i915_request_skip(rq);
3136ad8b1aafSjsg 		err = -ENOENT; /* override any transient errors */
3137c349dbc7Sjsg 	}
3138c349dbc7Sjsg 
31391bb76ff1Sjsg 	if (intel_context_is_parallel(eb->context)) {
31401bb76ff1Sjsg 		if (err) {
31411bb76ff1Sjsg 			__i915_request_skip(rq);
31421bb76ff1Sjsg 			set_bit(I915_FENCE_FLAG_SKIP_PARALLEL,
31431bb76ff1Sjsg 				&rq->fence.flags);
31441bb76ff1Sjsg 		}
31451bb76ff1Sjsg 		if (last_parallel)
31461bb76ff1Sjsg 			set_bit(I915_FENCE_FLAG_SUBMIT_PARALLEL,
31471bb76ff1Sjsg 				&rq->fence.flags);
31481bb76ff1Sjsg 	}
31491bb76ff1Sjsg 
3150c349dbc7Sjsg 	__i915_request_queue(rq, &attr);
3151c349dbc7Sjsg 
3152c349dbc7Sjsg 	/* Try to clean up the client's timeline after submitting the request */
3153c349dbc7Sjsg 	if (prev)
3154c349dbc7Sjsg 		retire_requests(tl, prev);
3155c349dbc7Sjsg 
3156c349dbc7Sjsg 	mutex_unlock(&tl->mutex);
3157ad8b1aafSjsg 
3158ad8b1aafSjsg 	return err;
3159ad8b1aafSjsg }
3160ad8b1aafSjsg 
eb_requests_add(struct i915_execbuffer * eb,int err)31611bb76ff1Sjsg static int eb_requests_add(struct i915_execbuffer *eb, int err)
31621bb76ff1Sjsg {
31631bb76ff1Sjsg 	int i;
31641bb76ff1Sjsg 
31651bb76ff1Sjsg 	/*
31661bb76ff1Sjsg 	 * We iterate in reverse order of creation to release timeline mutexes in
31671bb76ff1Sjsg 	 * same order.
31681bb76ff1Sjsg 	 */
31691bb76ff1Sjsg 	for_each_batch_add_order(eb, i) {
31701bb76ff1Sjsg 		struct i915_request *rq = eb->requests[i];
31711bb76ff1Sjsg 
31721bb76ff1Sjsg 		if (!rq)
31731bb76ff1Sjsg 			continue;
31741bb76ff1Sjsg 		err |= eb_request_add(eb, rq, err, i == 0);
31751bb76ff1Sjsg 	}
31761bb76ff1Sjsg 
31771bb76ff1Sjsg 	return err;
31781bb76ff1Sjsg }
31791bb76ff1Sjsg 
3180ad8b1aafSjsg static const i915_user_extension_fn execbuf_extensions[] = {
3181ad8b1aafSjsg 	[DRM_I915_GEM_EXECBUFFER_EXT_TIMELINE_FENCES] = parse_timeline_fences,
3182ad8b1aafSjsg };
3183ad8b1aafSjsg 
3184ad8b1aafSjsg static int
parse_execbuf2_extensions(struct drm_i915_gem_execbuffer2 * args,struct i915_execbuffer * eb)3185ad8b1aafSjsg parse_execbuf2_extensions(struct drm_i915_gem_execbuffer2 *args,
3186ad8b1aafSjsg 			  struct i915_execbuffer *eb)
3187ad8b1aafSjsg {
3188ad8b1aafSjsg 	if (!(args->flags & I915_EXEC_USE_EXTENSIONS))
3189ad8b1aafSjsg 		return 0;
3190ad8b1aafSjsg 
3191ad8b1aafSjsg 	/* The execbuf2 extension mechanism reuses cliprects_ptr. So we cannot
3192ad8b1aafSjsg 	 * have another flag also using it at the same time.
3193ad8b1aafSjsg 	 */
3194ad8b1aafSjsg 	if (eb->args->flags & I915_EXEC_FENCE_ARRAY)
3195ad8b1aafSjsg 		return -EINVAL;
3196ad8b1aafSjsg 
3197ad8b1aafSjsg 	if (args->num_cliprects != 0)
3198ad8b1aafSjsg 		return -EINVAL;
3199ad8b1aafSjsg 
3200ad8b1aafSjsg 	return i915_user_extensions(u64_to_user_ptr(args->cliprects_ptr),
3201ad8b1aafSjsg 				    execbuf_extensions,
3202ad8b1aafSjsg 				    ARRAY_SIZE(execbuf_extensions),
3203ad8b1aafSjsg 				    eb);
3204c349dbc7Sjsg }
3205c349dbc7Sjsg 
eb_requests_get(struct i915_execbuffer * eb)32061bb76ff1Sjsg static void eb_requests_get(struct i915_execbuffer *eb)
32071bb76ff1Sjsg {
32081bb76ff1Sjsg 	unsigned int i;
32091bb76ff1Sjsg 
32101bb76ff1Sjsg 	for_each_batch_create_order(eb, i) {
32111bb76ff1Sjsg 		if (!eb->requests[i])
32121bb76ff1Sjsg 			break;
32131bb76ff1Sjsg 
32141bb76ff1Sjsg 		i915_request_get(eb->requests[i]);
32151bb76ff1Sjsg 	}
32161bb76ff1Sjsg }
32171bb76ff1Sjsg 
eb_requests_put(struct i915_execbuffer * eb)32181bb76ff1Sjsg static void eb_requests_put(struct i915_execbuffer *eb)
32191bb76ff1Sjsg {
32201bb76ff1Sjsg 	unsigned int i;
32211bb76ff1Sjsg 
32221bb76ff1Sjsg 	for_each_batch_create_order(eb, i) {
32231bb76ff1Sjsg 		if (!eb->requests[i])
32241bb76ff1Sjsg 			break;
32251bb76ff1Sjsg 
32261bb76ff1Sjsg 		i915_request_put(eb->requests[i]);
32271bb76ff1Sjsg 	}
32281bb76ff1Sjsg }
32291bb76ff1Sjsg 
32301bb76ff1Sjsg static struct sync_file *
eb_composite_fence_create(struct i915_execbuffer * eb,int out_fence_fd)32311bb76ff1Sjsg eb_composite_fence_create(struct i915_execbuffer *eb, int out_fence_fd)
32321bb76ff1Sjsg {
32331bb76ff1Sjsg 	struct sync_file *out_fence = NULL;
32341bb76ff1Sjsg 	struct dma_fence_array *fence_array;
32351bb76ff1Sjsg 	struct dma_fence **fences;
32361bb76ff1Sjsg 	unsigned int i;
32371bb76ff1Sjsg 
32381bb76ff1Sjsg 	GEM_BUG_ON(!intel_context_is_parent(eb->context));
32391bb76ff1Sjsg 
32401bb76ff1Sjsg 	fences = kmalloc_array(eb->num_batches, sizeof(*fences), GFP_KERNEL);
32411bb76ff1Sjsg 	if (!fences)
32421bb76ff1Sjsg 		return ERR_PTR(-ENOMEM);
32431bb76ff1Sjsg 
32441bb76ff1Sjsg 	for_each_batch_create_order(eb, i) {
32451bb76ff1Sjsg 		fences[i] = &eb->requests[i]->fence;
32461bb76ff1Sjsg 		__set_bit(I915_FENCE_FLAG_COMPOSITE,
32471bb76ff1Sjsg 			  &eb->requests[i]->fence.flags);
32481bb76ff1Sjsg 	}
32491bb76ff1Sjsg 
32501bb76ff1Sjsg 	fence_array = dma_fence_array_create(eb->num_batches,
32511bb76ff1Sjsg 					     fences,
32521bb76ff1Sjsg 					     eb->context->parallel.fence_context,
32531bb76ff1Sjsg 					     eb->context->parallel.seqno++,
32541bb76ff1Sjsg 					     false);
32551bb76ff1Sjsg 	if (!fence_array) {
32561bb76ff1Sjsg 		kfree(fences);
32571bb76ff1Sjsg 		return ERR_PTR(-ENOMEM);
32581bb76ff1Sjsg 	}
32591bb76ff1Sjsg 
32601bb76ff1Sjsg 	/* Move ownership to the dma_fence_array created above */
32611bb76ff1Sjsg 	for_each_batch_create_order(eb, i)
32621bb76ff1Sjsg 		dma_fence_get(fences[i]);
32631bb76ff1Sjsg 
32641bb76ff1Sjsg 	if (out_fence_fd != -1) {
32651bb76ff1Sjsg 		out_fence = sync_file_create(&fence_array->base);
32661bb76ff1Sjsg 		/* sync_file now owns fence_arry, drop creation ref */
32671bb76ff1Sjsg 		dma_fence_put(&fence_array->base);
32681bb76ff1Sjsg 		if (!out_fence)
32691bb76ff1Sjsg 			return ERR_PTR(-ENOMEM);
32701bb76ff1Sjsg 	}
32711bb76ff1Sjsg 
32721bb76ff1Sjsg 	eb->composite_fence = &fence_array->base;
32731bb76ff1Sjsg 
32741bb76ff1Sjsg 	return out_fence;
32751bb76ff1Sjsg }
32761bb76ff1Sjsg 
32771bb76ff1Sjsg static struct sync_file *
eb_fences_add(struct i915_execbuffer * eb,struct i915_request * rq,struct dma_fence * in_fence,int out_fence_fd)32781bb76ff1Sjsg eb_fences_add(struct i915_execbuffer *eb, struct i915_request *rq,
32791bb76ff1Sjsg 	      struct dma_fence *in_fence, int out_fence_fd)
32801bb76ff1Sjsg {
32811bb76ff1Sjsg 	struct sync_file *out_fence = NULL;
32821bb76ff1Sjsg 	int err;
32831bb76ff1Sjsg 
32841bb76ff1Sjsg 	if (unlikely(eb->gem_context->syncobj)) {
32851bb76ff1Sjsg 		struct dma_fence *fence;
32861bb76ff1Sjsg 
32871bb76ff1Sjsg 		fence = drm_syncobj_fence_get(eb->gem_context->syncobj);
32881bb76ff1Sjsg 		err = i915_request_await_dma_fence(rq, fence);
32891bb76ff1Sjsg 		dma_fence_put(fence);
32901bb76ff1Sjsg 		if (err)
32911bb76ff1Sjsg 			return ERR_PTR(err);
32921bb76ff1Sjsg 	}
32931bb76ff1Sjsg 
32941bb76ff1Sjsg 	if (in_fence) {
32951bb76ff1Sjsg 		if (eb->args->flags & I915_EXEC_FENCE_SUBMIT)
32961bb76ff1Sjsg 			err = i915_request_await_execution(rq, in_fence);
32971bb76ff1Sjsg 		else
32981bb76ff1Sjsg 			err = i915_request_await_dma_fence(rq, in_fence);
32991bb76ff1Sjsg 		if (err < 0)
33001bb76ff1Sjsg 			return ERR_PTR(err);
33011bb76ff1Sjsg 	}
33021bb76ff1Sjsg 
33031bb76ff1Sjsg 	if (eb->fences) {
33041bb76ff1Sjsg 		err = await_fence_array(eb, rq);
33051bb76ff1Sjsg 		if (err)
33061bb76ff1Sjsg 			return ERR_PTR(err);
33071bb76ff1Sjsg 	}
33081bb76ff1Sjsg 
33091bb76ff1Sjsg 	if (intel_context_is_parallel(eb->context)) {
33101bb76ff1Sjsg 		out_fence = eb_composite_fence_create(eb, out_fence_fd);
33111bb76ff1Sjsg 		if (IS_ERR(out_fence))
33121bb76ff1Sjsg 			return ERR_PTR(-ENOMEM);
33131bb76ff1Sjsg 	} else if (out_fence_fd != -1) {
33141bb76ff1Sjsg 		out_fence = sync_file_create(&rq->fence);
33151bb76ff1Sjsg 		if (!out_fence)
33161bb76ff1Sjsg 			return ERR_PTR(-ENOMEM);
33171bb76ff1Sjsg 	}
33181bb76ff1Sjsg 
33191bb76ff1Sjsg 	return out_fence;
33201bb76ff1Sjsg }
33211bb76ff1Sjsg 
33221bb76ff1Sjsg static struct intel_context *
eb_find_context(struct i915_execbuffer * eb,unsigned int context_number)33231bb76ff1Sjsg eb_find_context(struct i915_execbuffer *eb, unsigned int context_number)
33241bb76ff1Sjsg {
33251bb76ff1Sjsg 	struct intel_context *child;
33261bb76ff1Sjsg 
33271bb76ff1Sjsg 	if (likely(context_number == 0))
33281bb76ff1Sjsg 		return eb->context;
33291bb76ff1Sjsg 
33301bb76ff1Sjsg 	for_each_child(eb->context, child)
33311bb76ff1Sjsg 		if (!--context_number)
33321bb76ff1Sjsg 			return child;
33331bb76ff1Sjsg 
33341bb76ff1Sjsg 	GEM_BUG_ON("Context not found");
33351bb76ff1Sjsg 
33361bb76ff1Sjsg 	return NULL;
33371bb76ff1Sjsg }
33381bb76ff1Sjsg 
33391bb76ff1Sjsg static struct sync_file *
eb_requests_create(struct i915_execbuffer * eb,struct dma_fence * in_fence,int out_fence_fd)33401bb76ff1Sjsg eb_requests_create(struct i915_execbuffer *eb, struct dma_fence *in_fence,
33411bb76ff1Sjsg 		   int out_fence_fd)
33421bb76ff1Sjsg {
33431bb76ff1Sjsg 	struct sync_file *out_fence = NULL;
33441bb76ff1Sjsg 	unsigned int i;
33451bb76ff1Sjsg 
33461bb76ff1Sjsg 	for_each_batch_create_order(eb, i) {
33471bb76ff1Sjsg 		/* Allocate a request for this batch buffer nice and early. */
33481bb76ff1Sjsg 		eb->requests[i] = i915_request_create(eb_find_context(eb, i));
33491bb76ff1Sjsg 		if (IS_ERR(eb->requests[i])) {
33501bb76ff1Sjsg 			out_fence = ERR_CAST(eb->requests[i]);
33511bb76ff1Sjsg 			eb->requests[i] = NULL;
33521bb76ff1Sjsg 			return out_fence;
33531bb76ff1Sjsg 		}
33541bb76ff1Sjsg 
33551bb76ff1Sjsg 		/*
33561bb76ff1Sjsg 		 * Only the first request added (committed to backend) has to
33571bb76ff1Sjsg 		 * take the in fences into account as all subsequent requests
33581bb76ff1Sjsg 		 * will have fences inserted inbetween them.
33591bb76ff1Sjsg 		 */
33601bb76ff1Sjsg 		if (i + 1 == eb->num_batches) {
33611bb76ff1Sjsg 			out_fence = eb_fences_add(eb, eb->requests[i],
33621bb76ff1Sjsg 						  in_fence, out_fence_fd);
33631bb76ff1Sjsg 			if (IS_ERR(out_fence))
33641bb76ff1Sjsg 				return out_fence;
33651bb76ff1Sjsg 		}
33661bb76ff1Sjsg 
33671bb76ff1Sjsg 		/*
33681bb76ff1Sjsg 		 * Not really on stack, but we don't want to call
33691bb76ff1Sjsg 		 * kfree on the batch_snapshot when we put it, so use the
33701bb76ff1Sjsg 		 * _onstack interface.
33711bb76ff1Sjsg 		 */
33721bb76ff1Sjsg 		if (eb->batches[i]->vma)
33731bb76ff1Sjsg 			eb->requests[i]->batch_res =
33741bb76ff1Sjsg 				i915_vma_resource_get(eb->batches[i]->vma->resource);
33751bb76ff1Sjsg 		if (eb->batch_pool) {
33761bb76ff1Sjsg 			GEM_BUG_ON(intel_context_is_parallel(eb->context));
33771bb76ff1Sjsg 			intel_gt_buffer_pool_mark_active(eb->batch_pool,
33781bb76ff1Sjsg 							 eb->requests[i]);
33791bb76ff1Sjsg 		}
33801bb76ff1Sjsg 	}
33811bb76ff1Sjsg 
33821bb76ff1Sjsg 	return out_fence;
33831bb76ff1Sjsg }
33841bb76ff1Sjsg 
3385c349dbc7Sjsg static int
i915_gem_do_execbuffer(struct drm_device * dev,struct drm_file * file,struct drm_i915_gem_execbuffer2 * args,struct drm_i915_gem_exec_object2 * exec)3386c349dbc7Sjsg i915_gem_do_execbuffer(struct drm_device *dev,
3387c349dbc7Sjsg 		       struct drm_file *file,
3388c349dbc7Sjsg 		       struct drm_i915_gem_execbuffer2 *args,
3389ad8b1aafSjsg 		       struct drm_i915_gem_exec_object2 *exec)
3390c349dbc7Sjsg {
3391c349dbc7Sjsg 	struct drm_i915_private *i915 = to_i915(dev);
3392c349dbc7Sjsg 	struct i915_execbuffer eb;
3393c349dbc7Sjsg 	struct dma_fence *in_fence = NULL;
3394c349dbc7Sjsg 	struct sync_file *out_fence = NULL;
3395c349dbc7Sjsg 	int out_fence_fd = -1;
3396c349dbc7Sjsg 	int err;
3397c349dbc7Sjsg 
3398c349dbc7Sjsg 	BUILD_BUG_ON(__EXEC_INTERNAL_FLAGS & ~__I915_EXEC_ILLEGAL_FLAGS);
3399c349dbc7Sjsg 	BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
3400c349dbc7Sjsg 		     ~__EXEC_OBJECT_UNKNOWN_FLAGS);
3401c349dbc7Sjsg 
3402c349dbc7Sjsg 	eb.i915 = i915;
3403c349dbc7Sjsg 	eb.file = file;
3404c349dbc7Sjsg 	eb.args = args;
3405c349dbc7Sjsg 	if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC))
3406c349dbc7Sjsg 		args->flags |= __EXEC_HAS_RELOC;
3407c349dbc7Sjsg 
3408c349dbc7Sjsg 	eb.exec = exec;
3409c349dbc7Sjsg 	eb.vma = (struct eb_vma *)(exec + args->buffer_count + 1);
3410c349dbc7Sjsg 	eb.vma[0].vma = NULL;
34115ca02815Sjsg 	eb.batch_pool = NULL;
3412c349dbc7Sjsg 
3413c349dbc7Sjsg 	eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
3414c349dbc7Sjsg 	reloc_cache_init(&eb.reloc_cache, eb.i915);
3415c349dbc7Sjsg 
3416c349dbc7Sjsg 	eb.buffer_count = args->buffer_count;
3417c349dbc7Sjsg 	eb.batch_start_offset = args->batch_start_offset;
3418c349dbc7Sjsg 	eb.trampoline = NULL;
3419c349dbc7Sjsg 
3420ad8b1aafSjsg 	eb.fences = NULL;
3421ad8b1aafSjsg 	eb.num_fences = 0;
3422ad8b1aafSjsg 
34231bb76ff1Sjsg 	eb_capture_list_clear(&eb);
34241bb76ff1Sjsg 
34251bb76ff1Sjsg 	memset(eb.requests, 0, sizeof(struct i915_request *) *
34261bb76ff1Sjsg 	       ARRAY_SIZE(eb.requests));
34271bb76ff1Sjsg 	eb.composite_fence = NULL;
34281bb76ff1Sjsg 
3429c349dbc7Sjsg 	eb.batch_flags = 0;
3430c349dbc7Sjsg 	if (args->flags & I915_EXEC_SECURE) {
34315ca02815Sjsg 		if (GRAPHICS_VER(i915) >= 11)
3432c349dbc7Sjsg 			return -ENODEV;
3433c349dbc7Sjsg 
3434c349dbc7Sjsg 		/* Return -EPERM to trigger fallback code on old binaries. */
3435c349dbc7Sjsg 		if (!HAS_SECURE_BATCHES(i915))
3436c349dbc7Sjsg 			return -EPERM;
3437c349dbc7Sjsg 
3438c349dbc7Sjsg 		if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
3439c349dbc7Sjsg 			return -EPERM;
3440c349dbc7Sjsg 
3441c349dbc7Sjsg 		eb.batch_flags |= I915_DISPATCH_SECURE;
3442c349dbc7Sjsg 	}
3443c349dbc7Sjsg 	if (args->flags & I915_EXEC_IS_PINNED)
3444c349dbc7Sjsg 		eb.batch_flags |= I915_DISPATCH_PINNED;
3445c349dbc7Sjsg 
3446ad8b1aafSjsg 	err = parse_execbuf2_extensions(args, &eb);
3447ad8b1aafSjsg 	if (err)
3448ad8b1aafSjsg 		goto err_ext;
3449ad8b1aafSjsg 
3450ad8b1aafSjsg 	err = add_fence_array(&eb);
3451ad8b1aafSjsg 	if (err)
3452ad8b1aafSjsg 		goto err_ext;
3453ad8b1aafSjsg 
3454ad8b1aafSjsg #define IN_FENCES (I915_EXEC_FENCE_IN | I915_EXEC_FENCE_SUBMIT)
3455ad8b1aafSjsg 	if (args->flags & IN_FENCES) {
3456ad8b1aafSjsg 		if ((args->flags & IN_FENCES) == IN_FENCES)
3457c349dbc7Sjsg 			return -EINVAL;
3458c349dbc7Sjsg 
3459ad8b1aafSjsg 		in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
3460ad8b1aafSjsg 		if (!in_fence) {
3461c349dbc7Sjsg 			err = -EINVAL;
3462ad8b1aafSjsg 			goto err_ext;
3463c349dbc7Sjsg 		}
3464c349dbc7Sjsg 	}
3465ad8b1aafSjsg #undef IN_FENCES
3466c349dbc7Sjsg 
3467c349dbc7Sjsg 	if (args->flags & I915_EXEC_FENCE_OUT) {
3468c349dbc7Sjsg 		out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
3469c349dbc7Sjsg 		if (out_fence_fd < 0) {
3470c349dbc7Sjsg 			err = out_fence_fd;
3471ad8b1aafSjsg 			goto err_in_fence;
3472c349dbc7Sjsg 		}
3473c349dbc7Sjsg 	}
3474c349dbc7Sjsg 
3475c349dbc7Sjsg 	err = eb_create(&eb);
3476c349dbc7Sjsg 	if (err)
3477c349dbc7Sjsg 		goto err_out_fence;
3478c349dbc7Sjsg 
3479c349dbc7Sjsg 	GEM_BUG_ON(!eb.lut_size);
3480c349dbc7Sjsg 
3481c349dbc7Sjsg 	err = eb_select_context(&eb);
3482c349dbc7Sjsg 	if (unlikely(err))
3483c349dbc7Sjsg 		goto err_destroy;
3484c349dbc7Sjsg 
3485ad8b1aafSjsg 	err = eb_select_engine(&eb);
3486c349dbc7Sjsg 	if (unlikely(err))
3487c349dbc7Sjsg 		goto err_context;
3488c349dbc7Sjsg 
3489ad8b1aafSjsg 	err = eb_lookup_vmas(&eb);
3490ad8b1aafSjsg 	if (err) {
3491ad8b1aafSjsg 		eb_release_vmas(&eb, true);
3492ad8b1aafSjsg 		goto err_engine;
3493ad8b1aafSjsg 	}
3494ad8b1aafSjsg 
3495ad8b1aafSjsg 	i915_gem_ww_ctx_init(&eb.ww, true);
3496ad8b1aafSjsg 
3497ad8b1aafSjsg 	err = eb_relocate_parse(&eb);
3498c349dbc7Sjsg 	if (err) {
3499c349dbc7Sjsg 		/*
3500c349dbc7Sjsg 		 * If the user expects the execobject.offset and
3501c349dbc7Sjsg 		 * reloc.presumed_offset to be an exact match,
3502c349dbc7Sjsg 		 * as for using NO_RELOC, then we cannot update
3503c349dbc7Sjsg 		 * the execobject.offset until we have completed
3504c349dbc7Sjsg 		 * relocation.
3505c349dbc7Sjsg 		 */
3506c349dbc7Sjsg 		args->flags &= ~__EXEC_HAS_RELOC;
3507c349dbc7Sjsg 		goto err_vma;
3508c349dbc7Sjsg 	}
3509c349dbc7Sjsg 
3510ad8b1aafSjsg 	ww_acquire_done(&eb.ww.ctx);
35111bb76ff1Sjsg 	err = eb_capture_stage(&eb);
35121bb76ff1Sjsg 	if (err)
35131bb76ff1Sjsg 		goto err_vma;
3514c349dbc7Sjsg 
35151bb76ff1Sjsg 	out_fence = eb_requests_create(&eb, in_fence, out_fence_fd);
35161bb76ff1Sjsg 	if (IS_ERR(out_fence)) {
35171bb76ff1Sjsg 		err = PTR_ERR(out_fence);
35181bb76ff1Sjsg 		out_fence = NULL;
35191bb76ff1Sjsg 		if (eb.requests[0])
35201bb76ff1Sjsg 			goto err_request;
35211bb76ff1Sjsg 		else
3522ad8b1aafSjsg 			goto err_vma;
3523c349dbc7Sjsg 	}
3524c349dbc7Sjsg 
35251bb76ff1Sjsg 	err = eb_submit(&eb);
35265ca02815Sjsg 
3527c349dbc7Sjsg err_request:
35281bb76ff1Sjsg 	eb_requests_get(&eb);
35291bb76ff1Sjsg 	err = eb_requests_add(&eb, err);
3530c349dbc7Sjsg 
3531ad8b1aafSjsg 	if (eb.fences)
35321bb76ff1Sjsg 		signal_fence_array(&eb, eb.composite_fence ?
35331bb76ff1Sjsg 				   eb.composite_fence :
35341bb76ff1Sjsg 				   &eb.requests[0]->fence);
3535c349dbc7Sjsg 
35360acc5a26Sjsg 	if (unlikely(eb.gem_context->syncobj)) {
35370acc5a26Sjsg 		drm_syncobj_replace_fence(eb.gem_context->syncobj,
35380acc5a26Sjsg 					  eb.composite_fence ?
35390acc5a26Sjsg 					  eb.composite_fence :
35400acc5a26Sjsg 					  &eb.requests[0]->fence);
35410acc5a26Sjsg 	}
35420acc5a26Sjsg 
3543c349dbc7Sjsg 	if (out_fence) {
3544c349dbc7Sjsg 		if (err == 0) {
3545c349dbc7Sjsg 			fd_install(out_fence_fd, out_fence->file);
3546c349dbc7Sjsg 			args->rsvd2 &= GENMASK_ULL(31, 0); /* keep in-fence */
3547c349dbc7Sjsg 			args->rsvd2 |= (u64)out_fence_fd << 32;
3548c349dbc7Sjsg 			out_fence_fd = -1;
3549c349dbc7Sjsg 		} else {
3550c349dbc7Sjsg 			fput(out_fence->file);
3551c349dbc7Sjsg 		}
3552c349dbc7Sjsg 	}
35535ca02815Sjsg 
35541bb76ff1Sjsg 	if (!out_fence && eb.composite_fence)
35551bb76ff1Sjsg 		dma_fence_put(eb.composite_fence);
35561bb76ff1Sjsg 
35571bb76ff1Sjsg 	eb_requests_put(&eb);
3558c349dbc7Sjsg 
3559c349dbc7Sjsg err_vma:
3560ad8b1aafSjsg 	eb_release_vmas(&eb, true);
3561ad8b1aafSjsg 	WARN_ON(err == -EDEADLK);
3562ad8b1aafSjsg 	i915_gem_ww_ctx_fini(&eb.ww);
3563ad8b1aafSjsg 
3564ad8b1aafSjsg 	if (eb.batch_pool)
3565ad8b1aafSjsg 		intel_gt_buffer_pool_put(eb.batch_pool);
3566ad8b1aafSjsg err_engine:
3567ad8b1aafSjsg 	eb_put_engine(&eb);
3568c349dbc7Sjsg err_context:
3569c349dbc7Sjsg 	i915_gem_context_put(eb.gem_context);
3570c349dbc7Sjsg err_destroy:
3571c349dbc7Sjsg 	eb_destroy(&eb);
3572c349dbc7Sjsg err_out_fence:
3573c349dbc7Sjsg 	if (out_fence_fd != -1)
3574c349dbc7Sjsg 		put_unused_fd(out_fence_fd);
3575c349dbc7Sjsg err_in_fence:
3576c349dbc7Sjsg 	dma_fence_put(in_fence);
3577ad8b1aafSjsg err_ext:
3578ad8b1aafSjsg 	put_fence_array(eb.fences, eb.num_fences);
3579c349dbc7Sjsg 	return err;
3580c349dbc7Sjsg }
3581c349dbc7Sjsg 
eb_element_size(void)3582c349dbc7Sjsg static size_t eb_element_size(void)
3583c349dbc7Sjsg {
3584c349dbc7Sjsg 	return sizeof(struct drm_i915_gem_exec_object2) + sizeof(struct eb_vma);
3585c349dbc7Sjsg }
3586c349dbc7Sjsg 
check_buffer_count(size_t count)3587c349dbc7Sjsg static bool check_buffer_count(size_t count)
3588c349dbc7Sjsg {
3589c349dbc7Sjsg 	const size_t sz = eb_element_size();
3590c349dbc7Sjsg 
3591c349dbc7Sjsg 	/*
3592c349dbc7Sjsg 	 * When using LUT_HANDLE, we impose a limit of INT_MAX for the lookup
3593c349dbc7Sjsg 	 * array size (see eb_create()). Otherwise, we can accept an array as
3594c349dbc7Sjsg 	 * large as can be addressed (though use large arrays at your peril)!
3595c349dbc7Sjsg 	 */
3596c349dbc7Sjsg 
3597c349dbc7Sjsg 	return !(count < 1 || count > INT_MAX || count > SIZE_MAX / sz - 1);
3598c349dbc7Sjsg }
3599c349dbc7Sjsg 
3600c349dbc7Sjsg int
i915_gem_execbuffer2_ioctl(struct drm_device * dev,void * data,struct drm_file * file)3601c349dbc7Sjsg i915_gem_execbuffer2_ioctl(struct drm_device *dev, void *data,
3602c349dbc7Sjsg 			   struct drm_file *file)
3603c349dbc7Sjsg {
3604c349dbc7Sjsg 	struct drm_i915_private *i915 = to_i915(dev);
3605c349dbc7Sjsg 	struct drm_i915_gem_execbuffer2 *args = data;
3606c349dbc7Sjsg 	struct drm_i915_gem_exec_object2 *exec2_list;
3607c349dbc7Sjsg 	const size_t count = args->buffer_count;
3608c349dbc7Sjsg 	int err;
3609c349dbc7Sjsg 
3610c349dbc7Sjsg 	if (!check_buffer_count(count)) {
3611c349dbc7Sjsg 		drm_dbg(&i915->drm, "execbuf2 with %zd buffers\n", count);
3612c349dbc7Sjsg 		return -EINVAL;
3613c349dbc7Sjsg 	}
3614c349dbc7Sjsg 
3615*f005ef32Sjsg 	err = i915_gem_check_execbuffer(i915, args);
3616c349dbc7Sjsg 	if (err)
3617c349dbc7Sjsg 		return err;
3618c349dbc7Sjsg 
3619ad8b1aafSjsg 	/* Allocate extra slots for use by the command parser */
3620ad8b1aafSjsg 	exec2_list = kvmalloc_array(count + 2, eb_element_size(),
3621c349dbc7Sjsg 				    __GFP_NOWARN | GFP_KERNEL);
3622c349dbc7Sjsg 	if (exec2_list == NULL) {
3623c349dbc7Sjsg 		drm_dbg(&i915->drm, "Failed to allocate exec list for %zd buffers\n",
3624c349dbc7Sjsg 			count);
3625c349dbc7Sjsg 		return -ENOMEM;
3626c349dbc7Sjsg 	}
3627c349dbc7Sjsg 	if (copy_from_user(exec2_list,
3628c349dbc7Sjsg 			   u64_to_user_ptr(args->buffers_ptr),
3629c349dbc7Sjsg 			   sizeof(*exec2_list) * count)) {
3630c349dbc7Sjsg 		drm_dbg(&i915->drm, "copy %zd exec entries failed\n", count);
3631c349dbc7Sjsg 		kvfree(exec2_list);
3632c349dbc7Sjsg 		return -EFAULT;
3633c349dbc7Sjsg 	}
3634c349dbc7Sjsg 
3635ad8b1aafSjsg 	err = i915_gem_do_execbuffer(dev, file, args, exec2_list);
3636c349dbc7Sjsg 
3637c349dbc7Sjsg 	/*
3638c349dbc7Sjsg 	 * Now that we have begun execution of the batchbuffer, we ignore
3639c349dbc7Sjsg 	 * any new error after this point. Also given that we have already
3640c349dbc7Sjsg 	 * updated the associated relocations, we try to write out the current
3641c349dbc7Sjsg 	 * object locations irrespective of any error.
3642c349dbc7Sjsg 	 */
3643c349dbc7Sjsg 	if (args->flags & __EXEC_HAS_RELOC) {
3644c349dbc7Sjsg 		struct drm_i915_gem_exec_object2 __user *user_exec_list =
3645c349dbc7Sjsg 			u64_to_user_ptr(args->buffers_ptr);
3646c349dbc7Sjsg 		unsigned int i;
3647c349dbc7Sjsg 
3648c349dbc7Sjsg 		/* Copy the new buffer offsets back to the user's exec list. */
3649c349dbc7Sjsg 		/*
3650c349dbc7Sjsg 		 * Note: count * sizeof(*user_exec_list) does not overflow,
3651c349dbc7Sjsg 		 * because we checked 'count' in check_buffer_count().
3652c349dbc7Sjsg 		 *
3653c349dbc7Sjsg 		 * And this range already got effectively checked earlier
3654c349dbc7Sjsg 		 * when we did the "copy_from_user()" above.
3655c349dbc7Sjsg 		 */
3656ad8b1aafSjsg 		if (!user_write_access_begin(user_exec_list,
3657ad8b1aafSjsg 					     count * sizeof(*user_exec_list)))
3658c349dbc7Sjsg 			goto end;
3659c349dbc7Sjsg 
3660c349dbc7Sjsg 		for (i = 0; i < args->buffer_count; i++) {
3661c349dbc7Sjsg 			if (!(exec2_list[i].offset & UPDATE))
3662c349dbc7Sjsg 				continue;
3663c349dbc7Sjsg 
3664c349dbc7Sjsg 			exec2_list[i].offset =
3665c349dbc7Sjsg 				gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
3666c349dbc7Sjsg 			unsafe_put_user(exec2_list[i].offset,
3667c349dbc7Sjsg 					&user_exec_list[i].offset,
3668c349dbc7Sjsg 					end_user);
3669c349dbc7Sjsg 		}
3670c349dbc7Sjsg end_user:
3671ad8b1aafSjsg 		user_write_access_end();
3672c349dbc7Sjsg end:;
3673c349dbc7Sjsg 	}
3674c349dbc7Sjsg 
3675c349dbc7Sjsg 	args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
3676c349dbc7Sjsg 	kvfree(exec2_list);
3677c349dbc7Sjsg 	return err;
3678c349dbc7Sjsg }
3679