Commit | Line | Data |
---|---|---|
54cf91dc CW |
1 | /* |
2 | * Copyright © 2008,2010 Intel Corporation | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
5 | * copy of this software and associated documentation files (the "Software"), | |
6 | * to deal in the Software without restriction, including without limitation | |
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
8 | * and/or sell copies of the Software, and to permit persons to whom the | |
9 | * Software is furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice (including the next | |
12 | * paragraph) shall be included in all copies or substantial portions of the | |
13 | * Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 | * IN THE SOFTWARE. | |
22 | * | |
23 | * Authors: | |
24 | * Eric Anholt <eric@anholt.net> | |
25 | * Chris Wilson <chris@chris-wilson.co.uk> | |
26 | * | |
27 | */ | |
28 | ||
daedaa33 | 29 | #include <linux/intel-iommu.h> |
ad778f89 | 30 | #include <linux/reservation.h> |
fec0445c | 31 | #include <linux/sync_file.h> |
ad778f89 CW |
32 | #include <linux/uaccess.h> |
33 | ||
cf6e7bac | 34 | #include <drm/drm_syncobj.h> |
760285e7 | 35 | #include <drm/i915_drm.h> |
ad778f89 | 36 | |
54cf91dc | 37 | #include "i915_drv.h" |
57822dc6 | 38 | #include "i915_gem_clflush.h" |
54cf91dc CW |
39 | #include "i915_trace.h" |
40 | #include "intel_drv.h" | |
5d723d7a | 41 | #include "intel_frontbuffer.h" |
54cf91dc | 42 | |
7dd4f672 CW |
43 | enum { |
44 | FORCE_CPU_RELOC = 1, | |
45 | FORCE_GTT_RELOC, | |
46 | FORCE_GPU_RELOC, | |
47 | #define DBG_FORCE_RELOC 0 /* choose one of the above! */ | |
48 | }; | |
d50415cc | 49 | |
dade2a61 CW |
50 | #define __EXEC_OBJECT_HAS_REF BIT(31) |
51 | #define __EXEC_OBJECT_HAS_PIN BIT(30) | |
52 | #define __EXEC_OBJECT_HAS_FENCE BIT(29) | |
53 | #define __EXEC_OBJECT_NEEDS_MAP BIT(28) | |
54 | #define __EXEC_OBJECT_NEEDS_BIAS BIT(27) | |
55 | #define __EXEC_OBJECT_INTERNAL_FLAGS (~0u << 27) /* all of the above */ | |
2889caa9 CW |
56 | #define __EXEC_OBJECT_RESERVED (__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_FENCE) |
57 | ||
58 | #define __EXEC_HAS_RELOC BIT(31) | |
59 | #define __EXEC_VALIDATED BIT(30) | |
74c1c694 | 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 | ||
650bc635 | 230 | struct i915_execbuffer { |
2889caa9 CW |
231 | struct drm_i915_private *i915; /** i915 backpointer */ |
232 | struct drm_file *file; /** per-file lookup tables and limits */ | |
233 | struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */ | |
234 | struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */ | |
c7c6e46f CW |
235 | struct i915_vma **vma; |
236 | unsigned int *flags; | |
2889caa9 CW |
237 | |
238 | struct intel_engine_cs *engine; /** engine to queue the request to */ | |
239 | struct i915_gem_context *ctx; /** context for building the request */ | |
240 | struct i915_address_space *vm; /** GTT and vma for the request */ | |
241 | ||
e61e0f51 | 242 | struct i915_request *request; /** our request to build */ |
2889caa9 CW |
243 | struct i915_vma *batch; /** identity of the batch obj/vma */ |
244 | ||
245 | /** actual size of execobj[] as we may extend it for the cmdparser */ | |
246 | unsigned int buffer_count; | |
247 | ||
248 | /** list of vma not yet bound during reservation phase */ | |
249 | struct list_head unbound; | |
250 | ||
251 | /** list of vma that have execobj.relocation_count */ | |
252 | struct list_head relocs; | |
253 | ||
254 | /** | |
255 | * Track the most recently used object for relocations, as we | |
256 | * frequently have to perform multiple relocations within the same | |
257 | * obj/page | |
258 | */ | |
650bc635 | 259 | struct reloc_cache { |
2889caa9 CW |
260 | struct drm_mm_node node; /** temporary GTT binding */ |
261 | unsigned long vaddr; /** Current kmap address */ | |
262 | unsigned long page; /** Currently mapped page index */ | |
7dd4f672 | 263 | unsigned int gen; /** Cached value of INTEL_GEN */ |
650bc635 | 264 | bool use_64bit_reloc : 1; |
2889caa9 CW |
265 | bool has_llc : 1; |
266 | bool has_fence : 1; | |
267 | bool needs_unfenced : 1; | |
7dd4f672 | 268 | |
e61e0f51 | 269 | struct i915_request *rq; |
7dd4f672 CW |
270 | u32 *rq_cmd; |
271 | unsigned int rq_size; | |
650bc635 | 272 | } reloc_cache; |
2889caa9 CW |
273 | |
274 | u64 invalid_flags; /** Set of execobj.flags that are invalid */ | |
275 | u32 context_flags; /** Set of execobj.flags to insert from the ctx */ | |
276 | ||
277 | u32 batch_start_offset; /** Location within object of batch */ | |
278 | u32 batch_len; /** Length of batch within object */ | |
279 | u32 batch_flags; /** Flags composed for emit_bb_start() */ | |
280 | ||
281 | /** | |
282 | * Indicate either the size of the hastable used to resolve | |
283 | * relocation handles, or if negative that we are using a direct | |
284 | * index into the execobj[]. | |
285 | */ | |
286 | int lut_size; | |
287 | struct hlist_head *buckets; /** ht for relocation handles */ | |
67731b87 CW |
288 | }; |
289 | ||
c7c6e46f | 290 | #define exec_entry(EB, VMA) (&(EB)->exec[(VMA)->exec_flags - (EB)->flags]) |
4ff4b44c | 291 | |
2889caa9 CW |
292 | /* |
293 | * Used to convert any address to canonical form. | |
294 | * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS, | |
295 | * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the | |
296 | * addresses to be in a canonical form: | |
297 | * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct | |
298 | * canonical form [63:48] == [47]." | |
299 | */ | |
300 | #define GEN8_HIGH_ADDRESS_BIT 47 | |
301 | static inline u64 gen8_canonical_addr(u64 address) | |
302 | { | |
303 | return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT); | |
304 | } | |
305 | ||
306 | static inline u64 gen8_noncanonical_addr(u64 address) | |
307 | { | |
308 | return address & GENMASK_ULL(GEN8_HIGH_ADDRESS_BIT, 0); | |
309 | } | |
310 | ||
3dbf26ed CW |
311 | static inline bool eb_use_cmdparser(const struct i915_execbuffer *eb) |
312 | { | |
439e2ee4 | 313 | return intel_engine_needs_cmd_parser(eb->engine) && eb->batch_len; |
3dbf26ed CW |
314 | } |
315 | ||
650bc635 | 316 | static int eb_create(struct i915_execbuffer *eb) |
67731b87 | 317 | { |
2889caa9 CW |
318 | if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) { |
319 | unsigned int size = 1 + ilog2(eb->buffer_count); | |
4ff4b44c | 320 | |
2889caa9 CW |
321 | /* |
322 | * Without a 1:1 association between relocation handles and | |
323 | * the execobject[] index, we instead create a hashtable. | |
324 | * We size it dynamically based on available memory, starting | |
325 | * first with 1:1 assocative hash and scaling back until | |
326 | * the allocation succeeds. | |
327 | * | |
328 | * Later on we use a positive lut_size to indicate we are | |
329 | * using this hashtable, and a negative value to indicate a | |
330 | * direct lookup. | |
331 | */ | |
4ff4b44c | 332 | do { |
0d95c883 | 333 | gfp_t flags; |
4d470f73 CW |
334 | |
335 | /* While we can still reduce the allocation size, don't | |
336 | * raise a warning and allow the allocation to fail. | |
337 | * On the last pass though, we want to try as hard | |
338 | * as possible to perform the allocation and warn | |
339 | * if it fails. | |
340 | */ | |
0ee931c4 | 341 | flags = GFP_KERNEL; |
4d470f73 CW |
342 | if (size > 1) |
343 | flags |= __GFP_NORETRY | __GFP_NOWARN; | |
344 | ||
4ff4b44c | 345 | eb->buckets = kzalloc(sizeof(struct hlist_head) << size, |
4d470f73 | 346 | flags); |
4ff4b44c CW |
347 | if (eb->buckets) |
348 | break; | |
349 | } while (--size); | |
350 | ||
4d470f73 CW |
351 | if (unlikely(!size)) |
352 | return -ENOMEM; | |
eef90ccb | 353 | |
2889caa9 | 354 | eb->lut_size = size; |
650bc635 | 355 | } else { |
2889caa9 | 356 | eb->lut_size = -eb->buffer_count; |
650bc635 | 357 | } |
eef90ccb | 358 | |
650bc635 | 359 | return 0; |
67731b87 CW |
360 | } |
361 | ||
2889caa9 CW |
362 | static bool |
363 | eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry, | |
c7c6e46f CW |
364 | const struct i915_vma *vma, |
365 | unsigned int flags) | |
2889caa9 | 366 | { |
2889caa9 CW |
367 | if (vma->node.size < entry->pad_to_size) |
368 | return true; | |
369 | ||
370 | if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment)) | |
371 | return true; | |
372 | ||
c7c6e46f | 373 | if (flags & EXEC_OBJECT_PINNED && |
2889caa9 CW |
374 | vma->node.start != entry->offset) |
375 | return true; | |
376 | ||
c7c6e46f | 377 | if (flags & __EXEC_OBJECT_NEEDS_BIAS && |
2889caa9 CW |
378 | vma->node.start < BATCH_OFFSET_BIAS) |
379 | return true; | |
380 | ||
c7c6e46f | 381 | if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) && |
2889caa9 CW |
382 | (vma->node.start + vma->node.size - 1) >> 32) |
383 | return true; | |
384 | ||
1d033beb CW |
385 | if (flags & __EXEC_OBJECT_NEEDS_MAP && |
386 | !i915_vma_is_map_and_fenceable(vma)) | |
387 | return true; | |
388 | ||
2889caa9 CW |
389 | return false; |
390 | } | |
391 | ||
c7c6e46f | 392 | static inline bool |
2889caa9 | 393 | eb_pin_vma(struct i915_execbuffer *eb, |
c7c6e46f | 394 | const struct drm_i915_gem_exec_object2 *entry, |
2889caa9 CW |
395 | struct i915_vma *vma) |
396 | { | |
c7c6e46f CW |
397 | unsigned int exec_flags = *vma->exec_flags; |
398 | u64 pin_flags; | |
2889caa9 | 399 | |
616d9cee | 400 | if (vma->node.size) |
c7c6e46f | 401 | pin_flags = vma->node.start; |
616d9cee | 402 | else |
c7c6e46f | 403 | pin_flags = entry->offset & PIN_OFFSET_MASK; |
616d9cee | 404 | |
c7c6e46f CW |
405 | pin_flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED; |
406 | if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_GTT)) | |
407 | pin_flags |= PIN_GLOBAL; | |
616d9cee | 408 | |
c7c6e46f CW |
409 | if (unlikely(i915_vma_pin(vma, 0, 0, pin_flags))) |
410 | return false; | |
2889caa9 | 411 | |
c7c6e46f | 412 | if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) { |
3bd40735 | 413 | if (unlikely(i915_vma_pin_fence(vma))) { |
2889caa9 | 414 | i915_vma_unpin(vma); |
c7c6e46f | 415 | return false; |
2889caa9 CW |
416 | } |
417 | ||
3bd40735 | 418 | if (vma->fence) |
c7c6e46f | 419 | exec_flags |= __EXEC_OBJECT_HAS_FENCE; |
2889caa9 CW |
420 | } |
421 | ||
c7c6e46f CW |
422 | *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN; |
423 | return !eb_vma_misplaced(entry, vma, exec_flags); | |
2889caa9 CW |
424 | } |
425 | ||
c7c6e46f | 426 | static inline void __eb_unreserve_vma(struct i915_vma *vma, unsigned int flags) |
d55495b4 | 427 | { |
c7c6e46f | 428 | GEM_BUG_ON(!(flags & __EXEC_OBJECT_HAS_PIN)); |
2889caa9 | 429 | |
c7c6e46f | 430 | if (unlikely(flags & __EXEC_OBJECT_HAS_FENCE)) |
3bd40735 | 431 | __i915_vma_unpin_fence(vma); |
d55495b4 | 432 | |
2889caa9 | 433 | __i915_vma_unpin(vma); |
d55495b4 CW |
434 | } |
435 | ||
2889caa9 | 436 | static inline void |
c7c6e46f | 437 | eb_unreserve_vma(struct i915_vma *vma, unsigned int *flags) |
d55495b4 | 438 | { |
c7c6e46f | 439 | if (!(*flags & __EXEC_OBJECT_HAS_PIN)) |
2889caa9 | 440 | return; |
d55495b4 | 441 | |
c7c6e46f CW |
442 | __eb_unreserve_vma(vma, *flags); |
443 | *flags &= ~__EXEC_OBJECT_RESERVED; | |
d55495b4 CW |
444 | } |
445 | ||
2889caa9 CW |
446 | static int |
447 | eb_validate_vma(struct i915_execbuffer *eb, | |
448 | struct drm_i915_gem_exec_object2 *entry, | |
449 | struct i915_vma *vma) | |
67731b87 | 450 | { |
2889caa9 CW |
451 | if (unlikely(entry->flags & eb->invalid_flags)) |
452 | return -EINVAL; | |
d55495b4 | 453 | |
2889caa9 CW |
454 | if (unlikely(entry->alignment && !is_power_of_2(entry->alignment))) |
455 | return -EINVAL; | |
456 | ||
457 | /* | |
458 | * Offset can be used as input (EXEC_OBJECT_PINNED), reject | |
459 | * any non-page-aligned or non-canonical addresses. | |
460 | */ | |
461 | if (unlikely(entry->flags & EXEC_OBJECT_PINNED && | |
6fc4e48f | 462 | entry->offset != gen8_canonical_addr(entry->offset & I915_GTT_PAGE_MASK))) |
2889caa9 CW |
463 | return -EINVAL; |
464 | ||
465 | /* pad_to_size was once a reserved field, so sanitize it */ | |
466 | if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) { | |
467 | if (unlikely(offset_in_page(entry->pad_to_size))) | |
468 | return -EINVAL; | |
469 | } else { | |
470 | entry->pad_to_size = 0; | |
d55495b4 CW |
471 | } |
472 | ||
c7c6e46f | 473 | if (unlikely(vma->exec_flags)) { |
2889caa9 CW |
474 | DRM_DEBUG("Object [handle %d, index %d] appears more than once in object list\n", |
475 | entry->handle, (int)(entry - eb->exec)); | |
476 | return -EINVAL; | |
477 | } | |
478 | ||
479 | /* | |
480 | * From drm_mm perspective address space is continuous, | |
481 | * so from this point we're always using non-canonical | |
482 | * form internally. | |
483 | */ | |
484 | entry->offset = gen8_noncanonical_addr(entry->offset); | |
485 | ||
c7c6e46f CW |
486 | if (!eb->reloc_cache.has_fence) { |
487 | entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE; | |
488 | } else { | |
489 | if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE || | |
490 | eb->reloc_cache.needs_unfenced) && | |
491 | i915_gem_object_is_tiled(vma->obj)) | |
492 | entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP; | |
493 | } | |
494 | ||
495 | if (!(entry->flags & EXEC_OBJECT_PINNED)) | |
496 | entry->flags |= eb->context_flags; | |
497 | ||
2889caa9 | 498 | return 0; |
67731b87 CW |
499 | } |
500 | ||
2889caa9 | 501 | static int |
746c8f14 CW |
502 | eb_add_vma(struct i915_execbuffer *eb, |
503 | unsigned int i, unsigned batch_idx, | |
504 | struct i915_vma *vma) | |
59bfa124 | 505 | { |
c7c6e46f | 506 | struct drm_i915_gem_exec_object2 *entry = &eb->exec[i]; |
2889caa9 CW |
507 | int err; |
508 | ||
509 | GEM_BUG_ON(i915_vma_is_closed(vma)); | |
510 | ||
511 | if (!(eb->args->flags & __EXEC_VALIDATED)) { | |
512 | err = eb_validate_vma(eb, entry, vma); | |
513 | if (unlikely(err)) | |
514 | return err; | |
4ff4b44c | 515 | } |
4ff4b44c | 516 | |
4d470f73 | 517 | if (eb->lut_size > 0) { |
2889caa9 | 518 | vma->exec_handle = entry->handle; |
4ff4b44c | 519 | hlist_add_head(&vma->exec_node, |
2889caa9 CW |
520 | &eb->buckets[hash_32(entry->handle, |
521 | eb->lut_size)]); | |
4ff4b44c | 522 | } |
59bfa124 | 523 | |
2889caa9 CW |
524 | if (entry->relocation_count) |
525 | list_add_tail(&vma->reloc_link, &eb->relocs); | |
526 | ||
2889caa9 CW |
527 | /* |
528 | * Stash a pointer from the vma to execobj, so we can query its flags, | |
529 | * size, alignment etc as provided by the user. Also we stash a pointer | |
530 | * to the vma inside the execobj so that we can use a direct lookup | |
531 | * to find the right target VMA when doing relocations. | |
532 | */ | |
c7c6e46f | 533 | eb->vma[i] = vma; |
d1b48c1e | 534 | eb->flags[i] = entry->flags; |
c7c6e46f | 535 | vma->exec_flags = &eb->flags[i]; |
2889caa9 | 536 | |
746c8f14 CW |
537 | /* |
538 | * SNA is doing fancy tricks with compressing batch buffers, which leads | |
539 | * to negative relocation deltas. Usually that works out ok since the | |
540 | * relocate address is still positive, except when the batch is placed | |
541 | * very low in the GTT. Ensure this doesn't happen. | |
542 | * | |
543 | * Note that actual hangs have only been observed on gen7, but for | |
544 | * paranoia do it everywhere. | |
545 | */ | |
546 | if (i == batch_idx) { | |
827db9d8 CW |
547 | if (entry->relocation_count && |
548 | !(eb->flags[i] & EXEC_OBJECT_PINNED)) | |
746c8f14 CW |
549 | eb->flags[i] |= __EXEC_OBJECT_NEEDS_BIAS; |
550 | if (eb->reloc_cache.has_fence) | |
551 | eb->flags[i] |= EXEC_OBJECT_NEEDS_FENCE; | |
552 | ||
553 | eb->batch = vma; | |
554 | } | |
555 | ||
2889caa9 | 556 | err = 0; |
c7c6e46f | 557 | if (eb_pin_vma(eb, entry, vma)) { |
2889caa9 CW |
558 | if (entry->offset != vma->node.start) { |
559 | entry->offset = vma->node.start | UPDATE; | |
560 | eb->args->flags |= __EXEC_HAS_RELOC; | |
561 | } | |
c7c6e46f CW |
562 | } else { |
563 | eb_unreserve_vma(vma, vma->exec_flags); | |
564 | ||
565 | list_add_tail(&vma->exec_link, &eb->unbound); | |
566 | if (drm_mm_node_allocated(&vma->node)) | |
567 | err = i915_vma_unbind(vma); | |
ed2f3532 CW |
568 | if (unlikely(err)) |
569 | vma->exec_flags = NULL; | |
2889caa9 CW |
570 | } |
571 | return err; | |
572 | } | |
573 | ||
574 | static inline int use_cpu_reloc(const struct reloc_cache *cache, | |
575 | const struct drm_i915_gem_object *obj) | |
576 | { | |
577 | if (!i915_gem_object_has_struct_page(obj)) | |
578 | return false; | |
579 | ||
7dd4f672 CW |
580 | if (DBG_FORCE_RELOC == FORCE_CPU_RELOC) |
581 | return true; | |
582 | ||
583 | if (DBG_FORCE_RELOC == FORCE_GTT_RELOC) | |
584 | return false; | |
2889caa9 CW |
585 | |
586 | return (cache->has_llc || | |
587 | obj->cache_dirty || | |
588 | obj->cache_level != I915_CACHE_NONE); | |
589 | } | |
590 | ||
591 | static int eb_reserve_vma(const struct i915_execbuffer *eb, | |
592 | struct i915_vma *vma) | |
593 | { | |
c7c6e46f CW |
594 | struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma); |
595 | unsigned int exec_flags = *vma->exec_flags; | |
596 | u64 pin_flags; | |
2889caa9 CW |
597 | int err; |
598 | ||
c7c6e46f CW |
599 | pin_flags = PIN_USER | PIN_NONBLOCK; |
600 | if (exec_flags & EXEC_OBJECT_NEEDS_GTT) | |
601 | pin_flags |= PIN_GLOBAL; | |
2889caa9 CW |
602 | |
603 | /* | |
604 | * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset, | |
605 | * limit address to the first 4GBs for unflagged objects. | |
606 | */ | |
c7c6e46f CW |
607 | if (!(exec_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS)) |
608 | pin_flags |= PIN_ZONE_4G; | |
2889caa9 | 609 | |
c7c6e46f CW |
610 | if (exec_flags & __EXEC_OBJECT_NEEDS_MAP) |
611 | pin_flags |= PIN_MAPPABLE; | |
2889caa9 | 612 | |
c7c6e46f CW |
613 | if (exec_flags & EXEC_OBJECT_PINNED) { |
614 | pin_flags |= entry->offset | PIN_OFFSET_FIXED; | |
615 | pin_flags &= ~PIN_NONBLOCK; /* force overlapping checks */ | |
616 | } else if (exec_flags & __EXEC_OBJECT_NEEDS_BIAS) { | |
617 | pin_flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS; | |
2889caa9 CW |
618 | } |
619 | ||
c7c6e46f CW |
620 | err = i915_vma_pin(vma, |
621 | entry->pad_to_size, entry->alignment, | |
622 | pin_flags); | |
2889caa9 CW |
623 | if (err) |
624 | return err; | |
625 | ||
626 | if (entry->offset != vma->node.start) { | |
627 | entry->offset = vma->node.start | UPDATE; | |
628 | eb->args->flags |= __EXEC_HAS_RELOC; | |
629 | } | |
630 | ||
c7c6e46f | 631 | if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) { |
3bd40735 | 632 | err = i915_vma_pin_fence(vma); |
2889caa9 CW |
633 | if (unlikely(err)) { |
634 | i915_vma_unpin(vma); | |
635 | return err; | |
636 | } | |
637 | ||
3bd40735 | 638 | if (vma->fence) |
c7c6e46f | 639 | exec_flags |= __EXEC_OBJECT_HAS_FENCE; |
2889caa9 CW |
640 | } |
641 | ||
c7c6e46f CW |
642 | *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN; |
643 | GEM_BUG_ON(eb_vma_misplaced(entry, vma, exec_flags)); | |
1da7b54c | 644 | |
2889caa9 CW |
645 | return 0; |
646 | } | |
647 | ||
648 | static int eb_reserve(struct i915_execbuffer *eb) | |
649 | { | |
650 | const unsigned int count = eb->buffer_count; | |
651 | struct list_head last; | |
652 | struct i915_vma *vma; | |
653 | unsigned int i, pass; | |
654 | int err; | |
655 | ||
656 | /* | |
657 | * Attempt to pin all of the buffers into the GTT. | |
658 | * This is done in 3 phases: | |
659 | * | |
660 | * 1a. Unbind all objects that do not match the GTT constraints for | |
661 | * the execbuffer (fenceable, mappable, alignment etc). | |
662 | * 1b. Increment pin count for already bound objects. | |
663 | * 2. Bind new objects. | |
664 | * 3. Decrement pin count. | |
665 | * | |
666 | * This avoid unnecessary unbinding of later objects in order to make | |
667 | * room for the earlier objects *unless* we need to defragment. | |
668 | */ | |
669 | ||
670 | pass = 0; | |
671 | err = 0; | |
672 | do { | |
673 | list_for_each_entry(vma, &eb->unbound, exec_link) { | |
674 | err = eb_reserve_vma(eb, vma); | |
675 | if (err) | |
676 | break; | |
677 | } | |
678 | if (err != -ENOSPC) | |
679 | return err; | |
680 | ||
681 | /* Resort *all* the objects into priority order */ | |
682 | INIT_LIST_HEAD(&eb->unbound); | |
683 | INIT_LIST_HEAD(&last); | |
684 | for (i = 0; i < count; i++) { | |
c7c6e46f CW |
685 | unsigned int flags = eb->flags[i]; |
686 | struct i915_vma *vma = eb->vma[i]; | |
2889caa9 | 687 | |
c7c6e46f CW |
688 | if (flags & EXEC_OBJECT_PINNED && |
689 | flags & __EXEC_OBJECT_HAS_PIN) | |
2889caa9 CW |
690 | continue; |
691 | ||
c7c6e46f | 692 | eb_unreserve_vma(vma, &eb->flags[i]); |
2889caa9 | 693 | |
c7c6e46f | 694 | if (flags & EXEC_OBJECT_PINNED) |
35e882a4 | 695 | /* Pinned must have their slot */ |
2889caa9 | 696 | list_add(&vma->exec_link, &eb->unbound); |
c7c6e46f | 697 | else if (flags & __EXEC_OBJECT_NEEDS_MAP) |
35e882a4 | 698 | /* Map require the lowest 256MiB (aperture) */ |
2889caa9 | 699 | list_add_tail(&vma->exec_link, &eb->unbound); |
35e882a4 CW |
700 | else if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS)) |
701 | /* Prioritise 4GiB region for restricted bo */ | |
702 | list_add(&vma->exec_link, &last); | |
2889caa9 CW |
703 | else |
704 | list_add_tail(&vma->exec_link, &last); | |
705 | } | |
706 | list_splice_tail(&last, &eb->unbound); | |
707 | ||
708 | switch (pass++) { | |
709 | case 0: | |
710 | break; | |
711 | ||
712 | case 1: | |
713 | /* Too fragmented, unbind everything and retry */ | |
714 | err = i915_gem_evict_vm(eb->vm); | |
715 | if (err) | |
716 | return err; | |
717 | break; | |
718 | ||
719 | default: | |
720 | return -ENOSPC; | |
721 | } | |
722 | } while (1); | |
4ff4b44c | 723 | } |
59bfa124 | 724 | |
2889caa9 CW |
725 | static unsigned int eb_batch_index(const struct i915_execbuffer *eb) |
726 | { | |
1a71cf2f CW |
727 | if (eb->args->flags & I915_EXEC_BATCH_FIRST) |
728 | return 0; | |
729 | else | |
730 | return eb->buffer_count - 1; | |
2889caa9 CW |
731 | } |
732 | ||
733 | static int eb_select_context(struct i915_execbuffer *eb) | |
734 | { | |
735 | struct i915_gem_context *ctx; | |
736 | ||
737 | ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1); | |
1acfc104 CW |
738 | if (unlikely(!ctx)) |
739 | return -ENOENT; | |
2889caa9 | 740 | |
1acfc104 | 741 | eb->ctx = ctx; |
4f2c7337 CW |
742 | if (ctx->ppgtt) { |
743 | eb->vm = &ctx->ppgtt->vm; | |
744 | eb->invalid_flags |= EXEC_OBJECT_NEEDS_GTT; | |
745 | } else { | |
746 | eb->vm = &eb->i915->ggtt.vm; | |
747 | } | |
2889caa9 CW |
748 | |
749 | eb->context_flags = 0; | |
d3f3e5e4 | 750 | if (test_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags)) |
2889caa9 CW |
751 | eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS; |
752 | ||
753 | return 0; | |
754 | } | |
755 | ||
756 | static int eb_lookup_vmas(struct i915_execbuffer *eb) | |
3b96eff4 | 757 | { |
d1b48c1e | 758 | struct radix_tree_root *handles_vma = &eb->ctx->handles_vma; |
ac70ebe8 | 759 | struct drm_i915_gem_object *obj; |
746c8f14 | 760 | unsigned int i, batch; |
2889caa9 | 761 | int err; |
3b96eff4 | 762 | |
8bcbfb12 CW |
763 | if (unlikely(i915_gem_context_is_closed(eb->ctx))) |
764 | return -ENOENT; | |
765 | ||
766 | if (unlikely(i915_gem_context_is_banned(eb->ctx))) | |
767 | return -EIO; | |
768 | ||
2889caa9 CW |
769 | INIT_LIST_HEAD(&eb->relocs); |
770 | INIT_LIST_HEAD(&eb->unbound); | |
d55495b4 | 771 | |
746c8f14 CW |
772 | batch = eb_batch_index(eb); |
773 | ||
170fa29b CW |
774 | for (i = 0; i < eb->buffer_count; i++) { |
775 | u32 handle = eb->exec[i].handle; | |
d1b48c1e | 776 | struct i915_lut_handle *lut; |
170fa29b | 777 | struct i915_vma *vma; |
4ff4b44c | 778 | |
d1b48c1e CW |
779 | vma = radix_tree_lookup(handles_vma, handle); |
780 | if (likely(vma)) | |
170fa29b | 781 | goto add_vma; |
4ff4b44c | 782 | |
170fa29b | 783 | obj = i915_gem_object_lookup(eb->file, handle); |
4ff4b44c | 784 | if (unlikely(!obj)) { |
2889caa9 | 785 | err = -ENOENT; |
170fa29b | 786 | goto err_vma; |
3b96eff4 CW |
787 | } |
788 | ||
650bc635 | 789 | vma = i915_vma_instance(obj, eb->vm, NULL); |
058d88c4 | 790 | if (unlikely(IS_ERR(vma))) { |
2889caa9 | 791 | err = PTR_ERR(vma); |
170fa29b | 792 | goto err_obj; |
27173f1f BW |
793 | } |
794 | ||
d1b48c1e CW |
795 | lut = kmem_cache_alloc(eb->i915->luts, GFP_KERNEL); |
796 | if (unlikely(!lut)) { | |
797 | err = -ENOMEM; | |
798 | goto err_obj; | |
799 | } | |
800 | ||
801 | err = radix_tree_insert(handles_vma, handle, vma); | |
802 | if (unlikely(err)) { | |
6be1187d | 803 | kmem_cache_free(eb->i915->luts, lut); |
d1b48c1e | 804 | goto err_obj; |
eef90ccb | 805 | } |
4ff4b44c | 806 | |
ac70ebe8 | 807 | /* transfer ref to ctx */ |
3365e226 CW |
808 | if (!vma->open_count++) |
809 | i915_vma_reopen(vma); | |
d1b48c1e CW |
810 | list_add(&lut->obj_link, &obj->lut_list); |
811 | list_add(&lut->ctx_link, &eb->ctx->handles_list); | |
812 | lut->ctx = eb->ctx; | |
813 | lut->handle = handle; | |
814 | ||
170fa29b | 815 | add_vma: |
746c8f14 | 816 | err = eb_add_vma(eb, i, batch, vma); |
2889caa9 | 817 | if (unlikely(err)) |
ac70ebe8 | 818 | goto err_vma; |
dade2a61 | 819 | |
c7c6e46f CW |
820 | GEM_BUG_ON(vma != eb->vma[i]); |
821 | GEM_BUG_ON(vma->exec_flags != &eb->flags[i]); | |
746c8f14 CW |
822 | GEM_BUG_ON(drm_mm_node_allocated(&vma->node) && |
823 | eb_vma_misplaced(&eb->exec[i], vma, eb->flags[i])); | |
4ff4b44c CW |
824 | } |
825 | ||
2889caa9 CW |
826 | eb->args->flags |= __EXEC_VALIDATED; |
827 | return eb_reserve(eb); | |
828 | ||
170fa29b | 829 | err_obj: |
ac70ebe8 | 830 | i915_gem_object_put(obj); |
170fa29b CW |
831 | err_vma: |
832 | eb->vma[i] = NULL; | |
2889caa9 | 833 | return err; |
3b96eff4 CW |
834 | } |
835 | ||
4ff4b44c | 836 | static struct i915_vma * |
2889caa9 | 837 | eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle) |
67731b87 | 838 | { |
2889caa9 CW |
839 | if (eb->lut_size < 0) { |
840 | if (handle >= -eb->lut_size) | |
eef90ccb | 841 | return NULL; |
c7c6e46f | 842 | return eb->vma[handle]; |
eef90ccb CW |
843 | } else { |
844 | struct hlist_head *head; | |
aa45950b | 845 | struct i915_vma *vma; |
67731b87 | 846 | |
2889caa9 | 847 | head = &eb->buckets[hash_32(handle, eb->lut_size)]; |
aa45950b | 848 | hlist_for_each_entry(vma, head, exec_node) { |
27173f1f BW |
849 | if (vma->exec_handle == handle) |
850 | return vma; | |
eef90ccb CW |
851 | } |
852 | return NULL; | |
853 | } | |
67731b87 CW |
854 | } |
855 | ||
2889caa9 | 856 | static void eb_release_vmas(const struct i915_execbuffer *eb) |
a415d355 | 857 | { |
2889caa9 CW |
858 | const unsigned int count = eb->buffer_count; |
859 | unsigned int i; | |
860 | ||
861 | for (i = 0; i < count; i++) { | |
c7c6e46f CW |
862 | struct i915_vma *vma = eb->vma[i]; |
863 | unsigned int flags = eb->flags[i]; | |
650bc635 | 864 | |
2889caa9 | 865 | if (!vma) |
170fa29b | 866 | break; |
bcffc3fa | 867 | |
c7c6e46f CW |
868 | GEM_BUG_ON(vma->exec_flags != &eb->flags[i]); |
869 | vma->exec_flags = NULL; | |
870 | eb->vma[i] = NULL; | |
9e53d9be | 871 | |
c7c6e46f CW |
872 | if (flags & __EXEC_OBJECT_HAS_PIN) |
873 | __eb_unreserve_vma(vma, flags); | |
dade2a61 | 874 | |
c7c6e46f | 875 | if (flags & __EXEC_OBJECT_HAS_REF) |
dade2a61 | 876 | i915_vma_put(vma); |
2889caa9 | 877 | } |
dabdfe02 CW |
878 | } |
879 | ||
2889caa9 | 880 | static void eb_reset_vmas(const struct i915_execbuffer *eb) |
934acce3 | 881 | { |
2889caa9 | 882 | eb_release_vmas(eb); |
4d470f73 | 883 | if (eb->lut_size > 0) |
2889caa9 CW |
884 | memset(eb->buckets, 0, |
885 | sizeof(struct hlist_head) << eb->lut_size); | |
934acce3 MW |
886 | } |
887 | ||
2889caa9 | 888 | static void eb_destroy(const struct i915_execbuffer *eb) |
934acce3 | 889 | { |
7dd4f672 CW |
890 | GEM_BUG_ON(eb->reloc_cache.rq); |
891 | ||
4d470f73 | 892 | if (eb->lut_size > 0) |
2889caa9 | 893 | kfree(eb->buckets); |
934acce3 MW |
894 | } |
895 | ||
2889caa9 | 896 | static inline u64 |
d50415cc | 897 | relocation_target(const struct drm_i915_gem_relocation_entry *reloc, |
2889caa9 | 898 | const struct i915_vma *target) |
934acce3 | 899 | { |
2889caa9 | 900 | return gen8_canonical_addr((int)reloc->delta + target->node.start); |
934acce3 MW |
901 | } |
902 | ||
d50415cc CW |
903 | static void reloc_cache_init(struct reloc_cache *cache, |
904 | struct drm_i915_private *i915) | |
5032d871 | 905 | { |
31a39207 | 906 | cache->page = -1; |
d50415cc | 907 | cache->vaddr = 0; |
dfc5148f | 908 | /* Must be a variable in the struct to allow GCC to unroll. */ |
7dd4f672 | 909 | cache->gen = INTEL_GEN(i915); |
2889caa9 | 910 | cache->has_llc = HAS_LLC(i915); |
dfc5148f | 911 | cache->use_64bit_reloc = HAS_64BIT_RELOC(i915); |
7dd4f672 CW |
912 | cache->has_fence = cache->gen < 4; |
913 | cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment; | |
e8cb909a | 914 | cache->node.allocated = false; |
7dd4f672 CW |
915 | cache->rq = NULL; |
916 | cache->rq_size = 0; | |
d50415cc | 917 | } |
5032d871 | 918 | |
d50415cc CW |
919 | static inline void *unmask_page(unsigned long p) |
920 | { | |
921 | return (void *)(uintptr_t)(p & PAGE_MASK); | |
922 | } | |
923 | ||
924 | static inline unsigned int unmask_flags(unsigned long p) | |
925 | { | |
926 | return p & ~PAGE_MASK; | |
31a39207 CW |
927 | } |
928 | ||
d50415cc CW |
929 | #define KMAP 0x4 /* after CLFLUSH_FLAGS */ |
930 | ||
650bc635 CW |
931 | static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache) |
932 | { | |
933 | struct drm_i915_private *i915 = | |
934 | container_of(cache, struct i915_execbuffer, reloc_cache)->i915; | |
935 | return &i915->ggtt; | |
936 | } | |
937 | ||
7dd4f672 CW |
938 | static void reloc_gpu_flush(struct reloc_cache *cache) |
939 | { | |
940 | GEM_BUG_ON(cache->rq_size >= cache->rq->batch->obj->base.size / sizeof(u32)); | |
941 | cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END; | |
942 | i915_gem_object_unpin_map(cache->rq->batch->obj); | |
943 | i915_gem_chipset_flush(cache->rq->i915); | |
944 | ||
697b9a87 | 945 | i915_request_add(cache->rq); |
7dd4f672 CW |
946 | cache->rq = NULL; |
947 | } | |
948 | ||
650bc635 | 949 | static void reloc_cache_reset(struct reloc_cache *cache) |
31a39207 | 950 | { |
d50415cc | 951 | void *vaddr; |
5032d871 | 952 | |
7dd4f672 CW |
953 | if (cache->rq) |
954 | reloc_gpu_flush(cache); | |
955 | ||
31a39207 CW |
956 | if (!cache->vaddr) |
957 | return; | |
3c94ceee | 958 | |
d50415cc CW |
959 | vaddr = unmask_page(cache->vaddr); |
960 | if (cache->vaddr & KMAP) { | |
961 | if (cache->vaddr & CLFLUSH_AFTER) | |
962 | mb(); | |
3c94ceee | 963 | |
d50415cc CW |
964 | kunmap_atomic(vaddr); |
965 | i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm); | |
966 | } else { | |
e8cb909a | 967 | wmb(); |
d50415cc | 968 | io_mapping_unmap_atomic((void __iomem *)vaddr); |
e8cb909a | 969 | if (cache->node.allocated) { |
650bc635 | 970 | struct i915_ggtt *ggtt = cache_to_ggtt(cache); |
e8cb909a | 971 | |
82ad6443 CW |
972 | ggtt->vm.clear_range(&ggtt->vm, |
973 | cache->node.start, | |
974 | cache->node.size); | |
e8cb909a CW |
975 | drm_mm_remove_node(&cache->node); |
976 | } else { | |
977 | i915_vma_unpin((struct i915_vma *)cache->node.mm); | |
3c94ceee | 978 | } |
31a39207 | 979 | } |
650bc635 CW |
980 | |
981 | cache->vaddr = 0; | |
982 | cache->page = -1; | |
31a39207 CW |
983 | } |
984 | ||
985 | static void *reloc_kmap(struct drm_i915_gem_object *obj, | |
986 | struct reloc_cache *cache, | |
2889caa9 | 987 | unsigned long page) |
31a39207 | 988 | { |
d50415cc CW |
989 | void *vaddr; |
990 | ||
991 | if (cache->vaddr) { | |
992 | kunmap_atomic(unmask_page(cache->vaddr)); | |
993 | } else { | |
994 | unsigned int flushes; | |
2889caa9 | 995 | int err; |
31a39207 | 996 | |
2889caa9 CW |
997 | err = i915_gem_obj_prepare_shmem_write(obj, &flushes); |
998 | if (err) | |
999 | return ERR_PTR(err); | |
d50415cc CW |
1000 | |
1001 | BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS); | |
1002 | BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK); | |
3c94ceee | 1003 | |
d50415cc CW |
1004 | cache->vaddr = flushes | KMAP; |
1005 | cache->node.mm = (void *)obj; | |
1006 | if (flushes) | |
1007 | mb(); | |
3c94ceee BW |
1008 | } |
1009 | ||
d50415cc CW |
1010 | vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page)); |
1011 | cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr; | |
31a39207 | 1012 | cache->page = page; |
5032d871 | 1013 | |
d50415cc | 1014 | return vaddr; |
5032d871 RB |
1015 | } |
1016 | ||
d50415cc CW |
1017 | static void *reloc_iomap(struct drm_i915_gem_object *obj, |
1018 | struct reloc_cache *cache, | |
2889caa9 | 1019 | unsigned long page) |
5032d871 | 1020 | { |
650bc635 | 1021 | struct i915_ggtt *ggtt = cache_to_ggtt(cache); |
e8cb909a | 1022 | unsigned long offset; |
d50415cc | 1023 | void *vaddr; |
5032d871 | 1024 | |
d50415cc | 1025 | if (cache->vaddr) { |
615e5000 | 1026 | io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr)); |
d50415cc CW |
1027 | } else { |
1028 | struct i915_vma *vma; | |
2889caa9 | 1029 | int err; |
5032d871 | 1030 | |
2889caa9 | 1031 | if (use_cpu_reloc(cache, obj)) |
d50415cc | 1032 | return NULL; |
3c94ceee | 1033 | |
2889caa9 CW |
1034 | err = i915_gem_object_set_to_gtt_domain(obj, true); |
1035 | if (err) | |
1036 | return ERR_PTR(err); | |
3c94ceee | 1037 | |
d50415cc | 1038 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
3c755c5b CW |
1039 | PIN_MAPPABLE | |
1040 | PIN_NONBLOCK | | |
1041 | PIN_NONFAULT); | |
e8cb909a CW |
1042 | if (IS_ERR(vma)) { |
1043 | memset(&cache->node, 0, sizeof(cache->node)); | |
2889caa9 | 1044 | err = drm_mm_insert_node_in_range |
82ad6443 | 1045 | (&ggtt->vm.mm, &cache->node, |
f51455d4 | 1046 | PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE, |
e8cb909a | 1047 | 0, ggtt->mappable_end, |
4e64e553 | 1048 | DRM_MM_INSERT_LOW); |
2889caa9 | 1049 | if (err) /* no inactive aperture space, use cpu reloc */ |
c92fa4fe | 1050 | return NULL; |
e8cb909a | 1051 | } else { |
2889caa9 CW |
1052 | err = i915_vma_put_fence(vma); |
1053 | if (err) { | |
e8cb909a | 1054 | i915_vma_unpin(vma); |
2889caa9 | 1055 | return ERR_PTR(err); |
e8cb909a | 1056 | } |
5032d871 | 1057 | |
e8cb909a CW |
1058 | cache->node.start = vma->node.start; |
1059 | cache->node.mm = (void *)vma; | |
3c94ceee | 1060 | } |
e8cb909a | 1061 | } |
3c94ceee | 1062 | |
e8cb909a CW |
1063 | offset = cache->node.start; |
1064 | if (cache->node.allocated) { | |
fc099090 | 1065 | wmb(); |
82ad6443 CW |
1066 | ggtt->vm.insert_page(&ggtt->vm, |
1067 | i915_gem_object_get_dma_address(obj, page), | |
1068 | offset, I915_CACHE_NONE, 0); | |
e8cb909a CW |
1069 | } else { |
1070 | offset += page << PAGE_SHIFT; | |
3c94ceee BW |
1071 | } |
1072 | ||
73ebd503 | 1073 | vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->iomap, |
650bc635 | 1074 | offset); |
d50415cc CW |
1075 | cache->page = page; |
1076 | cache->vaddr = (unsigned long)vaddr; | |
5032d871 | 1077 | |
d50415cc | 1078 | return vaddr; |
5032d871 RB |
1079 | } |
1080 | ||
d50415cc CW |
1081 | static void *reloc_vaddr(struct drm_i915_gem_object *obj, |
1082 | struct reloc_cache *cache, | |
2889caa9 | 1083 | unsigned long page) |
edf4427b | 1084 | { |
d50415cc | 1085 | void *vaddr; |
5032d871 | 1086 | |
d50415cc CW |
1087 | if (cache->page == page) { |
1088 | vaddr = unmask_page(cache->vaddr); | |
1089 | } else { | |
1090 | vaddr = NULL; | |
1091 | if ((cache->vaddr & KMAP) == 0) | |
1092 | vaddr = reloc_iomap(obj, cache, page); | |
1093 | if (!vaddr) | |
1094 | vaddr = reloc_kmap(obj, cache, page); | |
3c94ceee BW |
1095 | } |
1096 | ||
d50415cc | 1097 | return vaddr; |
edf4427b CW |
1098 | } |
1099 | ||
d50415cc | 1100 | static void clflush_write32(u32 *addr, u32 value, unsigned int flushes) |
edf4427b | 1101 | { |
d50415cc CW |
1102 | if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) { |
1103 | if (flushes & CLFLUSH_BEFORE) { | |
1104 | clflushopt(addr); | |
1105 | mb(); | |
1106 | } | |
edf4427b | 1107 | |
d50415cc | 1108 | *addr = value; |
edf4427b | 1109 | |
2889caa9 CW |
1110 | /* |
1111 | * Writes to the same cacheline are serialised by the CPU | |
d50415cc CW |
1112 | * (including clflush). On the write path, we only require |
1113 | * that it hits memory in an orderly fashion and place | |
1114 | * mb barriers at the start and end of the relocation phase | |
1115 | * to ensure ordering of clflush wrt to the system. | |
1116 | */ | |
1117 | if (flushes & CLFLUSH_AFTER) | |
1118 | clflushopt(addr); | |
1119 | } else | |
1120 | *addr = value; | |
edf4427b | 1121 | } |
edf4427b | 1122 | |
7dd4f672 CW |
1123 | static int __reloc_gpu_alloc(struct i915_execbuffer *eb, |
1124 | struct i915_vma *vma, | |
1125 | unsigned int len) | |
1126 | { | |
1127 | struct reloc_cache *cache = &eb->reloc_cache; | |
1128 | struct drm_i915_gem_object *obj; | |
e61e0f51 | 1129 | struct i915_request *rq; |
7dd4f672 CW |
1130 | struct i915_vma *batch; |
1131 | u32 *cmd; | |
1132 | int err; | |
1133 | ||
46223993 CW |
1134 | if (DBG_FORCE_RELOC == FORCE_GPU_RELOC) { |
1135 | obj = vma->obj; | |
1136 | if (obj->cache_dirty & ~obj->cache_coherent) | |
1137 | i915_gem_clflush_object(obj, 0); | |
1138 | obj->write_domain = 0; | |
1139 | } | |
1140 | ||
c0a51fd0 | 1141 | GEM_BUG_ON(vma->obj->write_domain & I915_GEM_DOMAIN_CPU); |
7dd4f672 CW |
1142 | |
1143 | obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, PAGE_SIZE); | |
1144 | if (IS_ERR(obj)) | |
1145 | return PTR_ERR(obj); | |
1146 | ||
1147 | cmd = i915_gem_object_pin_map(obj, | |
a575c676 CW |
1148 | cache->has_llc ? |
1149 | I915_MAP_FORCE_WB : | |
1150 | I915_MAP_FORCE_WC); | |
7dd4f672 CW |
1151 | i915_gem_object_unpin_pages(obj); |
1152 | if (IS_ERR(cmd)) | |
1153 | return PTR_ERR(cmd); | |
1154 | ||
1155 | err = i915_gem_object_set_to_wc_domain(obj, false); | |
1156 | if (err) | |
1157 | goto err_unmap; | |
1158 | ||
1159 | batch = i915_vma_instance(obj, vma->vm, NULL); | |
1160 | if (IS_ERR(batch)) { | |
1161 | err = PTR_ERR(batch); | |
1162 | goto err_unmap; | |
1163 | } | |
1164 | ||
1165 | err = i915_vma_pin(batch, 0, 0, PIN_USER | PIN_NONBLOCK); | |
1166 | if (err) | |
1167 | goto err_unmap; | |
1168 | ||
e61e0f51 | 1169 | rq = i915_request_alloc(eb->engine, eb->ctx); |
7dd4f672 CW |
1170 | if (IS_ERR(rq)) { |
1171 | err = PTR_ERR(rq); | |
1172 | goto err_unpin; | |
1173 | } | |
1174 | ||
e61e0f51 | 1175 | err = i915_request_await_object(rq, vma->obj, true); |
7dd4f672 CW |
1176 | if (err) |
1177 | goto err_request; | |
1178 | ||
7dd4f672 CW |
1179 | err = eb->engine->emit_bb_start(rq, |
1180 | batch->node.start, PAGE_SIZE, | |
1181 | cache->gen > 5 ? 0 : I915_DISPATCH_SECURE); | |
1182 | if (err) | |
1183 | goto err_request; | |
1184 | ||
95ff7c7d | 1185 | GEM_BUG_ON(!reservation_object_test_signaled_rcu(batch->resv, true)); |
a5236978 CW |
1186 | err = i915_vma_move_to_active(batch, rq, 0); |
1187 | if (err) | |
1188 | goto skip_request; | |
7dd4f672 | 1189 | |
a5236978 CW |
1190 | err = i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE); |
1191 | if (err) | |
1192 | goto skip_request; | |
7dd4f672 CW |
1193 | |
1194 | rq->batch = batch; | |
a5236978 | 1195 | i915_vma_unpin(batch); |
7dd4f672 CW |
1196 | |
1197 | cache->rq = rq; | |
1198 | cache->rq_cmd = cmd; | |
1199 | cache->rq_size = 0; | |
1200 | ||
1201 | /* Return with batch mapping (cmd) still pinned */ | |
1202 | return 0; | |
1203 | ||
a5236978 CW |
1204 | skip_request: |
1205 | i915_request_skip(rq, err); | |
7dd4f672 | 1206 | err_request: |
e61e0f51 | 1207 | i915_request_add(rq); |
7dd4f672 CW |
1208 | err_unpin: |
1209 | i915_vma_unpin(batch); | |
1210 | err_unmap: | |
1211 | i915_gem_object_unpin_map(obj); | |
1212 | return err; | |
1213 | } | |
1214 | ||
1215 | static u32 *reloc_gpu(struct i915_execbuffer *eb, | |
1216 | struct i915_vma *vma, | |
1217 | unsigned int len) | |
1218 | { | |
1219 | struct reloc_cache *cache = &eb->reloc_cache; | |
1220 | u32 *cmd; | |
1221 | ||
1222 | if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1)) | |
1223 | reloc_gpu_flush(cache); | |
1224 | ||
1225 | if (unlikely(!cache->rq)) { | |
1226 | int err; | |
1227 | ||
3dbf26ed CW |
1228 | /* If we need to copy for the cmdparser, we will stall anyway */ |
1229 | if (eb_use_cmdparser(eb)) | |
1230 | return ERR_PTR(-EWOULDBLOCK); | |
1231 | ||
90cad095 CW |
1232 | if (!intel_engine_can_store_dword(eb->engine)) |
1233 | return ERR_PTR(-ENODEV); | |
1234 | ||
7dd4f672 CW |
1235 | err = __reloc_gpu_alloc(eb, vma, len); |
1236 | if (unlikely(err)) | |
1237 | return ERR_PTR(err); | |
1238 | } | |
1239 | ||
1240 | cmd = cache->rq_cmd + cache->rq_size; | |
1241 | cache->rq_size += len; | |
1242 | ||
1243 | return cmd; | |
1244 | } | |
1245 | ||
2889caa9 CW |
1246 | static u64 |
1247 | relocate_entry(struct i915_vma *vma, | |
d50415cc | 1248 | const struct drm_i915_gem_relocation_entry *reloc, |
2889caa9 CW |
1249 | struct i915_execbuffer *eb, |
1250 | const struct i915_vma *target) | |
edf4427b | 1251 | { |
d50415cc | 1252 | u64 offset = reloc->offset; |
2889caa9 CW |
1253 | u64 target_offset = relocation_target(reloc, target); |
1254 | bool wide = eb->reloc_cache.use_64bit_reloc; | |
d50415cc | 1255 | void *vaddr; |
edf4427b | 1256 | |
7dd4f672 CW |
1257 | if (!eb->reloc_cache.vaddr && |
1258 | (DBG_FORCE_RELOC == FORCE_GPU_RELOC || | |
90cad095 | 1259 | !reservation_object_test_signaled_rcu(vma->resv, true))) { |
7dd4f672 CW |
1260 | const unsigned int gen = eb->reloc_cache.gen; |
1261 | unsigned int len; | |
1262 | u32 *batch; | |
1263 | u64 addr; | |
1264 | ||
1265 | if (wide) | |
1266 | len = offset & 7 ? 8 : 5; | |
1267 | else if (gen >= 4) | |
1268 | len = 4; | |
f2f5c061 | 1269 | else |
a889580c | 1270 | len = 3; |
7dd4f672 CW |
1271 | |
1272 | batch = reloc_gpu(eb, vma, len); | |
1273 | if (IS_ERR(batch)) | |
1274 | goto repeat; | |
1275 | ||
1276 | addr = gen8_canonical_addr(vma->node.start + offset); | |
1277 | if (wide) { | |
1278 | if (offset & 7) { | |
1279 | *batch++ = MI_STORE_DWORD_IMM_GEN4; | |
1280 | *batch++ = lower_32_bits(addr); | |
1281 | *batch++ = upper_32_bits(addr); | |
1282 | *batch++ = lower_32_bits(target_offset); | |
1283 | ||
1284 | addr = gen8_canonical_addr(addr + 4); | |
1285 | ||
1286 | *batch++ = MI_STORE_DWORD_IMM_GEN4; | |
1287 | *batch++ = lower_32_bits(addr); | |
1288 | *batch++ = upper_32_bits(addr); | |
1289 | *batch++ = upper_32_bits(target_offset); | |
1290 | } else { | |
1291 | *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1; | |
1292 | *batch++ = lower_32_bits(addr); | |
1293 | *batch++ = upper_32_bits(addr); | |
1294 | *batch++ = lower_32_bits(target_offset); | |
1295 | *batch++ = upper_32_bits(target_offset); | |
1296 | } | |
1297 | } else if (gen >= 6) { | |
1298 | *batch++ = MI_STORE_DWORD_IMM_GEN4; | |
1299 | *batch++ = 0; | |
1300 | *batch++ = addr; | |
1301 | *batch++ = target_offset; | |
1302 | } else if (gen >= 4) { | |
1303 | *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT; | |
1304 | *batch++ = 0; | |
1305 | *batch++ = addr; | |
1306 | *batch++ = target_offset; | |
1307 | } else { | |
1308 | *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL; | |
1309 | *batch++ = addr; | |
1310 | *batch++ = target_offset; | |
1311 | } | |
1312 | ||
1313 | goto out; | |
1314 | } | |
1315 | ||
d50415cc | 1316 | repeat: |
95ff7c7d | 1317 | vaddr = reloc_vaddr(vma->obj, &eb->reloc_cache, offset >> PAGE_SHIFT); |
d50415cc CW |
1318 | if (IS_ERR(vaddr)) |
1319 | return PTR_ERR(vaddr); | |
1320 | ||
1321 | clflush_write32(vaddr + offset_in_page(offset), | |
1322 | lower_32_bits(target_offset), | |
2889caa9 | 1323 | eb->reloc_cache.vaddr); |
d50415cc CW |
1324 | |
1325 | if (wide) { | |
1326 | offset += sizeof(u32); | |
1327 | target_offset >>= 32; | |
1328 | wide = false; | |
1329 | goto repeat; | |
edf4427b | 1330 | } |
edf4427b | 1331 | |
7dd4f672 | 1332 | out: |
2889caa9 | 1333 | return target->node.start | UPDATE; |
edf4427b | 1334 | } |
edf4427b | 1335 | |
2889caa9 CW |
1336 | static u64 |
1337 | eb_relocate_entry(struct i915_execbuffer *eb, | |
1338 | struct i915_vma *vma, | |
1339 | const struct drm_i915_gem_relocation_entry *reloc) | |
54cf91dc | 1340 | { |
507d977f | 1341 | struct i915_vma *target; |
2889caa9 | 1342 | int err; |
54cf91dc | 1343 | |
67731b87 | 1344 | /* we've already hold a reference to all valid objects */ |
507d977f CW |
1345 | target = eb_get_vma(eb, reloc->target_handle); |
1346 | if (unlikely(!target)) | |
54cf91dc | 1347 | return -ENOENT; |
e844b990 | 1348 | |
54cf91dc | 1349 | /* Validate that the target is in a valid r/w GPU domain */ |
b8f7ab17 | 1350 | if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) { |
ff240199 | 1351 | DRM_DEBUG("reloc with multiple write domains: " |
507d977f | 1352 | "target %d offset %d " |
54cf91dc | 1353 | "read %08x write %08x", |
507d977f | 1354 | reloc->target_handle, |
54cf91dc CW |
1355 | (int) reloc->offset, |
1356 | reloc->read_domains, | |
1357 | reloc->write_domain); | |
8b78f0e5 | 1358 | return -EINVAL; |
54cf91dc | 1359 | } |
4ca4a250 DV |
1360 | if (unlikely((reloc->write_domain | reloc->read_domains) |
1361 | & ~I915_GEM_GPU_DOMAINS)) { | |
ff240199 | 1362 | DRM_DEBUG("reloc with read/write non-GPU domains: " |
507d977f | 1363 | "target %d offset %d " |
54cf91dc | 1364 | "read %08x write %08x", |
507d977f | 1365 | reloc->target_handle, |
54cf91dc CW |
1366 | (int) reloc->offset, |
1367 | reloc->read_domains, | |
1368 | reloc->write_domain); | |
8b78f0e5 | 1369 | return -EINVAL; |
54cf91dc | 1370 | } |
54cf91dc | 1371 | |
2889caa9 | 1372 | if (reloc->write_domain) { |
c7c6e46f | 1373 | *target->exec_flags |= EXEC_OBJECT_WRITE; |
507d977f | 1374 | |
2889caa9 CW |
1375 | /* |
1376 | * Sandybridge PPGTT errata: We need a global gtt mapping | |
1377 | * for MI and pipe_control writes because the gpu doesn't | |
1378 | * properly redirect them through the ppgtt for non_secure | |
1379 | * batchbuffers. | |
1380 | */ | |
1381 | if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION && | |
cf819eff | 1382 | IS_GEN(eb->i915, 6)) { |
2889caa9 CW |
1383 | err = i915_vma_bind(target, target->obj->cache_level, |
1384 | PIN_GLOBAL); | |
1385 | if (WARN_ONCE(err, | |
1386 | "Unexpected failure to bind target VMA!")) | |
1387 | return err; | |
1388 | } | |
507d977f | 1389 | } |
54cf91dc | 1390 | |
2889caa9 CW |
1391 | /* |
1392 | * If the relocation already has the right value in it, no | |
54cf91dc CW |
1393 | * more work needs to be done. |
1394 | */ | |
7dd4f672 CW |
1395 | if (!DBG_FORCE_RELOC && |
1396 | gen8_canonical_addr(target->node.start) == reloc->presumed_offset) | |
67731b87 | 1397 | return 0; |
54cf91dc CW |
1398 | |
1399 | /* Check that the relocation address is valid... */ | |
3c94ceee | 1400 | if (unlikely(reloc->offset > |
507d977f | 1401 | vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) { |
ff240199 | 1402 | DRM_DEBUG("Relocation beyond object bounds: " |
507d977f CW |
1403 | "target %d offset %d size %d.\n", |
1404 | reloc->target_handle, | |
1405 | (int)reloc->offset, | |
1406 | (int)vma->size); | |
8b78f0e5 | 1407 | return -EINVAL; |
54cf91dc | 1408 | } |
b8f7ab17 | 1409 | if (unlikely(reloc->offset & 3)) { |
ff240199 | 1410 | DRM_DEBUG("Relocation not 4-byte aligned: " |
507d977f CW |
1411 | "target %d offset %d.\n", |
1412 | reloc->target_handle, | |
1413 | (int)reloc->offset); | |
8b78f0e5 | 1414 | return -EINVAL; |
54cf91dc CW |
1415 | } |
1416 | ||
071750e5 CW |
1417 | /* |
1418 | * If we write into the object, we need to force the synchronisation | |
1419 | * barrier, either with an asynchronous clflush or if we executed the | |
1420 | * patching using the GPU (though that should be serialised by the | |
1421 | * timeline). To be completely sure, and since we are required to | |
1422 | * do relocations we are already stalling, disable the user's opt | |
0519bcb1 | 1423 | * out of our synchronisation. |
071750e5 | 1424 | */ |
c7c6e46f | 1425 | *vma->exec_flags &= ~EXEC_OBJECT_ASYNC; |
071750e5 | 1426 | |
54cf91dc | 1427 | /* and update the user's relocation entry */ |
2889caa9 | 1428 | return relocate_entry(vma, reloc, eb, target); |
54cf91dc CW |
1429 | } |
1430 | ||
2889caa9 | 1431 | static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma) |
54cf91dc | 1432 | { |
1d83f442 | 1433 | #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry)) |
2889caa9 CW |
1434 | struct drm_i915_gem_relocation_entry stack[N_RELOC(512)]; |
1435 | struct drm_i915_gem_relocation_entry __user *urelocs; | |
c7c6e46f | 1436 | const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma); |
2889caa9 | 1437 | unsigned int remain; |
54cf91dc | 1438 | |
2889caa9 | 1439 | urelocs = u64_to_user_ptr(entry->relocs_ptr); |
1d83f442 | 1440 | remain = entry->relocation_count; |
2889caa9 CW |
1441 | if (unlikely(remain > N_RELOC(ULONG_MAX))) |
1442 | return -EINVAL; | |
ebc0808f | 1443 | |
2889caa9 CW |
1444 | /* |
1445 | * We must check that the entire relocation array is safe | |
1446 | * to read. However, if the array is not writable the user loses | |
1447 | * the updated relocation values. | |
1448 | */ | |
96d4f267 | 1449 | if (unlikely(!access_ok(urelocs, remain*sizeof(*urelocs)))) |
2889caa9 CW |
1450 | return -EFAULT; |
1451 | ||
1452 | do { | |
1453 | struct drm_i915_gem_relocation_entry *r = stack; | |
1454 | unsigned int count = | |
1455 | min_t(unsigned int, remain, ARRAY_SIZE(stack)); | |
1456 | unsigned int copied; | |
1d83f442 | 1457 | |
2889caa9 CW |
1458 | /* |
1459 | * This is the fast path and we cannot handle a pagefault | |
ebc0808f CW |
1460 | * whilst holding the struct mutex lest the user pass in the |
1461 | * relocations contained within a mmaped bo. For in such a case | |
1462 | * we, the page fault handler would call i915_gem_fault() and | |
1463 | * we would try to acquire the struct mutex again. Obviously | |
1464 | * this is bad and so lockdep complains vehemently. | |
1465 | */ | |
1466 | pagefault_disable(); | |
2889caa9 | 1467 | copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0])); |
ebc0808f | 1468 | pagefault_enable(); |
2889caa9 CW |
1469 | if (unlikely(copied)) { |
1470 | remain = -EFAULT; | |
31a39207 CW |
1471 | goto out; |
1472 | } | |
54cf91dc | 1473 | |
2889caa9 | 1474 | remain -= count; |
1d83f442 | 1475 | do { |
2889caa9 | 1476 | u64 offset = eb_relocate_entry(eb, vma, r); |
54cf91dc | 1477 | |
2889caa9 CW |
1478 | if (likely(offset == 0)) { |
1479 | } else if ((s64)offset < 0) { | |
1480 | remain = (int)offset; | |
31a39207 | 1481 | goto out; |
2889caa9 CW |
1482 | } else { |
1483 | /* | |
1484 | * Note that reporting an error now | |
1485 | * leaves everything in an inconsistent | |
1486 | * state as we have *already* changed | |
1487 | * the relocation value inside the | |
1488 | * object. As we have not changed the | |
1489 | * reloc.presumed_offset or will not | |
1490 | * change the execobject.offset, on the | |
1491 | * call we may not rewrite the value | |
1492 | * inside the object, leaving it | |
1493 | * dangling and causing a GPU hang. Unless | |
1494 | * userspace dynamically rebuilds the | |
1495 | * relocations on each execbuf rather than | |
1496 | * presume a static tree. | |
1497 | * | |
1498 | * We did previously check if the relocations | |
1499 | * were writable (access_ok), an error now | |
1500 | * would be a strange race with mprotect, | |
1501 | * having already demonstrated that we | |
1502 | * can read from this userspace address. | |
1503 | */ | |
1504 | offset = gen8_canonical_addr(offset & ~UPDATE); | |
fddcd00a CW |
1505 | if (unlikely(__put_user(offset, &urelocs[r-stack].presumed_offset))) { |
1506 | remain = -EFAULT; | |
1507 | goto out; | |
1508 | } | |
1d83f442 | 1509 | } |
2889caa9 CW |
1510 | } while (r++, --count); |
1511 | urelocs += ARRAY_SIZE(stack); | |
1512 | } while (remain); | |
31a39207 | 1513 | out: |
650bc635 | 1514 | reloc_cache_reset(&eb->reloc_cache); |
2889caa9 | 1515 | return remain; |
54cf91dc CW |
1516 | } |
1517 | ||
1518 | static int | |
2889caa9 | 1519 | eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma) |
54cf91dc | 1520 | { |
c7c6e46f | 1521 | const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma); |
2889caa9 CW |
1522 | struct drm_i915_gem_relocation_entry *relocs = |
1523 | u64_to_ptr(typeof(*relocs), entry->relocs_ptr); | |
1524 | unsigned int i; | |
1525 | int err; | |
54cf91dc CW |
1526 | |
1527 | for (i = 0; i < entry->relocation_count; i++) { | |
2889caa9 | 1528 | u64 offset = eb_relocate_entry(eb, vma, &relocs[i]); |
d4aeee77 | 1529 | |
2889caa9 CW |
1530 | if ((s64)offset < 0) { |
1531 | err = (int)offset; | |
1532 | goto err; | |
1533 | } | |
54cf91dc | 1534 | } |
2889caa9 CW |
1535 | err = 0; |
1536 | err: | |
1537 | reloc_cache_reset(&eb->reloc_cache); | |
1538 | return err; | |
edf4427b CW |
1539 | } |
1540 | ||
2889caa9 | 1541 | static int check_relocations(const struct drm_i915_gem_exec_object2 *entry) |
1690e1eb | 1542 | { |
2889caa9 CW |
1543 | const char __user *addr, *end; |
1544 | unsigned long size; | |
1545 | char __maybe_unused c; | |
1690e1eb | 1546 | |
2889caa9 CW |
1547 | size = entry->relocation_count; |
1548 | if (size == 0) | |
1549 | return 0; | |
7788a765 | 1550 | |
2889caa9 CW |
1551 | if (size > N_RELOC(ULONG_MAX)) |
1552 | return -EINVAL; | |
9a5a53b3 | 1553 | |
2889caa9 CW |
1554 | addr = u64_to_user_ptr(entry->relocs_ptr); |
1555 | size *= sizeof(struct drm_i915_gem_relocation_entry); | |
96d4f267 | 1556 | if (!access_ok(addr, size)) |
2889caa9 | 1557 | return -EFAULT; |
1690e1eb | 1558 | |
2889caa9 CW |
1559 | end = addr + size; |
1560 | for (; addr < end; addr += PAGE_SIZE) { | |
1561 | int err = __get_user(c, addr); | |
1562 | if (err) | |
1563 | return err; | |
ed5982e6 | 1564 | } |
2889caa9 | 1565 | return __get_user(c, end - 1); |
7788a765 | 1566 | } |
1690e1eb | 1567 | |
2889caa9 | 1568 | static int eb_copy_relocations(const struct i915_execbuffer *eb) |
d23db88c | 1569 | { |
2889caa9 CW |
1570 | const unsigned int count = eb->buffer_count; |
1571 | unsigned int i; | |
1572 | int err; | |
e6a84468 | 1573 | |
2889caa9 CW |
1574 | for (i = 0; i < count; i++) { |
1575 | const unsigned int nreloc = eb->exec[i].relocation_count; | |
1576 | struct drm_i915_gem_relocation_entry __user *urelocs; | |
1577 | struct drm_i915_gem_relocation_entry *relocs; | |
1578 | unsigned long size; | |
1579 | unsigned long copied; | |
e6a84468 | 1580 | |
2889caa9 CW |
1581 | if (nreloc == 0) |
1582 | continue; | |
e6a84468 | 1583 | |
2889caa9 CW |
1584 | err = check_relocations(&eb->exec[i]); |
1585 | if (err) | |
1586 | goto err; | |
d23db88c | 1587 | |
2889caa9 CW |
1588 | urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr); |
1589 | size = nreloc * sizeof(*relocs); | |
d23db88c | 1590 | |
0ee931c4 | 1591 | relocs = kvmalloc_array(size, 1, GFP_KERNEL); |
2889caa9 | 1592 | if (!relocs) { |
2889caa9 CW |
1593 | err = -ENOMEM; |
1594 | goto err; | |
1595 | } | |
d23db88c | 1596 | |
2889caa9 CW |
1597 | /* copy_from_user is limited to < 4GiB */ |
1598 | copied = 0; | |
1599 | do { | |
1600 | unsigned int len = | |
1601 | min_t(u64, BIT_ULL(31), size - copied); | |
1602 | ||
1603 | if (__copy_from_user((char *)relocs + copied, | |
908a6105 | 1604 | (char __user *)urelocs + copied, |
2889caa9 | 1605 | len)) { |
fddcd00a | 1606 | end_user: |
0b2c8f8b | 1607 | user_access_end(); |
2889caa9 CW |
1608 | kvfree(relocs); |
1609 | err = -EFAULT; | |
1610 | goto err; | |
1611 | } | |
91b2db6f | 1612 | |
2889caa9 CW |
1613 | copied += len; |
1614 | } while (copied < size); | |
506a8e87 | 1615 | |
2889caa9 CW |
1616 | /* |
1617 | * As we do not update the known relocation offsets after | |
1618 | * relocating (due to the complexities in lock handling), | |
1619 | * we need to mark them as invalid now so that we force the | |
1620 | * relocation processing next time. Just in case the target | |
1621 | * object is evicted and then rebound into its old | |
1622 | * presumed_offset before the next execbuffer - if that | |
1623 | * happened we would make the mistake of assuming that the | |
1624 | * relocations were valid. | |
1625 | */ | |
594cc251 LT |
1626 | if (!user_access_begin(urelocs, size)) |
1627 | goto end_user; | |
1628 | ||
2889caa9 CW |
1629 | for (copied = 0; copied < nreloc; copied++) |
1630 | unsafe_put_user(-1, | |
1631 | &urelocs[copied].presumed_offset, | |
1632 | end_user); | |
2889caa9 | 1633 | user_access_end(); |
d23db88c | 1634 | |
2889caa9 CW |
1635 | eb->exec[i].relocs_ptr = (uintptr_t)relocs; |
1636 | } | |
edf4427b | 1637 | |
2889caa9 | 1638 | return 0; |
101b506a | 1639 | |
2889caa9 CW |
1640 | err: |
1641 | while (i--) { | |
1642 | struct drm_i915_gem_relocation_entry *relocs = | |
1643 | u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr); | |
1644 | if (eb->exec[i].relocation_count) | |
1645 | kvfree(relocs); | |
1646 | } | |
1647 | return err; | |
d23db88c CW |
1648 | } |
1649 | ||
2889caa9 | 1650 | static int eb_prefault_relocations(const struct i915_execbuffer *eb) |
54cf91dc | 1651 | { |
2889caa9 CW |
1652 | const unsigned int count = eb->buffer_count; |
1653 | unsigned int i; | |
54cf91dc | 1654 | |
4f044a88 | 1655 | if (unlikely(i915_modparams.prefault_disable)) |
2889caa9 | 1656 | return 0; |
54cf91dc | 1657 | |
2889caa9 CW |
1658 | for (i = 0; i < count; i++) { |
1659 | int err; | |
54cf91dc | 1660 | |
2889caa9 CW |
1661 | err = check_relocations(&eb->exec[i]); |
1662 | if (err) | |
1663 | return err; | |
1664 | } | |
a415d355 | 1665 | |
2889caa9 | 1666 | return 0; |
54cf91dc CW |
1667 | } |
1668 | ||
2889caa9 | 1669 | static noinline int eb_relocate_slow(struct i915_execbuffer *eb) |
54cf91dc | 1670 | { |
650bc635 | 1671 | struct drm_device *dev = &eb->i915->drm; |
2889caa9 | 1672 | bool have_copy = false; |
27173f1f | 1673 | struct i915_vma *vma; |
2889caa9 CW |
1674 | int err = 0; |
1675 | ||
1676 | repeat: | |
1677 | if (signal_pending(current)) { | |
1678 | err = -ERESTARTSYS; | |
1679 | goto out; | |
1680 | } | |
27173f1f | 1681 | |
67731b87 | 1682 | /* We may process another execbuffer during the unlock... */ |
2889caa9 | 1683 | eb_reset_vmas(eb); |
54cf91dc CW |
1684 | mutex_unlock(&dev->struct_mutex); |
1685 | ||
2889caa9 CW |
1686 | /* |
1687 | * We take 3 passes through the slowpatch. | |
1688 | * | |
1689 | * 1 - we try to just prefault all the user relocation entries and | |
1690 | * then attempt to reuse the atomic pagefault disabled fast path again. | |
1691 | * | |
1692 | * 2 - we copy the user entries to a local buffer here outside of the | |
1693 | * local and allow ourselves to wait upon any rendering before | |
1694 | * relocations | |
1695 | * | |
1696 | * 3 - we already have a local copy of the relocation entries, but | |
1697 | * were interrupted (EAGAIN) whilst waiting for the objects, try again. | |
1698 | */ | |
1699 | if (!err) { | |
1700 | err = eb_prefault_relocations(eb); | |
1701 | } else if (!have_copy) { | |
1702 | err = eb_copy_relocations(eb); | |
1703 | have_copy = err == 0; | |
1704 | } else { | |
1705 | cond_resched(); | |
1706 | err = 0; | |
54cf91dc | 1707 | } |
2889caa9 CW |
1708 | if (err) { |
1709 | mutex_lock(&dev->struct_mutex); | |
1710 | goto out; | |
54cf91dc CW |
1711 | } |
1712 | ||
8a2421bd CW |
1713 | /* A frequent cause for EAGAIN are currently unavailable client pages */ |
1714 | flush_workqueue(eb->i915->mm.userptr_wq); | |
1715 | ||
2889caa9 CW |
1716 | err = i915_mutex_lock_interruptible(dev); |
1717 | if (err) { | |
54cf91dc | 1718 | mutex_lock(&dev->struct_mutex); |
2889caa9 | 1719 | goto out; |
54cf91dc CW |
1720 | } |
1721 | ||
67731b87 | 1722 | /* reacquire the objects */ |
2889caa9 CW |
1723 | err = eb_lookup_vmas(eb); |
1724 | if (err) | |
3b96eff4 | 1725 | goto err; |
67731b87 | 1726 | |
c7c6e46f CW |
1727 | GEM_BUG_ON(!eb->batch); |
1728 | ||
2889caa9 CW |
1729 | list_for_each_entry(vma, &eb->relocs, reloc_link) { |
1730 | if (!have_copy) { | |
1731 | pagefault_disable(); | |
1732 | err = eb_relocate_vma(eb, vma); | |
1733 | pagefault_enable(); | |
1734 | if (err) | |
1735 | goto repeat; | |
1736 | } else { | |
1737 | err = eb_relocate_vma_slow(eb, vma); | |
1738 | if (err) | |
1739 | goto err; | |
1740 | } | |
54cf91dc CW |
1741 | } |
1742 | ||
2889caa9 CW |
1743 | /* |
1744 | * Leave the user relocations as are, this is the painfully slow path, | |
54cf91dc CW |
1745 | * and we want to avoid the complication of dropping the lock whilst |
1746 | * having buffers reserved in the aperture and so causing spurious | |
1747 | * ENOSPC for random operations. | |
1748 | */ | |
1749 | ||
1750 | err: | |
2889caa9 CW |
1751 | if (err == -EAGAIN) |
1752 | goto repeat; | |
1753 | ||
1754 | out: | |
1755 | if (have_copy) { | |
1756 | const unsigned int count = eb->buffer_count; | |
1757 | unsigned int i; | |
1758 | ||
1759 | for (i = 0; i < count; i++) { | |
1760 | const struct drm_i915_gem_exec_object2 *entry = | |
1761 | &eb->exec[i]; | |
1762 | struct drm_i915_gem_relocation_entry *relocs; | |
1763 | ||
1764 | if (!entry->relocation_count) | |
1765 | continue; | |
1766 | ||
1767 | relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr); | |
1768 | kvfree(relocs); | |
1769 | } | |
1770 | } | |
1771 | ||
1f727d9e | 1772 | return err; |
54cf91dc CW |
1773 | } |
1774 | ||
2889caa9 | 1775 | static int eb_relocate(struct i915_execbuffer *eb) |
54cf91dc | 1776 | { |
2889caa9 CW |
1777 | if (eb_lookup_vmas(eb)) |
1778 | goto slow; | |
1779 | ||
1780 | /* The objects are in their final locations, apply the relocations. */ | |
1781 | if (eb->args->flags & __EXEC_HAS_RELOC) { | |
1782 | struct i915_vma *vma; | |
1783 | ||
1784 | list_for_each_entry(vma, &eb->relocs, reloc_link) { | |
1785 | if (eb_relocate_vma(eb, vma)) | |
1786 | goto slow; | |
1787 | } | |
1788 | } | |
1789 | ||
1790 | return 0; | |
1791 | ||
1792 | slow: | |
1793 | return eb_relocate_slow(eb); | |
1794 | } | |
1795 | ||
2889caa9 CW |
1796 | static int eb_move_to_gpu(struct i915_execbuffer *eb) |
1797 | { | |
1798 | const unsigned int count = eb->buffer_count; | |
1799 | unsigned int i; | |
1800 | int err; | |
54cf91dc | 1801 | |
2889caa9 | 1802 | for (i = 0; i < count; i++) { |
c7c6e46f CW |
1803 | unsigned int flags = eb->flags[i]; |
1804 | struct i915_vma *vma = eb->vma[i]; | |
27173f1f | 1805 | struct drm_i915_gem_object *obj = vma->obj; |
03ade511 | 1806 | |
c7c6e46f | 1807 | if (flags & EXEC_OBJECT_CAPTURE) { |
e61e0f51 | 1808 | struct i915_capture_list *capture; |
b0fd47ad CW |
1809 | |
1810 | capture = kmalloc(sizeof(*capture), GFP_KERNEL); | |
1811 | if (unlikely(!capture)) | |
1812 | return -ENOMEM; | |
1813 | ||
650bc635 | 1814 | capture->next = eb->request->capture_list; |
c7c6e46f | 1815 | capture->vma = eb->vma[i]; |
650bc635 | 1816 | eb->request->capture_list = capture; |
b0fd47ad CW |
1817 | } |
1818 | ||
b8f55be6 CW |
1819 | /* |
1820 | * If the GPU is not _reading_ through the CPU cache, we need | |
1821 | * to make sure that any writes (both previous GPU writes from | |
1822 | * before a change in snooping levels and normal CPU writes) | |
1823 | * caught in that cache are flushed to main memory. | |
1824 | * | |
1825 | * We want to say | |
1826 | * obj->cache_dirty && | |
1827 | * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ) | |
1828 | * but gcc's optimiser doesn't handle that as well and emits | |
1829 | * two jumps instead of one. Maybe one day... | |
1830 | */ | |
1831 | if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) { | |
0f46daa1 | 1832 | if (i915_gem_clflush_object(obj, 0)) |
c7c6e46f | 1833 | flags &= ~EXEC_OBJECT_ASYNC; |
0f46daa1 CW |
1834 | } |
1835 | ||
c7c6e46f CW |
1836 | if (flags & EXEC_OBJECT_ASYNC) |
1837 | continue; | |
77ae9957 | 1838 | |
e61e0f51 | 1839 | err = i915_request_await_object |
c7c6e46f | 1840 | (eb->request, obj, flags & EXEC_OBJECT_WRITE); |
2889caa9 CW |
1841 | if (err) |
1842 | return err; | |
2889caa9 CW |
1843 | } |
1844 | ||
1845 | for (i = 0; i < count; i++) { | |
c7c6e46f CW |
1846 | unsigned int flags = eb->flags[i]; |
1847 | struct i915_vma *vma = eb->vma[i]; | |
1848 | ||
a5236978 CW |
1849 | err = i915_vma_move_to_active(vma, eb->request, flags); |
1850 | if (unlikely(err)) { | |
1851 | i915_request_skip(eb->request, err); | |
1852 | return err; | |
1853 | } | |
2889caa9 | 1854 | |
c7c6e46f CW |
1855 | __eb_unreserve_vma(vma, flags); |
1856 | vma->exec_flags = NULL; | |
1857 | ||
1858 | if (unlikely(flags & __EXEC_OBJECT_HAS_REF)) | |
dade2a61 | 1859 | i915_vma_put(vma); |
c59a333f | 1860 | } |
2889caa9 | 1861 | eb->exec = NULL; |
c59a333f | 1862 | |
dcd79934 | 1863 | /* Unconditionally flush any chipset caches (for streaming writes). */ |
650bc635 | 1864 | i915_gem_chipset_flush(eb->i915); |
6ac42f41 | 1865 | |
2113184c | 1866 | return 0; |
54cf91dc CW |
1867 | } |
1868 | ||
2889caa9 | 1869 | static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec) |
54cf91dc | 1870 | { |
650bc635 | 1871 | if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS) |
ed5982e6 DV |
1872 | return false; |
1873 | ||
2f5945bc | 1874 | /* Kernel clipping was a DRI1 misfeature */ |
cf6e7bac JE |
1875 | if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) { |
1876 | if (exec->num_cliprects || exec->cliprects_ptr) | |
1877 | return false; | |
1878 | } | |
2f5945bc CW |
1879 | |
1880 | if (exec->DR4 == 0xffffffff) { | |
1881 | DRM_DEBUG("UXA submitting garbage DR4, fixing up\n"); | |
1882 | exec->DR4 = 0; | |
1883 | } | |
1884 | if (exec->DR1 || exec->DR4) | |
1885 | return false; | |
1886 | ||
1887 | if ((exec->batch_start_offset | exec->batch_len) & 0x7) | |
1888 | return false; | |
1889 | ||
1890 | return true; | |
54cf91dc CW |
1891 | } |
1892 | ||
e61e0f51 | 1893 | static int i915_reset_gen7_sol_offsets(struct i915_request *rq) |
ae662d31 | 1894 | { |
73dec95e TU |
1895 | u32 *cs; |
1896 | int i; | |
ae662d31 | 1897 | |
cf819eff | 1898 | if (!IS_GEN(rq->i915, 7) || rq->engine->id != RCS) { |
9d662da8 DV |
1899 | DRM_DEBUG("sol reset is gen7/rcs only\n"); |
1900 | return -EINVAL; | |
1901 | } | |
ae662d31 | 1902 | |
e61e0f51 | 1903 | cs = intel_ring_begin(rq, 4 * 2 + 2); |
73dec95e TU |
1904 | if (IS_ERR(cs)) |
1905 | return PTR_ERR(cs); | |
ae662d31 | 1906 | |
2889caa9 | 1907 | *cs++ = MI_LOAD_REGISTER_IMM(4); |
ae662d31 | 1908 | for (i = 0; i < 4; i++) { |
73dec95e TU |
1909 | *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i)); |
1910 | *cs++ = 0; | |
ae662d31 | 1911 | } |
2889caa9 | 1912 | *cs++ = MI_NOOP; |
e61e0f51 | 1913 | intel_ring_advance(rq, cs); |
ae662d31 EA |
1914 | |
1915 | return 0; | |
1916 | } | |
1917 | ||
650bc635 | 1918 | static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master) |
71745376 | 1919 | { |
71745376 | 1920 | struct drm_i915_gem_object *shadow_batch_obj; |
17cabf57 | 1921 | struct i915_vma *vma; |
2889caa9 | 1922 | int err; |
71745376 | 1923 | |
650bc635 CW |
1924 | shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, |
1925 | PAGE_ALIGN(eb->batch_len)); | |
71745376 | 1926 | if (IS_ERR(shadow_batch_obj)) |
59bfa124 | 1927 | return ERR_CAST(shadow_batch_obj); |
71745376 | 1928 | |
2889caa9 | 1929 | err = intel_engine_cmd_parser(eb->engine, |
650bc635 | 1930 | eb->batch->obj, |
33a051a5 | 1931 | shadow_batch_obj, |
650bc635 CW |
1932 | eb->batch_start_offset, |
1933 | eb->batch_len, | |
33a051a5 | 1934 | is_master); |
2889caa9 CW |
1935 | if (err) { |
1936 | if (err == -EACCES) /* unhandled chained batch */ | |
058d88c4 CW |
1937 | vma = NULL; |
1938 | else | |
2889caa9 | 1939 | vma = ERR_PTR(err); |
058d88c4 CW |
1940 | goto out; |
1941 | } | |
71745376 | 1942 | |
058d88c4 CW |
1943 | vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0); |
1944 | if (IS_ERR(vma)) | |
1945 | goto out; | |
de4e783a | 1946 | |
c7c6e46f CW |
1947 | eb->vma[eb->buffer_count] = i915_vma_get(vma); |
1948 | eb->flags[eb->buffer_count] = | |
1949 | __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF; | |
1950 | vma->exec_flags = &eb->flags[eb->buffer_count]; | |
1951 | eb->buffer_count++; | |
71745376 | 1952 | |
058d88c4 | 1953 | out: |
de4e783a | 1954 | i915_gem_object_unpin_pages(shadow_batch_obj); |
058d88c4 | 1955 | return vma; |
71745376 | 1956 | } |
5c6c6003 | 1957 | |
c8659efa | 1958 | static void |
e61e0f51 | 1959 | add_to_client(struct i915_request *rq, struct drm_file *file) |
c8659efa | 1960 | { |
e61e0f51 CW |
1961 | rq->file_priv = file->driver_priv; |
1962 | list_add_tail(&rq->client_link, &rq->file_priv->mm.request_list); | |
c8659efa CW |
1963 | } |
1964 | ||
2889caa9 | 1965 | static int eb_submit(struct i915_execbuffer *eb) |
78382593 | 1966 | { |
2889caa9 | 1967 | int err; |
78382593 | 1968 | |
2889caa9 CW |
1969 | err = eb_move_to_gpu(eb); |
1970 | if (err) | |
1971 | return err; | |
78382593 | 1972 | |
650bc635 | 1973 | if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) { |
2889caa9 CW |
1974 | err = i915_reset_gen7_sol_offsets(eb->request); |
1975 | if (err) | |
1976 | return err; | |
78382593 OM |
1977 | } |
1978 | ||
2889caa9 | 1979 | err = eb->engine->emit_bb_start(eb->request, |
650bc635 CW |
1980 | eb->batch->node.start + |
1981 | eb->batch_start_offset, | |
1982 | eb->batch_len, | |
2889caa9 CW |
1983 | eb->batch_flags); |
1984 | if (err) | |
1985 | return err; | |
78382593 | 1986 | |
2f5945bc | 1987 | return 0; |
78382593 OM |
1988 | } |
1989 | ||
204bcfef | 1990 | /* |
a8ebba75 | 1991 | * Find one BSD ring to dispatch the corresponding BSD command. |
c80ff16e | 1992 | * The engine index is returned. |
a8ebba75 | 1993 | */ |
de1add36 | 1994 | static unsigned int |
c80ff16e CW |
1995 | gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv, |
1996 | struct drm_file *file) | |
a8ebba75 | 1997 | { |
a8ebba75 ZY |
1998 | struct drm_i915_file_private *file_priv = file->driver_priv; |
1999 | ||
de1add36 | 2000 | /* Check whether the file_priv has already selected one ring. */ |
6f633402 JL |
2001 | if ((int)file_priv->bsd_engine < 0) |
2002 | file_priv->bsd_engine = atomic_fetch_xor(1, | |
2003 | &dev_priv->mm.bsd_engine_dispatch_index); | |
d23db88c | 2004 | |
c80ff16e | 2005 | return file_priv->bsd_engine; |
d23db88c CW |
2006 | } |
2007 | ||
de1add36 TU |
2008 | #define I915_USER_RINGS (4) |
2009 | ||
117897f4 | 2010 | static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = { |
de1add36 TU |
2011 | [I915_EXEC_DEFAULT] = RCS, |
2012 | [I915_EXEC_RENDER] = RCS, | |
2013 | [I915_EXEC_BLT] = BCS, | |
2014 | [I915_EXEC_BSD] = VCS, | |
2015 | [I915_EXEC_VEBOX] = VECS | |
2016 | }; | |
2017 | ||
f8ca0c07 DG |
2018 | static struct intel_engine_cs * |
2019 | eb_select_engine(struct drm_i915_private *dev_priv, | |
2020 | struct drm_file *file, | |
2021 | struct drm_i915_gem_execbuffer2 *args) | |
de1add36 TU |
2022 | { |
2023 | unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK; | |
f8ca0c07 | 2024 | struct intel_engine_cs *engine; |
de1add36 TU |
2025 | |
2026 | if (user_ring_id > I915_USER_RINGS) { | |
2027 | DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id); | |
f8ca0c07 | 2028 | return NULL; |
de1add36 TU |
2029 | } |
2030 | ||
2031 | if ((user_ring_id != I915_EXEC_BSD) && | |
2032 | ((args->flags & I915_EXEC_BSD_MASK) != 0)) { | |
2033 | DRM_DEBUG("execbuf with non bsd ring but with invalid " | |
2034 | "bsd dispatch flags: %d\n", (int)(args->flags)); | |
f8ca0c07 | 2035 | return NULL; |
de1add36 TU |
2036 | } |
2037 | ||
2038 | if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) { | |
2039 | unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK; | |
2040 | ||
2041 | if (bsd_idx == I915_EXEC_BSD_DEFAULT) { | |
c80ff16e | 2042 | bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file); |
de1add36 TU |
2043 | } else if (bsd_idx >= I915_EXEC_BSD_RING1 && |
2044 | bsd_idx <= I915_EXEC_BSD_RING2) { | |
d9da6aa0 | 2045 | bsd_idx >>= I915_EXEC_BSD_SHIFT; |
de1add36 TU |
2046 | bsd_idx--; |
2047 | } else { | |
2048 | DRM_DEBUG("execbuf with unknown bsd ring: %u\n", | |
2049 | bsd_idx); | |
f8ca0c07 | 2050 | return NULL; |
de1add36 TU |
2051 | } |
2052 | ||
3b3f1650 | 2053 | engine = dev_priv->engine[_VCS(bsd_idx)]; |
de1add36 | 2054 | } else { |
3b3f1650 | 2055 | engine = dev_priv->engine[user_ring_map[user_ring_id]]; |
de1add36 TU |
2056 | } |
2057 | ||
3b3f1650 | 2058 | if (!engine) { |
de1add36 | 2059 | DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id); |
f8ca0c07 | 2060 | return NULL; |
de1add36 TU |
2061 | } |
2062 | ||
f8ca0c07 | 2063 | return engine; |
de1add36 TU |
2064 | } |
2065 | ||
cf6e7bac JE |
2066 | static void |
2067 | __free_fence_array(struct drm_syncobj **fences, unsigned int n) | |
2068 | { | |
2069 | while (n--) | |
2070 | drm_syncobj_put(ptr_mask_bits(fences[n], 2)); | |
2071 | kvfree(fences); | |
2072 | } | |
2073 | ||
2074 | static struct drm_syncobj ** | |
2075 | get_fence_array(struct drm_i915_gem_execbuffer2 *args, | |
2076 | struct drm_file *file) | |
2077 | { | |
d710fc16 | 2078 | const unsigned long nfences = args->num_cliprects; |
cf6e7bac JE |
2079 | struct drm_i915_gem_exec_fence __user *user; |
2080 | struct drm_syncobj **fences; | |
d710fc16 | 2081 | unsigned long n; |
cf6e7bac JE |
2082 | int err; |
2083 | ||
2084 | if (!(args->flags & I915_EXEC_FENCE_ARRAY)) | |
2085 | return NULL; | |
2086 | ||
d710fc16 CW |
2087 | /* Check multiplication overflow for access_ok() and kvmalloc_array() */ |
2088 | BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long)); | |
2089 | if (nfences > min_t(unsigned long, | |
2090 | ULONG_MAX / sizeof(*user), | |
2091 | SIZE_MAX / sizeof(*fences))) | |
cf6e7bac JE |
2092 | return ERR_PTR(-EINVAL); |
2093 | ||
2094 | user = u64_to_user_ptr(args->cliprects_ptr); | |
96d4f267 | 2095 | if (!access_ok(user, nfences * sizeof(*user))) |
cf6e7bac JE |
2096 | return ERR_PTR(-EFAULT); |
2097 | ||
d710fc16 | 2098 | fences = kvmalloc_array(nfences, sizeof(*fences), |
0ee931c4 | 2099 | __GFP_NOWARN | GFP_KERNEL); |
cf6e7bac JE |
2100 | if (!fences) |
2101 | return ERR_PTR(-ENOMEM); | |
2102 | ||
2103 | for (n = 0; n < nfences; n++) { | |
2104 | struct drm_i915_gem_exec_fence fence; | |
2105 | struct drm_syncobj *syncobj; | |
2106 | ||
2107 | if (__copy_from_user(&fence, user++, sizeof(fence))) { | |
2108 | err = -EFAULT; | |
2109 | goto err; | |
2110 | } | |
2111 | ||
ebcaa1ff TU |
2112 | if (fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS) { |
2113 | err = -EINVAL; | |
2114 | goto err; | |
2115 | } | |
2116 | ||
cf6e7bac JE |
2117 | syncobj = drm_syncobj_find(file, fence.handle); |
2118 | if (!syncobj) { | |
2119 | DRM_DEBUG("Invalid syncobj handle provided\n"); | |
2120 | err = -ENOENT; | |
2121 | goto err; | |
2122 | } | |
2123 | ||
ebcaa1ff TU |
2124 | BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) & |
2125 | ~__I915_EXEC_FENCE_UNKNOWN_FLAGS); | |
2126 | ||
cf6e7bac JE |
2127 | fences[n] = ptr_pack_bits(syncobj, fence.flags, 2); |
2128 | } | |
2129 | ||
2130 | return fences; | |
2131 | ||
2132 | err: | |
2133 | __free_fence_array(fences, n); | |
2134 | return ERR_PTR(err); | |
2135 | } | |
2136 | ||
2137 | static void | |
2138 | put_fence_array(struct drm_i915_gem_execbuffer2 *args, | |
2139 | struct drm_syncobj **fences) | |
2140 | { | |
2141 | if (fences) | |
2142 | __free_fence_array(fences, args->num_cliprects); | |
2143 | } | |
2144 | ||
2145 | static int | |
2146 | await_fence_array(struct i915_execbuffer *eb, | |
2147 | struct drm_syncobj **fences) | |
2148 | { | |
2149 | const unsigned int nfences = eb->args->num_cliprects; | |
2150 | unsigned int n; | |
2151 | int err; | |
2152 | ||
2153 | for (n = 0; n < nfences; n++) { | |
2154 | struct drm_syncobj *syncobj; | |
2155 | struct dma_fence *fence; | |
2156 | unsigned int flags; | |
2157 | ||
2158 | syncobj = ptr_unpack_bits(fences[n], &flags, 2); | |
2159 | if (!(flags & I915_EXEC_FENCE_WAIT)) | |
2160 | continue; | |
2161 | ||
afca4216 | 2162 | fence = drm_syncobj_fence_get(syncobj); |
cf6e7bac JE |
2163 | if (!fence) |
2164 | return -EINVAL; | |
2165 | ||
e61e0f51 | 2166 | err = i915_request_await_dma_fence(eb->request, fence); |
cf6e7bac JE |
2167 | dma_fence_put(fence); |
2168 | if (err < 0) | |
2169 | return err; | |
2170 | } | |
2171 | ||
2172 | return 0; | |
2173 | } | |
2174 | ||
2175 | static void | |
2176 | signal_fence_array(struct i915_execbuffer *eb, | |
2177 | struct drm_syncobj **fences) | |
2178 | { | |
2179 | const unsigned int nfences = eb->args->num_cliprects; | |
2180 | struct dma_fence * const fence = &eb->request->fence; | |
2181 | unsigned int n; | |
2182 | ||
2183 | for (n = 0; n < nfences; n++) { | |
2184 | struct drm_syncobj *syncobj; | |
2185 | unsigned int flags; | |
2186 | ||
2187 | syncobj = ptr_unpack_bits(fences[n], &flags, 2); | |
2188 | if (!(flags & I915_EXEC_FENCE_SIGNAL)) | |
2189 | continue; | |
2190 | ||
0b258ed1 | 2191 | drm_syncobj_replace_fence(syncobj, fence); |
cf6e7bac JE |
2192 | } |
2193 | } | |
2194 | ||
54cf91dc | 2195 | static int |
650bc635 | 2196 | i915_gem_do_execbuffer(struct drm_device *dev, |
54cf91dc CW |
2197 | struct drm_file *file, |
2198 | struct drm_i915_gem_execbuffer2 *args, | |
cf6e7bac JE |
2199 | struct drm_i915_gem_exec_object2 *exec, |
2200 | struct drm_syncobj **fences) | |
54cf91dc | 2201 | { |
650bc635 | 2202 | struct i915_execbuffer eb; |
fec0445c CW |
2203 | struct dma_fence *in_fence = NULL; |
2204 | struct sync_file *out_fence = NULL; | |
538ef96b | 2205 | intel_wakeref_t wakeref; |
fec0445c | 2206 | int out_fence_fd = -1; |
2889caa9 | 2207 | int err; |
432e58ed | 2208 | |
74c1c694 | 2209 | BUILD_BUG_ON(__EXEC_INTERNAL_FLAGS & ~__I915_EXEC_ILLEGAL_FLAGS); |
2889caa9 CW |
2210 | BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & |
2211 | ~__EXEC_OBJECT_UNKNOWN_FLAGS); | |
54cf91dc | 2212 | |
650bc635 CW |
2213 | eb.i915 = to_i915(dev); |
2214 | eb.file = file; | |
2215 | eb.args = args; | |
7dd4f672 | 2216 | if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC)) |
2889caa9 | 2217 | args->flags |= __EXEC_HAS_RELOC; |
c7c6e46f | 2218 | |
650bc635 | 2219 | eb.exec = exec; |
170fa29b CW |
2220 | eb.vma = (struct i915_vma **)(exec + args->buffer_count + 1); |
2221 | eb.vma[0] = NULL; | |
c7c6e46f CW |
2222 | eb.flags = (unsigned int *)(eb.vma + args->buffer_count + 1); |
2223 | ||
2889caa9 | 2224 | eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS; |
650bc635 CW |
2225 | reloc_cache_init(&eb.reloc_cache, eb.i915); |
2226 | ||
2889caa9 | 2227 | eb.buffer_count = args->buffer_count; |
650bc635 CW |
2228 | eb.batch_start_offset = args->batch_start_offset; |
2229 | eb.batch_len = args->batch_len; | |
2230 | ||
2889caa9 | 2231 | eb.batch_flags = 0; |
d7d4eedd | 2232 | if (args->flags & I915_EXEC_SECURE) { |
b3ac9f25 | 2233 | if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN)) |
d7d4eedd CW |
2234 | return -EPERM; |
2235 | ||
2889caa9 | 2236 | eb.batch_flags |= I915_DISPATCH_SECURE; |
d7d4eedd | 2237 | } |
b45305fc | 2238 | if (args->flags & I915_EXEC_IS_PINNED) |
2889caa9 | 2239 | eb.batch_flags |= I915_DISPATCH_PINNED; |
54cf91dc | 2240 | |
650bc635 CW |
2241 | eb.engine = eb_select_engine(eb.i915, file, args); |
2242 | if (!eb.engine) | |
54cf91dc | 2243 | return -EINVAL; |
54cf91dc | 2244 | |
fec0445c CW |
2245 | if (args->flags & I915_EXEC_FENCE_IN) { |
2246 | in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2)); | |
4a04e371 DCS |
2247 | if (!in_fence) |
2248 | return -EINVAL; | |
fec0445c CW |
2249 | } |
2250 | ||
2251 | if (args->flags & I915_EXEC_FENCE_OUT) { | |
2252 | out_fence_fd = get_unused_fd_flags(O_CLOEXEC); | |
2253 | if (out_fence_fd < 0) { | |
2889caa9 | 2254 | err = out_fence_fd; |
4a04e371 | 2255 | goto err_in_fence; |
fec0445c CW |
2256 | } |
2257 | } | |
2258 | ||
4d470f73 CW |
2259 | err = eb_create(&eb); |
2260 | if (err) | |
2261 | goto err_out_fence; | |
2262 | ||
2263 | GEM_BUG_ON(!eb.lut_size); | |
2889caa9 | 2264 | |
1acfc104 CW |
2265 | err = eb_select_context(&eb); |
2266 | if (unlikely(err)) | |
2267 | goto err_destroy; | |
2268 | ||
2889caa9 CW |
2269 | /* |
2270 | * Take a local wakeref for preparing to dispatch the execbuf as | |
67d97da3 CW |
2271 | * we expect to access the hardware fairly frequently in the |
2272 | * process. Upon first dispatch, we acquire another prolonged | |
2273 | * wakeref that we hold until the GPU has been idle for at least | |
2274 | * 100ms. | |
2275 | */ | |
538ef96b | 2276 | wakeref = intel_runtime_pm_get(eb.i915); |
1acfc104 | 2277 | |
2889caa9 CW |
2278 | err = i915_mutex_lock_interruptible(dev); |
2279 | if (err) | |
2280 | goto err_rpm; | |
f65c9168 | 2281 | |
2889caa9 | 2282 | err = eb_relocate(&eb); |
1f727d9e | 2283 | if (err) { |
2889caa9 CW |
2284 | /* |
2285 | * If the user expects the execobject.offset and | |
2286 | * reloc.presumed_offset to be an exact match, | |
2287 | * as for using NO_RELOC, then we cannot update | |
2288 | * the execobject.offset until we have completed | |
2289 | * relocation. | |
2290 | */ | |
2291 | args->flags &= ~__EXEC_HAS_RELOC; | |
2889caa9 | 2292 | goto err_vma; |
1f727d9e | 2293 | } |
54cf91dc | 2294 | |
c7c6e46f | 2295 | if (unlikely(*eb.batch->exec_flags & EXEC_OBJECT_WRITE)) { |
ff240199 | 2296 | DRM_DEBUG("Attempting to use self-modifying batch buffer\n"); |
2889caa9 CW |
2297 | err = -EINVAL; |
2298 | goto err_vma; | |
54cf91dc | 2299 | } |
650bc635 CW |
2300 | if (eb.batch_start_offset > eb.batch->size || |
2301 | eb.batch_len > eb.batch->size - eb.batch_start_offset) { | |
0b537272 | 2302 | DRM_DEBUG("Attempting to use out-of-bounds batch\n"); |
2889caa9 CW |
2303 | err = -EINVAL; |
2304 | goto err_vma; | |
0b537272 | 2305 | } |
54cf91dc | 2306 | |
3dbf26ed | 2307 | if (eb_use_cmdparser(&eb)) { |
59bfa124 CW |
2308 | struct i915_vma *vma; |
2309 | ||
650bc635 | 2310 | vma = eb_parse(&eb, drm_is_current_master(file)); |
59bfa124 | 2311 | if (IS_ERR(vma)) { |
2889caa9 CW |
2312 | err = PTR_ERR(vma); |
2313 | goto err_vma; | |
78a42377 | 2314 | } |
17cabf57 | 2315 | |
59bfa124 | 2316 | if (vma) { |
c7c7372e RP |
2317 | /* |
2318 | * Batch parsed and accepted: | |
2319 | * | |
2320 | * Set the DISPATCH_SECURE bit to remove the NON_SECURE | |
2321 | * bit from MI_BATCH_BUFFER_START commands issued in | |
2322 | * the dispatch_execbuffer implementations. We | |
2323 | * specifically don't want that set on batches the | |
2324 | * command parser has accepted. | |
2325 | */ | |
2889caa9 | 2326 | eb.batch_flags |= I915_DISPATCH_SECURE; |
650bc635 CW |
2327 | eb.batch_start_offset = 0; |
2328 | eb.batch = vma; | |
c7c7372e | 2329 | } |
351e3db2 BV |
2330 | } |
2331 | ||
650bc635 CW |
2332 | if (eb.batch_len == 0) |
2333 | eb.batch_len = eb.batch->size - eb.batch_start_offset; | |
78a42377 | 2334 | |
2889caa9 CW |
2335 | /* |
2336 | * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure | |
d7d4eedd | 2337 | * batch" bit. Hence we need to pin secure batches into the global gtt. |
28cf5415 | 2338 | * hsw should have this fixed, but bdw mucks it up again. */ |
2889caa9 | 2339 | if (eb.batch_flags & I915_DISPATCH_SECURE) { |
058d88c4 | 2340 | struct i915_vma *vma; |
59bfa124 | 2341 | |
da51a1e7 DV |
2342 | /* |
2343 | * So on first glance it looks freaky that we pin the batch here | |
2344 | * outside of the reservation loop. But: | |
2345 | * - The batch is already pinned into the relevant ppgtt, so we | |
2346 | * already have the backing storage fully allocated. | |
2347 | * - No other BO uses the global gtt (well contexts, but meh), | |
fd0753cf | 2348 | * so we don't really have issues with multiple objects not |
da51a1e7 DV |
2349 | * fitting due to fragmentation. |
2350 | * So this is actually safe. | |
2351 | */ | |
2889caa9 | 2352 | vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0); |
058d88c4 | 2353 | if (IS_ERR(vma)) { |
2889caa9 CW |
2354 | err = PTR_ERR(vma); |
2355 | goto err_vma; | |
058d88c4 | 2356 | } |
d7d4eedd | 2357 | |
650bc635 | 2358 | eb.batch = vma; |
59bfa124 | 2359 | } |
d7d4eedd | 2360 | |
7dd4f672 CW |
2361 | /* All GPU relocation batches must be submitted prior to the user rq */ |
2362 | GEM_BUG_ON(eb.reloc_cache.rq); | |
2363 | ||
0c8dac88 | 2364 | /* Allocate a request for this batch buffer nice and early. */ |
e61e0f51 | 2365 | eb.request = i915_request_alloc(eb.engine, eb.ctx); |
650bc635 | 2366 | if (IS_ERR(eb.request)) { |
2889caa9 | 2367 | err = PTR_ERR(eb.request); |
0c8dac88 | 2368 | goto err_batch_unpin; |
26827088 | 2369 | } |
0c8dac88 | 2370 | |
fec0445c | 2371 | if (in_fence) { |
e61e0f51 | 2372 | err = i915_request_await_dma_fence(eb.request, in_fence); |
2889caa9 | 2373 | if (err < 0) |
fec0445c CW |
2374 | goto err_request; |
2375 | } | |
2376 | ||
cf6e7bac JE |
2377 | if (fences) { |
2378 | err = await_fence_array(&eb, fences); | |
2379 | if (err) | |
2380 | goto err_request; | |
2381 | } | |
2382 | ||
fec0445c | 2383 | if (out_fence_fd != -1) { |
650bc635 | 2384 | out_fence = sync_file_create(&eb.request->fence); |
fec0445c | 2385 | if (!out_fence) { |
2889caa9 | 2386 | err = -ENOMEM; |
fec0445c CW |
2387 | goto err_request; |
2388 | } | |
2389 | } | |
2390 | ||
2889caa9 CW |
2391 | /* |
2392 | * Whilst this request exists, batch_obj will be on the | |
17f298cf CW |
2393 | * active_list, and so will hold the active reference. Only when this |
2394 | * request is retired will the the batch_obj be moved onto the | |
2395 | * inactive_list and lose its active reference. Hence we do not need | |
2396 | * to explicitly hold another reference here. | |
2397 | */ | |
650bc635 | 2398 | eb.request->batch = eb.batch; |
5f19e2bf | 2399 | |
e61e0f51 | 2400 | trace_i915_request_queue(eb.request, eb.batch_flags); |
2889caa9 | 2401 | err = eb_submit(&eb); |
aa9b7810 | 2402 | err_request: |
697b9a87 | 2403 | i915_request_add(eb.request); |
650bc635 | 2404 | add_to_client(eb.request, file); |
c8659efa | 2405 | |
cf6e7bac JE |
2406 | if (fences) |
2407 | signal_fence_array(&eb, fences); | |
2408 | ||
fec0445c | 2409 | if (out_fence) { |
2889caa9 | 2410 | if (err == 0) { |
fec0445c | 2411 | fd_install(out_fence_fd, out_fence->file); |
b6a88e4a | 2412 | args->rsvd2 &= GENMASK_ULL(31, 0); /* keep in-fence */ |
fec0445c CW |
2413 | args->rsvd2 |= (u64)out_fence_fd << 32; |
2414 | out_fence_fd = -1; | |
2415 | } else { | |
2416 | fput(out_fence->file); | |
2417 | } | |
2418 | } | |
54cf91dc | 2419 | |
0c8dac88 | 2420 | err_batch_unpin: |
2889caa9 | 2421 | if (eb.batch_flags & I915_DISPATCH_SECURE) |
650bc635 | 2422 | i915_vma_unpin(eb.batch); |
2889caa9 CW |
2423 | err_vma: |
2424 | if (eb.exec) | |
2425 | eb_release_vmas(&eb); | |
54cf91dc | 2426 | mutex_unlock(&dev->struct_mutex); |
2889caa9 | 2427 | err_rpm: |
538ef96b | 2428 | intel_runtime_pm_put(eb.i915, wakeref); |
1acfc104 CW |
2429 | i915_gem_context_put(eb.ctx); |
2430 | err_destroy: | |
2889caa9 | 2431 | eb_destroy(&eb); |
4d470f73 | 2432 | err_out_fence: |
fec0445c CW |
2433 | if (out_fence_fd != -1) |
2434 | put_unused_fd(out_fence_fd); | |
4a04e371 | 2435 | err_in_fence: |
fec0445c | 2436 | dma_fence_put(in_fence); |
2889caa9 | 2437 | return err; |
54cf91dc CW |
2438 | } |
2439 | ||
d710fc16 CW |
2440 | static size_t eb_element_size(void) |
2441 | { | |
2442 | return (sizeof(struct drm_i915_gem_exec_object2) + | |
2443 | sizeof(struct i915_vma *) + | |
2444 | sizeof(unsigned int)); | |
2445 | } | |
2446 | ||
2447 | static bool check_buffer_count(size_t count) | |
2448 | { | |
2449 | const size_t sz = eb_element_size(); | |
2450 | ||
2451 | /* | |
2452 | * When using LUT_HANDLE, we impose a limit of INT_MAX for the lookup | |
2453 | * array size (see eb_create()). Otherwise, we can accept an array as | |
2454 | * large as can be addressed (though use large arrays at your peril)! | |
2455 | */ | |
2456 | ||
2457 | return !(count < 1 || count > INT_MAX || count > SIZE_MAX / sz - 1); | |
2458 | } | |
2459 | ||
54cf91dc CW |
2460 | /* |
2461 | * Legacy execbuffer just creates an exec2 list from the original exec object | |
2462 | * list array and passes it to the real function. | |
2463 | */ | |
2464 | int | |
6a20fe7b VS |
2465 | i915_gem_execbuffer_ioctl(struct drm_device *dev, void *data, |
2466 | struct drm_file *file) | |
54cf91dc CW |
2467 | { |
2468 | struct drm_i915_gem_execbuffer *args = data; | |
2469 | struct drm_i915_gem_execbuffer2 exec2; | |
2470 | struct drm_i915_gem_exec_object *exec_list = NULL; | |
2471 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; | |
d710fc16 | 2472 | const size_t count = args->buffer_count; |
2889caa9 CW |
2473 | unsigned int i; |
2474 | int err; | |
54cf91dc | 2475 | |
d710fc16 CW |
2476 | if (!check_buffer_count(count)) { |
2477 | DRM_DEBUG("execbuf2 with %zd buffers\n", count); | |
54cf91dc CW |
2478 | return -EINVAL; |
2479 | } | |
2480 | ||
2889caa9 CW |
2481 | exec2.buffers_ptr = args->buffers_ptr; |
2482 | exec2.buffer_count = args->buffer_count; | |
2483 | exec2.batch_start_offset = args->batch_start_offset; | |
2484 | exec2.batch_len = args->batch_len; | |
2485 | exec2.DR1 = args->DR1; | |
2486 | exec2.DR4 = args->DR4; | |
2487 | exec2.num_cliprects = args->num_cliprects; | |
2488 | exec2.cliprects_ptr = args->cliprects_ptr; | |
2489 | exec2.flags = I915_EXEC_RENDER; | |
2490 | i915_execbuffer2_set_context_id(exec2, 0); | |
2491 | ||
2492 | if (!i915_gem_check_execbuffer(&exec2)) | |
2493 | return -EINVAL; | |
2494 | ||
54cf91dc | 2495 | /* Copy in the exec list from userland */ |
d710fc16 | 2496 | exec_list = kvmalloc_array(count, sizeof(*exec_list), |
0ee931c4 | 2497 | __GFP_NOWARN | GFP_KERNEL); |
d710fc16 | 2498 | exec2_list = kvmalloc_array(count + 1, eb_element_size(), |
0ee931c4 | 2499 | __GFP_NOWARN | GFP_KERNEL); |
54cf91dc | 2500 | if (exec_list == NULL || exec2_list == NULL) { |
ff240199 | 2501 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
54cf91dc | 2502 | args->buffer_count); |
2098105e MH |
2503 | kvfree(exec_list); |
2504 | kvfree(exec2_list); | |
54cf91dc CW |
2505 | return -ENOMEM; |
2506 | } | |
2889caa9 | 2507 | err = copy_from_user(exec_list, |
3ed605bc | 2508 | u64_to_user_ptr(args->buffers_ptr), |
d710fc16 | 2509 | sizeof(*exec_list) * count); |
2889caa9 | 2510 | if (err) { |
ff240199 | 2511 | DRM_DEBUG("copy %d exec entries failed %d\n", |
2889caa9 | 2512 | args->buffer_count, err); |
2098105e MH |
2513 | kvfree(exec_list); |
2514 | kvfree(exec2_list); | |
54cf91dc CW |
2515 | return -EFAULT; |
2516 | } | |
2517 | ||
2518 | for (i = 0; i < args->buffer_count; i++) { | |
2519 | exec2_list[i].handle = exec_list[i].handle; | |
2520 | exec2_list[i].relocation_count = exec_list[i].relocation_count; | |
2521 | exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; | |
2522 | exec2_list[i].alignment = exec_list[i].alignment; | |
2523 | exec2_list[i].offset = exec_list[i].offset; | |
f0836b72 | 2524 | if (INTEL_GEN(to_i915(dev)) < 4) |
54cf91dc CW |
2525 | exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; |
2526 | else | |
2527 | exec2_list[i].flags = 0; | |
2528 | } | |
2529 | ||
cf6e7bac | 2530 | err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list, NULL); |
2889caa9 | 2531 | if (exec2.flags & __EXEC_HAS_RELOC) { |
9aab8bff | 2532 | struct drm_i915_gem_exec_object __user *user_exec_list = |
3ed605bc | 2533 | u64_to_user_ptr(args->buffers_ptr); |
9aab8bff | 2534 | |
54cf91dc | 2535 | /* Copy the new buffer offsets back to the user's exec list. */ |
9aab8bff | 2536 | for (i = 0; i < args->buffer_count; i++) { |
2889caa9 CW |
2537 | if (!(exec2_list[i].offset & UPDATE)) |
2538 | continue; | |
2539 | ||
934acce3 | 2540 | exec2_list[i].offset = |
2889caa9 CW |
2541 | gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK); |
2542 | exec2_list[i].offset &= PIN_OFFSET_MASK; | |
2543 | if (__copy_to_user(&user_exec_list[i].offset, | |
2544 | &exec2_list[i].offset, | |
2545 | sizeof(user_exec_list[i].offset))) | |
9aab8bff | 2546 | break; |
54cf91dc CW |
2547 | } |
2548 | } | |
2549 | ||
2098105e MH |
2550 | kvfree(exec_list); |
2551 | kvfree(exec2_list); | |
2889caa9 | 2552 | return err; |
54cf91dc CW |
2553 | } |
2554 | ||
2555 | int | |
6a20fe7b VS |
2556 | i915_gem_execbuffer2_ioctl(struct drm_device *dev, void *data, |
2557 | struct drm_file *file) | |
54cf91dc CW |
2558 | { |
2559 | struct drm_i915_gem_execbuffer2 *args = data; | |
2889caa9 | 2560 | struct drm_i915_gem_exec_object2 *exec2_list; |
cf6e7bac | 2561 | struct drm_syncobj **fences = NULL; |
d710fc16 | 2562 | const size_t count = args->buffer_count; |
2889caa9 | 2563 | int err; |
54cf91dc | 2564 | |
d710fc16 CW |
2565 | if (!check_buffer_count(count)) { |
2566 | DRM_DEBUG("execbuf2 with %zd buffers\n", count); | |
54cf91dc CW |
2567 | return -EINVAL; |
2568 | } | |
2569 | ||
2889caa9 CW |
2570 | if (!i915_gem_check_execbuffer(args)) |
2571 | return -EINVAL; | |
2572 | ||
2573 | /* Allocate an extra slot for use by the command parser */ | |
d710fc16 | 2574 | exec2_list = kvmalloc_array(count + 1, eb_element_size(), |
0ee931c4 | 2575 | __GFP_NOWARN | GFP_KERNEL); |
54cf91dc | 2576 | if (exec2_list == NULL) { |
d710fc16 CW |
2577 | DRM_DEBUG("Failed to allocate exec list for %zd buffers\n", |
2578 | count); | |
54cf91dc CW |
2579 | return -ENOMEM; |
2580 | } | |
2889caa9 CW |
2581 | if (copy_from_user(exec2_list, |
2582 | u64_to_user_ptr(args->buffers_ptr), | |
d710fc16 CW |
2583 | sizeof(*exec2_list) * count)) { |
2584 | DRM_DEBUG("copy %zd exec entries failed\n", count); | |
2098105e | 2585 | kvfree(exec2_list); |
54cf91dc CW |
2586 | return -EFAULT; |
2587 | } | |
2588 | ||
cf6e7bac JE |
2589 | if (args->flags & I915_EXEC_FENCE_ARRAY) { |
2590 | fences = get_fence_array(args, file); | |
2591 | if (IS_ERR(fences)) { | |
2592 | kvfree(exec2_list); | |
2593 | return PTR_ERR(fences); | |
2594 | } | |
2595 | } | |
2596 | ||
2597 | err = i915_gem_do_execbuffer(dev, file, args, exec2_list, fences); | |
2889caa9 CW |
2598 | |
2599 | /* | |
2600 | * Now that we have begun execution of the batchbuffer, we ignore | |
2601 | * any new error after this point. Also given that we have already | |
2602 | * updated the associated relocations, we try to write out the current | |
2603 | * object locations irrespective of any error. | |
2604 | */ | |
2605 | if (args->flags & __EXEC_HAS_RELOC) { | |
d593d992 | 2606 | struct drm_i915_gem_exec_object2 __user *user_exec_list = |
2889caa9 CW |
2607 | u64_to_user_ptr(args->buffers_ptr); |
2608 | unsigned int i; | |
9aab8bff | 2609 | |
2889caa9 | 2610 | /* Copy the new buffer offsets back to the user's exec list. */ |
594cc251 LT |
2611 | /* |
2612 | * Note: count * sizeof(*user_exec_list) does not overflow, | |
2613 | * because we checked 'count' in check_buffer_count(). | |
2614 | * | |
2615 | * And this range already got effectively checked earlier | |
2616 | * when we did the "copy_from_user()" above. | |
2617 | */ | |
2618 | if (!user_access_begin(user_exec_list, count * sizeof(*user_exec_list))) | |
2619 | goto end_user; | |
2620 | ||
9aab8bff | 2621 | for (i = 0; i < args->buffer_count; i++) { |
2889caa9 CW |
2622 | if (!(exec2_list[i].offset & UPDATE)) |
2623 | continue; | |
2624 | ||
934acce3 | 2625 | exec2_list[i].offset = |
2889caa9 CW |
2626 | gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK); |
2627 | unsafe_put_user(exec2_list[i].offset, | |
2628 | &user_exec_list[i].offset, | |
2629 | end_user); | |
54cf91dc | 2630 | } |
2889caa9 CW |
2631 | end_user: |
2632 | user_access_end(); | |
54cf91dc CW |
2633 | } |
2634 | ||
2889caa9 | 2635 | args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS; |
cf6e7bac | 2636 | put_fence_array(args, fences); |
2098105e | 2637 | kvfree(exec2_list); |
2889caa9 | 2638 | return err; |
54cf91dc | 2639 | } |