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