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