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