Commit | Line | Data |
---|---|---|
54cf91dc | 1 | /* |
10be98a7 | 2 | * SPDX-License-Identifier: MIT |
54cf91dc | 3 | * |
10be98a7 | 4 | * Copyright © 2008,2010 Intel Corporation |
54cf91dc CW |
5 | */ |
6 | ||
daedaa33 | 7 | #include <linux/intel-iommu.h> |
52791eee | 8 | #include <linux/dma-resv.h> |
fec0445c | 9 | #include <linux/sync_file.h> |
ad778f89 CW |
10 | #include <linux/uaccess.h> |
11 | ||
cf6e7bac | 12 | #include <drm/drm_syncobj.h> |
ad778f89 | 13 | |
df0566a6 JN |
14 | #include "display/intel_frontbuffer.h" |
15 | ||
afa13085 | 16 | #include "gem/i915_gem_ioctls.h" |
10be98a7 | 17 | #include "gt/intel_context.h" |
45233ab2 | 18 | #include "gt/intel_gpu_commands.h" |
baea429d | 19 | #include "gt/intel_gt.h" |
16e87459 | 20 | #include "gt/intel_gt_buffer_pool.h" |
8f2a1057 | 21 | #include "gt/intel_gt_pm.h" |
2871ea85 | 22 | #include "gt/intel_ring.h" |
8f2a1057 | 23 | |
d3ac8d42 DCS |
24 | #include "pxp/intel_pxp.h" |
25 | ||
6da4a2c4 | 26 | #include "i915_drv.h" |
57822dc6 | 27 | #include "i915_gem_clflush.h" |
10be98a7 | 28 | #include "i915_gem_context.h" |
6da4a2c4 | 29 | #include "i915_gem_ioctls.h" |
54cf91dc | 30 | #include "i915_trace.h" |
cda9edd0 | 31 | #include "i915_user_extensions.h" |
54cf91dc | 32 | |
7d6236bb CW |
33 | struct eb_vma { |
34 | struct i915_vma *vma; | |
35 | unsigned int flags; | |
36 | ||
37 | /** This vma's place in the execbuf reservation list */ | |
38 | struct drm_i915_gem_exec_object2 *exec; | |
39 | struct list_head bind_link; | |
40 | struct list_head reloc_link; | |
41 | ||
42 | struct hlist_node node; | |
43 | u32 handle; | |
44 | }; | |
45 | ||
ad5d95e4 DA |
46 | enum { |
47 | FORCE_CPU_RELOC = 1, | |
48 | FORCE_GTT_RELOC, | |
49 | FORCE_GPU_RELOC, | |
50 | #define DBG_FORCE_RELOC 0 /* choose one of the above! */ | |
51 | }; | |
52 | ||
bfaae47d ML |
53 | /* __EXEC_OBJECT_NO_RESERVE is BIT(31), defined in i915_vma.h */ |
54 | #define __EXEC_OBJECT_HAS_PIN BIT(30) | |
55 | #define __EXEC_OBJECT_HAS_FENCE BIT(29) | |
ed29c269 ML |
56 | #define __EXEC_OBJECT_USERPTR_INIT BIT(28) |
57 | #define __EXEC_OBJECT_NEEDS_MAP BIT(27) | |
58 | #define __EXEC_OBJECT_NEEDS_BIAS BIT(26) | |
59 | #define __EXEC_OBJECT_INTERNAL_FLAGS (~0u << 26) /* all of the above + */ | |
8ae275c2 | 60 | #define __EXEC_OBJECT_RESERVED (__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_FENCE) |
2889caa9 CW |
61 | |
62 | #define __EXEC_HAS_RELOC BIT(31) | |
2bf541ff | 63 | #define __EXEC_ENGINE_PINNED BIT(30) |
ed29c269 ML |
64 | #define __EXEC_USERPTR_USED BIT(29) |
65 | #define __EXEC_INTERNAL_FLAGS (~0u << 29) | |
2889caa9 | 66 | #define UPDATE PIN_OFFSET_FIXED |
d23db88c CW |
67 | |
68 | #define BATCH_OFFSET_BIAS (256*1024) | |
a415d355 | 69 | |
650bc635 | 70 | #define __I915_EXEC_ILLEGAL_FLAGS \ |
08e3e21a LDM |
71 | (__I915_EXEC_UNKNOWN_FLAGS | \ |
72 | I915_EXEC_CONSTANTS_MASK | \ | |
73 | I915_EXEC_RESOURCE_STREAMER) | |
5b043f4e | 74 | |
d20ac620 CW |
75 | /* Catch emission of unexpected errors for CI! */ |
76 | #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM) | |
77 | #undef EINVAL | |
78 | #define EINVAL ({ \ | |
79 | DRM_DEBUG_DRIVER("EINVAL at %s:%d\n", __func__, __LINE__); \ | |
80 | 22; \ | |
81 | }) | |
82 | #endif | |
83 | ||
2889caa9 CW |
84 | /** |
85 | * DOC: User command execution | |
86 | * | |
87 | * Userspace submits commands to be executed on the GPU as an instruction | |
88 | * stream within a GEM object we call a batchbuffer. This instructions may | |
89 | * refer to other GEM objects containing auxiliary state such as kernels, | |
90 | * samplers, render targets and even secondary batchbuffers. Userspace does | |
91 | * not know where in the GPU memory these objects reside and so before the | |
92 | * batchbuffer is passed to the GPU for execution, those addresses in the | |
93 | * batchbuffer and auxiliary objects are updated. This is known as relocation, | |
94 | * or patching. To try and avoid having to relocate each object on the next | |
95 | * execution, userspace is told the location of those objects in this pass, | |
96 | * but this remains just a hint as the kernel may choose a new location for | |
97 | * any object in the future. | |
98 | * | |
99d7e4ee KR |
99 | * At the level of talking to the hardware, submitting a batchbuffer for the |
100 | * GPU to execute is to add content to a buffer from which the HW | |
101 | * command streamer is reading. | |
102 | * | |
103 | * 1. Add a command to load the HW context. For Logical Ring Contexts, i.e. | |
104 | * Execlists, this command is not placed on the same buffer as the | |
105 | * remaining items. | |
106 | * | |
107 | * 2. Add a command to invalidate caches to the buffer. | |
108 | * | |
109 | * 3. Add a batchbuffer start command to the buffer; the start command is | |
110 | * essentially a token together with the GPU address of the batchbuffer | |
111 | * to be executed. | |
112 | * | |
113 | * 4. Add a pipeline flush to the buffer. | |
114 | * | |
115 | * 5. Add a memory write command to the buffer to record when the GPU | |
116 | * is done executing the batchbuffer. The memory write writes the | |
117 | * global sequence number of the request, ``i915_request::global_seqno``; | |
118 | * the i915 driver uses the current value in the register to determine | |
119 | * if the GPU has completed the batchbuffer. | |
120 | * | |
121 | * 6. Add a user interrupt command to the buffer. This command instructs | |
122 | * the GPU to issue an interrupt when the command, pipeline flush and | |
123 | * memory write are completed. | |
124 | * | |
125 | * 7. Inform the hardware of the additional commands added to the buffer | |
126 | * (by updating the tail pointer). | |
127 | * | |
2889caa9 CW |
128 | * Processing an execbuf ioctl is conceptually split up into a few phases. |
129 | * | |
130 | * 1. Validation - Ensure all the pointers, handles and flags are valid. | |
131 | * 2. Reservation - Assign GPU address space for every object | |
132 | * 3. Relocation - Update any addresses to point to the final locations | |
133 | * 4. Serialisation - Order the request with respect to its dependencies | |
134 | * 5. Construction - Construct a request to execute the batchbuffer | |
135 | * 6. Submission (at some point in the future execution) | |
136 | * | |
137 | * Reserving resources for the execbuf is the most complicated phase. We | |
138 | * neither want to have to migrate the object in the address space, nor do | |
139 | * we want to have to update any relocations pointing to this object. Ideally, | |
140 | * we want to leave the object where it is and for all the existing relocations | |
141 | * to match. If the object is given a new address, or if userspace thinks the | |
142 | * object is elsewhere, we have to parse all the relocation entries and update | |
143 | * the addresses. Userspace can set the I915_EXEC_NORELOC flag to hint that | |
144 | * all the target addresses in all of its objects match the value in the | |
145 | * relocation entries and that they all match the presumed offsets given by the | |
146 | * list of execbuffer objects. Using this knowledge, we know that if we haven't | |
147 | * moved any buffers, all the relocation entries are valid and we can skip | |
148 | * the update. (If userspace is wrong, the likely outcome is an impromptu GPU | |
149 | * hang.) The requirement for using I915_EXEC_NO_RELOC are: | |
150 | * | |
151 | * The addresses written in the objects must match the corresponding | |
152 | * reloc.presumed_offset which in turn must match the corresponding | |
153 | * execobject.offset. | |
154 | * | |
155 | * Any render targets written to in the batch must be flagged with | |
156 | * EXEC_OBJECT_WRITE. | |
157 | * | |
158 | * To avoid stalling, execobject.offset should match the current | |
159 | * address of that object within the active context. | |
160 | * | |
161 | * The reservation is done is multiple phases. First we try and keep any | |
162 | * object already bound in its current location - so as long as meets the | |
163 | * constraints imposed by the new execbuffer. Any object left unbound after the | |
164 | * first pass is then fitted into any available idle space. If an object does | |
165 | * not fit, all objects are removed from the reservation and the process rerun | |
166 | * after sorting the objects into a priority order (more difficult to fit | |
167 | * objects are tried first). Failing that, the entire VM is cleared and we try | |
168 | * to fit the execbuf once last time before concluding that it simply will not | |
169 | * fit. | |
170 | * | |
171 | * A small complication to all of this is that we allow userspace not only to | |
172 | * specify an alignment and a size for the object in the address space, but | |
173 | * we also allow userspace to specify the exact offset. This objects are | |
174 | * simpler to place (the location is known a priori) all we have to do is make | |
175 | * sure the space is available. | |
176 | * | |
177 | * Once all the objects are in place, patching up the buried pointers to point | |
178 | * to the final locations is a fairly simple job of walking over the relocation | |
179 | * entry arrays, looking up the right address and rewriting the value into | |
180 | * the object. Simple! ... The relocation entries are stored in user memory | |
181 | * and so to access them we have to copy them into a local buffer. That copy | |
182 | * has to avoid taking any pagefaults as they may lead back to a GEM object | |
183 | * requiring the struct_mutex (i.e. recursive deadlock). So once again we split | |
184 | * the relocation into multiple passes. First we try to do everything within an | |
185 | * atomic context (avoid the pagefaults) which requires that we never wait. If | |
186 | * we detect that we may wait, or if we need to fault, then we have to fallback | |
187 | * to a slower path. The slowpath has to drop the mutex. (Can you hear alarm | |
188 | * bells yet?) Dropping the mutex means that we lose all the state we have | |
189 | * built up so far for the execbuf and we must reset any global data. However, | |
190 | * we do leave the objects pinned in their final locations - which is a | |
191 | * potential issue for concurrent execbufs. Once we have left the mutex, we can | |
192 | * allocate and copy all the relocation entries into a large array at our | |
193 | * leisure, reacquire the mutex, reclaim all the objects and other state and | |
194 | * then proceed to update any incorrect addresses with the objects. | |
195 | * | |
196 | * As we process the relocation entries, we maintain a record of whether the | |
197 | * object is being written to. Using NORELOC, we expect userspace to provide | |
198 | * this information instead. We also check whether we can skip the relocation | |
199 | * by comparing the expected value inside the relocation entry with the target's | |
200 | * final address. If they differ, we have to map the current object and rewrite | |
201 | * the 4 or 8 byte pointer within. | |
202 | * | |
203 | * Serialising an execbuf is quite simple according to the rules of the GEM | |
204 | * ABI. Execution within each context is ordered by the order of submission. | |
205 | * Writes to any GEM object are in order of submission and are exclusive. Reads | |
206 | * from a GEM object are unordered with respect to other reads, but ordered by | |
207 | * writes. A write submitted after a read cannot occur before the read, and | |
208 | * similarly any read submitted after a write cannot occur before the write. | |
209 | * Writes are ordered between engines such that only one write occurs at any | |
210 | * time (completing any reads beforehand) - using semaphores where available | |
211 | * and CPU serialisation otherwise. Other GEM access obey the same rules, any | |
212 | * write (either via mmaps using set-domain, or via pwrite) must flush all GPU | |
213 | * reads before starting, and any read (either using set-domain or pread) must | |
214 | * flush all GPU writes before starting. (Note we only employ a barrier before, | |
215 | * we currently rely on userspace not concurrently starting a new execution | |
216 | * whilst reading or writing to an object. This may be an advantage or not | |
217 | * depending on how much you trust userspace not to shoot themselves in the | |
218 | * foot.) Serialisation may just result in the request being inserted into | |
219 | * a DAG awaiting its turn, but most simple is to wait on the CPU until | |
220 | * all dependencies are resolved. | |
221 | * | |
222 | * After all of that, is just a matter of closing the request and handing it to | |
223 | * the hardware (well, leaving it in a queue to be executed). However, we also | |
224 | * offer the ability for batchbuffers to be run with elevated privileges so | |
225 | * that they access otherwise hidden registers. (Used to adjust L3 cache etc.) | |
226 | * Before any batch is given extra privileges we first must check that it | |
227 | * contains no nefarious instructions, we check that each instruction is from | |
228 | * our whitelist and all registers are also from an allowed list. We first | |
229 | * copy the user's batchbuffer to a shadow (so that the user doesn't have | |
230 | * access to it, either by the CPU or GPU as we scan it) and then parse each | |
231 | * instruction. If everything is ok, we set a flag telling the hardware to run | |
232 | * the batchbuffer in trusted mode, otherwise the ioctl is rejected. | |
233 | */ | |
234 | ||
13149e8b LL |
235 | struct eb_fence { |
236 | struct drm_syncobj *syncobj; /* Use with ptr_mask_bits() */ | |
237 | struct dma_fence *dma_fence; | |
238 | u64 value; | |
239 | struct dma_fence_chain *chain_fence; | |
240 | }; | |
241 | ||
650bc635 | 242 | struct i915_execbuffer { |
2889caa9 CW |
243 | struct drm_i915_private *i915; /** i915 backpointer */ |
244 | struct drm_file *file; /** per-file lookup tables and limits */ | |
245 | struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */ | |
246 | struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */ | |
7d6236bb | 247 | struct eb_vma *vma; |
2889caa9 | 248 | |
544460c3 | 249 | struct intel_gt *gt; /* gt for the execbuf */ |
8f2a1057 CW |
250 | struct intel_context *context; /* logical state for the request */ |
251 | struct i915_gem_context *gem_context; /** caller's context */ | |
2889caa9 | 252 | |
544460c3 MB |
253 | /** our requests to build */ |
254 | struct i915_request *requests[MAX_ENGINE_INSTANCE + 1]; | |
255 | /** identity of the batch obj/vma */ | |
256 | struct eb_vma *batches[MAX_ENGINE_INSTANCE + 1]; | |
32d94048 | 257 | struct i915_vma *trampoline; /** trampoline used for chaining */ |
2889caa9 | 258 | |
544460c3 MB |
259 | /** used for excl fence in dma_resv objects when > 1 BB submitted */ |
260 | struct dma_fence *composite_fence; | |
261 | ||
2889caa9 CW |
262 | /** actual size of execobj[] as we may extend it for the cmdparser */ |
263 | unsigned int buffer_count; | |
264 | ||
544460c3 MB |
265 | /* number of batches in execbuf IOCTL */ |
266 | unsigned int num_batches; | |
267 | ||
2889caa9 CW |
268 | /** list of vma not yet bound during reservation phase */ |
269 | struct list_head unbound; | |
270 | ||
271 | /** list of vma that have execobj.relocation_count */ | |
272 | struct list_head relocs; | |
273 | ||
c43ce123 ML |
274 | struct i915_gem_ww_ctx ww; |
275 | ||
2889caa9 CW |
276 | /** |
277 | * Track the most recently used object for relocations, as we | |
278 | * frequently have to perform multiple relocations within the same | |
279 | * obj/page | |
280 | */ | |
650bc635 | 281 | struct reloc_cache { |
2889caa9 | 282 | struct drm_mm_node node; /** temporary GTT binding */ |
ad5d95e4 DA |
283 | unsigned long vaddr; /** Current kmap address */ |
284 | unsigned long page; /** Currently mapped page index */ | |
8802190f | 285 | unsigned int graphics_ver; /** Cached value of GRAPHICS_VER */ |
650bc635 | 286 | bool use_64bit_reloc : 1; |
2889caa9 CW |
287 | bool has_llc : 1; |
288 | bool has_fence : 1; | |
289 | bool needs_unfenced : 1; | |
650bc635 | 290 | } reloc_cache; |
2889caa9 CW |
291 | |
292 | u64 invalid_flags; /** Set of execobj.flags that are invalid */ | |
2889caa9 | 293 | |
544460c3 MB |
294 | /** Length of batch within object */ |
295 | u64 batch_len[MAX_ENGINE_INSTANCE + 1]; | |
2889caa9 | 296 | u32 batch_start_offset; /** Location within object of batch */ |
2889caa9 | 297 | u32 batch_flags; /** Flags composed for emit_bb_start() */ |
c43ce123 | 298 | struct intel_gt_buffer_pool_node *batch_pool; /** pool node for batch buffer */ |
2889caa9 CW |
299 | |
300 | /** | |
301 | * Indicate either the size of the hastable used to resolve | |
302 | * relocation handles, or if negative that we are using a direct | |
303 | * index into the execobj[]. | |
304 | */ | |
305 | int lut_size; | |
306 | struct hlist_head *buckets; /** ht for relocation handles */ | |
cda9edd0 | 307 | |
13149e8b LL |
308 | struct eb_fence *fences; |
309 | unsigned long num_fences; | |
67731b87 CW |
310 | }; |
311 | ||
8e4ba491 | 312 | static int eb_parse(struct i915_execbuffer *eb); |
544460c3 | 313 | static int eb_pin_engine(struct i915_execbuffer *eb, bool throttle); |
2bf541ff | 314 | static void eb_unpin_engine(struct i915_execbuffer *eb); |
8e4ba491 | 315 | |
3dbf26ed CW |
316 | static inline bool eb_use_cmdparser(const struct i915_execbuffer *eb) |
317 | { | |
544460c3 MB |
318 | return intel_engine_requires_cmd_parser(eb->context->engine) || |
319 | (intel_engine_using_cmd_parser(eb->context->engine) && | |
435e8fc0 | 320 | eb->args->batch_len); |
3dbf26ed CW |
321 | } |
322 | ||
650bc635 | 323 | static int eb_create(struct i915_execbuffer *eb) |
67731b87 | 324 | { |
2889caa9 CW |
325 | if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) { |
326 | unsigned int size = 1 + ilog2(eb->buffer_count); | |
4ff4b44c | 327 | |
2889caa9 CW |
328 | /* |
329 | * Without a 1:1 association between relocation handles and | |
330 | * the execobject[] index, we instead create a hashtable. | |
331 | * We size it dynamically based on available memory, starting | |
332 | * first with 1:1 assocative hash and scaling back until | |
333 | * the allocation succeeds. | |
334 | * | |
335 | * Later on we use a positive lut_size to indicate we are | |
336 | * using this hashtable, and a negative value to indicate a | |
337 | * direct lookup. | |
338 | */ | |
4ff4b44c | 339 | do { |
0d95c883 | 340 | gfp_t flags; |
4d470f73 CW |
341 | |
342 | /* While we can still reduce the allocation size, don't | |
343 | * raise a warning and allow the allocation to fail. | |
344 | * On the last pass though, we want to try as hard | |
345 | * as possible to perform the allocation and warn | |
346 | * if it fails. | |
347 | */ | |
0ee931c4 | 348 | flags = GFP_KERNEL; |
4d470f73 CW |
349 | if (size > 1) |
350 | flags |= __GFP_NORETRY | __GFP_NOWARN; | |
351 | ||
4ff4b44c | 352 | eb->buckets = kzalloc(sizeof(struct hlist_head) << size, |
4d470f73 | 353 | flags); |
4ff4b44c CW |
354 | if (eb->buckets) |
355 | break; | |
356 | } while (--size); | |
357 | ||
8ae275c2 | 358 | if (unlikely(!size)) |
4d470f73 | 359 | return -ENOMEM; |
eef90ccb | 360 | |
2889caa9 | 361 | eb->lut_size = size; |
650bc635 | 362 | } else { |
2889caa9 | 363 | eb->lut_size = -eb->buffer_count; |
650bc635 | 364 | } |
eef90ccb | 365 | |
650bc635 | 366 | return 0; |
67731b87 CW |
367 | } |
368 | ||
2889caa9 CW |
369 | static bool |
370 | eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry, | |
c7c6e46f CW |
371 | const struct i915_vma *vma, |
372 | unsigned int flags) | |
2889caa9 | 373 | { |
2889caa9 CW |
374 | if (vma->node.size < entry->pad_to_size) |
375 | return true; | |
376 | ||
377 | if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment)) | |
378 | return true; | |
379 | ||
c7c6e46f | 380 | if (flags & EXEC_OBJECT_PINNED && |
2889caa9 CW |
381 | vma->node.start != entry->offset) |
382 | return true; | |
383 | ||
c7c6e46f | 384 | if (flags & __EXEC_OBJECT_NEEDS_BIAS && |
2889caa9 CW |
385 | vma->node.start < BATCH_OFFSET_BIAS) |
386 | return true; | |
387 | ||
c7c6e46f | 388 | if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) && |
5f22cc0b | 389 | (vma->node.start + vma->node.size + 4095) >> 32) |
2889caa9 CW |
390 | return true; |
391 | ||
1d033beb CW |
392 | if (flags & __EXEC_OBJECT_NEEDS_MAP && |
393 | !i915_vma_is_map_and_fenceable(vma)) | |
394 | return true; | |
395 | ||
2889caa9 CW |
396 | return false; |
397 | } | |
398 | ||
8a338f4b CW |
399 | static u64 eb_pin_flags(const struct drm_i915_gem_exec_object2 *entry, |
400 | unsigned int exec_flags) | |
401 | { | |
402 | u64 pin_flags = 0; | |
403 | ||
404 | if (exec_flags & EXEC_OBJECT_NEEDS_GTT) | |
405 | pin_flags |= PIN_GLOBAL; | |
406 | ||
407 | /* | |
408 | * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset, | |
409 | * limit address to the first 4GBs for unflagged objects. | |
410 | */ | |
411 | if (!(exec_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS)) | |
412 | pin_flags |= PIN_ZONE_4G; | |
413 | ||
414 | if (exec_flags & __EXEC_OBJECT_NEEDS_MAP) | |
415 | pin_flags |= PIN_MAPPABLE; | |
416 | ||
417 | if (exec_flags & EXEC_OBJECT_PINNED) | |
418 | pin_flags |= entry->offset | PIN_OFFSET_FIXED; | |
419 | else if (exec_flags & __EXEC_OBJECT_NEEDS_BIAS) | |
420 | pin_flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS; | |
421 | ||
422 | return pin_flags; | |
423 | } | |
424 | ||
237647f4 | 425 | static inline int |
2889caa9 | 426 | eb_pin_vma(struct i915_execbuffer *eb, |
c7c6e46f | 427 | const struct drm_i915_gem_exec_object2 *entry, |
7d6236bb | 428 | struct eb_vma *ev) |
2889caa9 | 429 | { |
7d6236bb | 430 | struct i915_vma *vma = ev->vma; |
c7c6e46f | 431 | u64 pin_flags; |
237647f4 | 432 | int err; |
2889caa9 | 433 | |
616d9cee | 434 | if (vma->node.size) |
c7c6e46f | 435 | pin_flags = vma->node.start; |
616d9cee | 436 | else |
c7c6e46f | 437 | pin_flags = entry->offset & PIN_OFFSET_MASK; |
616d9cee | 438 | |
c7c6e46f | 439 | pin_flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED; |
7d6236bb | 440 | if (unlikely(ev->flags & EXEC_OBJECT_NEEDS_GTT)) |
c7c6e46f | 441 | pin_flags |= PIN_GLOBAL; |
616d9cee | 442 | |
8a338f4b | 443 | /* Attempt to reuse the current location if available */ |
237647f4 ML |
444 | err = i915_vma_pin_ww(vma, &eb->ww, 0, 0, pin_flags); |
445 | if (err == -EDEADLK) | |
446 | return err; | |
447 | ||
448 | if (unlikely(err)) { | |
8a338f4b | 449 | if (entry->flags & EXEC_OBJECT_PINNED) |
237647f4 | 450 | return err; |
8a338f4b CW |
451 | |
452 | /* Failing that pick any _free_ space if suitable */ | |
237647f4 | 453 | err = i915_vma_pin_ww(vma, &eb->ww, |
47b08693 ML |
454 | entry->pad_to_size, |
455 | entry->alignment, | |
456 | eb_pin_flags(entry, ev->flags) | | |
237647f4 ML |
457 | PIN_USER | PIN_NOEVICT); |
458 | if (unlikely(err)) | |
459 | return err; | |
8a338f4b | 460 | } |
2889caa9 | 461 | |
7d6236bb | 462 | if (unlikely(ev->flags & EXEC_OBJECT_NEEDS_FENCE)) { |
237647f4 ML |
463 | err = i915_vma_pin_fence(vma); |
464 | if (unlikely(err)) { | |
2889caa9 | 465 | i915_vma_unpin(vma); |
237647f4 | 466 | return err; |
2889caa9 CW |
467 | } |
468 | ||
3bd40735 | 469 | if (vma->fence) |
7d6236bb | 470 | ev->flags |= __EXEC_OBJECT_HAS_FENCE; |
2889caa9 CW |
471 | } |
472 | ||
7d6236bb | 473 | ev->flags |= __EXEC_OBJECT_HAS_PIN; |
237647f4 ML |
474 | if (eb_vma_misplaced(entry, vma, ev->flags)) |
475 | return -EBADSLT; | |
476 | ||
477 | return 0; | |
2889caa9 CW |
478 | } |
479 | ||
8ae275c2 ML |
480 | static inline void |
481 | eb_unreserve_vma(struct eb_vma *ev) | |
482 | { | |
483 | if (!(ev->flags & __EXEC_OBJECT_HAS_PIN)) | |
484 | return; | |
485 | ||
c43ce123 ML |
486 | if (unlikely(ev->flags & __EXEC_OBJECT_HAS_FENCE)) |
487 | __i915_vma_unpin_fence(ev->vma); | |
488 | ||
489 | __i915_vma_unpin(ev->vma); | |
8ae275c2 ML |
490 | ev->flags &= ~__EXEC_OBJECT_RESERVED; |
491 | } | |
492 | ||
2889caa9 CW |
493 | static int |
494 | eb_validate_vma(struct i915_execbuffer *eb, | |
495 | struct drm_i915_gem_exec_object2 *entry, | |
496 | struct i915_vma *vma) | |
67731b87 | 497 | { |
2eb8e1a6 JE |
498 | /* Relocations are disallowed for all platforms after TGL-LP. This |
499 | * also covers all platforms with local memory. | |
500 | */ | |
501 | if (entry->relocation_count && | |
40e1956e | 502 | GRAPHICS_VER(eb->i915) >= 12 && !IS_TIGERLAKE(eb->i915)) |
2eb8e1a6 JE |
503 | return -EINVAL; |
504 | ||
2889caa9 CW |
505 | if (unlikely(entry->flags & eb->invalid_flags)) |
506 | return -EINVAL; | |
d55495b4 | 507 | |
2920516b MA |
508 | if (unlikely(entry->alignment && |
509 | !is_power_of_2_u64(entry->alignment))) | |
2889caa9 CW |
510 | return -EINVAL; |
511 | ||
512 | /* | |
513 | * Offset can be used as input (EXEC_OBJECT_PINNED), reject | |
514 | * any non-page-aligned or non-canonical addresses. | |
515 | */ | |
516 | if (unlikely(entry->flags & EXEC_OBJECT_PINNED && | |
6fc4e48f | 517 | entry->offset != gen8_canonical_addr(entry->offset & I915_GTT_PAGE_MASK))) |
2889caa9 CW |
518 | return -EINVAL; |
519 | ||
520 | /* pad_to_size was once a reserved field, so sanitize it */ | |
521 | if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) { | |
522 | if (unlikely(offset_in_page(entry->pad_to_size))) | |
523 | return -EINVAL; | |
524 | } else { | |
525 | entry->pad_to_size = 0; | |
d55495b4 | 526 | } |
2889caa9 CW |
527 | /* |
528 | * From drm_mm perspective address space is continuous, | |
529 | * so from this point we're always using non-canonical | |
530 | * form internally. | |
531 | */ | |
532 | entry->offset = gen8_noncanonical_addr(entry->offset); | |
533 | ||
c7c6e46f CW |
534 | if (!eb->reloc_cache.has_fence) { |
535 | entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE; | |
536 | } else { | |
537 | if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE || | |
538 | eb->reloc_cache.needs_unfenced) && | |
539 | i915_gem_object_is_tiled(vma->obj)) | |
540 | entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP; | |
541 | } | |
542 | ||
2889caa9 | 543 | return 0; |
67731b87 CW |
544 | } |
545 | ||
544460c3 MB |
546 | static inline bool |
547 | is_batch_buffer(struct i915_execbuffer *eb, unsigned int buffer_idx) | |
548 | { | |
549 | return eb->args->flags & I915_EXEC_BATCH_FIRST ? | |
550 | buffer_idx < eb->num_batches : | |
551 | buffer_idx >= eb->args->buffer_count - eb->num_batches; | |
552 | } | |
553 | ||
554 | static int | |
746c8f14 | 555 | eb_add_vma(struct i915_execbuffer *eb, |
544460c3 MB |
556 | unsigned int *current_batch, |
557 | unsigned int i, | |
746c8f14 | 558 | struct i915_vma *vma) |
59bfa124 | 559 | { |
544460c3 | 560 | struct drm_i915_private *i915 = eb->i915; |
c7c6e46f | 561 | struct drm_i915_gem_exec_object2 *entry = &eb->exec[i]; |
7d6236bb | 562 | struct eb_vma *ev = &eb->vma[i]; |
2889caa9 | 563 | |
93159e12 | 564 | ev->vma = vma; |
7d6236bb CW |
565 | ev->exec = entry; |
566 | ev->flags = entry->flags; | |
567 | ||
4d470f73 | 568 | if (eb->lut_size > 0) { |
7d6236bb CW |
569 | ev->handle = entry->handle; |
570 | hlist_add_head(&ev->node, | |
2889caa9 CW |
571 | &eb->buckets[hash_32(entry->handle, |
572 | eb->lut_size)]); | |
4ff4b44c | 573 | } |
59bfa124 | 574 | |
2889caa9 | 575 | if (entry->relocation_count) |
7d6236bb | 576 | list_add_tail(&ev->reloc_link, &eb->relocs); |
2889caa9 | 577 | |
746c8f14 CW |
578 | /* |
579 | * SNA is doing fancy tricks with compressing batch buffers, which leads | |
580 | * to negative relocation deltas. Usually that works out ok since the | |
581 | * relocate address is still positive, except when the batch is placed | |
582 | * very low in the GTT. Ensure this doesn't happen. | |
583 | * | |
584 | * Note that actual hangs have only been observed on gen7, but for | |
585 | * paranoia do it everywhere. | |
586 | */ | |
544460c3 | 587 | if (is_batch_buffer(eb, i)) { |
827db9d8 | 588 | if (entry->relocation_count && |
7d6236bb CW |
589 | !(ev->flags & EXEC_OBJECT_PINNED)) |
590 | ev->flags |= __EXEC_OBJECT_NEEDS_BIAS; | |
746c8f14 | 591 | if (eb->reloc_cache.has_fence) |
7d6236bb | 592 | ev->flags |= EXEC_OBJECT_NEEDS_FENCE; |
746c8f14 | 593 | |
544460c3 MB |
594 | eb->batches[*current_batch] = ev; |
595 | ||
596 | if (unlikely(ev->flags & EXEC_OBJECT_WRITE)) { | |
597 | drm_dbg(&i915->drm, | |
598 | "Attempting to use self-modifying batch buffer\n"); | |
599 | return -EINVAL; | |
600 | } | |
601 | ||
602 | if (range_overflows_t(u64, | |
603 | eb->batch_start_offset, | |
604 | eb->args->batch_len, | |
605 | ev->vma->size)) { | |
606 | drm_dbg(&i915->drm, "Attempting to use out-of-bounds batch\n"); | |
607 | return -EINVAL; | |
608 | } | |
609 | ||
610 | if (eb->args->batch_len == 0) | |
611 | eb->batch_len[*current_batch] = ev->vma->size - | |
612 | eb->batch_start_offset; | |
613 | else | |
614 | eb->batch_len[*current_batch] = eb->args->batch_len; | |
615 | if (unlikely(eb->batch_len[*current_batch] == 0)) { /* impossible! */ | |
616 | drm_dbg(&i915->drm, "Invalid batch length\n"); | |
617 | return -EINVAL; | |
618 | } | |
619 | ||
620 | ++*current_batch; | |
746c8f14 | 621 | } |
544460c3 MB |
622 | |
623 | return 0; | |
2889caa9 CW |
624 | } |
625 | ||
ad5d95e4 DA |
626 | static inline int use_cpu_reloc(const struct reloc_cache *cache, |
627 | const struct drm_i915_gem_object *obj) | |
628 | { | |
629 | if (!i915_gem_object_has_struct_page(obj)) | |
630 | return false; | |
631 | ||
632 | if (DBG_FORCE_RELOC == FORCE_CPU_RELOC) | |
633 | return true; | |
634 | ||
635 | if (DBG_FORCE_RELOC == FORCE_GTT_RELOC) | |
636 | return false; | |
637 | ||
638 | return (cache->has_llc || | |
639 | obj->cache_dirty || | |
640 | obj->cache_level != I915_CACHE_NONE); | |
641 | } | |
642 | ||
47b08693 | 643 | static int eb_reserve_vma(struct i915_execbuffer *eb, |
7d6236bb | 644 | struct eb_vma *ev, |
2920bb94 | 645 | u64 pin_flags) |
2889caa9 | 646 | { |
7d6236bb | 647 | struct drm_i915_gem_exec_object2 *entry = ev->exec; |
7d6236bb | 648 | struct i915_vma *vma = ev->vma; |
2889caa9 CW |
649 | int err; |
650 | ||
003d8b91 CW |
651 | if (drm_mm_node_allocated(&vma->node) && |
652 | eb_vma_misplaced(entry, vma, ev->flags)) { | |
653 | err = i915_vma_unbind(vma); | |
654 | if (err) | |
655 | return err; | |
656 | } | |
657 | ||
47b08693 | 658 | err = i915_vma_pin_ww(vma, &eb->ww, |
c7c6e46f | 659 | entry->pad_to_size, entry->alignment, |
8a338f4b | 660 | eb_pin_flags(entry, ev->flags) | pin_flags); |
2889caa9 CW |
661 | if (err) |
662 | return err; | |
663 | ||
664 | if (entry->offset != vma->node.start) { | |
665 | entry->offset = vma->node.start | UPDATE; | |
666 | eb->args->flags |= __EXEC_HAS_RELOC; | |
667 | } | |
668 | ||
8a338f4b | 669 | if (unlikely(ev->flags & EXEC_OBJECT_NEEDS_FENCE)) { |
3bd40735 | 670 | err = i915_vma_pin_fence(vma); |
2889caa9 CW |
671 | if (unlikely(err)) { |
672 | i915_vma_unpin(vma); | |
673 | return err; | |
674 | } | |
675 | ||
3bd40735 | 676 | if (vma->fence) |
8a338f4b | 677 | ev->flags |= __EXEC_OBJECT_HAS_FENCE; |
2889caa9 CW |
678 | } |
679 | ||
8a338f4b | 680 | ev->flags |= __EXEC_OBJECT_HAS_PIN; |
7d6236bb | 681 | GEM_BUG_ON(eb_vma_misplaced(entry, vma, ev->flags)); |
1da7b54c | 682 | |
2889caa9 CW |
683 | return 0; |
684 | } | |
685 | ||
686 | static int eb_reserve(struct i915_execbuffer *eb) | |
687 | { | |
688 | const unsigned int count = eb->buffer_count; | |
2920bb94 | 689 | unsigned int pin_flags = PIN_USER | PIN_NONBLOCK; |
2889caa9 | 690 | struct list_head last; |
7d6236bb | 691 | struct eb_vma *ev; |
2889caa9 | 692 | unsigned int i, pass; |
ef398881 | 693 | int err = 0; |
2889caa9 CW |
694 | |
695 | /* | |
696 | * Attempt to pin all of the buffers into the GTT. | |
697 | * This is done in 3 phases: | |
698 | * | |
699 | * 1a. Unbind all objects that do not match the GTT constraints for | |
700 | * the execbuffer (fenceable, mappable, alignment etc). | |
701 | * 1b. Increment pin count for already bound objects. | |
702 | * 2. Bind new objects. | |
703 | * 3. Decrement pin count. | |
704 | * | |
705 | * This avoid unnecessary unbinding of later objects in order to make | |
706 | * room for the earlier objects *unless* we need to defragment. | |
707 | */ | |
2889caa9 | 708 | pass = 0; |
2889caa9 | 709 | do { |
7d6236bb CW |
710 | list_for_each_entry(ev, &eb->unbound, bind_link) { |
711 | err = eb_reserve_vma(eb, ev, pin_flags); | |
2889caa9 CW |
712 | if (err) |
713 | break; | |
714 | } | |
fd1500fc | 715 | if (err != -ENOSPC) |
c43ce123 | 716 | return err; |
2889caa9 CW |
717 | |
718 | /* Resort *all* the objects into priority order */ | |
719 | INIT_LIST_HEAD(&eb->unbound); | |
720 | INIT_LIST_HEAD(&last); | |
721 | for (i = 0; i < count; i++) { | |
7d6236bb | 722 | unsigned int flags; |
2889caa9 | 723 | |
7d6236bb CW |
724 | ev = &eb->vma[i]; |
725 | flags = ev->flags; | |
c7c6e46f CW |
726 | if (flags & EXEC_OBJECT_PINNED && |
727 | flags & __EXEC_OBJECT_HAS_PIN) | |
2889caa9 CW |
728 | continue; |
729 | ||
7d6236bb | 730 | eb_unreserve_vma(ev); |
2889caa9 | 731 | |
c7c6e46f | 732 | if (flags & EXEC_OBJECT_PINNED) |
35e882a4 | 733 | /* Pinned must have their slot */ |
7d6236bb | 734 | list_add(&ev->bind_link, &eb->unbound); |
c7c6e46f | 735 | else if (flags & __EXEC_OBJECT_NEEDS_MAP) |
35e882a4 | 736 | /* Map require the lowest 256MiB (aperture) */ |
7d6236bb | 737 | list_add_tail(&ev->bind_link, &eb->unbound); |
35e882a4 CW |
738 | else if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS)) |
739 | /* Prioritise 4GiB region for restricted bo */ | |
7d6236bb | 740 | list_add(&ev->bind_link, &last); |
2889caa9 | 741 | else |
7d6236bb | 742 | list_add_tail(&ev->bind_link, &last); |
2889caa9 CW |
743 | } |
744 | list_splice_tail(&last, &eb->unbound); | |
745 | ||
746 | switch (pass++) { | |
747 | case 0: | |
748 | break; | |
749 | ||
750 | case 1: | |
751 | /* Too fragmented, unbind everything and retry */ | |
2850748e | 752 | mutex_lock(&eb->context->vm->mutex); |
f5d974f9 | 753 | err = i915_gem_evict_vm(eb->context->vm); |
2850748e | 754 | mutex_unlock(&eb->context->vm->mutex); |
2889caa9 | 755 | if (err) |
c43ce123 | 756 | return err; |
2889caa9 CW |
757 | break; |
758 | ||
759 | default: | |
c43ce123 | 760 | return -ENOSPC; |
2889caa9 | 761 | } |
2920bb94 CW |
762 | |
763 | pin_flags = PIN_USER; | |
2889caa9 | 764 | } while (1); |
4ff4b44c | 765 | } |
59bfa124 | 766 | |
2889caa9 CW |
767 | static int eb_select_context(struct i915_execbuffer *eb) |
768 | { | |
769 | struct i915_gem_context *ctx; | |
770 | ||
771 | ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1); | |
046d1660 JE |
772 | if (unlikely(IS_ERR(ctx))) |
773 | return PTR_ERR(ctx); | |
2889caa9 | 774 | |
8f2a1057 | 775 | eb->gem_context = ctx; |
a82a9979 | 776 | if (i915_gem_context_has_full_ppgtt(ctx)) |
4f2c7337 | 777 | eb->invalid_flags |= EXEC_OBJECT_NEEDS_GTT; |
2889caa9 | 778 | |
2889caa9 CW |
779 | return 0; |
780 | } | |
781 | ||
93159e12 CW |
782 | static int __eb_add_lut(struct i915_execbuffer *eb, |
783 | u32 handle, struct i915_vma *vma) | |
3b96eff4 | 784 | { |
93159e12 CW |
785 | struct i915_gem_context *ctx = eb->gem_context; |
786 | struct i915_lut_handle *lut; | |
2889caa9 | 787 | int err; |
3b96eff4 | 788 | |
93159e12 CW |
789 | lut = i915_lut_handle_alloc(); |
790 | if (unlikely(!lut)) | |
791 | return -ENOMEM; | |
792 | ||
793 | i915_vma_get(vma); | |
794 | if (!atomic_fetch_inc(&vma->open_count)) | |
795 | i915_vma_reopen(vma); | |
796 | lut->handle = handle; | |
797 | lut->ctx = ctx; | |
798 | ||
799 | /* Check that the context hasn't been closed in the meantime */ | |
800 | err = -EINTR; | |
f7ce8639 | 801 | if (!mutex_lock_interruptible(&ctx->lut_mutex)) { |
e1068a9e | 802 | if (likely(!i915_gem_context_is_closed(ctx))) |
93159e12 | 803 | err = radix_tree_insert(&ctx->handles_vma, handle, vma); |
f7ce8639 CW |
804 | else |
805 | err = -ENOENT; | |
93159e12 CW |
806 | if (err == 0) { /* And nor has this handle */ |
807 | struct drm_i915_gem_object *obj = vma->obj; | |
808 | ||
096a42dd | 809 | spin_lock(&obj->lut_lock); |
93159e12 CW |
810 | if (idr_find(&eb->file->object_idr, handle) == obj) { |
811 | list_add(&lut->obj_link, &obj->lut_list); | |
812 | } else { | |
813 | radix_tree_delete(&ctx->handles_vma, handle); | |
814 | err = -ENOENT; | |
815 | } | |
096a42dd | 816 | spin_unlock(&obj->lut_lock); |
93159e12 | 817 | } |
f7ce8639 | 818 | mutex_unlock(&ctx->lut_mutex); |
93159e12 CW |
819 | } |
820 | if (unlikely(err)) | |
821 | goto err; | |
003d8b91 | 822 | |
93159e12 | 823 | return 0; |
d55495b4 | 824 | |
93159e12 | 825 | err: |
50689771 | 826 | i915_vma_close(vma); |
93159e12 CW |
827 | i915_vma_put(vma); |
828 | i915_lut_handle_free(lut); | |
829 | return err; | |
830 | } | |
746c8f14 | 831 | |
93159e12 CW |
832 | static struct i915_vma *eb_lookup_vma(struct i915_execbuffer *eb, u32 handle) |
833 | { | |
f7ce8639 CW |
834 | struct i915_address_space *vm = eb->context->vm; |
835 | ||
93159e12 CW |
836 | do { |
837 | struct drm_i915_gem_object *obj; | |
170fa29b | 838 | struct i915_vma *vma; |
93159e12 | 839 | int err; |
4ff4b44c | 840 | |
93159e12 CW |
841 | rcu_read_lock(); |
842 | vma = radix_tree_lookup(&eb->gem_context->handles_vma, handle); | |
f7ce8639 | 843 | if (likely(vma && vma->vm == vm)) |
93159e12 CW |
844 | vma = i915_vma_tryget(vma); |
845 | rcu_read_unlock(); | |
846 | if (likely(vma)) | |
847 | return vma; | |
4ff4b44c | 848 | |
170fa29b | 849 | obj = i915_gem_object_lookup(eb->file, handle); |
93159e12 CW |
850 | if (unlikely(!obj)) |
851 | return ERR_PTR(-ENOENT); | |
3b96eff4 | 852 | |
d3ac8d42 DCS |
853 | /* |
854 | * If the user has opted-in for protected-object tracking, make | |
855 | * sure the object encryption can be used. | |
856 | * We only need to do this when the object is first used with | |
857 | * this context, because the context itself will be banned when | |
858 | * the protected objects become invalid. | |
859 | */ | |
860 | if (i915_gem_context_uses_protected_content(eb->gem_context) && | |
861 | i915_gem_object_is_protected(obj)) { | |
ef6ba31d | 862 | err = intel_pxp_key_check(&vm->gt->pxp, obj, true); |
d3ac8d42 DCS |
863 | if (err) { |
864 | i915_gem_object_put(obj); | |
865 | return ERR_PTR(err); | |
866 | } | |
867 | } | |
868 | ||
f7ce8639 | 869 | vma = i915_vma_instance(obj, vm, NULL); |
772b5408 | 870 | if (IS_ERR(vma)) { |
93159e12 CW |
871 | i915_gem_object_put(obj); |
872 | return vma; | |
27173f1f BW |
873 | } |
874 | ||
93159e12 CW |
875 | err = __eb_add_lut(eb, handle, vma); |
876 | if (likely(!err)) | |
877 | return vma; | |
d1b48c1e | 878 | |
93159e12 CW |
879 | i915_gem_object_put(obj); |
880 | if (err != -EEXIST) | |
881 | return ERR_PTR(err); | |
882 | } while (1); | |
883 | } | |
4ff4b44c | 884 | |
93159e12 CW |
885 | static int eb_lookup_vmas(struct i915_execbuffer *eb) |
886 | { | |
544460c3 | 887 | unsigned int i, current_batch = 0; |
93159e12 | 888 | int err = 0; |
155ab883 | 889 | |
93159e12 | 890 | INIT_LIST_HEAD(&eb->relocs); |
93159e12 CW |
891 | |
892 | for (i = 0; i < eb->buffer_count; i++) { | |
893 | struct i915_vma *vma; | |
894 | ||
895 | vma = eb_lookup_vma(eb, eb->exec[i].handle); | |
896 | if (IS_ERR(vma)) { | |
897 | err = PTR_ERR(vma); | |
8e4ba491 | 898 | goto err; |
93159e12 | 899 | } |
d1b48c1e | 900 | |
003d8b91 | 901 | err = eb_validate_vma(eb, &eb->exec[i], vma); |
93159e12 CW |
902 | if (unlikely(err)) { |
903 | i915_vma_put(vma); | |
8e4ba491 | 904 | goto err; |
93159e12 | 905 | } |
dade2a61 | 906 | |
544460c3 MB |
907 | err = eb_add_vma(eb, ¤t_batch, i, vma); |
908 | if (err) | |
909 | return err; | |
ed29c269 ML |
910 | |
911 | if (i915_gem_object_is_userptr(vma->obj)) { | |
912 | err = i915_gem_object_userptr_submit_init(vma->obj); | |
913 | if (err) { | |
914 | if (i + 1 < eb->buffer_count) { | |
915 | /* | |
916 | * Execbuffer code expects last vma entry to be NULL, | |
917 | * since we already initialized this entry, | |
918 | * set the next value to NULL or we mess up | |
919 | * cleanup handling. | |
920 | */ | |
921 | eb->vma[i + 1].vma = NULL; | |
922 | } | |
923 | ||
924 | return err; | |
925 | } | |
926 | ||
927 | eb->vma[i].flags |= __EXEC_OBJECT_USERPTR_INIT; | |
928 | eb->args->flags |= __EXEC_USERPTR_USED; | |
929 | } | |
4ff4b44c CW |
930 | } |
931 | ||
8e4ba491 ML |
932 | return 0; |
933 | ||
934 | err: | |
7d6236bb | 935 | eb->vma[i].vma = NULL; |
2889caa9 | 936 | return err; |
3b96eff4 CW |
937 | } |
938 | ||
5cd57f67 | 939 | static int eb_lock_vmas(struct i915_execbuffer *eb) |
c43ce123 ML |
940 | { |
941 | unsigned int i; | |
942 | int err; | |
943 | ||
c43ce123 | 944 | for (i = 0; i < eb->buffer_count; i++) { |
c43ce123 ML |
945 | struct eb_vma *ev = &eb->vma[i]; |
946 | struct i915_vma *vma = ev->vma; | |
947 | ||
948 | err = i915_gem_object_lock(vma->obj, &eb->ww); | |
949 | if (err) | |
950 | return err; | |
5cd57f67 TH |
951 | } |
952 | ||
953 | return 0; | |
954 | } | |
955 | ||
956 | static int eb_validate_vmas(struct i915_execbuffer *eb) | |
957 | { | |
958 | unsigned int i; | |
959 | int err; | |
960 | ||
961 | INIT_LIST_HEAD(&eb->unbound); | |
962 | ||
963 | err = eb_lock_vmas(eb); | |
964 | if (err) | |
965 | return err; | |
966 | ||
967 | for (i = 0; i < eb->buffer_count; i++) { | |
968 | struct drm_i915_gem_exec_object2 *entry = &eb->exec[i]; | |
969 | struct eb_vma *ev = &eb->vma[i]; | |
970 | struct i915_vma *vma = ev->vma; | |
c43ce123 | 971 | |
237647f4 ML |
972 | err = eb_pin_vma(eb, entry, ev); |
973 | if (err == -EDEADLK) | |
974 | return err; | |
975 | ||
976 | if (!err) { | |
c43ce123 ML |
977 | if (entry->offset != vma->node.start) { |
978 | entry->offset = vma->node.start | UPDATE; | |
979 | eb->args->flags |= __EXEC_HAS_RELOC; | |
980 | } | |
981 | } else { | |
982 | eb_unreserve_vma(ev); | |
983 | ||
984 | list_add_tail(&ev->bind_link, &eb->unbound); | |
985 | if (drm_mm_node_allocated(&vma->node)) { | |
986 | err = i915_vma_unbind(vma); | |
987 | if (err) | |
988 | return err; | |
989 | } | |
990 | } | |
991 | ||
bfaae47d ML |
992 | if (!(ev->flags & EXEC_OBJECT_WRITE)) { |
993 | err = dma_resv_reserve_shared(vma->resv, 1); | |
994 | if (err) | |
995 | return err; | |
996 | } | |
997 | ||
c43ce123 ML |
998 | GEM_BUG_ON(drm_mm_node_allocated(&vma->node) && |
999 | eb_vma_misplaced(&eb->exec[i], vma, ev->flags)); | |
1000 | } | |
1001 | ||
1002 | if (!list_empty(&eb->unbound)) | |
1003 | return eb_reserve(eb); | |
1004 | ||
1005 | return 0; | |
1006 | } | |
1007 | ||
7d6236bb | 1008 | static struct eb_vma * |
2889caa9 | 1009 | eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle) |
67731b87 | 1010 | { |
2889caa9 CW |
1011 | if (eb->lut_size < 0) { |
1012 | if (handle >= -eb->lut_size) | |
eef90ccb | 1013 | return NULL; |
7d6236bb | 1014 | return &eb->vma[handle]; |
eef90ccb CW |
1015 | } else { |
1016 | struct hlist_head *head; | |
7d6236bb | 1017 | struct eb_vma *ev; |
67731b87 | 1018 | |
2889caa9 | 1019 | head = &eb->buckets[hash_32(handle, eb->lut_size)]; |
7d6236bb CW |
1020 | hlist_for_each_entry(ev, head, node) { |
1021 | if (ev->handle == handle) | |
1022 | return ev; | |
eef90ccb CW |
1023 | } |
1024 | return NULL; | |
1025 | } | |
67731b87 CW |
1026 | } |
1027 | ||
b4b9731b | 1028 | static void eb_release_vmas(struct i915_execbuffer *eb, bool final) |
8ae275c2 ML |
1029 | { |
1030 | const unsigned int count = eb->buffer_count; | |
1031 | unsigned int i; | |
1032 | ||
1033 | for (i = 0; i < count; i++) { | |
1034 | struct eb_vma *ev = &eb->vma[i]; | |
1035 | struct i915_vma *vma = ev->vma; | |
1036 | ||
1037 | if (!vma) | |
1038 | break; | |
1039 | ||
c43ce123 | 1040 | eb_unreserve_vma(ev); |
8ae275c2 | 1041 | |
c43ce123 ML |
1042 | if (final) |
1043 | i915_vma_put(vma); | |
8ae275c2 | 1044 | } |
2bf541ff ML |
1045 | |
1046 | eb_unpin_engine(eb); | |
8ae275c2 ML |
1047 | } |
1048 | ||
2889caa9 | 1049 | static void eb_destroy(const struct i915_execbuffer *eb) |
934acce3 | 1050 | { |
4d470f73 | 1051 | if (eb->lut_size > 0) |
2889caa9 | 1052 | kfree(eb->buckets); |
934acce3 MW |
1053 | } |
1054 | ||
2889caa9 | 1055 | static inline u64 |
d50415cc | 1056 | relocation_target(const struct drm_i915_gem_relocation_entry *reloc, |
2889caa9 | 1057 | const struct i915_vma *target) |
934acce3 | 1058 | { |
2889caa9 | 1059 | return gen8_canonical_addr((int)reloc->delta + target->node.start); |
934acce3 MW |
1060 | } |
1061 | ||
d50415cc CW |
1062 | static void reloc_cache_init(struct reloc_cache *cache, |
1063 | struct drm_i915_private *i915) | |
5032d871 | 1064 | { |
ad5d95e4 DA |
1065 | cache->page = -1; |
1066 | cache->vaddr = 0; | |
dfc5148f | 1067 | /* Must be a variable in the struct to allow GCC to unroll. */ |
8802190f | 1068 | cache->graphics_ver = GRAPHICS_VER(i915); |
2889caa9 | 1069 | cache->has_llc = HAS_LLC(i915); |
dfc5148f | 1070 | cache->use_64bit_reloc = HAS_64BIT_RELOC(i915); |
8802190f | 1071 | cache->has_fence = cache->graphics_ver < 4; |
7dd4f672 | 1072 | cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment; |
4ee92c71 | 1073 | cache->node.flags = 0; |
d50415cc | 1074 | } |
5032d871 | 1075 | |
20561da3 DA |
1076 | static inline void *unmask_page(unsigned long p) |
1077 | { | |
1078 | return (void *)(uintptr_t)(p & PAGE_MASK); | |
1079 | } | |
1080 | ||
1081 | static inline unsigned int unmask_flags(unsigned long p) | |
1082 | { | |
1083 | return p & ~PAGE_MASK; | |
1084 | } | |
1085 | ||
1086 | #define KMAP 0x4 /* after CLFLUSH_FLAGS */ | |
1087 | ||
1088 | static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache) | |
1089 | { | |
1090 | struct drm_i915_private *i915 = | |
1091 | container_of(cache, struct i915_execbuffer, reloc_cache)->i915; | |
1092 | return &i915->ggtt; | |
1093 | } | |
1094 | ||
c43ce123 | 1095 | static void reloc_cache_reset(struct reloc_cache *cache, struct i915_execbuffer *eb) |
ad5d95e4 DA |
1096 | { |
1097 | void *vaddr; | |
1098 | ||
1099 | if (!cache->vaddr) | |
1100 | return; | |
1101 | ||
1102 | vaddr = unmask_page(cache->vaddr); | |
1103 | if (cache->vaddr & KMAP) { | |
1af343cd ML |
1104 | struct drm_i915_gem_object *obj = |
1105 | (struct drm_i915_gem_object *)cache->node.mm; | |
ad5d95e4 DA |
1106 | if (cache->vaddr & CLFLUSH_AFTER) |
1107 | mb(); | |
1108 | ||
1109 | kunmap_atomic(vaddr); | |
1af343cd | 1110 | i915_gem_object_finish_access(obj); |
ad5d95e4 DA |
1111 | } else { |
1112 | struct i915_ggtt *ggtt = cache_to_ggtt(cache); | |
1113 | ||
1114 | intel_gt_flush_ggtt_writes(ggtt->vm.gt); | |
1115 | io_mapping_unmap_atomic((void __iomem *)vaddr); | |
1116 | ||
1117 | if (drm_mm_node_allocated(&cache->node)) { | |
1118 | ggtt->vm.clear_range(&ggtt->vm, | |
1119 | cache->node.start, | |
1120 | cache->node.size); | |
1121 | mutex_lock(&ggtt->vm.mutex); | |
1122 | drm_mm_remove_node(&cache->node); | |
1123 | mutex_unlock(&ggtt->vm.mutex); | |
1124 | } else { | |
1125 | i915_vma_unpin((struct i915_vma *)cache->node.mm); | |
1126 | } | |
1127 | } | |
1128 | ||
1129 | cache->vaddr = 0; | |
1130 | cache->page = -1; | |
1131 | } | |
1132 | ||
1133 | static void *reloc_kmap(struct drm_i915_gem_object *obj, | |
1134 | struct reloc_cache *cache, | |
102a0a90 | 1135 | unsigned long pageno) |
ad5d95e4 DA |
1136 | { |
1137 | void *vaddr; | |
102a0a90 | 1138 | struct page *page; |
ad5d95e4 DA |
1139 | |
1140 | if (cache->vaddr) { | |
1141 | kunmap_atomic(unmask_page(cache->vaddr)); | |
1142 | } else { | |
1143 | unsigned int flushes; | |
1144 | int err; | |
1145 | ||
1146 | err = i915_gem_object_prepare_write(obj, &flushes); | |
1147 | if (err) | |
1148 | return ERR_PTR(err); | |
1149 | ||
1150 | BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS); | |
1151 | BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK); | |
1152 | ||
1153 | cache->vaddr = flushes | KMAP; | |
1154 | cache->node.mm = (void *)obj; | |
1155 | if (flushes) | |
1156 | mb(); | |
1157 | } | |
1158 | ||
102a0a90 ML |
1159 | page = i915_gem_object_get_page(obj, pageno); |
1160 | if (!obj->mm.dirty) | |
1161 | set_page_dirty(page); | |
1162 | ||
1163 | vaddr = kmap_atomic(page); | |
ad5d95e4 | 1164 | cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr; |
102a0a90 | 1165 | cache->page = pageno; |
ad5d95e4 DA |
1166 | |
1167 | return vaddr; | |
1168 | } | |
1169 | ||
1170 | static void *reloc_iomap(struct drm_i915_gem_object *obj, | |
47b08693 | 1171 | struct i915_execbuffer *eb, |
ad5d95e4 DA |
1172 | unsigned long page) |
1173 | { | |
47b08693 | 1174 | struct reloc_cache *cache = &eb->reloc_cache; |
ad5d95e4 DA |
1175 | struct i915_ggtt *ggtt = cache_to_ggtt(cache); |
1176 | unsigned long offset; | |
1177 | void *vaddr; | |
1178 | ||
1179 | if (cache->vaddr) { | |
1180 | intel_gt_flush_ggtt_writes(ggtt->vm.gt); | |
1181 | io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr)); | |
1182 | } else { | |
1183 | struct i915_vma *vma; | |
1184 | int err; | |
1185 | ||
1186 | if (i915_gem_object_is_tiled(obj)) | |
1187 | return ERR_PTR(-EINVAL); | |
1188 | ||
1189 | if (use_cpu_reloc(cache, obj)) | |
1190 | return NULL; | |
1191 | ||
ad5d95e4 | 1192 | err = i915_gem_object_set_to_gtt_domain(obj, true); |
ad5d95e4 DA |
1193 | if (err) |
1194 | return ERR_PTR(err); | |
1195 | ||
47b08693 ML |
1196 | vma = i915_gem_object_ggtt_pin_ww(obj, &eb->ww, NULL, 0, 0, |
1197 | PIN_MAPPABLE | | |
1198 | PIN_NONBLOCK /* NOWARN */ | | |
1199 | PIN_NOEVICT); | |
1200 | if (vma == ERR_PTR(-EDEADLK)) | |
1201 | return vma; | |
1202 | ||
ad5d95e4 DA |
1203 | if (IS_ERR(vma)) { |
1204 | memset(&cache->node, 0, sizeof(cache->node)); | |
1205 | mutex_lock(&ggtt->vm.mutex); | |
1206 | err = drm_mm_insert_node_in_range | |
1207 | (&ggtt->vm.mm, &cache->node, | |
1208 | PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE, | |
1209 | 0, ggtt->mappable_end, | |
1210 | DRM_MM_INSERT_LOW); | |
1211 | mutex_unlock(&ggtt->vm.mutex); | |
1212 | if (err) /* no inactive aperture space, use cpu reloc */ | |
1213 | return NULL; | |
1214 | } else { | |
1215 | cache->node.start = vma->node.start; | |
1216 | cache->node.mm = (void *)vma; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | offset = cache->node.start; | |
1221 | if (drm_mm_node_allocated(&cache->node)) { | |
1222 | ggtt->vm.insert_page(&ggtt->vm, | |
1223 | i915_gem_object_get_dma_address(obj, page), | |
1224 | offset, I915_CACHE_NONE, 0); | |
1225 | } else { | |
1226 | offset += page << PAGE_SHIFT; | |
1227 | } | |
1228 | ||
1229 | vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->iomap, | |
1230 | offset); | |
1231 | cache->page = page; | |
1232 | cache->vaddr = (unsigned long)vaddr; | |
1233 | ||
1234 | return vaddr; | |
1235 | } | |
1236 | ||
1237 | static void *reloc_vaddr(struct drm_i915_gem_object *obj, | |
47b08693 | 1238 | struct i915_execbuffer *eb, |
ad5d95e4 DA |
1239 | unsigned long page) |
1240 | { | |
47b08693 | 1241 | struct reloc_cache *cache = &eb->reloc_cache; |
ad5d95e4 DA |
1242 | void *vaddr; |
1243 | ||
1244 | if (cache->page == page) { | |
1245 | vaddr = unmask_page(cache->vaddr); | |
1246 | } else { | |
1247 | vaddr = NULL; | |
1248 | if ((cache->vaddr & KMAP) == 0) | |
47b08693 | 1249 | vaddr = reloc_iomap(obj, eb, page); |
ad5d95e4 DA |
1250 | if (!vaddr) |
1251 | vaddr = reloc_kmap(obj, cache, page); | |
1252 | } | |
1253 | ||
1254 | return vaddr; | |
1255 | } | |
1256 | ||
1257 | static void clflush_write32(u32 *addr, u32 value, unsigned int flushes) | |
1258 | { | |
1259 | if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) { | |
1260 | if (flushes & CLFLUSH_BEFORE) { | |
1261 | clflushopt(addr); | |
1262 | mb(); | |
1263 | } | |
1264 | ||
1265 | *addr = value; | |
1266 | ||
1267 | /* | |
1268 | * Writes to the same cacheline are serialised by the CPU | |
1269 | * (including clflush). On the write path, we only require | |
1270 | * that it hits memory in an orderly fashion and place | |
1271 | * mb barriers at the start and end of the relocation phase | |
1272 | * to ensure ordering of clflush wrt to the system. | |
1273 | */ | |
1274 | if (flushes & CLFLUSH_AFTER) | |
1275 | clflushopt(addr); | |
1276 | } else | |
1277 | *addr = value; | |
1278 | } | |
1279 | ||
e3d29130 | 1280 | static u64 |
ad5d95e4 | 1281 | relocate_entry(struct i915_vma *vma, |
e3d29130 | 1282 | const struct drm_i915_gem_relocation_entry *reloc, |
ad5d95e4 | 1283 | struct i915_execbuffer *eb, |
e3d29130 CW |
1284 | const struct i915_vma *target) |
1285 | { | |
1286 | u64 target_addr = relocation_target(reloc, target); | |
ad5d95e4 | 1287 | u64 offset = reloc->offset; |
ce13c78f DV |
1288 | bool wide = eb->reloc_cache.use_64bit_reloc; |
1289 | void *vaddr; | |
ad5d95e4 DA |
1290 | |
1291 | repeat: | |
ce13c78f DV |
1292 | vaddr = reloc_vaddr(vma->obj, eb, |
1293 | offset >> PAGE_SHIFT); | |
1294 | if (IS_ERR(vaddr)) | |
1295 | return PTR_ERR(vaddr); | |
1296 | ||
1297 | GEM_BUG_ON(!IS_ALIGNED(offset, sizeof(u32))); | |
1298 | clflush_write32(vaddr + offset_in_page(offset), | |
1299 | lower_32_bits(target_addr), | |
1300 | eb->reloc_cache.vaddr); | |
1301 | ||
1302 | if (wide) { | |
1303 | offset += sizeof(u32); | |
1304 | target_addr >>= 32; | |
1305 | wide = false; | |
1306 | goto repeat; | |
ad5d95e4 | 1307 | } |
edf4427b | 1308 | |
2889caa9 | 1309 | return target->node.start | UPDATE; |
edf4427b | 1310 | } |
edf4427b | 1311 | |
2889caa9 CW |
1312 | static u64 |
1313 | eb_relocate_entry(struct i915_execbuffer *eb, | |
7d6236bb | 1314 | struct eb_vma *ev, |
2889caa9 | 1315 | const struct drm_i915_gem_relocation_entry *reloc) |
54cf91dc | 1316 | { |
baa89ba3 | 1317 | struct drm_i915_private *i915 = eb->i915; |
7d6236bb | 1318 | struct eb_vma *target; |
2889caa9 | 1319 | int err; |
54cf91dc | 1320 | |
67731b87 | 1321 | /* we've already hold a reference to all valid objects */ |
507d977f CW |
1322 | target = eb_get_vma(eb, reloc->target_handle); |
1323 | if (unlikely(!target)) | |
54cf91dc | 1324 | return -ENOENT; |
e844b990 | 1325 | |
54cf91dc | 1326 | /* Validate that the target is in a valid r/w GPU domain */ |
b8f7ab17 | 1327 | if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) { |
baa89ba3 | 1328 | drm_dbg(&i915->drm, "reloc with multiple write domains: " |
507d977f | 1329 | "target %d offset %d " |
54cf91dc | 1330 | "read %08x write %08x", |
507d977f | 1331 | reloc->target_handle, |
54cf91dc CW |
1332 | (int) reloc->offset, |
1333 | reloc->read_domains, | |
1334 | reloc->write_domain); | |
8b78f0e5 | 1335 | return -EINVAL; |
54cf91dc | 1336 | } |
4ca4a250 DV |
1337 | if (unlikely((reloc->write_domain | reloc->read_domains) |
1338 | & ~I915_GEM_GPU_DOMAINS)) { | |
baa89ba3 | 1339 | drm_dbg(&i915->drm, "reloc with read/write non-GPU domains: " |
507d977f | 1340 | "target %d offset %d " |
54cf91dc | 1341 | "read %08x write %08x", |
507d977f | 1342 | reloc->target_handle, |
54cf91dc CW |
1343 | (int) reloc->offset, |
1344 | reloc->read_domains, | |
1345 | reloc->write_domain); | |
8b78f0e5 | 1346 | return -EINVAL; |
54cf91dc | 1347 | } |
54cf91dc | 1348 | |
2889caa9 | 1349 | if (reloc->write_domain) { |
7d6236bb | 1350 | target->flags |= EXEC_OBJECT_WRITE; |
507d977f | 1351 | |
2889caa9 CW |
1352 | /* |
1353 | * Sandybridge PPGTT errata: We need a global gtt mapping | |
1354 | * for MI and pipe_control writes because the gpu doesn't | |
1355 | * properly redirect them through the ppgtt for non_secure | |
1356 | * batchbuffers. | |
1357 | */ | |
1358 | if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION && | |
40e1956e | 1359 | GRAPHICS_VER(eb->i915) == 6) { |
7d6236bb CW |
1360 | err = i915_vma_bind(target->vma, |
1361 | target->vma->obj->cache_level, | |
2850748e | 1362 | PIN_GLOBAL, NULL); |
ea97c4ca | 1363 | if (err) |
2889caa9 CW |
1364 | return err; |
1365 | } | |
507d977f | 1366 | } |
54cf91dc | 1367 | |
2889caa9 CW |
1368 | /* |
1369 | * If the relocation already has the right value in it, no | |
54cf91dc CW |
1370 | * more work needs to be done. |
1371 | */ | |
ad5d95e4 DA |
1372 | if (!DBG_FORCE_RELOC && |
1373 | gen8_canonical_addr(target->vma->node.start) == reloc->presumed_offset) | |
67731b87 | 1374 | return 0; |
54cf91dc CW |
1375 | |
1376 | /* Check that the relocation address is valid... */ | |
3c94ceee | 1377 | if (unlikely(reloc->offset > |
7d6236bb | 1378 | ev->vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) { |
baa89ba3 | 1379 | drm_dbg(&i915->drm, "Relocation beyond object bounds: " |
507d977f CW |
1380 | "target %d offset %d size %d.\n", |
1381 | reloc->target_handle, | |
1382 | (int)reloc->offset, | |
7d6236bb | 1383 | (int)ev->vma->size); |
8b78f0e5 | 1384 | return -EINVAL; |
54cf91dc | 1385 | } |
b8f7ab17 | 1386 | if (unlikely(reloc->offset & 3)) { |
baa89ba3 | 1387 | drm_dbg(&i915->drm, "Relocation not 4-byte aligned: " |
507d977f CW |
1388 | "target %d offset %d.\n", |
1389 | reloc->target_handle, | |
1390 | (int)reloc->offset); | |
8b78f0e5 | 1391 | return -EINVAL; |
54cf91dc CW |
1392 | } |
1393 | ||
071750e5 CW |
1394 | /* |
1395 | * If we write into the object, we need to force the synchronisation | |
1396 | * barrier, either with an asynchronous clflush or if we executed the | |
1397 | * patching using the GPU (though that should be serialised by the | |
1398 | * timeline). To be completely sure, and since we are required to | |
1399 | * do relocations we are already stalling, disable the user's opt | |
0519bcb1 | 1400 | * out of our synchronisation. |
071750e5 | 1401 | */ |
7d6236bb | 1402 | ev->flags &= ~EXEC_OBJECT_ASYNC; |
071750e5 | 1403 | |
54cf91dc | 1404 | /* and update the user's relocation entry */ |
ad5d95e4 | 1405 | return relocate_entry(ev->vma, reloc, eb, target->vma); |
54cf91dc CW |
1406 | } |
1407 | ||
7d6236bb | 1408 | static int eb_relocate_vma(struct i915_execbuffer *eb, struct eb_vma *ev) |
54cf91dc | 1409 | { |
1d83f442 | 1410 | #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry)) |
2889caa9 | 1411 | struct drm_i915_gem_relocation_entry stack[N_RELOC(512)]; |
7d6236bb | 1412 | const struct drm_i915_gem_exec_object2 *entry = ev->exec; |
e94f7856 CW |
1413 | struct drm_i915_gem_relocation_entry __user *urelocs = |
1414 | u64_to_user_ptr(entry->relocs_ptr); | |
1415 | unsigned long remain = entry->relocation_count; | |
54cf91dc | 1416 | |
e94f7856 | 1417 | if (unlikely(remain > N_RELOC(ULONG_MAX))) |
2889caa9 | 1418 | return -EINVAL; |
ebc0808f | 1419 | |
2889caa9 CW |
1420 | /* |
1421 | * We must check that the entire relocation array is safe | |
1422 | * to read. However, if the array is not writable the user loses | |
1423 | * the updated relocation values. | |
1424 | */ | |
e94f7856 | 1425 | if (unlikely(!access_ok(urelocs, remain * sizeof(*urelocs)))) |
2889caa9 CW |
1426 | return -EFAULT; |
1427 | ||
1428 | do { | |
1429 | struct drm_i915_gem_relocation_entry *r = stack; | |
1430 | unsigned int count = | |
e94f7856 | 1431 | min_t(unsigned long, remain, ARRAY_SIZE(stack)); |
2889caa9 | 1432 | unsigned int copied; |
1d83f442 | 1433 | |
2889caa9 CW |
1434 | /* |
1435 | * This is the fast path and we cannot handle a pagefault | |
ebc0808f CW |
1436 | * whilst holding the struct mutex lest the user pass in the |
1437 | * relocations contained within a mmaped bo. For in such a case | |
1438 | * we, the page fault handler would call i915_gem_fault() and | |
1439 | * we would try to acquire the struct mutex again. Obviously | |
1440 | * this is bad and so lockdep complains vehemently. | |
1441 | */ | |
fd1500fc ML |
1442 | pagefault_disable(); |
1443 | copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0])); | |
1444 | pagefault_enable(); | |
ad5d95e4 DA |
1445 | if (unlikely(copied)) { |
1446 | remain = -EFAULT; | |
1447 | goto out; | |
1448 | } | |
54cf91dc | 1449 | |
2889caa9 | 1450 | remain -= count; |
1d83f442 | 1451 | do { |
7d6236bb | 1452 | u64 offset = eb_relocate_entry(eb, ev, r); |
54cf91dc | 1453 | |
2889caa9 CW |
1454 | if (likely(offset == 0)) { |
1455 | } else if ((s64)offset < 0) { | |
ad5d95e4 DA |
1456 | remain = (int)offset; |
1457 | goto out; | |
2889caa9 CW |
1458 | } else { |
1459 | /* | |
1460 | * Note that reporting an error now | |
1461 | * leaves everything in an inconsistent | |
1462 | * state as we have *already* changed | |
1463 | * the relocation value inside the | |
1464 | * object. As we have not changed the | |
1465 | * reloc.presumed_offset or will not | |
1466 | * change the execobject.offset, on the | |
1467 | * call we may not rewrite the value | |
1468 | * inside the object, leaving it | |
1469 | * dangling and causing a GPU hang. Unless | |
1470 | * userspace dynamically rebuilds the | |
1471 | * relocations on each execbuf rather than | |
1472 | * presume a static tree. | |
1473 | * | |
1474 | * We did previously check if the relocations | |
1475 | * were writable (access_ok), an error now | |
1476 | * would be a strange race with mprotect, | |
1477 | * having already demonstrated that we | |
1478 | * can read from this userspace address. | |
1479 | */ | |
1480 | offset = gen8_canonical_addr(offset & ~UPDATE); | |
97a37c91 CW |
1481 | __put_user(offset, |
1482 | &urelocs[r - stack].presumed_offset); | |
1d83f442 | 1483 | } |
2889caa9 CW |
1484 | } while (r++, --count); |
1485 | urelocs += ARRAY_SIZE(stack); | |
1486 | } while (remain); | |
ad5d95e4 | 1487 | out: |
c43ce123 | 1488 | reloc_cache_reset(&eb->reloc_cache, eb); |
ad5d95e4 | 1489 | return remain; |
54cf91dc CW |
1490 | } |
1491 | ||
fd1500fc ML |
1492 | static int |
1493 | eb_relocate_vma_slow(struct i915_execbuffer *eb, struct eb_vma *ev) | |
54cf91dc | 1494 | { |
fd1500fc ML |
1495 | const struct drm_i915_gem_exec_object2 *entry = ev->exec; |
1496 | struct drm_i915_gem_relocation_entry *relocs = | |
1497 | u64_to_ptr(typeof(*relocs), entry->relocs_ptr); | |
1498 | unsigned int i; | |
003d8b91 CW |
1499 | int err; |
1500 | ||
fd1500fc ML |
1501 | for (i = 0; i < entry->relocation_count; i++) { |
1502 | u64 offset = eb_relocate_entry(eb, ev, &relocs[i]); | |
003d8b91 | 1503 | |
fd1500fc ML |
1504 | if ((s64)offset < 0) { |
1505 | err = (int)offset; | |
1506 | goto err; | |
1507 | } | |
ef398881 | 1508 | } |
fd1500fc ML |
1509 | err = 0; |
1510 | err: | |
c43ce123 | 1511 | reloc_cache_reset(&eb->reloc_cache, eb); |
fd1500fc ML |
1512 | return err; |
1513 | } | |
2889caa9 | 1514 | |
fd1500fc ML |
1515 | static int check_relocations(const struct drm_i915_gem_exec_object2 *entry) |
1516 | { | |
1517 | const char __user *addr, *end; | |
1518 | unsigned long size; | |
1519 | char __maybe_unused c; | |
2889caa9 | 1520 | |
fd1500fc ML |
1521 | size = entry->relocation_count; |
1522 | if (size == 0) | |
1523 | return 0; | |
0e97fbb0 | 1524 | |
fd1500fc ML |
1525 | if (size > N_RELOC(ULONG_MAX)) |
1526 | return -EINVAL; | |
2889caa9 | 1527 | |
fd1500fc ML |
1528 | addr = u64_to_user_ptr(entry->relocs_ptr); |
1529 | size *= sizeof(struct drm_i915_gem_relocation_entry); | |
1530 | if (!access_ok(addr, size)) | |
1531 | return -EFAULT; | |
1532 | ||
1533 | end = addr + size; | |
1534 | for (; addr < end; addr += PAGE_SIZE) { | |
1535 | int err = __get_user(c, addr); | |
1536 | if (err) | |
1537 | return err; | |
1538 | } | |
1539 | return __get_user(c, end - 1); | |
2889caa9 CW |
1540 | } |
1541 | ||
fd1500fc | 1542 | static int eb_copy_relocations(const struct i915_execbuffer *eb) |
2889caa9 | 1543 | { |
fd1500fc | 1544 | struct drm_i915_gem_relocation_entry *relocs; |
2889caa9 CW |
1545 | const unsigned int count = eb->buffer_count; |
1546 | unsigned int i; | |
fd1500fc | 1547 | int err; |
54cf91dc | 1548 | |
2889caa9 | 1549 | for (i = 0; i < count; i++) { |
fd1500fc ML |
1550 | const unsigned int nreloc = eb->exec[i].relocation_count; |
1551 | struct drm_i915_gem_relocation_entry __user *urelocs; | |
1552 | unsigned long size; | |
1553 | unsigned long copied; | |
6951e589 | 1554 | |
fd1500fc ML |
1555 | if (nreloc == 0) |
1556 | continue; | |
6951e589 | 1557 | |
fd1500fc ML |
1558 | err = check_relocations(&eb->exec[i]); |
1559 | if (err) | |
1560 | goto err; | |
6951e589 | 1561 | |
fd1500fc ML |
1562 | urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr); |
1563 | size = nreloc * sizeof(*relocs); | |
6951e589 | 1564 | |
fd1500fc ML |
1565 | relocs = kvmalloc_array(size, 1, GFP_KERNEL); |
1566 | if (!relocs) { | |
1567 | err = -ENOMEM; | |
1568 | goto err; | |
6951e589 | 1569 | } |
fd1500fc ML |
1570 | |
1571 | /* copy_from_user is limited to < 4GiB */ | |
1572 | copied = 0; | |
1573 | do { | |
1574 | unsigned int len = | |
1575 | min_t(u64, BIT_ULL(31), size - copied); | |
1576 | ||
1577 | if (__copy_from_user((char *)relocs + copied, | |
1578 | (char __user *)urelocs + copied, | |
1579 | len)) | |
1580 | goto end; | |
1581 | ||
1582 | copied += len; | |
1583 | } while (copied < size); | |
1584 | ||
1585 | /* | |
1586 | * As we do not update the known relocation offsets after | |
1587 | * relocating (due to the complexities in lock handling), | |
1588 | * we need to mark them as invalid now so that we force the | |
1589 | * relocation processing next time. Just in case the target | |
1590 | * object is evicted and then rebound into its old | |
1591 | * presumed_offset before the next execbuffer - if that | |
1592 | * happened we would make the mistake of assuming that the | |
1593 | * relocations were valid. | |
1594 | */ | |
1595 | if (!user_access_begin(urelocs, size)) | |
1596 | goto end; | |
1597 | ||
1598 | for (copied = 0; copied < nreloc; copied++) | |
1599 | unsafe_put_user(-1, | |
1600 | &urelocs[copied].presumed_offset, | |
1601 | end_user); | |
1602 | user_access_end(); | |
1603 | ||
1604 | eb->exec[i].relocs_ptr = (uintptr_t)relocs; | |
1605 | } | |
1606 | ||
1607 | return 0; | |
1608 | ||
1609 | end_user: | |
1610 | user_access_end(); | |
1611 | end: | |
1612 | kvfree(relocs); | |
1613 | err = -EFAULT; | |
1614 | err: | |
1615 | while (i--) { | |
1616 | relocs = u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr); | |
1617 | if (eb->exec[i].relocation_count) | |
1618 | kvfree(relocs); | |
1619 | } | |
1620 | return err; | |
1621 | } | |
1622 | ||
1623 | static int eb_prefault_relocations(const struct i915_execbuffer *eb) | |
1624 | { | |
1625 | const unsigned int count = eb->buffer_count; | |
1626 | unsigned int i; | |
1627 | ||
1628 | for (i = 0; i < count; i++) { | |
1629 | int err; | |
1630 | ||
1631 | err = check_relocations(&eb->exec[i]); | |
1632 | if (err) | |
1633 | return err; | |
1634 | } | |
1635 | ||
1636 | return 0; | |
1637 | } | |
1638 | ||
ed29c269 ML |
1639 | static int eb_reinit_userptr(struct i915_execbuffer *eb) |
1640 | { | |
1641 | const unsigned int count = eb->buffer_count; | |
1642 | unsigned int i; | |
1643 | int ret; | |
1644 | ||
1645 | if (likely(!(eb->args->flags & __EXEC_USERPTR_USED))) | |
1646 | return 0; | |
1647 | ||
1648 | for (i = 0; i < count; i++) { | |
1649 | struct eb_vma *ev = &eb->vma[i]; | |
1650 | ||
1651 | if (!i915_gem_object_is_userptr(ev->vma->obj)) | |
1652 | continue; | |
1653 | ||
1654 | ret = i915_gem_object_userptr_submit_init(ev->vma->obj); | |
1655 | if (ret) | |
1656 | return ret; | |
1657 | ||
1658 | ev->flags |= __EXEC_OBJECT_USERPTR_INIT; | |
1659 | } | |
1660 | ||
1661 | return 0; | |
1662 | } | |
1663 | ||
544460c3 | 1664 | static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb) |
fd1500fc ML |
1665 | { |
1666 | bool have_copy = false; | |
1667 | struct eb_vma *ev; | |
1668 | int err = 0; | |
1669 | ||
1670 | repeat: | |
1671 | if (signal_pending(current)) { | |
1672 | err = -ERESTARTSYS; | |
1673 | goto out; | |
6951e589 | 1674 | } |
fd1500fc | 1675 | |
c43ce123 | 1676 | /* We may process another execbuffer during the unlock... */ |
b4b9731b | 1677 | eb_release_vmas(eb, false); |
c43ce123 ML |
1678 | i915_gem_ww_ctx_fini(&eb->ww); |
1679 | ||
fd1500fc ML |
1680 | /* |
1681 | * We take 3 passes through the slowpatch. | |
1682 | * | |
1683 | * 1 - we try to just prefault all the user relocation entries and | |
1684 | * then attempt to reuse the atomic pagefault disabled fast path again. | |
1685 | * | |
1686 | * 2 - we copy the user entries to a local buffer here outside of the | |
1687 | * local and allow ourselves to wait upon any rendering before | |
1688 | * relocations | |
1689 | * | |
1690 | * 3 - we already have a local copy of the relocation entries, but | |
1691 | * were interrupted (EAGAIN) whilst waiting for the objects, try again. | |
1692 | */ | |
1693 | if (!err) { | |
1694 | err = eb_prefault_relocations(eb); | |
1695 | } else if (!have_copy) { | |
1696 | err = eb_copy_relocations(eb); | |
1697 | have_copy = err == 0; | |
1698 | } else { | |
1699 | cond_resched(); | |
1700 | err = 0; | |
1701 | } | |
1702 | ||
2bf541ff | 1703 | if (!err) |
ed29c269 | 1704 | err = eb_reinit_userptr(eb); |
fd1500fc | 1705 | |
c43ce123 | 1706 | i915_gem_ww_ctx_init(&eb->ww, true); |
fd1500fc ML |
1707 | if (err) |
1708 | goto out; | |
1709 | ||
c43ce123 ML |
1710 | /* reacquire the objects */ |
1711 | repeat_validate: | |
544460c3 MB |
1712 | err = eb_pin_engine(eb, false); |
1713 | if (err) | |
2bf541ff | 1714 | goto err; |
2bf541ff | 1715 | |
c43ce123 | 1716 | err = eb_validate_vmas(eb); |
fd1500fc | 1717 | if (err) |
c43ce123 ML |
1718 | goto err; |
1719 | ||
544460c3 | 1720 | GEM_BUG_ON(!eb->batches[0]); |
fd1500fc ML |
1721 | |
1722 | list_for_each_entry(ev, &eb->relocs, reloc_link) { | |
1723 | if (!have_copy) { | |
fd1500fc | 1724 | err = eb_relocate_vma(eb, ev); |
fd1500fc ML |
1725 | if (err) |
1726 | break; | |
1727 | } else { | |
1728 | err = eb_relocate_vma_slow(eb, ev); | |
1729 | if (err) | |
1730 | break; | |
1731 | } | |
1732 | } | |
1733 | ||
c43ce123 ML |
1734 | if (err == -EDEADLK) |
1735 | goto err; | |
1736 | ||
fd1500fc ML |
1737 | if (err && !have_copy) |
1738 | goto repeat; | |
1739 | ||
1740 | if (err) | |
1741 | goto err; | |
1742 | ||
8e4ba491 ML |
1743 | /* as last step, parse the command buffer */ |
1744 | err = eb_parse(eb); | |
1745 | if (err) | |
1746 | goto err; | |
1747 | ||
fd1500fc ML |
1748 | /* |
1749 | * Leave the user relocations as are, this is the painfully slow path, | |
1750 | * and we want to avoid the complication of dropping the lock whilst | |
1751 | * having buffers reserved in the aperture and so causing spurious | |
1752 | * ENOSPC for random operations. | |
1753 | */ | |
1754 | ||
1755 | err: | |
c43ce123 | 1756 | if (err == -EDEADLK) { |
b4b9731b | 1757 | eb_release_vmas(eb, false); |
c43ce123 ML |
1758 | err = i915_gem_ww_ctx_backoff(&eb->ww); |
1759 | if (!err) | |
1760 | goto repeat_validate; | |
1761 | } | |
1762 | ||
fd1500fc ML |
1763 | if (err == -EAGAIN) |
1764 | goto repeat; | |
1765 | ||
1766 | out: | |
1767 | if (have_copy) { | |
1768 | const unsigned int count = eb->buffer_count; | |
1769 | unsigned int i; | |
1770 | ||
1771 | for (i = 0; i < count; i++) { | |
1772 | const struct drm_i915_gem_exec_object2 *entry = | |
1773 | &eb->exec[i]; | |
1774 | struct drm_i915_gem_relocation_entry *relocs; | |
1775 | ||
1776 | if (!entry->relocation_count) | |
1777 | continue; | |
1778 | ||
1779 | relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr); | |
1780 | kvfree(relocs); | |
1781 | } | |
1782 | } | |
1783 | ||
1784 | return err; | |
1785 | } | |
1786 | ||
8e4ba491 | 1787 | static int eb_relocate_parse(struct i915_execbuffer *eb) |
54cf91dc | 1788 | { |
003d8b91 | 1789 | int err; |
2bf541ff | 1790 | bool throttle = true; |
003d8b91 | 1791 | |
c43ce123 | 1792 | retry: |
544460c3 MB |
1793 | err = eb_pin_engine(eb, throttle); |
1794 | if (err) { | |
2bf541ff ML |
1795 | if (err != -EDEADLK) |
1796 | return err; | |
1797 | ||
1798 | goto err; | |
1799 | } | |
1800 | ||
2bf541ff ML |
1801 | /* only throttle once, even if we didn't need to throttle */ |
1802 | throttle = false; | |
1803 | ||
c43ce123 ML |
1804 | err = eb_validate_vmas(eb); |
1805 | if (err == -EAGAIN) | |
1806 | goto slow; | |
1807 | else if (err) | |
1808 | goto err; | |
2889caa9 CW |
1809 | |
1810 | /* The objects are in their final locations, apply the relocations. */ | |
1811 | if (eb->args->flags & __EXEC_HAS_RELOC) { | |
7d6236bb | 1812 | struct eb_vma *ev; |
2889caa9 | 1813 | |
7d6236bb | 1814 | list_for_each_entry(ev, &eb->relocs, reloc_link) { |
7dc8f114 CW |
1815 | err = eb_relocate_vma(eb, ev); |
1816 | if (err) | |
fd1500fc | 1817 | break; |
2889caa9 | 1818 | } |
fd1500fc | 1819 | |
c43ce123 ML |
1820 | if (err == -EDEADLK) |
1821 | goto err; | |
1822 | else if (err) | |
1823 | goto slow; | |
1824 | } | |
1825 | ||
1826 | if (!err) | |
1827 | err = eb_parse(eb); | |
1828 | ||
1829 | err: | |
1830 | if (err == -EDEADLK) { | |
b4b9731b | 1831 | eb_release_vmas(eb, false); |
c43ce123 ML |
1832 | err = i915_gem_ww_ctx_backoff(&eb->ww); |
1833 | if (!err) | |
1834 | goto retry; | |
2889caa9 CW |
1835 | } |
1836 | ||
c43ce123 ML |
1837 | return err; |
1838 | ||
1839 | slow: | |
544460c3 | 1840 | err = eb_relocate_parse_slow(eb); |
c43ce123 ML |
1841 | if (err) |
1842 | /* | |
1843 | * If the user expects the execobject.offset and | |
1844 | * reloc.presumed_offset to be an exact match, | |
1845 | * as for using NO_RELOC, then we cannot update | |
1846 | * the execobject.offset until we have completed | |
1847 | * relocation. | |
1848 | */ | |
1849 | eb->args->flags &= ~__EXEC_HAS_RELOC; | |
1850 | ||
1851 | return err; | |
2889caa9 CW |
1852 | } |
1853 | ||
544460c3 MB |
1854 | /* |
1855 | * Using two helper loops for the order of which requests / batches are created | |
1856 | * and added the to backend. Requests are created in order from the parent to | |
1857 | * the last child. Requests are added in the reverse order, from the last child | |
1858 | * to parent. This is done for locking reasons as the timeline lock is acquired | |
1859 | * during request creation and released when the request is added to the | |
1860 | * backend. To make lockdep happy (see intel_context_timeline_lock) this must be | |
1861 | * the ordering. | |
1862 | */ | |
1863 | #define for_each_batch_create_order(_eb, _i) \ | |
1864 | for ((_i) = 0; (_i) < (_eb)->num_batches; ++(_i)) | |
1865 | #define for_each_batch_add_order(_eb, _i) \ | |
1866 | BUILD_BUG_ON(!typecheck(int, _i)); \ | |
1867 | for ((_i) = (_eb)->num_batches - 1; (_i) >= 0; --(_i)) | |
1868 | ||
1869 | static struct i915_request * | |
1870 | eb_find_first_request_added(struct i915_execbuffer *eb) | |
1871 | { | |
1872 | int i; | |
1873 | ||
1874 | for_each_batch_add_order(eb, i) | |
1875 | if (eb->requests[i]) | |
1876 | return eb->requests[i]; | |
1877 | ||
1878 | GEM_BUG_ON("Request not found"); | |
1879 | ||
1880 | return NULL; | |
1881 | } | |
1882 | ||
2889caa9 CW |
1883 | static int eb_move_to_gpu(struct i915_execbuffer *eb) |
1884 | { | |
1885 | const unsigned int count = eb->buffer_count; | |
c43ce123 | 1886 | unsigned int i = count; |
544460c3 | 1887 | int err = 0, j; |
6951e589 CW |
1888 | |
1889 | while (i--) { | |
7d6236bb CW |
1890 | struct eb_vma *ev = &eb->vma[i]; |
1891 | struct i915_vma *vma = ev->vma; | |
1892 | unsigned int flags = ev->flags; | |
27173f1f | 1893 | struct drm_i915_gem_object *obj = vma->obj; |
03ade511 | 1894 | |
6951e589 CW |
1895 | assert_vma_held(vma); |
1896 | ||
c7c6e46f | 1897 | if (flags & EXEC_OBJECT_CAPTURE) { |
e61e0f51 | 1898 | struct i915_capture_list *capture; |
b0fd47ad | 1899 | |
544460c3 MB |
1900 | for_each_batch_create_order(eb, j) { |
1901 | if (!eb->requests[j]) | |
1902 | break; | |
1903 | ||
1904 | capture = kmalloc(sizeof(*capture), GFP_KERNEL); | |
1905 | if (capture) { | |
1906 | capture->next = | |
1907 | eb->requests[j]->capture_list; | |
1908 | capture->vma = vma; | |
1909 | eb->requests[j]->capture_list = capture; | |
1910 | } | |
6951e589 | 1911 | } |
b0fd47ad CW |
1912 | } |
1913 | ||
b8f55be6 CW |
1914 | /* |
1915 | * If the GPU is not _reading_ through the CPU cache, we need | |
1916 | * to make sure that any writes (both previous GPU writes from | |
1917 | * before a change in snooping levels and normal CPU writes) | |
1918 | * caught in that cache are flushed to main memory. | |
1919 | * | |
1920 | * We want to say | |
1921 | * obj->cache_dirty && | |
1922 | * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ) | |
1923 | * but gcc's optimiser doesn't handle that as well and emits | |
1924 | * two jumps instead of one. Maybe one day... | |
df94fd05 MA |
1925 | * |
1926 | * FIXME: There is also sync flushing in set_pages(), which | |
1927 | * serves a different purpose(some of the time at least). | |
1928 | * | |
1929 | * We should consider: | |
1930 | * | |
1931 | * 1. Rip out the async flush code. | |
1932 | * | |
1933 | * 2. Or make the sync flushing use the async clflush path | |
1934 | * using mandatory fences underneath. Currently the below | |
1935 | * async flush happens after we bind the object. | |
b8f55be6 CW |
1936 | */ |
1937 | if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) { | |
0f46daa1 | 1938 | if (i915_gem_clflush_object(obj, 0)) |
c7c6e46f | 1939 | flags &= ~EXEC_OBJECT_ASYNC; |
0f46daa1 CW |
1940 | } |
1941 | ||
544460c3 | 1942 | /* We only need to await on the first request */ |
6951e589 CW |
1943 | if (err == 0 && !(flags & EXEC_OBJECT_ASYNC)) { |
1944 | err = i915_request_await_object | |
544460c3 MB |
1945 | (eb_find_first_request_added(eb), obj, |
1946 | flags & EXEC_OBJECT_WRITE); | |
6951e589 | 1947 | } |
2889caa9 | 1948 | |
544460c3 MB |
1949 | for_each_batch_add_order(eb, j) { |
1950 | if (err) | |
1951 | break; | |
1952 | if (!eb->requests[j]) | |
1953 | continue; | |
1954 | ||
1955 | err = _i915_vma_move_to_active(vma, eb->requests[j], | |
1956 | j ? NULL : | |
1957 | eb->composite_fence ? | |
1958 | eb->composite_fence : | |
1959 | &eb->requests[j]->fence, | |
1960 | flags | __EXEC_OBJECT_NO_RESERVE); | |
1961 | } | |
c59a333f | 1962 | } |
0f1dd022 | 1963 | |
ed29c269 ML |
1964 | #ifdef CONFIG_MMU_NOTIFIER |
1965 | if (!err && (eb->args->flags & __EXEC_USERPTR_USED)) { | |
b4b9731b | 1966 | read_lock(&eb->i915->mm.notifier_lock); |
ed29c269 ML |
1967 | |
1968 | /* | |
1969 | * count is always at least 1, otherwise __EXEC_USERPTR_USED | |
1970 | * could not have been set | |
1971 | */ | |
1972 | for (i = 0; i < count; i++) { | |
1973 | struct eb_vma *ev = &eb->vma[i]; | |
1974 | struct drm_i915_gem_object *obj = ev->vma->obj; | |
1975 | ||
1976 | if (!i915_gem_object_is_userptr(obj)) | |
1977 | continue; | |
1978 | ||
1979 | err = i915_gem_object_userptr_submit_done(obj); | |
1980 | if (err) | |
1981 | break; | |
1982 | } | |
1983 | ||
b4b9731b | 1984 | read_unlock(&eb->i915->mm.notifier_lock); |
ed29c269 ML |
1985 | } |
1986 | #endif | |
1987 | ||
6951e589 CW |
1988 | if (unlikely(err)) |
1989 | goto err_skip; | |
1990 | ||
dcd79934 | 1991 | /* Unconditionally flush any chipset caches (for streaming writes). */ |
544460c3 | 1992 | intel_gt_chipset_flush(eb->gt); |
2113184c | 1993 | return 0; |
6951e589 CW |
1994 | |
1995 | err_skip: | |
544460c3 MB |
1996 | for_each_batch_create_order(eb, j) { |
1997 | if (!eb->requests[j]) | |
1998 | break; | |
1999 | ||
2000 | i915_request_set_error_once(eb->requests[j], err); | |
2001 | } | |
6951e589 | 2002 | return err; |
54cf91dc CW |
2003 | } |
2004 | ||
00aff3f6 | 2005 | static int i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec) |
54cf91dc | 2006 | { |
650bc635 | 2007 | if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS) |
00aff3f6 | 2008 | return -EINVAL; |
ed5982e6 | 2009 | |
2f5945bc | 2010 | /* Kernel clipping was a DRI1 misfeature */ |
cda9edd0 LL |
2011 | if (!(exec->flags & (I915_EXEC_FENCE_ARRAY | |
2012 | I915_EXEC_USE_EXTENSIONS))) { | |
cf6e7bac | 2013 | if (exec->num_cliprects || exec->cliprects_ptr) |
00aff3f6 | 2014 | return -EINVAL; |
cf6e7bac | 2015 | } |
2f5945bc CW |
2016 | |
2017 | if (exec->DR4 == 0xffffffff) { | |
2018 | DRM_DEBUG("UXA submitting garbage DR4, fixing up\n"); | |
2019 | exec->DR4 = 0; | |
2020 | } | |
2021 | if (exec->DR1 || exec->DR4) | |
00aff3f6 | 2022 | return -EINVAL; |
2f5945bc CW |
2023 | |
2024 | if ((exec->batch_start_offset | exec->batch_len) & 0x7) | |
00aff3f6 | 2025 | return -EINVAL; |
2f5945bc | 2026 | |
00aff3f6 | 2027 | return 0; |
54cf91dc CW |
2028 | } |
2029 | ||
e61e0f51 | 2030 | static int i915_reset_gen7_sol_offsets(struct i915_request *rq) |
ae662d31 | 2031 | { |
73dec95e TU |
2032 | u32 *cs; |
2033 | int i; | |
ae662d31 | 2034 | |
40e1956e | 2035 | if (GRAPHICS_VER(rq->engine->i915) != 7 || rq->engine->id != RCS0) { |
5a833995 | 2036 | drm_dbg(&rq->engine->i915->drm, "sol reset is gen7/rcs only\n"); |
9d662da8 DV |
2037 | return -EINVAL; |
2038 | } | |
ae662d31 | 2039 | |
e61e0f51 | 2040 | cs = intel_ring_begin(rq, 4 * 2 + 2); |
73dec95e TU |
2041 | if (IS_ERR(cs)) |
2042 | return PTR_ERR(cs); | |
ae662d31 | 2043 | |
2889caa9 | 2044 | *cs++ = MI_LOAD_REGISTER_IMM(4); |
ae662d31 | 2045 | for (i = 0; i < 4; i++) { |
73dec95e TU |
2046 | *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i)); |
2047 | *cs++ = 0; | |
ae662d31 | 2048 | } |
2889caa9 | 2049 | *cs++ = MI_NOOP; |
e61e0f51 | 2050 | intel_ring_advance(rq, cs); |
ae662d31 EA |
2051 | |
2052 | return 0; | |
2053 | } | |
2054 | ||
4f7af194 | 2055 | static struct i915_vma * |
47b08693 ML |
2056 | shadow_batch_pin(struct i915_execbuffer *eb, |
2057 | struct drm_i915_gem_object *obj, | |
32d94048 CW |
2058 | struct i915_address_space *vm, |
2059 | unsigned int flags) | |
4f7af194 | 2060 | { |
b291ce0a | 2061 | struct i915_vma *vma; |
b291ce0a | 2062 | int err; |
4f7af194 | 2063 | |
b291ce0a CW |
2064 | vma = i915_vma_instance(obj, vm, NULL); |
2065 | if (IS_ERR(vma)) | |
2066 | return vma; | |
2067 | ||
47b08693 | 2068 | err = i915_vma_pin_ww(vma, &eb->ww, 0, 0, flags); |
b291ce0a CW |
2069 | if (err) |
2070 | return ERR_PTR(err); | |
2071 | ||
2072 | return vma; | |
4f7af194 JB |
2073 | } |
2074 | ||
47b08693 ML |
2075 | static struct i915_vma *eb_dispatch_secure(struct i915_execbuffer *eb, struct i915_vma *vma) |
2076 | { | |
2077 | /* | |
2078 | * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure | |
2079 | * batch" bit. Hence we need to pin secure batches into the global gtt. | |
2080 | * hsw should have this fixed, but bdw mucks it up again. */ | |
2081 | if (eb->batch_flags & I915_DISPATCH_SECURE) | |
2082 | return i915_gem_object_ggtt_pin_ww(vma->obj, &eb->ww, NULL, 0, 0, 0); | |
2083 | ||
2084 | return NULL; | |
2085 | } | |
2086 | ||
51696691 | 2087 | static int eb_parse(struct i915_execbuffer *eb) |
71745376 | 2088 | { |
baa89ba3 | 2089 | struct drm_i915_private *i915 = eb->i915; |
c43ce123 | 2090 | struct intel_gt_buffer_pool_node *pool = eb->batch_pool; |
47b08693 | 2091 | struct i915_vma *shadow, *trampoline, *batch; |
d5e87821 | 2092 | unsigned long len; |
2889caa9 | 2093 | int err; |
71745376 | 2094 | |
47b08693 | 2095 | if (!eb_use_cmdparser(eb)) { |
544460c3 | 2096 | batch = eb_dispatch_secure(eb, eb->batches[0]->vma); |
47b08693 ML |
2097 | if (IS_ERR(batch)) |
2098 | return PTR_ERR(batch); | |
2099 | ||
2100 | goto secure_batch; | |
2101 | } | |
51696691 | 2102 | |
544460c3 MB |
2103 | if (intel_context_is_parallel(eb->context)) |
2104 | return -EINVAL; | |
2105 | ||
2106 | len = eb->batch_len[0]; | |
32d94048 CW |
2107 | if (!CMDPARSER_USES_GGTT(eb->i915)) { |
2108 | /* | |
2109 | * ppGTT backed shadow buffers must be mapped RO, to prevent | |
2110 | * post-scan tampering | |
2111 | */ | |
2112 | if (!eb->context->vm->has_read_only) { | |
baa89ba3 WK |
2113 | drm_dbg(&i915->drm, |
2114 | "Cannot prevent post-scan tampering without RO capable vm\n"); | |
32d94048 CW |
2115 | return -EINVAL; |
2116 | } | |
2117 | } else { | |
2118 | len += I915_CMD_PARSER_TRAMPOLINE_SIZE; | |
2119 | } | |
544460c3 | 2120 | if (unlikely(len < eb->batch_len[0])) /* last paranoid check of overflow */ |
d5e87821 | 2121 | return -EINVAL; |
32d94048 | 2122 | |
c43ce123 | 2123 | if (!pool) { |
544460c3 | 2124 | pool = intel_gt_get_buffer_pool(eb->gt, len, |
8f47c8c3 | 2125 | I915_MAP_WB); |
c43ce123 ML |
2126 | if (IS_ERR(pool)) |
2127 | return PTR_ERR(pool); | |
2128 | eb->batch_pool = pool; | |
2129 | } | |
71745376 | 2130 | |
c43ce123 ML |
2131 | err = i915_gem_object_lock(pool->obj, &eb->ww); |
2132 | if (err) | |
2133 | goto err; | |
71745376 | 2134 | |
47b08693 | 2135 | shadow = shadow_batch_pin(eb, pool->obj, eb->context->vm, PIN_USER); |
32d94048 CW |
2136 | if (IS_ERR(shadow)) { |
2137 | err = PTR_ERR(shadow); | |
f8c08d8f | 2138 | goto err; |
51696691 | 2139 | } |
c9398775 | 2140 | intel_gt_buffer_pool_mark_used(pool); |
32d94048 | 2141 | i915_gem_object_set_readonly(shadow->obj); |
57a78ca4 | 2142 | shadow->private = pool; |
32d94048 CW |
2143 | |
2144 | trampoline = NULL; | |
2145 | if (CMDPARSER_USES_GGTT(eb->i915)) { | |
2146 | trampoline = shadow; | |
2147 | ||
47b08693 | 2148 | shadow = shadow_batch_pin(eb, pool->obj, |
544460c3 | 2149 | &eb->gt->ggtt->vm, |
32d94048 CW |
2150 | PIN_GLOBAL); |
2151 | if (IS_ERR(shadow)) { | |
2152 | err = PTR_ERR(shadow); | |
2153 | shadow = trampoline; | |
2154 | goto err_shadow; | |
2155 | } | |
57a78ca4 | 2156 | shadow->private = pool; |
32d94048 CW |
2157 | |
2158 | eb->batch_flags |= I915_DISPATCH_SECURE; | |
2159 | } | |
f8c08d8f | 2160 | |
47b08693 ML |
2161 | batch = eb_dispatch_secure(eb, shadow); |
2162 | if (IS_ERR(batch)) { | |
2163 | err = PTR_ERR(batch); | |
2164 | goto err_trampoline; | |
2165 | } | |
2166 | ||
93b71330 JE |
2167 | err = dma_resv_reserve_shared(shadow->resv, 1); |
2168 | if (err) | |
2169 | goto err_trampoline; | |
2170 | ||
544460c3 MB |
2171 | err = intel_engine_cmd_parser(eb->context->engine, |
2172 | eb->batches[0]->vma, | |
93b71330 | 2173 | eb->batch_start_offset, |
544460c3 | 2174 | eb->batch_len[0], |
93b71330 | 2175 | shadow, trampoline); |
32d94048 | 2176 | if (err) |
47b08693 | 2177 | goto err_unpin_batch; |
71745376 | 2178 | |
544460c3 MB |
2179 | eb->batches[0] = &eb->vma[eb->buffer_count++]; |
2180 | eb->batches[0]->vma = i915_vma_get(shadow); | |
2181 | eb->batches[0]->flags = __EXEC_OBJECT_HAS_PIN; | |
71745376 | 2182 | |
32d94048 | 2183 | eb->trampoline = trampoline; |
4f7af194 | 2184 | eb->batch_start_offset = 0; |
4f7af194 | 2185 | |
47b08693 ML |
2186 | secure_batch: |
2187 | if (batch) { | |
544460c3 MB |
2188 | if (intel_context_is_parallel(eb->context)) |
2189 | return -EINVAL; | |
2190 | ||
2191 | eb->batches[0] = &eb->vma[eb->buffer_count++]; | |
2192 | eb->batches[0]->flags = __EXEC_OBJECT_HAS_PIN; | |
2193 | eb->batches[0]->vma = i915_vma_get(batch); | |
47b08693 | 2194 | } |
51696691 | 2195 | return 0; |
b40d7378 | 2196 | |
47b08693 ML |
2197 | err_unpin_batch: |
2198 | if (batch) | |
2199 | i915_vma_unpin(batch); | |
32d94048 CW |
2200 | err_trampoline: |
2201 | if (trampoline) | |
2202 | i915_vma_unpin(trampoline); | |
2203 | err_shadow: | |
2204 | i915_vma_unpin(shadow); | |
b40d7378 | 2205 | err: |
51696691 | 2206 | return err; |
71745376 | 2207 | } |
5c6c6003 | 2208 | |
544460c3 MB |
2209 | static int eb_request_submit(struct i915_execbuffer *eb, |
2210 | struct i915_request *rq, | |
2211 | struct i915_vma *batch, | |
2212 | u64 batch_len) | |
78382593 | 2213 | { |
2889caa9 | 2214 | int err; |
78382593 | 2215 | |
544460c3 MB |
2216 | if (intel_context_nopreempt(rq->context)) |
2217 | __set_bit(I915_FENCE_FLAG_NOPREEMPT, &rq->fence.flags); | |
78382593 | 2218 | |
650bc635 | 2219 | if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) { |
544460c3 | 2220 | err = i915_reset_gen7_sol_offsets(rq); |
2889caa9 CW |
2221 | if (err) |
2222 | return err; | |
78382593 OM |
2223 | } |
2224 | ||
85474441 CW |
2225 | /* |
2226 | * After we completed waiting for other engines (using HW semaphores) | |
2227 | * then we can signal that this request/batch is ready to run. This | |
2228 | * allows us to determine if the batch is still waiting on the GPU | |
2229 | * or actually running by checking the breadcrumb. | |
2230 | */ | |
544460c3 MB |
2231 | if (rq->context->engine->emit_init_breadcrumb) { |
2232 | err = rq->context->engine->emit_init_breadcrumb(rq); | |
85474441 CW |
2233 | if (err) |
2234 | return err; | |
2235 | } | |
2236 | ||
544460c3 MB |
2237 | err = rq->context->engine->emit_bb_start(rq, |
2238 | batch->node.start + | |
2239 | eb->batch_start_offset, | |
2240 | batch_len, | |
2241 | eb->batch_flags); | |
2889caa9 CW |
2242 | if (err) |
2243 | return err; | |
78382593 | 2244 | |
32d94048 | 2245 | if (eb->trampoline) { |
544460c3 | 2246 | GEM_BUG_ON(intel_context_is_parallel(rq->context)); |
32d94048 | 2247 | GEM_BUG_ON(eb->batch_start_offset); |
544460c3 MB |
2248 | err = rq->context->engine->emit_bb_start(rq, |
2249 | eb->trampoline->node.start + | |
2250 | batch_len, 0, 0); | |
32d94048 CW |
2251 | if (err) |
2252 | return err; | |
2253 | } | |
2254 | ||
2f5945bc | 2255 | return 0; |
78382593 OM |
2256 | } |
2257 | ||
544460c3 MB |
2258 | static int eb_submit(struct i915_execbuffer *eb) |
2259 | { | |
2260 | unsigned int i; | |
2261 | int err; | |
2262 | ||
2263 | err = eb_move_to_gpu(eb); | |
2264 | ||
2265 | for_each_batch_create_order(eb, i) { | |
2266 | if (!eb->requests[i]) | |
2267 | break; | |
2268 | ||
2269 | trace_i915_request_queue(eb->requests[i], eb->batch_flags); | |
2270 | if (!err) | |
2271 | err = eb_request_submit(eb, eb->requests[i], | |
2272 | eb->batches[i]->vma, | |
2273 | eb->batch_len[i]); | |
2274 | } | |
2275 | ||
2276 | return err; | |
2277 | } | |
2278 | ||
d5b2a3a4 CW |
2279 | static int num_vcs_engines(const struct drm_i915_private *i915) |
2280 | { | |
bb80d878 | 2281 | return hweight_long(VDBOX_MASK(&i915->gt)); |
d5b2a3a4 CW |
2282 | } |
2283 | ||
204bcfef | 2284 | /* |
a8ebba75 | 2285 | * Find one BSD ring to dispatch the corresponding BSD command. |
c80ff16e | 2286 | * The engine index is returned. |
a8ebba75 | 2287 | */ |
de1add36 | 2288 | static unsigned int |
c80ff16e CW |
2289 | gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv, |
2290 | struct drm_file *file) | |
a8ebba75 | 2291 | { |
a8ebba75 ZY |
2292 | struct drm_i915_file_private *file_priv = file->driver_priv; |
2293 | ||
de1add36 | 2294 | /* Check whether the file_priv has already selected one ring. */ |
6f633402 | 2295 | if ((int)file_priv->bsd_engine < 0) |
1a07e86c CW |
2296 | file_priv->bsd_engine = |
2297 | get_random_int() % num_vcs_engines(dev_priv); | |
d23db88c | 2298 | |
c80ff16e | 2299 | return file_priv->bsd_engine; |
d23db88c CW |
2300 | } |
2301 | ||
5e2a0419 | 2302 | static const enum intel_engine_id user_ring_map[] = { |
8a68d464 CW |
2303 | [I915_EXEC_DEFAULT] = RCS0, |
2304 | [I915_EXEC_RENDER] = RCS0, | |
2305 | [I915_EXEC_BLT] = BCS0, | |
2306 | [I915_EXEC_BSD] = VCS0, | |
2307 | [I915_EXEC_VEBOX] = VECS0 | |
de1add36 TU |
2308 | }; |
2309 | ||
2bf541ff | 2310 | static struct i915_request *eb_throttle(struct i915_execbuffer *eb, struct intel_context *ce) |
e5dadff4 CW |
2311 | { |
2312 | struct intel_ring *ring = ce->ring; | |
2313 | struct intel_timeline *tl = ce->timeline; | |
2314 | struct i915_request *rq; | |
2315 | ||
2316 | /* | |
2317 | * Completely unscientific finger-in-the-air estimates for suitable | |
2318 | * maximum user request size (to avoid blocking) and then backoff. | |
2319 | */ | |
2320 | if (intel_ring_update_space(ring) >= PAGE_SIZE) | |
2321 | return NULL; | |
2322 | ||
2323 | /* | |
2324 | * Find a request that after waiting upon, there will be at least half | |
2325 | * the ring available. The hysteresis allows us to compete for the | |
2326 | * shared ring and should mean that we sleep less often prior to | |
2327 | * claiming our resources, but not so long that the ring completely | |
2328 | * drains before we can submit our next request. | |
2329 | */ | |
2330 | list_for_each_entry(rq, &tl->requests, link) { | |
2331 | if (rq->ring != ring) | |
2332 | continue; | |
2333 | ||
2334 | if (__intel_ring_space(rq->postfix, | |
2335 | ring->emit, ring->size) > ring->size / 2) | |
2336 | break; | |
2337 | } | |
2338 | if (&rq->link == &tl->requests) | |
2339 | return NULL; /* weird, we will check again later for real */ | |
2340 | ||
2341 | return i915_request_get(rq); | |
2342 | } | |
2343 | ||
544460c3 MB |
2344 | static int eb_pin_timeline(struct i915_execbuffer *eb, struct intel_context *ce, |
2345 | bool throttle) | |
e5dadff4 CW |
2346 | { |
2347 | struct intel_timeline *tl; | |
2bf541ff | 2348 | struct i915_request *rq = NULL; |
8f2a1057 | 2349 | |
a4e57f90 CW |
2350 | /* |
2351 | * Take a local wakeref for preparing to dispatch the execbuf as | |
2352 | * we expect to access the hardware fairly frequently in the | |
2353 | * process, and require the engine to be kept awake between accesses. | |
2354 | * Upon dispatch, we acquire another prolonged wakeref that we hold | |
2355 | * until the timeline is idle, which in turn releases the wakeref | |
2356 | * taken on the engine, and the parent device. | |
2357 | */ | |
e5dadff4 | 2358 | tl = intel_context_timeline_lock(ce); |
544460c3 MB |
2359 | if (IS_ERR(tl)) |
2360 | return PTR_ERR(tl); | |
a4e57f90 CW |
2361 | |
2362 | intel_context_enter(ce); | |
2bf541ff ML |
2363 | if (throttle) |
2364 | rq = eb_throttle(eb, ce); | |
e5dadff4 CW |
2365 | intel_context_timeline_unlock(tl); |
2366 | ||
544460c3 MB |
2367 | if (rq) { |
2368 | bool nonblock = eb->file->filp->f_flags & O_NONBLOCK; | |
2369 | long timeout = nonblock ? 0 : MAX_SCHEDULE_TIMEOUT; | |
2370 | ||
2371 | if (i915_request_wait(rq, I915_WAIT_INTERRUPTIBLE, | |
2372 | timeout) < 0) { | |
2373 | i915_request_put(rq); | |
2374 | ||
2375 | tl = intel_context_timeline_lock(ce); | |
2376 | intel_context_exit(ce); | |
2377 | intel_context_timeline_unlock(tl); | |
2378 | ||
2379 | if (nonblock) | |
2380 | return -EWOULDBLOCK; | |
2381 | else | |
2382 | return -EINTR; | |
2383 | } | |
2384 | i915_request_put(rq); | |
2385 | } | |
2386 | ||
2387 | return 0; | |
2388 | } | |
2389 | ||
2390 | static int eb_pin_engine(struct i915_execbuffer *eb, bool throttle) | |
2391 | { | |
2392 | struct intel_context *ce = eb->context, *child; | |
2393 | int err; | |
2394 | int i = 0, j = 0; | |
2395 | ||
2396 | GEM_BUG_ON(eb->args->flags & __EXEC_ENGINE_PINNED); | |
2397 | ||
2398 | if (unlikely(intel_context_is_banned(ce))) | |
2399 | return -EIO; | |
2400 | ||
2401 | /* | |
2402 | * Pinning the contexts may generate requests in order to acquire | |
2403 | * GGTT space, so do this first before we reserve a seqno for | |
2404 | * ourselves. | |
2405 | */ | |
2406 | err = intel_context_pin_ww(ce, &eb->ww); | |
2407 | if (err) | |
2408 | return err; | |
2409 | for_each_child(ce, child) { | |
2410 | err = intel_context_pin_ww(child, &eb->ww); | |
2411 | GEM_BUG_ON(err); /* perma-pinned should incr a counter */ | |
2412 | } | |
2413 | ||
2414 | for_each_child(ce, child) { | |
2415 | err = eb_pin_timeline(eb, child, throttle); | |
2416 | if (err) | |
2417 | goto unwind; | |
2418 | ++i; | |
2419 | } | |
2420 | err = eb_pin_timeline(eb, ce, throttle); | |
2421 | if (err) | |
2422 | goto unwind; | |
2423 | ||
2bf541ff | 2424 | eb->args->flags |= __EXEC_ENGINE_PINNED; |
544460c3 MB |
2425 | return 0; |
2426 | ||
2427 | unwind: | |
2428 | for_each_child(ce, child) { | |
2429 | if (j++ < i) { | |
2430 | mutex_lock(&child->timeline->mutex); | |
2431 | intel_context_exit(child); | |
2432 | mutex_unlock(&child->timeline->mutex); | |
2433 | } | |
2434 | } | |
2435 | for_each_child(ce, child) | |
2436 | intel_context_unpin(child); | |
2437 | intel_context_unpin(ce); | |
2438 | return err; | |
8f2a1057 CW |
2439 | } |
2440 | ||
e5dadff4 | 2441 | static void eb_unpin_engine(struct i915_execbuffer *eb) |
8f2a1057 | 2442 | { |
544460c3 | 2443 | struct intel_context *ce = eb->context, *child; |
a4e57f90 | 2444 | |
2bf541ff ML |
2445 | if (!(eb->args->flags & __EXEC_ENGINE_PINNED)) |
2446 | return; | |
2447 | ||
2448 | eb->args->flags &= ~__EXEC_ENGINE_PINNED; | |
2449 | ||
544460c3 MB |
2450 | for_each_child(ce, child) { |
2451 | mutex_lock(&child->timeline->mutex); | |
2452 | intel_context_exit(child); | |
2453 | mutex_unlock(&child->timeline->mutex); | |
2454 | ||
2455 | intel_context_unpin(child); | |
2456 | } | |
2457 | ||
2458 | mutex_lock(&ce->timeline->mutex); | |
a4e57f90 | 2459 | intel_context_exit(ce); |
544460c3 | 2460 | mutex_unlock(&ce->timeline->mutex); |
a4e57f90 | 2461 | |
2850748e | 2462 | intel_context_unpin(ce); |
8f2a1057 | 2463 | } |
de1add36 | 2464 | |
5e2a0419 | 2465 | static unsigned int |
b49a7d51 | 2466 | eb_select_legacy_ring(struct i915_execbuffer *eb) |
de1add36 | 2467 | { |
8f2a1057 | 2468 | struct drm_i915_private *i915 = eb->i915; |
b49a7d51 | 2469 | struct drm_i915_gem_execbuffer2 *args = eb->args; |
de1add36 | 2470 | unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK; |
de1add36 | 2471 | |
5e2a0419 CW |
2472 | if (user_ring_id != I915_EXEC_BSD && |
2473 | (args->flags & I915_EXEC_BSD_MASK)) { | |
baa89ba3 WK |
2474 | drm_dbg(&i915->drm, |
2475 | "execbuf with non bsd ring but with invalid " | |
2476 | "bsd dispatch flags: %d\n", (int)(args->flags)); | |
5e2a0419 | 2477 | return -1; |
de1add36 TU |
2478 | } |
2479 | ||
d5b2a3a4 | 2480 | if (user_ring_id == I915_EXEC_BSD && num_vcs_engines(i915) > 1) { |
de1add36 TU |
2481 | unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK; |
2482 | ||
2483 | if (bsd_idx == I915_EXEC_BSD_DEFAULT) { | |
b49a7d51 | 2484 | bsd_idx = gen8_dispatch_bsd_engine(i915, eb->file); |
de1add36 TU |
2485 | } else if (bsd_idx >= I915_EXEC_BSD_RING1 && |
2486 | bsd_idx <= I915_EXEC_BSD_RING2) { | |
d9da6aa0 | 2487 | bsd_idx >>= I915_EXEC_BSD_SHIFT; |
de1add36 TU |
2488 | bsd_idx--; |
2489 | } else { | |
baa89ba3 WK |
2490 | drm_dbg(&i915->drm, |
2491 | "execbuf with unknown bsd ring: %u\n", | |
2492 | bsd_idx); | |
5e2a0419 | 2493 | return -1; |
de1add36 TU |
2494 | } |
2495 | ||
5e2a0419 | 2496 | return _VCS(bsd_idx); |
de1add36 TU |
2497 | } |
2498 | ||
5e2a0419 | 2499 | if (user_ring_id >= ARRAY_SIZE(user_ring_map)) { |
baa89ba3 WK |
2500 | drm_dbg(&i915->drm, "execbuf with unknown ring: %u\n", |
2501 | user_ring_id); | |
5e2a0419 | 2502 | return -1; |
de1add36 TU |
2503 | } |
2504 | ||
5e2a0419 CW |
2505 | return user_ring_map[user_ring_id]; |
2506 | } | |
2507 | ||
2508 | static int | |
2bf541ff | 2509 | eb_select_engine(struct i915_execbuffer *eb) |
5e2a0419 | 2510 | { |
544460c3 | 2511 | struct intel_context *ce, *child; |
5e2a0419 CW |
2512 | unsigned int idx; |
2513 | int err; | |
2514 | ||
976b55f0 | 2515 | if (i915_gem_context_user_engines(eb->gem_context)) |
b49a7d51 | 2516 | idx = eb->args->flags & I915_EXEC_RING_MASK; |
976b55f0 | 2517 | else |
b49a7d51 | 2518 | idx = eb_select_legacy_ring(eb); |
5e2a0419 CW |
2519 | |
2520 | ce = i915_gem_context_get_engine(eb->gem_context, idx); | |
2521 | if (IS_ERR(ce)) | |
2522 | return PTR_ERR(ce); | |
2523 | ||
544460c3 MB |
2524 | if (intel_context_is_parallel(ce)) { |
2525 | if (eb->buffer_count < ce->parallel.number_children + 1) { | |
2526 | intel_context_put(ce); | |
2527 | return -EINVAL; | |
2528 | } | |
2529 | if (eb->batch_start_offset || eb->args->batch_len) { | |
2530 | intel_context_put(ce); | |
2531 | return -EINVAL; | |
2532 | } | |
2533 | } | |
2534 | eb->num_batches = ce->parallel.number_children + 1; | |
2535 | ||
2536 | for_each_child(ce, child) | |
2537 | intel_context_get(child); | |
2bf541ff | 2538 | intel_gt_pm_get(ce->engine->gt); |
5e2a0419 | 2539 | |
2bf541ff ML |
2540 | if (!test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) { |
2541 | err = intel_context_alloc_state(ce); | |
2542 | if (err) | |
2543 | goto err; | |
2544 | } | |
544460c3 MB |
2545 | for_each_child(ce, child) { |
2546 | if (!test_bit(CONTEXT_ALLOC_BIT, &child->flags)) { | |
2547 | err = intel_context_alloc_state(child); | |
2548 | if (err) | |
2549 | goto err; | |
2550 | } | |
2551 | } | |
2bf541ff ML |
2552 | |
2553 | /* | |
2554 | * ABI: Before userspace accesses the GPU (e.g. execbuffer), report | |
2555 | * EIO if the GPU is already wedged. | |
2556 | */ | |
2557 | err = intel_gt_terminally_wedged(ce->engine->gt); | |
2558 | if (err) | |
2559 | goto err; | |
2560 | ||
2561 | eb->context = ce; | |
544460c3 | 2562 | eb->gt = ce->engine->gt; |
2bf541ff ML |
2563 | |
2564 | /* | |
2565 | * Make sure engine pool stays alive even if we call intel_context_put | |
2566 | * during ww handling. The pool is destroyed when last pm reference | |
2567 | * is dropped, which breaks our -EDEADLK handling. | |
2568 | */ | |
2569 | return err; | |
2570 | ||
2571 | err: | |
2572 | intel_gt_pm_put(ce->engine->gt); | |
544460c3 MB |
2573 | for_each_child(ce, child) |
2574 | intel_context_put(child); | |
2bf541ff | 2575 | intel_context_put(ce); |
5e2a0419 | 2576 | return err; |
de1add36 TU |
2577 | } |
2578 | ||
2bf541ff ML |
2579 | static void |
2580 | eb_put_engine(struct i915_execbuffer *eb) | |
2581 | { | |
544460c3 MB |
2582 | struct intel_context *child; |
2583 | ||
2584 | intel_gt_pm_put(eb->gt); | |
2585 | for_each_child(eb->context, child) | |
2586 | intel_context_put(child); | |
2bf541ff ML |
2587 | intel_context_put(eb->context); |
2588 | } | |
2589 | ||
cf6e7bac | 2590 | static void |
13149e8b | 2591 | __free_fence_array(struct eb_fence *fences, unsigned int n) |
cf6e7bac | 2592 | { |
13149e8b | 2593 | while (n--) { |
cda9edd0 | 2594 | drm_syncobj_put(ptr_mask_bits(fences[n].syncobj, 2)); |
13149e8b | 2595 | dma_fence_put(fences[n].dma_fence); |
440d0f12 | 2596 | dma_fence_chain_free(fences[n].chain_fence); |
13149e8b | 2597 | } |
cf6e7bac JE |
2598 | kvfree(fences); |
2599 | } | |
2600 | ||
cda9edd0 | 2601 | static int |
13149e8b LL |
2602 | add_timeline_fence_array(struct i915_execbuffer *eb, |
2603 | const struct drm_i915_gem_execbuffer_ext_timeline_fences *timeline_fences) | |
cf6e7bac | 2604 | { |
13149e8b LL |
2605 | struct drm_i915_gem_exec_fence __user *user_fences; |
2606 | u64 __user *user_values; | |
2607 | struct eb_fence *f; | |
2608 | u64 nfences; | |
2609 | int err = 0; | |
cf6e7bac | 2610 | |
13149e8b LL |
2611 | nfences = timeline_fences->fence_count; |
2612 | if (!nfences) | |
cda9edd0 | 2613 | return 0; |
cf6e7bac | 2614 | |
d710fc16 CW |
2615 | /* Check multiplication overflow for access_ok() and kvmalloc_array() */ |
2616 | BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long)); | |
2617 | if (nfences > min_t(unsigned long, | |
13149e8b LL |
2618 | ULONG_MAX / sizeof(*user_fences), |
2619 | SIZE_MAX / sizeof(*f)) - eb->num_fences) | |
cda9edd0 | 2620 | return -EINVAL; |
cf6e7bac | 2621 | |
13149e8b LL |
2622 | user_fences = u64_to_user_ptr(timeline_fences->handles_ptr); |
2623 | if (!access_ok(user_fences, nfences * sizeof(*user_fences))) | |
2624 | return -EFAULT; | |
2625 | ||
2626 | user_values = u64_to_user_ptr(timeline_fences->values_ptr); | |
2627 | if (!access_ok(user_values, nfences * sizeof(*user_values))) | |
cda9edd0 | 2628 | return -EFAULT; |
cf6e7bac | 2629 | |
13149e8b LL |
2630 | f = krealloc(eb->fences, |
2631 | (eb->num_fences + nfences) * sizeof(*f), | |
2632 | __GFP_NOWARN | GFP_KERNEL); | |
2633 | if (!f) | |
cda9edd0 | 2634 | return -ENOMEM; |
cf6e7bac | 2635 | |
13149e8b LL |
2636 | eb->fences = f; |
2637 | f += eb->num_fences; | |
2638 | ||
2639 | BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) & | |
2640 | ~__I915_EXEC_FENCE_UNKNOWN_FLAGS); | |
2641 | ||
2642 | while (nfences--) { | |
2643 | struct drm_i915_gem_exec_fence user_fence; | |
cf6e7bac | 2644 | struct drm_syncobj *syncobj; |
13149e8b LL |
2645 | struct dma_fence *fence = NULL; |
2646 | u64 point; | |
2647 | ||
2648 | if (__copy_from_user(&user_fence, | |
2649 | user_fences++, | |
2650 | sizeof(user_fence))) | |
2651 | return -EFAULT; | |
2652 | ||
2653 | if (user_fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS) | |
2654 | return -EINVAL; | |
2655 | ||
2656 | if (__get_user(point, user_values++)) | |
2657 | return -EFAULT; | |
2658 | ||
2659 | syncobj = drm_syncobj_find(eb->file, user_fence.handle); | |
2660 | if (!syncobj) { | |
2661 | DRM_DEBUG("Invalid syncobj handle provided\n"); | |
2662 | return -ENOENT; | |
2663 | } | |
2664 | ||
2665 | fence = drm_syncobj_fence_get(syncobj); | |
cf6e7bac | 2666 | |
13149e8b LL |
2667 | if (!fence && user_fence.flags && |
2668 | !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) { | |
2669 | DRM_DEBUG("Syncobj handle has no fence\n"); | |
2670 | drm_syncobj_put(syncobj); | |
2671 | return -EINVAL; | |
cf6e7bac JE |
2672 | } |
2673 | ||
13149e8b LL |
2674 | if (fence) |
2675 | err = dma_fence_chain_find_seqno(&fence, point); | |
2676 | ||
2677 | if (err && !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) { | |
2678 | DRM_DEBUG("Syncobj handle missing requested point %llu\n", point); | |
da1ea128 | 2679 | dma_fence_put(fence); |
13149e8b LL |
2680 | drm_syncobj_put(syncobj); |
2681 | return err; | |
2682 | } | |
2683 | ||
2684 | /* | |
2685 | * A point might have been signaled already and | |
2686 | * garbage collected from the timeline. In this case | |
2687 | * just ignore the point and carry on. | |
2688 | */ | |
2689 | if (!fence && !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) { | |
2690 | drm_syncobj_put(syncobj); | |
2691 | continue; | |
2692 | } | |
2693 | ||
2694 | /* | |
2695 | * For timeline syncobjs we need to preallocate chains for | |
2696 | * later signaling. | |
2697 | */ | |
2698 | if (point != 0 && user_fence.flags & I915_EXEC_FENCE_SIGNAL) { | |
2699 | /* | |
2700 | * Waiting and signaling the same point (when point != | |
2701 | * 0) would break the timeline. | |
2702 | */ | |
2703 | if (user_fence.flags & I915_EXEC_FENCE_WAIT) { | |
2704 | DRM_DEBUG("Trying to wait & signal the same timeline point.\n"); | |
2705 | dma_fence_put(fence); | |
2706 | drm_syncobj_put(syncobj); | |
2707 | return -EINVAL; | |
2708 | } | |
2709 | ||
440d0f12 | 2710 | f->chain_fence = dma_fence_chain_alloc(); |
13149e8b LL |
2711 | if (!f->chain_fence) { |
2712 | drm_syncobj_put(syncobj); | |
2713 | dma_fence_put(fence); | |
2714 | return -ENOMEM; | |
2715 | } | |
2716 | } else { | |
2717 | f->chain_fence = NULL; | |
ebcaa1ff TU |
2718 | } |
2719 | ||
13149e8b LL |
2720 | f->syncobj = ptr_pack_bits(syncobj, user_fence.flags, 2); |
2721 | f->dma_fence = fence; | |
2722 | f->value = point; | |
2723 | f++; | |
2724 | eb->num_fences++; | |
2725 | } | |
2726 | ||
2727 | return 0; | |
2728 | } | |
2729 | ||
2730 | static int add_fence_array(struct i915_execbuffer *eb) | |
2731 | { | |
2732 | struct drm_i915_gem_execbuffer2 *args = eb->args; | |
2733 | struct drm_i915_gem_exec_fence __user *user; | |
2734 | unsigned long num_fences = args->num_cliprects; | |
2735 | struct eb_fence *f; | |
2736 | ||
2737 | if (!(args->flags & I915_EXEC_FENCE_ARRAY)) | |
2738 | return 0; | |
2739 | ||
2740 | if (!num_fences) | |
2741 | return 0; | |
2742 | ||
2743 | /* Check multiplication overflow for access_ok() and kvmalloc_array() */ | |
2744 | BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long)); | |
2745 | if (num_fences > min_t(unsigned long, | |
2746 | ULONG_MAX / sizeof(*user), | |
2747 | SIZE_MAX / sizeof(*f) - eb->num_fences)) | |
2748 | return -EINVAL; | |
2749 | ||
2750 | user = u64_to_user_ptr(args->cliprects_ptr); | |
2751 | if (!access_ok(user, num_fences * sizeof(*user))) | |
2752 | return -EFAULT; | |
2753 | ||
2754 | f = krealloc(eb->fences, | |
2755 | (eb->num_fences + num_fences) * sizeof(*f), | |
2756 | __GFP_NOWARN | GFP_KERNEL); | |
2757 | if (!f) | |
2758 | return -ENOMEM; | |
2759 | ||
2760 | eb->fences = f; | |
2761 | f += eb->num_fences; | |
2762 | while (num_fences--) { | |
2763 | struct drm_i915_gem_exec_fence user_fence; | |
2764 | struct drm_syncobj *syncobj; | |
2765 | struct dma_fence *fence = NULL; | |
2766 | ||
2767 | if (__copy_from_user(&user_fence, user++, sizeof(user_fence))) | |
2768 | return -EFAULT; | |
2769 | ||
2770 | if (user_fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS) | |
2771 | return -EINVAL; | |
2772 | ||
2773 | syncobj = drm_syncobj_find(eb->file, user_fence.handle); | |
cf6e7bac JE |
2774 | if (!syncobj) { |
2775 | DRM_DEBUG("Invalid syncobj handle provided\n"); | |
13149e8b LL |
2776 | return -ENOENT; |
2777 | } | |
2778 | ||
2779 | if (user_fence.flags & I915_EXEC_FENCE_WAIT) { | |
2780 | fence = drm_syncobj_fence_get(syncobj); | |
2781 | if (!fence) { | |
2782 | DRM_DEBUG("Syncobj handle has no fence\n"); | |
2783 | drm_syncobj_put(syncobj); | |
2784 | return -EINVAL; | |
2785 | } | |
cf6e7bac JE |
2786 | } |
2787 | ||
ebcaa1ff TU |
2788 | BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) & |
2789 | ~__I915_EXEC_FENCE_UNKNOWN_FLAGS); | |
2790 | ||
13149e8b LL |
2791 | f->syncobj = ptr_pack_bits(syncobj, user_fence.flags, 2); |
2792 | f->dma_fence = fence; | |
2793 | f->value = 0; | |
2794 | f->chain_fence = NULL; | |
2795 | f++; | |
2796 | eb->num_fences++; | |
cf6e7bac JE |
2797 | } |
2798 | ||
cda9edd0 | 2799 | return 0; |
13149e8b | 2800 | } |
cf6e7bac | 2801 | |
13149e8b LL |
2802 | static void put_fence_array(struct eb_fence *fences, int num_fences) |
2803 | { | |
2804 | if (fences) | |
2805 | __free_fence_array(fences, num_fences); | |
cf6e7bac JE |
2806 | } |
2807 | ||
2808 | static int | |
544460c3 MB |
2809 | await_fence_array(struct i915_execbuffer *eb, |
2810 | struct i915_request *rq) | |
cf6e7bac | 2811 | { |
cf6e7bac JE |
2812 | unsigned int n; |
2813 | int err; | |
2814 | ||
13149e8b | 2815 | for (n = 0; n < eb->num_fences; n++) { |
cf6e7bac | 2816 | struct drm_syncobj *syncobj; |
cf6e7bac JE |
2817 | unsigned int flags; |
2818 | ||
cda9edd0 | 2819 | syncobj = ptr_unpack_bits(eb->fences[n].syncobj, &flags, 2); |
cf6e7bac | 2820 | |
13149e8b LL |
2821 | if (!eb->fences[n].dma_fence) |
2822 | continue; | |
cf6e7bac | 2823 | |
544460c3 | 2824 | err = i915_request_await_dma_fence(rq, eb->fences[n].dma_fence); |
cf6e7bac JE |
2825 | if (err < 0) |
2826 | return err; | |
2827 | } | |
2828 | ||
2829 | return 0; | |
2830 | } | |
2831 | ||
544460c3 MB |
2832 | static void signal_fence_array(const struct i915_execbuffer *eb, |
2833 | struct dma_fence * const fence) | |
cf6e7bac | 2834 | { |
cf6e7bac JE |
2835 | unsigned int n; |
2836 | ||
13149e8b | 2837 | for (n = 0; n < eb->num_fences; n++) { |
cf6e7bac JE |
2838 | struct drm_syncobj *syncobj; |
2839 | unsigned int flags; | |
2840 | ||
cda9edd0 | 2841 | syncobj = ptr_unpack_bits(eb->fences[n].syncobj, &flags, 2); |
cf6e7bac JE |
2842 | if (!(flags & I915_EXEC_FENCE_SIGNAL)) |
2843 | continue; | |
2844 | ||
13149e8b LL |
2845 | if (eb->fences[n].chain_fence) { |
2846 | drm_syncobj_add_point(syncobj, | |
2847 | eb->fences[n].chain_fence, | |
2848 | fence, | |
2849 | eb->fences[n].value); | |
2850 | /* | |
2851 | * The chain's ownership is transferred to the | |
2852 | * timeline. | |
2853 | */ | |
2854 | eb->fences[n].chain_fence = NULL; | |
2855 | } else { | |
2856 | drm_syncobj_replace_fence(syncobj, fence); | |
2857 | } | |
cf6e7bac JE |
2858 | } |
2859 | } | |
2860 | ||
13149e8b LL |
2861 | static int |
2862 | parse_timeline_fences(struct i915_user_extension __user *ext, void *data) | |
2863 | { | |
2864 | struct i915_execbuffer *eb = data; | |
2865 | struct drm_i915_gem_execbuffer_ext_timeline_fences timeline_fences; | |
2866 | ||
2867 | if (copy_from_user(&timeline_fences, ext, sizeof(timeline_fences))) | |
2868 | return -EFAULT; | |
2869 | ||
2870 | return add_timeline_fence_array(eb, &timeline_fences); | |
2871 | } | |
2872 | ||
61231f6b CW |
2873 | static void retire_requests(struct intel_timeline *tl, struct i915_request *end) |
2874 | { | |
2875 | struct i915_request *rq, *rn; | |
2876 | ||
2877 | list_for_each_entry_safe(rq, rn, &tl->requests, link) | |
2878 | if (rq == end || !i915_request_retire(rq)) | |
2879 | break; | |
2880 | } | |
2881 | ||
544460c3 MB |
2882 | static int eb_request_add(struct i915_execbuffer *eb, struct i915_request *rq, |
2883 | int err, bool last_parallel) | |
61231f6b | 2884 | { |
61231f6b CW |
2885 | struct intel_timeline * const tl = i915_request_timeline(rq); |
2886 | struct i915_sched_attr attr = {}; | |
2887 | struct i915_request *prev; | |
2888 | ||
2889 | lockdep_assert_held(&tl->mutex); | |
2890 | lockdep_unpin_lock(&tl->mutex, rq->cookie); | |
2891 | ||
2892 | trace_i915_request_add(rq); | |
2893 | ||
2894 | prev = __i915_request_commit(rq); | |
2895 | ||
2896 | /* Check that the context wasn't destroyed before submission */ | |
207e4a71 | 2897 | if (likely(!intel_context_is_closed(eb->context))) { |
61231f6b | 2898 | attr = eb->gem_context->sched; |
61231f6b CW |
2899 | } else { |
2900 | /* Serialise with context_close via the add_to_timeline */ | |
36e191f0 CW |
2901 | i915_request_set_error_once(rq, -ENOENT); |
2902 | __i915_request_skip(rq); | |
ba38b79e | 2903 | err = -ENOENT; /* override any transient errors */ |
61231f6b CW |
2904 | } |
2905 | ||
544460c3 MB |
2906 | if (intel_context_is_parallel(eb->context)) { |
2907 | if (err) { | |
2908 | __i915_request_skip(rq); | |
2909 | set_bit(I915_FENCE_FLAG_SKIP_PARALLEL, | |
2910 | &rq->fence.flags); | |
2911 | } | |
2912 | if (last_parallel) | |
2913 | set_bit(I915_FENCE_FLAG_SUBMIT_PARALLEL, | |
2914 | &rq->fence.flags); | |
2915 | } | |
2916 | ||
61231f6b | 2917 | __i915_request_queue(rq, &attr); |
61231f6b CW |
2918 | |
2919 | /* Try to clean up the client's timeline after submitting the request */ | |
2920 | if (prev) | |
2921 | retire_requests(tl, prev); | |
2922 | ||
2923 | mutex_unlock(&tl->mutex); | |
ba38b79e CW |
2924 | |
2925 | return err; | |
61231f6b CW |
2926 | } |
2927 | ||
544460c3 MB |
2928 | static int eb_requests_add(struct i915_execbuffer *eb, int err) |
2929 | { | |
2930 | int i; | |
2931 | ||
2932 | /* | |
2933 | * We iterate in reverse order of creation to release timeline mutexes in | |
2934 | * same order. | |
2935 | */ | |
2936 | for_each_batch_add_order(eb, i) { | |
2937 | struct i915_request *rq = eb->requests[i]; | |
2938 | ||
2939 | if (!rq) | |
2940 | continue; | |
2941 | err |= eb_request_add(eb, rq, err, i == 0); | |
2942 | } | |
2943 | ||
2944 | return err; | |
2945 | } | |
2946 | ||
cda9edd0 | 2947 | static const i915_user_extension_fn execbuf_extensions[] = { |
13149e8b | 2948 | [DRM_I915_GEM_EXECBUFFER_EXT_TIMELINE_FENCES] = parse_timeline_fences, |
cda9edd0 LL |
2949 | }; |
2950 | ||
2951 | static int | |
2952 | parse_execbuf2_extensions(struct drm_i915_gem_execbuffer2 *args, | |
2953 | struct i915_execbuffer *eb) | |
2954 | { | |
cda9edd0 LL |
2955 | if (!(args->flags & I915_EXEC_USE_EXTENSIONS)) |
2956 | return 0; | |
2957 | ||
2958 | /* The execbuf2 extension mechanism reuses cliprects_ptr. So we cannot | |
2959 | * have another flag also using it at the same time. | |
2960 | */ | |
2961 | if (eb->args->flags & I915_EXEC_FENCE_ARRAY) | |
2962 | return -EINVAL; | |
2963 | ||
2964 | if (args->num_cliprects != 0) | |
2965 | return -EINVAL; | |
2966 | ||
2967 | return i915_user_extensions(u64_to_user_ptr(args->cliprects_ptr), | |
2968 | execbuf_extensions, | |
2969 | ARRAY_SIZE(execbuf_extensions), | |
2970 | eb); | |
2971 | } | |
2972 | ||
544460c3 MB |
2973 | static void eb_requests_get(struct i915_execbuffer *eb) |
2974 | { | |
2975 | unsigned int i; | |
2976 | ||
2977 | for_each_batch_create_order(eb, i) { | |
2978 | if (!eb->requests[i]) | |
2979 | break; | |
2980 | ||
2981 | i915_request_get(eb->requests[i]); | |
2982 | } | |
2983 | } | |
2984 | ||
2985 | static void eb_requests_put(struct i915_execbuffer *eb) | |
2986 | { | |
2987 | unsigned int i; | |
2988 | ||
2989 | for_each_batch_create_order(eb, i) { | |
2990 | if (!eb->requests[i]) | |
2991 | break; | |
2992 | ||
2993 | i915_request_put(eb->requests[i]); | |
2994 | } | |
2995 | } | |
2996 | ||
2997 | static struct sync_file * | |
2998 | eb_composite_fence_create(struct i915_execbuffer *eb, int out_fence_fd) | |
2999 | { | |
3000 | struct sync_file *out_fence = NULL; | |
3001 | struct dma_fence_array *fence_array; | |
3002 | struct dma_fence **fences; | |
3003 | unsigned int i; | |
3004 | ||
3005 | GEM_BUG_ON(!intel_context_is_parent(eb->context)); | |
3006 | ||
3007 | fences = kmalloc_array(eb->num_batches, sizeof(*fences), GFP_KERNEL); | |
3008 | if (!fences) | |
3009 | return ERR_PTR(-ENOMEM); | |
3010 | ||
7647f009 | 3011 | for_each_batch_create_order(eb, i) { |
544460c3 | 3012 | fences[i] = &eb->requests[i]->fence; |
7647f009 MB |
3013 | __set_bit(I915_FENCE_FLAG_COMPOSITE, |
3014 | &eb->requests[i]->fence.flags); | |
3015 | } | |
544460c3 MB |
3016 | |
3017 | fence_array = dma_fence_array_create(eb->num_batches, | |
3018 | fences, | |
3019 | eb->context->parallel.fence_context, | |
3020 | eb->context->parallel.seqno, | |
3021 | false); | |
3022 | if (!fence_array) { | |
3023 | kfree(fences); | |
3024 | return ERR_PTR(-ENOMEM); | |
3025 | } | |
3026 | ||
3027 | /* Move ownership to the dma_fence_array created above */ | |
3028 | for_each_batch_create_order(eb, i) | |
3029 | dma_fence_get(fences[i]); | |
3030 | ||
3031 | if (out_fence_fd != -1) { | |
3032 | out_fence = sync_file_create(&fence_array->base); | |
3033 | /* sync_file now owns fence_arry, drop creation ref */ | |
3034 | dma_fence_put(&fence_array->base); | |
3035 | if (!out_fence) | |
3036 | return ERR_PTR(-ENOMEM); | |
3037 | } | |
3038 | ||
3039 | eb->composite_fence = &fence_array->base; | |
3040 | ||
3041 | return out_fence; | |
3042 | } | |
3043 | ||
3044 | static struct sync_file * | |
3045 | eb_fences_add(struct i915_execbuffer *eb, struct i915_request *rq, | |
3046 | struct dma_fence *in_fence, int out_fence_fd) | |
3047 | { | |
3048 | struct sync_file *out_fence = NULL; | |
3049 | int err; | |
3050 | ||
3051 | if (unlikely(eb->gem_context->syncobj)) { | |
3052 | struct dma_fence *fence; | |
3053 | ||
3054 | fence = drm_syncobj_fence_get(eb->gem_context->syncobj); | |
3055 | err = i915_request_await_dma_fence(rq, fence); | |
3056 | dma_fence_put(fence); | |
3057 | if (err) | |
3058 | return ERR_PTR(err); | |
3059 | } | |
3060 | ||
3061 | if (in_fence) { | |
3062 | if (eb->args->flags & I915_EXEC_FENCE_SUBMIT) | |
3063 | err = i915_request_await_execution(rq, in_fence); | |
3064 | else | |
3065 | err = i915_request_await_dma_fence(rq, in_fence); | |
3066 | if (err < 0) | |
3067 | return ERR_PTR(err); | |
3068 | } | |
3069 | ||
3070 | if (eb->fences) { | |
3071 | err = await_fence_array(eb, rq); | |
3072 | if (err) | |
3073 | return ERR_PTR(err); | |
3074 | } | |
3075 | ||
3076 | if (intel_context_is_parallel(eb->context)) { | |
3077 | out_fence = eb_composite_fence_create(eb, out_fence_fd); | |
3078 | if (IS_ERR(out_fence)) | |
3079 | return ERR_PTR(-ENOMEM); | |
3080 | } else if (out_fence_fd != -1) { | |
3081 | out_fence = sync_file_create(&rq->fence); | |
3082 | if (!out_fence) | |
3083 | return ERR_PTR(-ENOMEM); | |
3084 | } | |
3085 | ||
3086 | return out_fence; | |
3087 | } | |
3088 | ||
3089 | static struct intel_context * | |
3090 | eb_find_context(struct i915_execbuffer *eb, unsigned int context_number) | |
3091 | { | |
3092 | struct intel_context *child; | |
3093 | ||
3094 | if (likely(context_number == 0)) | |
3095 | return eb->context; | |
3096 | ||
3097 | for_each_child(eb->context, child) | |
3098 | if (!--context_number) | |
3099 | return child; | |
3100 | ||
3101 | GEM_BUG_ON("Context not found"); | |
3102 | ||
3103 | return NULL; | |
3104 | } | |
3105 | ||
3106 | static struct sync_file * | |
3107 | eb_requests_create(struct i915_execbuffer *eb, struct dma_fence *in_fence, | |
3108 | int out_fence_fd) | |
3109 | { | |
3110 | struct sync_file *out_fence = NULL; | |
3111 | unsigned int i; | |
3112 | ||
3113 | for_each_batch_create_order(eb, i) { | |
3114 | /* Allocate a request for this batch buffer nice and early. */ | |
3115 | eb->requests[i] = i915_request_create(eb_find_context(eb, i)); | |
3116 | if (IS_ERR(eb->requests[i])) { | |
3117 | out_fence = ERR_PTR(PTR_ERR(eb->requests[i])); | |
3118 | eb->requests[i] = NULL; | |
3119 | return out_fence; | |
3120 | } | |
3121 | ||
3122 | /* | |
3123 | * Only the first request added (committed to backend) has to | |
3124 | * take the in fences into account as all subsequent requests | |
3125 | * will have fences inserted inbetween them. | |
3126 | */ | |
3127 | if (i + 1 == eb->num_batches) { | |
3128 | out_fence = eb_fences_add(eb, eb->requests[i], | |
3129 | in_fence, out_fence_fd); | |
3130 | if (IS_ERR(out_fence)) | |
3131 | return out_fence; | |
3132 | } | |
3133 | ||
3134 | /* | |
3135 | * Whilst this request exists, batch_obj will be on the | |
3136 | * active_list, and so will hold the active reference. Only when | |
3137 | * this request is retired will the batch_obj be moved onto | |
3138 | * the inactive_list and lose its active reference. Hence we do | |
3139 | * not need to explicitly hold another reference here. | |
3140 | */ | |
3141 | eb->requests[i]->batch = eb->batches[i]->vma; | |
3142 | if (eb->batch_pool) { | |
3143 | GEM_BUG_ON(intel_context_is_parallel(eb->context)); | |
3144 | intel_gt_buffer_pool_mark_active(eb->batch_pool, | |
3145 | eb->requests[i]); | |
3146 | } | |
3147 | } | |
3148 | ||
3149 | return out_fence; | |
3150 | } | |
3151 | ||
54cf91dc | 3152 | static int |
650bc635 | 3153 | i915_gem_do_execbuffer(struct drm_device *dev, |
54cf91dc CW |
3154 | struct drm_file *file, |
3155 | struct drm_i915_gem_execbuffer2 *args, | |
cda9edd0 | 3156 | struct drm_i915_gem_exec_object2 *exec) |
54cf91dc | 3157 | { |
44157641 | 3158 | struct drm_i915_private *i915 = to_i915(dev); |
650bc635 | 3159 | struct i915_execbuffer eb; |
fec0445c CW |
3160 | struct dma_fence *in_fence = NULL; |
3161 | struct sync_file *out_fence = NULL; | |
3162 | int out_fence_fd = -1; | |
2889caa9 | 3163 | int err; |
432e58ed | 3164 | |
74c1c694 | 3165 | BUILD_BUG_ON(__EXEC_INTERNAL_FLAGS & ~__I915_EXEC_ILLEGAL_FLAGS); |
2889caa9 CW |
3166 | BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & |
3167 | ~__EXEC_OBJECT_UNKNOWN_FLAGS); | |
54cf91dc | 3168 | |
44157641 | 3169 | eb.i915 = i915; |
650bc635 CW |
3170 | eb.file = file; |
3171 | eb.args = args; | |
ad5d95e4 | 3172 | if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC)) |
2889caa9 | 3173 | args->flags |= __EXEC_HAS_RELOC; |
c7c6e46f | 3174 | |
650bc635 | 3175 | eb.exec = exec; |
8ae275c2 ML |
3176 | eb.vma = (struct eb_vma *)(exec + args->buffer_count + 1); |
3177 | eb.vma[0].vma = NULL; | |
8e02cceb | 3178 | eb.batch_pool = NULL; |
c7c6e46f | 3179 | |
2889caa9 | 3180 | eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS; |
650bc635 CW |
3181 | reloc_cache_init(&eb.reloc_cache, eb.i915); |
3182 | ||
2889caa9 | 3183 | eb.buffer_count = args->buffer_count; |
650bc635 | 3184 | eb.batch_start_offset = args->batch_start_offset; |
32d94048 | 3185 | eb.trampoline = NULL; |
650bc635 | 3186 | |
cda9edd0 | 3187 | eb.fences = NULL; |
13149e8b | 3188 | eb.num_fences = 0; |
cda9edd0 | 3189 | |
544460c3 MB |
3190 | memset(eb.requests, 0, sizeof(struct i915_request *) * |
3191 | ARRAY_SIZE(eb.requests)); | |
3192 | eb.composite_fence = NULL; | |
3193 | ||
2889caa9 | 3194 | eb.batch_flags = 0; |
d7d4eedd | 3195 | if (args->flags & I915_EXEC_SECURE) { |
40e1956e | 3196 | if (GRAPHICS_VER(i915) >= 11) |
44157641 JB |
3197 | return -ENODEV; |
3198 | ||
3199 | /* Return -EPERM to trigger fallback code on old binaries. */ | |
3200 | if (!HAS_SECURE_BATCHES(i915)) | |
3201 | return -EPERM; | |
3202 | ||
b3ac9f25 | 3203 | if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN)) |
44157641 | 3204 | return -EPERM; |
d7d4eedd | 3205 | |
2889caa9 | 3206 | eb.batch_flags |= I915_DISPATCH_SECURE; |
d7d4eedd | 3207 | } |
b45305fc | 3208 | if (args->flags & I915_EXEC_IS_PINNED) |
2889caa9 | 3209 | eb.batch_flags |= I915_DISPATCH_PINNED; |
54cf91dc | 3210 | |
13149e8b LL |
3211 | err = parse_execbuf2_extensions(args, &eb); |
3212 | if (err) | |
3213 | goto err_ext; | |
3214 | ||
3215 | err = add_fence_array(&eb); | |
3216 | if (err) | |
3217 | goto err_ext; | |
3218 | ||
889333c7 CW |
3219 | #define IN_FENCES (I915_EXEC_FENCE_IN | I915_EXEC_FENCE_SUBMIT) |
3220 | if (args->flags & IN_FENCES) { | |
3221 | if ((args->flags & IN_FENCES) == IN_FENCES) | |
3222 | return -EINVAL; | |
3223 | ||
fec0445c | 3224 | in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2)); |
13149e8b LL |
3225 | if (!in_fence) { |
3226 | err = -EINVAL; | |
3227 | goto err_ext; | |
3228 | } | |
fec0445c | 3229 | } |
889333c7 | 3230 | #undef IN_FENCES |
a88b6e4c | 3231 | |
fec0445c CW |
3232 | if (args->flags & I915_EXEC_FENCE_OUT) { |
3233 | out_fence_fd = get_unused_fd_flags(O_CLOEXEC); | |
3234 | if (out_fence_fd < 0) { | |
2889caa9 | 3235 | err = out_fence_fd; |
889333c7 | 3236 | goto err_in_fence; |
fec0445c CW |
3237 | } |
3238 | } | |
3239 | ||
cda9edd0 LL |
3240 | err = eb_create(&eb); |
3241 | if (err) | |
13149e8b | 3242 | goto err_out_fence; |
cda9edd0 | 3243 | |
4d470f73 | 3244 | GEM_BUG_ON(!eb.lut_size); |
2889caa9 | 3245 | |
1acfc104 CW |
3246 | err = eb_select_context(&eb); |
3247 | if (unlikely(err)) | |
3248 | goto err_destroy; | |
3249 | ||
2bf541ff | 3250 | err = eb_select_engine(&eb); |
d6f328bf | 3251 | if (unlikely(err)) |
e5dadff4 | 3252 | goto err_context; |
d6f328bf | 3253 | |
c43ce123 ML |
3254 | err = eb_lookup_vmas(&eb); |
3255 | if (err) { | |
b4b9731b | 3256 | eb_release_vmas(&eb, true); |
c43ce123 ML |
3257 | goto err_engine; |
3258 | } | |
3259 | ||
3260 | i915_gem_ww_ctx_init(&eb.ww, true); | |
3261 | ||
8e4ba491 | 3262 | err = eb_relocate_parse(&eb); |
1f727d9e | 3263 | if (err) { |
2889caa9 CW |
3264 | /* |
3265 | * If the user expects the execobject.offset and | |
3266 | * reloc.presumed_offset to be an exact match, | |
3267 | * as for using NO_RELOC, then we cannot update | |
3268 | * the execobject.offset until we have completed | |
3269 | * relocation. | |
3270 | */ | |
3271 | args->flags &= ~__EXEC_HAS_RELOC; | |
2889caa9 | 3272 | goto err_vma; |
1f727d9e | 3273 | } |
54cf91dc | 3274 | |
c43ce123 | 3275 | ww_acquire_done(&eb.ww.ctx); |
7d6236bb | 3276 | |
544460c3 MB |
3277 | out_fence = eb_requests_create(&eb, in_fence, out_fence_fd); |
3278 | if (IS_ERR(out_fence)) { | |
3279 | err = PTR_ERR(out_fence); | |
3280 | if (eb.requests[0]) | |
cf6e7bac | 3281 | goto err_request; |
544460c3 MB |
3282 | else |
3283 | goto err_vma; | |
cf6e7bac JE |
3284 | } |
3285 | ||
544460c3 | 3286 | err = eb_submit(&eb); |
ed29c269 | 3287 | |
aa9b7810 | 3288 | err_request: |
544460c3 MB |
3289 | eb_requests_get(&eb); |
3290 | err = eb_requests_add(&eb, err); | |
c8659efa | 3291 | |
13149e8b | 3292 | if (eb.fences) |
544460c3 MB |
3293 | signal_fence_array(&eb, eb.composite_fence ? |
3294 | eb.composite_fence : | |
3295 | &eb.requests[0]->fence); | |
cf6e7bac | 3296 | |
fec0445c | 3297 | if (out_fence) { |
2889caa9 | 3298 | if (err == 0) { |
fec0445c | 3299 | fd_install(out_fence_fd, out_fence->file); |
b6a88e4a | 3300 | args->rsvd2 &= GENMASK_ULL(31, 0); /* keep in-fence */ |
fec0445c CW |
3301 | args->rsvd2 |= (u64)out_fence_fd << 32; |
3302 | out_fence_fd = -1; | |
3303 | } else { | |
3304 | fput(out_fence->file); | |
3305 | } | |
3306 | } | |
00dae4d3 JE |
3307 | |
3308 | if (unlikely(eb.gem_context->syncobj)) { | |
3309 | drm_syncobj_replace_fence(eb.gem_context->syncobj, | |
544460c3 MB |
3310 | eb.composite_fence ? |
3311 | eb.composite_fence : | |
3312 | &eb.requests[0]->fence); | |
00dae4d3 JE |
3313 | } |
3314 | ||
544460c3 MB |
3315 | if (!out_fence && eb.composite_fence) |
3316 | dma_fence_put(eb.composite_fence); | |
3317 | ||
3318 | eb_requests_put(&eb); | |
54cf91dc | 3319 | |
2889caa9 | 3320 | err_vma: |
b4b9731b | 3321 | eb_release_vmas(&eb, true); |
32d94048 CW |
3322 | if (eb.trampoline) |
3323 | i915_vma_unpin(eb.trampoline); | |
c43ce123 ML |
3324 | WARN_ON(err == -EDEADLK); |
3325 | i915_gem_ww_ctx_fini(&eb.ww); | |
3326 | ||
3327 | if (eb.batch_pool) | |
3328 | intel_gt_buffer_pool_put(eb.batch_pool); | |
c43ce123 | 3329 | err_engine: |
2bf541ff | 3330 | eb_put_engine(&eb); |
a4e57f90 | 3331 | err_context: |
8f2a1057 | 3332 | i915_gem_context_put(eb.gem_context); |
1acfc104 | 3333 | err_destroy: |
2889caa9 | 3334 | eb_destroy(&eb); |
4d470f73 | 3335 | err_out_fence: |
fec0445c CW |
3336 | if (out_fence_fd != -1) |
3337 | put_unused_fd(out_fence_fd); | |
4a04e371 | 3338 | err_in_fence: |
fec0445c | 3339 | dma_fence_put(in_fence); |
13149e8b LL |
3340 | err_ext: |
3341 | put_fence_array(eb.fences, eb.num_fences); | |
2889caa9 | 3342 | return err; |
54cf91dc CW |
3343 | } |
3344 | ||
d710fc16 CW |
3345 | static size_t eb_element_size(void) |
3346 | { | |
8ae275c2 | 3347 | return sizeof(struct drm_i915_gem_exec_object2) + sizeof(struct eb_vma); |
d710fc16 CW |
3348 | } |
3349 | ||
3350 | static bool check_buffer_count(size_t count) | |
3351 | { | |
3352 | const size_t sz = eb_element_size(); | |
3353 | ||
3354 | /* | |
3355 | * When using LUT_HANDLE, we impose a limit of INT_MAX for the lookup | |
3356 | * array size (see eb_create()). Otherwise, we can accept an array as | |
3357 | * large as can be addressed (though use large arrays at your peril)! | |
3358 | */ | |
3359 | ||
3360 | return !(count < 1 || count > INT_MAX || count > SIZE_MAX / sz - 1); | |
3361 | } | |
3362 | ||
54cf91dc | 3363 | int |
6a20fe7b VS |
3364 | i915_gem_execbuffer2_ioctl(struct drm_device *dev, void *data, |
3365 | struct drm_file *file) | |
54cf91dc | 3366 | { |
d0bf4582 | 3367 | struct drm_i915_private *i915 = to_i915(dev); |
54cf91dc | 3368 | struct drm_i915_gem_execbuffer2 *args = data; |
2889caa9 | 3369 | struct drm_i915_gem_exec_object2 *exec2_list; |
d710fc16 | 3370 | const size_t count = args->buffer_count; |
2889caa9 | 3371 | int err; |
54cf91dc | 3372 | |
d710fc16 | 3373 | if (!check_buffer_count(count)) { |
d0bf4582 | 3374 | drm_dbg(&i915->drm, "execbuf2 with %zd buffers\n", count); |
54cf91dc CW |
3375 | return -EINVAL; |
3376 | } | |
3377 | ||
00aff3f6 TU |
3378 | err = i915_gem_check_execbuffer(args); |
3379 | if (err) | |
3380 | return err; | |
2889caa9 | 3381 | |
47b08693 ML |
3382 | /* Allocate extra slots for use by the command parser */ |
3383 | exec2_list = kvmalloc_array(count + 2, eb_element_size(), | |
0ee931c4 | 3384 | __GFP_NOWARN | GFP_KERNEL); |
54cf91dc | 3385 | if (exec2_list == NULL) { |
d0bf4582 WK |
3386 | drm_dbg(&i915->drm, "Failed to allocate exec list for %zd buffers\n", |
3387 | count); | |
54cf91dc CW |
3388 | return -ENOMEM; |
3389 | } | |
2889caa9 CW |
3390 | if (copy_from_user(exec2_list, |
3391 | u64_to_user_ptr(args->buffers_ptr), | |
d710fc16 | 3392 | sizeof(*exec2_list) * count)) { |
d0bf4582 | 3393 | drm_dbg(&i915->drm, "copy %zd exec entries failed\n", count); |
2098105e | 3394 | kvfree(exec2_list); |
54cf91dc CW |
3395 | return -EFAULT; |
3396 | } | |
3397 | ||
cda9edd0 | 3398 | err = i915_gem_do_execbuffer(dev, file, args, exec2_list); |
2889caa9 CW |
3399 | |
3400 | /* | |
3401 | * Now that we have begun execution of the batchbuffer, we ignore | |
3402 | * any new error after this point. Also given that we have already | |
3403 | * updated the associated relocations, we try to write out the current | |
3404 | * object locations irrespective of any error. | |
3405 | */ | |
3406 | if (args->flags & __EXEC_HAS_RELOC) { | |
d593d992 | 3407 | struct drm_i915_gem_exec_object2 __user *user_exec_list = |
2889caa9 CW |
3408 | u64_to_user_ptr(args->buffers_ptr); |
3409 | unsigned int i; | |
9aab8bff | 3410 | |
2889caa9 | 3411 | /* Copy the new buffer offsets back to the user's exec list. */ |
594cc251 LT |
3412 | /* |
3413 | * Note: count * sizeof(*user_exec_list) does not overflow, | |
3414 | * because we checked 'count' in check_buffer_count(). | |
3415 | * | |
3416 | * And this range already got effectively checked earlier | |
3417 | * when we did the "copy_from_user()" above. | |
3418 | */ | |
b44f6873 CL |
3419 | if (!user_write_access_begin(user_exec_list, |
3420 | count * sizeof(*user_exec_list))) | |
8f4faed0 | 3421 | goto end; |
594cc251 | 3422 | |
9aab8bff | 3423 | for (i = 0; i < args->buffer_count; i++) { |
2889caa9 CW |
3424 | if (!(exec2_list[i].offset & UPDATE)) |
3425 | continue; | |
3426 | ||
934acce3 | 3427 | exec2_list[i].offset = |
2889caa9 CW |
3428 | gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK); |
3429 | unsafe_put_user(exec2_list[i].offset, | |
3430 | &user_exec_list[i].offset, | |
3431 | end_user); | |
54cf91dc | 3432 | } |
2889caa9 | 3433 | end_user: |
b44f6873 | 3434 | user_write_access_end(); |
8f4faed0 | 3435 | end:; |
54cf91dc CW |
3436 | } |
3437 | ||
2889caa9 | 3438 | args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS; |
2098105e | 3439 | kvfree(exec2_list); |
2889caa9 | 3440 | return err; |
54cf91dc | 3441 | } |