blob: 044fb1205554e3687f5fb99dcd49ea136271ca3e [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>
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +010035#include <drm/drm_syncobj.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000036#include <drm/i915_drm.h>
Chris Wilsonad778f82016-08-04 16:32:42 +010037
Chris Wilson54cf91d2010-11-25 18:00:26 +000038#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000039#include "i915_gem_clflush.h"
Chris Wilson54cf91d2010-11-25 18:00:26 +000040#include "i915_trace.h"
41#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010042#include "intel_frontbuffer.h"
Chris Wilson54cf91d2010-11-25 18:00:26 +000043
Chris Wilson7dd4f672017-06-16 15:05:24 +010044enum {
45 FORCE_CPU_RELOC = 1,
46 FORCE_GTT_RELOC,
47 FORCE_GPU_RELOC,
48#define DBG_FORCE_RELOC 0 /* choose one of the above! */
49};
Chris Wilsond50415c2016-08-18 17:16:52 +010050
Chris Wilsondade2a62017-06-16 15:05:20 +010051#define __EXEC_OBJECT_HAS_REF BIT(31)
52#define __EXEC_OBJECT_HAS_PIN BIT(30)
53#define __EXEC_OBJECT_HAS_FENCE BIT(29)
54#define __EXEC_OBJECT_NEEDS_MAP BIT(28)
55#define __EXEC_OBJECT_NEEDS_BIAS BIT(27)
56#define __EXEC_OBJECT_INTERNAL_FLAGS (~0u << 27) /* all of the above */
Chris Wilson2889caa2017-06-16 15:05:19 +010057#define __EXEC_OBJECT_RESERVED (__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_FENCE)
58
59#define __EXEC_HAS_RELOC BIT(31)
60#define __EXEC_VALIDATED BIT(30)
61#define UPDATE PIN_OFFSET_FIXED
Chris Wilsond23db882014-05-23 08:48:08 +020062
63#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000064
Chris Wilson650bc632017-06-15 09:14:33 +010065#define __I915_EXEC_ILLEGAL_FLAGS \
66 (__I915_EXEC_UNKNOWN_FLAGS | I915_EXEC_CONSTANTS_MASK)
Chris Wilson5b043f42016-08-02 22:50:38 +010067
Chris Wilson2889caa2017-06-16 15:05:19 +010068/**
69 * DOC: User command execution
70 *
71 * Userspace submits commands to be executed on the GPU as an instruction
72 * stream within a GEM object we call a batchbuffer. This instructions may
73 * refer to other GEM objects containing auxiliary state such as kernels,
74 * samplers, render targets and even secondary batchbuffers. Userspace does
75 * not know where in the GPU memory these objects reside and so before the
76 * batchbuffer is passed to the GPU for execution, those addresses in the
77 * batchbuffer and auxiliary objects are updated. This is known as relocation,
78 * or patching. To try and avoid having to relocate each object on the next
79 * execution, userspace is told the location of those objects in this pass,
80 * but this remains just a hint as the kernel may choose a new location for
81 * any object in the future.
82 *
83 * Processing an execbuf ioctl is conceptually split up into a few phases.
84 *
85 * 1. Validation - Ensure all the pointers, handles and flags are valid.
86 * 2. Reservation - Assign GPU address space for every object
87 * 3. Relocation - Update any addresses to point to the final locations
88 * 4. Serialisation - Order the request with respect to its dependencies
89 * 5. Construction - Construct a request to execute the batchbuffer
90 * 6. Submission (at some point in the future execution)
91 *
92 * Reserving resources for the execbuf is the most complicated phase. We
93 * neither want to have to migrate the object in the address space, nor do
94 * we want to have to update any relocations pointing to this object. Ideally,
95 * we want to leave the object where it is and for all the existing relocations
96 * to match. If the object is given a new address, or if userspace thinks the
97 * object is elsewhere, we have to parse all the relocation entries and update
98 * the addresses. Userspace can set the I915_EXEC_NORELOC flag to hint that
99 * all the target addresses in all of its objects match the value in the
100 * relocation entries and that they all match the presumed offsets given by the
101 * list of execbuffer objects. Using this knowledge, we know that if we haven't
102 * moved any buffers, all the relocation entries are valid and we can skip
103 * the update. (If userspace is wrong, the likely outcome is an impromptu GPU
104 * hang.) The requirement for using I915_EXEC_NO_RELOC are:
105 *
106 * The addresses written in the objects must match the corresponding
107 * reloc.presumed_offset which in turn must match the corresponding
108 * execobject.offset.
109 *
110 * Any render targets written to in the batch must be flagged with
111 * EXEC_OBJECT_WRITE.
112 *
113 * To avoid stalling, execobject.offset should match the current
114 * address of that object within the active context.
115 *
116 * The reservation is done is multiple phases. First we try and keep any
117 * object already bound in its current location - so as long as meets the
118 * constraints imposed by the new execbuffer. Any object left unbound after the
119 * first pass is then fitted into any available idle space. If an object does
120 * not fit, all objects are removed from the reservation and the process rerun
121 * after sorting the objects into a priority order (more difficult to fit
122 * objects are tried first). Failing that, the entire VM is cleared and we try
123 * to fit the execbuf once last time before concluding that it simply will not
124 * fit.
125 *
126 * A small complication to all of this is that we allow userspace not only to
127 * specify an alignment and a size for the object in the address space, but
128 * we also allow userspace to specify the exact offset. This objects are
129 * simpler to place (the location is known a priori) all we have to do is make
130 * sure the space is available.
131 *
132 * Once all the objects are in place, patching up the buried pointers to point
133 * to the final locations is a fairly simple job of walking over the relocation
134 * entry arrays, looking up the right address and rewriting the value into
135 * the object. Simple! ... The relocation entries are stored in user memory
136 * and so to access them we have to copy them into a local buffer. That copy
137 * has to avoid taking any pagefaults as they may lead back to a GEM object
138 * requiring the struct_mutex (i.e. recursive deadlock). So once again we split
139 * the relocation into multiple passes. First we try to do everything within an
140 * atomic context (avoid the pagefaults) which requires that we never wait. If
141 * we detect that we may wait, or if we need to fault, then we have to fallback
142 * to a slower path. The slowpath has to drop the mutex. (Can you hear alarm
143 * bells yet?) Dropping the mutex means that we lose all the state we have
144 * built up so far for the execbuf and we must reset any global data. However,
145 * we do leave the objects pinned in their final locations - which is a
146 * potential issue for concurrent execbufs. Once we have left the mutex, we can
147 * allocate and copy all the relocation entries into a large array at our
148 * leisure, reacquire the mutex, reclaim all the objects and other state and
149 * then proceed to update any incorrect addresses with the objects.
150 *
151 * As we process the relocation entries, we maintain a record of whether the
152 * object is being written to. Using NORELOC, we expect userspace to provide
153 * this information instead. We also check whether we can skip the relocation
154 * by comparing the expected value inside the relocation entry with the target's
155 * final address. If they differ, we have to map the current object and rewrite
156 * the 4 or 8 byte pointer within.
157 *
158 * Serialising an execbuf is quite simple according to the rules of the GEM
159 * ABI. Execution within each context is ordered by the order of submission.
160 * Writes to any GEM object are in order of submission and are exclusive. Reads
161 * from a GEM object are unordered with respect to other reads, but ordered by
162 * writes. A write submitted after a read cannot occur before the read, and
163 * similarly any read submitted after a write cannot occur before the write.
164 * Writes are ordered between engines such that only one write occurs at any
165 * time (completing any reads beforehand) - using semaphores where available
166 * and CPU serialisation otherwise. Other GEM access obey the same rules, any
167 * write (either via mmaps using set-domain, or via pwrite) must flush all GPU
168 * reads before starting, and any read (either using set-domain or pread) must
169 * flush all GPU writes before starting. (Note we only employ a barrier before,
170 * we currently rely on userspace not concurrently starting a new execution
171 * whilst reading or writing to an object. This may be an advantage or not
172 * depending on how much you trust userspace not to shoot themselves in the
173 * foot.) Serialisation may just result in the request being inserted into
174 * a DAG awaiting its turn, but most simple is to wait on the CPU until
175 * all dependencies are resolved.
176 *
177 * After all of that, is just a matter of closing the request and handing it to
178 * the hardware (well, leaving it in a queue to be executed). However, we also
179 * offer the ability for batchbuffers to be run with elevated privileges so
180 * that they access otherwise hidden registers. (Used to adjust L3 cache etc.)
181 * Before any batch is given extra privileges we first must check that it
182 * contains no nefarious instructions, we check that each instruction is from
183 * our whitelist and all registers are also from an allowed list. We first
184 * copy the user's batchbuffer to a shadow (so that the user doesn't have
185 * access to it, either by the CPU or GPU as we scan it) and then parse each
186 * instruction. If everything is ok, we set a flag telling the hardware to run
187 * the batchbuffer in trusted mode, otherwise the ioctl is rejected.
188 */
189
Chris Wilson650bc632017-06-15 09:14:33 +0100190struct i915_execbuffer {
Chris Wilson2889caa2017-06-16 15:05:19 +0100191 struct drm_i915_private *i915; /** i915 backpointer */
192 struct drm_file *file; /** per-file lookup tables and limits */
193 struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */
194 struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */
195
196 struct intel_engine_cs *engine; /** engine to queue the request to */
197 struct i915_gem_context *ctx; /** context for building the request */
198 struct i915_address_space *vm; /** GTT and vma for the request */
199
200 struct drm_i915_gem_request *request; /** our request to build */
201 struct i915_vma *batch; /** identity of the batch obj/vma */
202
203 /** actual size of execobj[] as we may extend it for the cmdparser */
204 unsigned int buffer_count;
205
206 /** list of vma not yet bound during reservation phase */
207 struct list_head unbound;
208
209 /** list of vma that have execobj.relocation_count */
210 struct list_head relocs;
211
212 /**
213 * Track the most recently used object for relocations, as we
214 * frequently have to perform multiple relocations within the same
215 * obj/page
216 */
Chris Wilson650bc632017-06-15 09:14:33 +0100217 struct reloc_cache {
Chris Wilson2889caa2017-06-16 15:05:19 +0100218 struct drm_mm_node node; /** temporary GTT binding */
219 unsigned long vaddr; /** Current kmap address */
220 unsigned long page; /** Currently mapped page index */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100221 unsigned int gen; /** Cached value of INTEL_GEN */
Chris Wilson650bc632017-06-15 09:14:33 +0100222 bool use_64bit_reloc : 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100223 bool has_llc : 1;
224 bool has_fence : 1;
225 bool needs_unfenced : 1;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100226
227 struct drm_i915_gem_request *rq;
228 u32 *rq_cmd;
229 unsigned int rq_size;
Chris Wilson650bc632017-06-15 09:14:33 +0100230 } reloc_cache;
Chris Wilson2889caa2017-06-16 15:05:19 +0100231
232 u64 invalid_flags; /** Set of execobj.flags that are invalid */
233 u32 context_flags; /** Set of execobj.flags to insert from the ctx */
234
235 u32 batch_start_offset; /** Location within object of batch */
236 u32 batch_len; /** Length of batch within object */
237 u32 batch_flags; /** Flags composed for emit_bb_start() */
238
239 /**
240 * Indicate either the size of the hastable used to resolve
241 * relocation handles, or if negative that we are using a direct
242 * index into the execobj[].
243 */
244 int lut_size;
245 struct hlist_head *buckets; /** ht for relocation handles */
Chris Wilson67731b82010-12-08 10:38:14 +0000246};
247
Chris Wilson4ff4b442017-06-16 15:05:16 +0100248/*
249 * As an alternative to creating a hashtable of handle-to-vma for a batch,
250 * we used the last available reserved field in the execobject[] and stash
251 * a link from the execobj to its vma.
252 */
253#define __exec_to_vma(ee) (ee)->rsvd2
254#define exec_to_vma(ee) u64_to_ptr(struct i915_vma, __exec_to_vma(ee))
255
Chris Wilson2889caa2017-06-16 15:05:19 +0100256/*
257 * Used to convert any address to canonical form.
258 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
259 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
260 * addresses to be in a canonical form:
261 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
262 * canonical form [63:48] == [47]."
263 */
264#define GEN8_HIGH_ADDRESS_BIT 47
265static inline u64 gen8_canonical_addr(u64 address)
266{
267 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
268}
269
270static inline u64 gen8_noncanonical_addr(u64 address)
271{
272 return address & GENMASK_ULL(GEN8_HIGH_ADDRESS_BIT, 0);
273}
274
Chris Wilson650bc632017-06-15 09:14:33 +0100275static int eb_create(struct i915_execbuffer *eb)
Chris Wilson67731b82010-12-08 10:38:14 +0000276{
Chris Wilson2889caa2017-06-16 15:05:19 +0100277 if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) {
278 unsigned int size = 1 + ilog2(eb->buffer_count);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000279
Chris Wilson2889caa2017-06-16 15:05:19 +0100280 /*
281 * Without a 1:1 association between relocation handles and
282 * the execobject[] index, we instead create a hashtable.
283 * We size it dynamically based on available memory, starting
284 * first with 1:1 assocative hash and scaling back until
285 * the allocation succeeds.
286 *
287 * Later on we use a positive lut_size to indicate we are
288 * using this hashtable, and a negative value to indicate a
289 * direct lookup.
290 */
Chris Wilson4ff4b442017-06-16 15:05:16 +0100291 do {
Chris Wilson4d470f72017-06-29 16:04:25 +0100292 unsigned int flags;
293
294 /* While we can still reduce the allocation size, don't
295 * raise a warning and allow the allocation to fail.
296 * On the last pass though, we want to try as hard
297 * as possible to perform the allocation and warn
298 * if it fails.
299 */
300 flags = GFP_TEMPORARY;
301 if (size > 1)
302 flags |= __GFP_NORETRY | __GFP_NOWARN;
303
Chris Wilson4ff4b442017-06-16 15:05:16 +0100304 eb->buckets = kzalloc(sizeof(struct hlist_head) << size,
Chris Wilson4d470f72017-06-29 16:04:25 +0100305 flags);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100306 if (eb->buckets)
307 break;
308 } while (--size);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000309
Chris Wilson4d470f72017-06-29 16:04:25 +0100310 if (unlikely(!size))
311 return -ENOMEM;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100312
Chris Wilson2889caa2017-06-16 15:05:19 +0100313 eb->lut_size = size;
Chris Wilson650bc632017-06-15 09:14:33 +0100314 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +0100315 eb->lut_size = -eb->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +0100316 }
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000317
Chris Wilson650bc632017-06-15 09:14:33 +0100318 return 0;
Chris Wilson67731b82010-12-08 10:38:14 +0000319}
320
Chris Wilson2889caa2017-06-16 15:05:19 +0100321static bool
322eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry,
323 const struct i915_vma *vma)
324{
325 if (!(entry->flags & __EXEC_OBJECT_HAS_PIN))
326 return true;
327
328 if (vma->node.size < entry->pad_to_size)
329 return true;
330
331 if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment))
332 return true;
333
334 if (entry->flags & EXEC_OBJECT_PINNED &&
335 vma->node.start != entry->offset)
336 return true;
337
338 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
339 vma->node.start < BATCH_OFFSET_BIAS)
340 return true;
341
342 if (!(entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) &&
343 (vma->node.start + vma->node.size - 1) >> 32)
344 return true;
345
346 return false;
347}
348
349static inline void
350eb_pin_vma(struct i915_execbuffer *eb,
351 struct drm_i915_gem_exec_object2 *entry,
352 struct i915_vma *vma)
353{
354 u64 flags;
355
Chris Wilson616d9ce2017-06-16 15:05:21 +0100356 if (vma->node.size)
357 flags = vma->node.start;
358 else
359 flags = entry->offset & PIN_OFFSET_MASK;
360
361 flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED;
Chris Wilson2889caa2017-06-16 15:05:19 +0100362 if (unlikely(entry->flags & EXEC_OBJECT_NEEDS_GTT))
363 flags |= PIN_GLOBAL;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100364
Chris Wilson2889caa2017-06-16 15:05:19 +0100365 if (unlikely(i915_vma_pin(vma, 0, 0, flags)))
366 return;
367
368 if (unlikely(entry->flags & EXEC_OBJECT_NEEDS_FENCE)) {
369 if (unlikely(i915_vma_get_fence(vma))) {
370 i915_vma_unpin(vma);
371 return;
372 }
373
374 if (i915_vma_pin_fence(vma))
375 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
376 }
377
378 entry->flags |= __EXEC_OBJECT_HAS_PIN;
379}
380
Chris Wilsond55495b2017-06-15 09:14:34 +0100381static inline void
382__eb_unreserve_vma(struct i915_vma *vma,
383 const struct drm_i915_gem_exec_object2 *entry)
384{
Chris Wilson2889caa2017-06-16 15:05:19 +0100385 GEM_BUG_ON(!(entry->flags & __EXEC_OBJECT_HAS_PIN));
386
Chris Wilsond55495b2017-06-15 09:14:34 +0100387 if (unlikely(entry->flags & __EXEC_OBJECT_HAS_FENCE))
388 i915_vma_unpin_fence(vma);
389
Chris Wilson2889caa2017-06-16 15:05:19 +0100390 __i915_vma_unpin(vma);
Chris Wilsond55495b2017-06-15 09:14:34 +0100391}
392
Chris Wilson2889caa2017-06-16 15:05:19 +0100393static inline void
394eb_unreserve_vma(struct i915_vma *vma,
395 struct drm_i915_gem_exec_object2 *entry)
Chris Wilsond55495b2017-06-15 09:14:34 +0100396{
Chris Wilson2889caa2017-06-16 15:05:19 +0100397 if (!(entry->flags & __EXEC_OBJECT_HAS_PIN))
398 return;
Chris Wilsond55495b2017-06-15 09:14:34 +0100399
400 __eb_unreserve_vma(vma, entry);
Chris Wilson2889caa2017-06-16 15:05:19 +0100401 entry->flags &= ~__EXEC_OBJECT_RESERVED;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100402}
403
404static int
Chris Wilson2889caa2017-06-16 15:05:19 +0100405eb_validate_vma(struct i915_execbuffer *eb,
406 struct drm_i915_gem_exec_object2 *entry,
407 struct i915_vma *vma)
408{
409 if (unlikely(entry->flags & eb->invalid_flags))
410 return -EINVAL;
411
412 if (unlikely(entry->alignment && !is_power_of_2(entry->alignment)))
413 return -EINVAL;
414
415 /*
416 * Offset can be used as input (EXEC_OBJECT_PINNED), reject
417 * any non-page-aligned or non-canonical addresses.
418 */
419 if (unlikely(entry->flags & EXEC_OBJECT_PINNED &&
420 entry->offset != gen8_canonical_addr(entry->offset & PAGE_MASK)))
421 return -EINVAL;
422
423 /* pad_to_size was once a reserved field, so sanitize it */
424 if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) {
425 if (unlikely(offset_in_page(entry->pad_to_size)))
426 return -EINVAL;
427 } else {
428 entry->pad_to_size = 0;
429 }
430
431 if (unlikely(vma->exec_entry)) {
432 DRM_DEBUG("Object [handle %d, index %d] appears more than once in object list\n",
433 entry->handle, (int)(entry - eb->exec));
434 return -EINVAL;
435 }
436
437 /*
438 * From drm_mm perspective address space is continuous,
439 * so from this point we're always using non-canonical
440 * form internally.
441 */
442 entry->offset = gen8_noncanonical_addr(entry->offset);
443
444 return 0;
445}
446
447static int
448eb_add_vma(struct i915_execbuffer *eb,
449 struct drm_i915_gem_exec_object2 *entry,
450 struct i915_vma *vma)
451{
452 int err;
453
454 GEM_BUG_ON(i915_vma_is_closed(vma));
455
456 if (!(eb->args->flags & __EXEC_VALIDATED)) {
457 err = eb_validate_vma(eb, entry, vma);
458 if (unlikely(err))
459 return err;
460 }
461
Chris Wilson4d470f72017-06-29 16:04:25 +0100462 if (eb->lut_size > 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100463 vma->exec_handle = entry->handle;
464 hlist_add_head(&vma->exec_node,
465 &eb->buckets[hash_32(entry->handle,
466 eb->lut_size)]);
467 }
468
469 if (entry->relocation_count)
470 list_add_tail(&vma->reloc_link, &eb->relocs);
471
472 if (!eb->reloc_cache.has_fence) {
473 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
474 } else {
475 if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE ||
476 eb->reloc_cache.needs_unfenced) &&
477 i915_gem_object_is_tiled(vma->obj))
478 entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP;
479 }
480
481 if (!(entry->flags & EXEC_OBJECT_PINNED))
482 entry->flags |= eb->context_flags;
483
484 /*
485 * Stash a pointer from the vma to execobj, so we can query its flags,
486 * size, alignment etc as provided by the user. Also we stash a pointer
487 * to the vma inside the execobj so that we can use a direct lookup
488 * to find the right target VMA when doing relocations.
489 */
490 vma->exec_entry = entry;
Chris Wilsondade2a62017-06-16 15:05:20 +0100491 __exec_to_vma(entry) = (uintptr_t)vma;
Chris Wilson2889caa2017-06-16 15:05:19 +0100492
493 err = 0;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100494 eb_pin_vma(eb, entry, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100495 if (eb_vma_misplaced(entry, vma)) {
496 eb_unreserve_vma(vma, entry);
497
498 list_add_tail(&vma->exec_link, &eb->unbound);
499 if (drm_mm_node_allocated(&vma->node))
500 err = i915_vma_unbind(vma);
501 } else {
502 if (entry->offset != vma->node.start) {
503 entry->offset = vma->node.start | UPDATE;
504 eb->args->flags |= __EXEC_HAS_RELOC;
505 }
506 }
507 return err;
508}
509
510static inline int use_cpu_reloc(const struct reloc_cache *cache,
511 const struct drm_i915_gem_object *obj)
512{
513 if (!i915_gem_object_has_struct_page(obj))
514 return false;
515
Chris Wilson7dd4f672017-06-16 15:05:24 +0100516 if (DBG_FORCE_RELOC == FORCE_CPU_RELOC)
517 return true;
518
519 if (DBG_FORCE_RELOC == FORCE_GTT_RELOC)
520 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100521
522 return (cache->has_llc ||
523 obj->cache_dirty ||
524 obj->cache_level != I915_CACHE_NONE);
525}
526
527static int eb_reserve_vma(const struct i915_execbuffer *eb,
528 struct i915_vma *vma)
529{
530 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
531 u64 flags;
532 int err;
533
534 flags = PIN_USER | PIN_NONBLOCK;
535 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
536 flags |= PIN_GLOBAL;
537
538 /*
539 * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
540 * limit address to the first 4GBs for unflagged objects.
541 */
542 if (!(entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
543 flags |= PIN_ZONE_4G;
544
545 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
546 flags |= PIN_MAPPABLE;
547
548 if (entry->flags & EXEC_OBJECT_PINNED) {
549 flags |= entry->offset | PIN_OFFSET_FIXED;
550 flags &= ~PIN_NONBLOCK; /* force overlapping PINNED checks */
551 } else if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS) {
552 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
553 }
554
555 err = i915_vma_pin(vma, entry->pad_to_size, entry->alignment, flags);
556 if (err)
557 return err;
558
559 if (entry->offset != vma->node.start) {
560 entry->offset = vma->node.start | UPDATE;
561 eb->args->flags |= __EXEC_HAS_RELOC;
562 }
563
Chris Wilson2889caa2017-06-16 15:05:19 +0100564 if (unlikely(entry->flags & EXEC_OBJECT_NEEDS_FENCE)) {
565 err = i915_vma_get_fence(vma);
566 if (unlikely(err)) {
567 i915_vma_unpin(vma);
568 return err;
569 }
570
571 if (i915_vma_pin_fence(vma))
572 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
573 }
574
Chris Wilson1da7b542017-07-21 15:50:35 +0100575 entry->flags |= __EXEC_OBJECT_HAS_PIN;
576 GEM_BUG_ON(eb_vma_misplaced(entry, vma));
577
Chris Wilson2889caa2017-06-16 15:05:19 +0100578 return 0;
579}
580
581static int eb_reserve(struct i915_execbuffer *eb)
582{
583 const unsigned int count = eb->buffer_count;
584 struct list_head last;
585 struct i915_vma *vma;
586 unsigned int i, pass;
587 int err;
588
589 /*
590 * Attempt to pin all of the buffers into the GTT.
591 * This is done in 3 phases:
592 *
593 * 1a. Unbind all objects that do not match the GTT constraints for
594 * the execbuffer (fenceable, mappable, alignment etc).
595 * 1b. Increment pin count for already bound objects.
596 * 2. Bind new objects.
597 * 3. Decrement pin count.
598 *
599 * This avoid unnecessary unbinding of later objects in order to make
600 * room for the earlier objects *unless* we need to defragment.
601 */
602
603 pass = 0;
604 err = 0;
605 do {
606 list_for_each_entry(vma, &eb->unbound, exec_link) {
607 err = eb_reserve_vma(eb, vma);
608 if (err)
609 break;
610 }
611 if (err != -ENOSPC)
612 return err;
613
614 /* Resort *all* the objects into priority order */
615 INIT_LIST_HEAD(&eb->unbound);
616 INIT_LIST_HEAD(&last);
617 for (i = 0; i < count; i++) {
618 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
619
620 if (entry->flags & EXEC_OBJECT_PINNED &&
621 entry->flags & __EXEC_OBJECT_HAS_PIN)
622 continue;
623
624 vma = exec_to_vma(entry);
625 eb_unreserve_vma(vma, entry);
626
627 if (entry->flags & EXEC_OBJECT_PINNED)
628 list_add(&vma->exec_link, &eb->unbound);
629 else if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
630 list_add_tail(&vma->exec_link, &eb->unbound);
631 else
632 list_add_tail(&vma->exec_link, &last);
633 }
634 list_splice_tail(&last, &eb->unbound);
635
636 switch (pass++) {
637 case 0:
638 break;
639
640 case 1:
641 /* Too fragmented, unbind everything and retry */
642 err = i915_gem_evict_vm(eb->vm);
643 if (err)
644 return err;
645 break;
646
647 default:
648 return -ENOSPC;
649 }
650 } while (1);
651}
652
653static inline struct hlist_head *
654ht_head(const struct i915_gem_context_vma_lut *lut, u32 handle)
655{
656 return &lut->ht[hash_32(handle, lut->ht_bits)];
657}
658
659static inline bool
660ht_needs_resize(const struct i915_gem_context_vma_lut *lut)
661{
662 return (4*lut->ht_count > 3*lut->ht_size ||
663 4*lut->ht_count + 1 < lut->ht_size);
664}
665
666static unsigned int eb_batch_index(const struct i915_execbuffer *eb)
667{
Chris Wilson1a71cf22017-06-16 15:05:23 +0100668 if (eb->args->flags & I915_EXEC_BATCH_FIRST)
669 return 0;
670 else
671 return eb->buffer_count - 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100672}
673
674static int eb_select_context(struct i915_execbuffer *eb)
675{
676 struct i915_gem_context *ctx;
677
678 ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1);
Chris Wilson1acfc102017-06-20 12:05:47 +0100679 if (unlikely(!ctx))
680 return -ENOENT;
Chris Wilson2889caa2017-06-16 15:05:19 +0100681
Chris Wilson1acfc102017-06-20 12:05:47 +0100682 eb->ctx = ctx;
Chris Wilson2889caa2017-06-16 15:05:19 +0100683 eb->vm = ctx->ppgtt ? &ctx->ppgtt->base : &eb->i915->ggtt.base;
684
685 eb->context_flags = 0;
686 if (ctx->flags & CONTEXT_NO_ZEROMAP)
687 eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS;
688
689 return 0;
690}
691
692static int eb_lookup_vmas(struct i915_execbuffer *eb)
Chris Wilson4ff4b442017-06-16 15:05:16 +0100693{
694#define INTERMEDIATE BIT(0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100695 const unsigned int count = eb->buffer_count;
696 struct i915_gem_context_vma_lut *lut = &eb->ctx->vma_lut;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100697 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +0100698 struct idr *idr;
699 unsigned int i;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100700 int slow_pass = -1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100701 int err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100702
Chris Wilson8bcbfb12017-08-16 09:52:05 +0100703 if (unlikely(i915_gem_context_is_closed(eb->ctx)))
704 return -ENOENT;
705
706 if (unlikely(i915_gem_context_is_banned(eb->ctx)))
707 return -EIO;
708
Chris Wilson2889caa2017-06-16 15:05:19 +0100709 INIT_LIST_HEAD(&eb->relocs);
710 INIT_LIST_HEAD(&eb->unbound);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100711
Chris Wilson2889caa2017-06-16 15:05:19 +0100712 if (unlikely(lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS))
713 flush_work(&lut->resize);
714 GEM_BUG_ON(lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100715
716 for (i = 0; i < count; i++) {
717 __exec_to_vma(&eb->exec[i]) = 0;
718
719 hlist_for_each_entry(vma,
Chris Wilson2889caa2017-06-16 15:05:19 +0100720 ht_head(lut, eb->exec[i].handle),
Chris Wilson4ff4b442017-06-16 15:05:16 +0100721 ctx_node) {
722 if (vma->ctx_handle != eb->exec[i].handle)
723 continue;
724
Chris Wilson2889caa2017-06-16 15:05:19 +0100725 err = eb_add_vma(eb, &eb->exec[i], vma);
726 if (unlikely(err))
727 return err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100728
729 goto next_vma;
730 }
731
732 if (slow_pass < 0)
733 slow_pass = i;
734next_vma: ;
735 }
736
737 if (slow_pass < 0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100738 goto out;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100739
740 spin_lock(&eb->file->table_lock);
Chris Wilson2889caa2017-06-16 15:05:19 +0100741 /*
742 * Grab a reference to the object and release the lock so we can lookup
743 * or create the VMA without using GFP_ATOMIC
744 */
745 idr = &eb->file->object_idr;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100746 for (i = slow_pass; i < count; i++) {
747 struct drm_i915_gem_object *obj;
748
749 if (__exec_to_vma(&eb->exec[i]))
750 continue;
751
Chris Wilson2889caa2017-06-16 15:05:19 +0100752 obj = to_intel_bo(idr_find(idr, eb->exec[i].handle));
Chris Wilson4ff4b442017-06-16 15:05:16 +0100753 if (unlikely(!obj)) {
754 spin_unlock(&eb->file->table_lock);
755 DRM_DEBUG("Invalid object handle %d at index %d\n",
756 eb->exec[i].handle, i);
Chris Wilson2889caa2017-06-16 15:05:19 +0100757 err = -ENOENT;
758 goto err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100759 }
760
761 __exec_to_vma(&eb->exec[i]) = INTERMEDIATE | (uintptr_t)obj;
762 }
763 spin_unlock(&eb->file->table_lock);
764
765 for (i = slow_pass; i < count; i++) {
766 struct drm_i915_gem_object *obj;
767
Chris Wilson2889caa2017-06-16 15:05:19 +0100768 if (!(__exec_to_vma(&eb->exec[i]) & INTERMEDIATE))
Chris Wilson4ff4b442017-06-16 15:05:16 +0100769 continue;
770
771 /*
772 * NOTE: We can leak any vmas created here when something fails
773 * later on. But that's no issue since vma_unbind can deal with
774 * vmas which are not actually bound. And since only
775 * lookup_or_create exists as an interface to get at the vma
776 * from the (obj, vm) we don't run the risk of creating
777 * duplicated vmas for the same vm.
778 */
Chris Wilson2889caa2017-06-16 15:05:19 +0100779 obj = u64_to_ptr(typeof(*obj),
Chris Wilson4ff4b442017-06-16 15:05:16 +0100780 __exec_to_vma(&eb->exec[i]) & ~INTERMEDIATE);
781 vma = i915_vma_instance(obj, eb->vm, NULL);
782 if (unlikely(IS_ERR(vma))) {
783 DRM_DEBUG("Failed to lookup VMA\n");
Chris Wilson2889caa2017-06-16 15:05:19 +0100784 err = PTR_ERR(vma);
785 goto err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100786 }
787
788 /* First come, first served */
789 if (!vma->ctx) {
790 vma->ctx = eb->ctx;
791 vma->ctx_handle = eb->exec[i].handle;
792 hlist_add_head(&vma->ctx_node,
Chris Wilson2889caa2017-06-16 15:05:19 +0100793 ht_head(lut, eb->exec[i].handle));
794 lut->ht_count++;
795 lut->ht_size |= I915_CTX_RESIZE_IN_PROGRESS;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100796 if (i915_vma_is_ggtt(vma)) {
797 GEM_BUG_ON(obj->vma_hashed);
798 obj->vma_hashed = vma;
799 }
Chris Wilsondade2a62017-06-16 15:05:20 +0100800
801 i915_vma_get(vma);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100802 }
803
Chris Wilson2889caa2017-06-16 15:05:19 +0100804 err = eb_add_vma(eb, &eb->exec[i], vma);
805 if (unlikely(err))
806 goto err;
Chris Wilsondade2a62017-06-16 15:05:20 +0100807
808 /* Only after we validated the user didn't use our bits */
809 if (vma->ctx != eb->ctx) {
810 i915_vma_get(vma);
811 eb->exec[i].flags |= __EXEC_OBJECT_HAS_REF;
812 }
Chris Wilson4ff4b442017-06-16 15:05:16 +0100813 }
814
Chris Wilson2889caa2017-06-16 15:05:19 +0100815 if (lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS) {
816 if (ht_needs_resize(lut))
817 queue_work(system_highpri_wq, &lut->resize);
818 else
819 lut->ht_size &= ~I915_CTX_RESIZE_IN_PROGRESS;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100820 }
821
Chris Wilson2889caa2017-06-16 15:05:19 +0100822out:
823 /* take note of the batch buffer before we might reorder the lists */
824 i = eb_batch_index(eb);
825 eb->batch = exec_to_vma(&eb->exec[i]);
Chris Wilson59bfa122016-08-04 16:32:31 +0100826
827 /*
828 * SNA is doing fancy tricks with compressing batch buffers, which leads
829 * to negative relocation deltas. Usually that works out ok since the
830 * relocate address is still positive, except when the batch is placed
831 * very low in the GTT. Ensure this doesn't happen.
832 *
833 * Note that actual hangs have only been observed on gen7, but for
834 * paranoia do it everywhere.
835 */
Chris Wilson2889caa2017-06-16 15:05:19 +0100836 if (!(eb->exec[i].flags & EXEC_OBJECT_PINNED))
837 eb->exec[i].flags |= __EXEC_OBJECT_NEEDS_BIAS;
838 if (eb->reloc_cache.has_fence)
839 eb->exec[i].flags |= EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson59bfa122016-08-04 16:32:31 +0100840
Chris Wilson2889caa2017-06-16 15:05:19 +0100841 eb->args->flags |= __EXEC_VALIDATED;
842 return eb_reserve(eb);
843
844err:
845 for (i = slow_pass; i < count; i++) {
846 if (__exec_to_vma(&eb->exec[i]) & INTERMEDIATE)
847 __exec_to_vma(&eb->exec[i]) = 0;
848 }
849 lut->ht_size &= ~I915_CTX_RESIZE_IN_PROGRESS;
850 return err;
851#undef INTERMEDIATE
Chris Wilson59bfa122016-08-04 16:32:31 +0100852}
853
Chris Wilson4ff4b442017-06-16 15:05:16 +0100854static struct i915_vma *
Chris Wilson2889caa2017-06-16 15:05:19 +0100855eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000856{
Chris Wilson2889caa2017-06-16 15:05:19 +0100857 if (eb->lut_size < 0) {
858 if (handle >= -eb->lut_size)
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000859 return NULL;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100860 return exec_to_vma(&eb->exec[handle]);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000861 } else {
862 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800863 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000864
Chris Wilson2889caa2017-06-16 15:05:19 +0100865 head = &eb->buckets[hash_32(handle, eb->lut_size)];
Geliang Tangaa459502016-01-18 23:54:20 +0800866 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200867 if (vma->exec_handle == handle)
868 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000869 }
870 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000871 }
Chris Wilson67731b82010-12-08 10:38:14 +0000872}
873
Chris Wilson2889caa2017-06-16 15:05:19 +0100874static void eb_release_vmas(const struct i915_execbuffer *eb)
Chris Wilsona415d352013-11-26 11:23:15 +0000875{
Chris Wilson2889caa2017-06-16 15:05:19 +0100876 const unsigned int count = eb->buffer_count;
877 unsigned int i;
Chris Wilson650bc632017-06-15 09:14:33 +0100878
Chris Wilson2889caa2017-06-16 15:05:19 +0100879 for (i = 0; i < count; i++) {
880 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
881 struct i915_vma *vma = exec_to_vma(entry);
882
883 if (!vma)
Chris Wilsond55495b2017-06-15 09:14:34 +0100884 continue;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000885
Chris Wilson2889caa2017-06-16 15:05:19 +0100886 GEM_BUG_ON(vma->exec_entry != entry);
Chris Wilson172ae5b2016-12-05 14:29:37 +0000887 vma->exec_entry = NULL;
Chris Wilson51d05e12017-06-22 11:47:22 +0100888 __exec_to_vma(entry) = 0;
Chris Wilson2889caa2017-06-16 15:05:19 +0100889
Chris Wilsondade2a62017-06-16 15:05:20 +0100890 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
891 __eb_unreserve_vma(vma, entry);
Chris Wilson2889caa2017-06-16 15:05:19 +0100892
Chris Wilsondade2a62017-06-16 15:05:20 +0100893 if (entry->flags & __EXEC_OBJECT_HAS_REF)
894 i915_vma_put(vma);
895
896 entry->flags &=
897 ~(__EXEC_OBJECT_RESERVED | __EXEC_OBJECT_HAS_REF);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000898 }
Chris Wilson2889caa2017-06-16 15:05:19 +0100899}
Chris Wilsond55495b2017-06-15 09:14:34 +0100900
Chris Wilson2889caa2017-06-16 15:05:19 +0100901static void eb_reset_vmas(const struct i915_execbuffer *eb)
902{
903 eb_release_vmas(eb);
Chris Wilson4d470f72017-06-29 16:04:25 +0100904 if (eb->lut_size > 0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100905 memset(eb->buckets, 0,
906 sizeof(struct hlist_head) << eb->lut_size);
907}
Chris Wilsond55495b2017-06-15 09:14:34 +0100908
Chris Wilson2889caa2017-06-16 15:05:19 +0100909static void eb_destroy(const struct i915_execbuffer *eb)
910{
Chris Wilson7dd4f672017-06-16 15:05:24 +0100911 GEM_BUG_ON(eb->reloc_cache.rq);
912
Chris Wilson4d470f72017-06-29 16:04:25 +0100913 if (eb->lut_size > 0)
Chris Wilsond55495b2017-06-15 09:14:34 +0100914 kfree(eb->buckets);
Chris Wilson67731b82010-12-08 10:38:14 +0000915}
916
Chris Wilson2889caa2017-06-16 15:05:19 +0100917static inline u64
Chris Wilsond50415c2016-08-18 17:16:52 +0100918relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +0100919 const struct i915_vma *target)
Michał Winiarski934acce2015-12-29 18:24:52 +0100920{
Chris Wilson2889caa2017-06-16 15:05:19 +0100921 return gen8_canonical_addr((int)reloc->delta + target->node.start);
Michał Winiarski934acce2015-12-29 18:24:52 +0100922}
923
Chris Wilsond50415c2016-08-18 17:16:52 +0100924static void reloc_cache_init(struct reloc_cache *cache,
925 struct drm_i915_private *i915)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100926{
Chris Wilson31a39202016-08-18 17:16:46 +0100927 cache->page = -1;
Chris Wilsond50415c2016-08-18 17:16:52 +0100928 cache->vaddr = 0;
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200929 /* Must be a variable in the struct to allow GCC to unroll. */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100930 cache->gen = INTEL_GEN(i915);
Chris Wilson2889caa2017-06-16 15:05:19 +0100931 cache->has_llc = HAS_LLC(i915);
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200932 cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
Chris Wilson7dd4f672017-06-16 15:05:24 +0100933 cache->has_fence = cache->gen < 4;
934 cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
Chris Wilsone8cb9092016-08-18 17:16:53 +0100935 cache->node.allocated = false;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100936 cache->rq = NULL;
937 cache->rq_size = 0;
Chris Wilson31a39202016-08-18 17:16:46 +0100938}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100939
Chris Wilsond50415c2016-08-18 17:16:52 +0100940static inline void *unmask_page(unsigned long p)
941{
942 return (void *)(uintptr_t)(p & PAGE_MASK);
943}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100944
Chris Wilsond50415c2016-08-18 17:16:52 +0100945static inline unsigned int unmask_flags(unsigned long p)
946{
947 return p & ~PAGE_MASK;
948}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700949
Chris Wilsond50415c2016-08-18 17:16:52 +0100950#define KMAP 0x4 /* after CLFLUSH_FLAGS */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700951
Chris Wilson650bc632017-06-15 09:14:33 +0100952static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
953{
954 struct drm_i915_private *i915 =
955 container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
956 return &i915->ggtt;
957}
958
Chris Wilson7dd4f672017-06-16 15:05:24 +0100959static void reloc_gpu_flush(struct reloc_cache *cache)
960{
961 GEM_BUG_ON(cache->rq_size >= cache->rq->batch->obj->base.size / sizeof(u32));
962 cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END;
963 i915_gem_object_unpin_map(cache->rq->batch->obj);
964 i915_gem_chipset_flush(cache->rq->i915);
965
966 __i915_add_request(cache->rq, true);
967 cache->rq = NULL;
968}
969
Chris Wilson650bc632017-06-15 09:14:33 +0100970static void reloc_cache_reset(struct reloc_cache *cache)
Chris Wilson31a39202016-08-18 17:16:46 +0100971{
Chris Wilsond50415c2016-08-18 17:16:52 +0100972 void *vaddr;
973
Chris Wilson7dd4f672017-06-16 15:05:24 +0100974 if (cache->rq)
975 reloc_gpu_flush(cache);
976
Chris Wilson31a39202016-08-18 17:16:46 +0100977 if (!cache->vaddr)
978 return;
979
Chris Wilsond50415c2016-08-18 17:16:52 +0100980 vaddr = unmask_page(cache->vaddr);
981 if (cache->vaddr & KMAP) {
982 if (cache->vaddr & CLFLUSH_AFTER)
983 mb();
Chris Wilson31a39202016-08-18 17:16:46 +0100984
Chris Wilsond50415c2016-08-18 17:16:52 +0100985 kunmap_atomic(vaddr);
986 i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
987 } else {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100988 wmb();
Chris Wilsond50415c2016-08-18 17:16:52 +0100989 io_mapping_unmap_atomic((void __iomem *)vaddr);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100990 if (cache->node.allocated) {
Chris Wilson650bc632017-06-15 09:14:33 +0100991 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100992
993 ggtt->base.clear_range(&ggtt->base,
994 cache->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200995 cache->node.size);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100996 drm_mm_remove_node(&cache->node);
997 } else {
998 i915_vma_unpin((struct i915_vma *)cache->node.mm);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700999 }
Chris Wilson31a39202016-08-18 17:16:46 +01001000 }
Chris Wilson650bc632017-06-15 09:14:33 +01001001
1002 cache->vaddr = 0;
1003 cache->page = -1;
Chris Wilson31a39202016-08-18 17:16:46 +01001004}
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001005
Chris Wilson31a39202016-08-18 17:16:46 +01001006static void *reloc_kmap(struct drm_i915_gem_object *obj,
1007 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001008 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +01001009{
Chris Wilsond50415c2016-08-18 17:16:52 +01001010 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +01001011
Chris Wilsond50415c2016-08-18 17:16:52 +01001012 if (cache->vaddr) {
1013 kunmap_atomic(unmask_page(cache->vaddr));
1014 } else {
1015 unsigned int flushes;
Chris Wilson2889caa2017-06-16 15:05:19 +01001016 int err;
Chris Wilson31a39202016-08-18 17:16:46 +01001017
Chris Wilson2889caa2017-06-16 15:05:19 +01001018 err = i915_gem_obj_prepare_shmem_write(obj, &flushes);
1019 if (err)
1020 return ERR_PTR(err);
Chris Wilsond50415c2016-08-18 17:16:52 +01001021
1022 BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
1023 BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
1024
1025 cache->vaddr = flushes | KMAP;
1026 cache->node.mm = (void *)obj;
1027 if (flushes)
1028 mb();
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001029 }
1030
Chris Wilsond50415c2016-08-18 17:16:52 +01001031 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
1032 cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +01001033 cache->page = page;
Chris Wilson31a39202016-08-18 17:16:46 +01001034
Chris Wilsond50415c2016-08-18 17:16:52 +01001035 return vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +01001036}
1037
Chris Wilsond50415c2016-08-18 17:16:52 +01001038static void *reloc_iomap(struct drm_i915_gem_object *obj,
Chris Wilson31a39202016-08-18 17:16:46 +01001039 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001040 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +01001041{
Chris Wilson650bc632017-06-15 09:14:33 +01001042 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001043 unsigned long offset;
Chris Wilsond50415c2016-08-18 17:16:52 +01001044 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +01001045
Chris Wilsond50415c2016-08-18 17:16:52 +01001046 if (cache->vaddr) {
Jani Nikula615e5002016-10-04 12:54:13 +03001047 io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
Chris Wilsond50415c2016-08-18 17:16:52 +01001048 } else {
1049 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001050 int err;
Chris Wilson31a39202016-08-18 17:16:46 +01001051
Chris Wilson2889caa2017-06-16 15:05:19 +01001052 if (use_cpu_reloc(cache, obj))
Chris Wilsond50415c2016-08-18 17:16:52 +01001053 return NULL;
Chris Wilson31a39202016-08-18 17:16:46 +01001054
Chris Wilson2889caa2017-06-16 15:05:19 +01001055 err = i915_gem_object_set_to_gtt_domain(obj, true);
1056 if (err)
1057 return ERR_PTR(err);
Chris Wilson31a39202016-08-18 17:16:46 +01001058
Chris Wilsond50415c2016-08-18 17:16:52 +01001059 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1060 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001061 if (IS_ERR(vma)) {
1062 memset(&cache->node, 0, sizeof(cache->node));
Chris Wilson2889caa2017-06-16 15:05:19 +01001063 err = drm_mm_insert_node_in_range
Chris Wilsone8cb9092016-08-18 17:16:53 +01001064 (&ggtt->base.mm, &cache->node,
Chris Wilsonf51455d2017-01-10 14:47:34 +00001065 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
Chris Wilsone8cb9092016-08-18 17:16:53 +01001066 0, ggtt->mappable_end,
Chris Wilson4e64e552017-02-02 21:04:38 +00001067 DRM_MM_INSERT_LOW);
Chris Wilson2889caa2017-06-16 15:05:19 +01001068 if (err) /* no inactive aperture space, use cpu reloc */
Chris Wilsonc92fa4f2016-10-07 07:53:25 +01001069 return NULL;
Chris Wilsone8cb9092016-08-18 17:16:53 +01001070 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +01001071 err = i915_vma_put_fence(vma);
1072 if (err) {
Chris Wilsone8cb9092016-08-18 17:16:53 +01001073 i915_vma_unpin(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001074 return ERR_PTR(err);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001075 }
Rafael Barbalho5032d872013-08-21 17:10:51 +01001076
Chris Wilsone8cb9092016-08-18 17:16:53 +01001077 cache->node.start = vma->node.start;
1078 cache->node.mm = (void *)vma;
Chris Wilsond50415c2016-08-18 17:16:52 +01001079 }
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001080 }
1081
Chris Wilsone8cb9092016-08-18 17:16:53 +01001082 offset = cache->node.start;
1083 if (cache->node.allocated) {
Chris Wilsonfc099092016-10-28 15:27:56 +01001084 wmb();
Chris Wilsone8cb9092016-08-18 17:16:53 +01001085 ggtt->base.insert_page(&ggtt->base,
1086 i915_gem_object_get_dma_address(obj, page),
1087 offset, I915_CACHE_NONE, 0);
1088 } else {
1089 offset += page << PAGE_SHIFT;
1090 }
1091
Chris Wilson650bc632017-06-15 09:14:33 +01001092 vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->mappable,
1093 offset);
Chris Wilsond50415c2016-08-18 17:16:52 +01001094 cache->page = page;
1095 cache->vaddr = (unsigned long)vaddr;
1096
1097 return vaddr;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001098}
1099
Chris Wilsond50415c2016-08-18 17:16:52 +01001100static void *reloc_vaddr(struct drm_i915_gem_object *obj,
1101 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001102 unsigned long page)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001103{
Chris Wilsond50415c2016-08-18 17:16:52 +01001104 void *vaddr;
1105
1106 if (cache->page == page) {
1107 vaddr = unmask_page(cache->vaddr);
1108 } else {
1109 vaddr = NULL;
1110 if ((cache->vaddr & KMAP) == 0)
1111 vaddr = reloc_iomap(obj, cache, page);
1112 if (!vaddr)
1113 vaddr = reloc_kmap(obj, cache, page);
1114 }
1115
1116 return vaddr;
1117}
1118
1119static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
1120{
1121 if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
1122 if (flushes & CLFLUSH_BEFORE) {
1123 clflushopt(addr);
1124 mb();
1125 }
1126
1127 *addr = value;
1128
Chris Wilson2889caa2017-06-16 15:05:19 +01001129 /*
1130 * Writes to the same cacheline are serialised by the CPU
Chris Wilsond50415c2016-08-18 17:16:52 +01001131 * (including clflush). On the write path, we only require
1132 * that it hits memory in an orderly fashion and place
1133 * mb barriers at the start and end of the relocation phase
1134 * to ensure ordering of clflush wrt to the system.
1135 */
1136 if (flushes & CLFLUSH_AFTER)
1137 clflushopt(addr);
1138 } else
1139 *addr = value;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001140}
1141
Chris Wilson7dd4f672017-06-16 15:05:24 +01001142static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
1143 struct i915_vma *vma,
1144 unsigned int len)
1145{
1146 struct reloc_cache *cache = &eb->reloc_cache;
1147 struct drm_i915_gem_object *obj;
1148 struct drm_i915_gem_request *rq;
1149 struct i915_vma *batch;
1150 u32 *cmd;
1151 int err;
1152
1153 GEM_BUG_ON(vma->obj->base.write_domain & I915_GEM_DOMAIN_CPU);
1154
1155 obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, PAGE_SIZE);
1156 if (IS_ERR(obj))
1157 return PTR_ERR(obj);
1158
1159 cmd = i915_gem_object_pin_map(obj,
1160 cache->has_llc ? I915_MAP_WB : I915_MAP_WC);
1161 i915_gem_object_unpin_pages(obj);
1162 if (IS_ERR(cmd))
1163 return PTR_ERR(cmd);
1164
1165 err = i915_gem_object_set_to_wc_domain(obj, false);
1166 if (err)
1167 goto err_unmap;
1168
1169 batch = i915_vma_instance(obj, vma->vm, NULL);
1170 if (IS_ERR(batch)) {
1171 err = PTR_ERR(batch);
1172 goto err_unmap;
1173 }
1174
1175 err = i915_vma_pin(batch, 0, 0, PIN_USER | PIN_NONBLOCK);
1176 if (err)
1177 goto err_unmap;
1178
1179 rq = i915_gem_request_alloc(eb->engine, eb->ctx);
1180 if (IS_ERR(rq)) {
1181 err = PTR_ERR(rq);
1182 goto err_unpin;
1183 }
1184
1185 err = i915_gem_request_await_object(rq, vma->obj, true);
1186 if (err)
1187 goto err_request;
1188
1189 err = eb->engine->emit_flush(rq, EMIT_INVALIDATE);
1190 if (err)
1191 goto err_request;
1192
1193 err = i915_switch_context(rq);
1194 if (err)
1195 goto err_request;
1196
1197 err = eb->engine->emit_bb_start(rq,
1198 batch->node.start, PAGE_SIZE,
1199 cache->gen > 5 ? 0 : I915_DISPATCH_SECURE);
1200 if (err)
1201 goto err_request;
1202
Chris Wilson95ff7c72017-06-16 15:05:25 +01001203 GEM_BUG_ON(!reservation_object_test_signaled_rcu(batch->resv, true));
Chris Wilson7dd4f672017-06-16 15:05:24 +01001204 i915_vma_move_to_active(batch, rq, 0);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001205 reservation_object_lock(batch->resv, NULL);
1206 reservation_object_add_excl_fence(batch->resv, &rq->fence);
1207 reservation_object_unlock(batch->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001208 i915_vma_unpin(batch);
1209
Chris Wilson25ffaa62017-06-20 13:43:20 +01001210 i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001211 reservation_object_lock(vma->resv, NULL);
1212 reservation_object_add_excl_fence(vma->resv, &rq->fence);
1213 reservation_object_unlock(vma->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001214
1215 rq->batch = batch;
1216
1217 cache->rq = rq;
1218 cache->rq_cmd = cmd;
1219 cache->rq_size = 0;
1220
1221 /* Return with batch mapping (cmd) still pinned */
1222 return 0;
1223
1224err_request:
1225 i915_add_request(rq);
1226err_unpin:
1227 i915_vma_unpin(batch);
1228err_unmap:
1229 i915_gem_object_unpin_map(obj);
1230 return err;
1231}
1232
1233static u32 *reloc_gpu(struct i915_execbuffer *eb,
1234 struct i915_vma *vma,
1235 unsigned int len)
1236{
1237 struct reloc_cache *cache = &eb->reloc_cache;
1238 u32 *cmd;
1239
1240 if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1))
1241 reloc_gpu_flush(cache);
1242
1243 if (unlikely(!cache->rq)) {
1244 int err;
1245
1246 err = __reloc_gpu_alloc(eb, vma, len);
1247 if (unlikely(err))
1248 return ERR_PTR(err);
1249 }
1250
1251 cmd = cache->rq_cmd + cache->rq_size;
1252 cache->rq_size += len;
1253
1254 return cmd;
1255}
1256
Chris Wilson2889caa2017-06-16 15:05:19 +01001257static u64
1258relocate_entry(struct i915_vma *vma,
Chris Wilsond50415c2016-08-18 17:16:52 +01001259 const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +01001260 struct i915_execbuffer *eb,
1261 const struct i915_vma *target)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001262{
Chris Wilsond50415c2016-08-18 17:16:52 +01001263 u64 offset = reloc->offset;
Chris Wilson2889caa2017-06-16 15:05:19 +01001264 u64 target_offset = relocation_target(reloc, target);
1265 bool wide = eb->reloc_cache.use_64bit_reloc;
Chris Wilsond50415c2016-08-18 17:16:52 +01001266 void *vaddr;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001267
Chris Wilson7dd4f672017-06-16 15:05:24 +01001268 if (!eb->reloc_cache.vaddr &&
1269 (DBG_FORCE_RELOC == FORCE_GPU_RELOC ||
Chris Wilsonf2f5c062017-08-16 09:52:04 +01001270 !reservation_object_test_signaled_rcu(vma->resv, true)) &&
1271 __intel_engine_can_store_dword(eb->reloc_cache.gen,
1272 eb->engine->class)) {
Chris Wilson7dd4f672017-06-16 15:05:24 +01001273 const unsigned int gen = eb->reloc_cache.gen;
1274 unsigned int len;
1275 u32 *batch;
1276 u64 addr;
1277
1278 if (wide)
1279 len = offset & 7 ? 8 : 5;
1280 else if (gen >= 4)
1281 len = 4;
Chris Wilsonf2f5c062017-08-16 09:52:04 +01001282 else
Chris Wilson7dd4f672017-06-16 15:05:24 +01001283 len = 3;
Chris Wilson7dd4f672017-06-16 15:05:24 +01001284
1285 batch = reloc_gpu(eb, vma, len);
1286 if (IS_ERR(batch))
1287 goto repeat;
1288
1289 addr = gen8_canonical_addr(vma->node.start + offset);
1290 if (wide) {
1291 if (offset & 7) {
1292 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1293 *batch++ = lower_32_bits(addr);
1294 *batch++ = upper_32_bits(addr);
1295 *batch++ = lower_32_bits(target_offset);
1296
1297 addr = gen8_canonical_addr(addr + 4);
1298
1299 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1300 *batch++ = lower_32_bits(addr);
1301 *batch++ = upper_32_bits(addr);
1302 *batch++ = upper_32_bits(target_offset);
1303 } else {
1304 *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1;
1305 *batch++ = lower_32_bits(addr);
1306 *batch++ = upper_32_bits(addr);
1307 *batch++ = lower_32_bits(target_offset);
1308 *batch++ = upper_32_bits(target_offset);
1309 }
1310 } else if (gen >= 6) {
1311 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1312 *batch++ = 0;
1313 *batch++ = addr;
1314 *batch++ = target_offset;
1315 } else if (gen >= 4) {
1316 *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1317 *batch++ = 0;
1318 *batch++ = addr;
1319 *batch++ = target_offset;
1320 } else {
1321 *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
1322 *batch++ = addr;
1323 *batch++ = target_offset;
1324 }
1325
1326 goto out;
1327 }
1328
Chris Wilsond50415c2016-08-18 17:16:52 +01001329repeat:
Chris Wilson95ff7c72017-06-16 15:05:25 +01001330 vaddr = reloc_vaddr(vma->obj, &eb->reloc_cache, offset >> PAGE_SHIFT);
Chris Wilsond50415c2016-08-18 17:16:52 +01001331 if (IS_ERR(vaddr))
1332 return PTR_ERR(vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001333
Chris Wilsond50415c2016-08-18 17:16:52 +01001334 clflush_write32(vaddr + offset_in_page(offset),
1335 lower_32_bits(target_offset),
Chris Wilson2889caa2017-06-16 15:05:19 +01001336 eb->reloc_cache.vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001337
Chris Wilsond50415c2016-08-18 17:16:52 +01001338 if (wide) {
1339 offset += sizeof(u32);
1340 target_offset >>= 32;
1341 wide = false;
1342 goto repeat;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001343 }
Chris Wilsondabdfe02012-03-26 10:10:27 +02001344
Chris Wilson7dd4f672017-06-16 15:05:24 +01001345out:
Chris Wilson2889caa2017-06-16 15:05:19 +01001346 return target->node.start | UPDATE;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001347}
1348
Chris Wilson2889caa2017-06-16 15:05:19 +01001349static u64
1350eb_relocate_entry(struct i915_execbuffer *eb,
1351 struct i915_vma *vma,
1352 const struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001353{
Chris Wilson507d9772017-06-16 15:05:17 +01001354 struct i915_vma *target;
Chris Wilson2889caa2017-06-16 15:05:19 +01001355 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001356
Chris Wilson67731b82010-12-08 10:38:14 +00001357 /* we've already hold a reference to all valid objects */
Chris Wilson507d9772017-06-16 15:05:17 +01001358 target = eb_get_vma(eb, reloc->target_handle);
1359 if (unlikely(!target))
Chris Wilson54cf91d2010-11-25 18:00:26 +00001360 return -ENOENT;
Eric Anholte844b992012-07-31 15:35:01 -07001361
Chris Wilson54cf91d2010-11-25 18:00:26 +00001362 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001363 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001364 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001365 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001366 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001367 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001368 (int) reloc->offset,
1369 reloc->read_domains,
1370 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001371 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001372 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +01001373 if (unlikely((reloc->write_domain | reloc->read_domains)
1374 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001375 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001376 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001377 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001378 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001379 (int) reloc->offset,
1380 reloc->read_domains,
1381 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001382 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001383 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001384
Chris Wilson2889caa2017-06-16 15:05:19 +01001385 if (reloc->write_domain) {
Chris Wilson507d9772017-06-16 15:05:17 +01001386 target->exec_entry->flags |= EXEC_OBJECT_WRITE;
1387
Chris Wilson2889caa2017-06-16 15:05:19 +01001388 /*
1389 * Sandybridge PPGTT errata: We need a global gtt mapping
1390 * for MI and pipe_control writes because the gpu doesn't
1391 * properly redirect them through the ppgtt for non_secure
1392 * batchbuffers.
1393 */
1394 if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
1395 IS_GEN6(eb->i915)) {
1396 err = i915_vma_bind(target, target->obj->cache_level,
1397 PIN_GLOBAL);
1398 if (WARN_ONCE(err,
1399 "Unexpected failure to bind target VMA!"))
1400 return err;
1401 }
Chris Wilson507d9772017-06-16 15:05:17 +01001402 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001403
Chris Wilson2889caa2017-06-16 15:05:19 +01001404 /*
1405 * If the relocation already has the right value in it, no
Chris Wilson54cf91d2010-11-25 18:00:26 +00001406 * more work needs to be done.
1407 */
Chris Wilson7dd4f672017-06-16 15:05:24 +01001408 if (!DBG_FORCE_RELOC &&
1409 gen8_canonical_addr(target->node.start) == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +00001410 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001411
1412 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001413 if (unlikely(reloc->offset >
Chris Wilson507d9772017-06-16 15:05:17 +01001414 vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001415 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson507d9772017-06-16 15:05:17 +01001416 "target %d offset %d size %d.\n",
1417 reloc->target_handle,
1418 (int)reloc->offset,
1419 (int)vma->size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001420 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001421 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001422 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001423 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson507d9772017-06-16 15:05:17 +01001424 "target %d offset %d.\n",
1425 reloc->target_handle,
1426 (int)reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001427 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001428 }
1429
Chris Wilson071750e2017-06-16 15:05:18 +01001430 /*
1431 * If we write into the object, we need to force the synchronisation
1432 * barrier, either with an asynchronous clflush or if we executed the
1433 * patching using the GPU (though that should be serialised by the
1434 * timeline). To be completely sure, and since we are required to
1435 * do relocations we are already stalling, disable the user's opt
1436 * of our synchronisation.
1437 */
1438 vma->exec_entry->flags &= ~EXEC_OBJECT_ASYNC;
1439
Chris Wilson54cf91d2010-11-25 18:00:26 +00001440 /* and update the user's relocation entry */
Chris Wilson2889caa2017-06-16 15:05:19 +01001441 return relocate_entry(vma, reloc, eb, target);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001442}
1443
Chris Wilson2889caa2017-06-16 15:05:19 +01001444static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001445{
Chris Wilson1d83f442012-03-24 20:12:53 +00001446#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
Chris Wilson2889caa2017-06-16 15:05:19 +01001447 struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
1448 struct drm_i915_gem_relocation_entry __user *urelocs;
1449 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
1450 unsigned int remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001451
Chris Wilson2889caa2017-06-16 15:05:19 +01001452 urelocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson1d83f442012-03-24 20:12:53 +00001453 remain = entry->relocation_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01001454 if (unlikely(remain > N_RELOC(ULONG_MAX)))
1455 return -EINVAL;
Chris Wilsonebc08082016-10-18 13:02:51 +01001456
Chris Wilson2889caa2017-06-16 15:05:19 +01001457 /*
1458 * We must check that the entire relocation array is safe
1459 * to read. However, if the array is not writable the user loses
1460 * the updated relocation values.
1461 */
Imre Deakedd90032017-07-14 18:12:42 +03001462 if (unlikely(!access_ok(VERIFY_READ, urelocs, remain*sizeof(*urelocs))))
Chris Wilson2889caa2017-06-16 15:05:19 +01001463 return -EFAULT;
Chris Wilson1d83f442012-03-24 20:12:53 +00001464
Chris Wilson2889caa2017-06-16 15:05:19 +01001465 do {
1466 struct drm_i915_gem_relocation_entry *r = stack;
1467 unsigned int count =
1468 min_t(unsigned int, remain, ARRAY_SIZE(stack));
1469 unsigned int copied;
1470
1471 /*
1472 * This is the fast path and we cannot handle a pagefault
Chris Wilsonebc08082016-10-18 13:02:51 +01001473 * whilst holding the struct mutex lest the user pass in the
1474 * relocations contained within a mmaped bo. For in such a case
1475 * we, the page fault handler would call i915_gem_fault() and
1476 * we would try to acquire the struct mutex again. Obviously
1477 * this is bad and so lockdep complains vehemently.
1478 */
1479 pagefault_disable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001480 copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
Chris Wilsonebc08082016-10-18 13:02:51 +01001481 pagefault_enable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001482 if (unlikely(copied)) {
1483 remain = -EFAULT;
Chris Wilson31a39202016-08-18 17:16:46 +01001484 goto out;
1485 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001486
Chris Wilson2889caa2017-06-16 15:05:19 +01001487 remain -= count;
Chris Wilson1d83f442012-03-24 20:12:53 +00001488 do {
Chris Wilson2889caa2017-06-16 15:05:19 +01001489 u64 offset = eb_relocate_entry(eb, vma, r);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001490
Chris Wilson2889caa2017-06-16 15:05:19 +01001491 if (likely(offset == 0)) {
1492 } else if ((s64)offset < 0) {
1493 remain = (int)offset;
Chris Wilson31a39202016-08-18 17:16:46 +01001494 goto out;
Chris Wilson2889caa2017-06-16 15:05:19 +01001495 } else {
1496 /*
1497 * Note that reporting an error now
1498 * leaves everything in an inconsistent
1499 * state as we have *already* changed
1500 * the relocation value inside the
1501 * object. As we have not changed the
1502 * reloc.presumed_offset or will not
1503 * change the execobject.offset, on the
1504 * call we may not rewrite the value
1505 * inside the object, leaving it
1506 * dangling and causing a GPU hang. Unless
1507 * userspace dynamically rebuilds the
1508 * relocations on each execbuf rather than
1509 * presume a static tree.
1510 *
1511 * We did previously check if the relocations
1512 * were writable (access_ok), an error now
1513 * would be a strange race with mprotect,
1514 * having already demonstrated that we
1515 * can read from this userspace address.
1516 */
1517 offset = gen8_canonical_addr(offset & ~UPDATE);
1518 __put_user(offset,
1519 &urelocs[r-stack].presumed_offset);
Chris Wilson1d83f442012-03-24 20:12:53 +00001520 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001521 } while (r++, --count);
1522 urelocs += ARRAY_SIZE(stack);
1523 } while (remain);
Chris Wilson31a39202016-08-18 17:16:46 +01001524out:
Chris Wilson650bc632017-06-15 09:14:33 +01001525 reloc_cache_reset(&eb->reloc_cache);
Chris Wilson2889caa2017-06-16 15:05:19 +01001526 return remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001527}
1528
1529static int
Chris Wilson2889caa2017-06-16 15:05:19 +01001530eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001531{
Ben Widawsky27173f12013-08-14 11:38:36 +02001532 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson2889caa2017-06-16 15:05:19 +01001533 struct drm_i915_gem_relocation_entry *relocs =
1534 u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1535 unsigned int i;
1536 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001537
1538 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001539 u64 offset = eb_relocate_entry(eb, vma, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001540
Chris Wilson2889caa2017-06-16 15:05:19 +01001541 if ((s64)offset < 0) {
1542 err = (int)offset;
1543 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001544 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001545 }
1546 err = 0;
Chris Wilsona415d352013-11-26 11:23:15 +00001547err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001548 reloc_cache_reset(&eb->reloc_cache);
1549 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001550}
1551
Chris Wilson2889caa2017-06-16 15:05:19 +01001552static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001553{
Chris Wilson2889caa2017-06-16 15:05:19 +01001554 const char __user *addr, *end;
1555 unsigned long size;
1556 char __maybe_unused c;
Ben Widawsky27173f12013-08-14 11:38:36 +02001557
Chris Wilson2889caa2017-06-16 15:05:19 +01001558 size = entry->relocation_count;
1559 if (size == 0)
1560 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001561
Chris Wilson2889caa2017-06-16 15:05:19 +01001562 if (size > N_RELOC(ULONG_MAX))
1563 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001564
Chris Wilson2889caa2017-06-16 15:05:19 +01001565 addr = u64_to_user_ptr(entry->relocs_ptr);
1566 size *= sizeof(struct drm_i915_gem_relocation_entry);
1567 if (!access_ok(VERIFY_READ, addr, size))
1568 return -EFAULT;
1569
1570 end = addr + size;
1571 for (; addr < end; addr += PAGE_SIZE) {
1572 int err = __get_user(c, addr);
1573 if (err)
1574 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001575 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001576 return __get_user(c, end - 1);
1577}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001578
Chris Wilson2889caa2017-06-16 15:05:19 +01001579static int eb_copy_relocations(const struct i915_execbuffer *eb)
1580{
1581 const unsigned int count = eb->buffer_count;
1582 unsigned int i;
1583 int err;
1584
Chris Wilson54cf91d2010-11-25 18:00:26 +00001585 for (i = 0; i < count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001586 const unsigned int nreloc = eb->exec[i].relocation_count;
1587 struct drm_i915_gem_relocation_entry __user *urelocs;
1588 struct drm_i915_gem_relocation_entry *relocs;
1589 unsigned long size;
1590 unsigned long copied;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001591
Chris Wilson2889caa2017-06-16 15:05:19 +01001592 if (nreloc == 0)
1593 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001594
Chris Wilson2889caa2017-06-16 15:05:19 +01001595 err = check_relocations(&eb->exec[i]);
1596 if (err)
1597 goto err;
1598
1599 urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
1600 size = nreloc * sizeof(*relocs);
1601
1602 relocs = kvmalloc_array(size, 1, GFP_TEMPORARY);
1603 if (!relocs) {
1604 kvfree(relocs);
1605 err = -ENOMEM;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001606 goto err;
1607 }
1608
Chris Wilson2889caa2017-06-16 15:05:19 +01001609 /* copy_from_user is limited to < 4GiB */
1610 copied = 0;
1611 do {
1612 unsigned int len =
1613 min_t(u64, BIT_ULL(31), size - copied);
1614
1615 if (__copy_from_user((char *)relocs + copied,
1616 (char *)urelocs + copied,
1617 len)) {
1618 kvfree(relocs);
1619 err = -EFAULT;
1620 goto err;
1621 }
1622
1623 copied += len;
1624 } while (copied < size);
1625
1626 /*
1627 * As we do not update the known relocation offsets after
Chris Wilson262b6d32013-01-15 16:17:54 +00001628 * relocating (due to the complexities in lock handling),
1629 * we need to mark them as invalid now so that we force the
1630 * relocation processing next time. Just in case the target
1631 * object is evicted and then rebound into its old
1632 * presumed_offset before the next execbuffer - if that
1633 * happened we would make the mistake of assuming that the
1634 * relocations were valid.
1635 */
Chris Wilson2889caa2017-06-16 15:05:19 +01001636 user_access_begin();
1637 for (copied = 0; copied < nreloc; copied++)
1638 unsafe_put_user(-1,
1639 &urelocs[copied].presumed_offset,
1640 end_user);
1641end_user:
1642 user_access_end();
Chris Wilson262b6d32013-01-15 16:17:54 +00001643
Chris Wilson2889caa2017-06-16 15:05:19 +01001644 eb->exec[i].relocs_ptr = (uintptr_t)relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001645 }
1646
Chris Wilson2889caa2017-06-16 15:05:19 +01001647 return 0;
1648
1649err:
1650 while (i--) {
1651 struct drm_i915_gem_relocation_entry *relocs =
1652 u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr);
1653 if (eb->exec[i].relocation_count)
1654 kvfree(relocs);
1655 }
1656 return err;
1657}
1658
1659static int eb_prefault_relocations(const struct i915_execbuffer *eb)
1660{
1661 const unsigned int count = eb->buffer_count;
1662 unsigned int i;
1663
1664 if (unlikely(i915.prefault_disable))
1665 return 0;
1666
1667 for (i = 0; i < count; i++) {
1668 int err;
1669
1670 err = check_relocations(&eb->exec[i]);
1671 if (err)
1672 return err;
1673 }
1674
1675 return 0;
1676}
1677
1678static noinline int eb_relocate_slow(struct i915_execbuffer *eb)
1679{
1680 struct drm_device *dev = &eb->i915->drm;
1681 bool have_copy = false;
1682 struct i915_vma *vma;
1683 int err = 0;
1684
1685repeat:
1686 if (signal_pending(current)) {
1687 err = -ERESTARTSYS;
1688 goto out;
1689 }
1690
1691 /* We may process another execbuffer during the unlock... */
1692 eb_reset_vmas(eb);
1693 mutex_unlock(&dev->struct_mutex);
1694
1695 /*
1696 * We take 3 passes through the slowpatch.
1697 *
1698 * 1 - we try to just prefault all the user relocation entries and
1699 * then attempt to reuse the atomic pagefault disabled fast path again.
1700 *
1701 * 2 - we copy the user entries to a local buffer here outside of the
1702 * local and allow ourselves to wait upon any rendering before
1703 * relocations
1704 *
1705 * 3 - we already have a local copy of the relocation entries, but
1706 * were interrupted (EAGAIN) whilst waiting for the objects, try again.
1707 */
1708 if (!err) {
1709 err = eb_prefault_relocations(eb);
1710 } else if (!have_copy) {
1711 err = eb_copy_relocations(eb);
1712 have_copy = err == 0;
1713 } else {
1714 cond_resched();
1715 err = 0;
1716 }
1717 if (err) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001718 mutex_lock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01001719 goto out;
1720 }
1721
Chris Wilson8a2421b2017-06-16 15:05:22 +01001722 /* A frequent cause for EAGAIN are currently unavailable client pages */
1723 flush_workqueue(eb->i915->mm.userptr_wq);
1724
Chris Wilson2889caa2017-06-16 15:05:19 +01001725 err = i915_mutex_lock_interruptible(dev);
1726 if (err) {
1727 mutex_lock(&dev->struct_mutex);
1728 goto out;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001729 }
1730
Chris Wilson67731b82010-12-08 10:38:14 +00001731 /* reacquire the objects */
Chris Wilson2889caa2017-06-16 15:05:19 +01001732 err = eb_lookup_vmas(eb);
1733 if (err)
Chris Wilson3b96eff2013-01-08 10:53:14 +00001734 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +00001735
Chris Wilson2889caa2017-06-16 15:05:19 +01001736 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1737 if (!have_copy) {
1738 pagefault_disable();
1739 err = eb_relocate_vma(eb, vma);
1740 pagefault_enable();
1741 if (err)
1742 goto repeat;
1743 } else {
1744 err = eb_relocate_vma_slow(eb, vma);
1745 if (err)
1746 goto err;
1747 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001748 }
1749
Chris Wilson2889caa2017-06-16 15:05:19 +01001750 /*
1751 * Leave the user relocations as are, this is the painfully slow path,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001752 * and we want to avoid the complication of dropping the lock whilst
1753 * having buffers reserved in the aperture and so causing spurious
1754 * ENOSPC for random operations.
1755 */
1756
1757err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001758 if (err == -EAGAIN)
1759 goto repeat;
1760
1761out:
1762 if (have_copy) {
1763 const unsigned int count = eb->buffer_count;
1764 unsigned int i;
1765
1766 for (i = 0; i < count; i++) {
1767 const struct drm_i915_gem_exec_object2 *entry =
1768 &eb->exec[i];
1769 struct drm_i915_gem_relocation_entry *relocs;
1770
1771 if (!entry->relocation_count)
1772 continue;
1773
1774 relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1775 kvfree(relocs);
1776 }
1777 }
1778
Chris Wilson1f727d92017-07-21 15:50:36 +01001779 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001780}
1781
Chris Wilson2889caa2017-06-16 15:05:19 +01001782static int eb_relocate(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001783{
Chris Wilson2889caa2017-06-16 15:05:19 +01001784 if (eb_lookup_vmas(eb))
1785 goto slow;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001786
Chris Wilson2889caa2017-06-16 15:05:19 +01001787 /* The objects are in their final locations, apply the relocations. */
1788 if (eb->args->flags & __EXEC_HAS_RELOC) {
1789 struct i915_vma *vma;
1790
1791 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1792 if (eb_relocate_vma(eb, vma))
1793 goto slow;
1794 }
1795 }
1796
1797 return 0;
1798
1799slow:
1800 return eb_relocate_slow(eb);
1801}
1802
Chris Wilson95ff7c72017-06-16 15:05:25 +01001803static void eb_export_fence(struct i915_vma *vma,
Chris Wilson2889caa2017-06-16 15:05:19 +01001804 struct drm_i915_gem_request *req,
1805 unsigned int flags)
1806{
Chris Wilson95ff7c72017-06-16 15:05:25 +01001807 struct reservation_object *resv = vma->resv;
Chris Wilson2889caa2017-06-16 15:05:19 +01001808
1809 /*
1810 * Ignore errors from failing to allocate the new fence, we can't
1811 * handle an error right now. Worst case should be missed
1812 * synchronisation leading to rendering corruption.
1813 */
1814 reservation_object_lock(resv, NULL);
1815 if (flags & EXEC_OBJECT_WRITE)
1816 reservation_object_add_excl_fence(resv, &req->fence);
1817 else if (reservation_object_reserve_shared(resv) == 0)
1818 reservation_object_add_shared_fence(resv, &req->fence);
1819 reservation_object_unlock(resv);
1820}
1821
1822static int eb_move_to_gpu(struct i915_execbuffer *eb)
1823{
1824 const unsigned int count = eb->buffer_count;
1825 unsigned int i;
1826 int err;
1827
1828 for (i = 0; i < count; i++) {
Chris Wilson0f46daa2017-07-21 15:50:37 +01001829 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
Chris Wilson2889caa2017-06-16 15:05:19 +01001830 struct i915_vma *vma = exec_to_vma(entry);
Ben Widawsky27173f12013-08-14 11:38:36 +02001831 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001832
Chris Wilson2889caa2017-06-16 15:05:19 +01001833 if (entry->flags & EXEC_OBJECT_CAPTURE) {
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001834 struct i915_gem_capture_list *capture;
1835
1836 capture = kmalloc(sizeof(*capture), GFP_KERNEL);
1837 if (unlikely(!capture))
1838 return -ENOMEM;
1839
Chris Wilson650bc632017-06-15 09:14:33 +01001840 capture->next = eb->request->capture_list;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001841 capture->vma = vma;
Chris Wilson650bc632017-06-15 09:14:33 +01001842 eb->request->capture_list = capture;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001843 }
1844
Chris Wilsonb8f55be2017-08-11 12:11:16 +01001845 /*
1846 * If the GPU is not _reading_ through the CPU cache, we need
1847 * to make sure that any writes (both previous GPU writes from
1848 * before a change in snooping levels and normal CPU writes)
1849 * caught in that cache are flushed to main memory.
1850 *
1851 * We want to say
1852 * obj->cache_dirty &&
1853 * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
1854 * but gcc's optimiser doesn't handle that as well and emits
1855 * two jumps instead of one. Maybe one day...
1856 */
1857 if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) {
Chris Wilson0f46daa2017-07-21 15:50:37 +01001858 if (i915_gem_clflush_object(obj, 0))
1859 entry->flags &= ~EXEC_OBJECT_ASYNC;
1860 }
1861
Chris Wilson2889caa2017-06-16 15:05:19 +01001862 if (entry->flags & EXEC_OBJECT_ASYNC)
1863 goto skip_flushes;
Chris Wilson77ae9952017-01-27 09:40:07 +00001864
Chris Wilson2889caa2017-06-16 15:05:19 +01001865 err = i915_gem_request_await_object
1866 (eb->request, obj, entry->flags & EXEC_OBJECT_WRITE);
1867 if (err)
1868 return err;
1869
1870skip_flushes:
1871 i915_vma_move_to_active(vma, eb->request, entry->flags);
1872 __eb_unreserve_vma(vma, entry);
1873 vma->exec_entry = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001874 }
1875
Chris Wilson2889caa2017-06-16 15:05:19 +01001876 for (i = 0; i < count; i++) {
1877 const struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
1878 struct i915_vma *vma = exec_to_vma(entry);
1879
Chris Wilson95ff7c72017-06-16 15:05:25 +01001880 eb_export_fence(vma, eb->request, entry->flags);
Chris Wilsondade2a62017-06-16 15:05:20 +01001881 if (unlikely(entry->flags & __EXEC_OBJECT_HAS_REF))
1882 i915_vma_put(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001883 }
1884 eb->exec = NULL;
1885
Chris Wilsondcd79932016-08-18 17:16:40 +01001886 /* Unconditionally flush any chipset caches (for streaming writes). */
Chris Wilson650bc632017-06-15 09:14:33 +01001887 i915_gem_chipset_flush(eb->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001888
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001889 /* Unconditionally invalidate GPU caches and TLBs. */
Chris Wilson650bc632017-06-15 09:14:33 +01001890 return eb->engine->emit_flush(eb->request, EMIT_INVALIDATE);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001891}
1892
Chris Wilson2889caa2017-06-16 15:05:19 +01001893static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001894{
Chris Wilson650bc632017-06-15 09:14:33 +01001895 if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
Daniel Vettered5982e2013-01-17 22:23:36 +01001896 return false;
1897
Chris Wilson2f5945b2015-10-06 11:39:55 +01001898 /* Kernel clipping was a DRI1 misfeature */
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01001899 if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
1900 if (exec->num_cliprects || exec->cliprects_ptr)
1901 return false;
1902 }
Chris Wilson2f5945b2015-10-06 11:39:55 +01001903
1904 if (exec->DR4 == 0xffffffff) {
1905 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1906 exec->DR4 = 0;
1907 }
1908 if (exec->DR1 || exec->DR4)
1909 return false;
1910
1911 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1912 return false;
1913
1914 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001915}
1916
Chris Wilson5cf3d282016-08-04 07:52:43 +01001917void i915_vma_move_to_active(struct i915_vma *vma,
1918 struct drm_i915_gem_request *req,
1919 unsigned int flags)
1920{
1921 struct drm_i915_gem_object *obj = vma->obj;
1922 const unsigned int idx = req->engine->id;
1923
Chris Wilson81147b02016-12-18 15:37:18 +00001924 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001925 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1926
Chris Wilson2889caa2017-06-16 15:05:19 +01001927 /*
1928 * Add a reference if we're newly entering the active list.
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001929 * The order in which we add operations to the retirement queue is
1930 * vital here: mark_active adds to the start of the callback list,
1931 * such that subsequent callbacks are called first. Therefore we
1932 * add the active reference first and queue for it to be dropped
1933 * *last*.
1934 */
Chris Wilsond07f0e52016-10-28 13:58:44 +01001935 if (!i915_vma_is_active(vma))
1936 obj->active_count++;
1937 i915_vma_set_active(vma, idx);
1938 i915_gem_active_set(&vma->last_read[idx], req);
1939 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001940
Chris Wilsone27ab732017-06-15 13:38:49 +01001941 obj->base.write_domain = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001942 if (flags & EXEC_OBJECT_WRITE) {
Chris Wilsone27ab732017-06-15 13:38:49 +01001943 obj->base.write_domain = I915_GEM_DOMAIN_RENDER;
1944
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00001945 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
1946 i915_gem_active_set(&obj->frontbuffer_write, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001947
Chris Wilsone27ab732017-06-15 13:38:49 +01001948 obj->base.read_domains = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001949 }
Chris Wilsone27ab732017-06-15 13:38:49 +01001950 obj->base.read_domains |= I915_GEM_GPU_DOMAINS;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001951
Chris Wilson49ef5292016-08-18 17:17:00 +01001952 if (flags & EXEC_OBJECT_NEEDS_FENCE)
1953 i915_gem_active_set(&vma->last_fence, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001954}
1955
Chris Wilson2889caa2017-06-16 15:05:19 +01001956static int i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001957{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001958 u32 *cs;
1959 int i;
Eric Anholtae662d32012-01-03 09:23:29 -08001960
Chris Wilsonb5321f32016-08-02 22:50:18 +01001961 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001962 DRM_DEBUG("sol reset is gen7/rcs only\n");
1963 return -EINVAL;
1964 }
Eric Anholtae662d32012-01-03 09:23:29 -08001965
Chris Wilson2889caa2017-06-16 15:05:19 +01001966 cs = intel_ring_begin(req, 4 * 2 + 2);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001967 if (IS_ERR(cs))
1968 return PTR_ERR(cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001969
Chris Wilson2889caa2017-06-16 15:05:19 +01001970 *cs++ = MI_LOAD_REGISTER_IMM(4);
Eric Anholtae662d32012-01-03 09:23:29 -08001971 for (i = 0; i < 4; i++) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001972 *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
1973 *cs++ = 0;
Eric Anholtae662d32012-01-03 09:23:29 -08001974 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001975 *cs++ = MI_NOOP;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001976 intel_ring_advance(req, cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001977
1978 return 0;
1979}
1980
Chris Wilson650bc632017-06-15 09:14:33 +01001981static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001982{
Brad Volkin71745372014-12-11 12:13:12 -08001983 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001984 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001985 int err;
Brad Volkin71745372014-12-11 12:13:12 -08001986
Chris Wilson650bc632017-06-15 09:14:33 +01001987 shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool,
1988 PAGE_ALIGN(eb->batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001989 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001990 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001991
Chris Wilson2889caa2017-06-16 15:05:19 +01001992 err = intel_engine_cmd_parser(eb->engine,
Chris Wilson650bc632017-06-15 09:14:33 +01001993 eb->batch->obj,
Chris Wilson33a051a2016-07-27 09:07:26 +01001994 shadow_batch_obj,
Chris Wilson650bc632017-06-15 09:14:33 +01001995 eb->batch_start_offset,
1996 eb->batch_len,
Chris Wilson33a051a2016-07-27 09:07:26 +01001997 is_master);
Chris Wilson2889caa2017-06-16 15:05:19 +01001998 if (err) {
1999 if (err == -EACCES) /* unhandled chained batch */
Chris Wilson058d88c2016-08-15 10:49:06 +01002000 vma = NULL;
2001 else
Chris Wilson2889caa2017-06-16 15:05:19 +01002002 vma = ERR_PTR(err);
Chris Wilson058d88c2016-08-15 10:49:06 +01002003 goto out;
2004 }
Brad Volkin71745372014-12-11 12:13:12 -08002005
Chris Wilson058d88c2016-08-15 10:49:06 +01002006 vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
2007 if (IS_ERR(vma))
2008 goto out;
Chris Wilsonde4e7832015-04-07 16:20:35 +01002009
Chris Wilson650bc632017-06-15 09:14:33 +01002010 vma->exec_entry =
Chris Wilson2889caa2017-06-16 15:05:19 +01002011 memset(&eb->exec[eb->buffer_count++],
2012 0, sizeof(*vma->exec_entry));
Chris Wilsondade2a62017-06-16 15:05:20 +01002013 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF;
Chris Wilson2889caa2017-06-16 15:05:19 +01002014 __exec_to_vma(vma->exec_entry) = (uintptr_t)i915_vma_get(vma);
Brad Volkin71745372014-12-11 12:13:12 -08002015
Chris Wilson058d88c2016-08-15 10:49:06 +01002016out:
Chris Wilsonde4e7832015-04-07 16:20:35 +01002017 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson058d88c2016-08-15 10:49:06 +01002018 return vma;
Brad Volkin71745372014-12-11 12:13:12 -08002019}
Chris Wilson5c6c6002014-09-06 10:28:27 +01002020
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002021static void
Chris Wilson2889caa2017-06-16 15:05:19 +01002022add_to_client(struct drm_i915_gem_request *req, struct drm_file *file)
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002023{
2024 req->file_priv = file->driver_priv;
2025 list_add_tail(&req->client_link, &req->file_priv->mm.request_list);
2026}
2027
Chris Wilson2889caa2017-06-16 15:05:19 +01002028static int eb_submit(struct i915_execbuffer *eb)
Oscar Mateo78382592014-07-03 16:28:05 +01002029{
Chris Wilson2889caa2017-06-16 15:05:19 +01002030 int err;
Oscar Mateo78382592014-07-03 16:28:05 +01002031
Chris Wilson2889caa2017-06-16 15:05:19 +01002032 err = eb_move_to_gpu(eb);
2033 if (err)
2034 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01002035
Chris Wilson2889caa2017-06-16 15:05:19 +01002036 err = i915_switch_context(eb->request);
2037 if (err)
2038 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01002039
Chris Wilson650bc632017-06-15 09:14:33 +01002040 if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002041 err = i915_reset_gen7_sol_offsets(eb->request);
2042 if (err)
2043 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01002044 }
2045
Chris Wilson2889caa2017-06-16 15:05:19 +01002046 err = eb->engine->emit_bb_start(eb->request,
Chris Wilson650bc632017-06-15 09:14:33 +01002047 eb->batch->node.start +
2048 eb->batch_start_offset,
2049 eb->batch_len,
Chris Wilson2889caa2017-06-16 15:05:19 +01002050 eb->batch_flags);
2051 if (err)
2052 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01002053
Chris Wilson2f5945b2015-10-06 11:39:55 +01002054 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01002055}
2056
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002057/**
2058 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01002059 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002060 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002061static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01002062gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
2063 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002064{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002065 struct drm_i915_file_private *file_priv = file->driver_priv;
2066
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002067 /* Check whether the file_priv has already selected one ring. */
Joonas Lahtinen6f633402016-09-01 14:58:21 +03002068 if ((int)file_priv->bsd_engine < 0)
2069 file_priv->bsd_engine = atomic_fetch_xor(1,
2070 &dev_priv->mm.bsd_engine_dispatch_index);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002071
Chris Wilsonc80ff162016-07-27 09:07:27 +01002072 return file_priv->bsd_engine;
Chris Wilsond23db882014-05-23 08:48:08 +02002073}
2074
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002075#define I915_USER_RINGS (4)
2076
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002077static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002078 [I915_EXEC_DEFAULT] = RCS,
2079 [I915_EXEC_RENDER] = RCS,
2080 [I915_EXEC_BLT] = BCS,
2081 [I915_EXEC_BSD] = VCS,
2082 [I915_EXEC_VEBOX] = VECS
2083};
2084
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002085static struct intel_engine_cs *
2086eb_select_engine(struct drm_i915_private *dev_priv,
2087 struct drm_file *file,
2088 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002089{
2090 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002091 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002092
2093 if (user_ring_id > I915_USER_RINGS) {
2094 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002095 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002096 }
2097
2098 if ((user_ring_id != I915_EXEC_BSD) &&
2099 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
2100 DRM_DEBUG("execbuf with non bsd ring but with invalid "
2101 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002102 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002103 }
2104
2105 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
2106 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
2107
2108 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01002109 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002110 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
2111 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00002112 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002113 bsd_idx--;
2114 } else {
2115 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
2116 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002117 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002118 }
2119
Akash Goel3b3f1652016-10-13 22:44:48 +05302120 engine = dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002121 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +05302122 engine = dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002123 }
2124
Akash Goel3b3f1652016-10-13 22:44:48 +05302125 if (!engine) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002126 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002127 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002128 }
2129
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002130 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002131}
2132
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002133static void
2134__free_fence_array(struct drm_syncobj **fences, unsigned int n)
2135{
2136 while (n--)
2137 drm_syncobj_put(ptr_mask_bits(fences[n], 2));
2138 kvfree(fences);
2139}
2140
2141static struct drm_syncobj **
2142get_fence_array(struct drm_i915_gem_execbuffer2 *args,
2143 struct drm_file *file)
2144{
2145 const unsigned int nfences = args->num_cliprects;
2146 struct drm_i915_gem_exec_fence __user *user;
2147 struct drm_syncobj **fences;
2148 unsigned int n;
2149 int err;
2150
2151 if (!(args->flags & I915_EXEC_FENCE_ARRAY))
2152 return NULL;
2153
2154 if (nfences > SIZE_MAX / sizeof(*fences))
2155 return ERR_PTR(-EINVAL);
2156
2157 user = u64_to_user_ptr(args->cliprects_ptr);
2158 if (!access_ok(VERIFY_READ, user, nfences * 2 * sizeof(u32)))
2159 return ERR_PTR(-EFAULT);
2160
2161 fences = kvmalloc_array(args->num_cliprects, sizeof(*fences),
2162 __GFP_NOWARN | GFP_TEMPORARY);
2163 if (!fences)
2164 return ERR_PTR(-ENOMEM);
2165
2166 for (n = 0; n < nfences; n++) {
2167 struct drm_i915_gem_exec_fence fence;
2168 struct drm_syncobj *syncobj;
2169
2170 if (__copy_from_user(&fence, user++, sizeof(fence))) {
2171 err = -EFAULT;
2172 goto err;
2173 }
2174
2175 syncobj = drm_syncobj_find(file, fence.handle);
2176 if (!syncobj) {
2177 DRM_DEBUG("Invalid syncobj handle provided\n");
2178 err = -ENOENT;
2179 goto err;
2180 }
2181
2182 fences[n] = ptr_pack_bits(syncobj, fence.flags, 2);
2183 }
2184
2185 return fences;
2186
2187err:
2188 __free_fence_array(fences, n);
2189 return ERR_PTR(err);
2190}
2191
2192static void
2193put_fence_array(struct drm_i915_gem_execbuffer2 *args,
2194 struct drm_syncobj **fences)
2195{
2196 if (fences)
2197 __free_fence_array(fences, args->num_cliprects);
2198}
2199
2200static int
2201await_fence_array(struct i915_execbuffer *eb,
2202 struct drm_syncobj **fences)
2203{
2204 const unsigned int nfences = eb->args->num_cliprects;
2205 unsigned int n;
2206 int err;
2207
2208 for (n = 0; n < nfences; n++) {
2209 struct drm_syncobj *syncobj;
2210 struct dma_fence *fence;
2211 unsigned int flags;
2212
2213 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2214 if (!(flags & I915_EXEC_FENCE_WAIT))
2215 continue;
2216
2217 rcu_read_lock();
2218 fence = dma_fence_get_rcu_safe(&syncobj->fence);
2219 rcu_read_unlock();
2220 if (!fence)
2221 return -EINVAL;
2222
2223 err = i915_gem_request_await_dma_fence(eb->request, fence);
2224 dma_fence_put(fence);
2225 if (err < 0)
2226 return err;
2227 }
2228
2229 return 0;
2230}
2231
2232static void
2233signal_fence_array(struct i915_execbuffer *eb,
2234 struct drm_syncobj **fences)
2235{
2236 const unsigned int nfences = eb->args->num_cliprects;
2237 struct dma_fence * const fence = &eb->request->fence;
2238 unsigned int n;
2239
2240 for (n = 0; n < nfences; n++) {
2241 struct drm_syncobj *syncobj;
2242 unsigned int flags;
2243
2244 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2245 if (!(flags & I915_EXEC_FENCE_SIGNAL))
2246 continue;
2247
2248 drm_syncobj_replace_fence(syncobj, fence);
2249 }
2250}
2251
Eric Anholtae662d32012-01-03 09:23:29 -08002252static int
Chris Wilson650bc632017-06-15 09:14:33 +01002253i915_gem_do_execbuffer(struct drm_device *dev,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002254 struct drm_file *file,
2255 struct drm_i915_gem_execbuffer2 *args,
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002256 struct drm_i915_gem_exec_object2 *exec,
2257 struct drm_syncobj **fences)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002258{
Chris Wilson650bc632017-06-15 09:14:33 +01002259 struct i915_execbuffer eb;
Chris Wilsonfec04452017-01-27 09:40:08 +00002260 struct dma_fence *in_fence = NULL;
2261 struct sync_file *out_fence = NULL;
2262 int out_fence_fd = -1;
Chris Wilson2889caa2017-06-16 15:05:19 +01002263 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002264
Chris Wilson2889caa2017-06-16 15:05:19 +01002265 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
2266 ~__EXEC_OBJECT_UNKNOWN_FLAGS);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002267
Chris Wilson650bc632017-06-15 09:14:33 +01002268 eb.i915 = to_i915(dev);
2269 eb.file = file;
2270 eb.args = args;
Chris Wilson7dd4f672017-06-16 15:05:24 +01002271 if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC))
Chris Wilson2889caa2017-06-16 15:05:19 +01002272 args->flags |= __EXEC_HAS_RELOC;
Chris Wilson650bc632017-06-15 09:14:33 +01002273 eb.exec = exec;
Chris Wilson2889caa2017-06-16 15:05:19 +01002274 eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
2275 if (USES_FULL_PPGTT(eb.i915))
2276 eb.invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson650bc632017-06-15 09:14:33 +01002277 reloc_cache_init(&eb.reloc_cache, eb.i915);
2278
Chris Wilson2889caa2017-06-16 15:05:19 +01002279 eb.buffer_count = args->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +01002280 eb.batch_start_offset = args->batch_start_offset;
2281 eb.batch_len = args->batch_len;
2282
Chris Wilson2889caa2017-06-16 15:05:19 +01002283 eb.batch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002284 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02002285 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002286 return -EPERM;
2287
Chris Wilson2889caa2017-06-16 15:05:19 +01002288 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002289 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01002290 if (args->flags & I915_EXEC_IS_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +01002291 eb.batch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002292
Chris Wilson650bc632017-06-15 09:14:33 +01002293 eb.engine = eb_select_engine(eb.i915, file, args);
2294 if (!eb.engine)
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002295 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002296
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002297 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
Chris Wilson650bc632017-06-15 09:14:33 +01002298 if (!HAS_RESOURCE_STREAMER(eb.i915)) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002299 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
2300 return -EINVAL;
2301 }
Chris Wilson650bc632017-06-15 09:14:33 +01002302 if (eb.engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002303 DRM_DEBUG("RS is not available on %s\n",
Chris Wilson650bc632017-06-15 09:14:33 +01002304 eb.engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002305 return -EINVAL;
2306 }
2307
Chris Wilson2889caa2017-06-16 15:05:19 +01002308 eb.batch_flags |= I915_DISPATCH_RS;
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002309 }
2310
Chris Wilsonfec04452017-01-27 09:40:08 +00002311 if (args->flags & I915_EXEC_FENCE_IN) {
2312 in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002313 if (!in_fence)
2314 return -EINVAL;
Chris Wilsonfec04452017-01-27 09:40:08 +00002315 }
2316
2317 if (args->flags & I915_EXEC_FENCE_OUT) {
2318 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
2319 if (out_fence_fd < 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002320 err = out_fence_fd;
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002321 goto err_in_fence;
Chris Wilsonfec04452017-01-27 09:40:08 +00002322 }
2323 }
2324
Chris Wilson4d470f72017-06-29 16:04:25 +01002325 err = eb_create(&eb);
2326 if (err)
2327 goto err_out_fence;
2328
2329 GEM_BUG_ON(!eb.lut_size);
Chris Wilson2889caa2017-06-16 15:05:19 +01002330
Chris Wilson1acfc102017-06-20 12:05:47 +01002331 err = eb_select_context(&eb);
2332 if (unlikely(err))
2333 goto err_destroy;
2334
Chris Wilson2889caa2017-06-16 15:05:19 +01002335 /*
2336 * Take a local wakeref for preparing to dispatch the execbuf as
Chris Wilson67d97da2016-07-04 08:08:31 +01002337 * we expect to access the hardware fairly frequently in the
2338 * process. Upon first dispatch, we acquire another prolonged
2339 * wakeref that we hold until the GPU has been idle for at least
2340 * 100ms.
2341 */
Chris Wilson650bc632017-06-15 09:14:33 +01002342 intel_runtime_pm_get(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002343
Chris Wilson2889caa2017-06-16 15:05:19 +01002344 err = i915_mutex_lock_interruptible(dev);
2345 if (err)
2346 goto err_rpm;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002347
Chris Wilson2889caa2017-06-16 15:05:19 +01002348 err = eb_relocate(&eb);
Chris Wilson1f727d92017-07-21 15:50:36 +01002349 if (err) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002350 /*
2351 * If the user expects the execobject.offset and
2352 * reloc.presumed_offset to be an exact match,
2353 * as for using NO_RELOC, then we cannot update
2354 * the execobject.offset until we have completed
2355 * relocation.
2356 */
2357 args->flags &= ~__EXEC_HAS_RELOC;
Chris Wilson2889caa2017-06-16 15:05:19 +01002358 goto err_vma;
Chris Wilson1f727d92017-07-21 15:50:36 +01002359 }
Ben Widawsky41bde552013-12-06 14:11:21 -08002360
Chris Wilson2889caa2017-06-16 15:05:19 +01002361 if (unlikely(eb.batch->exec_entry->flags & EXEC_OBJECT_WRITE)) {
Daniel Vetterff240192012-01-31 21:08:14 +01002362 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002363 err = -EINVAL;
2364 goto err_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002365 }
Chris Wilson650bc632017-06-15 09:14:33 +01002366 if (eb.batch_start_offset > eb.batch->size ||
2367 eb.batch_len > eb.batch->size - eb.batch_start_offset) {
Chris Wilson0b537272016-08-18 17:17:12 +01002368 DRM_DEBUG("Attempting to use out-of-bounds batch\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002369 err = -EINVAL;
2370 goto err_vma;
Chris Wilson0b537272016-08-18 17:17:12 +01002371 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002372
Chris Wilson650bc632017-06-15 09:14:33 +01002373 if (eb.engine->needs_cmd_parser && eb.batch_len) {
Chris Wilson59bfa122016-08-04 16:32:31 +01002374 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002375
Chris Wilson650bc632017-06-15 09:14:33 +01002376 vma = eb_parse(&eb, drm_is_current_master(file));
Chris Wilson59bfa122016-08-04 16:32:31 +01002377 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002378 err = PTR_ERR(vma);
2379 goto err_vma;
Brad Volkin78a42372014-12-11 12:13:09 -08002380 }
Chris Wilson17cabf52015-01-14 11:20:57 +00002381
Chris Wilson59bfa122016-08-04 16:32:31 +01002382 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002383 /*
2384 * Batch parsed and accepted:
2385 *
2386 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
2387 * bit from MI_BATCH_BUFFER_START commands issued in
2388 * the dispatch_execbuffer implementations. We
2389 * specifically don't want that set on batches the
2390 * command parser has accepted.
2391 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002392 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilson650bc632017-06-15 09:14:33 +01002393 eb.batch_start_offset = 0;
2394 eb.batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002395 }
Brad Volkin351e3db2014-02-18 10:15:46 -08002396 }
2397
Chris Wilson650bc632017-06-15 09:14:33 +01002398 if (eb.batch_len == 0)
2399 eb.batch_len = eb.batch->size - eb.batch_start_offset;
Brad Volkin78a42372014-12-11 12:13:09 -08002400
Chris Wilson2889caa2017-06-16 15:05:19 +01002401 /*
2402 * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002403 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07002404 * hsw should have this fixed, but bdw mucks it up again. */
Chris Wilson2889caa2017-06-16 15:05:19 +01002405 if (eb.batch_flags & I915_DISPATCH_SECURE) {
Chris Wilson058d88c2016-08-15 10:49:06 +01002406 struct i915_vma *vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002407
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002408 /*
2409 * So on first glance it looks freaky that we pin the batch here
2410 * outside of the reservation loop. But:
2411 * - The batch is already pinned into the relevant ppgtt, so we
2412 * already have the backing storage fully allocated.
2413 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01002414 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002415 * fitting due to fragmentation.
2416 * So this is actually safe.
2417 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002418 vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0);
Chris Wilson058d88c2016-08-15 10:49:06 +01002419 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002420 err = PTR_ERR(vma);
2421 goto err_vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01002422 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002423
Chris Wilson650bc632017-06-15 09:14:33 +01002424 eb.batch = vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002425 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002426
Chris Wilson7dd4f672017-06-16 15:05:24 +01002427 /* All GPU relocation batches must be submitted prior to the user rq */
2428 GEM_BUG_ON(eb.reloc_cache.rq);
2429
John Harrison0c8dac82015-05-29 17:43:25 +01002430 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson650bc632017-06-15 09:14:33 +01002431 eb.request = i915_gem_request_alloc(eb.engine, eb.ctx);
2432 if (IS_ERR(eb.request)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002433 err = PTR_ERR(eb.request);
John Harrison0c8dac82015-05-29 17:43:25 +01002434 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00002435 }
John Harrison0c8dac82015-05-29 17:43:25 +01002436
Chris Wilsonfec04452017-01-27 09:40:08 +00002437 if (in_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002438 err = i915_gem_request_await_dma_fence(eb.request, in_fence);
2439 if (err < 0)
Chris Wilsonfec04452017-01-27 09:40:08 +00002440 goto err_request;
2441 }
2442
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002443 if (fences) {
2444 err = await_fence_array(&eb, fences);
2445 if (err)
2446 goto err_request;
2447 }
2448
Chris Wilsonfec04452017-01-27 09:40:08 +00002449 if (out_fence_fd != -1) {
Chris Wilson650bc632017-06-15 09:14:33 +01002450 out_fence = sync_file_create(&eb.request->fence);
Chris Wilsonfec04452017-01-27 09:40:08 +00002451 if (!out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002452 err = -ENOMEM;
Chris Wilsonfec04452017-01-27 09:40:08 +00002453 goto err_request;
2454 }
2455 }
2456
Chris Wilson2889caa2017-06-16 15:05:19 +01002457 /*
2458 * Whilst this request exists, batch_obj will be on the
Chris Wilson17f298cf2016-08-10 13:41:46 +01002459 * active_list, and so will hold the active reference. Only when this
2460 * request is retired will the the batch_obj be moved onto the
2461 * inactive_list and lose its active reference. Hence we do not need
2462 * to explicitly hold another reference here.
2463 */
Chris Wilson650bc632017-06-15 09:14:33 +01002464 eb.request->batch = eb.batch;
Chris Wilson17f298cf2016-08-10 13:41:46 +01002465
Chris Wilson2889caa2017-06-16 15:05:19 +01002466 trace_i915_gem_request_queue(eb.request, eb.batch_flags);
2467 err = eb_submit(&eb);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01002468err_request:
Chris Wilson2889caa2017-06-16 15:05:19 +01002469 __i915_add_request(eb.request, err == 0);
Chris Wilson650bc632017-06-15 09:14:33 +01002470 add_to_client(eb.request, file);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002471
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002472 if (fences)
2473 signal_fence_array(&eb, fences);
2474
Chris Wilsonfec04452017-01-27 09:40:08 +00002475 if (out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002476 if (err == 0) {
Chris Wilsonfec04452017-01-27 09:40:08 +00002477 fd_install(out_fence_fd, out_fence->file);
2478 args->rsvd2 &= GENMASK_ULL(0, 31); /* keep in-fence */
2479 args->rsvd2 |= (u64)out_fence_fd << 32;
2480 out_fence_fd = -1;
2481 } else {
2482 fput(out_fence->file);
2483 }
2484 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002485
John Harrison0c8dac82015-05-29 17:43:25 +01002486err_batch_unpin:
Chris Wilson2889caa2017-06-16 15:05:19 +01002487 if (eb.batch_flags & I915_DISPATCH_SECURE)
Chris Wilson650bc632017-06-15 09:14:33 +01002488 i915_vma_unpin(eb.batch);
Chris Wilson2889caa2017-06-16 15:05:19 +01002489err_vma:
2490 if (eb.exec)
2491 eb_release_vmas(&eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002492 mutex_unlock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01002493err_rpm:
Chris Wilson650bc632017-06-15 09:14:33 +01002494 intel_runtime_pm_put(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002495 i915_gem_context_put(eb.ctx);
2496err_destroy:
Chris Wilson2889caa2017-06-16 15:05:19 +01002497 eb_destroy(&eb);
Chris Wilson4d470f72017-06-29 16:04:25 +01002498err_out_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002499 if (out_fence_fd != -1)
2500 put_unused_fd(out_fence_fd);
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002501err_in_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002502 dma_fence_put(in_fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01002503 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002504}
2505
2506/*
2507 * Legacy execbuffer just creates an exec2 list from the original exec object
2508 * list array and passes it to the real function.
2509 */
2510int
2511i915_gem_execbuffer(struct drm_device *dev, void *data,
2512 struct drm_file *file)
2513{
Chris Wilson2889caa2017-06-16 15:05:19 +01002514 const size_t sz = sizeof(struct drm_i915_gem_exec_object2);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002515 struct drm_i915_gem_execbuffer *args = data;
2516 struct drm_i915_gem_execbuffer2 exec2;
2517 struct drm_i915_gem_exec_object *exec_list = NULL;
2518 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +01002519 unsigned int i;
2520 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002521
Chris Wilson2889caa2017-06-16 15:05:19 +01002522 if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) {
2523 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002524 return -EINVAL;
2525 }
2526
Chris Wilson2889caa2017-06-16 15:05:19 +01002527 exec2.buffers_ptr = args->buffers_ptr;
2528 exec2.buffer_count = args->buffer_count;
2529 exec2.batch_start_offset = args->batch_start_offset;
2530 exec2.batch_len = args->batch_len;
2531 exec2.DR1 = args->DR1;
2532 exec2.DR4 = args->DR4;
2533 exec2.num_cliprects = args->num_cliprects;
2534 exec2.cliprects_ptr = args->cliprects_ptr;
2535 exec2.flags = I915_EXEC_RENDER;
2536 i915_execbuffer2_set_context_id(exec2, 0);
2537
2538 if (!i915_gem_check_execbuffer(&exec2))
2539 return -EINVAL;
2540
Chris Wilson54cf91d2010-11-25 18:00:26 +00002541 /* Copy in the exec list from userland */
Chris Wilson2889caa2017-06-16 15:05:19 +01002542 exec_list = kvmalloc_array(args->buffer_count, sizeof(*exec_list),
2543 __GFP_NOWARN | GFP_TEMPORARY);
2544 exec2_list = kvmalloc_array(args->buffer_count + 1, sz,
2545 __GFP_NOWARN | GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002546 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002547 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002548 args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002549 kvfree(exec_list);
2550 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002551 return -ENOMEM;
2552 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002553 err = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002554 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00002555 sizeof(*exec_list) * args->buffer_count);
Chris Wilson2889caa2017-06-16 15:05:19 +01002556 if (err) {
Daniel Vetterff240192012-01-31 21:08:14 +01002557 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson2889caa2017-06-16 15:05:19 +01002558 args->buffer_count, err);
Michal Hocko20981052017-05-17 14:23:12 +02002559 kvfree(exec_list);
2560 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002561 return -EFAULT;
2562 }
2563
2564 for (i = 0; i < args->buffer_count; i++) {
2565 exec2_list[i].handle = exec_list[i].handle;
2566 exec2_list[i].relocation_count = exec_list[i].relocation_count;
2567 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
2568 exec2_list[i].alignment = exec_list[i].alignment;
2569 exec2_list[i].offset = exec_list[i].offset;
Tvrtko Ursulinf0836b72016-11-16 08:55:32 +00002570 if (INTEL_GEN(to_i915(dev)) < 4)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002571 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
2572 else
2573 exec2_list[i].flags = 0;
2574 }
2575
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002576 err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list, NULL);
Chris Wilson2889caa2017-06-16 15:05:19 +01002577 if (exec2.flags & __EXEC_HAS_RELOC) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002578 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002579 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002580
Chris Wilson54cf91d2010-11-25 18:00:26 +00002581 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002582 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002583 if (!(exec2_list[i].offset & UPDATE))
2584 continue;
2585
Michał Winiarski934acce2015-12-29 18:24:52 +01002586 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002587 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2588 exec2_list[i].offset &= PIN_OFFSET_MASK;
2589 if (__copy_to_user(&user_exec_list[i].offset,
2590 &exec2_list[i].offset,
2591 sizeof(user_exec_list[i].offset)))
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002592 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002593 }
2594 }
2595
Michal Hocko20981052017-05-17 14:23:12 +02002596 kvfree(exec_list);
2597 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002598 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002599}
2600
2601int
2602i915_gem_execbuffer2(struct drm_device *dev, void *data,
2603 struct drm_file *file)
2604{
Chris Wilson2889caa2017-06-16 15:05:19 +01002605 const size_t sz = sizeof(struct drm_i915_gem_exec_object2);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002606 struct drm_i915_gem_execbuffer2 *args = data;
Chris Wilson2889caa2017-06-16 15:05:19 +01002607 struct drm_i915_gem_exec_object2 *exec2_list;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002608 struct drm_syncobj **fences = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +01002609 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002610
Chris Wilson2889caa2017-06-16 15:05:19 +01002611 if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01002612 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002613 return -EINVAL;
2614 }
2615
Chris Wilson2889caa2017-06-16 15:05:19 +01002616 if (!i915_gem_check_execbuffer(args))
2617 return -EINVAL;
2618
2619 /* Allocate an extra slot for use by the command parser */
2620 exec2_list = kvmalloc_array(args->buffer_count + 1, sz,
2621 __GFP_NOWARN | GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002622 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002623 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002624 args->buffer_count);
2625 return -ENOMEM;
2626 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002627 if (copy_from_user(exec2_list,
2628 u64_to_user_ptr(args->buffers_ptr),
2629 sizeof(*exec2_list) * args->buffer_count)) {
2630 DRM_DEBUG("copy %d exec entries failed\n", args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002631 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002632 return -EFAULT;
2633 }
2634
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002635 if (args->flags & I915_EXEC_FENCE_ARRAY) {
2636 fences = get_fence_array(args, file);
2637 if (IS_ERR(fences)) {
2638 kvfree(exec2_list);
2639 return PTR_ERR(fences);
2640 }
2641 }
2642
2643 err = i915_gem_do_execbuffer(dev, file, args, exec2_list, fences);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002644
Chris Wilson2889caa2017-06-16 15:05:19 +01002645 /*
2646 * Now that we have begun execution of the batchbuffer, we ignore
2647 * any new error after this point. Also given that we have already
2648 * updated the associated relocations, we try to write out the current
2649 * object locations irrespective of any error.
2650 */
2651 if (args->flags & __EXEC_HAS_RELOC) {
2652 struct drm_i915_gem_exec_object2 __user *user_exec_list =
2653 u64_to_user_ptr(args->buffers_ptr);
2654 unsigned int i;
2655
2656 /* Copy the new buffer offsets back to the user's exec list. */
2657 user_access_begin();
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002658 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002659 if (!(exec2_list[i].offset & UPDATE))
2660 continue;
2661
Michał Winiarski934acce2015-12-29 18:24:52 +01002662 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002663 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2664 unsafe_put_user(exec2_list[i].offset,
2665 &user_exec_list[i].offset,
2666 end_user);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002667 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002668end_user:
2669 user_access_end();
Chris Wilson54cf91d2010-11-25 18:00:26 +00002670 }
2671
Chris Wilson2889caa2017-06-16 15:05:19 +01002672 args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002673 put_fence_array(args, fences);
Michal Hocko20981052017-05-17 14:23:12 +02002674 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002675 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002676}