blob: e262133a7cf571cbdd3d5a5eee57b675d1b1b6ad [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 Wilsond50415c2016-08-18 17:16:52 +010043#define DBG_USE_CPU_RELOC 0 /* -1 force GTT relocs; 1 force CPU relocs */
44
Chris Wilsondade2a62017-06-16 15:05:20 +010045#define __EXEC_OBJECT_HAS_REF BIT(31)
46#define __EXEC_OBJECT_HAS_PIN BIT(30)
47#define __EXEC_OBJECT_HAS_FENCE BIT(29)
48#define __EXEC_OBJECT_NEEDS_MAP BIT(28)
49#define __EXEC_OBJECT_NEEDS_BIAS BIT(27)
50#define __EXEC_OBJECT_INTERNAL_FLAGS (~0u << 27) /* all of the above */
Chris Wilson2889caa2017-06-16 15:05:19 +010051#define __EXEC_OBJECT_RESERVED (__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_FENCE)
52
53#define __EXEC_HAS_RELOC BIT(31)
54#define __EXEC_VALIDATED BIT(30)
55#define UPDATE PIN_OFFSET_FIXED
Chris Wilsond23db882014-05-23 08:48:08 +020056
57#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000058
Chris Wilson650bc632017-06-15 09:14:33 +010059#define __I915_EXEC_ILLEGAL_FLAGS \
60 (__I915_EXEC_UNKNOWN_FLAGS | I915_EXEC_CONSTANTS_MASK)
Chris Wilson5b043f42016-08-02 22:50:38 +010061
Chris Wilson2889caa2017-06-16 15:05:19 +010062/**
63 * DOC: User command execution
64 *
65 * Userspace submits commands to be executed on the GPU as an instruction
66 * stream within a GEM object we call a batchbuffer. This instructions may
67 * refer to other GEM objects containing auxiliary state such as kernels,
68 * samplers, render targets and even secondary batchbuffers. Userspace does
69 * not know where in the GPU memory these objects reside and so before the
70 * batchbuffer is passed to the GPU for execution, those addresses in the
71 * batchbuffer and auxiliary objects are updated. This is known as relocation,
72 * or patching. To try and avoid having to relocate each object on the next
73 * execution, userspace is told the location of those objects in this pass,
74 * but this remains just a hint as the kernel may choose a new location for
75 * any object in the future.
76 *
77 * Processing an execbuf ioctl is conceptually split up into a few phases.
78 *
79 * 1. Validation - Ensure all the pointers, handles and flags are valid.
80 * 2. Reservation - Assign GPU address space for every object
81 * 3. Relocation - Update any addresses to point to the final locations
82 * 4. Serialisation - Order the request with respect to its dependencies
83 * 5. Construction - Construct a request to execute the batchbuffer
84 * 6. Submission (at some point in the future execution)
85 *
86 * Reserving resources for the execbuf is the most complicated phase. We
87 * neither want to have to migrate the object in the address space, nor do
88 * we want to have to update any relocations pointing to this object. Ideally,
89 * we want to leave the object where it is and for all the existing relocations
90 * to match. If the object is given a new address, or if userspace thinks the
91 * object is elsewhere, we have to parse all the relocation entries and update
92 * the addresses. Userspace can set the I915_EXEC_NORELOC flag to hint that
93 * all the target addresses in all of its objects match the value in the
94 * relocation entries and that they all match the presumed offsets given by the
95 * list of execbuffer objects. Using this knowledge, we know that if we haven't
96 * moved any buffers, all the relocation entries are valid and we can skip
97 * the update. (If userspace is wrong, the likely outcome is an impromptu GPU
98 * hang.) The requirement for using I915_EXEC_NO_RELOC are:
99 *
100 * The addresses written in the objects must match the corresponding
101 * reloc.presumed_offset which in turn must match the corresponding
102 * execobject.offset.
103 *
104 * Any render targets written to in the batch must be flagged with
105 * EXEC_OBJECT_WRITE.
106 *
107 * To avoid stalling, execobject.offset should match the current
108 * address of that object within the active context.
109 *
110 * The reservation is done is multiple phases. First we try and keep any
111 * object already bound in its current location - so as long as meets the
112 * constraints imposed by the new execbuffer. Any object left unbound after the
113 * first pass is then fitted into any available idle space. If an object does
114 * not fit, all objects are removed from the reservation and the process rerun
115 * after sorting the objects into a priority order (more difficult to fit
116 * objects are tried first). Failing that, the entire VM is cleared and we try
117 * to fit the execbuf once last time before concluding that it simply will not
118 * fit.
119 *
120 * A small complication to all of this is that we allow userspace not only to
121 * specify an alignment and a size for the object in the address space, but
122 * we also allow userspace to specify the exact offset. This objects are
123 * simpler to place (the location is known a priori) all we have to do is make
124 * sure the space is available.
125 *
126 * Once all the objects are in place, patching up the buried pointers to point
127 * to the final locations is a fairly simple job of walking over the relocation
128 * entry arrays, looking up the right address and rewriting the value into
129 * the object. Simple! ... The relocation entries are stored in user memory
130 * and so to access them we have to copy them into a local buffer. That copy
131 * has to avoid taking any pagefaults as they may lead back to a GEM object
132 * requiring the struct_mutex (i.e. recursive deadlock). So once again we split
133 * the relocation into multiple passes. First we try to do everything within an
134 * atomic context (avoid the pagefaults) which requires that we never wait. If
135 * we detect that we may wait, or if we need to fault, then we have to fallback
136 * to a slower path. The slowpath has to drop the mutex. (Can you hear alarm
137 * bells yet?) Dropping the mutex means that we lose all the state we have
138 * built up so far for the execbuf and we must reset any global data. However,
139 * we do leave the objects pinned in their final locations - which is a
140 * potential issue for concurrent execbufs. Once we have left the mutex, we can
141 * allocate and copy all the relocation entries into a large array at our
142 * leisure, reacquire the mutex, reclaim all the objects and other state and
143 * then proceed to update any incorrect addresses with the objects.
144 *
145 * As we process the relocation entries, we maintain a record of whether the
146 * object is being written to. Using NORELOC, we expect userspace to provide
147 * this information instead. We also check whether we can skip the relocation
148 * by comparing the expected value inside the relocation entry with the target's
149 * final address. If they differ, we have to map the current object and rewrite
150 * the 4 or 8 byte pointer within.
151 *
152 * Serialising an execbuf is quite simple according to the rules of the GEM
153 * ABI. Execution within each context is ordered by the order of submission.
154 * Writes to any GEM object are in order of submission and are exclusive. Reads
155 * from a GEM object are unordered with respect to other reads, but ordered by
156 * writes. A write submitted after a read cannot occur before the read, and
157 * similarly any read submitted after a write cannot occur before the write.
158 * Writes are ordered between engines such that only one write occurs at any
159 * time (completing any reads beforehand) - using semaphores where available
160 * and CPU serialisation otherwise. Other GEM access obey the same rules, any
161 * write (either via mmaps using set-domain, or via pwrite) must flush all GPU
162 * reads before starting, and any read (either using set-domain or pread) must
163 * flush all GPU writes before starting. (Note we only employ a barrier before,
164 * we currently rely on userspace not concurrently starting a new execution
165 * whilst reading or writing to an object. This may be an advantage or not
166 * depending on how much you trust userspace not to shoot themselves in the
167 * foot.) Serialisation may just result in the request being inserted into
168 * a DAG awaiting its turn, but most simple is to wait on the CPU until
169 * all dependencies are resolved.
170 *
171 * After all of that, is just a matter of closing the request and handing it to
172 * the hardware (well, leaving it in a queue to be executed). However, we also
173 * offer the ability for batchbuffers to be run with elevated privileges so
174 * that they access otherwise hidden registers. (Used to adjust L3 cache etc.)
175 * Before any batch is given extra privileges we first must check that it
176 * contains no nefarious instructions, we check that each instruction is from
177 * our whitelist and all registers are also from an allowed list. We first
178 * copy the user's batchbuffer to a shadow (so that the user doesn't have
179 * access to it, either by the CPU or GPU as we scan it) and then parse each
180 * instruction. If everything is ok, we set a flag telling the hardware to run
181 * the batchbuffer in trusted mode, otherwise the ioctl is rejected.
182 */
183
Chris Wilson650bc632017-06-15 09:14:33 +0100184struct i915_execbuffer {
Chris Wilson2889caa2017-06-16 15:05:19 +0100185 struct drm_i915_private *i915; /** i915 backpointer */
186 struct drm_file *file; /** per-file lookup tables and limits */
187 struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */
188 struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */
189
190 struct intel_engine_cs *engine; /** engine to queue the request to */
191 struct i915_gem_context *ctx; /** context for building the request */
192 struct i915_address_space *vm; /** GTT and vma for the request */
193
194 struct drm_i915_gem_request *request; /** our request to build */
195 struct i915_vma *batch; /** identity of the batch obj/vma */
196
197 /** actual size of execobj[] as we may extend it for the cmdparser */
198 unsigned int buffer_count;
199
200 /** list of vma not yet bound during reservation phase */
201 struct list_head unbound;
202
203 /** list of vma that have execobj.relocation_count */
204 struct list_head relocs;
205
206 /**
207 * Track the most recently used object for relocations, as we
208 * frequently have to perform multiple relocations within the same
209 * obj/page
210 */
Chris Wilson650bc632017-06-15 09:14:33 +0100211 struct reloc_cache {
Chris Wilson2889caa2017-06-16 15:05:19 +0100212 struct drm_mm_node node; /** temporary GTT binding */
213 unsigned long vaddr; /** Current kmap address */
214 unsigned long page; /** Currently mapped page index */
Chris Wilson650bc632017-06-15 09:14:33 +0100215 bool use_64bit_reloc : 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100216 bool has_llc : 1;
217 bool has_fence : 1;
218 bool needs_unfenced : 1;
Chris Wilson650bc632017-06-15 09:14:33 +0100219 } reloc_cache;
Chris Wilson2889caa2017-06-16 15:05:19 +0100220
221 u64 invalid_flags; /** Set of execobj.flags that are invalid */
222 u32 context_flags; /** Set of execobj.flags to insert from the ctx */
223
224 u32 batch_start_offset; /** Location within object of batch */
225 u32 batch_len; /** Length of batch within object */
226 u32 batch_flags; /** Flags composed for emit_bb_start() */
227
228 /**
229 * Indicate either the size of the hastable used to resolve
230 * relocation handles, or if negative that we are using a direct
231 * index into the execobj[].
232 */
233 int lut_size;
234 struct hlist_head *buckets; /** ht for relocation handles */
Chris Wilson67731b82010-12-08 10:38:14 +0000235};
236
Chris Wilson4ff4b442017-06-16 15:05:16 +0100237/*
238 * As an alternative to creating a hashtable of handle-to-vma for a batch,
239 * we used the last available reserved field in the execobject[] and stash
240 * a link from the execobj to its vma.
241 */
242#define __exec_to_vma(ee) (ee)->rsvd2
243#define exec_to_vma(ee) u64_to_ptr(struct i915_vma, __exec_to_vma(ee))
244
Chris Wilson2889caa2017-06-16 15:05:19 +0100245/*
246 * Used to convert any address to canonical form.
247 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
248 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
249 * addresses to be in a canonical form:
250 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
251 * canonical form [63:48] == [47]."
252 */
253#define GEN8_HIGH_ADDRESS_BIT 47
254static inline u64 gen8_canonical_addr(u64 address)
255{
256 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
257}
258
259static inline u64 gen8_noncanonical_addr(u64 address)
260{
261 return address & GENMASK_ULL(GEN8_HIGH_ADDRESS_BIT, 0);
262}
263
Chris Wilson650bc632017-06-15 09:14:33 +0100264static int eb_create(struct i915_execbuffer *eb)
Chris Wilson67731b82010-12-08 10:38:14 +0000265{
Chris Wilson2889caa2017-06-16 15:05:19 +0100266 if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) {
267 unsigned int size = 1 + ilog2(eb->buffer_count);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000268
Chris Wilson2889caa2017-06-16 15:05:19 +0100269 /*
270 * Without a 1:1 association between relocation handles and
271 * the execobject[] index, we instead create a hashtable.
272 * We size it dynamically based on available memory, starting
273 * first with 1:1 assocative hash and scaling back until
274 * the allocation succeeds.
275 *
276 * Later on we use a positive lut_size to indicate we are
277 * using this hashtable, and a negative value to indicate a
278 * direct lookup.
279 */
Chris Wilson4ff4b442017-06-16 15:05:16 +0100280 do {
281 eb->buckets = kzalloc(sizeof(struct hlist_head) << size,
282 GFP_TEMPORARY |
283 __GFP_NORETRY |
284 __GFP_NOWARN);
285 if (eb->buckets)
286 break;
287 } while (--size);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000288
Chris Wilson4ff4b442017-06-16 15:05:16 +0100289 if (unlikely(!eb->buckets)) {
290 eb->buckets = kzalloc(sizeof(struct hlist_head),
291 GFP_TEMPORARY);
292 if (unlikely(!eb->buckets))
293 return -ENOMEM;
294 }
295
Chris Wilson2889caa2017-06-16 15:05:19 +0100296 eb->lut_size = size;
Chris Wilson650bc632017-06-15 09:14:33 +0100297 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +0100298 eb->lut_size = -eb->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +0100299 }
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000300
Chris Wilson650bc632017-06-15 09:14:33 +0100301 return 0;
Chris Wilson67731b82010-12-08 10:38:14 +0000302}
303
Chris Wilson2889caa2017-06-16 15:05:19 +0100304static bool
305eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry,
306 const struct i915_vma *vma)
307{
308 if (!(entry->flags & __EXEC_OBJECT_HAS_PIN))
309 return true;
310
311 if (vma->node.size < entry->pad_to_size)
312 return true;
313
314 if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment))
315 return true;
316
317 if (entry->flags & EXEC_OBJECT_PINNED &&
318 vma->node.start != entry->offset)
319 return true;
320
321 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
322 vma->node.start < BATCH_OFFSET_BIAS)
323 return true;
324
325 if (!(entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) &&
326 (vma->node.start + vma->node.size - 1) >> 32)
327 return true;
328
329 return false;
330}
331
332static inline void
333eb_pin_vma(struct i915_execbuffer *eb,
334 struct drm_i915_gem_exec_object2 *entry,
335 struct i915_vma *vma)
336{
337 u64 flags;
338
Chris Wilson616d9ce2017-06-16 15:05:21 +0100339 if (vma->node.size)
340 flags = vma->node.start;
341 else
342 flags = entry->offset & PIN_OFFSET_MASK;
343
344 flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED;
Chris Wilson2889caa2017-06-16 15:05:19 +0100345 if (unlikely(entry->flags & EXEC_OBJECT_NEEDS_GTT))
346 flags |= PIN_GLOBAL;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100347
Chris Wilson2889caa2017-06-16 15:05:19 +0100348 if (unlikely(i915_vma_pin(vma, 0, 0, flags)))
349 return;
350
351 if (unlikely(entry->flags & EXEC_OBJECT_NEEDS_FENCE)) {
352 if (unlikely(i915_vma_get_fence(vma))) {
353 i915_vma_unpin(vma);
354 return;
355 }
356
357 if (i915_vma_pin_fence(vma))
358 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
359 }
360
361 entry->flags |= __EXEC_OBJECT_HAS_PIN;
362}
363
Chris Wilsond55495b2017-06-15 09:14:34 +0100364static inline void
365__eb_unreserve_vma(struct i915_vma *vma,
366 const struct drm_i915_gem_exec_object2 *entry)
367{
Chris Wilson2889caa2017-06-16 15:05:19 +0100368 GEM_BUG_ON(!(entry->flags & __EXEC_OBJECT_HAS_PIN));
369
Chris Wilsond55495b2017-06-15 09:14:34 +0100370 if (unlikely(entry->flags & __EXEC_OBJECT_HAS_FENCE))
371 i915_vma_unpin_fence(vma);
372
Chris Wilson2889caa2017-06-16 15:05:19 +0100373 __i915_vma_unpin(vma);
Chris Wilsond55495b2017-06-15 09:14:34 +0100374}
375
Chris Wilson2889caa2017-06-16 15:05:19 +0100376static inline void
377eb_unreserve_vma(struct i915_vma *vma,
378 struct drm_i915_gem_exec_object2 *entry)
Chris Wilsond55495b2017-06-15 09:14:34 +0100379{
Chris Wilson2889caa2017-06-16 15:05:19 +0100380 if (!(entry->flags & __EXEC_OBJECT_HAS_PIN))
381 return;
Chris Wilsond55495b2017-06-15 09:14:34 +0100382
383 __eb_unreserve_vma(vma, entry);
Chris Wilson2889caa2017-06-16 15:05:19 +0100384 entry->flags &= ~__EXEC_OBJECT_RESERVED;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100385}
386
387static int
Chris Wilson2889caa2017-06-16 15:05:19 +0100388eb_validate_vma(struct i915_execbuffer *eb,
389 struct drm_i915_gem_exec_object2 *entry,
390 struct i915_vma *vma)
391{
392 if (unlikely(entry->flags & eb->invalid_flags))
393 return -EINVAL;
394
395 if (unlikely(entry->alignment && !is_power_of_2(entry->alignment)))
396 return -EINVAL;
397
398 /*
399 * Offset can be used as input (EXEC_OBJECT_PINNED), reject
400 * any non-page-aligned or non-canonical addresses.
401 */
402 if (unlikely(entry->flags & EXEC_OBJECT_PINNED &&
403 entry->offset != gen8_canonical_addr(entry->offset & PAGE_MASK)))
404 return -EINVAL;
405
406 /* pad_to_size was once a reserved field, so sanitize it */
407 if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) {
408 if (unlikely(offset_in_page(entry->pad_to_size)))
409 return -EINVAL;
410 } else {
411 entry->pad_to_size = 0;
412 }
413
414 if (unlikely(vma->exec_entry)) {
415 DRM_DEBUG("Object [handle %d, index %d] appears more than once in object list\n",
416 entry->handle, (int)(entry - eb->exec));
417 return -EINVAL;
418 }
419
420 /*
421 * From drm_mm perspective address space is continuous,
422 * so from this point we're always using non-canonical
423 * form internally.
424 */
425 entry->offset = gen8_noncanonical_addr(entry->offset);
426
427 return 0;
428}
429
430static int
431eb_add_vma(struct i915_execbuffer *eb,
432 struct drm_i915_gem_exec_object2 *entry,
433 struct i915_vma *vma)
434{
435 int err;
436
437 GEM_BUG_ON(i915_vma_is_closed(vma));
438
439 if (!(eb->args->flags & __EXEC_VALIDATED)) {
440 err = eb_validate_vma(eb, entry, vma);
441 if (unlikely(err))
442 return err;
443 }
444
445 if (eb->lut_size >= 0) {
446 vma->exec_handle = entry->handle;
447 hlist_add_head(&vma->exec_node,
448 &eb->buckets[hash_32(entry->handle,
449 eb->lut_size)]);
450 }
451
452 if (entry->relocation_count)
453 list_add_tail(&vma->reloc_link, &eb->relocs);
454
455 if (!eb->reloc_cache.has_fence) {
456 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
457 } else {
458 if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE ||
459 eb->reloc_cache.needs_unfenced) &&
460 i915_gem_object_is_tiled(vma->obj))
461 entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP;
462 }
463
464 if (!(entry->flags & EXEC_OBJECT_PINNED))
465 entry->flags |= eb->context_flags;
466
467 /*
468 * Stash a pointer from the vma to execobj, so we can query its flags,
469 * size, alignment etc as provided by the user. Also we stash a pointer
470 * to the vma inside the execobj so that we can use a direct lookup
471 * to find the right target VMA when doing relocations.
472 */
473 vma->exec_entry = entry;
Chris Wilsondade2a62017-06-16 15:05:20 +0100474 __exec_to_vma(entry) = (uintptr_t)vma;
Chris Wilson2889caa2017-06-16 15:05:19 +0100475
476 err = 0;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100477 eb_pin_vma(eb, entry, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100478 if (eb_vma_misplaced(entry, vma)) {
479 eb_unreserve_vma(vma, entry);
480
481 list_add_tail(&vma->exec_link, &eb->unbound);
482 if (drm_mm_node_allocated(&vma->node))
483 err = i915_vma_unbind(vma);
484 } else {
485 if (entry->offset != vma->node.start) {
486 entry->offset = vma->node.start | UPDATE;
487 eb->args->flags |= __EXEC_HAS_RELOC;
488 }
489 }
490 return err;
491}
492
493static inline int use_cpu_reloc(const struct reloc_cache *cache,
494 const struct drm_i915_gem_object *obj)
495{
496 if (!i915_gem_object_has_struct_page(obj))
497 return false;
498
499 if (DBG_USE_CPU_RELOC)
500 return DBG_USE_CPU_RELOC > 0;
501
502 return (cache->has_llc ||
503 obj->cache_dirty ||
504 obj->cache_level != I915_CACHE_NONE);
505}
506
507static int eb_reserve_vma(const struct i915_execbuffer *eb,
508 struct i915_vma *vma)
509{
510 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
511 u64 flags;
512 int err;
513
514 flags = PIN_USER | PIN_NONBLOCK;
515 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
516 flags |= PIN_GLOBAL;
517
518 /*
519 * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
520 * limit address to the first 4GBs for unflagged objects.
521 */
522 if (!(entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
523 flags |= PIN_ZONE_4G;
524
525 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
526 flags |= PIN_MAPPABLE;
527
528 if (entry->flags & EXEC_OBJECT_PINNED) {
529 flags |= entry->offset | PIN_OFFSET_FIXED;
530 flags &= ~PIN_NONBLOCK; /* force overlapping PINNED checks */
531 } else if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS) {
532 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
533 }
534
535 err = i915_vma_pin(vma, entry->pad_to_size, entry->alignment, flags);
536 if (err)
537 return err;
538
539 if (entry->offset != vma->node.start) {
540 entry->offset = vma->node.start | UPDATE;
541 eb->args->flags |= __EXEC_HAS_RELOC;
542 }
543
544 entry->flags |= __EXEC_OBJECT_HAS_PIN;
545 GEM_BUG_ON(eb_vma_misplaced(entry, vma));
546
547 if (unlikely(entry->flags & EXEC_OBJECT_NEEDS_FENCE)) {
548 err = i915_vma_get_fence(vma);
549 if (unlikely(err)) {
550 i915_vma_unpin(vma);
551 return err;
552 }
553
554 if (i915_vma_pin_fence(vma))
555 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
556 }
557
558 return 0;
559}
560
561static int eb_reserve(struct i915_execbuffer *eb)
562{
563 const unsigned int count = eb->buffer_count;
564 struct list_head last;
565 struct i915_vma *vma;
566 unsigned int i, pass;
567 int err;
568
569 /*
570 * Attempt to pin all of the buffers into the GTT.
571 * This is done in 3 phases:
572 *
573 * 1a. Unbind all objects that do not match the GTT constraints for
574 * the execbuffer (fenceable, mappable, alignment etc).
575 * 1b. Increment pin count for already bound objects.
576 * 2. Bind new objects.
577 * 3. Decrement pin count.
578 *
579 * This avoid unnecessary unbinding of later objects in order to make
580 * room for the earlier objects *unless* we need to defragment.
581 */
582
583 pass = 0;
584 err = 0;
585 do {
586 list_for_each_entry(vma, &eb->unbound, exec_link) {
587 err = eb_reserve_vma(eb, vma);
588 if (err)
589 break;
590 }
591 if (err != -ENOSPC)
592 return err;
593
594 /* Resort *all* the objects into priority order */
595 INIT_LIST_HEAD(&eb->unbound);
596 INIT_LIST_HEAD(&last);
597 for (i = 0; i < count; i++) {
598 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
599
600 if (entry->flags & EXEC_OBJECT_PINNED &&
601 entry->flags & __EXEC_OBJECT_HAS_PIN)
602 continue;
603
604 vma = exec_to_vma(entry);
605 eb_unreserve_vma(vma, entry);
606
607 if (entry->flags & EXEC_OBJECT_PINNED)
608 list_add(&vma->exec_link, &eb->unbound);
609 else if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
610 list_add_tail(&vma->exec_link, &eb->unbound);
611 else
612 list_add_tail(&vma->exec_link, &last);
613 }
614 list_splice_tail(&last, &eb->unbound);
615
616 switch (pass++) {
617 case 0:
618 break;
619
620 case 1:
621 /* Too fragmented, unbind everything and retry */
622 err = i915_gem_evict_vm(eb->vm);
623 if (err)
624 return err;
625 break;
626
627 default:
628 return -ENOSPC;
629 }
630 } while (1);
631}
632
633static inline struct hlist_head *
634ht_head(const struct i915_gem_context_vma_lut *lut, u32 handle)
635{
636 return &lut->ht[hash_32(handle, lut->ht_bits)];
637}
638
639static inline bool
640ht_needs_resize(const struct i915_gem_context_vma_lut *lut)
641{
642 return (4*lut->ht_count > 3*lut->ht_size ||
643 4*lut->ht_count + 1 < lut->ht_size);
644}
645
646static unsigned int eb_batch_index(const struct i915_execbuffer *eb)
647{
Chris Wilson1a71cf22017-06-16 15:05:23 +0100648 if (eb->args->flags & I915_EXEC_BATCH_FIRST)
649 return 0;
650 else
651 return eb->buffer_count - 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100652}
653
654static int eb_select_context(struct i915_execbuffer *eb)
655{
656 struct i915_gem_context *ctx;
657
658 ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1);
659 if (unlikely(IS_ERR(ctx)))
660 return PTR_ERR(ctx);
661
662 if (unlikely(i915_gem_context_is_banned(ctx))) {
663 DRM_DEBUG("Context %u tried to submit while banned\n",
664 ctx->user_handle);
665 return -EIO;
666 }
667
668 eb->ctx = i915_gem_context_get(ctx);
669 eb->vm = ctx->ppgtt ? &ctx->ppgtt->base : &eb->i915->ggtt.base;
670
671 eb->context_flags = 0;
672 if (ctx->flags & CONTEXT_NO_ZEROMAP)
673 eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS;
674
675 return 0;
676}
677
678static int eb_lookup_vmas(struct i915_execbuffer *eb)
Chris Wilson4ff4b442017-06-16 15:05:16 +0100679{
680#define INTERMEDIATE BIT(0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100681 const unsigned int count = eb->buffer_count;
682 struct i915_gem_context_vma_lut *lut = &eb->ctx->vma_lut;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100683 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +0100684 struct idr *idr;
685 unsigned int i;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100686 int slow_pass = -1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100687 int err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100688
Chris Wilson2889caa2017-06-16 15:05:19 +0100689 INIT_LIST_HEAD(&eb->relocs);
690 INIT_LIST_HEAD(&eb->unbound);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100691
Chris Wilson2889caa2017-06-16 15:05:19 +0100692 if (unlikely(lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS))
693 flush_work(&lut->resize);
694 GEM_BUG_ON(lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100695
696 for (i = 0; i < count; i++) {
697 __exec_to_vma(&eb->exec[i]) = 0;
698
699 hlist_for_each_entry(vma,
Chris Wilson2889caa2017-06-16 15:05:19 +0100700 ht_head(lut, eb->exec[i].handle),
Chris Wilson4ff4b442017-06-16 15:05:16 +0100701 ctx_node) {
702 if (vma->ctx_handle != eb->exec[i].handle)
703 continue;
704
Chris Wilson2889caa2017-06-16 15:05:19 +0100705 err = eb_add_vma(eb, &eb->exec[i], vma);
706 if (unlikely(err))
707 return err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100708
709 goto next_vma;
710 }
711
712 if (slow_pass < 0)
713 slow_pass = i;
714next_vma: ;
715 }
716
717 if (slow_pass < 0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100718 goto out;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100719
720 spin_lock(&eb->file->table_lock);
Chris Wilson2889caa2017-06-16 15:05:19 +0100721 /*
722 * Grab a reference to the object and release the lock so we can lookup
723 * or create the VMA without using GFP_ATOMIC
724 */
725 idr = &eb->file->object_idr;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100726 for (i = slow_pass; i < count; i++) {
727 struct drm_i915_gem_object *obj;
728
729 if (__exec_to_vma(&eb->exec[i]))
730 continue;
731
Chris Wilson2889caa2017-06-16 15:05:19 +0100732 obj = to_intel_bo(idr_find(idr, eb->exec[i].handle));
Chris Wilson4ff4b442017-06-16 15:05:16 +0100733 if (unlikely(!obj)) {
734 spin_unlock(&eb->file->table_lock);
735 DRM_DEBUG("Invalid object handle %d at index %d\n",
736 eb->exec[i].handle, i);
Chris Wilson2889caa2017-06-16 15:05:19 +0100737 err = -ENOENT;
738 goto err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100739 }
740
741 __exec_to_vma(&eb->exec[i]) = INTERMEDIATE | (uintptr_t)obj;
742 }
743 spin_unlock(&eb->file->table_lock);
744
745 for (i = slow_pass; i < count; i++) {
746 struct drm_i915_gem_object *obj;
747
Chris Wilson2889caa2017-06-16 15:05:19 +0100748 if (!(__exec_to_vma(&eb->exec[i]) & INTERMEDIATE))
Chris Wilson4ff4b442017-06-16 15:05:16 +0100749 continue;
750
751 /*
752 * NOTE: We can leak any vmas created here when something fails
753 * later on. But that's no issue since vma_unbind can deal with
754 * vmas which are not actually bound. And since only
755 * lookup_or_create exists as an interface to get at the vma
756 * from the (obj, vm) we don't run the risk of creating
757 * duplicated vmas for the same vm.
758 */
Chris Wilson2889caa2017-06-16 15:05:19 +0100759 obj = u64_to_ptr(typeof(*obj),
Chris Wilson4ff4b442017-06-16 15:05:16 +0100760 __exec_to_vma(&eb->exec[i]) & ~INTERMEDIATE);
761 vma = i915_vma_instance(obj, eb->vm, NULL);
762 if (unlikely(IS_ERR(vma))) {
763 DRM_DEBUG("Failed to lookup VMA\n");
Chris Wilson2889caa2017-06-16 15:05:19 +0100764 err = PTR_ERR(vma);
765 goto err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100766 }
767
768 /* First come, first served */
769 if (!vma->ctx) {
770 vma->ctx = eb->ctx;
771 vma->ctx_handle = eb->exec[i].handle;
772 hlist_add_head(&vma->ctx_node,
Chris Wilson2889caa2017-06-16 15:05:19 +0100773 ht_head(lut, eb->exec[i].handle));
774 lut->ht_count++;
775 lut->ht_size |= I915_CTX_RESIZE_IN_PROGRESS;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100776 if (i915_vma_is_ggtt(vma)) {
777 GEM_BUG_ON(obj->vma_hashed);
778 obj->vma_hashed = vma;
779 }
Chris Wilsondade2a62017-06-16 15:05:20 +0100780
781 i915_vma_get(vma);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100782 }
783
Chris Wilson2889caa2017-06-16 15:05:19 +0100784 err = eb_add_vma(eb, &eb->exec[i], vma);
785 if (unlikely(err))
786 goto err;
Chris Wilsondade2a62017-06-16 15:05:20 +0100787
788 /* Only after we validated the user didn't use our bits */
789 if (vma->ctx != eb->ctx) {
790 i915_vma_get(vma);
791 eb->exec[i].flags |= __EXEC_OBJECT_HAS_REF;
792 }
Chris Wilson4ff4b442017-06-16 15:05:16 +0100793 }
794
Chris Wilson2889caa2017-06-16 15:05:19 +0100795 if (lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS) {
796 if (ht_needs_resize(lut))
797 queue_work(system_highpri_wq, &lut->resize);
798 else
799 lut->ht_size &= ~I915_CTX_RESIZE_IN_PROGRESS;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100800 }
801
Chris Wilson2889caa2017-06-16 15:05:19 +0100802out:
803 /* take note of the batch buffer before we might reorder the lists */
804 i = eb_batch_index(eb);
805 eb->batch = exec_to_vma(&eb->exec[i]);
Chris Wilson59bfa122016-08-04 16:32:31 +0100806
807 /*
808 * SNA is doing fancy tricks with compressing batch buffers, which leads
809 * to negative relocation deltas. Usually that works out ok since the
810 * relocate address is still positive, except when the batch is placed
811 * very low in the GTT. Ensure this doesn't happen.
812 *
813 * Note that actual hangs have only been observed on gen7, but for
814 * paranoia do it everywhere.
815 */
Chris Wilson2889caa2017-06-16 15:05:19 +0100816 if (!(eb->exec[i].flags & EXEC_OBJECT_PINNED))
817 eb->exec[i].flags |= __EXEC_OBJECT_NEEDS_BIAS;
818 if (eb->reloc_cache.has_fence)
819 eb->exec[i].flags |= EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson59bfa122016-08-04 16:32:31 +0100820
Chris Wilson2889caa2017-06-16 15:05:19 +0100821 eb->args->flags |= __EXEC_VALIDATED;
822 return eb_reserve(eb);
823
824err:
825 for (i = slow_pass; i < count; i++) {
826 if (__exec_to_vma(&eb->exec[i]) & INTERMEDIATE)
827 __exec_to_vma(&eb->exec[i]) = 0;
828 }
829 lut->ht_size &= ~I915_CTX_RESIZE_IN_PROGRESS;
830 return err;
831#undef INTERMEDIATE
Chris Wilson59bfa122016-08-04 16:32:31 +0100832}
833
Chris Wilson4ff4b442017-06-16 15:05:16 +0100834static struct i915_vma *
Chris Wilson2889caa2017-06-16 15:05:19 +0100835eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000836{
Chris Wilson2889caa2017-06-16 15:05:19 +0100837 if (eb->lut_size < 0) {
838 if (handle >= -eb->lut_size)
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000839 return NULL;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100840 return exec_to_vma(&eb->exec[handle]);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000841 } else {
842 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800843 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000844
Chris Wilson2889caa2017-06-16 15:05:19 +0100845 head = &eb->buckets[hash_32(handle, eb->lut_size)];
Geliang Tangaa459502016-01-18 23:54:20 +0800846 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200847 if (vma->exec_handle == handle)
848 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000849 }
850 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000851 }
Chris Wilson67731b82010-12-08 10:38:14 +0000852}
853
Chris Wilson2889caa2017-06-16 15:05:19 +0100854static void eb_release_vmas(const struct i915_execbuffer *eb)
Chris Wilsona415d352013-11-26 11:23:15 +0000855{
Chris Wilson2889caa2017-06-16 15:05:19 +0100856 const unsigned int count = eb->buffer_count;
857 unsigned int i;
Chris Wilson650bc632017-06-15 09:14:33 +0100858
Chris Wilson2889caa2017-06-16 15:05:19 +0100859 for (i = 0; i < count; i++) {
860 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
861 struct i915_vma *vma = exec_to_vma(entry);
862
863 if (!vma)
Chris Wilsond55495b2017-06-15 09:14:34 +0100864 continue;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000865
Chris Wilson2889caa2017-06-16 15:05:19 +0100866 GEM_BUG_ON(vma->exec_entry != entry);
Chris Wilson172ae5b2016-12-05 14:29:37 +0000867 vma->exec_entry = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100868
Chris Wilsondade2a62017-06-16 15:05:20 +0100869 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
870 __eb_unreserve_vma(vma, entry);
Chris Wilson2889caa2017-06-16 15:05:19 +0100871
Chris Wilsondade2a62017-06-16 15:05:20 +0100872 if (entry->flags & __EXEC_OBJECT_HAS_REF)
873 i915_vma_put(vma);
874
875 entry->flags &=
876 ~(__EXEC_OBJECT_RESERVED | __EXEC_OBJECT_HAS_REF);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000877 }
Chris Wilson2889caa2017-06-16 15:05:19 +0100878}
Chris Wilsond55495b2017-06-15 09:14:34 +0100879
Chris Wilson2889caa2017-06-16 15:05:19 +0100880static void eb_reset_vmas(const struct i915_execbuffer *eb)
881{
882 eb_release_vmas(eb);
883 if (eb->lut_size >= 0)
884 memset(eb->buckets, 0,
885 sizeof(struct hlist_head) << eb->lut_size);
886}
Chris Wilsond55495b2017-06-15 09:14:34 +0100887
Chris Wilson2889caa2017-06-16 15:05:19 +0100888static void eb_destroy(const struct i915_execbuffer *eb)
889{
890 if (eb->lut_size >= 0)
Chris Wilsond55495b2017-06-15 09:14:34 +0100891 kfree(eb->buckets);
Chris Wilson67731b82010-12-08 10:38:14 +0000892}
893
Chris Wilson2889caa2017-06-16 15:05:19 +0100894static inline u64
Chris Wilsond50415c2016-08-18 17:16:52 +0100895relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +0100896 const struct i915_vma *target)
Michał Winiarski934acce2015-12-29 18:24:52 +0100897{
Chris Wilson2889caa2017-06-16 15:05:19 +0100898 return gen8_canonical_addr((int)reloc->delta + target->node.start);
Michał Winiarski934acce2015-12-29 18:24:52 +0100899}
900
Chris Wilsond50415c2016-08-18 17:16:52 +0100901static void reloc_cache_init(struct reloc_cache *cache,
902 struct drm_i915_private *i915)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100903{
Chris Wilson31a39202016-08-18 17:16:46 +0100904 cache->page = -1;
Chris Wilsond50415c2016-08-18 17:16:52 +0100905 cache->vaddr = 0;
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200906 /* Must be a variable in the struct to allow GCC to unroll. */
Chris Wilson2889caa2017-06-16 15:05:19 +0100907 cache->has_llc = HAS_LLC(i915);
908 cache->has_fence = INTEL_GEN(i915) < 4;
909 cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200910 cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100911 cache->node.allocated = false;
Chris Wilson31a39202016-08-18 17:16:46 +0100912}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100913
Chris Wilsond50415c2016-08-18 17:16:52 +0100914static inline void *unmask_page(unsigned long p)
915{
916 return (void *)(uintptr_t)(p & PAGE_MASK);
917}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100918
Chris Wilsond50415c2016-08-18 17:16:52 +0100919static inline unsigned int unmask_flags(unsigned long p)
920{
921 return p & ~PAGE_MASK;
922}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700923
Chris Wilsond50415c2016-08-18 17:16:52 +0100924#define KMAP 0x4 /* after CLFLUSH_FLAGS */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700925
Chris Wilson650bc632017-06-15 09:14:33 +0100926static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
927{
928 struct drm_i915_private *i915 =
929 container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
930 return &i915->ggtt;
931}
932
933static void reloc_cache_reset(struct reloc_cache *cache)
Chris Wilson31a39202016-08-18 17:16:46 +0100934{
Chris Wilsond50415c2016-08-18 17:16:52 +0100935 void *vaddr;
936
Chris Wilson31a39202016-08-18 17:16:46 +0100937 if (!cache->vaddr)
938 return;
939
Chris Wilsond50415c2016-08-18 17:16:52 +0100940 vaddr = unmask_page(cache->vaddr);
941 if (cache->vaddr & KMAP) {
942 if (cache->vaddr & CLFLUSH_AFTER)
943 mb();
Chris Wilson31a39202016-08-18 17:16:46 +0100944
Chris Wilsond50415c2016-08-18 17:16:52 +0100945 kunmap_atomic(vaddr);
946 i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
947 } else {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100948 wmb();
Chris Wilsond50415c2016-08-18 17:16:52 +0100949 io_mapping_unmap_atomic((void __iomem *)vaddr);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100950 if (cache->node.allocated) {
Chris Wilson650bc632017-06-15 09:14:33 +0100951 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100952
953 ggtt->base.clear_range(&ggtt->base,
954 cache->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200955 cache->node.size);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100956 drm_mm_remove_node(&cache->node);
957 } else {
958 i915_vma_unpin((struct i915_vma *)cache->node.mm);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700959 }
Chris Wilson31a39202016-08-18 17:16:46 +0100960 }
Chris Wilson650bc632017-06-15 09:14:33 +0100961
962 cache->vaddr = 0;
963 cache->page = -1;
Chris Wilson31a39202016-08-18 17:16:46 +0100964}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700965
Chris Wilson31a39202016-08-18 17:16:46 +0100966static void *reloc_kmap(struct drm_i915_gem_object *obj,
967 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100968 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100969{
Chris Wilsond50415c2016-08-18 17:16:52 +0100970 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100971
Chris Wilsond50415c2016-08-18 17:16:52 +0100972 if (cache->vaddr) {
973 kunmap_atomic(unmask_page(cache->vaddr));
974 } else {
975 unsigned int flushes;
Chris Wilson2889caa2017-06-16 15:05:19 +0100976 int err;
Chris Wilson31a39202016-08-18 17:16:46 +0100977
Chris Wilson2889caa2017-06-16 15:05:19 +0100978 err = i915_gem_obj_prepare_shmem_write(obj, &flushes);
979 if (err)
980 return ERR_PTR(err);
Chris Wilsond50415c2016-08-18 17:16:52 +0100981
982 BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
983 BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
984
985 cache->vaddr = flushes | KMAP;
986 cache->node.mm = (void *)obj;
987 if (flushes)
988 mb();
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700989 }
990
Chris Wilsond50415c2016-08-18 17:16:52 +0100991 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
992 cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100993 cache->page = page;
Chris Wilson31a39202016-08-18 17:16:46 +0100994
Chris Wilsond50415c2016-08-18 17:16:52 +0100995 return vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100996}
997
Chris Wilsond50415c2016-08-18 17:16:52 +0100998static void *reloc_iomap(struct drm_i915_gem_object *obj,
Chris Wilson31a39202016-08-18 17:16:46 +0100999 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001000 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +01001001{
Chris Wilson650bc632017-06-15 09:14:33 +01001002 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001003 unsigned long offset;
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) {
Jani Nikula615e5002016-10-04 12:54:13 +03001007 io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
Chris Wilsond50415c2016-08-18 17:16:52 +01001008 } else {
1009 struct i915_vma *vma;
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 if (use_cpu_reloc(cache, obj))
Chris Wilsond50415c2016-08-18 17:16:52 +01001013 return NULL;
Chris Wilson31a39202016-08-18 17:16:46 +01001014
Chris Wilson2889caa2017-06-16 15:05:19 +01001015 err = i915_gem_object_set_to_gtt_domain(obj, true);
1016 if (err)
1017 return ERR_PTR(err);
Chris Wilson31a39202016-08-18 17:16:46 +01001018
Chris Wilsond50415c2016-08-18 17:16:52 +01001019 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1020 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001021 if (IS_ERR(vma)) {
1022 memset(&cache->node, 0, sizeof(cache->node));
Chris Wilson2889caa2017-06-16 15:05:19 +01001023 err = drm_mm_insert_node_in_range
Chris Wilsone8cb9092016-08-18 17:16:53 +01001024 (&ggtt->base.mm, &cache->node,
Chris Wilsonf51455d2017-01-10 14:47:34 +00001025 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
Chris Wilsone8cb9092016-08-18 17:16:53 +01001026 0, ggtt->mappable_end,
Chris Wilson4e64e552017-02-02 21:04:38 +00001027 DRM_MM_INSERT_LOW);
Chris Wilson2889caa2017-06-16 15:05:19 +01001028 if (err) /* no inactive aperture space, use cpu reloc */
Chris Wilsonc92fa4f2016-10-07 07:53:25 +01001029 return NULL;
Chris Wilsone8cb9092016-08-18 17:16:53 +01001030 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +01001031 err = i915_vma_put_fence(vma);
1032 if (err) {
Chris Wilsone8cb9092016-08-18 17:16:53 +01001033 i915_vma_unpin(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001034 return ERR_PTR(err);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001035 }
Rafael Barbalho5032d872013-08-21 17:10:51 +01001036
Chris Wilsone8cb9092016-08-18 17:16:53 +01001037 cache->node.start = vma->node.start;
1038 cache->node.mm = (void *)vma;
Chris Wilsond50415c2016-08-18 17:16:52 +01001039 }
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001040 }
1041
Chris Wilsone8cb9092016-08-18 17:16:53 +01001042 offset = cache->node.start;
1043 if (cache->node.allocated) {
Chris Wilsonfc099092016-10-28 15:27:56 +01001044 wmb();
Chris Wilsone8cb9092016-08-18 17:16:53 +01001045 ggtt->base.insert_page(&ggtt->base,
1046 i915_gem_object_get_dma_address(obj, page),
1047 offset, I915_CACHE_NONE, 0);
1048 } else {
1049 offset += page << PAGE_SHIFT;
1050 }
1051
Chris Wilson650bc632017-06-15 09:14:33 +01001052 vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->mappable,
1053 offset);
Chris Wilsond50415c2016-08-18 17:16:52 +01001054 cache->page = page;
1055 cache->vaddr = (unsigned long)vaddr;
1056
1057 return vaddr;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001058}
1059
Chris Wilsond50415c2016-08-18 17:16:52 +01001060static void *reloc_vaddr(struct drm_i915_gem_object *obj,
1061 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001062 unsigned long page)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001063{
Chris Wilsond50415c2016-08-18 17:16:52 +01001064 void *vaddr;
1065
1066 if (cache->page == page) {
1067 vaddr = unmask_page(cache->vaddr);
1068 } else {
1069 vaddr = NULL;
1070 if ((cache->vaddr & KMAP) == 0)
1071 vaddr = reloc_iomap(obj, cache, page);
1072 if (!vaddr)
1073 vaddr = reloc_kmap(obj, cache, page);
1074 }
1075
1076 return vaddr;
1077}
1078
1079static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
1080{
1081 if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
1082 if (flushes & CLFLUSH_BEFORE) {
1083 clflushopt(addr);
1084 mb();
1085 }
1086
1087 *addr = value;
1088
Chris Wilson2889caa2017-06-16 15:05:19 +01001089 /*
1090 * Writes to the same cacheline are serialised by the CPU
Chris Wilsond50415c2016-08-18 17:16:52 +01001091 * (including clflush). On the write path, we only require
1092 * that it hits memory in an orderly fashion and place
1093 * mb barriers at the start and end of the relocation phase
1094 * to ensure ordering of clflush wrt to the system.
1095 */
1096 if (flushes & CLFLUSH_AFTER)
1097 clflushopt(addr);
1098 } else
1099 *addr = value;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001100}
1101
Chris Wilson2889caa2017-06-16 15:05:19 +01001102static u64
1103relocate_entry(struct i915_vma *vma,
Chris Wilsond50415c2016-08-18 17:16:52 +01001104 const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +01001105 struct i915_execbuffer *eb,
1106 const struct i915_vma *target)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001107{
Chris Wilson2889caa2017-06-16 15:05:19 +01001108 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond50415c2016-08-18 17:16:52 +01001109 u64 offset = reloc->offset;
Chris Wilson2889caa2017-06-16 15:05:19 +01001110 u64 target_offset = relocation_target(reloc, target);
1111 bool wide = eb->reloc_cache.use_64bit_reloc;
Chris Wilsond50415c2016-08-18 17:16:52 +01001112 void *vaddr;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001113
Chris Wilsond50415c2016-08-18 17:16:52 +01001114repeat:
Chris Wilson2889caa2017-06-16 15:05:19 +01001115 vaddr = reloc_vaddr(obj, &eb->reloc_cache, offset >> PAGE_SHIFT);
Chris Wilsond50415c2016-08-18 17:16:52 +01001116 if (IS_ERR(vaddr))
1117 return PTR_ERR(vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001118
Chris Wilsond50415c2016-08-18 17:16:52 +01001119 clflush_write32(vaddr + offset_in_page(offset),
1120 lower_32_bits(target_offset),
Chris Wilson2889caa2017-06-16 15:05:19 +01001121 eb->reloc_cache.vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001122
Chris Wilsond50415c2016-08-18 17:16:52 +01001123 if (wide) {
1124 offset += sizeof(u32);
1125 target_offset >>= 32;
1126 wide = false;
1127 goto repeat;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001128 }
Chris Wilsondabdfe02012-03-26 10:10:27 +02001129
Chris Wilson2889caa2017-06-16 15:05:19 +01001130 return target->node.start | UPDATE;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001131}
1132
Chris Wilson2889caa2017-06-16 15:05:19 +01001133static u64
1134eb_relocate_entry(struct i915_execbuffer *eb,
1135 struct i915_vma *vma,
1136 const struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001137{
Chris Wilson507d9772017-06-16 15:05:17 +01001138 struct i915_vma *target;
Chris Wilson2889caa2017-06-16 15:05:19 +01001139 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001140
Chris Wilson67731b82010-12-08 10:38:14 +00001141 /* we've already hold a reference to all valid objects */
Chris Wilson507d9772017-06-16 15:05:17 +01001142 target = eb_get_vma(eb, reloc->target_handle);
1143 if (unlikely(!target))
Chris Wilson54cf91d2010-11-25 18:00:26 +00001144 return -ENOENT;
Eric Anholte844b992012-07-31 15:35:01 -07001145
Chris Wilson54cf91d2010-11-25 18:00:26 +00001146 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001147 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001148 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001149 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001150 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001151 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001152 (int) reloc->offset,
1153 reloc->read_domains,
1154 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001155 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001156 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +01001157 if (unlikely((reloc->write_domain | reloc->read_domains)
1158 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001159 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001160 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001161 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001162 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001163 (int) reloc->offset,
1164 reloc->read_domains,
1165 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001166 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001167 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001168
Chris Wilson2889caa2017-06-16 15:05:19 +01001169 if (reloc->write_domain) {
Chris Wilson507d9772017-06-16 15:05:17 +01001170 target->exec_entry->flags |= EXEC_OBJECT_WRITE;
1171
Chris Wilson2889caa2017-06-16 15:05:19 +01001172 /*
1173 * Sandybridge PPGTT errata: We need a global gtt mapping
1174 * for MI and pipe_control writes because the gpu doesn't
1175 * properly redirect them through the ppgtt for non_secure
1176 * batchbuffers.
1177 */
1178 if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
1179 IS_GEN6(eb->i915)) {
1180 err = i915_vma_bind(target, target->obj->cache_level,
1181 PIN_GLOBAL);
1182 if (WARN_ONCE(err,
1183 "Unexpected failure to bind target VMA!"))
1184 return err;
1185 }
Chris Wilson507d9772017-06-16 15:05:17 +01001186 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001187
Chris Wilson2889caa2017-06-16 15:05:19 +01001188 /*
1189 * If the relocation already has the right value in it, no
Chris Wilson54cf91d2010-11-25 18:00:26 +00001190 * more work needs to be done.
1191 */
Chris Wilson2889caa2017-06-16 15:05:19 +01001192 if (gen8_canonical_addr(target->node.start) == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +00001193 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001194
1195 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001196 if (unlikely(reloc->offset >
Chris Wilson507d9772017-06-16 15:05:17 +01001197 vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001198 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson507d9772017-06-16 15:05:17 +01001199 "target %d offset %d size %d.\n",
1200 reloc->target_handle,
1201 (int)reloc->offset,
1202 (int)vma->size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001203 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001204 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001205 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001206 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson507d9772017-06-16 15:05:17 +01001207 "target %d offset %d.\n",
1208 reloc->target_handle,
1209 (int)reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001210 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001211 }
1212
Chris Wilson071750e2017-06-16 15:05:18 +01001213 /*
1214 * If we write into the object, we need to force the synchronisation
1215 * barrier, either with an asynchronous clflush or if we executed the
1216 * patching using the GPU (though that should be serialised by the
1217 * timeline). To be completely sure, and since we are required to
1218 * do relocations we are already stalling, disable the user's opt
1219 * of our synchronisation.
1220 */
1221 vma->exec_entry->flags &= ~EXEC_OBJECT_ASYNC;
1222
Chris Wilson54cf91d2010-11-25 18:00:26 +00001223 /* and update the user's relocation entry */
Chris Wilson2889caa2017-06-16 15:05:19 +01001224 return relocate_entry(vma, reloc, eb, target);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001225}
1226
Chris Wilson2889caa2017-06-16 15:05:19 +01001227static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001228{
Chris Wilson1d83f442012-03-24 20:12:53 +00001229#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
Chris Wilson2889caa2017-06-16 15:05:19 +01001230 struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
1231 struct drm_i915_gem_relocation_entry __user *urelocs;
1232 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
1233 unsigned int remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001234
Chris Wilson2889caa2017-06-16 15:05:19 +01001235 urelocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson1d83f442012-03-24 20:12:53 +00001236 remain = entry->relocation_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01001237 if (unlikely(remain > N_RELOC(ULONG_MAX)))
1238 return -EINVAL;
Chris Wilsonebc08082016-10-18 13:02:51 +01001239
Chris Wilson2889caa2017-06-16 15:05:19 +01001240 /*
1241 * We must check that the entire relocation array is safe
1242 * to read. However, if the array is not writable the user loses
1243 * the updated relocation values.
1244 */
1245 if (unlikely(!access_ok(VERIFY_READ, urelocs, remain*sizeof(urelocs))))
1246 return -EFAULT;
Chris Wilson1d83f442012-03-24 20:12:53 +00001247
Chris Wilson2889caa2017-06-16 15:05:19 +01001248 do {
1249 struct drm_i915_gem_relocation_entry *r = stack;
1250 unsigned int count =
1251 min_t(unsigned int, remain, ARRAY_SIZE(stack));
1252 unsigned int copied;
1253
1254 /*
1255 * This is the fast path and we cannot handle a pagefault
Chris Wilsonebc08082016-10-18 13:02:51 +01001256 * whilst holding the struct mutex lest the user pass in the
1257 * relocations contained within a mmaped bo. For in such a case
1258 * we, the page fault handler would call i915_gem_fault() and
1259 * we would try to acquire the struct mutex again. Obviously
1260 * this is bad and so lockdep complains vehemently.
1261 */
1262 pagefault_disable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001263 copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
Chris Wilsonebc08082016-10-18 13:02:51 +01001264 pagefault_enable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001265 if (unlikely(copied)) {
1266 remain = -EFAULT;
Chris Wilson31a39202016-08-18 17:16:46 +01001267 goto out;
1268 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001269
Chris Wilson2889caa2017-06-16 15:05:19 +01001270 remain -= count;
Chris Wilson1d83f442012-03-24 20:12:53 +00001271 do {
Chris Wilson2889caa2017-06-16 15:05:19 +01001272 u64 offset = eb_relocate_entry(eb, vma, r);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001273
Chris Wilson2889caa2017-06-16 15:05:19 +01001274 if (likely(offset == 0)) {
1275 } else if ((s64)offset < 0) {
1276 remain = (int)offset;
Chris Wilson31a39202016-08-18 17:16:46 +01001277 goto out;
Chris Wilson2889caa2017-06-16 15:05:19 +01001278 } else {
1279 /*
1280 * Note that reporting an error now
1281 * leaves everything in an inconsistent
1282 * state as we have *already* changed
1283 * the relocation value inside the
1284 * object. As we have not changed the
1285 * reloc.presumed_offset or will not
1286 * change the execobject.offset, on the
1287 * call we may not rewrite the value
1288 * inside the object, leaving it
1289 * dangling and causing a GPU hang. Unless
1290 * userspace dynamically rebuilds the
1291 * relocations on each execbuf rather than
1292 * presume a static tree.
1293 *
1294 * We did previously check if the relocations
1295 * were writable (access_ok), an error now
1296 * would be a strange race with mprotect,
1297 * having already demonstrated that we
1298 * can read from this userspace address.
1299 */
1300 offset = gen8_canonical_addr(offset & ~UPDATE);
1301 __put_user(offset,
1302 &urelocs[r-stack].presumed_offset);
Chris Wilson1d83f442012-03-24 20:12:53 +00001303 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001304 } while (r++, --count);
1305 urelocs += ARRAY_SIZE(stack);
1306 } while (remain);
Chris Wilson31a39202016-08-18 17:16:46 +01001307out:
Chris Wilson650bc632017-06-15 09:14:33 +01001308 reloc_cache_reset(&eb->reloc_cache);
Chris Wilson2889caa2017-06-16 15:05:19 +01001309 return remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001310}
1311
1312static int
Chris Wilson2889caa2017-06-16 15:05:19 +01001313eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001314{
Ben Widawsky27173f12013-08-14 11:38:36 +02001315 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson2889caa2017-06-16 15:05:19 +01001316 struct drm_i915_gem_relocation_entry *relocs =
1317 u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1318 unsigned int i;
1319 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001320
1321 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001322 u64 offset = eb_relocate_entry(eb, vma, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001323
Chris Wilson2889caa2017-06-16 15:05:19 +01001324 if ((s64)offset < 0) {
1325 err = (int)offset;
1326 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001327 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001328 }
1329 err = 0;
Chris Wilsona415d352013-11-26 11:23:15 +00001330err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001331 reloc_cache_reset(&eb->reloc_cache);
1332 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001333}
1334
Chris Wilson2889caa2017-06-16 15:05:19 +01001335static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001336{
Chris Wilson2889caa2017-06-16 15:05:19 +01001337 const char __user *addr, *end;
1338 unsigned long size;
1339 char __maybe_unused c;
Ben Widawsky27173f12013-08-14 11:38:36 +02001340
Chris Wilson2889caa2017-06-16 15:05:19 +01001341 size = entry->relocation_count;
1342 if (size == 0)
1343 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001344
Chris Wilson2889caa2017-06-16 15:05:19 +01001345 if (size > N_RELOC(ULONG_MAX))
1346 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001347
Chris Wilson2889caa2017-06-16 15:05:19 +01001348 addr = u64_to_user_ptr(entry->relocs_ptr);
1349 size *= sizeof(struct drm_i915_gem_relocation_entry);
1350 if (!access_ok(VERIFY_READ, addr, size))
1351 return -EFAULT;
1352
1353 end = addr + size;
1354 for (; addr < end; addr += PAGE_SIZE) {
1355 int err = __get_user(c, addr);
1356 if (err)
1357 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001358 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001359 return __get_user(c, end - 1);
1360}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001361
Chris Wilson2889caa2017-06-16 15:05:19 +01001362static int eb_copy_relocations(const struct i915_execbuffer *eb)
1363{
1364 const unsigned int count = eb->buffer_count;
1365 unsigned int i;
1366 int err;
1367
Chris Wilson54cf91d2010-11-25 18:00:26 +00001368 for (i = 0; i < count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001369 const unsigned int nreloc = eb->exec[i].relocation_count;
1370 struct drm_i915_gem_relocation_entry __user *urelocs;
1371 struct drm_i915_gem_relocation_entry *relocs;
1372 unsigned long size;
1373 unsigned long copied;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001374
Chris Wilson2889caa2017-06-16 15:05:19 +01001375 if (nreloc == 0)
1376 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001377
Chris Wilson2889caa2017-06-16 15:05:19 +01001378 err = check_relocations(&eb->exec[i]);
1379 if (err)
1380 goto err;
1381
1382 urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
1383 size = nreloc * sizeof(*relocs);
1384
1385 relocs = kvmalloc_array(size, 1, GFP_TEMPORARY);
1386 if (!relocs) {
1387 kvfree(relocs);
1388 err = -ENOMEM;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001389 goto err;
1390 }
1391
Chris Wilson2889caa2017-06-16 15:05:19 +01001392 /* copy_from_user is limited to < 4GiB */
1393 copied = 0;
1394 do {
1395 unsigned int len =
1396 min_t(u64, BIT_ULL(31), size - copied);
1397
1398 if (__copy_from_user((char *)relocs + copied,
1399 (char *)urelocs + copied,
1400 len)) {
1401 kvfree(relocs);
1402 err = -EFAULT;
1403 goto err;
1404 }
1405
1406 copied += len;
1407 } while (copied < size);
1408
1409 /*
1410 * As we do not update the known relocation offsets after
Chris Wilson262b6d32013-01-15 16:17:54 +00001411 * relocating (due to the complexities in lock handling),
1412 * we need to mark them as invalid now so that we force the
1413 * relocation processing next time. Just in case the target
1414 * object is evicted and then rebound into its old
1415 * presumed_offset before the next execbuffer - if that
1416 * happened we would make the mistake of assuming that the
1417 * relocations were valid.
1418 */
Chris Wilson2889caa2017-06-16 15:05:19 +01001419 user_access_begin();
1420 for (copied = 0; copied < nreloc; copied++)
1421 unsafe_put_user(-1,
1422 &urelocs[copied].presumed_offset,
1423 end_user);
1424end_user:
1425 user_access_end();
Chris Wilson262b6d32013-01-15 16:17:54 +00001426
Chris Wilson2889caa2017-06-16 15:05:19 +01001427 eb->exec[i].relocs_ptr = (uintptr_t)relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001428 }
1429
Chris Wilson2889caa2017-06-16 15:05:19 +01001430 return 0;
1431
1432err:
1433 while (i--) {
1434 struct drm_i915_gem_relocation_entry *relocs =
1435 u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr);
1436 if (eb->exec[i].relocation_count)
1437 kvfree(relocs);
1438 }
1439 return err;
1440}
1441
1442static int eb_prefault_relocations(const struct i915_execbuffer *eb)
1443{
1444 const unsigned int count = eb->buffer_count;
1445 unsigned int i;
1446
1447 if (unlikely(i915.prefault_disable))
1448 return 0;
1449
1450 for (i = 0; i < count; i++) {
1451 int err;
1452
1453 err = check_relocations(&eb->exec[i]);
1454 if (err)
1455 return err;
1456 }
1457
1458 return 0;
1459}
1460
1461static noinline int eb_relocate_slow(struct i915_execbuffer *eb)
1462{
1463 struct drm_device *dev = &eb->i915->drm;
1464 bool have_copy = false;
1465 struct i915_vma *vma;
1466 int err = 0;
1467
1468repeat:
1469 if (signal_pending(current)) {
1470 err = -ERESTARTSYS;
1471 goto out;
1472 }
1473
1474 /* We may process another execbuffer during the unlock... */
1475 eb_reset_vmas(eb);
1476 mutex_unlock(&dev->struct_mutex);
1477
1478 /*
1479 * We take 3 passes through the slowpatch.
1480 *
1481 * 1 - we try to just prefault all the user relocation entries and
1482 * then attempt to reuse the atomic pagefault disabled fast path again.
1483 *
1484 * 2 - we copy the user entries to a local buffer here outside of the
1485 * local and allow ourselves to wait upon any rendering before
1486 * relocations
1487 *
1488 * 3 - we already have a local copy of the relocation entries, but
1489 * were interrupted (EAGAIN) whilst waiting for the objects, try again.
1490 */
1491 if (!err) {
1492 err = eb_prefault_relocations(eb);
1493 } else if (!have_copy) {
1494 err = eb_copy_relocations(eb);
1495 have_copy = err == 0;
1496 } else {
1497 cond_resched();
1498 err = 0;
1499 }
1500 if (err) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001501 mutex_lock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01001502 goto out;
1503 }
1504
Chris Wilson8a2421b2017-06-16 15:05:22 +01001505 /* A frequent cause for EAGAIN are currently unavailable client pages */
1506 flush_workqueue(eb->i915->mm.userptr_wq);
1507
Chris Wilson2889caa2017-06-16 15:05:19 +01001508 err = i915_mutex_lock_interruptible(dev);
1509 if (err) {
1510 mutex_lock(&dev->struct_mutex);
1511 goto out;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001512 }
1513
Chris Wilson67731b82010-12-08 10:38:14 +00001514 /* reacquire the objects */
Chris Wilson2889caa2017-06-16 15:05:19 +01001515 err = eb_lookup_vmas(eb);
1516 if (err)
Chris Wilson3b96eff2013-01-08 10:53:14 +00001517 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +00001518
Chris Wilson2889caa2017-06-16 15:05:19 +01001519 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1520 if (!have_copy) {
1521 pagefault_disable();
1522 err = eb_relocate_vma(eb, vma);
1523 pagefault_enable();
1524 if (err)
1525 goto repeat;
1526 } else {
1527 err = eb_relocate_vma_slow(eb, vma);
1528 if (err)
1529 goto err;
1530 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001531 }
1532
Chris Wilson2889caa2017-06-16 15:05:19 +01001533 /*
1534 * Leave the user relocations as are, this is the painfully slow path,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001535 * and we want to avoid the complication of dropping the lock whilst
1536 * having buffers reserved in the aperture and so causing spurious
1537 * ENOSPC for random operations.
1538 */
1539
1540err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001541 if (err == -EAGAIN)
1542 goto repeat;
1543
1544out:
1545 if (have_copy) {
1546 const unsigned int count = eb->buffer_count;
1547 unsigned int i;
1548
1549 for (i = 0; i < count; i++) {
1550 const struct drm_i915_gem_exec_object2 *entry =
1551 &eb->exec[i];
1552 struct drm_i915_gem_relocation_entry *relocs;
1553
1554 if (!entry->relocation_count)
1555 continue;
1556
1557 relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1558 kvfree(relocs);
1559 }
1560 }
1561
1562 return err ?: have_copy;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001563}
1564
Chris Wilson2889caa2017-06-16 15:05:19 +01001565static int eb_relocate(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001566{
Chris Wilson2889caa2017-06-16 15:05:19 +01001567 if (eb_lookup_vmas(eb))
1568 goto slow;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001569
Chris Wilson2889caa2017-06-16 15:05:19 +01001570 /* The objects are in their final locations, apply the relocations. */
1571 if (eb->args->flags & __EXEC_HAS_RELOC) {
1572 struct i915_vma *vma;
1573
1574 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1575 if (eb_relocate_vma(eb, vma))
1576 goto slow;
1577 }
1578 }
1579
1580 return 0;
1581
1582slow:
1583 return eb_relocate_slow(eb);
1584}
1585
1586static void eb_export_fence(struct drm_i915_gem_object *obj,
1587 struct drm_i915_gem_request *req,
1588 unsigned int flags)
1589{
1590 struct reservation_object *resv = obj->resv;
1591
1592 /*
1593 * Ignore errors from failing to allocate the new fence, we can't
1594 * handle an error right now. Worst case should be missed
1595 * synchronisation leading to rendering corruption.
1596 */
1597 reservation_object_lock(resv, NULL);
1598 if (flags & EXEC_OBJECT_WRITE)
1599 reservation_object_add_excl_fence(resv, &req->fence);
1600 else if (reservation_object_reserve_shared(resv) == 0)
1601 reservation_object_add_shared_fence(resv, &req->fence);
1602 reservation_object_unlock(resv);
1603}
1604
1605static int eb_move_to_gpu(struct i915_execbuffer *eb)
1606{
1607 const unsigned int count = eb->buffer_count;
1608 unsigned int i;
1609 int err;
1610
1611 for (i = 0; i < count; i++) {
1612 const struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
1613 struct i915_vma *vma = exec_to_vma(entry);
Ben Widawsky27173f12013-08-14 11:38:36 +02001614 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001615
Chris Wilson2889caa2017-06-16 15:05:19 +01001616 if (entry->flags & EXEC_OBJECT_CAPTURE) {
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001617 struct i915_gem_capture_list *capture;
1618
1619 capture = kmalloc(sizeof(*capture), GFP_KERNEL);
1620 if (unlikely(!capture))
1621 return -ENOMEM;
1622
Chris Wilson650bc632017-06-15 09:14:33 +01001623 capture->next = eb->request->capture_list;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001624 capture->vma = vma;
Chris Wilson650bc632017-06-15 09:14:33 +01001625 eb->request->capture_list = capture;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001626 }
1627
Chris Wilson2889caa2017-06-16 15:05:19 +01001628 if (entry->flags & EXEC_OBJECT_ASYNC)
1629 goto skip_flushes;
Chris Wilson77ae9952017-01-27 09:40:07 +00001630
Chris Wilson7fc92e92017-06-16 11:54:55 +01001631 if (unlikely(obj->cache_dirty && !obj->cache_coherent))
Chris Wilson57822dc2017-02-22 11:40:48 +00001632 i915_gem_clflush_object(obj, 0);
Chris Wilson57822dc2017-02-22 11:40:48 +00001633
Chris Wilson2889caa2017-06-16 15:05:19 +01001634 err = i915_gem_request_await_object
1635 (eb->request, obj, entry->flags & EXEC_OBJECT_WRITE);
1636 if (err)
1637 return err;
1638
1639skip_flushes:
1640 i915_vma_move_to_active(vma, eb->request, entry->flags);
1641 __eb_unreserve_vma(vma, entry);
1642 vma->exec_entry = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001643 }
1644
Chris Wilson2889caa2017-06-16 15:05:19 +01001645 for (i = 0; i < count; i++) {
1646 const struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
1647 struct i915_vma *vma = exec_to_vma(entry);
1648
1649 eb_export_fence(vma->obj, eb->request, entry->flags);
Chris Wilsondade2a62017-06-16 15:05:20 +01001650 if (unlikely(entry->flags & __EXEC_OBJECT_HAS_REF))
1651 i915_vma_put(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001652 }
1653 eb->exec = NULL;
1654
Chris Wilsondcd79932016-08-18 17:16:40 +01001655 /* Unconditionally flush any chipset caches (for streaming writes). */
Chris Wilson650bc632017-06-15 09:14:33 +01001656 i915_gem_chipset_flush(eb->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001657
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001658 /* Unconditionally invalidate GPU caches and TLBs. */
Chris Wilson650bc632017-06-15 09:14:33 +01001659 return eb->engine->emit_flush(eb->request, EMIT_INVALIDATE);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001660}
1661
Chris Wilson2889caa2017-06-16 15:05:19 +01001662static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001663{
Chris Wilson650bc632017-06-15 09:14:33 +01001664 if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
Daniel Vettered5982e2013-01-17 22:23:36 +01001665 return false;
1666
Chris Wilson2f5945b2015-10-06 11:39:55 +01001667 /* Kernel clipping was a DRI1 misfeature */
1668 if (exec->num_cliprects || exec->cliprects_ptr)
1669 return false;
1670
1671 if (exec->DR4 == 0xffffffff) {
1672 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1673 exec->DR4 = 0;
1674 }
1675 if (exec->DR1 || exec->DR4)
1676 return false;
1677
1678 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1679 return false;
1680
1681 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001682}
1683
Chris Wilson5cf3d282016-08-04 07:52:43 +01001684void i915_vma_move_to_active(struct i915_vma *vma,
1685 struct drm_i915_gem_request *req,
1686 unsigned int flags)
1687{
1688 struct drm_i915_gem_object *obj = vma->obj;
1689 const unsigned int idx = req->engine->id;
1690
Chris Wilson81147b02016-12-18 15:37:18 +00001691 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001692 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1693
Chris Wilson2889caa2017-06-16 15:05:19 +01001694 /*
1695 * Add a reference if we're newly entering the active list.
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001696 * The order in which we add operations to the retirement queue is
1697 * vital here: mark_active adds to the start of the callback list,
1698 * such that subsequent callbacks are called first. Therefore we
1699 * add the active reference first and queue for it to be dropped
1700 * *last*.
1701 */
Chris Wilsond07f0e52016-10-28 13:58:44 +01001702 if (!i915_vma_is_active(vma))
1703 obj->active_count++;
1704 i915_vma_set_active(vma, idx);
1705 i915_gem_active_set(&vma->last_read[idx], req);
1706 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001707
Chris Wilsone27ab732017-06-15 13:38:49 +01001708 obj->base.write_domain = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001709 if (flags & EXEC_OBJECT_WRITE) {
Chris Wilsone27ab732017-06-15 13:38:49 +01001710 obj->base.write_domain = I915_GEM_DOMAIN_RENDER;
1711
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00001712 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
1713 i915_gem_active_set(&obj->frontbuffer_write, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001714
Chris Wilsone27ab732017-06-15 13:38:49 +01001715 obj->base.read_domains = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001716 }
Chris Wilsone27ab732017-06-15 13:38:49 +01001717 obj->base.read_domains |= I915_GEM_GPU_DOMAINS;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001718
Chris Wilson49ef5292016-08-18 17:17:00 +01001719 if (flags & EXEC_OBJECT_NEEDS_FENCE)
1720 i915_gem_active_set(&vma->last_fence, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001721}
1722
Chris Wilson2889caa2017-06-16 15:05:19 +01001723static int i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001724{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001725 u32 *cs;
1726 int i;
Eric Anholtae662d32012-01-03 09:23:29 -08001727
Chris Wilsonb5321f32016-08-02 22:50:18 +01001728 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001729 DRM_DEBUG("sol reset is gen7/rcs only\n");
1730 return -EINVAL;
1731 }
Eric Anholtae662d32012-01-03 09:23:29 -08001732
Chris Wilson2889caa2017-06-16 15:05:19 +01001733 cs = intel_ring_begin(req, 4 * 2 + 2);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001734 if (IS_ERR(cs))
1735 return PTR_ERR(cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001736
Chris Wilson2889caa2017-06-16 15:05:19 +01001737 *cs++ = MI_LOAD_REGISTER_IMM(4);
Eric Anholtae662d32012-01-03 09:23:29 -08001738 for (i = 0; i < 4; i++) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001739 *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
1740 *cs++ = 0;
Eric Anholtae662d32012-01-03 09:23:29 -08001741 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001742 *cs++ = MI_NOOP;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001743 intel_ring_advance(req, cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001744
1745 return 0;
1746}
1747
Chris Wilson650bc632017-06-15 09:14:33 +01001748static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001749{
Brad Volkin71745372014-12-11 12:13:12 -08001750 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001751 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001752 int err;
Brad Volkin71745372014-12-11 12:13:12 -08001753
Chris Wilson650bc632017-06-15 09:14:33 +01001754 shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool,
1755 PAGE_ALIGN(eb->batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001756 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001757 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001758
Chris Wilson2889caa2017-06-16 15:05:19 +01001759 err = intel_engine_cmd_parser(eb->engine,
Chris Wilson650bc632017-06-15 09:14:33 +01001760 eb->batch->obj,
Chris Wilson33a051a2016-07-27 09:07:26 +01001761 shadow_batch_obj,
Chris Wilson650bc632017-06-15 09:14:33 +01001762 eb->batch_start_offset,
1763 eb->batch_len,
Chris Wilson33a051a2016-07-27 09:07:26 +01001764 is_master);
Chris Wilson2889caa2017-06-16 15:05:19 +01001765 if (err) {
1766 if (err == -EACCES) /* unhandled chained batch */
Chris Wilson058d88c2016-08-15 10:49:06 +01001767 vma = NULL;
1768 else
Chris Wilson2889caa2017-06-16 15:05:19 +01001769 vma = ERR_PTR(err);
Chris Wilson058d88c2016-08-15 10:49:06 +01001770 goto out;
1771 }
Brad Volkin71745372014-12-11 12:13:12 -08001772
Chris Wilson058d88c2016-08-15 10:49:06 +01001773 vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
1774 if (IS_ERR(vma))
1775 goto out;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001776
Chris Wilson650bc632017-06-15 09:14:33 +01001777 vma->exec_entry =
Chris Wilson2889caa2017-06-16 15:05:19 +01001778 memset(&eb->exec[eb->buffer_count++],
1779 0, sizeof(*vma->exec_entry));
Chris Wilsondade2a62017-06-16 15:05:20 +01001780 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF;
Chris Wilson2889caa2017-06-16 15:05:19 +01001781 __exec_to_vma(vma->exec_entry) = (uintptr_t)i915_vma_get(vma);
Brad Volkin71745372014-12-11 12:13:12 -08001782
Chris Wilson058d88c2016-08-15 10:49:06 +01001783out:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001784 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson058d88c2016-08-15 10:49:06 +01001785 return vma;
Brad Volkin71745372014-12-11 12:13:12 -08001786}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001787
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001788static void
Chris Wilson2889caa2017-06-16 15:05:19 +01001789add_to_client(struct drm_i915_gem_request *req, struct drm_file *file)
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001790{
1791 req->file_priv = file->driver_priv;
1792 list_add_tail(&req->client_link, &req->file_priv->mm.request_list);
1793}
1794
Chris Wilson2889caa2017-06-16 15:05:19 +01001795static int eb_submit(struct i915_execbuffer *eb)
Oscar Mateo78382592014-07-03 16:28:05 +01001796{
Chris Wilson2889caa2017-06-16 15:05:19 +01001797 int err;
Oscar Mateo78382592014-07-03 16:28:05 +01001798
Chris Wilson2889caa2017-06-16 15:05:19 +01001799 err = eb_move_to_gpu(eb);
1800 if (err)
1801 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001802
Chris Wilson2889caa2017-06-16 15:05:19 +01001803 err = i915_switch_context(eb->request);
1804 if (err)
1805 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001806
Chris Wilson650bc632017-06-15 09:14:33 +01001807 if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001808 err = i915_reset_gen7_sol_offsets(eb->request);
1809 if (err)
1810 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001811 }
1812
Chris Wilson2889caa2017-06-16 15:05:19 +01001813 err = eb->engine->emit_bb_start(eb->request,
Chris Wilson650bc632017-06-15 09:14:33 +01001814 eb->batch->node.start +
1815 eb->batch_start_offset,
1816 eb->batch_len,
Chris Wilson2889caa2017-06-16 15:05:19 +01001817 eb->batch_flags);
1818 if (err)
1819 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001820
Chris Wilson2f5945b2015-10-06 11:39:55 +01001821 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001822}
1823
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001824/**
1825 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01001826 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001827 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001828static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01001829gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
1830 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001831{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001832 struct drm_i915_file_private *file_priv = file->driver_priv;
1833
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001834 /* Check whether the file_priv has already selected one ring. */
Joonas Lahtinen6f633402016-09-01 14:58:21 +03001835 if ((int)file_priv->bsd_engine < 0)
1836 file_priv->bsd_engine = atomic_fetch_xor(1,
1837 &dev_priv->mm.bsd_engine_dispatch_index);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001838
Chris Wilsonc80ff162016-07-27 09:07:27 +01001839 return file_priv->bsd_engine;
Chris Wilsond23db882014-05-23 08:48:08 +02001840}
1841
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001842#define I915_USER_RINGS (4)
1843
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00001844static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001845 [I915_EXEC_DEFAULT] = RCS,
1846 [I915_EXEC_RENDER] = RCS,
1847 [I915_EXEC_BLT] = BCS,
1848 [I915_EXEC_BSD] = VCS,
1849 [I915_EXEC_VEBOX] = VECS
1850};
1851
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001852static struct intel_engine_cs *
1853eb_select_engine(struct drm_i915_private *dev_priv,
1854 struct drm_file *file,
1855 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001856{
1857 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001858 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001859
1860 if (user_ring_id > I915_USER_RINGS) {
1861 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001862 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001863 }
1864
1865 if ((user_ring_id != I915_EXEC_BSD) &&
1866 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1867 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1868 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001869 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001870 }
1871
1872 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
1873 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
1874
1875 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01001876 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001877 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
1878 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00001879 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001880 bsd_idx--;
1881 } else {
1882 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
1883 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001884 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001885 }
1886
Akash Goel3b3f1652016-10-13 22:44:48 +05301887 engine = dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001888 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +05301889 engine = dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001890 }
1891
Akash Goel3b3f1652016-10-13 22:44:48 +05301892 if (!engine) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001893 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001894 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001895 }
1896
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001897 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001898}
1899
Eric Anholtae662d32012-01-03 09:23:29 -08001900static int
Chris Wilson650bc632017-06-15 09:14:33 +01001901i915_gem_do_execbuffer(struct drm_device *dev,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001902 struct drm_file *file,
1903 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001904 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001905{
Chris Wilson650bc632017-06-15 09:14:33 +01001906 struct i915_execbuffer eb;
Chris Wilsonfec04452017-01-27 09:40:08 +00001907 struct dma_fence *in_fence = NULL;
1908 struct sync_file *out_fence = NULL;
1909 int out_fence_fd = -1;
Chris Wilson2889caa2017-06-16 15:05:19 +01001910 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001911
Chris Wilson2889caa2017-06-16 15:05:19 +01001912 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
1913 ~__EXEC_OBJECT_UNKNOWN_FLAGS);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001914
Chris Wilson650bc632017-06-15 09:14:33 +01001915 eb.i915 = to_i915(dev);
1916 eb.file = file;
1917 eb.args = args;
Chris Wilson2889caa2017-06-16 15:05:19 +01001918 if (!(args->flags & I915_EXEC_NO_RELOC))
1919 args->flags |= __EXEC_HAS_RELOC;
Chris Wilson650bc632017-06-15 09:14:33 +01001920 eb.exec = exec;
Chris Wilson2889caa2017-06-16 15:05:19 +01001921 eb.ctx = NULL;
1922 eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
1923 if (USES_FULL_PPGTT(eb.i915))
1924 eb.invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson650bc632017-06-15 09:14:33 +01001925 reloc_cache_init(&eb.reloc_cache, eb.i915);
1926
Chris Wilson2889caa2017-06-16 15:05:19 +01001927 eb.buffer_count = args->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +01001928 eb.batch_start_offset = args->batch_start_offset;
1929 eb.batch_len = args->batch_len;
1930
Chris Wilson2889caa2017-06-16 15:05:19 +01001931 eb.batch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001932 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02001933 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001934 return -EPERM;
1935
Chris Wilson2889caa2017-06-16 15:05:19 +01001936 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001937 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001938 if (args->flags & I915_EXEC_IS_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +01001939 eb.batch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001940
Chris Wilson650bc632017-06-15 09:14:33 +01001941 eb.engine = eb_select_engine(eb.i915, file, args);
1942 if (!eb.engine)
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001943 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001944
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001945 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
Chris Wilson650bc632017-06-15 09:14:33 +01001946 if (!HAS_RESOURCE_STREAMER(eb.i915)) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001947 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
1948 return -EINVAL;
1949 }
Chris Wilson650bc632017-06-15 09:14:33 +01001950 if (eb.engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001951 DRM_DEBUG("RS is not available on %s\n",
Chris Wilson650bc632017-06-15 09:14:33 +01001952 eb.engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001953 return -EINVAL;
1954 }
1955
Chris Wilson2889caa2017-06-16 15:05:19 +01001956 eb.batch_flags |= I915_DISPATCH_RS;
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001957 }
1958
Chris Wilsonfec04452017-01-27 09:40:08 +00001959 if (args->flags & I915_EXEC_FENCE_IN) {
1960 in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08001961 if (!in_fence)
1962 return -EINVAL;
Chris Wilsonfec04452017-01-27 09:40:08 +00001963 }
1964
1965 if (args->flags & I915_EXEC_FENCE_OUT) {
1966 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
1967 if (out_fence_fd < 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001968 err = out_fence_fd;
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08001969 goto err_in_fence;
Chris Wilsonfec04452017-01-27 09:40:08 +00001970 }
1971 }
1972
Chris Wilson2889caa2017-06-16 15:05:19 +01001973 if (eb_create(&eb))
1974 return -ENOMEM;
1975
1976 /*
1977 * Take a local wakeref for preparing to dispatch the execbuf as
Chris Wilson67d97da2016-07-04 08:08:31 +01001978 * we expect to access the hardware fairly frequently in the
1979 * process. Upon first dispatch, we acquire another prolonged
1980 * wakeref that we hold until the GPU has been idle for at least
1981 * 100ms.
1982 */
Chris Wilson650bc632017-06-15 09:14:33 +01001983 intel_runtime_pm_get(eb.i915);
Chris Wilson2889caa2017-06-16 15:05:19 +01001984 err = i915_mutex_lock_interruptible(dev);
1985 if (err)
1986 goto err_rpm;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001987
Chris Wilson2889caa2017-06-16 15:05:19 +01001988 err = eb_select_context(&eb);
1989 if (unlikely(err))
1990 goto err_unlock;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001991
Chris Wilson2889caa2017-06-16 15:05:19 +01001992 err = eb_relocate(&eb);
1993 if (err)
1994 /*
1995 * If the user expects the execobject.offset and
1996 * reloc.presumed_offset to be an exact match,
1997 * as for using NO_RELOC, then we cannot update
1998 * the execobject.offset until we have completed
1999 * relocation.
2000 */
2001 args->flags &= ~__EXEC_HAS_RELOC;
2002 if (err < 0)
2003 goto err_vma;
Ben Widawsky41bde552013-12-06 14:11:21 -08002004
Chris Wilson2889caa2017-06-16 15:05:19 +01002005 if (unlikely(eb.batch->exec_entry->flags & EXEC_OBJECT_WRITE)) {
Daniel Vetterff240192012-01-31 21:08:14 +01002006 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002007 err = -EINVAL;
2008 goto err_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002009 }
Chris Wilson650bc632017-06-15 09:14:33 +01002010 if (eb.batch_start_offset > eb.batch->size ||
2011 eb.batch_len > eb.batch->size - eb.batch_start_offset) {
Chris Wilson0b537272016-08-18 17:17:12 +01002012 DRM_DEBUG("Attempting to use out-of-bounds batch\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002013 err = -EINVAL;
2014 goto err_vma;
Chris Wilson0b537272016-08-18 17:17:12 +01002015 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002016
Chris Wilson650bc632017-06-15 09:14:33 +01002017 if (eb.engine->needs_cmd_parser && eb.batch_len) {
Chris Wilson59bfa122016-08-04 16:32:31 +01002018 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002019
Chris Wilson650bc632017-06-15 09:14:33 +01002020 vma = eb_parse(&eb, drm_is_current_master(file));
Chris Wilson59bfa122016-08-04 16:32:31 +01002021 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002022 err = PTR_ERR(vma);
2023 goto err_vma;
Brad Volkin78a42372014-12-11 12:13:09 -08002024 }
Chris Wilson17cabf52015-01-14 11:20:57 +00002025
Chris Wilson59bfa122016-08-04 16:32:31 +01002026 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002027 /*
2028 * Batch parsed and accepted:
2029 *
2030 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
2031 * bit from MI_BATCH_BUFFER_START commands issued in
2032 * the dispatch_execbuffer implementations. We
2033 * specifically don't want that set on batches the
2034 * command parser has accepted.
2035 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002036 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilson650bc632017-06-15 09:14:33 +01002037 eb.batch_start_offset = 0;
2038 eb.batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002039 }
Brad Volkin351e3db2014-02-18 10:15:46 -08002040 }
2041
Chris Wilson650bc632017-06-15 09:14:33 +01002042 if (eb.batch_len == 0)
2043 eb.batch_len = eb.batch->size - eb.batch_start_offset;
Brad Volkin78a42372014-12-11 12:13:09 -08002044
Chris Wilson2889caa2017-06-16 15:05:19 +01002045 /*
2046 * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002047 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07002048 * hsw should have this fixed, but bdw mucks it up again. */
Chris Wilson2889caa2017-06-16 15:05:19 +01002049 if (eb.batch_flags & I915_DISPATCH_SECURE) {
Chris Wilson058d88c2016-08-15 10:49:06 +01002050 struct i915_vma *vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002051
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002052 /*
2053 * So on first glance it looks freaky that we pin the batch here
2054 * outside of the reservation loop. But:
2055 * - The batch is already pinned into the relevant ppgtt, so we
2056 * already have the backing storage fully allocated.
2057 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01002058 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002059 * fitting due to fragmentation.
2060 * So this is actually safe.
2061 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002062 vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0);
Chris Wilson058d88c2016-08-15 10:49:06 +01002063 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002064 err = PTR_ERR(vma);
2065 goto err_vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01002066 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002067
Chris Wilson650bc632017-06-15 09:14:33 +01002068 eb.batch = vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002069 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002070
John Harrison0c8dac82015-05-29 17:43:25 +01002071 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson650bc632017-06-15 09:14:33 +01002072 eb.request = i915_gem_request_alloc(eb.engine, eb.ctx);
2073 if (IS_ERR(eb.request)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002074 err = PTR_ERR(eb.request);
John Harrison0c8dac82015-05-29 17:43:25 +01002075 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00002076 }
John Harrison0c8dac82015-05-29 17:43:25 +01002077
Chris Wilsonfec04452017-01-27 09:40:08 +00002078 if (in_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002079 err = i915_gem_request_await_dma_fence(eb.request, in_fence);
2080 if (err < 0)
Chris Wilsonfec04452017-01-27 09:40:08 +00002081 goto err_request;
2082 }
2083
2084 if (out_fence_fd != -1) {
Chris Wilson650bc632017-06-15 09:14:33 +01002085 out_fence = sync_file_create(&eb.request->fence);
Chris Wilsonfec04452017-01-27 09:40:08 +00002086 if (!out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002087 err = -ENOMEM;
Chris Wilsonfec04452017-01-27 09:40:08 +00002088 goto err_request;
2089 }
2090 }
2091
Chris Wilson2889caa2017-06-16 15:05:19 +01002092 /*
2093 * Whilst this request exists, batch_obj will be on the
Chris Wilson17f298cf2016-08-10 13:41:46 +01002094 * active_list, and so will hold the active reference. Only when this
2095 * request is retired will the the batch_obj be moved onto the
2096 * inactive_list and lose its active reference. Hence we do not need
2097 * to explicitly hold another reference here.
2098 */
Chris Wilson650bc632017-06-15 09:14:33 +01002099 eb.request->batch = eb.batch;
Chris Wilson17f298cf2016-08-10 13:41:46 +01002100
Chris Wilson2889caa2017-06-16 15:05:19 +01002101 trace_i915_gem_request_queue(eb.request, eb.batch_flags);
2102 err = eb_submit(&eb);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01002103err_request:
Chris Wilson2889caa2017-06-16 15:05:19 +01002104 __i915_add_request(eb.request, err == 0);
Chris Wilson650bc632017-06-15 09:14:33 +01002105 add_to_client(eb.request, file);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002106
Chris Wilsonfec04452017-01-27 09:40:08 +00002107 if (out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002108 if (err == 0) {
Chris Wilsonfec04452017-01-27 09:40:08 +00002109 fd_install(out_fence_fd, out_fence->file);
2110 args->rsvd2 &= GENMASK_ULL(0, 31); /* keep in-fence */
2111 args->rsvd2 |= (u64)out_fence_fd << 32;
2112 out_fence_fd = -1;
2113 } else {
2114 fput(out_fence->file);
2115 }
2116 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002117
John Harrison0c8dac82015-05-29 17:43:25 +01002118err_batch_unpin:
Chris Wilson2889caa2017-06-16 15:05:19 +01002119 if (eb.batch_flags & I915_DISPATCH_SECURE)
Chris Wilson650bc632017-06-15 09:14:33 +01002120 i915_vma_unpin(eb.batch);
Chris Wilson2889caa2017-06-16 15:05:19 +01002121err_vma:
2122 if (eb.exec)
2123 eb_release_vmas(&eb);
2124 i915_gem_context_put(eb.ctx);
2125err_unlock:
Chris Wilson54cf91d2010-11-25 18:00:26 +00002126 mutex_unlock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01002127err_rpm:
Chris Wilson650bc632017-06-15 09:14:33 +01002128 intel_runtime_pm_put(eb.i915);
Chris Wilson2889caa2017-06-16 15:05:19 +01002129 eb_destroy(&eb);
Chris Wilsonfec04452017-01-27 09:40:08 +00002130 if (out_fence_fd != -1)
2131 put_unused_fd(out_fence_fd);
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002132err_in_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002133 dma_fence_put(in_fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01002134 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002135}
2136
2137/*
2138 * Legacy execbuffer just creates an exec2 list from the original exec object
2139 * list array and passes it to the real function.
2140 */
2141int
2142i915_gem_execbuffer(struct drm_device *dev, void *data,
2143 struct drm_file *file)
2144{
Chris Wilson2889caa2017-06-16 15:05:19 +01002145 const size_t sz = sizeof(struct drm_i915_gem_exec_object2);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002146 struct drm_i915_gem_execbuffer *args = data;
2147 struct drm_i915_gem_execbuffer2 exec2;
2148 struct drm_i915_gem_exec_object *exec_list = NULL;
2149 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +01002150 unsigned int i;
2151 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002152
Chris Wilson2889caa2017-06-16 15:05:19 +01002153 if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) {
2154 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002155 return -EINVAL;
2156 }
2157
Chris Wilson2889caa2017-06-16 15:05:19 +01002158 exec2.buffers_ptr = args->buffers_ptr;
2159 exec2.buffer_count = args->buffer_count;
2160 exec2.batch_start_offset = args->batch_start_offset;
2161 exec2.batch_len = args->batch_len;
2162 exec2.DR1 = args->DR1;
2163 exec2.DR4 = args->DR4;
2164 exec2.num_cliprects = args->num_cliprects;
2165 exec2.cliprects_ptr = args->cliprects_ptr;
2166 exec2.flags = I915_EXEC_RENDER;
2167 i915_execbuffer2_set_context_id(exec2, 0);
2168
2169 if (!i915_gem_check_execbuffer(&exec2))
2170 return -EINVAL;
2171
Chris Wilson54cf91d2010-11-25 18:00:26 +00002172 /* Copy in the exec list from userland */
Chris Wilson2889caa2017-06-16 15:05:19 +01002173 exec_list = kvmalloc_array(args->buffer_count, sizeof(*exec_list),
2174 __GFP_NOWARN | GFP_TEMPORARY);
2175 exec2_list = kvmalloc_array(args->buffer_count + 1, sz,
2176 __GFP_NOWARN | GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002177 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002178 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002179 args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002180 kvfree(exec_list);
2181 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002182 return -ENOMEM;
2183 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002184 err = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002185 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00002186 sizeof(*exec_list) * args->buffer_count);
Chris Wilson2889caa2017-06-16 15:05:19 +01002187 if (err) {
Daniel Vetterff240192012-01-31 21:08:14 +01002188 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson2889caa2017-06-16 15:05:19 +01002189 args->buffer_count, err);
Michal Hocko20981052017-05-17 14:23:12 +02002190 kvfree(exec_list);
2191 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002192 return -EFAULT;
2193 }
2194
2195 for (i = 0; i < args->buffer_count; i++) {
2196 exec2_list[i].handle = exec_list[i].handle;
2197 exec2_list[i].relocation_count = exec_list[i].relocation_count;
2198 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
2199 exec2_list[i].alignment = exec_list[i].alignment;
2200 exec2_list[i].offset = exec_list[i].offset;
Tvrtko Ursulinf0836b72016-11-16 08:55:32 +00002201 if (INTEL_GEN(to_i915(dev)) < 4)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002202 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
2203 else
2204 exec2_list[i].flags = 0;
2205 }
2206
Chris Wilson2889caa2017-06-16 15:05:19 +01002207 err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list);
2208 if (exec2.flags & __EXEC_HAS_RELOC) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002209 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002210 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002211
Chris Wilson54cf91d2010-11-25 18:00:26 +00002212 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002213 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002214 if (!(exec2_list[i].offset & UPDATE))
2215 continue;
2216
Michał Winiarski934acce2015-12-29 18:24:52 +01002217 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002218 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2219 exec2_list[i].offset &= PIN_OFFSET_MASK;
2220 if (__copy_to_user(&user_exec_list[i].offset,
2221 &exec2_list[i].offset,
2222 sizeof(user_exec_list[i].offset)))
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002223 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002224 }
2225 }
2226
Michal Hocko20981052017-05-17 14:23:12 +02002227 kvfree(exec_list);
2228 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002229 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002230}
2231
2232int
2233i915_gem_execbuffer2(struct drm_device *dev, void *data,
2234 struct drm_file *file)
2235{
Chris Wilson2889caa2017-06-16 15:05:19 +01002236 const size_t sz = sizeof(struct drm_i915_gem_exec_object2);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002237 struct drm_i915_gem_execbuffer2 *args = data;
Chris Wilson2889caa2017-06-16 15:05:19 +01002238 struct drm_i915_gem_exec_object2 *exec2_list;
2239 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002240
Chris Wilson2889caa2017-06-16 15:05:19 +01002241 if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01002242 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002243 return -EINVAL;
2244 }
2245
Chris Wilson2889caa2017-06-16 15:05:19 +01002246 if (!i915_gem_check_execbuffer(args))
2247 return -EINVAL;
2248
2249 /* Allocate an extra slot for use by the command parser */
2250 exec2_list = kvmalloc_array(args->buffer_count + 1, sz,
2251 __GFP_NOWARN | GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002252 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002253 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002254 args->buffer_count);
2255 return -ENOMEM;
2256 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002257 if (copy_from_user(exec2_list,
2258 u64_to_user_ptr(args->buffers_ptr),
2259 sizeof(*exec2_list) * args->buffer_count)) {
2260 DRM_DEBUG("copy %d exec entries failed\n", args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002261 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002262 return -EFAULT;
2263 }
2264
Chris Wilson2889caa2017-06-16 15:05:19 +01002265 err = i915_gem_do_execbuffer(dev, file, args, exec2_list);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002266
Chris Wilson2889caa2017-06-16 15:05:19 +01002267 /*
2268 * Now that we have begun execution of the batchbuffer, we ignore
2269 * any new error after this point. Also given that we have already
2270 * updated the associated relocations, we try to write out the current
2271 * object locations irrespective of any error.
2272 */
2273 if (args->flags & __EXEC_HAS_RELOC) {
2274 struct drm_i915_gem_exec_object2 __user *user_exec_list =
2275 u64_to_user_ptr(args->buffers_ptr);
2276 unsigned int i;
2277
2278 /* Copy the new buffer offsets back to the user's exec list. */
2279 user_access_begin();
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002280 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002281 if (!(exec2_list[i].offset & UPDATE))
2282 continue;
2283
Michał Winiarski934acce2015-12-29 18:24:52 +01002284 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002285 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2286 unsafe_put_user(exec2_list[i].offset,
2287 &user_exec_list[i].offset,
2288 end_user);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002289 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002290end_user:
2291 user_access_end();
Chris Wilson54cf91d2010-11-25 18:00:26 +00002292 }
2293
Chris Wilson2889caa2017-06-16 15:05:19 +01002294 args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
Michal Hocko20981052017-05-17 14:23:12 +02002295 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002296 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002297}