blob: ec33b358fba9503b5435fb3a7df9cc0734a13b39 [file] [log] [blame]
Chris Wilson54cf91d2010-11-25 18:00:26 +00001/*
2 * Copyright © 2008,2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
26 *
27 */
28
Eugeni Dodonovf45b5552011-12-09 17:16:37 -080029#include <linux/dma_remapping.h>
Chris Wilsonad778f82016-08-04 16:32:42 +010030#include <linux/reservation.h>
Chris Wilsonfec04452017-01-27 09:40:08 +000031#include <linux/sync_file.h>
David Hildenbrand32d82062015-05-11 17:52:12 +020032#include <linux/uaccess.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000033
Chris Wilson54cf91d2010-11-25 18:00:26 +000034#include <drm/drmP.h>
35#include <drm/i915_drm.h>
Chris Wilsonad778f82016-08-04 16:32:42 +010036
Chris Wilson54cf91d2010-11-25 18:00:26 +000037#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000038#include "i915_gem_clflush.h"
Chris Wilson54cf91d2010-11-25 18:00:26 +000039#include "i915_trace.h"
40#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010041#include "intel_frontbuffer.h"
Chris Wilson54cf91d2010-11-25 18:00:26 +000042
Chris Wilson7dd4f672017-06-16 15:05:24 +010043enum {
44 FORCE_CPU_RELOC = 1,
45 FORCE_GTT_RELOC,
46 FORCE_GPU_RELOC,
47#define DBG_FORCE_RELOC 0 /* choose one of the above! */
48};
Chris Wilsond50415c2016-08-18 17:16:52 +010049
Chris Wilsondade2a62017-06-16 15:05:20 +010050#define __EXEC_OBJECT_HAS_REF BIT(31)
51#define __EXEC_OBJECT_HAS_PIN BIT(30)
52#define __EXEC_OBJECT_HAS_FENCE BIT(29)
53#define __EXEC_OBJECT_NEEDS_MAP BIT(28)
54#define __EXEC_OBJECT_NEEDS_BIAS BIT(27)
55#define __EXEC_OBJECT_INTERNAL_FLAGS (~0u << 27) /* all of the above */
Chris Wilson2889caa2017-06-16 15:05:19 +010056#define __EXEC_OBJECT_RESERVED (__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_FENCE)
57
58#define __EXEC_HAS_RELOC BIT(31)
59#define __EXEC_VALIDATED BIT(30)
60#define UPDATE PIN_OFFSET_FIXED
Chris Wilsond23db882014-05-23 08:48:08 +020061
62#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000063
Chris Wilson650bc632017-06-15 09:14:33 +010064#define __I915_EXEC_ILLEGAL_FLAGS \
65 (__I915_EXEC_UNKNOWN_FLAGS | I915_EXEC_CONSTANTS_MASK)
Chris Wilson5b043f42016-08-02 22:50:38 +010066
Chris Wilson2889caa2017-06-16 15:05:19 +010067/**
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 *
82 * Processing an execbuf ioctl is conceptually split up into a few phases.
83 *
84 * 1. Validation - Ensure all the pointers, handles and flags are valid.
85 * 2. Reservation - Assign GPU address space for every object
86 * 3. Relocation - Update any addresses to point to the final locations
87 * 4. Serialisation - Order the request with respect to its dependencies
88 * 5. Construction - Construct a request to execute the batchbuffer
89 * 6. Submission (at some point in the future execution)
90 *
91 * Reserving resources for the execbuf is the most complicated phase. We
92 * neither want to have to migrate the object in the address space, nor do
93 * we want to have to update any relocations pointing to this object. Ideally,
94 * we want to leave the object where it is and for all the existing relocations
95 * to match. If the object is given a new address, or if userspace thinks the
96 * object is elsewhere, we have to parse all the relocation entries and update
97 * the addresses. Userspace can set the I915_EXEC_NORELOC flag to hint that
98 * all the target addresses in all of its objects match the value in the
99 * relocation entries and that they all match the presumed offsets given by the
100 * list of execbuffer objects. Using this knowledge, we know that if we haven't
101 * moved any buffers, all the relocation entries are valid and we can skip
102 * the update. (If userspace is wrong, the likely outcome is an impromptu GPU
103 * hang.) The requirement for using I915_EXEC_NO_RELOC are:
104 *
105 * The addresses written in the objects must match the corresponding
106 * reloc.presumed_offset which in turn must match the corresponding
107 * execobject.offset.
108 *
109 * Any render targets written to in the batch must be flagged with
110 * EXEC_OBJECT_WRITE.
111 *
112 * To avoid stalling, execobject.offset should match the current
113 * address of that object within the active context.
114 *
115 * The reservation is done is multiple phases. First we try and keep any
116 * object already bound in its current location - so as long as meets the
117 * constraints imposed by the new execbuffer. Any object left unbound after the
118 * first pass is then fitted into any available idle space. If an object does
119 * not fit, all objects are removed from the reservation and the process rerun
120 * after sorting the objects into a priority order (more difficult to fit
121 * objects are tried first). Failing that, the entire VM is cleared and we try
122 * to fit the execbuf once last time before concluding that it simply will not
123 * fit.
124 *
125 * A small complication to all of this is that we allow userspace not only to
126 * specify an alignment and a size for the object in the address space, but
127 * we also allow userspace to specify the exact offset. This objects are
128 * simpler to place (the location is known a priori) all we have to do is make
129 * sure the space is available.
130 *
131 * Once all the objects are in place, patching up the buried pointers to point
132 * to the final locations is a fairly simple job of walking over the relocation
133 * entry arrays, looking up the right address and rewriting the value into
134 * the object. Simple! ... The relocation entries are stored in user memory
135 * and so to access them we have to copy them into a local buffer. That copy
136 * has to avoid taking any pagefaults as they may lead back to a GEM object
137 * requiring the struct_mutex (i.e. recursive deadlock). So once again we split
138 * the relocation into multiple passes. First we try to do everything within an
139 * atomic context (avoid the pagefaults) which requires that we never wait. If
140 * we detect that we may wait, or if we need to fault, then we have to fallback
141 * to a slower path. The slowpath has to drop the mutex. (Can you hear alarm
142 * bells yet?) Dropping the mutex means that we lose all the state we have
143 * built up so far for the execbuf and we must reset any global data. However,
144 * we do leave the objects pinned in their final locations - which is a
145 * potential issue for concurrent execbufs. Once we have left the mutex, we can
146 * allocate and copy all the relocation entries into a large array at our
147 * leisure, reacquire the mutex, reclaim all the objects and other state and
148 * then proceed to update any incorrect addresses with the objects.
149 *
150 * As we process the relocation entries, we maintain a record of whether the
151 * object is being written to. Using NORELOC, we expect userspace to provide
152 * this information instead. We also check whether we can skip the relocation
153 * by comparing the expected value inside the relocation entry with the target's
154 * final address. If they differ, we have to map the current object and rewrite
155 * the 4 or 8 byte pointer within.
156 *
157 * Serialising an execbuf is quite simple according to the rules of the GEM
158 * ABI. Execution within each context is ordered by the order of submission.
159 * Writes to any GEM object are in order of submission and are exclusive. Reads
160 * from a GEM object are unordered with respect to other reads, but ordered by
161 * writes. A write submitted after a read cannot occur before the read, and
162 * similarly any read submitted after a write cannot occur before the write.
163 * Writes are ordered between engines such that only one write occurs at any
164 * time (completing any reads beforehand) - using semaphores where available
165 * and CPU serialisation otherwise. Other GEM access obey the same rules, any
166 * write (either via mmaps using set-domain, or via pwrite) must flush all GPU
167 * reads before starting, and any read (either using set-domain or pread) must
168 * flush all GPU writes before starting. (Note we only employ a barrier before,
169 * we currently rely on userspace not concurrently starting a new execution
170 * whilst reading or writing to an object. This may be an advantage or not
171 * depending on how much you trust userspace not to shoot themselves in the
172 * foot.) Serialisation may just result in the request being inserted into
173 * a DAG awaiting its turn, but most simple is to wait on the CPU until
174 * all dependencies are resolved.
175 *
176 * After all of that, is just a matter of closing the request and handing it to
177 * the hardware (well, leaving it in a queue to be executed). However, we also
178 * offer the ability for batchbuffers to be run with elevated privileges so
179 * that they access otherwise hidden registers. (Used to adjust L3 cache etc.)
180 * Before any batch is given extra privileges we first must check that it
181 * contains no nefarious instructions, we check that each instruction is from
182 * our whitelist and all registers are also from an allowed list. We first
183 * copy the user's batchbuffer to a shadow (so that the user doesn't have
184 * access to it, either by the CPU or GPU as we scan it) and then parse each
185 * instruction. If everything is ok, we set a flag telling the hardware to run
186 * the batchbuffer in trusted mode, otherwise the ioctl is rejected.
187 */
188
Chris Wilson650bc632017-06-15 09:14:33 +0100189struct i915_execbuffer {
Chris Wilson2889caa2017-06-16 15:05:19 +0100190 struct drm_i915_private *i915; /** i915 backpointer */
191 struct drm_file *file; /** per-file lookup tables and limits */
192 struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */
193 struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */
194
195 struct intel_engine_cs *engine; /** engine to queue the request to */
196 struct i915_gem_context *ctx; /** context for building the request */
197 struct i915_address_space *vm; /** GTT and vma for the request */
198
199 struct drm_i915_gem_request *request; /** our request to build */
200 struct i915_vma *batch; /** identity of the batch obj/vma */
201
202 /** actual size of execobj[] as we may extend it for the cmdparser */
203 unsigned int buffer_count;
204
205 /** list of vma not yet bound during reservation phase */
206 struct list_head unbound;
207
208 /** list of vma that have execobj.relocation_count */
209 struct list_head relocs;
210
211 /**
212 * Track the most recently used object for relocations, as we
213 * frequently have to perform multiple relocations within the same
214 * obj/page
215 */
Chris Wilson650bc632017-06-15 09:14:33 +0100216 struct reloc_cache {
Chris Wilson2889caa2017-06-16 15:05:19 +0100217 struct drm_mm_node node; /** temporary GTT binding */
218 unsigned long vaddr; /** Current kmap address */
219 unsigned long page; /** Currently mapped page index */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100220 unsigned int gen; /** Cached value of INTEL_GEN */
Chris Wilson650bc632017-06-15 09:14:33 +0100221 bool use_64bit_reloc : 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100222 bool has_llc : 1;
223 bool has_fence : 1;
224 bool needs_unfenced : 1;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100225
226 struct drm_i915_gem_request *rq;
227 u32 *rq_cmd;
228 unsigned int rq_size;
Chris Wilson650bc632017-06-15 09:14:33 +0100229 } reloc_cache;
Chris Wilson2889caa2017-06-16 15:05:19 +0100230
231 u64 invalid_flags; /** Set of execobj.flags that are invalid */
232 u32 context_flags; /** Set of execobj.flags to insert from the ctx */
233
234 u32 batch_start_offset; /** Location within object of batch */
235 u32 batch_len; /** Length of batch within object */
236 u32 batch_flags; /** Flags composed for emit_bb_start() */
237
238 /**
239 * Indicate either the size of the hastable used to resolve
240 * relocation handles, or if negative that we are using a direct
241 * index into the execobj[].
242 */
243 int lut_size;
244 struct hlist_head *buckets; /** ht for relocation handles */
Chris Wilson67731b82010-12-08 10:38:14 +0000245};
246
Chris Wilson4ff4b442017-06-16 15:05:16 +0100247/*
248 * As an alternative to creating a hashtable of handle-to-vma for a batch,
249 * we used the last available reserved field in the execobject[] and stash
250 * a link from the execobj to its vma.
251 */
252#define __exec_to_vma(ee) (ee)->rsvd2
253#define exec_to_vma(ee) u64_to_ptr(struct i915_vma, __exec_to_vma(ee))
254
Chris Wilson2889caa2017-06-16 15:05:19 +0100255/*
256 * Used to convert any address to canonical form.
257 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
258 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
259 * addresses to be in a canonical form:
260 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
261 * canonical form [63:48] == [47]."
262 */
263#define GEN8_HIGH_ADDRESS_BIT 47
264static inline u64 gen8_canonical_addr(u64 address)
265{
266 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
267}
268
269static inline u64 gen8_noncanonical_addr(u64 address)
270{
271 return address & GENMASK_ULL(GEN8_HIGH_ADDRESS_BIT, 0);
272}
273
Chris Wilson650bc632017-06-15 09:14:33 +0100274static int eb_create(struct i915_execbuffer *eb)
Chris Wilson67731b82010-12-08 10:38:14 +0000275{
Chris Wilson2889caa2017-06-16 15:05:19 +0100276 if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) {
277 unsigned int size = 1 + ilog2(eb->buffer_count);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000278
Chris Wilson2889caa2017-06-16 15:05:19 +0100279 /*
280 * Without a 1:1 association between relocation handles and
281 * the execobject[] index, we instead create a hashtable.
282 * We size it dynamically based on available memory, starting
283 * first with 1:1 assocative hash and scaling back until
284 * the allocation succeeds.
285 *
286 * Later on we use a positive lut_size to indicate we are
287 * using this hashtable, and a negative value to indicate a
288 * direct lookup.
289 */
Chris Wilson4ff4b442017-06-16 15:05:16 +0100290 do {
291 eb->buckets = kzalloc(sizeof(struct hlist_head) << size,
292 GFP_TEMPORARY |
293 __GFP_NORETRY |
294 __GFP_NOWARN);
295 if (eb->buckets)
296 break;
297 } while (--size);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000298
Chris Wilson4ff4b442017-06-16 15:05:16 +0100299 if (unlikely(!eb->buckets)) {
300 eb->buckets = kzalloc(sizeof(struct hlist_head),
301 GFP_TEMPORARY);
302 if (unlikely(!eb->buckets))
303 return -ENOMEM;
304 }
305
Chris Wilson2889caa2017-06-16 15:05:19 +0100306 eb->lut_size = size;
Chris Wilson650bc632017-06-15 09:14:33 +0100307 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +0100308 eb->lut_size = -eb->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +0100309 }
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000310
Chris Wilson650bc632017-06-15 09:14:33 +0100311 return 0;
Chris Wilson67731b82010-12-08 10:38:14 +0000312}
313
Chris Wilson2889caa2017-06-16 15:05:19 +0100314static bool
315eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry,
316 const struct i915_vma *vma)
317{
318 if (!(entry->flags & __EXEC_OBJECT_HAS_PIN))
319 return true;
320
321 if (vma->node.size < entry->pad_to_size)
322 return true;
323
324 if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment))
325 return true;
326
327 if (entry->flags & EXEC_OBJECT_PINNED &&
328 vma->node.start != entry->offset)
329 return true;
330
331 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
332 vma->node.start < BATCH_OFFSET_BIAS)
333 return true;
334
335 if (!(entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) &&
336 (vma->node.start + vma->node.size - 1) >> 32)
337 return true;
338
339 return false;
340}
341
342static inline void
343eb_pin_vma(struct i915_execbuffer *eb,
344 struct drm_i915_gem_exec_object2 *entry,
345 struct i915_vma *vma)
346{
347 u64 flags;
348
Chris Wilson616d9ce2017-06-16 15:05:21 +0100349 if (vma->node.size)
350 flags = vma->node.start;
351 else
352 flags = entry->offset & PIN_OFFSET_MASK;
353
354 flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED;
Chris Wilson2889caa2017-06-16 15:05:19 +0100355 if (unlikely(entry->flags & EXEC_OBJECT_NEEDS_GTT))
356 flags |= PIN_GLOBAL;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100357
Chris Wilson2889caa2017-06-16 15:05:19 +0100358 if (unlikely(i915_vma_pin(vma, 0, 0, flags)))
359 return;
360
361 if (unlikely(entry->flags & EXEC_OBJECT_NEEDS_FENCE)) {
362 if (unlikely(i915_vma_get_fence(vma))) {
363 i915_vma_unpin(vma);
364 return;
365 }
366
367 if (i915_vma_pin_fence(vma))
368 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
369 }
370
371 entry->flags |= __EXEC_OBJECT_HAS_PIN;
372}
373
Chris Wilsond55495b2017-06-15 09:14:34 +0100374static inline void
375__eb_unreserve_vma(struct i915_vma *vma,
376 const struct drm_i915_gem_exec_object2 *entry)
377{
Chris Wilson2889caa2017-06-16 15:05:19 +0100378 GEM_BUG_ON(!(entry->flags & __EXEC_OBJECT_HAS_PIN));
379
Chris Wilsond55495b2017-06-15 09:14:34 +0100380 if (unlikely(entry->flags & __EXEC_OBJECT_HAS_FENCE))
381 i915_vma_unpin_fence(vma);
382
Chris Wilson2889caa2017-06-16 15:05:19 +0100383 __i915_vma_unpin(vma);
Chris Wilsond55495b2017-06-15 09:14:34 +0100384}
385
Chris Wilson2889caa2017-06-16 15:05:19 +0100386static inline void
387eb_unreserve_vma(struct i915_vma *vma,
388 struct drm_i915_gem_exec_object2 *entry)
Chris Wilsond55495b2017-06-15 09:14:34 +0100389{
Chris Wilson2889caa2017-06-16 15:05:19 +0100390 if (!(entry->flags & __EXEC_OBJECT_HAS_PIN))
391 return;
Chris Wilsond55495b2017-06-15 09:14:34 +0100392
393 __eb_unreserve_vma(vma, entry);
Chris Wilson2889caa2017-06-16 15:05:19 +0100394 entry->flags &= ~__EXEC_OBJECT_RESERVED;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100395}
396
397static int
Chris Wilson2889caa2017-06-16 15:05:19 +0100398eb_validate_vma(struct i915_execbuffer *eb,
399 struct drm_i915_gem_exec_object2 *entry,
400 struct i915_vma *vma)
401{
402 if (unlikely(entry->flags & eb->invalid_flags))
403 return -EINVAL;
404
405 if (unlikely(entry->alignment && !is_power_of_2(entry->alignment)))
406 return -EINVAL;
407
408 /*
409 * Offset can be used as input (EXEC_OBJECT_PINNED), reject
410 * any non-page-aligned or non-canonical addresses.
411 */
412 if (unlikely(entry->flags & EXEC_OBJECT_PINNED &&
413 entry->offset != gen8_canonical_addr(entry->offset & PAGE_MASK)))
414 return -EINVAL;
415
416 /* pad_to_size was once a reserved field, so sanitize it */
417 if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) {
418 if (unlikely(offset_in_page(entry->pad_to_size)))
419 return -EINVAL;
420 } else {
421 entry->pad_to_size = 0;
422 }
423
424 if (unlikely(vma->exec_entry)) {
425 DRM_DEBUG("Object [handle %d, index %d] appears more than once in object list\n",
426 entry->handle, (int)(entry - eb->exec));
427 return -EINVAL;
428 }
429
430 /*
431 * From drm_mm perspective address space is continuous,
432 * so from this point we're always using non-canonical
433 * form internally.
434 */
435 entry->offset = gen8_noncanonical_addr(entry->offset);
436
437 return 0;
438}
439
440static int
441eb_add_vma(struct i915_execbuffer *eb,
442 struct drm_i915_gem_exec_object2 *entry,
443 struct i915_vma *vma)
444{
445 int err;
446
447 GEM_BUG_ON(i915_vma_is_closed(vma));
448
449 if (!(eb->args->flags & __EXEC_VALIDATED)) {
450 err = eb_validate_vma(eb, entry, vma);
451 if (unlikely(err))
452 return err;
453 }
454
455 if (eb->lut_size >= 0) {
456 vma->exec_handle = entry->handle;
457 hlist_add_head(&vma->exec_node,
458 &eb->buckets[hash_32(entry->handle,
459 eb->lut_size)]);
460 }
461
462 if (entry->relocation_count)
463 list_add_tail(&vma->reloc_link, &eb->relocs);
464
465 if (!eb->reloc_cache.has_fence) {
466 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
467 } else {
468 if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE ||
469 eb->reloc_cache.needs_unfenced) &&
470 i915_gem_object_is_tiled(vma->obj))
471 entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP;
472 }
473
474 if (!(entry->flags & EXEC_OBJECT_PINNED))
475 entry->flags |= eb->context_flags;
476
477 /*
478 * Stash a pointer from the vma to execobj, so we can query its flags,
479 * size, alignment etc as provided by the user. Also we stash a pointer
480 * to the vma inside the execobj so that we can use a direct lookup
481 * to find the right target VMA when doing relocations.
482 */
483 vma->exec_entry = entry;
Chris Wilsondade2a62017-06-16 15:05:20 +0100484 __exec_to_vma(entry) = (uintptr_t)vma;
Chris Wilson2889caa2017-06-16 15:05:19 +0100485
486 err = 0;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100487 eb_pin_vma(eb, entry, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100488 if (eb_vma_misplaced(entry, vma)) {
489 eb_unreserve_vma(vma, entry);
490
491 list_add_tail(&vma->exec_link, &eb->unbound);
492 if (drm_mm_node_allocated(&vma->node))
493 err = i915_vma_unbind(vma);
494 } else {
495 if (entry->offset != vma->node.start) {
496 entry->offset = vma->node.start | UPDATE;
497 eb->args->flags |= __EXEC_HAS_RELOC;
498 }
499 }
500 return err;
501}
502
503static inline int use_cpu_reloc(const struct reloc_cache *cache,
504 const struct drm_i915_gem_object *obj)
505{
506 if (!i915_gem_object_has_struct_page(obj))
507 return false;
508
Chris Wilson7dd4f672017-06-16 15:05:24 +0100509 if (DBG_FORCE_RELOC == FORCE_CPU_RELOC)
510 return true;
511
512 if (DBG_FORCE_RELOC == FORCE_GTT_RELOC)
513 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100514
515 return (cache->has_llc ||
516 obj->cache_dirty ||
517 obj->cache_level != I915_CACHE_NONE);
518}
519
520static int eb_reserve_vma(const struct i915_execbuffer *eb,
521 struct i915_vma *vma)
522{
523 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
524 u64 flags;
525 int err;
526
527 flags = PIN_USER | PIN_NONBLOCK;
528 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
529 flags |= PIN_GLOBAL;
530
531 /*
532 * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
533 * limit address to the first 4GBs for unflagged objects.
534 */
535 if (!(entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
536 flags |= PIN_ZONE_4G;
537
538 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
539 flags |= PIN_MAPPABLE;
540
541 if (entry->flags & EXEC_OBJECT_PINNED) {
542 flags |= entry->offset | PIN_OFFSET_FIXED;
543 flags &= ~PIN_NONBLOCK; /* force overlapping PINNED checks */
544 } else if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS) {
545 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
546 }
547
548 err = i915_vma_pin(vma, entry->pad_to_size, entry->alignment, flags);
549 if (err)
550 return err;
551
552 if (entry->offset != vma->node.start) {
553 entry->offset = vma->node.start | UPDATE;
554 eb->args->flags |= __EXEC_HAS_RELOC;
555 }
556
557 entry->flags |= __EXEC_OBJECT_HAS_PIN;
558 GEM_BUG_ON(eb_vma_misplaced(entry, vma));
559
560 if (unlikely(entry->flags & EXEC_OBJECT_NEEDS_FENCE)) {
561 err = i915_vma_get_fence(vma);
562 if (unlikely(err)) {
563 i915_vma_unpin(vma);
564 return err;
565 }
566
567 if (i915_vma_pin_fence(vma))
568 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
569 }
570
571 return 0;
572}
573
574static int eb_reserve(struct i915_execbuffer *eb)
575{
576 const unsigned int count = eb->buffer_count;
577 struct list_head last;
578 struct i915_vma *vma;
579 unsigned int i, pass;
580 int err;
581
582 /*
583 * Attempt to pin all of the buffers into the GTT.
584 * This is done in 3 phases:
585 *
586 * 1a. Unbind all objects that do not match the GTT constraints for
587 * the execbuffer (fenceable, mappable, alignment etc).
588 * 1b. Increment pin count for already bound objects.
589 * 2. Bind new objects.
590 * 3. Decrement pin count.
591 *
592 * This avoid unnecessary unbinding of later objects in order to make
593 * room for the earlier objects *unless* we need to defragment.
594 */
595
596 pass = 0;
597 err = 0;
598 do {
599 list_for_each_entry(vma, &eb->unbound, exec_link) {
600 err = eb_reserve_vma(eb, vma);
601 if (err)
602 break;
603 }
604 if (err != -ENOSPC)
605 return err;
606
607 /* Resort *all* the objects into priority order */
608 INIT_LIST_HEAD(&eb->unbound);
609 INIT_LIST_HEAD(&last);
610 for (i = 0; i < count; i++) {
611 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
612
613 if (entry->flags & EXEC_OBJECT_PINNED &&
614 entry->flags & __EXEC_OBJECT_HAS_PIN)
615 continue;
616
617 vma = exec_to_vma(entry);
618 eb_unreserve_vma(vma, entry);
619
620 if (entry->flags & EXEC_OBJECT_PINNED)
621 list_add(&vma->exec_link, &eb->unbound);
622 else if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
623 list_add_tail(&vma->exec_link, &eb->unbound);
624 else
625 list_add_tail(&vma->exec_link, &last);
626 }
627 list_splice_tail(&last, &eb->unbound);
628
629 switch (pass++) {
630 case 0:
631 break;
632
633 case 1:
634 /* Too fragmented, unbind everything and retry */
635 err = i915_gem_evict_vm(eb->vm);
636 if (err)
637 return err;
638 break;
639
640 default:
641 return -ENOSPC;
642 }
643 } while (1);
644}
645
646static inline struct hlist_head *
647ht_head(const struct i915_gem_context_vma_lut *lut, u32 handle)
648{
649 return &lut->ht[hash_32(handle, lut->ht_bits)];
650}
651
652static inline bool
653ht_needs_resize(const struct i915_gem_context_vma_lut *lut)
654{
655 return (4*lut->ht_count > 3*lut->ht_size ||
656 4*lut->ht_count + 1 < lut->ht_size);
657}
658
659static unsigned int eb_batch_index(const struct i915_execbuffer *eb)
660{
Chris Wilson1a71cf22017-06-16 15:05:23 +0100661 if (eb->args->flags & I915_EXEC_BATCH_FIRST)
662 return 0;
663 else
664 return eb->buffer_count - 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100665}
666
667static int eb_select_context(struct i915_execbuffer *eb)
668{
669 struct i915_gem_context *ctx;
670
671 ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1);
Chris Wilson1acfc102017-06-20 12:05:47 +0100672 if (unlikely(!ctx))
673 return -ENOENT;
Chris Wilson2889caa2017-06-16 15:05:19 +0100674
675 if (unlikely(i915_gem_context_is_banned(ctx))) {
676 DRM_DEBUG("Context %u tried to submit while banned\n",
677 ctx->user_handle);
Chris Wilson1acfc102017-06-20 12:05:47 +0100678 i915_gem_context_put(ctx);
Chris Wilson2889caa2017-06-16 15:05:19 +0100679 return -EIO;
680 }
681
Chris Wilson1acfc102017-06-20 12:05:47 +0100682 eb->ctx = ctx;
Chris Wilson2889caa2017-06-16 15:05:19 +0100683 eb->vm = ctx->ppgtt ? &ctx->ppgtt->base : &eb->i915->ggtt.base;
684
685 eb->context_flags = 0;
686 if (ctx->flags & CONTEXT_NO_ZEROMAP)
687 eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS;
688
689 return 0;
690}
691
692static int eb_lookup_vmas(struct i915_execbuffer *eb)
Chris Wilson4ff4b442017-06-16 15:05:16 +0100693{
694#define INTERMEDIATE BIT(0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100695 const unsigned int count = eb->buffer_count;
696 struct i915_gem_context_vma_lut *lut = &eb->ctx->vma_lut;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100697 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +0100698 struct idr *idr;
699 unsigned int i;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100700 int slow_pass = -1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100701 int err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100702
Chris Wilson2889caa2017-06-16 15:05:19 +0100703 INIT_LIST_HEAD(&eb->relocs);
704 INIT_LIST_HEAD(&eb->unbound);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100705
Chris Wilson2889caa2017-06-16 15:05:19 +0100706 if (unlikely(lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS))
707 flush_work(&lut->resize);
708 GEM_BUG_ON(lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100709
710 for (i = 0; i < count; i++) {
711 __exec_to_vma(&eb->exec[i]) = 0;
712
713 hlist_for_each_entry(vma,
Chris Wilson2889caa2017-06-16 15:05:19 +0100714 ht_head(lut, eb->exec[i].handle),
Chris Wilson4ff4b442017-06-16 15:05:16 +0100715 ctx_node) {
716 if (vma->ctx_handle != eb->exec[i].handle)
717 continue;
718
Chris Wilson2889caa2017-06-16 15:05:19 +0100719 err = eb_add_vma(eb, &eb->exec[i], vma);
720 if (unlikely(err))
721 return err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100722
723 goto next_vma;
724 }
725
726 if (slow_pass < 0)
727 slow_pass = i;
728next_vma: ;
729 }
730
731 if (slow_pass < 0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100732 goto out;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100733
734 spin_lock(&eb->file->table_lock);
Chris Wilson2889caa2017-06-16 15:05:19 +0100735 /*
736 * Grab a reference to the object and release the lock so we can lookup
737 * or create the VMA without using GFP_ATOMIC
738 */
739 idr = &eb->file->object_idr;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100740 for (i = slow_pass; i < count; i++) {
741 struct drm_i915_gem_object *obj;
742
743 if (__exec_to_vma(&eb->exec[i]))
744 continue;
745
Chris Wilson2889caa2017-06-16 15:05:19 +0100746 obj = to_intel_bo(idr_find(idr, eb->exec[i].handle));
Chris Wilson4ff4b442017-06-16 15:05:16 +0100747 if (unlikely(!obj)) {
748 spin_unlock(&eb->file->table_lock);
749 DRM_DEBUG("Invalid object handle %d at index %d\n",
750 eb->exec[i].handle, i);
Chris Wilson2889caa2017-06-16 15:05:19 +0100751 err = -ENOENT;
752 goto err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100753 }
754
755 __exec_to_vma(&eb->exec[i]) = INTERMEDIATE | (uintptr_t)obj;
756 }
757 spin_unlock(&eb->file->table_lock);
758
759 for (i = slow_pass; i < count; i++) {
760 struct drm_i915_gem_object *obj;
761
Chris Wilson2889caa2017-06-16 15:05:19 +0100762 if (!(__exec_to_vma(&eb->exec[i]) & INTERMEDIATE))
Chris Wilson4ff4b442017-06-16 15:05:16 +0100763 continue;
764
765 /*
766 * NOTE: We can leak any vmas created here when something fails
767 * later on. But that's no issue since vma_unbind can deal with
768 * vmas which are not actually bound. And since only
769 * lookup_or_create exists as an interface to get at the vma
770 * from the (obj, vm) we don't run the risk of creating
771 * duplicated vmas for the same vm.
772 */
Chris Wilson2889caa2017-06-16 15:05:19 +0100773 obj = u64_to_ptr(typeof(*obj),
Chris Wilson4ff4b442017-06-16 15:05:16 +0100774 __exec_to_vma(&eb->exec[i]) & ~INTERMEDIATE);
775 vma = i915_vma_instance(obj, eb->vm, NULL);
776 if (unlikely(IS_ERR(vma))) {
777 DRM_DEBUG("Failed to lookup VMA\n");
Chris Wilson2889caa2017-06-16 15:05:19 +0100778 err = PTR_ERR(vma);
779 goto err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100780 }
781
782 /* First come, first served */
783 if (!vma->ctx) {
784 vma->ctx = eb->ctx;
785 vma->ctx_handle = eb->exec[i].handle;
786 hlist_add_head(&vma->ctx_node,
Chris Wilson2889caa2017-06-16 15:05:19 +0100787 ht_head(lut, eb->exec[i].handle));
788 lut->ht_count++;
789 lut->ht_size |= I915_CTX_RESIZE_IN_PROGRESS;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100790 if (i915_vma_is_ggtt(vma)) {
791 GEM_BUG_ON(obj->vma_hashed);
792 obj->vma_hashed = vma;
793 }
Chris Wilsondade2a62017-06-16 15:05:20 +0100794
795 i915_vma_get(vma);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100796 }
797
Chris Wilson2889caa2017-06-16 15:05:19 +0100798 err = eb_add_vma(eb, &eb->exec[i], vma);
799 if (unlikely(err))
800 goto err;
Chris Wilsondade2a62017-06-16 15:05:20 +0100801
802 /* Only after we validated the user didn't use our bits */
803 if (vma->ctx != eb->ctx) {
804 i915_vma_get(vma);
805 eb->exec[i].flags |= __EXEC_OBJECT_HAS_REF;
806 }
Chris Wilson4ff4b442017-06-16 15:05:16 +0100807 }
808
Chris Wilson2889caa2017-06-16 15:05:19 +0100809 if (lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS) {
810 if (ht_needs_resize(lut))
811 queue_work(system_highpri_wq, &lut->resize);
812 else
813 lut->ht_size &= ~I915_CTX_RESIZE_IN_PROGRESS;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100814 }
815
Chris Wilson2889caa2017-06-16 15:05:19 +0100816out:
817 /* take note of the batch buffer before we might reorder the lists */
818 i = eb_batch_index(eb);
819 eb->batch = exec_to_vma(&eb->exec[i]);
Chris Wilson59bfa122016-08-04 16:32:31 +0100820
821 /*
822 * SNA is doing fancy tricks with compressing batch buffers, which leads
823 * to negative relocation deltas. Usually that works out ok since the
824 * relocate address is still positive, except when the batch is placed
825 * very low in the GTT. Ensure this doesn't happen.
826 *
827 * Note that actual hangs have only been observed on gen7, but for
828 * paranoia do it everywhere.
829 */
Chris Wilson2889caa2017-06-16 15:05:19 +0100830 if (!(eb->exec[i].flags & EXEC_OBJECT_PINNED))
831 eb->exec[i].flags |= __EXEC_OBJECT_NEEDS_BIAS;
832 if (eb->reloc_cache.has_fence)
833 eb->exec[i].flags |= EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson59bfa122016-08-04 16:32:31 +0100834
Chris Wilson2889caa2017-06-16 15:05:19 +0100835 eb->args->flags |= __EXEC_VALIDATED;
836 return eb_reserve(eb);
837
838err:
839 for (i = slow_pass; i < count; i++) {
840 if (__exec_to_vma(&eb->exec[i]) & INTERMEDIATE)
841 __exec_to_vma(&eb->exec[i]) = 0;
842 }
843 lut->ht_size &= ~I915_CTX_RESIZE_IN_PROGRESS;
844 return err;
845#undef INTERMEDIATE
Chris Wilson59bfa122016-08-04 16:32:31 +0100846}
847
Chris Wilson4ff4b442017-06-16 15:05:16 +0100848static struct i915_vma *
Chris Wilson2889caa2017-06-16 15:05:19 +0100849eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000850{
Chris Wilson2889caa2017-06-16 15:05:19 +0100851 if (eb->lut_size < 0) {
852 if (handle >= -eb->lut_size)
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000853 return NULL;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100854 return exec_to_vma(&eb->exec[handle]);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000855 } else {
856 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800857 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000858
Chris Wilson2889caa2017-06-16 15:05:19 +0100859 head = &eb->buckets[hash_32(handle, eb->lut_size)];
Geliang Tangaa459502016-01-18 23:54:20 +0800860 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200861 if (vma->exec_handle == handle)
862 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000863 }
864 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000865 }
Chris Wilson67731b82010-12-08 10:38:14 +0000866}
867
Chris Wilson2889caa2017-06-16 15:05:19 +0100868static void eb_release_vmas(const struct i915_execbuffer *eb)
Chris Wilsona415d352013-11-26 11:23:15 +0000869{
Chris Wilson2889caa2017-06-16 15:05:19 +0100870 const unsigned int count = eb->buffer_count;
871 unsigned int i;
Chris Wilson650bc632017-06-15 09:14:33 +0100872
Chris Wilson2889caa2017-06-16 15:05:19 +0100873 for (i = 0; i < count; i++) {
874 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
875 struct i915_vma *vma = exec_to_vma(entry);
876
877 if (!vma)
Chris Wilsond55495b2017-06-15 09:14:34 +0100878 continue;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000879
Chris Wilson2889caa2017-06-16 15:05:19 +0100880 GEM_BUG_ON(vma->exec_entry != entry);
Chris Wilson172ae5b2016-12-05 14:29:37 +0000881 vma->exec_entry = NULL;
Chris Wilson51d05e12017-06-22 11:47:22 +0100882 __exec_to_vma(entry) = 0;
Chris Wilson2889caa2017-06-16 15:05:19 +0100883
Chris Wilsondade2a62017-06-16 15:05:20 +0100884 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
885 __eb_unreserve_vma(vma, entry);
Chris Wilson2889caa2017-06-16 15:05:19 +0100886
Chris Wilsondade2a62017-06-16 15:05:20 +0100887 if (entry->flags & __EXEC_OBJECT_HAS_REF)
888 i915_vma_put(vma);
889
890 entry->flags &=
891 ~(__EXEC_OBJECT_RESERVED | __EXEC_OBJECT_HAS_REF);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000892 }
Chris Wilson2889caa2017-06-16 15:05:19 +0100893}
Chris Wilsond55495b2017-06-15 09:14:34 +0100894
Chris Wilson2889caa2017-06-16 15:05:19 +0100895static void eb_reset_vmas(const struct i915_execbuffer *eb)
896{
897 eb_release_vmas(eb);
898 if (eb->lut_size >= 0)
899 memset(eb->buckets, 0,
900 sizeof(struct hlist_head) << eb->lut_size);
901}
Chris Wilsond55495b2017-06-15 09:14:34 +0100902
Chris Wilson2889caa2017-06-16 15:05:19 +0100903static void eb_destroy(const struct i915_execbuffer *eb)
904{
Chris Wilson7dd4f672017-06-16 15:05:24 +0100905 GEM_BUG_ON(eb->reloc_cache.rq);
906
Chris Wilson2889caa2017-06-16 15:05:19 +0100907 if (eb->lut_size >= 0)
Chris Wilsond55495b2017-06-15 09:14:34 +0100908 kfree(eb->buckets);
Chris Wilson67731b82010-12-08 10:38:14 +0000909}
910
Chris Wilson2889caa2017-06-16 15:05:19 +0100911static inline u64
Chris Wilsond50415c2016-08-18 17:16:52 +0100912relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +0100913 const struct i915_vma *target)
Michał Winiarski934acce2015-12-29 18:24:52 +0100914{
Chris Wilson2889caa2017-06-16 15:05:19 +0100915 return gen8_canonical_addr((int)reloc->delta + target->node.start);
Michał Winiarski934acce2015-12-29 18:24:52 +0100916}
917
Chris Wilsond50415c2016-08-18 17:16:52 +0100918static void reloc_cache_init(struct reloc_cache *cache,
919 struct drm_i915_private *i915)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100920{
Chris Wilson31a39202016-08-18 17:16:46 +0100921 cache->page = -1;
Chris Wilsond50415c2016-08-18 17:16:52 +0100922 cache->vaddr = 0;
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200923 /* Must be a variable in the struct to allow GCC to unroll. */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100924 cache->gen = INTEL_GEN(i915);
Chris Wilson2889caa2017-06-16 15:05:19 +0100925 cache->has_llc = HAS_LLC(i915);
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200926 cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
Chris Wilson7dd4f672017-06-16 15:05:24 +0100927 cache->has_fence = cache->gen < 4;
928 cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
Chris Wilsone8cb9092016-08-18 17:16:53 +0100929 cache->node.allocated = false;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100930 cache->rq = NULL;
931 cache->rq_size = 0;
Chris Wilson31a39202016-08-18 17:16:46 +0100932}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100933
Chris Wilsond50415c2016-08-18 17:16:52 +0100934static inline void *unmask_page(unsigned long p)
935{
936 return (void *)(uintptr_t)(p & PAGE_MASK);
937}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100938
Chris Wilsond50415c2016-08-18 17:16:52 +0100939static inline unsigned int unmask_flags(unsigned long p)
940{
941 return p & ~PAGE_MASK;
942}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700943
Chris Wilsond50415c2016-08-18 17:16:52 +0100944#define KMAP 0x4 /* after CLFLUSH_FLAGS */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700945
Chris Wilson650bc632017-06-15 09:14:33 +0100946static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
947{
948 struct drm_i915_private *i915 =
949 container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
950 return &i915->ggtt;
951}
952
Chris Wilson7dd4f672017-06-16 15:05:24 +0100953static void reloc_gpu_flush(struct reloc_cache *cache)
954{
955 GEM_BUG_ON(cache->rq_size >= cache->rq->batch->obj->base.size / sizeof(u32));
956 cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END;
957 i915_gem_object_unpin_map(cache->rq->batch->obj);
958 i915_gem_chipset_flush(cache->rq->i915);
959
960 __i915_add_request(cache->rq, true);
961 cache->rq = NULL;
962}
963
Chris Wilson650bc632017-06-15 09:14:33 +0100964static void reloc_cache_reset(struct reloc_cache *cache)
Chris Wilson31a39202016-08-18 17:16:46 +0100965{
Chris Wilsond50415c2016-08-18 17:16:52 +0100966 void *vaddr;
967
Chris Wilson7dd4f672017-06-16 15:05:24 +0100968 if (cache->rq)
969 reloc_gpu_flush(cache);
970
Chris Wilson31a39202016-08-18 17:16:46 +0100971 if (!cache->vaddr)
972 return;
973
Chris Wilsond50415c2016-08-18 17:16:52 +0100974 vaddr = unmask_page(cache->vaddr);
975 if (cache->vaddr & KMAP) {
976 if (cache->vaddr & CLFLUSH_AFTER)
977 mb();
Chris Wilson31a39202016-08-18 17:16:46 +0100978
Chris Wilsond50415c2016-08-18 17:16:52 +0100979 kunmap_atomic(vaddr);
980 i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
981 } else {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100982 wmb();
Chris Wilsond50415c2016-08-18 17:16:52 +0100983 io_mapping_unmap_atomic((void __iomem *)vaddr);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100984 if (cache->node.allocated) {
Chris Wilson650bc632017-06-15 09:14:33 +0100985 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100986
987 ggtt->base.clear_range(&ggtt->base,
988 cache->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200989 cache->node.size);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100990 drm_mm_remove_node(&cache->node);
991 } else {
992 i915_vma_unpin((struct i915_vma *)cache->node.mm);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700993 }
Chris Wilson31a39202016-08-18 17:16:46 +0100994 }
Chris Wilson650bc632017-06-15 09:14:33 +0100995
996 cache->vaddr = 0;
997 cache->page = -1;
Chris Wilson31a39202016-08-18 17:16:46 +0100998}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700999
Chris Wilson31a39202016-08-18 17:16:46 +01001000static void *reloc_kmap(struct drm_i915_gem_object *obj,
1001 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001002 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +01001003{
Chris Wilsond50415c2016-08-18 17:16:52 +01001004 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +01001005
Chris Wilsond50415c2016-08-18 17:16:52 +01001006 if (cache->vaddr) {
1007 kunmap_atomic(unmask_page(cache->vaddr));
1008 } else {
1009 unsigned int flushes;
Chris Wilson2889caa2017-06-16 15:05:19 +01001010 int err;
Chris Wilson31a39202016-08-18 17:16:46 +01001011
Chris Wilson2889caa2017-06-16 15:05:19 +01001012 err = i915_gem_obj_prepare_shmem_write(obj, &flushes);
1013 if (err)
1014 return ERR_PTR(err);
Chris Wilsond50415c2016-08-18 17:16:52 +01001015
1016 BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
1017 BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
1018
1019 cache->vaddr = flushes | KMAP;
1020 cache->node.mm = (void *)obj;
1021 if (flushes)
1022 mb();
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001023 }
1024
Chris Wilsond50415c2016-08-18 17:16:52 +01001025 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
1026 cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +01001027 cache->page = page;
Chris Wilson31a39202016-08-18 17:16:46 +01001028
Chris Wilsond50415c2016-08-18 17:16:52 +01001029 return vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +01001030}
1031
Chris Wilsond50415c2016-08-18 17:16:52 +01001032static void *reloc_iomap(struct drm_i915_gem_object *obj,
Chris Wilson31a39202016-08-18 17:16:46 +01001033 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001034 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +01001035{
Chris Wilson650bc632017-06-15 09:14:33 +01001036 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001037 unsigned long offset;
Chris Wilsond50415c2016-08-18 17:16:52 +01001038 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +01001039
Chris Wilsond50415c2016-08-18 17:16:52 +01001040 if (cache->vaddr) {
Jani Nikula615e5002016-10-04 12:54:13 +03001041 io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
Chris Wilsond50415c2016-08-18 17:16:52 +01001042 } else {
1043 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001044 int err;
Chris Wilson31a39202016-08-18 17:16:46 +01001045
Chris Wilson2889caa2017-06-16 15:05:19 +01001046 if (use_cpu_reloc(cache, obj))
Chris Wilsond50415c2016-08-18 17:16:52 +01001047 return NULL;
Chris Wilson31a39202016-08-18 17:16:46 +01001048
Chris Wilson2889caa2017-06-16 15:05:19 +01001049 err = i915_gem_object_set_to_gtt_domain(obj, true);
1050 if (err)
1051 return ERR_PTR(err);
Chris Wilson31a39202016-08-18 17:16:46 +01001052
Chris Wilsond50415c2016-08-18 17:16:52 +01001053 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1054 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001055 if (IS_ERR(vma)) {
1056 memset(&cache->node, 0, sizeof(cache->node));
Chris Wilson2889caa2017-06-16 15:05:19 +01001057 err = drm_mm_insert_node_in_range
Chris Wilsone8cb9092016-08-18 17:16:53 +01001058 (&ggtt->base.mm, &cache->node,
Chris Wilsonf51455d2017-01-10 14:47:34 +00001059 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
Chris Wilsone8cb9092016-08-18 17:16:53 +01001060 0, ggtt->mappable_end,
Chris Wilson4e64e552017-02-02 21:04:38 +00001061 DRM_MM_INSERT_LOW);
Chris Wilson2889caa2017-06-16 15:05:19 +01001062 if (err) /* no inactive aperture space, use cpu reloc */
Chris Wilsonc92fa4f2016-10-07 07:53:25 +01001063 return NULL;
Chris Wilsone8cb9092016-08-18 17:16:53 +01001064 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +01001065 err = i915_vma_put_fence(vma);
1066 if (err) {
Chris Wilsone8cb9092016-08-18 17:16:53 +01001067 i915_vma_unpin(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001068 return ERR_PTR(err);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001069 }
Rafael Barbalho5032d872013-08-21 17:10:51 +01001070
Chris Wilsone8cb9092016-08-18 17:16:53 +01001071 cache->node.start = vma->node.start;
1072 cache->node.mm = (void *)vma;
Chris Wilsond50415c2016-08-18 17:16:52 +01001073 }
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001074 }
1075
Chris Wilsone8cb9092016-08-18 17:16:53 +01001076 offset = cache->node.start;
1077 if (cache->node.allocated) {
Chris Wilsonfc099092016-10-28 15:27:56 +01001078 wmb();
Chris Wilsone8cb9092016-08-18 17:16:53 +01001079 ggtt->base.insert_page(&ggtt->base,
1080 i915_gem_object_get_dma_address(obj, page),
1081 offset, I915_CACHE_NONE, 0);
1082 } else {
1083 offset += page << PAGE_SHIFT;
1084 }
1085
Chris Wilson650bc632017-06-15 09:14:33 +01001086 vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->mappable,
1087 offset);
Chris Wilsond50415c2016-08-18 17:16:52 +01001088 cache->page = page;
1089 cache->vaddr = (unsigned long)vaddr;
1090
1091 return vaddr;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001092}
1093
Chris Wilsond50415c2016-08-18 17:16:52 +01001094static void *reloc_vaddr(struct drm_i915_gem_object *obj,
1095 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001096 unsigned long page)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001097{
Chris Wilsond50415c2016-08-18 17:16:52 +01001098 void *vaddr;
1099
1100 if (cache->page == page) {
1101 vaddr = unmask_page(cache->vaddr);
1102 } else {
1103 vaddr = NULL;
1104 if ((cache->vaddr & KMAP) == 0)
1105 vaddr = reloc_iomap(obj, cache, page);
1106 if (!vaddr)
1107 vaddr = reloc_kmap(obj, cache, page);
1108 }
1109
1110 return vaddr;
1111}
1112
1113static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
1114{
1115 if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
1116 if (flushes & CLFLUSH_BEFORE) {
1117 clflushopt(addr);
1118 mb();
1119 }
1120
1121 *addr = value;
1122
Chris Wilson2889caa2017-06-16 15:05:19 +01001123 /*
1124 * Writes to the same cacheline are serialised by the CPU
Chris Wilsond50415c2016-08-18 17:16:52 +01001125 * (including clflush). On the write path, we only require
1126 * that it hits memory in an orderly fashion and place
1127 * mb barriers at the start and end of the relocation phase
1128 * to ensure ordering of clflush wrt to the system.
1129 */
1130 if (flushes & CLFLUSH_AFTER)
1131 clflushopt(addr);
1132 } else
1133 *addr = value;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001134}
1135
Chris Wilson7dd4f672017-06-16 15:05:24 +01001136static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
1137 struct i915_vma *vma,
1138 unsigned int len)
1139{
1140 struct reloc_cache *cache = &eb->reloc_cache;
1141 struct drm_i915_gem_object *obj;
1142 struct drm_i915_gem_request *rq;
1143 struct i915_vma *batch;
1144 u32 *cmd;
1145 int err;
1146
1147 GEM_BUG_ON(vma->obj->base.write_domain & I915_GEM_DOMAIN_CPU);
1148
1149 obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, PAGE_SIZE);
1150 if (IS_ERR(obj))
1151 return PTR_ERR(obj);
1152
1153 cmd = i915_gem_object_pin_map(obj,
1154 cache->has_llc ? I915_MAP_WB : I915_MAP_WC);
1155 i915_gem_object_unpin_pages(obj);
1156 if (IS_ERR(cmd))
1157 return PTR_ERR(cmd);
1158
1159 err = i915_gem_object_set_to_wc_domain(obj, false);
1160 if (err)
1161 goto err_unmap;
1162
1163 batch = i915_vma_instance(obj, vma->vm, NULL);
1164 if (IS_ERR(batch)) {
1165 err = PTR_ERR(batch);
1166 goto err_unmap;
1167 }
1168
1169 err = i915_vma_pin(batch, 0, 0, PIN_USER | PIN_NONBLOCK);
1170 if (err)
1171 goto err_unmap;
1172
1173 rq = i915_gem_request_alloc(eb->engine, eb->ctx);
1174 if (IS_ERR(rq)) {
1175 err = PTR_ERR(rq);
1176 goto err_unpin;
1177 }
1178
1179 err = i915_gem_request_await_object(rq, vma->obj, true);
1180 if (err)
1181 goto err_request;
1182
1183 err = eb->engine->emit_flush(rq, EMIT_INVALIDATE);
1184 if (err)
1185 goto err_request;
1186
1187 err = i915_switch_context(rq);
1188 if (err)
1189 goto err_request;
1190
1191 err = eb->engine->emit_bb_start(rq,
1192 batch->node.start, PAGE_SIZE,
1193 cache->gen > 5 ? 0 : I915_DISPATCH_SECURE);
1194 if (err)
1195 goto err_request;
1196
Chris Wilson95ff7c72017-06-16 15:05:25 +01001197 GEM_BUG_ON(!reservation_object_test_signaled_rcu(batch->resv, true));
Chris Wilson7dd4f672017-06-16 15:05:24 +01001198 i915_vma_move_to_active(batch, rq, 0);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001199 reservation_object_lock(batch->resv, NULL);
1200 reservation_object_add_excl_fence(batch->resv, &rq->fence);
1201 reservation_object_unlock(batch->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001202 i915_vma_unpin(batch);
1203
Chris Wilson25ffaa62017-06-20 13:43:20 +01001204 i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001205 reservation_object_lock(vma->resv, NULL);
1206 reservation_object_add_excl_fence(vma->resv, &rq->fence);
1207 reservation_object_unlock(vma->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001208
1209 rq->batch = batch;
1210
1211 cache->rq = rq;
1212 cache->rq_cmd = cmd;
1213 cache->rq_size = 0;
1214
1215 /* Return with batch mapping (cmd) still pinned */
1216 return 0;
1217
1218err_request:
1219 i915_add_request(rq);
1220err_unpin:
1221 i915_vma_unpin(batch);
1222err_unmap:
1223 i915_gem_object_unpin_map(obj);
1224 return err;
1225}
1226
1227static u32 *reloc_gpu(struct i915_execbuffer *eb,
1228 struct i915_vma *vma,
1229 unsigned int len)
1230{
1231 struct reloc_cache *cache = &eb->reloc_cache;
1232 u32 *cmd;
1233
1234 if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1))
1235 reloc_gpu_flush(cache);
1236
1237 if (unlikely(!cache->rq)) {
1238 int err;
1239
1240 err = __reloc_gpu_alloc(eb, vma, len);
1241 if (unlikely(err))
1242 return ERR_PTR(err);
1243 }
1244
1245 cmd = cache->rq_cmd + cache->rq_size;
1246 cache->rq_size += len;
1247
1248 return cmd;
1249}
1250
Chris Wilson2889caa2017-06-16 15:05:19 +01001251static u64
1252relocate_entry(struct i915_vma *vma,
Chris Wilsond50415c2016-08-18 17:16:52 +01001253 const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +01001254 struct i915_execbuffer *eb,
1255 const struct i915_vma *target)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001256{
Chris Wilsond50415c2016-08-18 17:16:52 +01001257 u64 offset = reloc->offset;
Chris Wilson2889caa2017-06-16 15:05:19 +01001258 u64 target_offset = relocation_target(reloc, target);
1259 bool wide = eb->reloc_cache.use_64bit_reloc;
Chris Wilsond50415c2016-08-18 17:16:52 +01001260 void *vaddr;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001261
Chris Wilson7dd4f672017-06-16 15:05:24 +01001262 if (!eb->reloc_cache.vaddr &&
1263 (DBG_FORCE_RELOC == FORCE_GPU_RELOC ||
Chris Wilson95ff7c72017-06-16 15:05:25 +01001264 !reservation_object_test_signaled_rcu(vma->resv, true))) {
Chris Wilson7dd4f672017-06-16 15:05:24 +01001265 const unsigned int gen = eb->reloc_cache.gen;
1266 unsigned int len;
1267 u32 *batch;
1268 u64 addr;
1269
1270 if (wide)
1271 len = offset & 7 ? 8 : 5;
1272 else if (gen >= 4)
1273 len = 4;
1274 else if (gen >= 3)
1275 len = 3;
1276 else /* On gen2 MI_STORE_DWORD_IMM uses a physical address */
1277 goto repeat;
1278
1279 batch = reloc_gpu(eb, vma, len);
1280 if (IS_ERR(batch))
1281 goto repeat;
1282
1283 addr = gen8_canonical_addr(vma->node.start + offset);
1284 if (wide) {
1285 if (offset & 7) {
1286 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1287 *batch++ = lower_32_bits(addr);
1288 *batch++ = upper_32_bits(addr);
1289 *batch++ = lower_32_bits(target_offset);
1290
1291 addr = gen8_canonical_addr(addr + 4);
1292
1293 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1294 *batch++ = lower_32_bits(addr);
1295 *batch++ = upper_32_bits(addr);
1296 *batch++ = upper_32_bits(target_offset);
1297 } else {
1298 *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1;
1299 *batch++ = lower_32_bits(addr);
1300 *batch++ = upper_32_bits(addr);
1301 *batch++ = lower_32_bits(target_offset);
1302 *batch++ = upper_32_bits(target_offset);
1303 }
1304 } else if (gen >= 6) {
1305 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1306 *batch++ = 0;
1307 *batch++ = addr;
1308 *batch++ = target_offset;
1309 } else if (gen >= 4) {
1310 *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1311 *batch++ = 0;
1312 *batch++ = addr;
1313 *batch++ = target_offset;
1314 } else {
1315 *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
1316 *batch++ = addr;
1317 *batch++ = target_offset;
1318 }
1319
1320 goto out;
1321 }
1322
Chris Wilsond50415c2016-08-18 17:16:52 +01001323repeat:
Chris Wilson95ff7c72017-06-16 15:05:25 +01001324 vaddr = reloc_vaddr(vma->obj, &eb->reloc_cache, offset >> PAGE_SHIFT);
Chris Wilsond50415c2016-08-18 17:16:52 +01001325 if (IS_ERR(vaddr))
1326 return PTR_ERR(vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001327
Chris Wilsond50415c2016-08-18 17:16:52 +01001328 clflush_write32(vaddr + offset_in_page(offset),
1329 lower_32_bits(target_offset),
Chris Wilson2889caa2017-06-16 15:05:19 +01001330 eb->reloc_cache.vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001331
Chris Wilsond50415c2016-08-18 17:16:52 +01001332 if (wide) {
1333 offset += sizeof(u32);
1334 target_offset >>= 32;
1335 wide = false;
1336 goto repeat;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001337 }
Chris Wilsondabdfe02012-03-26 10:10:27 +02001338
Chris Wilson7dd4f672017-06-16 15:05:24 +01001339out:
Chris Wilson2889caa2017-06-16 15:05:19 +01001340 return target->node.start | UPDATE;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001341}
1342
Chris Wilson2889caa2017-06-16 15:05:19 +01001343static u64
1344eb_relocate_entry(struct i915_execbuffer *eb,
1345 struct i915_vma *vma,
1346 const struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001347{
Chris Wilson507d9772017-06-16 15:05:17 +01001348 struct i915_vma *target;
Chris Wilson2889caa2017-06-16 15:05:19 +01001349 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001350
Chris Wilson67731b82010-12-08 10:38:14 +00001351 /* we've already hold a reference to all valid objects */
Chris Wilson507d9772017-06-16 15:05:17 +01001352 target = eb_get_vma(eb, reloc->target_handle);
1353 if (unlikely(!target))
Chris Wilson54cf91d2010-11-25 18:00:26 +00001354 return -ENOENT;
Eric Anholte844b992012-07-31 15:35:01 -07001355
Chris Wilson54cf91d2010-11-25 18:00:26 +00001356 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001357 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001358 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001359 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001360 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001361 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001362 (int) reloc->offset,
1363 reloc->read_domains,
1364 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001365 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001366 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +01001367 if (unlikely((reloc->write_domain | reloc->read_domains)
1368 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001369 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001370 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001371 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001372 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001373 (int) reloc->offset,
1374 reloc->read_domains,
1375 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001376 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001377 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001378
Chris Wilson2889caa2017-06-16 15:05:19 +01001379 if (reloc->write_domain) {
Chris Wilson507d9772017-06-16 15:05:17 +01001380 target->exec_entry->flags |= EXEC_OBJECT_WRITE;
1381
Chris Wilson2889caa2017-06-16 15:05:19 +01001382 /*
1383 * Sandybridge PPGTT errata: We need a global gtt mapping
1384 * for MI and pipe_control writes because the gpu doesn't
1385 * properly redirect them through the ppgtt for non_secure
1386 * batchbuffers.
1387 */
1388 if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
1389 IS_GEN6(eb->i915)) {
1390 err = i915_vma_bind(target, target->obj->cache_level,
1391 PIN_GLOBAL);
1392 if (WARN_ONCE(err,
1393 "Unexpected failure to bind target VMA!"))
1394 return err;
1395 }
Chris Wilson507d9772017-06-16 15:05:17 +01001396 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001397
Chris Wilson2889caa2017-06-16 15:05:19 +01001398 /*
1399 * If the relocation already has the right value in it, no
Chris Wilson54cf91d2010-11-25 18:00:26 +00001400 * more work needs to be done.
1401 */
Chris Wilson7dd4f672017-06-16 15:05:24 +01001402 if (!DBG_FORCE_RELOC &&
1403 gen8_canonical_addr(target->node.start) == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +00001404 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001405
1406 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001407 if (unlikely(reloc->offset >
Chris Wilson507d9772017-06-16 15:05:17 +01001408 vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001409 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson507d9772017-06-16 15:05:17 +01001410 "target %d offset %d size %d.\n",
1411 reloc->target_handle,
1412 (int)reloc->offset,
1413 (int)vma->size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001414 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001415 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001416 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001417 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson507d9772017-06-16 15:05:17 +01001418 "target %d offset %d.\n",
1419 reloc->target_handle,
1420 (int)reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001421 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001422 }
1423
Chris Wilson071750e2017-06-16 15:05:18 +01001424 /*
1425 * If we write into the object, we need to force the synchronisation
1426 * barrier, either with an asynchronous clflush or if we executed the
1427 * patching using the GPU (though that should be serialised by the
1428 * timeline). To be completely sure, and since we are required to
1429 * do relocations we are already stalling, disable the user's opt
1430 * of our synchronisation.
1431 */
1432 vma->exec_entry->flags &= ~EXEC_OBJECT_ASYNC;
1433
Chris Wilson54cf91d2010-11-25 18:00:26 +00001434 /* and update the user's relocation entry */
Chris Wilson2889caa2017-06-16 15:05:19 +01001435 return relocate_entry(vma, reloc, eb, target);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001436}
1437
Chris Wilson2889caa2017-06-16 15:05:19 +01001438static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001439{
Chris Wilson1d83f442012-03-24 20:12:53 +00001440#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
Chris Wilson2889caa2017-06-16 15:05:19 +01001441 struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
1442 struct drm_i915_gem_relocation_entry __user *urelocs;
1443 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
1444 unsigned int remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001445
Chris Wilson2889caa2017-06-16 15:05:19 +01001446 urelocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson1d83f442012-03-24 20:12:53 +00001447 remain = entry->relocation_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01001448 if (unlikely(remain > N_RELOC(ULONG_MAX)))
1449 return -EINVAL;
Chris Wilsonebc08082016-10-18 13:02:51 +01001450
Chris Wilson2889caa2017-06-16 15:05:19 +01001451 /*
1452 * We must check that the entire relocation array is safe
1453 * to read. However, if the array is not writable the user loses
1454 * the updated relocation values.
1455 */
1456 if (unlikely(!access_ok(VERIFY_READ, urelocs, remain*sizeof(urelocs))))
1457 return -EFAULT;
Chris Wilson1d83f442012-03-24 20:12:53 +00001458
Chris Wilson2889caa2017-06-16 15:05:19 +01001459 do {
1460 struct drm_i915_gem_relocation_entry *r = stack;
1461 unsigned int count =
1462 min_t(unsigned int, remain, ARRAY_SIZE(stack));
1463 unsigned int copied;
1464
1465 /*
1466 * This is the fast path and we cannot handle a pagefault
Chris Wilsonebc08082016-10-18 13:02:51 +01001467 * whilst holding the struct mutex lest the user pass in the
1468 * relocations contained within a mmaped bo. For in such a case
1469 * we, the page fault handler would call i915_gem_fault() and
1470 * we would try to acquire the struct mutex again. Obviously
1471 * this is bad and so lockdep complains vehemently.
1472 */
1473 pagefault_disable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001474 copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
Chris Wilsonebc08082016-10-18 13:02:51 +01001475 pagefault_enable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001476 if (unlikely(copied)) {
1477 remain = -EFAULT;
Chris Wilson31a39202016-08-18 17:16:46 +01001478 goto out;
1479 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001480
Chris Wilson2889caa2017-06-16 15:05:19 +01001481 remain -= count;
Chris Wilson1d83f442012-03-24 20:12:53 +00001482 do {
Chris Wilson2889caa2017-06-16 15:05:19 +01001483 u64 offset = eb_relocate_entry(eb, vma, r);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001484
Chris Wilson2889caa2017-06-16 15:05:19 +01001485 if (likely(offset == 0)) {
1486 } else if ((s64)offset < 0) {
1487 remain = (int)offset;
Chris Wilson31a39202016-08-18 17:16:46 +01001488 goto out;
Chris Wilson2889caa2017-06-16 15:05:19 +01001489 } else {
1490 /*
1491 * Note that reporting an error now
1492 * leaves everything in an inconsistent
1493 * state as we have *already* changed
1494 * the relocation value inside the
1495 * object. As we have not changed the
1496 * reloc.presumed_offset or will not
1497 * change the execobject.offset, on the
1498 * call we may not rewrite the value
1499 * inside the object, leaving it
1500 * dangling and causing a GPU hang. Unless
1501 * userspace dynamically rebuilds the
1502 * relocations on each execbuf rather than
1503 * presume a static tree.
1504 *
1505 * We did previously check if the relocations
1506 * were writable (access_ok), an error now
1507 * would be a strange race with mprotect,
1508 * having already demonstrated that we
1509 * can read from this userspace address.
1510 */
1511 offset = gen8_canonical_addr(offset & ~UPDATE);
1512 __put_user(offset,
1513 &urelocs[r-stack].presumed_offset);
Chris Wilson1d83f442012-03-24 20:12:53 +00001514 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001515 } while (r++, --count);
1516 urelocs += ARRAY_SIZE(stack);
1517 } while (remain);
Chris Wilson31a39202016-08-18 17:16:46 +01001518out:
Chris Wilson650bc632017-06-15 09:14:33 +01001519 reloc_cache_reset(&eb->reloc_cache);
Chris Wilson2889caa2017-06-16 15:05:19 +01001520 return remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001521}
1522
1523static int
Chris Wilson2889caa2017-06-16 15:05:19 +01001524eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001525{
Ben Widawsky27173f12013-08-14 11:38:36 +02001526 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson2889caa2017-06-16 15:05:19 +01001527 struct drm_i915_gem_relocation_entry *relocs =
1528 u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1529 unsigned int i;
1530 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001531
1532 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001533 u64 offset = eb_relocate_entry(eb, vma, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001534
Chris Wilson2889caa2017-06-16 15:05:19 +01001535 if ((s64)offset < 0) {
1536 err = (int)offset;
1537 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001538 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001539 }
1540 err = 0;
Chris Wilsona415d352013-11-26 11:23:15 +00001541err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001542 reloc_cache_reset(&eb->reloc_cache);
1543 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001544}
1545
Chris Wilson2889caa2017-06-16 15:05:19 +01001546static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001547{
Chris Wilson2889caa2017-06-16 15:05:19 +01001548 const char __user *addr, *end;
1549 unsigned long size;
1550 char __maybe_unused c;
Ben Widawsky27173f12013-08-14 11:38:36 +02001551
Chris Wilson2889caa2017-06-16 15:05:19 +01001552 size = entry->relocation_count;
1553 if (size == 0)
1554 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001555
Chris Wilson2889caa2017-06-16 15:05:19 +01001556 if (size > N_RELOC(ULONG_MAX))
1557 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001558
Chris Wilson2889caa2017-06-16 15:05:19 +01001559 addr = u64_to_user_ptr(entry->relocs_ptr);
1560 size *= sizeof(struct drm_i915_gem_relocation_entry);
1561 if (!access_ok(VERIFY_READ, addr, size))
1562 return -EFAULT;
1563
1564 end = addr + size;
1565 for (; addr < end; addr += PAGE_SIZE) {
1566 int err = __get_user(c, addr);
1567 if (err)
1568 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001569 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001570 return __get_user(c, end - 1);
1571}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001572
Chris Wilson2889caa2017-06-16 15:05:19 +01001573static int eb_copy_relocations(const struct i915_execbuffer *eb)
1574{
1575 const unsigned int count = eb->buffer_count;
1576 unsigned int i;
1577 int err;
1578
Chris Wilson54cf91d2010-11-25 18:00:26 +00001579 for (i = 0; i < count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001580 const unsigned int nreloc = eb->exec[i].relocation_count;
1581 struct drm_i915_gem_relocation_entry __user *urelocs;
1582 struct drm_i915_gem_relocation_entry *relocs;
1583 unsigned long size;
1584 unsigned long copied;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001585
Chris Wilson2889caa2017-06-16 15:05:19 +01001586 if (nreloc == 0)
1587 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001588
Chris Wilson2889caa2017-06-16 15:05:19 +01001589 err = check_relocations(&eb->exec[i]);
1590 if (err)
1591 goto err;
1592
1593 urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
1594 size = nreloc * sizeof(*relocs);
1595
1596 relocs = kvmalloc_array(size, 1, GFP_TEMPORARY);
1597 if (!relocs) {
1598 kvfree(relocs);
1599 err = -ENOMEM;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001600 goto err;
1601 }
1602
Chris Wilson2889caa2017-06-16 15:05:19 +01001603 /* 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,
1610 (char *)urelocs + copied,
1611 len)) {
1612 kvfree(relocs);
1613 err = -EFAULT;
1614 goto err;
1615 }
1616
1617 copied += len;
1618 } while (copied < size);
1619
1620 /*
1621 * As we do not update the known relocation offsets after
Chris Wilson262b6d32013-01-15 16:17:54 +00001622 * relocating (due to the complexities in lock handling),
1623 * we need to mark them as invalid now so that we force the
1624 * relocation processing next time. Just in case the target
1625 * object is evicted and then rebound into its old
1626 * presumed_offset before the next execbuffer - if that
1627 * happened we would make the mistake of assuming that the
1628 * relocations were valid.
1629 */
Chris Wilson2889caa2017-06-16 15:05:19 +01001630 user_access_begin();
1631 for (copied = 0; copied < nreloc; copied++)
1632 unsafe_put_user(-1,
1633 &urelocs[copied].presumed_offset,
1634 end_user);
1635end_user:
1636 user_access_end();
Chris Wilson262b6d32013-01-15 16:17:54 +00001637
Chris Wilson2889caa2017-06-16 15:05:19 +01001638 eb->exec[i].relocs_ptr = (uintptr_t)relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001639 }
1640
Chris Wilson2889caa2017-06-16 15:05:19 +01001641 return 0;
1642
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;
1651}
1652
1653static int eb_prefault_relocations(const struct i915_execbuffer *eb)
1654{
1655 const unsigned int count = eb->buffer_count;
1656 unsigned int i;
1657
1658 if (unlikely(i915.prefault_disable))
1659 return 0;
1660
1661 for (i = 0; i < count; i++) {
1662 int err;
1663
1664 err = check_relocations(&eb->exec[i]);
1665 if (err)
1666 return err;
1667 }
1668
1669 return 0;
1670}
1671
1672static noinline int eb_relocate_slow(struct i915_execbuffer *eb)
1673{
1674 struct drm_device *dev = &eb->i915->drm;
1675 bool have_copy = false;
1676 struct i915_vma *vma;
1677 int err = 0;
1678
1679repeat:
1680 if (signal_pending(current)) {
1681 err = -ERESTARTSYS;
1682 goto out;
1683 }
1684
1685 /* We may process another execbuffer during the unlock... */
1686 eb_reset_vmas(eb);
1687 mutex_unlock(&dev->struct_mutex);
1688
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;
1710 }
1711 if (err) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001712 mutex_lock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01001713 goto out;
1714 }
1715
Chris Wilson8a2421b2017-06-16 15:05:22 +01001716 /* A frequent cause for EAGAIN are currently unavailable client pages */
1717 flush_workqueue(eb->i915->mm.userptr_wq);
1718
Chris Wilson2889caa2017-06-16 15:05:19 +01001719 err = i915_mutex_lock_interruptible(dev);
1720 if (err) {
1721 mutex_lock(&dev->struct_mutex);
1722 goto out;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001723 }
1724
Chris Wilson67731b82010-12-08 10:38:14 +00001725 /* reacquire the objects */
Chris Wilson2889caa2017-06-16 15:05:19 +01001726 err = eb_lookup_vmas(eb);
1727 if (err)
Chris Wilson3b96eff2013-01-08 10:53:14 +00001728 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +00001729
Chris Wilson2889caa2017-06-16 15:05:19 +01001730 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1731 if (!have_copy) {
1732 pagefault_disable();
1733 err = eb_relocate_vma(eb, vma);
1734 pagefault_enable();
1735 if (err)
1736 goto repeat;
1737 } else {
1738 err = eb_relocate_vma_slow(eb, vma);
1739 if (err)
1740 goto err;
1741 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001742 }
1743
Chris Wilson2889caa2017-06-16 15:05:19 +01001744 /*
1745 * Leave the user relocations as are, this is the painfully slow path,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001746 * and we want to avoid the complication of dropping the lock whilst
1747 * having buffers reserved in the aperture and so causing spurious
1748 * ENOSPC for random operations.
1749 */
1750
1751err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001752 if (err == -EAGAIN)
1753 goto repeat;
1754
1755out:
1756 if (have_copy) {
1757 const unsigned int count = eb->buffer_count;
1758 unsigned int i;
1759
1760 for (i = 0; i < count; i++) {
1761 const struct drm_i915_gem_exec_object2 *entry =
1762 &eb->exec[i];
1763 struct drm_i915_gem_relocation_entry *relocs;
1764
1765 if (!entry->relocation_count)
1766 continue;
1767
1768 relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1769 kvfree(relocs);
1770 }
1771 }
1772
1773 return err ?: have_copy;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001774}
1775
Chris Wilson2889caa2017-06-16 15:05:19 +01001776static int eb_relocate(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001777{
Chris Wilson2889caa2017-06-16 15:05:19 +01001778 if (eb_lookup_vmas(eb))
1779 goto slow;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001780
Chris Wilson2889caa2017-06-16 15:05:19 +01001781 /* The objects are in their final locations, apply the relocations. */
1782 if (eb->args->flags & __EXEC_HAS_RELOC) {
1783 struct i915_vma *vma;
1784
1785 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1786 if (eb_relocate_vma(eb, vma))
1787 goto slow;
1788 }
1789 }
1790
1791 return 0;
1792
1793slow:
1794 return eb_relocate_slow(eb);
1795}
1796
Chris Wilson95ff7c72017-06-16 15:05:25 +01001797static void eb_export_fence(struct i915_vma *vma,
Chris Wilson2889caa2017-06-16 15:05:19 +01001798 struct drm_i915_gem_request *req,
1799 unsigned int flags)
1800{
Chris Wilson95ff7c72017-06-16 15:05:25 +01001801 struct reservation_object *resv = vma->resv;
Chris Wilson2889caa2017-06-16 15:05:19 +01001802
1803 /*
1804 * Ignore errors from failing to allocate the new fence, we can't
1805 * handle an error right now. Worst case should be missed
1806 * synchronisation leading to rendering corruption.
1807 */
1808 reservation_object_lock(resv, NULL);
1809 if (flags & EXEC_OBJECT_WRITE)
1810 reservation_object_add_excl_fence(resv, &req->fence);
1811 else if (reservation_object_reserve_shared(resv) == 0)
1812 reservation_object_add_shared_fence(resv, &req->fence);
1813 reservation_object_unlock(resv);
1814}
1815
1816static int eb_move_to_gpu(struct i915_execbuffer *eb)
1817{
1818 const unsigned int count = eb->buffer_count;
1819 unsigned int i;
1820 int err;
1821
1822 for (i = 0; i < count; i++) {
1823 const struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
1824 struct i915_vma *vma = exec_to_vma(entry);
Ben Widawsky27173f12013-08-14 11:38:36 +02001825 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001826
Chris Wilson2889caa2017-06-16 15:05:19 +01001827 if (entry->flags & EXEC_OBJECT_CAPTURE) {
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001828 struct i915_gem_capture_list *capture;
1829
1830 capture = kmalloc(sizeof(*capture), GFP_KERNEL);
1831 if (unlikely(!capture))
1832 return -ENOMEM;
1833
Chris Wilson650bc632017-06-15 09:14:33 +01001834 capture->next = eb->request->capture_list;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001835 capture->vma = vma;
Chris Wilson650bc632017-06-15 09:14:33 +01001836 eb->request->capture_list = capture;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001837 }
1838
Chris Wilson2889caa2017-06-16 15:05:19 +01001839 if (entry->flags & EXEC_OBJECT_ASYNC)
1840 goto skip_flushes;
Chris Wilson77ae9952017-01-27 09:40:07 +00001841
Chris Wilson7fc92e92017-06-16 11:54:55 +01001842 if (unlikely(obj->cache_dirty && !obj->cache_coherent))
Chris Wilson57822dc2017-02-22 11:40:48 +00001843 i915_gem_clflush_object(obj, 0);
Chris Wilson57822dc2017-02-22 11:40:48 +00001844
Chris Wilson2889caa2017-06-16 15:05:19 +01001845 err = i915_gem_request_await_object
1846 (eb->request, obj, entry->flags & EXEC_OBJECT_WRITE);
1847 if (err)
1848 return err;
1849
1850skip_flushes:
1851 i915_vma_move_to_active(vma, eb->request, entry->flags);
1852 __eb_unreserve_vma(vma, entry);
1853 vma->exec_entry = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001854 }
1855
Chris Wilson2889caa2017-06-16 15:05:19 +01001856 for (i = 0; i < count; i++) {
1857 const struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
1858 struct i915_vma *vma = exec_to_vma(entry);
1859
Chris Wilson95ff7c72017-06-16 15:05:25 +01001860 eb_export_fence(vma, eb->request, entry->flags);
Chris Wilsondade2a62017-06-16 15:05:20 +01001861 if (unlikely(entry->flags & __EXEC_OBJECT_HAS_REF))
1862 i915_vma_put(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001863 }
1864 eb->exec = NULL;
1865
Chris Wilsondcd79932016-08-18 17:16:40 +01001866 /* Unconditionally flush any chipset caches (for streaming writes). */
Chris Wilson650bc632017-06-15 09:14:33 +01001867 i915_gem_chipset_flush(eb->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001868
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001869 /* Unconditionally invalidate GPU caches and TLBs. */
Chris Wilson650bc632017-06-15 09:14:33 +01001870 return eb->engine->emit_flush(eb->request, EMIT_INVALIDATE);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001871}
1872
Chris Wilson2889caa2017-06-16 15:05:19 +01001873static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001874{
Chris Wilson650bc632017-06-15 09:14:33 +01001875 if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
Daniel Vettered5982e2013-01-17 22:23:36 +01001876 return false;
1877
Chris Wilson2f5945b2015-10-06 11:39:55 +01001878 /* Kernel clipping was a DRI1 misfeature */
1879 if (exec->num_cliprects || exec->cliprects_ptr)
1880 return false;
1881
1882 if (exec->DR4 == 0xffffffff) {
1883 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1884 exec->DR4 = 0;
1885 }
1886 if (exec->DR1 || exec->DR4)
1887 return false;
1888
1889 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1890 return false;
1891
1892 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001893}
1894
Chris Wilson5cf3d282016-08-04 07:52:43 +01001895void i915_vma_move_to_active(struct i915_vma *vma,
1896 struct drm_i915_gem_request *req,
1897 unsigned int flags)
1898{
1899 struct drm_i915_gem_object *obj = vma->obj;
1900 const unsigned int idx = req->engine->id;
1901
Chris Wilson81147b02016-12-18 15:37:18 +00001902 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001903 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1904
Chris Wilson2889caa2017-06-16 15:05:19 +01001905 /*
1906 * Add a reference if we're newly entering the active list.
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001907 * The order in which we add operations to the retirement queue is
1908 * vital here: mark_active adds to the start of the callback list,
1909 * such that subsequent callbacks are called first. Therefore we
1910 * add the active reference first and queue for it to be dropped
1911 * *last*.
1912 */
Chris Wilsond07f0e52016-10-28 13:58:44 +01001913 if (!i915_vma_is_active(vma))
1914 obj->active_count++;
1915 i915_vma_set_active(vma, idx);
1916 i915_gem_active_set(&vma->last_read[idx], req);
1917 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001918
Chris Wilsone27ab732017-06-15 13:38:49 +01001919 obj->base.write_domain = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001920 if (flags & EXEC_OBJECT_WRITE) {
Chris Wilsone27ab732017-06-15 13:38:49 +01001921 obj->base.write_domain = I915_GEM_DOMAIN_RENDER;
1922
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00001923 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
1924 i915_gem_active_set(&obj->frontbuffer_write, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001925
Chris Wilsone27ab732017-06-15 13:38:49 +01001926 obj->base.read_domains = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001927 }
Chris Wilsone27ab732017-06-15 13:38:49 +01001928 obj->base.read_domains |= I915_GEM_GPU_DOMAINS;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001929
Chris Wilson49ef5292016-08-18 17:17:00 +01001930 if (flags & EXEC_OBJECT_NEEDS_FENCE)
1931 i915_gem_active_set(&vma->last_fence, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001932}
1933
Chris Wilson2889caa2017-06-16 15:05:19 +01001934static int i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001935{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001936 u32 *cs;
1937 int i;
Eric Anholtae662d32012-01-03 09:23:29 -08001938
Chris Wilsonb5321f32016-08-02 22:50:18 +01001939 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001940 DRM_DEBUG("sol reset is gen7/rcs only\n");
1941 return -EINVAL;
1942 }
Eric Anholtae662d32012-01-03 09:23:29 -08001943
Chris Wilson2889caa2017-06-16 15:05:19 +01001944 cs = intel_ring_begin(req, 4 * 2 + 2);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001945 if (IS_ERR(cs))
1946 return PTR_ERR(cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001947
Chris Wilson2889caa2017-06-16 15:05:19 +01001948 *cs++ = MI_LOAD_REGISTER_IMM(4);
Eric Anholtae662d32012-01-03 09:23:29 -08001949 for (i = 0; i < 4; i++) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001950 *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
1951 *cs++ = 0;
Eric Anholtae662d32012-01-03 09:23:29 -08001952 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001953 *cs++ = MI_NOOP;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001954 intel_ring_advance(req, cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001955
1956 return 0;
1957}
1958
Chris Wilson650bc632017-06-15 09:14:33 +01001959static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001960{
Brad Volkin71745372014-12-11 12:13:12 -08001961 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001962 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001963 int err;
Brad Volkin71745372014-12-11 12:13:12 -08001964
Chris Wilson650bc632017-06-15 09:14:33 +01001965 shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool,
1966 PAGE_ALIGN(eb->batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001967 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001968 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001969
Chris Wilson2889caa2017-06-16 15:05:19 +01001970 err = intel_engine_cmd_parser(eb->engine,
Chris Wilson650bc632017-06-15 09:14:33 +01001971 eb->batch->obj,
Chris Wilson33a051a2016-07-27 09:07:26 +01001972 shadow_batch_obj,
Chris Wilson650bc632017-06-15 09:14:33 +01001973 eb->batch_start_offset,
1974 eb->batch_len,
Chris Wilson33a051a2016-07-27 09:07:26 +01001975 is_master);
Chris Wilson2889caa2017-06-16 15:05:19 +01001976 if (err) {
1977 if (err == -EACCES) /* unhandled chained batch */
Chris Wilson058d88c2016-08-15 10:49:06 +01001978 vma = NULL;
1979 else
Chris Wilson2889caa2017-06-16 15:05:19 +01001980 vma = ERR_PTR(err);
Chris Wilson058d88c2016-08-15 10:49:06 +01001981 goto out;
1982 }
Brad Volkin71745372014-12-11 12:13:12 -08001983
Chris Wilson058d88c2016-08-15 10:49:06 +01001984 vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
1985 if (IS_ERR(vma))
1986 goto out;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001987
Chris Wilson650bc632017-06-15 09:14:33 +01001988 vma->exec_entry =
Chris Wilson2889caa2017-06-16 15:05:19 +01001989 memset(&eb->exec[eb->buffer_count++],
1990 0, sizeof(*vma->exec_entry));
Chris Wilsondade2a62017-06-16 15:05:20 +01001991 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF;
Chris Wilson2889caa2017-06-16 15:05:19 +01001992 __exec_to_vma(vma->exec_entry) = (uintptr_t)i915_vma_get(vma);
Brad Volkin71745372014-12-11 12:13:12 -08001993
Chris Wilson058d88c2016-08-15 10:49:06 +01001994out:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001995 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson058d88c2016-08-15 10:49:06 +01001996 return vma;
Brad Volkin71745372014-12-11 12:13:12 -08001997}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001998
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001999static void
Chris Wilson2889caa2017-06-16 15:05:19 +01002000add_to_client(struct drm_i915_gem_request *req, struct drm_file *file)
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002001{
2002 req->file_priv = file->driver_priv;
2003 list_add_tail(&req->client_link, &req->file_priv->mm.request_list);
2004}
2005
Chris Wilson2889caa2017-06-16 15:05:19 +01002006static int eb_submit(struct i915_execbuffer *eb)
Oscar Mateo78382592014-07-03 16:28:05 +01002007{
Chris Wilson2889caa2017-06-16 15:05:19 +01002008 int err;
Oscar Mateo78382592014-07-03 16:28:05 +01002009
Chris Wilson2889caa2017-06-16 15:05:19 +01002010 err = eb_move_to_gpu(eb);
2011 if (err)
2012 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01002013
Chris Wilson2889caa2017-06-16 15:05:19 +01002014 err = i915_switch_context(eb->request);
2015 if (err)
2016 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01002017
Chris Wilson650bc632017-06-15 09:14:33 +01002018 if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002019 err = i915_reset_gen7_sol_offsets(eb->request);
2020 if (err)
2021 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01002022 }
2023
Chris Wilson2889caa2017-06-16 15:05:19 +01002024 err = eb->engine->emit_bb_start(eb->request,
Chris Wilson650bc632017-06-15 09:14:33 +01002025 eb->batch->node.start +
2026 eb->batch_start_offset,
2027 eb->batch_len,
Chris Wilson2889caa2017-06-16 15:05:19 +01002028 eb->batch_flags);
2029 if (err)
2030 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01002031
Chris Wilson2f5945b2015-10-06 11:39:55 +01002032 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01002033}
2034
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002035/**
2036 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01002037 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002038 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002039static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01002040gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
2041 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002042{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002043 struct drm_i915_file_private *file_priv = file->driver_priv;
2044
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002045 /* Check whether the file_priv has already selected one ring. */
Joonas Lahtinen6f633402016-09-01 14:58:21 +03002046 if ((int)file_priv->bsd_engine < 0)
2047 file_priv->bsd_engine = atomic_fetch_xor(1,
2048 &dev_priv->mm.bsd_engine_dispatch_index);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002049
Chris Wilsonc80ff162016-07-27 09:07:27 +01002050 return file_priv->bsd_engine;
Chris Wilsond23db882014-05-23 08:48:08 +02002051}
2052
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002053#define I915_USER_RINGS (4)
2054
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002055static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002056 [I915_EXEC_DEFAULT] = RCS,
2057 [I915_EXEC_RENDER] = RCS,
2058 [I915_EXEC_BLT] = BCS,
2059 [I915_EXEC_BSD] = VCS,
2060 [I915_EXEC_VEBOX] = VECS
2061};
2062
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002063static struct intel_engine_cs *
2064eb_select_engine(struct drm_i915_private *dev_priv,
2065 struct drm_file *file,
2066 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002067{
2068 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002069 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002070
2071 if (user_ring_id > I915_USER_RINGS) {
2072 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002073 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002074 }
2075
2076 if ((user_ring_id != I915_EXEC_BSD) &&
2077 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
2078 DRM_DEBUG("execbuf with non bsd ring but with invalid "
2079 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002080 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002081 }
2082
2083 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
2084 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
2085
2086 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01002087 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002088 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
2089 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00002090 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002091 bsd_idx--;
2092 } else {
2093 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
2094 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002095 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002096 }
2097
Akash Goel3b3f1652016-10-13 22:44:48 +05302098 engine = dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002099 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +05302100 engine = dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002101 }
2102
Akash Goel3b3f1652016-10-13 22:44:48 +05302103 if (!engine) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002104 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002105 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002106 }
2107
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002108 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002109}
2110
Eric Anholtae662d32012-01-03 09:23:29 -08002111static int
Chris Wilson650bc632017-06-15 09:14:33 +01002112i915_gem_do_execbuffer(struct drm_device *dev,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002113 struct drm_file *file,
2114 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08002115 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002116{
Chris Wilson650bc632017-06-15 09:14:33 +01002117 struct i915_execbuffer eb;
Chris Wilsonfec04452017-01-27 09:40:08 +00002118 struct dma_fence *in_fence = NULL;
2119 struct sync_file *out_fence = NULL;
2120 int out_fence_fd = -1;
Chris Wilson2889caa2017-06-16 15:05:19 +01002121 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002122
Chris Wilson2889caa2017-06-16 15:05:19 +01002123 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
2124 ~__EXEC_OBJECT_UNKNOWN_FLAGS);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002125
Chris Wilson650bc632017-06-15 09:14:33 +01002126 eb.i915 = to_i915(dev);
2127 eb.file = file;
2128 eb.args = args;
Chris Wilson7dd4f672017-06-16 15:05:24 +01002129 if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC))
Chris Wilson2889caa2017-06-16 15:05:19 +01002130 args->flags |= __EXEC_HAS_RELOC;
Chris Wilson650bc632017-06-15 09:14:33 +01002131 eb.exec = exec;
Chris Wilson2889caa2017-06-16 15:05:19 +01002132 eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
2133 if (USES_FULL_PPGTT(eb.i915))
2134 eb.invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson650bc632017-06-15 09:14:33 +01002135 reloc_cache_init(&eb.reloc_cache, eb.i915);
2136
Chris Wilson2889caa2017-06-16 15:05:19 +01002137 eb.buffer_count = args->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +01002138 eb.batch_start_offset = args->batch_start_offset;
2139 eb.batch_len = args->batch_len;
2140
Chris Wilson2889caa2017-06-16 15:05:19 +01002141 eb.batch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002142 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02002143 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002144 return -EPERM;
2145
Chris Wilson2889caa2017-06-16 15:05:19 +01002146 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002147 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01002148 if (args->flags & I915_EXEC_IS_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +01002149 eb.batch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002150
Chris Wilson650bc632017-06-15 09:14:33 +01002151 eb.engine = eb_select_engine(eb.i915, file, args);
2152 if (!eb.engine)
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002153 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002154
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002155 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
Chris Wilson650bc632017-06-15 09:14:33 +01002156 if (!HAS_RESOURCE_STREAMER(eb.i915)) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002157 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
2158 return -EINVAL;
2159 }
Chris Wilson650bc632017-06-15 09:14:33 +01002160 if (eb.engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002161 DRM_DEBUG("RS is not available on %s\n",
Chris Wilson650bc632017-06-15 09:14:33 +01002162 eb.engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002163 return -EINVAL;
2164 }
2165
Chris Wilson2889caa2017-06-16 15:05:19 +01002166 eb.batch_flags |= I915_DISPATCH_RS;
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002167 }
2168
Chris Wilsonfec04452017-01-27 09:40:08 +00002169 if (args->flags & I915_EXEC_FENCE_IN) {
2170 in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002171 if (!in_fence)
2172 return -EINVAL;
Chris Wilsonfec04452017-01-27 09:40:08 +00002173 }
2174
2175 if (args->flags & I915_EXEC_FENCE_OUT) {
2176 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
2177 if (out_fence_fd < 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002178 err = out_fence_fd;
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002179 goto err_in_fence;
Chris Wilsonfec04452017-01-27 09:40:08 +00002180 }
2181 }
2182
Chris Wilson2889caa2017-06-16 15:05:19 +01002183 if (eb_create(&eb))
2184 return -ENOMEM;
2185
Chris Wilson1acfc102017-06-20 12:05:47 +01002186 err = eb_select_context(&eb);
2187 if (unlikely(err))
2188 goto err_destroy;
2189
Chris Wilson2889caa2017-06-16 15:05:19 +01002190 /*
2191 * Take a local wakeref for preparing to dispatch the execbuf as
Chris Wilson67d97da2016-07-04 08:08:31 +01002192 * we expect to access the hardware fairly frequently in the
2193 * process. Upon first dispatch, we acquire another prolonged
2194 * wakeref that we hold until the GPU has been idle for at least
2195 * 100ms.
2196 */
Chris Wilson650bc632017-06-15 09:14:33 +01002197 intel_runtime_pm_get(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002198
Chris Wilson2889caa2017-06-16 15:05:19 +01002199 err = i915_mutex_lock_interruptible(dev);
2200 if (err)
2201 goto err_rpm;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002202
Chris Wilson2889caa2017-06-16 15:05:19 +01002203 err = eb_relocate(&eb);
2204 if (err)
2205 /*
2206 * If the user expects the execobject.offset and
2207 * reloc.presumed_offset to be an exact match,
2208 * as for using NO_RELOC, then we cannot update
2209 * the execobject.offset until we have completed
2210 * relocation.
2211 */
2212 args->flags &= ~__EXEC_HAS_RELOC;
2213 if (err < 0)
2214 goto err_vma;
Ben Widawsky41bde552013-12-06 14:11:21 -08002215
Chris Wilson2889caa2017-06-16 15:05:19 +01002216 if (unlikely(eb.batch->exec_entry->flags & EXEC_OBJECT_WRITE)) {
Daniel Vetterff240192012-01-31 21:08:14 +01002217 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002218 err = -EINVAL;
2219 goto err_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002220 }
Chris Wilson650bc632017-06-15 09:14:33 +01002221 if (eb.batch_start_offset > eb.batch->size ||
2222 eb.batch_len > eb.batch->size - eb.batch_start_offset) {
Chris Wilson0b537272016-08-18 17:17:12 +01002223 DRM_DEBUG("Attempting to use out-of-bounds batch\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002224 err = -EINVAL;
2225 goto err_vma;
Chris Wilson0b537272016-08-18 17:17:12 +01002226 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002227
Chris Wilson650bc632017-06-15 09:14:33 +01002228 if (eb.engine->needs_cmd_parser && eb.batch_len) {
Chris Wilson59bfa122016-08-04 16:32:31 +01002229 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002230
Chris Wilson650bc632017-06-15 09:14:33 +01002231 vma = eb_parse(&eb, drm_is_current_master(file));
Chris Wilson59bfa122016-08-04 16:32:31 +01002232 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002233 err = PTR_ERR(vma);
2234 goto err_vma;
Brad Volkin78a42372014-12-11 12:13:09 -08002235 }
Chris Wilson17cabf52015-01-14 11:20:57 +00002236
Chris Wilson59bfa122016-08-04 16:32:31 +01002237 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002238 /*
2239 * Batch parsed and accepted:
2240 *
2241 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
2242 * bit from MI_BATCH_BUFFER_START commands issued in
2243 * the dispatch_execbuffer implementations. We
2244 * specifically don't want that set on batches the
2245 * command parser has accepted.
2246 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002247 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilson650bc632017-06-15 09:14:33 +01002248 eb.batch_start_offset = 0;
2249 eb.batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002250 }
Brad Volkin351e3db2014-02-18 10:15:46 -08002251 }
2252
Chris Wilson650bc632017-06-15 09:14:33 +01002253 if (eb.batch_len == 0)
2254 eb.batch_len = eb.batch->size - eb.batch_start_offset;
Brad Volkin78a42372014-12-11 12:13:09 -08002255
Chris Wilson2889caa2017-06-16 15:05:19 +01002256 /*
2257 * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002258 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07002259 * hsw should have this fixed, but bdw mucks it up again. */
Chris Wilson2889caa2017-06-16 15:05:19 +01002260 if (eb.batch_flags & I915_DISPATCH_SECURE) {
Chris Wilson058d88c2016-08-15 10:49:06 +01002261 struct i915_vma *vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002262
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002263 /*
2264 * So on first glance it looks freaky that we pin the batch here
2265 * outside of the reservation loop. But:
2266 * - The batch is already pinned into the relevant ppgtt, so we
2267 * already have the backing storage fully allocated.
2268 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01002269 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002270 * fitting due to fragmentation.
2271 * So this is actually safe.
2272 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002273 vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0);
Chris Wilson058d88c2016-08-15 10:49:06 +01002274 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002275 err = PTR_ERR(vma);
2276 goto err_vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01002277 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002278
Chris Wilson650bc632017-06-15 09:14:33 +01002279 eb.batch = vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002280 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002281
Chris Wilson7dd4f672017-06-16 15:05:24 +01002282 /* All GPU relocation batches must be submitted prior to the user rq */
2283 GEM_BUG_ON(eb.reloc_cache.rq);
2284
John Harrison0c8dac82015-05-29 17:43:25 +01002285 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson650bc632017-06-15 09:14:33 +01002286 eb.request = i915_gem_request_alloc(eb.engine, eb.ctx);
2287 if (IS_ERR(eb.request)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002288 err = PTR_ERR(eb.request);
John Harrison0c8dac82015-05-29 17:43:25 +01002289 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00002290 }
John Harrison0c8dac82015-05-29 17:43:25 +01002291
Chris Wilsonfec04452017-01-27 09:40:08 +00002292 if (in_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002293 err = i915_gem_request_await_dma_fence(eb.request, in_fence);
2294 if (err < 0)
Chris Wilsonfec04452017-01-27 09:40:08 +00002295 goto err_request;
2296 }
2297
2298 if (out_fence_fd != -1) {
Chris Wilson650bc632017-06-15 09:14:33 +01002299 out_fence = sync_file_create(&eb.request->fence);
Chris Wilsonfec04452017-01-27 09:40:08 +00002300 if (!out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002301 err = -ENOMEM;
Chris Wilsonfec04452017-01-27 09:40:08 +00002302 goto err_request;
2303 }
2304 }
2305
Chris Wilson2889caa2017-06-16 15:05:19 +01002306 /*
2307 * Whilst this request exists, batch_obj will be on the
Chris Wilson17f298cf2016-08-10 13:41:46 +01002308 * active_list, and so will hold the active reference. Only when this
2309 * request is retired will the the batch_obj be moved onto the
2310 * inactive_list and lose its active reference. Hence we do not need
2311 * to explicitly hold another reference here.
2312 */
Chris Wilson650bc632017-06-15 09:14:33 +01002313 eb.request->batch = eb.batch;
Chris Wilson17f298cf2016-08-10 13:41:46 +01002314
Chris Wilson2889caa2017-06-16 15:05:19 +01002315 trace_i915_gem_request_queue(eb.request, eb.batch_flags);
2316 err = eb_submit(&eb);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01002317err_request:
Chris Wilson2889caa2017-06-16 15:05:19 +01002318 __i915_add_request(eb.request, err == 0);
Chris Wilson650bc632017-06-15 09:14:33 +01002319 add_to_client(eb.request, file);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002320
Chris Wilsonfec04452017-01-27 09:40:08 +00002321 if (out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002322 if (err == 0) {
Chris Wilsonfec04452017-01-27 09:40:08 +00002323 fd_install(out_fence_fd, out_fence->file);
2324 args->rsvd2 &= GENMASK_ULL(0, 31); /* keep in-fence */
2325 args->rsvd2 |= (u64)out_fence_fd << 32;
2326 out_fence_fd = -1;
2327 } else {
2328 fput(out_fence->file);
2329 }
2330 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002331
John Harrison0c8dac82015-05-29 17:43:25 +01002332err_batch_unpin:
Chris Wilson2889caa2017-06-16 15:05:19 +01002333 if (eb.batch_flags & I915_DISPATCH_SECURE)
Chris Wilson650bc632017-06-15 09:14:33 +01002334 i915_vma_unpin(eb.batch);
Chris Wilson2889caa2017-06-16 15:05:19 +01002335err_vma:
2336 if (eb.exec)
2337 eb_release_vmas(&eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002338 mutex_unlock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01002339err_rpm:
Chris Wilson650bc632017-06-15 09:14:33 +01002340 intel_runtime_pm_put(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002341 i915_gem_context_put(eb.ctx);
2342err_destroy:
Chris Wilson2889caa2017-06-16 15:05:19 +01002343 eb_destroy(&eb);
Chris Wilsonfec04452017-01-27 09:40:08 +00002344 if (out_fence_fd != -1)
2345 put_unused_fd(out_fence_fd);
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002346err_in_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002347 dma_fence_put(in_fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01002348 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002349}
2350
2351/*
2352 * Legacy execbuffer just creates an exec2 list from the original exec object
2353 * list array and passes it to the real function.
2354 */
2355int
2356i915_gem_execbuffer(struct drm_device *dev, void *data,
2357 struct drm_file *file)
2358{
Chris Wilson2889caa2017-06-16 15:05:19 +01002359 const size_t sz = sizeof(struct drm_i915_gem_exec_object2);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002360 struct drm_i915_gem_execbuffer *args = data;
2361 struct drm_i915_gem_execbuffer2 exec2;
2362 struct drm_i915_gem_exec_object *exec_list = NULL;
2363 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +01002364 unsigned int i;
2365 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002366
Chris Wilson2889caa2017-06-16 15:05:19 +01002367 if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) {
2368 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002369 return -EINVAL;
2370 }
2371
Chris Wilson2889caa2017-06-16 15:05:19 +01002372 exec2.buffers_ptr = args->buffers_ptr;
2373 exec2.buffer_count = args->buffer_count;
2374 exec2.batch_start_offset = args->batch_start_offset;
2375 exec2.batch_len = args->batch_len;
2376 exec2.DR1 = args->DR1;
2377 exec2.DR4 = args->DR4;
2378 exec2.num_cliprects = args->num_cliprects;
2379 exec2.cliprects_ptr = args->cliprects_ptr;
2380 exec2.flags = I915_EXEC_RENDER;
2381 i915_execbuffer2_set_context_id(exec2, 0);
2382
2383 if (!i915_gem_check_execbuffer(&exec2))
2384 return -EINVAL;
2385
Chris Wilson54cf91d2010-11-25 18:00:26 +00002386 /* Copy in the exec list from userland */
Chris Wilson2889caa2017-06-16 15:05:19 +01002387 exec_list = kvmalloc_array(args->buffer_count, sizeof(*exec_list),
2388 __GFP_NOWARN | GFP_TEMPORARY);
2389 exec2_list = kvmalloc_array(args->buffer_count + 1, sz,
2390 __GFP_NOWARN | GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002391 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002392 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002393 args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002394 kvfree(exec_list);
2395 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002396 return -ENOMEM;
2397 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002398 err = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002399 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00002400 sizeof(*exec_list) * args->buffer_count);
Chris Wilson2889caa2017-06-16 15:05:19 +01002401 if (err) {
Daniel Vetterff240192012-01-31 21:08:14 +01002402 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson2889caa2017-06-16 15:05:19 +01002403 args->buffer_count, err);
Michal Hocko20981052017-05-17 14:23:12 +02002404 kvfree(exec_list);
2405 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002406 return -EFAULT;
2407 }
2408
2409 for (i = 0; i < args->buffer_count; i++) {
2410 exec2_list[i].handle = exec_list[i].handle;
2411 exec2_list[i].relocation_count = exec_list[i].relocation_count;
2412 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
2413 exec2_list[i].alignment = exec_list[i].alignment;
2414 exec2_list[i].offset = exec_list[i].offset;
Tvrtko Ursulinf0836b72016-11-16 08:55:32 +00002415 if (INTEL_GEN(to_i915(dev)) < 4)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002416 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
2417 else
2418 exec2_list[i].flags = 0;
2419 }
2420
Chris Wilson2889caa2017-06-16 15:05:19 +01002421 err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list);
2422 if (exec2.flags & __EXEC_HAS_RELOC) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002423 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002424 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002425
Chris Wilson54cf91d2010-11-25 18:00:26 +00002426 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002427 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002428 if (!(exec2_list[i].offset & UPDATE))
2429 continue;
2430
Michał Winiarski934acce2015-12-29 18:24:52 +01002431 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002432 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2433 exec2_list[i].offset &= PIN_OFFSET_MASK;
2434 if (__copy_to_user(&user_exec_list[i].offset,
2435 &exec2_list[i].offset,
2436 sizeof(user_exec_list[i].offset)))
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002437 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002438 }
2439 }
2440
Michal Hocko20981052017-05-17 14:23:12 +02002441 kvfree(exec_list);
2442 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002443 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002444}
2445
2446int
2447i915_gem_execbuffer2(struct drm_device *dev, void *data,
2448 struct drm_file *file)
2449{
Chris Wilson2889caa2017-06-16 15:05:19 +01002450 const size_t sz = sizeof(struct drm_i915_gem_exec_object2);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002451 struct drm_i915_gem_execbuffer2 *args = data;
Chris Wilson2889caa2017-06-16 15:05:19 +01002452 struct drm_i915_gem_exec_object2 *exec2_list;
2453 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002454
Chris Wilson2889caa2017-06-16 15:05:19 +01002455 if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01002456 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002457 return -EINVAL;
2458 }
2459
Chris Wilson2889caa2017-06-16 15:05:19 +01002460 if (!i915_gem_check_execbuffer(args))
2461 return -EINVAL;
2462
2463 /* Allocate an extra slot for use by the command parser */
2464 exec2_list = kvmalloc_array(args->buffer_count + 1, sz,
2465 __GFP_NOWARN | GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002466 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002467 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002468 args->buffer_count);
2469 return -ENOMEM;
2470 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002471 if (copy_from_user(exec2_list,
2472 u64_to_user_ptr(args->buffers_ptr),
2473 sizeof(*exec2_list) * args->buffer_count)) {
2474 DRM_DEBUG("copy %d exec entries failed\n", args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002475 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002476 return -EFAULT;
2477 }
2478
Chris Wilson2889caa2017-06-16 15:05:19 +01002479 err = i915_gem_do_execbuffer(dev, file, args, exec2_list);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002480
Chris Wilson2889caa2017-06-16 15:05:19 +01002481 /*
2482 * Now that we have begun execution of the batchbuffer, we ignore
2483 * any new error after this point. Also given that we have already
2484 * updated the associated relocations, we try to write out the current
2485 * object locations irrespective of any error.
2486 */
2487 if (args->flags & __EXEC_HAS_RELOC) {
2488 struct drm_i915_gem_exec_object2 __user *user_exec_list =
2489 u64_to_user_ptr(args->buffers_ptr);
2490 unsigned int i;
2491
2492 /* Copy the new buffer offsets back to the user's exec list. */
2493 user_access_begin();
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002494 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002495 if (!(exec2_list[i].offset & UPDATE))
2496 continue;
2497
Michał Winiarski934acce2015-12-29 18:24:52 +01002498 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002499 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2500 unsafe_put_user(exec2_list[i].offset,
2501 &user_exec_list[i].offset,
2502 end_user);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002503 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002504end_user:
2505 user_access_end();
Chris Wilson54cf91d2010-11-25 18:00:26 +00002506 }
2507
Chris Wilson2889caa2017-06-16 15:05:19 +01002508 args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
Michal Hocko20981052017-05-17 14:23:12 +02002509 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002510 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002511}