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