blob: 67317b79b9e096062384600688d7ae32d297b325 [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[] */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100195 struct i915_vma **vma;
196 unsigned int *flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100197
198 struct intel_engine_cs *engine; /** engine to queue the request to */
199 struct i915_gem_context *ctx; /** context for building the request */
200 struct i915_address_space *vm; /** GTT and vma for the request */
201
202 struct drm_i915_gem_request *request; /** our request to build */
203 struct i915_vma *batch; /** identity of the batch obj/vma */
204
205 /** actual size of execobj[] as we may extend it for the cmdparser */
206 unsigned int buffer_count;
207
208 /** list of vma not yet bound during reservation phase */
209 struct list_head unbound;
210
211 /** list of vma that have execobj.relocation_count */
212 struct list_head relocs;
213
214 /**
215 * Track the most recently used object for relocations, as we
216 * frequently have to perform multiple relocations within the same
217 * obj/page
218 */
Chris Wilson650bc632017-06-15 09:14:33 +0100219 struct reloc_cache {
Chris Wilson2889caa2017-06-16 15:05:19 +0100220 struct drm_mm_node node; /** temporary GTT binding */
221 unsigned long vaddr; /** Current kmap address */
222 unsigned long page; /** Currently mapped page index */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100223 unsigned int gen; /** Cached value of INTEL_GEN */
Chris Wilson650bc632017-06-15 09:14:33 +0100224 bool use_64bit_reloc : 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100225 bool has_llc : 1;
226 bool has_fence : 1;
227 bool needs_unfenced : 1;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100228
229 struct drm_i915_gem_request *rq;
230 u32 *rq_cmd;
231 unsigned int rq_size;
Chris Wilson650bc632017-06-15 09:14:33 +0100232 } reloc_cache;
Chris Wilson2889caa2017-06-16 15:05:19 +0100233
234 u64 invalid_flags; /** Set of execobj.flags that are invalid */
235 u32 context_flags; /** Set of execobj.flags to insert from the ctx */
236
237 u32 batch_start_offset; /** Location within object of batch */
238 u32 batch_len; /** Length of batch within object */
239 u32 batch_flags; /** Flags composed for emit_bb_start() */
240
241 /**
242 * Indicate either the size of the hastable used to resolve
243 * relocation handles, or if negative that we are using a direct
244 * index into the execobj[].
245 */
246 int lut_size;
247 struct hlist_head *buckets; /** ht for relocation handles */
Chris Wilson67731b82010-12-08 10:38:14 +0000248};
249
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100250#define exec_entry(EB, VMA) (&(EB)->exec[(VMA)->exec_flags - (EB)->flags])
Chris Wilson4ff4b442017-06-16 15:05:16 +0100251
Chris Wilson2889caa2017-06-16 15:05:19 +0100252/*
253 * Used to convert any address to canonical form.
254 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
255 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
256 * addresses to be in a canonical form:
257 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
258 * canonical form [63:48] == [47]."
259 */
260#define GEN8_HIGH_ADDRESS_BIT 47
261static inline u64 gen8_canonical_addr(u64 address)
262{
263 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
264}
265
266static inline u64 gen8_noncanonical_addr(u64 address)
267{
268 return address & GENMASK_ULL(GEN8_HIGH_ADDRESS_BIT, 0);
269}
270
Chris Wilson3dbf26e2017-08-26 14:56:20 +0100271static inline bool eb_use_cmdparser(const struct i915_execbuffer *eb)
272{
273 return eb->engine->needs_cmd_parser && eb->batch_len;
274}
275
Chris Wilson650bc632017-06-15 09:14:33 +0100276static int eb_create(struct i915_execbuffer *eb)
Chris Wilson67731b82010-12-08 10:38:14 +0000277{
Chris Wilson2889caa2017-06-16 15:05:19 +0100278 if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) {
279 unsigned int size = 1 + ilog2(eb->buffer_count);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000280
Chris Wilson2889caa2017-06-16 15:05:19 +0100281 /*
282 * Without a 1:1 association between relocation handles and
283 * the execobject[] index, we instead create a hashtable.
284 * We size it dynamically based on available memory, starting
285 * first with 1:1 assocative hash and scaling back until
286 * the allocation succeeds.
287 *
288 * Later on we use a positive lut_size to indicate we are
289 * using this hashtable, and a negative value to indicate a
290 * direct lookup.
291 */
Chris Wilson4ff4b442017-06-16 15:05:16 +0100292 do {
Chris Wilson0d95c882017-09-01 15:57:28 +0100293 gfp_t flags;
Chris Wilson4d470f72017-06-29 16:04:25 +0100294
295 /* While we can still reduce the allocation size, don't
296 * raise a warning and allow the allocation to fail.
297 * On the last pass though, we want to try as hard
298 * as possible to perform the allocation and warn
299 * if it fails.
300 */
301 flags = GFP_TEMPORARY;
302 if (size > 1)
303 flags |= __GFP_NORETRY | __GFP_NOWARN;
304
Chris Wilson4ff4b442017-06-16 15:05:16 +0100305 eb->buckets = kzalloc(sizeof(struct hlist_head) << size,
Chris Wilson4d470f72017-06-29 16:04:25 +0100306 flags);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100307 if (eb->buckets)
308 break;
309 } while (--size);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000310
Chris Wilson4d470f72017-06-29 16:04:25 +0100311 if (unlikely(!size))
312 return -ENOMEM;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100313
Chris Wilson2889caa2017-06-16 15:05:19 +0100314 eb->lut_size = size;
Chris Wilson650bc632017-06-15 09:14:33 +0100315 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +0100316 eb->lut_size = -eb->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +0100317 }
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000318
Chris Wilson650bc632017-06-15 09:14:33 +0100319 return 0;
Chris Wilson67731b82010-12-08 10:38:14 +0000320}
321
Chris Wilson2889caa2017-06-16 15:05:19 +0100322static bool
323eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry,
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100324 const struct i915_vma *vma,
325 unsigned int flags)
Chris Wilson2889caa2017-06-16 15:05:19 +0100326{
Chris Wilson2889caa2017-06-16 15:05:19 +0100327 if (vma->node.size < entry->pad_to_size)
328 return true;
329
330 if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment))
331 return true;
332
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100333 if (flags & EXEC_OBJECT_PINNED &&
Chris Wilson2889caa2017-06-16 15:05:19 +0100334 vma->node.start != entry->offset)
335 return true;
336
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100337 if (flags & __EXEC_OBJECT_NEEDS_BIAS &&
Chris Wilson2889caa2017-06-16 15:05:19 +0100338 vma->node.start < BATCH_OFFSET_BIAS)
339 return true;
340
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100341 if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) &&
Chris Wilson2889caa2017-06-16 15:05:19 +0100342 (vma->node.start + vma->node.size - 1) >> 32)
343 return true;
344
345 return false;
346}
347
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100348static inline bool
Chris Wilson2889caa2017-06-16 15:05:19 +0100349eb_pin_vma(struct i915_execbuffer *eb,
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100350 const struct drm_i915_gem_exec_object2 *entry,
Chris Wilson2889caa2017-06-16 15:05:19 +0100351 struct i915_vma *vma)
352{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100353 unsigned int exec_flags = *vma->exec_flags;
354 u64 pin_flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100355
Chris Wilson616d9ce2017-06-16 15:05:21 +0100356 if (vma->node.size)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100357 pin_flags = vma->node.start;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100358 else
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100359 pin_flags = entry->offset & PIN_OFFSET_MASK;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100360
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100361 pin_flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED;
362 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_GTT))
363 pin_flags |= PIN_GLOBAL;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100364
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100365 if (unlikely(i915_vma_pin(vma, 0, 0, pin_flags)))
366 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100367
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100368 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100369 if (unlikely(i915_vma_get_fence(vma))) {
370 i915_vma_unpin(vma);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100371 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100372 }
373
374 if (i915_vma_pin_fence(vma))
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100375 exec_flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100376 }
377
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100378 *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN;
379 return !eb_vma_misplaced(entry, vma, exec_flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100380}
381
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100382static inline void __eb_unreserve_vma(struct i915_vma *vma, unsigned int flags)
Chris Wilsond55495b2017-06-15 09:14:34 +0100383{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100384 GEM_BUG_ON(!(flags & __EXEC_OBJECT_HAS_PIN));
Chris Wilson2889caa2017-06-16 15:05:19 +0100385
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100386 if (unlikely(flags & __EXEC_OBJECT_HAS_FENCE))
Chris Wilsond55495b2017-06-15 09:14:34 +0100387 i915_vma_unpin_fence(vma);
388
Chris Wilson2889caa2017-06-16 15:05:19 +0100389 __i915_vma_unpin(vma);
Chris Wilsond55495b2017-06-15 09:14:34 +0100390}
391
Chris Wilson2889caa2017-06-16 15:05:19 +0100392static inline void
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100393eb_unreserve_vma(struct i915_vma *vma, unsigned int *flags)
Chris Wilsond55495b2017-06-15 09:14:34 +0100394{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100395 if (!(*flags & __EXEC_OBJECT_HAS_PIN))
Chris Wilson2889caa2017-06-16 15:05:19 +0100396 return;
Chris Wilsond55495b2017-06-15 09:14:34 +0100397
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100398 __eb_unreserve_vma(vma, *flags);
399 *flags &= ~__EXEC_OBJECT_RESERVED;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100400}
401
402static int
Chris Wilson2889caa2017-06-16 15:05:19 +0100403eb_validate_vma(struct i915_execbuffer *eb,
404 struct drm_i915_gem_exec_object2 *entry,
405 struct i915_vma *vma)
406{
407 if (unlikely(entry->flags & eb->invalid_flags))
408 return -EINVAL;
409
410 if (unlikely(entry->alignment && !is_power_of_2(entry->alignment)))
411 return -EINVAL;
412
413 /*
414 * Offset can be used as input (EXEC_OBJECT_PINNED), reject
415 * any non-page-aligned or non-canonical addresses.
416 */
417 if (unlikely(entry->flags & EXEC_OBJECT_PINNED &&
418 entry->offset != gen8_canonical_addr(entry->offset & PAGE_MASK)))
419 return -EINVAL;
420
421 /* pad_to_size was once a reserved field, so sanitize it */
422 if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) {
423 if (unlikely(offset_in_page(entry->pad_to_size)))
424 return -EINVAL;
425 } else {
426 entry->pad_to_size = 0;
427 }
428
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100429 if (unlikely(vma->exec_flags)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100430 DRM_DEBUG("Object [handle %d, index %d] appears more than once in object list\n",
431 entry->handle, (int)(entry - eb->exec));
432 return -EINVAL;
433 }
434
435 /*
436 * From drm_mm perspective address space is continuous,
437 * so from this point we're always using non-canonical
438 * form internally.
439 */
440 entry->offset = gen8_noncanonical_addr(entry->offset);
441
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100442 if (!eb->reloc_cache.has_fence) {
443 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
444 } else {
445 if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE ||
446 eb->reloc_cache.needs_unfenced) &&
447 i915_gem_object_is_tiled(vma->obj))
448 entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP;
449 }
450
451 if (!(entry->flags & EXEC_OBJECT_PINNED))
452 entry->flags |= eb->context_flags;
453
Chris Wilson2889caa2017-06-16 15:05:19 +0100454 return 0;
455}
456
457static int
Chris Wilsond1b48c12017-08-16 09:52:08 +0100458eb_add_vma(struct i915_execbuffer *eb, unsigned int i, struct i915_vma *vma)
Chris Wilson2889caa2017-06-16 15:05:19 +0100459{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100460 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100461 int err;
462
463 GEM_BUG_ON(i915_vma_is_closed(vma));
464
465 if (!(eb->args->flags & __EXEC_VALIDATED)) {
466 err = eb_validate_vma(eb, entry, vma);
467 if (unlikely(err))
468 return err;
469 }
470
Chris Wilson4d470f72017-06-29 16:04:25 +0100471 if (eb->lut_size > 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100472 vma->exec_handle = entry->handle;
473 hlist_add_head(&vma->exec_node,
474 &eb->buckets[hash_32(entry->handle,
475 eb->lut_size)]);
476 }
477
478 if (entry->relocation_count)
479 list_add_tail(&vma->reloc_link, &eb->relocs);
480
Chris Wilson2889caa2017-06-16 15:05:19 +0100481 /*
482 * Stash a pointer from the vma to execobj, so we can query its flags,
483 * size, alignment etc as provided by the user. Also we stash a pointer
484 * to the vma inside the execobj so that we can use a direct lookup
485 * to find the right target VMA when doing relocations.
486 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100487 eb->vma[i] = vma;
Chris Wilsond1b48c12017-08-16 09:52:08 +0100488 eb->flags[i] = entry->flags;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100489 vma->exec_flags = &eb->flags[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100490
491 err = 0;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100492 if (eb_pin_vma(eb, entry, vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100493 if (entry->offset != vma->node.start) {
494 entry->offset = vma->node.start | UPDATE;
495 eb->args->flags |= __EXEC_HAS_RELOC;
496 }
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100497 } else {
498 eb_unreserve_vma(vma, vma->exec_flags);
499
500 list_add_tail(&vma->exec_link, &eb->unbound);
501 if (drm_mm_node_allocated(&vma->node))
502 err = i915_vma_unbind(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100503 }
504 return err;
505}
506
507static inline int use_cpu_reloc(const struct reloc_cache *cache,
508 const struct drm_i915_gem_object *obj)
509{
510 if (!i915_gem_object_has_struct_page(obj))
511 return false;
512
Chris Wilson7dd4f672017-06-16 15:05:24 +0100513 if (DBG_FORCE_RELOC == FORCE_CPU_RELOC)
514 return true;
515
516 if (DBG_FORCE_RELOC == FORCE_GTT_RELOC)
517 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100518
519 return (cache->has_llc ||
520 obj->cache_dirty ||
521 obj->cache_level != I915_CACHE_NONE);
522}
523
524static int eb_reserve_vma(const struct i915_execbuffer *eb,
525 struct i915_vma *vma)
526{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100527 struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
528 unsigned int exec_flags = *vma->exec_flags;
529 u64 pin_flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100530 int err;
531
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100532 pin_flags = PIN_USER | PIN_NONBLOCK;
533 if (exec_flags & EXEC_OBJECT_NEEDS_GTT)
534 pin_flags |= PIN_GLOBAL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100535
536 /*
537 * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
538 * limit address to the first 4GBs for unflagged objects.
539 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100540 if (!(exec_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
541 pin_flags |= PIN_ZONE_4G;
Chris Wilson2889caa2017-06-16 15:05:19 +0100542
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100543 if (exec_flags & __EXEC_OBJECT_NEEDS_MAP)
544 pin_flags |= PIN_MAPPABLE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100545
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100546 if (exec_flags & EXEC_OBJECT_PINNED) {
547 pin_flags |= entry->offset | PIN_OFFSET_FIXED;
548 pin_flags &= ~PIN_NONBLOCK; /* force overlapping checks */
549 } else if (exec_flags & __EXEC_OBJECT_NEEDS_BIAS) {
550 pin_flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson2889caa2017-06-16 15:05:19 +0100551 }
552
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100553 err = i915_vma_pin(vma,
554 entry->pad_to_size, entry->alignment,
555 pin_flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100556 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 Wilsonc7c6e462017-08-16 09:52:06 +0100564 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100565 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))
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100572 exec_flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100573 }
574
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100575 *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN;
576 GEM_BUG_ON(eb_vma_misplaced(entry, vma, exec_flags));
Chris Wilson1da7b542017-07-21 15:50:35 +0100577
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++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100618 unsigned int flags = eb->flags[i];
619 struct i915_vma *vma = eb->vma[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100620
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100621 if (flags & EXEC_OBJECT_PINNED &&
622 flags & __EXEC_OBJECT_HAS_PIN)
Chris Wilson2889caa2017-06-16 15:05:19 +0100623 continue;
624
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100625 eb_unreserve_vma(vma, &eb->flags[i]);
Chris Wilson2889caa2017-06-16 15:05:19 +0100626
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100627 if (flags & EXEC_OBJECT_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +0100628 list_add(&vma->exec_link, &eb->unbound);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100629 else if (flags & __EXEC_OBJECT_NEEDS_MAP)
Chris Wilson2889caa2017-06-16 15:05:19 +0100630 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
Chris Wilson2889caa2017-06-16 15:05:19 +0100653static unsigned int eb_batch_index(const struct i915_execbuffer *eb)
654{
Chris Wilson1a71cf22017-06-16 15:05:23 +0100655 if (eb->args->flags & I915_EXEC_BATCH_FIRST)
656 return 0;
657 else
658 return eb->buffer_count - 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100659}
660
661static int eb_select_context(struct i915_execbuffer *eb)
662{
663 struct i915_gem_context *ctx;
664
665 ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1);
Chris Wilson1acfc102017-06-20 12:05:47 +0100666 if (unlikely(!ctx))
667 return -ENOENT;
Chris Wilson2889caa2017-06-16 15:05:19 +0100668
Chris Wilson1acfc102017-06-20 12:05:47 +0100669 eb->ctx = ctx;
Chris Wilson2889caa2017-06-16 15:05:19 +0100670 eb->vm = ctx->ppgtt ? &ctx->ppgtt->base : &eb->i915->ggtt.base;
671
672 eb->context_flags = 0;
673 if (ctx->flags & CONTEXT_NO_ZEROMAP)
674 eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS;
675
676 return 0;
677}
678
679static int eb_lookup_vmas(struct i915_execbuffer *eb)
Chris Wilson4ff4b442017-06-16 15:05:16 +0100680{
Chris Wilsond1b48c12017-08-16 09:52:08 +0100681 struct radix_tree_root *handles_vma = &eb->ctx->handles_vma;
Chris Wilson170fa292017-08-16 09:52:07 +0100682 struct drm_i915_gem_object *uninitialized_var(obj);
Chris Wilson2889caa2017-06-16 15:05:19 +0100683 unsigned int i;
Chris Wilson2889caa2017-06-16 15:05:19 +0100684 int err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100685
Chris Wilson8bcbfb12017-08-16 09:52:05 +0100686 if (unlikely(i915_gem_context_is_closed(eb->ctx)))
687 return -ENOENT;
688
689 if (unlikely(i915_gem_context_is_banned(eb->ctx)))
690 return -EIO;
691
Chris Wilson2889caa2017-06-16 15:05:19 +0100692 INIT_LIST_HEAD(&eb->relocs);
693 INIT_LIST_HEAD(&eb->unbound);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100694
Chris Wilson170fa292017-08-16 09:52:07 +0100695 for (i = 0; i < eb->buffer_count; i++) {
696 u32 handle = eb->exec[i].handle;
Chris Wilsond1b48c12017-08-16 09:52:08 +0100697 struct i915_lut_handle *lut;
Chris Wilson170fa292017-08-16 09:52:07 +0100698 struct i915_vma *vma;
699
Chris Wilsond1b48c12017-08-16 09:52:08 +0100700 vma = radix_tree_lookup(handles_vma, handle);
701 if (likely(vma))
Chris Wilson170fa292017-08-16 09:52:07 +0100702 goto add_vma;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100703
Chris Wilson170fa292017-08-16 09:52:07 +0100704 obj = i915_gem_object_lookup(eb->file, handle);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100705 if (unlikely(!obj)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100706 err = -ENOENT;
Chris Wilson170fa292017-08-16 09:52:07 +0100707 goto err_vma;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100708 }
709
Chris Wilson4ff4b442017-06-16 15:05:16 +0100710 vma = i915_vma_instance(obj, eb->vm, NULL);
711 if (unlikely(IS_ERR(vma))) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100712 err = PTR_ERR(vma);
Chris Wilson170fa292017-08-16 09:52:07 +0100713 goto err_obj;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100714 }
715
Chris Wilsond1b48c12017-08-16 09:52:08 +0100716 lut = kmem_cache_alloc(eb->i915->luts, GFP_KERNEL);
717 if (unlikely(!lut)) {
718 err = -ENOMEM;
719 goto err_obj;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100720 }
721
Chris Wilsond1b48c12017-08-16 09:52:08 +0100722 err = radix_tree_insert(handles_vma, handle, vma);
723 if (unlikely(err)) {
724 kfree(lut);
725 goto err_obj;
726 }
727
Chris Wilson3ffff012017-08-22 12:05:17 +0100728 vma->open_count++;
Chris Wilsond1b48c12017-08-16 09:52:08 +0100729 list_add(&lut->obj_link, &obj->lut_list);
730 list_add(&lut->ctx_link, &eb->ctx->handles_list);
731 lut->ctx = eb->ctx;
732 lut->handle = handle;
733
734 /* transfer ref to ctx */
735 obj = NULL;
736
Chris Wilson170fa292017-08-16 09:52:07 +0100737add_vma:
Chris Wilsond1b48c12017-08-16 09:52:08 +0100738 err = eb_add_vma(eb, i, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100739 if (unlikely(err))
Chris Wilson170fa292017-08-16 09:52:07 +0100740 goto err_obj;
Chris Wilsondade2a62017-06-16 15:05:20 +0100741
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100742 GEM_BUG_ON(vma != eb->vma[i]);
743 GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100744 }
745
Chris Wilson2889caa2017-06-16 15:05:19 +0100746 /* take note of the batch buffer before we might reorder the lists */
747 i = eb_batch_index(eb);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100748 eb->batch = eb->vma[i];
749 GEM_BUG_ON(eb->batch->exec_flags != &eb->flags[i]);
Chris Wilson59bfa122016-08-04 16:32:31 +0100750
751 /*
752 * SNA is doing fancy tricks with compressing batch buffers, which leads
753 * to negative relocation deltas. Usually that works out ok since the
754 * relocate address is still positive, except when the batch is placed
755 * very low in the GTT. Ensure this doesn't happen.
756 *
757 * Note that actual hangs have only been observed on gen7, but for
758 * paranoia do it everywhere.
759 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100760 if (!(eb->flags[i] & EXEC_OBJECT_PINNED))
761 eb->flags[i] |= __EXEC_OBJECT_NEEDS_BIAS;
Chris Wilson2889caa2017-06-16 15:05:19 +0100762 if (eb->reloc_cache.has_fence)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100763 eb->flags[i] |= EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson59bfa122016-08-04 16:32:31 +0100764
Chris Wilson2889caa2017-06-16 15:05:19 +0100765 eb->args->flags |= __EXEC_VALIDATED;
766 return eb_reserve(eb);
767
Chris Wilson170fa292017-08-16 09:52:07 +0100768err_obj:
769 if (obj)
770 i915_gem_object_put(obj);
771err_vma:
772 eb->vma[i] = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100773 return err;
Chris Wilson59bfa122016-08-04 16:32:31 +0100774}
775
Chris Wilson4ff4b442017-06-16 15:05:16 +0100776static struct i915_vma *
Chris Wilson2889caa2017-06-16 15:05:19 +0100777eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000778{
Chris Wilson2889caa2017-06-16 15:05:19 +0100779 if (eb->lut_size < 0) {
780 if (handle >= -eb->lut_size)
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000781 return NULL;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100782 return eb->vma[handle];
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000783 } else {
784 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800785 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000786
Chris Wilson2889caa2017-06-16 15:05:19 +0100787 head = &eb->buckets[hash_32(handle, eb->lut_size)];
Geliang Tangaa459502016-01-18 23:54:20 +0800788 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200789 if (vma->exec_handle == handle)
790 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000791 }
792 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000793 }
Chris Wilson67731b82010-12-08 10:38:14 +0000794}
795
Chris Wilson2889caa2017-06-16 15:05:19 +0100796static void eb_release_vmas(const struct i915_execbuffer *eb)
Chris Wilsona415d352013-11-26 11:23:15 +0000797{
Chris Wilson2889caa2017-06-16 15:05:19 +0100798 const unsigned int count = eb->buffer_count;
799 unsigned int i;
Chris Wilson650bc632017-06-15 09:14:33 +0100800
Chris Wilson2889caa2017-06-16 15:05:19 +0100801 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100802 struct i915_vma *vma = eb->vma[i];
803 unsigned int flags = eb->flags[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100804
805 if (!vma)
Chris Wilson170fa292017-08-16 09:52:07 +0100806 break;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000807
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100808 GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
809 vma->exec_flags = NULL;
810 eb->vma[i] = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100811
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100812 if (flags & __EXEC_OBJECT_HAS_PIN)
813 __eb_unreserve_vma(vma, flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100814
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100815 if (flags & __EXEC_OBJECT_HAS_REF)
Chris Wilsondade2a62017-06-16 15:05:20 +0100816 i915_vma_put(vma);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000817 }
Chris Wilson2889caa2017-06-16 15:05:19 +0100818}
Chris Wilsond55495b2017-06-15 09:14:34 +0100819
Chris Wilson2889caa2017-06-16 15:05:19 +0100820static void eb_reset_vmas(const struct i915_execbuffer *eb)
821{
822 eb_release_vmas(eb);
Chris Wilson4d470f72017-06-29 16:04:25 +0100823 if (eb->lut_size > 0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100824 memset(eb->buckets, 0,
825 sizeof(struct hlist_head) << eb->lut_size);
826}
Chris Wilsond55495b2017-06-15 09:14:34 +0100827
Chris Wilson2889caa2017-06-16 15:05:19 +0100828static void eb_destroy(const struct i915_execbuffer *eb)
829{
Chris Wilson7dd4f672017-06-16 15:05:24 +0100830 GEM_BUG_ON(eb->reloc_cache.rq);
831
Chris Wilson4d470f72017-06-29 16:04:25 +0100832 if (eb->lut_size > 0)
Chris Wilsond55495b2017-06-15 09:14:34 +0100833 kfree(eb->buckets);
Chris Wilson67731b82010-12-08 10:38:14 +0000834}
835
Chris Wilson2889caa2017-06-16 15:05:19 +0100836static inline u64
Chris Wilsond50415c2016-08-18 17:16:52 +0100837relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +0100838 const struct i915_vma *target)
Michał Winiarski934acce2015-12-29 18:24:52 +0100839{
Chris Wilson2889caa2017-06-16 15:05:19 +0100840 return gen8_canonical_addr((int)reloc->delta + target->node.start);
Michał Winiarski934acce2015-12-29 18:24:52 +0100841}
842
Chris Wilsond50415c2016-08-18 17:16:52 +0100843static void reloc_cache_init(struct reloc_cache *cache,
844 struct drm_i915_private *i915)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100845{
Chris Wilson31a39202016-08-18 17:16:46 +0100846 cache->page = -1;
Chris Wilsond50415c2016-08-18 17:16:52 +0100847 cache->vaddr = 0;
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200848 /* Must be a variable in the struct to allow GCC to unroll. */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100849 cache->gen = INTEL_GEN(i915);
Chris Wilson2889caa2017-06-16 15:05:19 +0100850 cache->has_llc = HAS_LLC(i915);
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200851 cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
Chris Wilson7dd4f672017-06-16 15:05:24 +0100852 cache->has_fence = cache->gen < 4;
853 cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
Chris Wilsone8cb9092016-08-18 17:16:53 +0100854 cache->node.allocated = false;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100855 cache->rq = NULL;
856 cache->rq_size = 0;
Chris Wilson31a39202016-08-18 17:16:46 +0100857}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100858
Chris Wilsond50415c2016-08-18 17:16:52 +0100859static inline void *unmask_page(unsigned long p)
860{
861 return (void *)(uintptr_t)(p & PAGE_MASK);
862}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100863
Chris Wilsond50415c2016-08-18 17:16:52 +0100864static inline unsigned int unmask_flags(unsigned long p)
865{
866 return p & ~PAGE_MASK;
867}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700868
Chris Wilsond50415c2016-08-18 17:16:52 +0100869#define KMAP 0x4 /* after CLFLUSH_FLAGS */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700870
Chris Wilson650bc632017-06-15 09:14:33 +0100871static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
872{
873 struct drm_i915_private *i915 =
874 container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
875 return &i915->ggtt;
876}
877
Chris Wilson7dd4f672017-06-16 15:05:24 +0100878static void reloc_gpu_flush(struct reloc_cache *cache)
879{
880 GEM_BUG_ON(cache->rq_size >= cache->rq->batch->obj->base.size / sizeof(u32));
881 cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END;
882 i915_gem_object_unpin_map(cache->rq->batch->obj);
883 i915_gem_chipset_flush(cache->rq->i915);
884
885 __i915_add_request(cache->rq, true);
886 cache->rq = NULL;
887}
888
Chris Wilson650bc632017-06-15 09:14:33 +0100889static void reloc_cache_reset(struct reloc_cache *cache)
Chris Wilson31a39202016-08-18 17:16:46 +0100890{
Chris Wilsond50415c2016-08-18 17:16:52 +0100891 void *vaddr;
892
Chris Wilson7dd4f672017-06-16 15:05:24 +0100893 if (cache->rq)
894 reloc_gpu_flush(cache);
895
Chris Wilson31a39202016-08-18 17:16:46 +0100896 if (!cache->vaddr)
897 return;
898
Chris Wilsond50415c2016-08-18 17:16:52 +0100899 vaddr = unmask_page(cache->vaddr);
900 if (cache->vaddr & KMAP) {
901 if (cache->vaddr & CLFLUSH_AFTER)
902 mb();
Chris Wilson31a39202016-08-18 17:16:46 +0100903
Chris Wilsond50415c2016-08-18 17:16:52 +0100904 kunmap_atomic(vaddr);
905 i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
906 } else {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100907 wmb();
Chris Wilsond50415c2016-08-18 17:16:52 +0100908 io_mapping_unmap_atomic((void __iomem *)vaddr);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100909 if (cache->node.allocated) {
Chris Wilson650bc632017-06-15 09:14:33 +0100910 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100911
912 ggtt->base.clear_range(&ggtt->base,
913 cache->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200914 cache->node.size);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100915 drm_mm_remove_node(&cache->node);
916 } else {
917 i915_vma_unpin((struct i915_vma *)cache->node.mm);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700918 }
Chris Wilson31a39202016-08-18 17:16:46 +0100919 }
Chris Wilson650bc632017-06-15 09:14:33 +0100920
921 cache->vaddr = 0;
922 cache->page = -1;
Chris Wilson31a39202016-08-18 17:16:46 +0100923}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700924
Chris Wilson31a39202016-08-18 17:16:46 +0100925static void *reloc_kmap(struct drm_i915_gem_object *obj,
926 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100927 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100928{
Chris Wilsond50415c2016-08-18 17:16:52 +0100929 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100930
Chris Wilsond50415c2016-08-18 17:16:52 +0100931 if (cache->vaddr) {
932 kunmap_atomic(unmask_page(cache->vaddr));
933 } else {
934 unsigned int flushes;
Chris Wilson2889caa2017-06-16 15:05:19 +0100935 int err;
Chris Wilson31a39202016-08-18 17:16:46 +0100936
Chris Wilson2889caa2017-06-16 15:05:19 +0100937 err = i915_gem_obj_prepare_shmem_write(obj, &flushes);
938 if (err)
939 return ERR_PTR(err);
Chris Wilsond50415c2016-08-18 17:16:52 +0100940
941 BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
942 BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
943
944 cache->vaddr = flushes | KMAP;
945 cache->node.mm = (void *)obj;
946 if (flushes)
947 mb();
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700948 }
949
Chris Wilsond50415c2016-08-18 17:16:52 +0100950 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
951 cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100952 cache->page = page;
Chris Wilson31a39202016-08-18 17:16:46 +0100953
Chris Wilsond50415c2016-08-18 17:16:52 +0100954 return vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100955}
956
Chris Wilsond50415c2016-08-18 17:16:52 +0100957static void *reloc_iomap(struct drm_i915_gem_object *obj,
Chris Wilson31a39202016-08-18 17:16:46 +0100958 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100959 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100960{
Chris Wilson650bc632017-06-15 09:14:33 +0100961 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100962 unsigned long offset;
Chris Wilsond50415c2016-08-18 17:16:52 +0100963 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100964
Chris Wilsond50415c2016-08-18 17:16:52 +0100965 if (cache->vaddr) {
Jani Nikula615e5002016-10-04 12:54:13 +0300966 io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
Chris Wilsond50415c2016-08-18 17:16:52 +0100967 } else {
968 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +0100969 int err;
Chris Wilson31a39202016-08-18 17:16:46 +0100970
Chris Wilson2889caa2017-06-16 15:05:19 +0100971 if (use_cpu_reloc(cache, obj))
Chris Wilsond50415c2016-08-18 17:16:52 +0100972 return NULL;
Chris Wilson31a39202016-08-18 17:16:46 +0100973
Chris Wilson2889caa2017-06-16 15:05:19 +0100974 err = i915_gem_object_set_to_gtt_domain(obj, true);
975 if (err)
976 return ERR_PTR(err);
Chris Wilson31a39202016-08-18 17:16:46 +0100977
Chris Wilsond50415c2016-08-18 17:16:52 +0100978 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
979 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100980 if (IS_ERR(vma)) {
981 memset(&cache->node, 0, sizeof(cache->node));
Chris Wilson2889caa2017-06-16 15:05:19 +0100982 err = drm_mm_insert_node_in_range
Chris Wilsone8cb9092016-08-18 17:16:53 +0100983 (&ggtt->base.mm, &cache->node,
Chris Wilsonf51455d2017-01-10 14:47:34 +0000984 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
Chris Wilsone8cb9092016-08-18 17:16:53 +0100985 0, ggtt->mappable_end,
Chris Wilson4e64e552017-02-02 21:04:38 +0000986 DRM_MM_INSERT_LOW);
Chris Wilson2889caa2017-06-16 15:05:19 +0100987 if (err) /* no inactive aperture space, use cpu reloc */
Chris Wilsonc92fa4f2016-10-07 07:53:25 +0100988 return NULL;
Chris Wilsone8cb9092016-08-18 17:16:53 +0100989 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +0100990 err = i915_vma_put_fence(vma);
991 if (err) {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100992 i915_vma_unpin(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100993 return ERR_PTR(err);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100994 }
Rafael Barbalho5032d872013-08-21 17:10:51 +0100995
Chris Wilsone8cb9092016-08-18 17:16:53 +0100996 cache->node.start = vma->node.start;
997 cache->node.mm = (void *)vma;
Chris Wilsond50415c2016-08-18 17:16:52 +0100998 }
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700999 }
1000
Chris Wilsone8cb9092016-08-18 17:16:53 +01001001 offset = cache->node.start;
1002 if (cache->node.allocated) {
Chris Wilsonfc099092016-10-28 15:27:56 +01001003 wmb();
Chris Wilsone8cb9092016-08-18 17:16:53 +01001004 ggtt->base.insert_page(&ggtt->base,
1005 i915_gem_object_get_dma_address(obj, page),
1006 offset, I915_CACHE_NONE, 0);
1007 } else {
1008 offset += page << PAGE_SHIFT;
1009 }
1010
Chris Wilson650bc632017-06-15 09:14:33 +01001011 vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->mappable,
1012 offset);
Chris Wilsond50415c2016-08-18 17:16:52 +01001013 cache->page = page;
1014 cache->vaddr = (unsigned long)vaddr;
1015
1016 return vaddr;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001017}
1018
Chris Wilsond50415c2016-08-18 17:16:52 +01001019static void *reloc_vaddr(struct drm_i915_gem_object *obj,
1020 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001021 unsigned long page)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001022{
Chris Wilsond50415c2016-08-18 17:16:52 +01001023 void *vaddr;
1024
1025 if (cache->page == page) {
1026 vaddr = unmask_page(cache->vaddr);
1027 } else {
1028 vaddr = NULL;
1029 if ((cache->vaddr & KMAP) == 0)
1030 vaddr = reloc_iomap(obj, cache, page);
1031 if (!vaddr)
1032 vaddr = reloc_kmap(obj, cache, page);
1033 }
1034
1035 return vaddr;
1036}
1037
1038static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
1039{
1040 if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
1041 if (flushes & CLFLUSH_BEFORE) {
1042 clflushopt(addr);
1043 mb();
1044 }
1045
1046 *addr = value;
1047
Chris Wilson2889caa2017-06-16 15:05:19 +01001048 /*
1049 * Writes to the same cacheline are serialised by the CPU
Chris Wilsond50415c2016-08-18 17:16:52 +01001050 * (including clflush). On the write path, we only require
1051 * that it hits memory in an orderly fashion and place
1052 * mb barriers at the start and end of the relocation phase
1053 * to ensure ordering of clflush wrt to the system.
1054 */
1055 if (flushes & CLFLUSH_AFTER)
1056 clflushopt(addr);
1057 } else
1058 *addr = value;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001059}
1060
Chris Wilson7dd4f672017-06-16 15:05:24 +01001061static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
1062 struct i915_vma *vma,
1063 unsigned int len)
1064{
1065 struct reloc_cache *cache = &eb->reloc_cache;
1066 struct drm_i915_gem_object *obj;
1067 struct drm_i915_gem_request *rq;
1068 struct i915_vma *batch;
1069 u32 *cmd;
1070 int err;
1071
1072 GEM_BUG_ON(vma->obj->base.write_domain & I915_GEM_DOMAIN_CPU);
1073
1074 obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, PAGE_SIZE);
1075 if (IS_ERR(obj))
1076 return PTR_ERR(obj);
1077
1078 cmd = i915_gem_object_pin_map(obj,
Chris Wilsona575c672017-08-28 11:46:31 +01001079 cache->has_llc ?
1080 I915_MAP_FORCE_WB :
1081 I915_MAP_FORCE_WC);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001082 i915_gem_object_unpin_pages(obj);
1083 if (IS_ERR(cmd))
1084 return PTR_ERR(cmd);
1085
1086 err = i915_gem_object_set_to_wc_domain(obj, false);
1087 if (err)
1088 goto err_unmap;
1089
1090 batch = i915_vma_instance(obj, vma->vm, NULL);
1091 if (IS_ERR(batch)) {
1092 err = PTR_ERR(batch);
1093 goto err_unmap;
1094 }
1095
1096 err = i915_vma_pin(batch, 0, 0, PIN_USER | PIN_NONBLOCK);
1097 if (err)
1098 goto err_unmap;
1099
1100 rq = i915_gem_request_alloc(eb->engine, eb->ctx);
1101 if (IS_ERR(rq)) {
1102 err = PTR_ERR(rq);
1103 goto err_unpin;
1104 }
1105
1106 err = i915_gem_request_await_object(rq, vma->obj, true);
1107 if (err)
1108 goto err_request;
1109
1110 err = eb->engine->emit_flush(rq, EMIT_INVALIDATE);
1111 if (err)
1112 goto err_request;
1113
1114 err = i915_switch_context(rq);
1115 if (err)
1116 goto err_request;
1117
1118 err = eb->engine->emit_bb_start(rq,
1119 batch->node.start, PAGE_SIZE,
1120 cache->gen > 5 ? 0 : I915_DISPATCH_SECURE);
1121 if (err)
1122 goto err_request;
1123
Chris Wilson95ff7c72017-06-16 15:05:25 +01001124 GEM_BUG_ON(!reservation_object_test_signaled_rcu(batch->resv, true));
Chris Wilson7dd4f672017-06-16 15:05:24 +01001125 i915_vma_move_to_active(batch, rq, 0);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001126 reservation_object_lock(batch->resv, NULL);
1127 reservation_object_add_excl_fence(batch->resv, &rq->fence);
1128 reservation_object_unlock(batch->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001129 i915_vma_unpin(batch);
1130
Chris Wilson25ffaa62017-06-20 13:43:20 +01001131 i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001132 reservation_object_lock(vma->resv, NULL);
1133 reservation_object_add_excl_fence(vma->resv, &rq->fence);
1134 reservation_object_unlock(vma->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001135
1136 rq->batch = batch;
1137
1138 cache->rq = rq;
1139 cache->rq_cmd = cmd;
1140 cache->rq_size = 0;
1141
1142 /* Return with batch mapping (cmd) still pinned */
1143 return 0;
1144
1145err_request:
1146 i915_add_request(rq);
1147err_unpin:
1148 i915_vma_unpin(batch);
1149err_unmap:
1150 i915_gem_object_unpin_map(obj);
1151 return err;
1152}
1153
1154static u32 *reloc_gpu(struct i915_execbuffer *eb,
1155 struct i915_vma *vma,
1156 unsigned int len)
1157{
1158 struct reloc_cache *cache = &eb->reloc_cache;
1159 u32 *cmd;
1160
1161 if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1))
1162 reloc_gpu_flush(cache);
1163
1164 if (unlikely(!cache->rq)) {
1165 int err;
1166
Chris Wilson3dbf26e2017-08-26 14:56:20 +01001167 /* If we need to copy for the cmdparser, we will stall anyway */
1168 if (eb_use_cmdparser(eb))
1169 return ERR_PTR(-EWOULDBLOCK);
1170
Chris Wilson7dd4f672017-06-16 15:05:24 +01001171 err = __reloc_gpu_alloc(eb, vma, len);
1172 if (unlikely(err))
1173 return ERR_PTR(err);
1174 }
1175
1176 cmd = cache->rq_cmd + cache->rq_size;
1177 cache->rq_size += len;
1178
1179 return cmd;
1180}
1181
Chris Wilson2889caa2017-06-16 15:05:19 +01001182static u64
1183relocate_entry(struct i915_vma *vma,
Chris Wilsond50415c2016-08-18 17:16:52 +01001184 const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +01001185 struct i915_execbuffer *eb,
1186 const struct i915_vma *target)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001187{
Chris Wilsond50415c2016-08-18 17:16:52 +01001188 u64 offset = reloc->offset;
Chris Wilson2889caa2017-06-16 15:05:19 +01001189 u64 target_offset = relocation_target(reloc, target);
1190 bool wide = eb->reloc_cache.use_64bit_reloc;
Chris Wilsond50415c2016-08-18 17:16:52 +01001191 void *vaddr;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001192
Chris Wilson7dd4f672017-06-16 15:05:24 +01001193 if (!eb->reloc_cache.vaddr &&
1194 (DBG_FORCE_RELOC == FORCE_GPU_RELOC ||
Chris Wilsonf2f5c062017-08-16 09:52:04 +01001195 !reservation_object_test_signaled_rcu(vma->resv, true)) &&
1196 __intel_engine_can_store_dword(eb->reloc_cache.gen,
1197 eb->engine->class)) {
Chris Wilson7dd4f672017-06-16 15:05:24 +01001198 const unsigned int gen = eb->reloc_cache.gen;
1199 unsigned int len;
1200 u32 *batch;
1201 u64 addr;
1202
1203 if (wide)
1204 len = offset & 7 ? 8 : 5;
1205 else if (gen >= 4)
1206 len = 4;
Chris Wilsonf2f5c062017-08-16 09:52:04 +01001207 else
Chris Wilson7dd4f672017-06-16 15:05:24 +01001208 len = 3;
Chris Wilson7dd4f672017-06-16 15:05:24 +01001209
1210 batch = reloc_gpu(eb, vma, len);
1211 if (IS_ERR(batch))
1212 goto repeat;
1213
1214 addr = gen8_canonical_addr(vma->node.start + offset);
1215 if (wide) {
1216 if (offset & 7) {
1217 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1218 *batch++ = lower_32_bits(addr);
1219 *batch++ = upper_32_bits(addr);
1220 *batch++ = lower_32_bits(target_offset);
1221
1222 addr = gen8_canonical_addr(addr + 4);
1223
1224 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1225 *batch++ = lower_32_bits(addr);
1226 *batch++ = upper_32_bits(addr);
1227 *batch++ = upper_32_bits(target_offset);
1228 } else {
1229 *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1;
1230 *batch++ = lower_32_bits(addr);
1231 *batch++ = upper_32_bits(addr);
1232 *batch++ = lower_32_bits(target_offset);
1233 *batch++ = upper_32_bits(target_offset);
1234 }
1235 } else if (gen >= 6) {
1236 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1237 *batch++ = 0;
1238 *batch++ = addr;
1239 *batch++ = target_offset;
1240 } else if (gen >= 4) {
1241 *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1242 *batch++ = 0;
1243 *batch++ = addr;
1244 *batch++ = target_offset;
1245 } else {
1246 *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
1247 *batch++ = addr;
1248 *batch++ = target_offset;
1249 }
1250
1251 goto out;
1252 }
1253
Chris Wilsond50415c2016-08-18 17:16:52 +01001254repeat:
Chris Wilson95ff7c72017-06-16 15:05:25 +01001255 vaddr = reloc_vaddr(vma->obj, &eb->reloc_cache, offset >> PAGE_SHIFT);
Chris Wilsond50415c2016-08-18 17:16:52 +01001256 if (IS_ERR(vaddr))
1257 return PTR_ERR(vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001258
Chris Wilsond50415c2016-08-18 17:16:52 +01001259 clflush_write32(vaddr + offset_in_page(offset),
1260 lower_32_bits(target_offset),
Chris Wilson2889caa2017-06-16 15:05:19 +01001261 eb->reloc_cache.vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001262
Chris Wilsond50415c2016-08-18 17:16:52 +01001263 if (wide) {
1264 offset += sizeof(u32);
1265 target_offset >>= 32;
1266 wide = false;
1267 goto repeat;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001268 }
Chris Wilsondabdfe02012-03-26 10:10:27 +02001269
Chris Wilson7dd4f672017-06-16 15:05:24 +01001270out:
Chris Wilson2889caa2017-06-16 15:05:19 +01001271 return target->node.start | UPDATE;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001272}
1273
Chris Wilson2889caa2017-06-16 15:05:19 +01001274static u64
1275eb_relocate_entry(struct i915_execbuffer *eb,
1276 struct i915_vma *vma,
1277 const struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001278{
Chris Wilson507d9772017-06-16 15:05:17 +01001279 struct i915_vma *target;
Chris Wilson2889caa2017-06-16 15:05:19 +01001280 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001281
Chris Wilson67731b82010-12-08 10:38:14 +00001282 /* we've already hold a reference to all valid objects */
Chris Wilson507d9772017-06-16 15:05:17 +01001283 target = eb_get_vma(eb, reloc->target_handle);
1284 if (unlikely(!target))
Chris Wilson54cf91d2010-11-25 18:00:26 +00001285 return -ENOENT;
Eric Anholte844b992012-07-31 15:35:01 -07001286
Chris Wilson54cf91d2010-11-25 18:00:26 +00001287 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001288 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001289 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001290 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001291 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001292 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001293 (int) reloc->offset,
1294 reloc->read_domains,
1295 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001296 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001297 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +01001298 if (unlikely((reloc->write_domain | reloc->read_domains)
1299 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001300 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001301 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001302 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001303 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001304 (int) reloc->offset,
1305 reloc->read_domains,
1306 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001307 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001308 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001309
Chris Wilson2889caa2017-06-16 15:05:19 +01001310 if (reloc->write_domain) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001311 *target->exec_flags |= EXEC_OBJECT_WRITE;
Chris Wilson507d9772017-06-16 15:05:17 +01001312
Chris Wilson2889caa2017-06-16 15:05:19 +01001313 /*
1314 * Sandybridge PPGTT errata: We need a global gtt mapping
1315 * for MI and pipe_control writes because the gpu doesn't
1316 * properly redirect them through the ppgtt for non_secure
1317 * batchbuffers.
1318 */
1319 if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
1320 IS_GEN6(eb->i915)) {
1321 err = i915_vma_bind(target, target->obj->cache_level,
1322 PIN_GLOBAL);
1323 if (WARN_ONCE(err,
1324 "Unexpected failure to bind target VMA!"))
1325 return err;
1326 }
Chris Wilson507d9772017-06-16 15:05:17 +01001327 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001328
Chris Wilson2889caa2017-06-16 15:05:19 +01001329 /*
1330 * If the relocation already has the right value in it, no
Chris Wilson54cf91d2010-11-25 18:00:26 +00001331 * more work needs to be done.
1332 */
Chris Wilson7dd4f672017-06-16 15:05:24 +01001333 if (!DBG_FORCE_RELOC &&
1334 gen8_canonical_addr(target->node.start) == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +00001335 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001336
1337 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001338 if (unlikely(reloc->offset >
Chris Wilson507d9772017-06-16 15:05:17 +01001339 vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001340 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson507d9772017-06-16 15:05:17 +01001341 "target %d offset %d size %d.\n",
1342 reloc->target_handle,
1343 (int)reloc->offset,
1344 (int)vma->size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001345 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001346 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001347 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001348 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson507d9772017-06-16 15:05:17 +01001349 "target %d offset %d.\n",
1350 reloc->target_handle,
1351 (int)reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001352 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001353 }
1354
Chris Wilson071750e2017-06-16 15:05:18 +01001355 /*
1356 * If we write into the object, we need to force the synchronisation
1357 * barrier, either with an asynchronous clflush or if we executed the
1358 * patching using the GPU (though that should be serialised by the
1359 * timeline). To be completely sure, and since we are required to
1360 * do relocations we are already stalling, disable the user's opt
Chris Wilson0519bcb2017-08-16 09:52:09 +01001361 * out of our synchronisation.
Chris Wilson071750e2017-06-16 15:05:18 +01001362 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001363 *vma->exec_flags &= ~EXEC_OBJECT_ASYNC;
Chris Wilson071750e2017-06-16 15:05:18 +01001364
Chris Wilson54cf91d2010-11-25 18:00:26 +00001365 /* and update the user's relocation entry */
Chris Wilson2889caa2017-06-16 15:05:19 +01001366 return relocate_entry(vma, reloc, eb, target);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001367}
1368
Chris Wilson2889caa2017-06-16 15:05:19 +01001369static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001370{
Chris Wilson1d83f442012-03-24 20:12:53 +00001371#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
Chris Wilson2889caa2017-06-16 15:05:19 +01001372 struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
1373 struct drm_i915_gem_relocation_entry __user *urelocs;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001374 const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001375 unsigned int remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001376
Chris Wilson2889caa2017-06-16 15:05:19 +01001377 urelocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson1d83f442012-03-24 20:12:53 +00001378 remain = entry->relocation_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01001379 if (unlikely(remain > N_RELOC(ULONG_MAX)))
1380 return -EINVAL;
Chris Wilsonebc08082016-10-18 13:02:51 +01001381
Chris Wilson2889caa2017-06-16 15:05:19 +01001382 /*
1383 * We must check that the entire relocation array is safe
1384 * to read. However, if the array is not writable the user loses
1385 * the updated relocation values.
1386 */
Imre Deakedd90032017-07-14 18:12:42 +03001387 if (unlikely(!access_ok(VERIFY_READ, urelocs, remain*sizeof(*urelocs))))
Chris Wilson2889caa2017-06-16 15:05:19 +01001388 return -EFAULT;
Chris Wilson1d83f442012-03-24 20:12:53 +00001389
Chris Wilson2889caa2017-06-16 15:05:19 +01001390 do {
1391 struct drm_i915_gem_relocation_entry *r = stack;
1392 unsigned int count =
1393 min_t(unsigned int, remain, ARRAY_SIZE(stack));
1394 unsigned int copied;
1395
1396 /*
1397 * This is the fast path and we cannot handle a pagefault
Chris Wilsonebc08082016-10-18 13:02:51 +01001398 * whilst holding the struct mutex lest the user pass in the
1399 * relocations contained within a mmaped bo. For in such a case
1400 * we, the page fault handler would call i915_gem_fault() and
1401 * we would try to acquire the struct mutex again. Obviously
1402 * this is bad and so lockdep complains vehemently.
1403 */
1404 pagefault_disable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001405 copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
Chris Wilsonebc08082016-10-18 13:02:51 +01001406 pagefault_enable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001407 if (unlikely(copied)) {
1408 remain = -EFAULT;
Chris Wilson31a39202016-08-18 17:16:46 +01001409 goto out;
1410 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001411
Chris Wilson2889caa2017-06-16 15:05:19 +01001412 remain -= count;
Chris Wilson1d83f442012-03-24 20:12:53 +00001413 do {
Chris Wilson2889caa2017-06-16 15:05:19 +01001414 u64 offset = eb_relocate_entry(eb, vma, r);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001415
Chris Wilson2889caa2017-06-16 15:05:19 +01001416 if (likely(offset == 0)) {
1417 } else if ((s64)offset < 0) {
1418 remain = (int)offset;
Chris Wilson31a39202016-08-18 17:16:46 +01001419 goto out;
Chris Wilson2889caa2017-06-16 15:05:19 +01001420 } else {
1421 /*
1422 * Note that reporting an error now
1423 * leaves everything in an inconsistent
1424 * state as we have *already* changed
1425 * the relocation value inside the
1426 * object. As we have not changed the
1427 * reloc.presumed_offset or will not
1428 * change the execobject.offset, on the
1429 * call we may not rewrite the value
1430 * inside the object, leaving it
1431 * dangling and causing a GPU hang. Unless
1432 * userspace dynamically rebuilds the
1433 * relocations on each execbuf rather than
1434 * presume a static tree.
1435 *
1436 * We did previously check if the relocations
1437 * were writable (access_ok), an error now
1438 * would be a strange race with mprotect,
1439 * having already demonstrated that we
1440 * can read from this userspace address.
1441 */
1442 offset = gen8_canonical_addr(offset & ~UPDATE);
1443 __put_user(offset,
1444 &urelocs[r-stack].presumed_offset);
Chris Wilson1d83f442012-03-24 20:12:53 +00001445 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001446 } while (r++, --count);
1447 urelocs += ARRAY_SIZE(stack);
1448 } while (remain);
Chris Wilson31a39202016-08-18 17:16:46 +01001449out:
Chris Wilson650bc632017-06-15 09:14:33 +01001450 reloc_cache_reset(&eb->reloc_cache);
Chris Wilson2889caa2017-06-16 15:05:19 +01001451 return remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001452}
1453
1454static int
Chris Wilson2889caa2017-06-16 15:05:19 +01001455eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001456{
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001457 const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001458 struct drm_i915_gem_relocation_entry *relocs =
1459 u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1460 unsigned int i;
1461 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001462
1463 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001464 u64 offset = eb_relocate_entry(eb, vma, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001465
Chris Wilson2889caa2017-06-16 15:05:19 +01001466 if ((s64)offset < 0) {
1467 err = (int)offset;
1468 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001469 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001470 }
1471 err = 0;
Chris Wilsona415d352013-11-26 11:23:15 +00001472err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001473 reloc_cache_reset(&eb->reloc_cache);
1474 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001475}
1476
Chris Wilson2889caa2017-06-16 15:05:19 +01001477static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001478{
Chris Wilson2889caa2017-06-16 15:05:19 +01001479 const char __user *addr, *end;
1480 unsigned long size;
1481 char __maybe_unused c;
Ben Widawsky27173f12013-08-14 11:38:36 +02001482
Chris Wilson2889caa2017-06-16 15:05:19 +01001483 size = entry->relocation_count;
1484 if (size == 0)
1485 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001486
Chris Wilson2889caa2017-06-16 15:05:19 +01001487 if (size > N_RELOC(ULONG_MAX))
1488 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001489
Chris Wilson2889caa2017-06-16 15:05:19 +01001490 addr = u64_to_user_ptr(entry->relocs_ptr);
1491 size *= sizeof(struct drm_i915_gem_relocation_entry);
1492 if (!access_ok(VERIFY_READ, addr, size))
1493 return -EFAULT;
1494
1495 end = addr + size;
1496 for (; addr < end; addr += PAGE_SIZE) {
1497 int err = __get_user(c, addr);
1498 if (err)
1499 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001500 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001501 return __get_user(c, end - 1);
1502}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001503
Chris Wilson2889caa2017-06-16 15:05:19 +01001504static int eb_copy_relocations(const struct i915_execbuffer *eb)
1505{
1506 const unsigned int count = eb->buffer_count;
1507 unsigned int i;
1508 int err;
1509
Chris Wilson54cf91d2010-11-25 18:00:26 +00001510 for (i = 0; i < count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001511 const unsigned int nreloc = eb->exec[i].relocation_count;
1512 struct drm_i915_gem_relocation_entry __user *urelocs;
1513 struct drm_i915_gem_relocation_entry *relocs;
1514 unsigned long size;
1515 unsigned long copied;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001516
Chris Wilson2889caa2017-06-16 15:05:19 +01001517 if (nreloc == 0)
1518 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001519
Chris Wilson2889caa2017-06-16 15:05:19 +01001520 err = check_relocations(&eb->exec[i]);
1521 if (err)
1522 goto err;
1523
1524 urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
1525 size = nreloc * sizeof(*relocs);
1526
1527 relocs = kvmalloc_array(size, 1, GFP_TEMPORARY);
1528 if (!relocs) {
1529 kvfree(relocs);
1530 err = -ENOMEM;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001531 goto err;
1532 }
1533
Chris Wilson2889caa2017-06-16 15:05:19 +01001534 /* copy_from_user is limited to < 4GiB */
1535 copied = 0;
1536 do {
1537 unsigned int len =
1538 min_t(u64, BIT_ULL(31), size - copied);
1539
1540 if (__copy_from_user((char *)relocs + copied,
1541 (char *)urelocs + copied,
1542 len)) {
1543 kvfree(relocs);
1544 err = -EFAULT;
1545 goto err;
1546 }
1547
1548 copied += len;
1549 } while (copied < size);
1550
1551 /*
1552 * As we do not update the known relocation offsets after
Chris Wilson262b6d32013-01-15 16:17:54 +00001553 * relocating (due to the complexities in lock handling),
1554 * we need to mark them as invalid now so that we force the
1555 * relocation processing next time. Just in case the target
1556 * object is evicted and then rebound into its old
1557 * presumed_offset before the next execbuffer - if that
1558 * happened we would make the mistake of assuming that the
1559 * relocations were valid.
1560 */
Chris Wilson2889caa2017-06-16 15:05:19 +01001561 user_access_begin();
1562 for (copied = 0; copied < nreloc; copied++)
1563 unsafe_put_user(-1,
1564 &urelocs[copied].presumed_offset,
1565 end_user);
1566end_user:
1567 user_access_end();
Chris Wilson262b6d32013-01-15 16:17:54 +00001568
Chris Wilson2889caa2017-06-16 15:05:19 +01001569 eb->exec[i].relocs_ptr = (uintptr_t)relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001570 }
1571
Chris Wilson2889caa2017-06-16 15:05:19 +01001572 return 0;
1573
1574err:
1575 while (i--) {
1576 struct drm_i915_gem_relocation_entry *relocs =
1577 u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr);
1578 if (eb->exec[i].relocation_count)
1579 kvfree(relocs);
1580 }
1581 return err;
1582}
1583
1584static int eb_prefault_relocations(const struct i915_execbuffer *eb)
1585{
1586 const unsigned int count = eb->buffer_count;
1587 unsigned int i;
1588
1589 if (unlikely(i915.prefault_disable))
1590 return 0;
1591
1592 for (i = 0; i < count; i++) {
1593 int err;
1594
1595 err = check_relocations(&eb->exec[i]);
1596 if (err)
1597 return err;
1598 }
1599
1600 return 0;
1601}
1602
1603static noinline int eb_relocate_slow(struct i915_execbuffer *eb)
1604{
1605 struct drm_device *dev = &eb->i915->drm;
1606 bool have_copy = false;
1607 struct i915_vma *vma;
1608 int err = 0;
1609
1610repeat:
1611 if (signal_pending(current)) {
1612 err = -ERESTARTSYS;
1613 goto out;
1614 }
1615
1616 /* We may process another execbuffer during the unlock... */
1617 eb_reset_vmas(eb);
1618 mutex_unlock(&dev->struct_mutex);
1619
1620 /*
1621 * We take 3 passes through the slowpatch.
1622 *
1623 * 1 - we try to just prefault all the user relocation entries and
1624 * then attempt to reuse the atomic pagefault disabled fast path again.
1625 *
1626 * 2 - we copy the user entries to a local buffer here outside of the
1627 * local and allow ourselves to wait upon any rendering before
1628 * relocations
1629 *
1630 * 3 - we already have a local copy of the relocation entries, but
1631 * were interrupted (EAGAIN) whilst waiting for the objects, try again.
1632 */
1633 if (!err) {
1634 err = eb_prefault_relocations(eb);
1635 } else if (!have_copy) {
1636 err = eb_copy_relocations(eb);
1637 have_copy = err == 0;
1638 } else {
1639 cond_resched();
1640 err = 0;
1641 }
1642 if (err) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001643 mutex_lock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01001644 goto out;
1645 }
1646
Chris Wilson8a2421b2017-06-16 15:05:22 +01001647 /* A frequent cause for EAGAIN are currently unavailable client pages */
1648 flush_workqueue(eb->i915->mm.userptr_wq);
1649
Chris Wilson2889caa2017-06-16 15:05:19 +01001650 err = i915_mutex_lock_interruptible(dev);
1651 if (err) {
1652 mutex_lock(&dev->struct_mutex);
1653 goto out;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001654 }
1655
Chris Wilson67731b82010-12-08 10:38:14 +00001656 /* reacquire the objects */
Chris Wilson2889caa2017-06-16 15:05:19 +01001657 err = eb_lookup_vmas(eb);
1658 if (err)
Chris Wilson3b96eff2013-01-08 10:53:14 +00001659 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +00001660
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001661 GEM_BUG_ON(!eb->batch);
1662
Chris Wilson2889caa2017-06-16 15:05:19 +01001663 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1664 if (!have_copy) {
1665 pagefault_disable();
1666 err = eb_relocate_vma(eb, vma);
1667 pagefault_enable();
1668 if (err)
1669 goto repeat;
1670 } else {
1671 err = eb_relocate_vma_slow(eb, vma);
1672 if (err)
1673 goto err;
1674 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001675 }
1676
Chris Wilson2889caa2017-06-16 15:05:19 +01001677 /*
1678 * Leave the user relocations as are, this is the painfully slow path,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001679 * and we want to avoid the complication of dropping the lock whilst
1680 * having buffers reserved in the aperture and so causing spurious
1681 * ENOSPC for random operations.
1682 */
1683
1684err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001685 if (err == -EAGAIN)
1686 goto repeat;
1687
1688out:
1689 if (have_copy) {
1690 const unsigned int count = eb->buffer_count;
1691 unsigned int i;
1692
1693 for (i = 0; i < count; i++) {
1694 const struct drm_i915_gem_exec_object2 *entry =
1695 &eb->exec[i];
1696 struct drm_i915_gem_relocation_entry *relocs;
1697
1698 if (!entry->relocation_count)
1699 continue;
1700
1701 relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1702 kvfree(relocs);
1703 }
1704 }
1705
Chris Wilson1f727d92017-07-21 15:50:36 +01001706 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001707}
1708
Chris Wilson2889caa2017-06-16 15:05:19 +01001709static int eb_relocate(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001710{
Chris Wilson2889caa2017-06-16 15:05:19 +01001711 if (eb_lookup_vmas(eb))
1712 goto slow;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001713
Chris Wilson2889caa2017-06-16 15:05:19 +01001714 /* The objects are in their final locations, apply the relocations. */
1715 if (eb->args->flags & __EXEC_HAS_RELOC) {
1716 struct i915_vma *vma;
1717
1718 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1719 if (eb_relocate_vma(eb, vma))
1720 goto slow;
1721 }
1722 }
1723
1724 return 0;
1725
1726slow:
1727 return eb_relocate_slow(eb);
1728}
1729
Chris Wilson95ff7c72017-06-16 15:05:25 +01001730static void eb_export_fence(struct i915_vma *vma,
Chris Wilson2889caa2017-06-16 15:05:19 +01001731 struct drm_i915_gem_request *req,
1732 unsigned int flags)
1733{
Chris Wilson95ff7c72017-06-16 15:05:25 +01001734 struct reservation_object *resv = vma->resv;
Chris Wilson2889caa2017-06-16 15:05:19 +01001735
1736 /*
1737 * Ignore errors from failing to allocate the new fence, we can't
1738 * handle an error right now. Worst case should be missed
1739 * synchronisation leading to rendering corruption.
1740 */
1741 reservation_object_lock(resv, NULL);
1742 if (flags & EXEC_OBJECT_WRITE)
1743 reservation_object_add_excl_fence(resv, &req->fence);
1744 else if (reservation_object_reserve_shared(resv) == 0)
1745 reservation_object_add_shared_fence(resv, &req->fence);
1746 reservation_object_unlock(resv);
1747}
1748
1749static int eb_move_to_gpu(struct i915_execbuffer *eb)
1750{
1751 const unsigned int count = eb->buffer_count;
1752 unsigned int i;
1753 int err;
1754
1755 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001756 unsigned int flags = eb->flags[i];
1757 struct i915_vma *vma = eb->vma[i];
Ben Widawsky27173f12013-08-14 11:38:36 +02001758 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001759
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001760 if (flags & EXEC_OBJECT_CAPTURE) {
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001761 struct i915_gem_capture_list *capture;
1762
1763 capture = kmalloc(sizeof(*capture), GFP_KERNEL);
1764 if (unlikely(!capture))
1765 return -ENOMEM;
1766
Chris Wilson650bc632017-06-15 09:14:33 +01001767 capture->next = eb->request->capture_list;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001768 capture->vma = eb->vma[i];
Chris Wilson650bc632017-06-15 09:14:33 +01001769 eb->request->capture_list = capture;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001770 }
1771
Chris Wilsonb8f55be2017-08-11 12:11:16 +01001772 /*
1773 * If the GPU is not _reading_ through the CPU cache, we need
1774 * to make sure that any writes (both previous GPU writes from
1775 * before a change in snooping levels and normal CPU writes)
1776 * caught in that cache are flushed to main memory.
1777 *
1778 * We want to say
1779 * obj->cache_dirty &&
1780 * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
1781 * but gcc's optimiser doesn't handle that as well and emits
1782 * two jumps instead of one. Maybe one day...
1783 */
1784 if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) {
Chris Wilson0f46daa2017-07-21 15:50:37 +01001785 if (i915_gem_clflush_object(obj, 0))
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001786 flags &= ~EXEC_OBJECT_ASYNC;
Chris Wilson0f46daa2017-07-21 15:50:37 +01001787 }
1788
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001789 if (flags & EXEC_OBJECT_ASYNC)
1790 continue;
Chris Wilson77ae9952017-01-27 09:40:07 +00001791
Chris Wilson2889caa2017-06-16 15:05:19 +01001792 err = i915_gem_request_await_object
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001793 (eb->request, obj, flags & EXEC_OBJECT_WRITE);
Chris Wilson2889caa2017-06-16 15:05:19 +01001794 if (err)
1795 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001796 }
1797
Chris Wilson2889caa2017-06-16 15:05:19 +01001798 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001799 unsigned int flags = eb->flags[i];
1800 struct i915_vma *vma = eb->vma[i];
Chris Wilson2889caa2017-06-16 15:05:19 +01001801
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001802 i915_vma_move_to_active(vma, eb->request, flags);
1803 eb_export_fence(vma, eb->request, flags);
1804
1805 __eb_unreserve_vma(vma, flags);
1806 vma->exec_flags = NULL;
1807
1808 if (unlikely(flags & __EXEC_OBJECT_HAS_REF))
Chris Wilsondade2a62017-06-16 15:05:20 +01001809 i915_vma_put(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001810 }
1811 eb->exec = NULL;
1812
Chris Wilsondcd79932016-08-18 17:16:40 +01001813 /* Unconditionally flush any chipset caches (for streaming writes). */
Chris Wilson650bc632017-06-15 09:14:33 +01001814 i915_gem_chipset_flush(eb->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001815
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001816 /* Unconditionally invalidate GPU caches and TLBs. */
Chris Wilson650bc632017-06-15 09:14:33 +01001817 return eb->engine->emit_flush(eb->request, EMIT_INVALIDATE);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001818}
1819
Chris Wilson2889caa2017-06-16 15:05:19 +01001820static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001821{
Chris Wilson650bc632017-06-15 09:14:33 +01001822 if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
Daniel Vettered5982e2013-01-17 22:23:36 +01001823 return false;
1824
Chris Wilson2f5945b2015-10-06 11:39:55 +01001825 /* Kernel clipping was a DRI1 misfeature */
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01001826 if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
1827 if (exec->num_cliprects || exec->cliprects_ptr)
1828 return false;
1829 }
Chris Wilson2f5945b2015-10-06 11:39:55 +01001830
1831 if (exec->DR4 == 0xffffffff) {
1832 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1833 exec->DR4 = 0;
1834 }
1835 if (exec->DR1 || exec->DR4)
1836 return false;
1837
1838 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1839 return false;
1840
1841 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001842}
1843
Chris Wilson5cf3d282016-08-04 07:52:43 +01001844void i915_vma_move_to_active(struct i915_vma *vma,
1845 struct drm_i915_gem_request *req,
1846 unsigned int flags)
1847{
1848 struct drm_i915_gem_object *obj = vma->obj;
1849 const unsigned int idx = req->engine->id;
1850
Chris Wilson81147b02016-12-18 15:37:18 +00001851 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001852 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1853
Chris Wilson2889caa2017-06-16 15:05:19 +01001854 /*
1855 * Add a reference if we're newly entering the active list.
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001856 * The order in which we add operations to the retirement queue is
1857 * vital here: mark_active adds to the start of the callback list,
1858 * such that subsequent callbacks are called first. Therefore we
1859 * add the active reference first and queue for it to be dropped
1860 * *last*.
1861 */
Chris Wilsond07f0e52016-10-28 13:58:44 +01001862 if (!i915_vma_is_active(vma))
1863 obj->active_count++;
1864 i915_vma_set_active(vma, idx);
1865 i915_gem_active_set(&vma->last_read[idx], req);
1866 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001867
Chris Wilsone27ab732017-06-15 13:38:49 +01001868 obj->base.write_domain = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001869 if (flags & EXEC_OBJECT_WRITE) {
Chris Wilsone27ab732017-06-15 13:38:49 +01001870 obj->base.write_domain = I915_GEM_DOMAIN_RENDER;
1871
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00001872 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
1873 i915_gem_active_set(&obj->frontbuffer_write, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001874
Chris Wilsone27ab732017-06-15 13:38:49 +01001875 obj->base.read_domains = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001876 }
Chris Wilsone27ab732017-06-15 13:38:49 +01001877 obj->base.read_domains |= I915_GEM_GPU_DOMAINS;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001878
Chris Wilson49ef5292016-08-18 17:17:00 +01001879 if (flags & EXEC_OBJECT_NEEDS_FENCE)
1880 i915_gem_active_set(&vma->last_fence, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001881}
1882
Chris Wilson2889caa2017-06-16 15:05:19 +01001883static int i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001884{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001885 u32 *cs;
1886 int i;
Eric Anholtae662d32012-01-03 09:23:29 -08001887
Chris Wilsonb5321f32016-08-02 22:50:18 +01001888 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001889 DRM_DEBUG("sol reset is gen7/rcs only\n");
1890 return -EINVAL;
1891 }
Eric Anholtae662d32012-01-03 09:23:29 -08001892
Chris Wilson2889caa2017-06-16 15:05:19 +01001893 cs = intel_ring_begin(req, 4 * 2 + 2);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001894 if (IS_ERR(cs))
1895 return PTR_ERR(cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001896
Chris Wilson2889caa2017-06-16 15:05:19 +01001897 *cs++ = MI_LOAD_REGISTER_IMM(4);
Eric Anholtae662d32012-01-03 09:23:29 -08001898 for (i = 0; i < 4; i++) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001899 *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
1900 *cs++ = 0;
Eric Anholtae662d32012-01-03 09:23:29 -08001901 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001902 *cs++ = MI_NOOP;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001903 intel_ring_advance(req, cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001904
1905 return 0;
1906}
1907
Chris Wilson650bc632017-06-15 09:14:33 +01001908static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001909{
Brad Volkin71745372014-12-11 12:13:12 -08001910 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001911 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001912 int err;
Brad Volkin71745372014-12-11 12:13:12 -08001913
Chris Wilson650bc632017-06-15 09:14:33 +01001914 shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool,
1915 PAGE_ALIGN(eb->batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001916 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001917 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001918
Chris Wilson2889caa2017-06-16 15:05:19 +01001919 err = intel_engine_cmd_parser(eb->engine,
Chris Wilson650bc632017-06-15 09:14:33 +01001920 eb->batch->obj,
Chris Wilson33a051a2016-07-27 09:07:26 +01001921 shadow_batch_obj,
Chris Wilson650bc632017-06-15 09:14:33 +01001922 eb->batch_start_offset,
1923 eb->batch_len,
Chris Wilson33a051a2016-07-27 09:07:26 +01001924 is_master);
Chris Wilson2889caa2017-06-16 15:05:19 +01001925 if (err) {
1926 if (err == -EACCES) /* unhandled chained batch */
Chris Wilson058d88c2016-08-15 10:49:06 +01001927 vma = NULL;
1928 else
Chris Wilson2889caa2017-06-16 15:05:19 +01001929 vma = ERR_PTR(err);
Chris Wilson058d88c2016-08-15 10:49:06 +01001930 goto out;
1931 }
Brad Volkin71745372014-12-11 12:13:12 -08001932
Chris Wilson058d88c2016-08-15 10:49:06 +01001933 vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
1934 if (IS_ERR(vma))
1935 goto out;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001936
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001937 eb->vma[eb->buffer_count] = i915_vma_get(vma);
1938 eb->flags[eb->buffer_count] =
1939 __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF;
1940 vma->exec_flags = &eb->flags[eb->buffer_count];
1941 eb->buffer_count++;
Brad Volkin71745372014-12-11 12:13:12 -08001942
Chris Wilson058d88c2016-08-15 10:49:06 +01001943out:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001944 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson058d88c2016-08-15 10:49:06 +01001945 return vma;
Brad Volkin71745372014-12-11 12:13:12 -08001946}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001947
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001948static void
Chris Wilson2889caa2017-06-16 15:05:19 +01001949add_to_client(struct drm_i915_gem_request *req, struct drm_file *file)
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001950{
1951 req->file_priv = file->driver_priv;
1952 list_add_tail(&req->client_link, &req->file_priv->mm.request_list);
1953}
1954
Chris Wilson2889caa2017-06-16 15:05:19 +01001955static int eb_submit(struct i915_execbuffer *eb)
Oscar Mateo78382592014-07-03 16:28:05 +01001956{
Chris Wilson2889caa2017-06-16 15:05:19 +01001957 int err;
Oscar Mateo78382592014-07-03 16:28:05 +01001958
Chris Wilson2889caa2017-06-16 15:05:19 +01001959 err = eb_move_to_gpu(eb);
1960 if (err)
1961 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001962
Chris Wilson2889caa2017-06-16 15:05:19 +01001963 err = i915_switch_context(eb->request);
1964 if (err)
1965 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001966
Chris Wilson650bc632017-06-15 09:14:33 +01001967 if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001968 err = i915_reset_gen7_sol_offsets(eb->request);
1969 if (err)
1970 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001971 }
1972
Chris Wilson2889caa2017-06-16 15:05:19 +01001973 err = eb->engine->emit_bb_start(eb->request,
Chris Wilson650bc632017-06-15 09:14:33 +01001974 eb->batch->node.start +
1975 eb->batch_start_offset,
1976 eb->batch_len,
Chris Wilson2889caa2017-06-16 15:05:19 +01001977 eb->batch_flags);
1978 if (err)
1979 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001980
Chris Wilson2f5945b2015-10-06 11:39:55 +01001981 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001982}
1983
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001984/**
1985 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01001986 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001987 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001988static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01001989gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
1990 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001991{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001992 struct drm_i915_file_private *file_priv = file->driver_priv;
1993
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001994 /* Check whether the file_priv has already selected one ring. */
Joonas Lahtinen6f633402016-09-01 14:58:21 +03001995 if ((int)file_priv->bsd_engine < 0)
1996 file_priv->bsd_engine = atomic_fetch_xor(1,
1997 &dev_priv->mm.bsd_engine_dispatch_index);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001998
Chris Wilsonc80ff162016-07-27 09:07:27 +01001999 return file_priv->bsd_engine;
Chris Wilsond23db882014-05-23 08:48:08 +02002000}
2001
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002002#define I915_USER_RINGS (4)
2003
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002004static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002005 [I915_EXEC_DEFAULT] = RCS,
2006 [I915_EXEC_RENDER] = RCS,
2007 [I915_EXEC_BLT] = BCS,
2008 [I915_EXEC_BSD] = VCS,
2009 [I915_EXEC_VEBOX] = VECS
2010};
2011
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002012static struct intel_engine_cs *
2013eb_select_engine(struct drm_i915_private *dev_priv,
2014 struct drm_file *file,
2015 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002016{
2017 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002018 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002019
2020 if (user_ring_id > I915_USER_RINGS) {
2021 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002022 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002023 }
2024
2025 if ((user_ring_id != I915_EXEC_BSD) &&
2026 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
2027 DRM_DEBUG("execbuf with non bsd ring but with invalid "
2028 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002029 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002030 }
2031
2032 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
2033 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
2034
2035 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01002036 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002037 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
2038 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00002039 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002040 bsd_idx--;
2041 } else {
2042 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
2043 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002044 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002045 }
2046
Akash Goel3b3f1652016-10-13 22:44:48 +05302047 engine = dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002048 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +05302049 engine = dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002050 }
2051
Akash Goel3b3f1652016-10-13 22:44:48 +05302052 if (!engine) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002053 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002054 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002055 }
2056
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002057 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002058}
2059
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002060static void
2061__free_fence_array(struct drm_syncobj **fences, unsigned int n)
2062{
2063 while (n--)
2064 drm_syncobj_put(ptr_mask_bits(fences[n], 2));
2065 kvfree(fences);
2066}
2067
2068static struct drm_syncobj **
2069get_fence_array(struct drm_i915_gem_execbuffer2 *args,
2070 struct drm_file *file)
2071{
2072 const unsigned int nfences = args->num_cliprects;
2073 struct drm_i915_gem_exec_fence __user *user;
2074 struct drm_syncobj **fences;
2075 unsigned int n;
2076 int err;
2077
2078 if (!(args->flags & I915_EXEC_FENCE_ARRAY))
2079 return NULL;
2080
2081 if (nfences > SIZE_MAX / sizeof(*fences))
2082 return ERR_PTR(-EINVAL);
2083
2084 user = u64_to_user_ptr(args->cliprects_ptr);
2085 if (!access_ok(VERIFY_READ, user, nfences * 2 * sizeof(u32)))
2086 return ERR_PTR(-EFAULT);
2087
2088 fences = kvmalloc_array(args->num_cliprects, sizeof(*fences),
2089 __GFP_NOWARN | GFP_TEMPORARY);
2090 if (!fences)
2091 return ERR_PTR(-ENOMEM);
2092
2093 for (n = 0; n < nfences; n++) {
2094 struct drm_i915_gem_exec_fence fence;
2095 struct drm_syncobj *syncobj;
2096
2097 if (__copy_from_user(&fence, user++, sizeof(fence))) {
2098 err = -EFAULT;
2099 goto err;
2100 }
2101
2102 syncobj = drm_syncobj_find(file, fence.handle);
2103 if (!syncobj) {
2104 DRM_DEBUG("Invalid syncobj handle provided\n");
2105 err = -ENOENT;
2106 goto err;
2107 }
2108
2109 fences[n] = ptr_pack_bits(syncobj, fence.flags, 2);
2110 }
2111
2112 return fences;
2113
2114err:
2115 __free_fence_array(fences, n);
2116 return ERR_PTR(err);
2117}
2118
2119static void
2120put_fence_array(struct drm_i915_gem_execbuffer2 *args,
2121 struct drm_syncobj **fences)
2122{
2123 if (fences)
2124 __free_fence_array(fences, args->num_cliprects);
2125}
2126
2127static int
2128await_fence_array(struct i915_execbuffer *eb,
2129 struct drm_syncobj **fences)
2130{
2131 const unsigned int nfences = eb->args->num_cliprects;
2132 unsigned int n;
2133 int err;
2134
2135 for (n = 0; n < nfences; n++) {
2136 struct drm_syncobj *syncobj;
2137 struct dma_fence *fence;
2138 unsigned int flags;
2139
2140 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2141 if (!(flags & I915_EXEC_FENCE_WAIT))
2142 continue;
2143
Jason Ekstrandafca4212017-08-25 10:52:21 -07002144 fence = drm_syncobj_fence_get(syncobj);
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002145 if (!fence)
2146 return -EINVAL;
2147
2148 err = i915_gem_request_await_dma_fence(eb->request, fence);
2149 dma_fence_put(fence);
2150 if (err < 0)
2151 return err;
2152 }
2153
2154 return 0;
2155}
2156
2157static void
2158signal_fence_array(struct i915_execbuffer *eb,
2159 struct drm_syncobj **fences)
2160{
2161 const unsigned int nfences = eb->args->num_cliprects;
2162 struct dma_fence * const fence = &eb->request->fence;
2163 unsigned int n;
2164
2165 for (n = 0; n < nfences; n++) {
2166 struct drm_syncobj *syncobj;
2167 unsigned int flags;
2168
2169 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2170 if (!(flags & I915_EXEC_FENCE_SIGNAL))
2171 continue;
2172
2173 drm_syncobj_replace_fence(syncobj, fence);
2174 }
2175}
2176
Eric Anholtae662d32012-01-03 09:23:29 -08002177static int
Chris Wilson650bc632017-06-15 09:14:33 +01002178i915_gem_do_execbuffer(struct drm_device *dev,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002179 struct drm_file *file,
2180 struct drm_i915_gem_execbuffer2 *args,
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002181 struct drm_i915_gem_exec_object2 *exec,
2182 struct drm_syncobj **fences)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002183{
Chris Wilson650bc632017-06-15 09:14:33 +01002184 struct i915_execbuffer eb;
Chris Wilsonfec04452017-01-27 09:40:08 +00002185 struct dma_fence *in_fence = NULL;
2186 struct sync_file *out_fence = NULL;
2187 int out_fence_fd = -1;
Chris Wilson2889caa2017-06-16 15:05:19 +01002188 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002189
Chris Wilson2889caa2017-06-16 15:05:19 +01002190 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
2191 ~__EXEC_OBJECT_UNKNOWN_FLAGS);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002192
Chris Wilson650bc632017-06-15 09:14:33 +01002193 eb.i915 = to_i915(dev);
2194 eb.file = file;
2195 eb.args = args;
Chris Wilson7dd4f672017-06-16 15:05:24 +01002196 if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC))
Chris Wilson2889caa2017-06-16 15:05:19 +01002197 args->flags |= __EXEC_HAS_RELOC;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002198
Chris Wilson650bc632017-06-15 09:14:33 +01002199 eb.exec = exec;
Chris Wilson170fa292017-08-16 09:52:07 +01002200 eb.vma = (struct i915_vma **)(exec + args->buffer_count + 1);
2201 eb.vma[0] = NULL;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002202 eb.flags = (unsigned int *)(eb.vma + args->buffer_count + 1);
2203
Chris Wilson2889caa2017-06-16 15:05:19 +01002204 eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
2205 if (USES_FULL_PPGTT(eb.i915))
2206 eb.invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson650bc632017-06-15 09:14:33 +01002207 reloc_cache_init(&eb.reloc_cache, eb.i915);
2208
Chris Wilson2889caa2017-06-16 15:05:19 +01002209 eb.buffer_count = args->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +01002210 eb.batch_start_offset = args->batch_start_offset;
2211 eb.batch_len = args->batch_len;
2212
Chris Wilson2889caa2017-06-16 15:05:19 +01002213 eb.batch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002214 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02002215 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002216 return -EPERM;
2217
Chris Wilson2889caa2017-06-16 15:05:19 +01002218 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002219 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01002220 if (args->flags & I915_EXEC_IS_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +01002221 eb.batch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002222
Chris Wilson650bc632017-06-15 09:14:33 +01002223 eb.engine = eb_select_engine(eb.i915, file, args);
2224 if (!eb.engine)
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002225 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002226
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002227 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
Chris Wilson650bc632017-06-15 09:14:33 +01002228 if (!HAS_RESOURCE_STREAMER(eb.i915)) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002229 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
2230 return -EINVAL;
2231 }
Chris Wilson650bc632017-06-15 09:14:33 +01002232 if (eb.engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002233 DRM_DEBUG("RS is not available on %s\n",
Chris Wilson650bc632017-06-15 09:14:33 +01002234 eb.engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002235 return -EINVAL;
2236 }
2237
Chris Wilson2889caa2017-06-16 15:05:19 +01002238 eb.batch_flags |= I915_DISPATCH_RS;
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002239 }
2240
Chris Wilsonfec04452017-01-27 09:40:08 +00002241 if (args->flags & I915_EXEC_FENCE_IN) {
2242 in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002243 if (!in_fence)
2244 return -EINVAL;
Chris Wilsonfec04452017-01-27 09:40:08 +00002245 }
2246
2247 if (args->flags & I915_EXEC_FENCE_OUT) {
2248 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
2249 if (out_fence_fd < 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002250 err = out_fence_fd;
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002251 goto err_in_fence;
Chris Wilsonfec04452017-01-27 09:40:08 +00002252 }
2253 }
2254
Chris Wilson4d470f72017-06-29 16:04:25 +01002255 err = eb_create(&eb);
2256 if (err)
2257 goto err_out_fence;
2258
2259 GEM_BUG_ON(!eb.lut_size);
Chris Wilson2889caa2017-06-16 15:05:19 +01002260
Chris Wilson1acfc102017-06-20 12:05:47 +01002261 err = eb_select_context(&eb);
2262 if (unlikely(err))
2263 goto err_destroy;
2264
Chris Wilson2889caa2017-06-16 15:05:19 +01002265 /*
2266 * Take a local wakeref for preparing to dispatch the execbuf as
Chris Wilson67d97da2016-07-04 08:08:31 +01002267 * we expect to access the hardware fairly frequently in the
2268 * process. Upon first dispatch, we acquire another prolonged
2269 * wakeref that we hold until the GPU has been idle for at least
2270 * 100ms.
2271 */
Chris Wilson650bc632017-06-15 09:14:33 +01002272 intel_runtime_pm_get(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002273
Chris Wilson2889caa2017-06-16 15:05:19 +01002274 err = i915_mutex_lock_interruptible(dev);
2275 if (err)
2276 goto err_rpm;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002277
Chris Wilson2889caa2017-06-16 15:05:19 +01002278 err = eb_relocate(&eb);
Chris Wilson1f727d92017-07-21 15:50:36 +01002279 if (err) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002280 /*
2281 * If the user expects the execobject.offset and
2282 * reloc.presumed_offset to be an exact match,
2283 * as for using NO_RELOC, then we cannot update
2284 * the execobject.offset until we have completed
2285 * relocation.
2286 */
2287 args->flags &= ~__EXEC_HAS_RELOC;
Chris Wilson2889caa2017-06-16 15:05:19 +01002288 goto err_vma;
Chris Wilson1f727d92017-07-21 15:50:36 +01002289 }
Ben Widawsky41bde552013-12-06 14:11:21 -08002290
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002291 if (unlikely(*eb.batch->exec_flags & EXEC_OBJECT_WRITE)) {
Daniel Vetterff240192012-01-31 21:08:14 +01002292 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002293 err = -EINVAL;
2294 goto err_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002295 }
Chris Wilson650bc632017-06-15 09:14:33 +01002296 if (eb.batch_start_offset > eb.batch->size ||
2297 eb.batch_len > eb.batch->size - eb.batch_start_offset) {
Chris Wilson0b537272016-08-18 17:17:12 +01002298 DRM_DEBUG("Attempting to use out-of-bounds batch\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002299 err = -EINVAL;
2300 goto err_vma;
Chris Wilson0b537272016-08-18 17:17:12 +01002301 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002302
Chris Wilson3dbf26e2017-08-26 14:56:20 +01002303 if (eb_use_cmdparser(&eb)) {
Chris Wilson59bfa122016-08-04 16:32:31 +01002304 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002305
Chris Wilson650bc632017-06-15 09:14:33 +01002306 vma = eb_parse(&eb, drm_is_current_master(file));
Chris Wilson59bfa122016-08-04 16:32:31 +01002307 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002308 err = PTR_ERR(vma);
2309 goto err_vma;
Brad Volkin78a42372014-12-11 12:13:09 -08002310 }
Chris Wilson17cabf52015-01-14 11:20:57 +00002311
Chris Wilson59bfa122016-08-04 16:32:31 +01002312 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002313 /*
2314 * Batch parsed and accepted:
2315 *
2316 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
2317 * bit from MI_BATCH_BUFFER_START commands issued in
2318 * the dispatch_execbuffer implementations. We
2319 * specifically don't want that set on batches the
2320 * command parser has accepted.
2321 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002322 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilson650bc632017-06-15 09:14:33 +01002323 eb.batch_start_offset = 0;
2324 eb.batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002325 }
Brad Volkin351e3db2014-02-18 10:15:46 -08002326 }
2327
Chris Wilson650bc632017-06-15 09:14:33 +01002328 if (eb.batch_len == 0)
2329 eb.batch_len = eb.batch->size - eb.batch_start_offset;
Brad Volkin78a42372014-12-11 12:13:09 -08002330
Chris Wilson2889caa2017-06-16 15:05:19 +01002331 /*
2332 * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002333 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07002334 * hsw should have this fixed, but bdw mucks it up again. */
Chris Wilson2889caa2017-06-16 15:05:19 +01002335 if (eb.batch_flags & I915_DISPATCH_SECURE) {
Chris Wilson058d88c2016-08-15 10:49:06 +01002336 struct i915_vma *vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002337
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002338 /*
2339 * So on first glance it looks freaky that we pin the batch here
2340 * outside of the reservation loop. But:
2341 * - The batch is already pinned into the relevant ppgtt, so we
2342 * already have the backing storage fully allocated.
2343 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01002344 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002345 * fitting due to fragmentation.
2346 * So this is actually safe.
2347 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002348 vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0);
Chris Wilson058d88c2016-08-15 10:49:06 +01002349 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002350 err = PTR_ERR(vma);
2351 goto err_vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01002352 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002353
Chris Wilson650bc632017-06-15 09:14:33 +01002354 eb.batch = vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002355 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002356
Chris Wilson7dd4f672017-06-16 15:05:24 +01002357 /* All GPU relocation batches must be submitted prior to the user rq */
2358 GEM_BUG_ON(eb.reloc_cache.rq);
2359
John Harrison0c8dac82015-05-29 17:43:25 +01002360 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson650bc632017-06-15 09:14:33 +01002361 eb.request = i915_gem_request_alloc(eb.engine, eb.ctx);
2362 if (IS_ERR(eb.request)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002363 err = PTR_ERR(eb.request);
John Harrison0c8dac82015-05-29 17:43:25 +01002364 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00002365 }
John Harrison0c8dac82015-05-29 17:43:25 +01002366
Chris Wilsonfec04452017-01-27 09:40:08 +00002367 if (in_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002368 err = i915_gem_request_await_dma_fence(eb.request, in_fence);
2369 if (err < 0)
Chris Wilsonfec04452017-01-27 09:40:08 +00002370 goto err_request;
2371 }
2372
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002373 if (fences) {
2374 err = await_fence_array(&eb, fences);
2375 if (err)
2376 goto err_request;
2377 }
2378
Chris Wilsonfec04452017-01-27 09:40:08 +00002379 if (out_fence_fd != -1) {
Chris Wilson650bc632017-06-15 09:14:33 +01002380 out_fence = sync_file_create(&eb.request->fence);
Chris Wilsonfec04452017-01-27 09:40:08 +00002381 if (!out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002382 err = -ENOMEM;
Chris Wilsonfec04452017-01-27 09:40:08 +00002383 goto err_request;
2384 }
2385 }
2386
Chris Wilson2889caa2017-06-16 15:05:19 +01002387 /*
2388 * Whilst this request exists, batch_obj will be on the
Chris Wilson17f298cf2016-08-10 13:41:46 +01002389 * active_list, and so will hold the active reference. Only when this
2390 * request is retired will the the batch_obj be moved onto the
2391 * inactive_list and lose its active reference. Hence we do not need
2392 * to explicitly hold another reference here.
2393 */
Chris Wilson650bc632017-06-15 09:14:33 +01002394 eb.request->batch = eb.batch;
Chris Wilson17f298cf2016-08-10 13:41:46 +01002395
Chris Wilson2889caa2017-06-16 15:05:19 +01002396 trace_i915_gem_request_queue(eb.request, eb.batch_flags);
2397 err = eb_submit(&eb);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01002398err_request:
Chris Wilson2889caa2017-06-16 15:05:19 +01002399 __i915_add_request(eb.request, err == 0);
Chris Wilson650bc632017-06-15 09:14:33 +01002400 add_to_client(eb.request, file);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002401
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002402 if (fences)
2403 signal_fence_array(&eb, fences);
2404
Chris Wilsonfec04452017-01-27 09:40:08 +00002405 if (out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002406 if (err == 0) {
Chris Wilsonfec04452017-01-27 09:40:08 +00002407 fd_install(out_fence_fd, out_fence->file);
2408 args->rsvd2 &= GENMASK_ULL(0, 31); /* keep in-fence */
2409 args->rsvd2 |= (u64)out_fence_fd << 32;
2410 out_fence_fd = -1;
2411 } else {
2412 fput(out_fence->file);
2413 }
2414 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002415
John Harrison0c8dac82015-05-29 17:43:25 +01002416err_batch_unpin:
Chris Wilson2889caa2017-06-16 15:05:19 +01002417 if (eb.batch_flags & I915_DISPATCH_SECURE)
Chris Wilson650bc632017-06-15 09:14:33 +01002418 i915_vma_unpin(eb.batch);
Chris Wilson2889caa2017-06-16 15:05:19 +01002419err_vma:
2420 if (eb.exec)
2421 eb_release_vmas(&eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002422 mutex_unlock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01002423err_rpm:
Chris Wilson650bc632017-06-15 09:14:33 +01002424 intel_runtime_pm_put(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002425 i915_gem_context_put(eb.ctx);
2426err_destroy:
Chris Wilson2889caa2017-06-16 15:05:19 +01002427 eb_destroy(&eb);
Chris Wilson4d470f72017-06-29 16:04:25 +01002428err_out_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002429 if (out_fence_fd != -1)
2430 put_unused_fd(out_fence_fd);
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002431err_in_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002432 dma_fence_put(in_fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01002433 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002434}
2435
2436/*
2437 * Legacy execbuffer just creates an exec2 list from the original exec object
2438 * list array and passes it to the real function.
2439 */
2440int
2441i915_gem_execbuffer(struct drm_device *dev, void *data,
2442 struct drm_file *file)
2443{
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002444 const size_t sz = (sizeof(struct drm_i915_gem_exec_object2) +
2445 sizeof(struct i915_vma *) +
2446 sizeof(unsigned int));
Chris Wilson54cf91d2010-11-25 18:00:26 +00002447 struct drm_i915_gem_execbuffer *args = data;
2448 struct drm_i915_gem_execbuffer2 exec2;
2449 struct drm_i915_gem_exec_object *exec_list = NULL;
2450 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +01002451 unsigned int i;
2452 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002453
Chris Wilson2889caa2017-06-16 15:05:19 +01002454 if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) {
2455 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002456 return -EINVAL;
2457 }
2458
Chris Wilson2889caa2017-06-16 15:05:19 +01002459 exec2.buffers_ptr = args->buffers_ptr;
2460 exec2.buffer_count = args->buffer_count;
2461 exec2.batch_start_offset = args->batch_start_offset;
2462 exec2.batch_len = args->batch_len;
2463 exec2.DR1 = args->DR1;
2464 exec2.DR4 = args->DR4;
2465 exec2.num_cliprects = args->num_cliprects;
2466 exec2.cliprects_ptr = args->cliprects_ptr;
2467 exec2.flags = I915_EXEC_RENDER;
2468 i915_execbuffer2_set_context_id(exec2, 0);
2469
2470 if (!i915_gem_check_execbuffer(&exec2))
2471 return -EINVAL;
2472
Chris Wilson54cf91d2010-11-25 18:00:26 +00002473 /* Copy in the exec list from userland */
Chris Wilson2889caa2017-06-16 15:05:19 +01002474 exec_list = kvmalloc_array(args->buffer_count, sizeof(*exec_list),
2475 __GFP_NOWARN | GFP_TEMPORARY);
2476 exec2_list = kvmalloc_array(args->buffer_count + 1, sz,
2477 __GFP_NOWARN | GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002478 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002479 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002480 args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002481 kvfree(exec_list);
2482 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002483 return -ENOMEM;
2484 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002485 err = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002486 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00002487 sizeof(*exec_list) * args->buffer_count);
Chris Wilson2889caa2017-06-16 15:05:19 +01002488 if (err) {
Daniel Vetterff240192012-01-31 21:08:14 +01002489 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson2889caa2017-06-16 15:05:19 +01002490 args->buffer_count, err);
Michal Hocko20981052017-05-17 14:23:12 +02002491 kvfree(exec_list);
2492 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002493 return -EFAULT;
2494 }
2495
2496 for (i = 0; i < args->buffer_count; i++) {
2497 exec2_list[i].handle = exec_list[i].handle;
2498 exec2_list[i].relocation_count = exec_list[i].relocation_count;
2499 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
2500 exec2_list[i].alignment = exec_list[i].alignment;
2501 exec2_list[i].offset = exec_list[i].offset;
Tvrtko Ursulinf0836b72016-11-16 08:55:32 +00002502 if (INTEL_GEN(to_i915(dev)) < 4)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002503 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
2504 else
2505 exec2_list[i].flags = 0;
2506 }
2507
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002508 err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list, NULL);
Chris Wilson2889caa2017-06-16 15:05:19 +01002509 if (exec2.flags & __EXEC_HAS_RELOC) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002510 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002511 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002512
Chris Wilson54cf91d2010-11-25 18:00:26 +00002513 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002514 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002515 if (!(exec2_list[i].offset & UPDATE))
2516 continue;
2517
Michał Winiarski934acce2015-12-29 18:24:52 +01002518 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002519 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2520 exec2_list[i].offset &= PIN_OFFSET_MASK;
2521 if (__copy_to_user(&user_exec_list[i].offset,
2522 &exec2_list[i].offset,
2523 sizeof(user_exec_list[i].offset)))
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002524 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002525 }
2526 }
2527
Michal Hocko20981052017-05-17 14:23:12 +02002528 kvfree(exec_list);
2529 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002530 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002531}
2532
2533int
2534i915_gem_execbuffer2(struct drm_device *dev, void *data,
2535 struct drm_file *file)
2536{
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002537 const size_t sz = (sizeof(struct drm_i915_gem_exec_object2) +
2538 sizeof(struct i915_vma *) +
2539 sizeof(unsigned int));
Chris Wilson54cf91d2010-11-25 18:00:26 +00002540 struct drm_i915_gem_execbuffer2 *args = data;
Chris Wilson2889caa2017-06-16 15:05:19 +01002541 struct drm_i915_gem_exec_object2 *exec2_list;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002542 struct drm_syncobj **fences = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +01002543 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002544
Chris Wilson2889caa2017-06-16 15:05:19 +01002545 if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01002546 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002547 return -EINVAL;
2548 }
2549
Chris Wilson2889caa2017-06-16 15:05:19 +01002550 if (!i915_gem_check_execbuffer(args))
2551 return -EINVAL;
2552
2553 /* Allocate an extra slot for use by the command parser */
2554 exec2_list = kvmalloc_array(args->buffer_count + 1, sz,
2555 __GFP_NOWARN | GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002556 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002557 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002558 args->buffer_count);
2559 return -ENOMEM;
2560 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002561 if (copy_from_user(exec2_list,
2562 u64_to_user_ptr(args->buffers_ptr),
2563 sizeof(*exec2_list) * args->buffer_count)) {
2564 DRM_DEBUG("copy %d exec entries failed\n", args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002565 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002566 return -EFAULT;
2567 }
2568
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002569 if (args->flags & I915_EXEC_FENCE_ARRAY) {
2570 fences = get_fence_array(args, file);
2571 if (IS_ERR(fences)) {
2572 kvfree(exec2_list);
2573 return PTR_ERR(fences);
2574 }
2575 }
2576
2577 err = i915_gem_do_execbuffer(dev, file, args, exec2_list, fences);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002578
Chris Wilson2889caa2017-06-16 15:05:19 +01002579 /*
2580 * Now that we have begun execution of the batchbuffer, we ignore
2581 * any new error after this point. Also given that we have already
2582 * updated the associated relocations, we try to write out the current
2583 * object locations irrespective of any error.
2584 */
2585 if (args->flags & __EXEC_HAS_RELOC) {
2586 struct drm_i915_gem_exec_object2 __user *user_exec_list =
2587 u64_to_user_ptr(args->buffers_ptr);
2588 unsigned int i;
2589
2590 /* Copy the new buffer offsets back to the user's exec list. */
2591 user_access_begin();
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002592 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002593 if (!(exec2_list[i].offset & UPDATE))
2594 continue;
2595
Michał Winiarski934acce2015-12-29 18:24:52 +01002596 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002597 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2598 unsafe_put_user(exec2_list[i].offset,
2599 &user_exec_list[i].offset,
2600 end_user);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002601 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002602end_user:
2603 user_access_end();
Chris Wilson54cf91d2010-11-25 18:00:26 +00002604 }
2605
Chris Wilson2889caa2017-06-16 15:05:19 +01002606 args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002607 put_fence_array(args, fences);
Michal Hocko20981052017-05-17 14:23:12 +02002608 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002609 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002610}