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