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