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