blob: 214a850b4b3cb1f2fb7f13212e298e19aaede99d [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 Wilsonac70ebe2017-09-12 16:07:52 +0100682 struct drm_i915_gem_object *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 Wilsonac70ebe2017-09-12 16:07:52 +0100728 /* transfer ref to ctx */
Chris Wilson3ffff012017-08-22 12:05:17 +0100729 vma->open_count++;
Chris Wilsond1b48c12017-08-16 09:52:08 +0100730 list_add(&lut->obj_link, &obj->lut_list);
731 list_add(&lut->ctx_link, &eb->ctx->handles_list);
732 lut->ctx = eb->ctx;
733 lut->handle = handle;
734
Chris Wilson170fa292017-08-16 09:52:07 +0100735add_vma:
Chris Wilsond1b48c12017-08-16 09:52:08 +0100736 err = eb_add_vma(eb, i, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100737 if (unlikely(err))
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100738 goto err_vma;
Chris Wilsondade2a62017-06-16 15:05:20 +0100739
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100740 GEM_BUG_ON(vma != eb->vma[i]);
741 GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100742 }
743
Chris Wilson2889caa2017-06-16 15:05:19 +0100744 /* take note of the batch buffer before we might reorder the lists */
745 i = eb_batch_index(eb);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100746 eb->batch = eb->vma[i];
747 GEM_BUG_ON(eb->batch->exec_flags != &eb->flags[i]);
Chris Wilson59bfa122016-08-04 16:32:31 +0100748
749 /*
750 * SNA is doing fancy tricks with compressing batch buffers, which leads
751 * to negative relocation deltas. Usually that works out ok since the
752 * relocate address is still positive, except when the batch is placed
753 * very low in the GTT. Ensure this doesn't happen.
754 *
755 * Note that actual hangs have only been observed on gen7, but for
756 * paranoia do it everywhere.
757 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100758 if (!(eb->flags[i] & EXEC_OBJECT_PINNED))
759 eb->flags[i] |= __EXEC_OBJECT_NEEDS_BIAS;
Chris Wilson2889caa2017-06-16 15:05:19 +0100760 if (eb->reloc_cache.has_fence)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100761 eb->flags[i] |= EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson59bfa122016-08-04 16:32:31 +0100762
Chris Wilson2889caa2017-06-16 15:05:19 +0100763 eb->args->flags |= __EXEC_VALIDATED;
764 return eb_reserve(eb);
765
Chris Wilson170fa292017-08-16 09:52:07 +0100766err_obj:
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100767 i915_gem_object_put(obj);
Chris Wilson170fa292017-08-16 09:52:07 +0100768err_vma:
769 eb->vma[i] = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100770 return err;
Chris Wilson59bfa122016-08-04 16:32:31 +0100771}
772
Chris Wilson4ff4b442017-06-16 15:05:16 +0100773static struct i915_vma *
Chris Wilson2889caa2017-06-16 15:05:19 +0100774eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000775{
Chris Wilson2889caa2017-06-16 15:05:19 +0100776 if (eb->lut_size < 0) {
777 if (handle >= -eb->lut_size)
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000778 return NULL;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100779 return eb->vma[handle];
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000780 } else {
781 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800782 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000783
Chris Wilson2889caa2017-06-16 15:05:19 +0100784 head = &eb->buckets[hash_32(handle, eb->lut_size)];
Geliang Tangaa459502016-01-18 23:54:20 +0800785 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200786 if (vma->exec_handle == handle)
787 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000788 }
789 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000790 }
Chris Wilson67731b82010-12-08 10:38:14 +0000791}
792
Chris Wilson2889caa2017-06-16 15:05:19 +0100793static void eb_release_vmas(const struct i915_execbuffer *eb)
Chris Wilsona415d352013-11-26 11:23:15 +0000794{
Chris Wilson2889caa2017-06-16 15:05:19 +0100795 const unsigned int count = eb->buffer_count;
796 unsigned int i;
Chris Wilson650bc632017-06-15 09:14:33 +0100797
Chris Wilson2889caa2017-06-16 15:05:19 +0100798 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100799 struct i915_vma *vma = eb->vma[i];
800 unsigned int flags = eb->flags[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100801
802 if (!vma)
Chris Wilson170fa292017-08-16 09:52:07 +0100803 break;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000804
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100805 GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
806 vma->exec_flags = NULL;
807 eb->vma[i] = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100808
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100809 if (flags & __EXEC_OBJECT_HAS_PIN)
810 __eb_unreserve_vma(vma, flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100811
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100812 if (flags & __EXEC_OBJECT_HAS_REF)
Chris Wilsondade2a62017-06-16 15:05:20 +0100813 i915_vma_put(vma);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000814 }
Chris Wilson2889caa2017-06-16 15:05:19 +0100815}
Chris Wilsond55495b2017-06-15 09:14:34 +0100816
Chris Wilson2889caa2017-06-16 15:05:19 +0100817static void eb_reset_vmas(const struct i915_execbuffer *eb)
818{
819 eb_release_vmas(eb);
Chris Wilson4d470f72017-06-29 16:04:25 +0100820 if (eb->lut_size > 0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100821 memset(eb->buckets, 0,
822 sizeof(struct hlist_head) << eb->lut_size);
823}
Chris Wilsond55495b2017-06-15 09:14:34 +0100824
Chris Wilson2889caa2017-06-16 15:05:19 +0100825static void eb_destroy(const struct i915_execbuffer *eb)
826{
Chris Wilson7dd4f672017-06-16 15:05:24 +0100827 GEM_BUG_ON(eb->reloc_cache.rq);
828
Chris Wilson4d470f72017-06-29 16:04:25 +0100829 if (eb->lut_size > 0)
Chris Wilsond55495b2017-06-15 09:14:34 +0100830 kfree(eb->buckets);
Chris Wilson67731b82010-12-08 10:38:14 +0000831}
832
Chris Wilson2889caa2017-06-16 15:05:19 +0100833static inline u64
Chris Wilsond50415c2016-08-18 17:16:52 +0100834relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +0100835 const struct i915_vma *target)
Michał Winiarski934acce2015-12-29 18:24:52 +0100836{
Chris Wilson2889caa2017-06-16 15:05:19 +0100837 return gen8_canonical_addr((int)reloc->delta + target->node.start);
Michał Winiarski934acce2015-12-29 18:24:52 +0100838}
839
Chris Wilsond50415c2016-08-18 17:16:52 +0100840static void reloc_cache_init(struct reloc_cache *cache,
841 struct drm_i915_private *i915)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100842{
Chris Wilson31a39202016-08-18 17:16:46 +0100843 cache->page = -1;
Chris Wilsond50415c2016-08-18 17:16:52 +0100844 cache->vaddr = 0;
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200845 /* Must be a variable in the struct to allow GCC to unroll. */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100846 cache->gen = INTEL_GEN(i915);
Chris Wilson2889caa2017-06-16 15:05:19 +0100847 cache->has_llc = HAS_LLC(i915);
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200848 cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
Chris Wilson7dd4f672017-06-16 15:05:24 +0100849 cache->has_fence = cache->gen < 4;
850 cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
Chris Wilsone8cb9092016-08-18 17:16:53 +0100851 cache->node.allocated = false;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100852 cache->rq = NULL;
853 cache->rq_size = 0;
Chris Wilson31a39202016-08-18 17:16:46 +0100854}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100855
Chris Wilsond50415c2016-08-18 17:16:52 +0100856static inline void *unmask_page(unsigned long p)
857{
858 return (void *)(uintptr_t)(p & PAGE_MASK);
859}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100860
Chris Wilsond50415c2016-08-18 17:16:52 +0100861static inline unsigned int unmask_flags(unsigned long p)
862{
863 return p & ~PAGE_MASK;
864}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700865
Chris Wilsond50415c2016-08-18 17:16:52 +0100866#define KMAP 0x4 /* after CLFLUSH_FLAGS */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700867
Chris Wilson650bc632017-06-15 09:14:33 +0100868static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
869{
870 struct drm_i915_private *i915 =
871 container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
872 return &i915->ggtt;
873}
874
Chris Wilson7dd4f672017-06-16 15:05:24 +0100875static void reloc_gpu_flush(struct reloc_cache *cache)
876{
877 GEM_BUG_ON(cache->rq_size >= cache->rq->batch->obj->base.size / sizeof(u32));
878 cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END;
879 i915_gem_object_unpin_map(cache->rq->batch->obj);
880 i915_gem_chipset_flush(cache->rq->i915);
881
882 __i915_add_request(cache->rq, true);
883 cache->rq = NULL;
884}
885
Chris Wilson650bc632017-06-15 09:14:33 +0100886static void reloc_cache_reset(struct reloc_cache *cache)
Chris Wilson31a39202016-08-18 17:16:46 +0100887{
Chris Wilsond50415c2016-08-18 17:16:52 +0100888 void *vaddr;
889
Chris Wilson7dd4f672017-06-16 15:05:24 +0100890 if (cache->rq)
891 reloc_gpu_flush(cache);
892
Chris Wilson31a39202016-08-18 17:16:46 +0100893 if (!cache->vaddr)
894 return;
895
Chris Wilsond50415c2016-08-18 17:16:52 +0100896 vaddr = unmask_page(cache->vaddr);
897 if (cache->vaddr & KMAP) {
898 if (cache->vaddr & CLFLUSH_AFTER)
899 mb();
Chris Wilson31a39202016-08-18 17:16:46 +0100900
Chris Wilsond50415c2016-08-18 17:16:52 +0100901 kunmap_atomic(vaddr);
902 i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
903 } else {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100904 wmb();
Chris Wilsond50415c2016-08-18 17:16:52 +0100905 io_mapping_unmap_atomic((void __iomem *)vaddr);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100906 if (cache->node.allocated) {
Chris Wilson650bc632017-06-15 09:14:33 +0100907 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100908
909 ggtt->base.clear_range(&ggtt->base,
910 cache->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200911 cache->node.size);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100912 drm_mm_remove_node(&cache->node);
913 } else {
914 i915_vma_unpin((struct i915_vma *)cache->node.mm);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700915 }
Chris Wilson31a39202016-08-18 17:16:46 +0100916 }
Chris Wilson650bc632017-06-15 09:14:33 +0100917
918 cache->vaddr = 0;
919 cache->page = -1;
Chris Wilson31a39202016-08-18 17:16:46 +0100920}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700921
Chris Wilson31a39202016-08-18 17:16:46 +0100922static void *reloc_kmap(struct drm_i915_gem_object *obj,
923 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100924 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100925{
Chris Wilsond50415c2016-08-18 17:16:52 +0100926 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100927
Chris Wilsond50415c2016-08-18 17:16:52 +0100928 if (cache->vaddr) {
929 kunmap_atomic(unmask_page(cache->vaddr));
930 } else {
931 unsigned int flushes;
Chris Wilson2889caa2017-06-16 15:05:19 +0100932 int err;
Chris Wilson31a39202016-08-18 17:16:46 +0100933
Chris Wilson2889caa2017-06-16 15:05:19 +0100934 err = i915_gem_obj_prepare_shmem_write(obj, &flushes);
935 if (err)
936 return ERR_PTR(err);
Chris Wilsond50415c2016-08-18 17:16:52 +0100937
938 BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
939 BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
940
941 cache->vaddr = flushes | KMAP;
942 cache->node.mm = (void *)obj;
943 if (flushes)
944 mb();
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700945 }
946
Chris Wilsond50415c2016-08-18 17:16:52 +0100947 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
948 cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100949 cache->page = page;
Chris Wilson31a39202016-08-18 17:16:46 +0100950
Chris Wilsond50415c2016-08-18 17:16:52 +0100951 return vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100952}
953
Chris Wilsond50415c2016-08-18 17:16:52 +0100954static void *reloc_iomap(struct drm_i915_gem_object *obj,
Chris Wilson31a39202016-08-18 17:16:46 +0100955 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100956 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100957{
Chris Wilson650bc632017-06-15 09:14:33 +0100958 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100959 unsigned long offset;
Chris Wilsond50415c2016-08-18 17:16:52 +0100960 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100961
Chris Wilsond50415c2016-08-18 17:16:52 +0100962 if (cache->vaddr) {
Jani Nikula615e5002016-10-04 12:54:13 +0300963 io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
Chris Wilsond50415c2016-08-18 17:16:52 +0100964 } else {
965 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +0100966 int err;
Chris Wilson31a39202016-08-18 17:16:46 +0100967
Chris Wilson2889caa2017-06-16 15:05:19 +0100968 if (use_cpu_reloc(cache, obj))
Chris Wilsond50415c2016-08-18 17:16:52 +0100969 return NULL;
Chris Wilson31a39202016-08-18 17:16:46 +0100970
Chris Wilson2889caa2017-06-16 15:05:19 +0100971 err = i915_gem_object_set_to_gtt_domain(obj, true);
972 if (err)
973 return ERR_PTR(err);
Chris Wilson31a39202016-08-18 17:16:46 +0100974
Chris Wilsond50415c2016-08-18 17:16:52 +0100975 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
976 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100977 if (IS_ERR(vma)) {
978 memset(&cache->node, 0, sizeof(cache->node));
Chris Wilson2889caa2017-06-16 15:05:19 +0100979 err = drm_mm_insert_node_in_range
Chris Wilsone8cb9092016-08-18 17:16:53 +0100980 (&ggtt->base.mm, &cache->node,
Chris Wilsonf51455d2017-01-10 14:47:34 +0000981 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
Chris Wilsone8cb9092016-08-18 17:16:53 +0100982 0, ggtt->mappable_end,
Chris Wilson4e64e552017-02-02 21:04:38 +0000983 DRM_MM_INSERT_LOW);
Chris Wilson2889caa2017-06-16 15:05:19 +0100984 if (err) /* no inactive aperture space, use cpu reloc */
Chris Wilsonc92fa4f2016-10-07 07:53:25 +0100985 return NULL;
Chris Wilsone8cb9092016-08-18 17:16:53 +0100986 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +0100987 err = i915_vma_put_fence(vma);
988 if (err) {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100989 i915_vma_unpin(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100990 return ERR_PTR(err);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100991 }
Rafael Barbalho5032d872013-08-21 17:10:51 +0100992
Chris Wilsone8cb9092016-08-18 17:16:53 +0100993 cache->node.start = vma->node.start;
994 cache->node.mm = (void *)vma;
Chris Wilsond50415c2016-08-18 17:16:52 +0100995 }
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700996 }
997
Chris Wilsone8cb9092016-08-18 17:16:53 +0100998 offset = cache->node.start;
999 if (cache->node.allocated) {
Chris Wilsonfc099092016-10-28 15:27:56 +01001000 wmb();
Chris Wilsone8cb9092016-08-18 17:16:53 +01001001 ggtt->base.insert_page(&ggtt->base,
1002 i915_gem_object_get_dma_address(obj, page),
1003 offset, I915_CACHE_NONE, 0);
1004 } else {
1005 offset += page << PAGE_SHIFT;
1006 }
1007
Chris Wilson650bc632017-06-15 09:14:33 +01001008 vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->mappable,
1009 offset);
Chris Wilsond50415c2016-08-18 17:16:52 +01001010 cache->page = page;
1011 cache->vaddr = (unsigned long)vaddr;
1012
1013 return vaddr;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001014}
1015
Chris Wilsond50415c2016-08-18 17:16:52 +01001016static void *reloc_vaddr(struct drm_i915_gem_object *obj,
1017 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001018 unsigned long page)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001019{
Chris Wilsond50415c2016-08-18 17:16:52 +01001020 void *vaddr;
1021
1022 if (cache->page == page) {
1023 vaddr = unmask_page(cache->vaddr);
1024 } else {
1025 vaddr = NULL;
1026 if ((cache->vaddr & KMAP) == 0)
1027 vaddr = reloc_iomap(obj, cache, page);
1028 if (!vaddr)
1029 vaddr = reloc_kmap(obj, cache, page);
1030 }
1031
1032 return vaddr;
1033}
1034
1035static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
1036{
1037 if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
1038 if (flushes & CLFLUSH_BEFORE) {
1039 clflushopt(addr);
1040 mb();
1041 }
1042
1043 *addr = value;
1044
Chris Wilson2889caa2017-06-16 15:05:19 +01001045 /*
1046 * Writes to the same cacheline are serialised by the CPU
Chris Wilsond50415c2016-08-18 17:16:52 +01001047 * (including clflush). On the write path, we only require
1048 * that it hits memory in an orderly fashion and place
1049 * mb barriers at the start and end of the relocation phase
1050 * to ensure ordering of clflush wrt to the system.
1051 */
1052 if (flushes & CLFLUSH_AFTER)
1053 clflushopt(addr);
1054 } else
1055 *addr = value;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001056}
1057
Chris Wilson7dd4f672017-06-16 15:05:24 +01001058static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
1059 struct i915_vma *vma,
1060 unsigned int len)
1061{
1062 struct reloc_cache *cache = &eb->reloc_cache;
1063 struct drm_i915_gem_object *obj;
1064 struct drm_i915_gem_request *rq;
1065 struct i915_vma *batch;
1066 u32 *cmd;
1067 int err;
1068
1069 GEM_BUG_ON(vma->obj->base.write_domain & I915_GEM_DOMAIN_CPU);
1070
1071 obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, PAGE_SIZE);
1072 if (IS_ERR(obj))
1073 return PTR_ERR(obj);
1074
1075 cmd = i915_gem_object_pin_map(obj,
Chris Wilsona575c672017-08-28 11:46:31 +01001076 cache->has_llc ?
1077 I915_MAP_FORCE_WB :
1078 I915_MAP_FORCE_WC);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001079 i915_gem_object_unpin_pages(obj);
1080 if (IS_ERR(cmd))
1081 return PTR_ERR(cmd);
1082
1083 err = i915_gem_object_set_to_wc_domain(obj, false);
1084 if (err)
1085 goto err_unmap;
1086
1087 batch = i915_vma_instance(obj, vma->vm, NULL);
1088 if (IS_ERR(batch)) {
1089 err = PTR_ERR(batch);
1090 goto err_unmap;
1091 }
1092
1093 err = i915_vma_pin(batch, 0, 0, PIN_USER | PIN_NONBLOCK);
1094 if (err)
1095 goto err_unmap;
1096
1097 rq = i915_gem_request_alloc(eb->engine, eb->ctx);
1098 if (IS_ERR(rq)) {
1099 err = PTR_ERR(rq);
1100 goto err_unpin;
1101 }
1102
1103 err = i915_gem_request_await_object(rq, vma->obj, true);
1104 if (err)
1105 goto err_request;
1106
1107 err = eb->engine->emit_flush(rq, EMIT_INVALIDATE);
1108 if (err)
1109 goto err_request;
1110
1111 err = i915_switch_context(rq);
1112 if (err)
1113 goto err_request;
1114
1115 err = eb->engine->emit_bb_start(rq,
1116 batch->node.start, PAGE_SIZE,
1117 cache->gen > 5 ? 0 : I915_DISPATCH_SECURE);
1118 if (err)
1119 goto err_request;
1120
Chris Wilson95ff7c72017-06-16 15:05:25 +01001121 GEM_BUG_ON(!reservation_object_test_signaled_rcu(batch->resv, true));
Chris Wilson7dd4f672017-06-16 15:05:24 +01001122 i915_vma_move_to_active(batch, rq, 0);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001123 reservation_object_lock(batch->resv, NULL);
1124 reservation_object_add_excl_fence(batch->resv, &rq->fence);
1125 reservation_object_unlock(batch->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001126 i915_vma_unpin(batch);
1127
Chris Wilson25ffaa62017-06-20 13:43:20 +01001128 i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001129 reservation_object_lock(vma->resv, NULL);
1130 reservation_object_add_excl_fence(vma->resv, &rq->fence);
1131 reservation_object_unlock(vma->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001132
1133 rq->batch = batch;
1134
1135 cache->rq = rq;
1136 cache->rq_cmd = cmd;
1137 cache->rq_size = 0;
1138
1139 /* Return with batch mapping (cmd) still pinned */
1140 return 0;
1141
1142err_request:
1143 i915_add_request(rq);
1144err_unpin:
1145 i915_vma_unpin(batch);
1146err_unmap:
1147 i915_gem_object_unpin_map(obj);
1148 return err;
1149}
1150
1151static u32 *reloc_gpu(struct i915_execbuffer *eb,
1152 struct i915_vma *vma,
1153 unsigned int len)
1154{
1155 struct reloc_cache *cache = &eb->reloc_cache;
1156 u32 *cmd;
1157
1158 if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1))
1159 reloc_gpu_flush(cache);
1160
1161 if (unlikely(!cache->rq)) {
1162 int err;
1163
Chris Wilson3dbf26e2017-08-26 14:56:20 +01001164 /* If we need to copy for the cmdparser, we will stall anyway */
1165 if (eb_use_cmdparser(eb))
1166 return ERR_PTR(-EWOULDBLOCK);
1167
Chris Wilson90cad092017-09-06 16:28:59 +01001168 if (!intel_engine_can_store_dword(eb->engine))
1169 return ERR_PTR(-ENODEV);
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 Wilson90cad092017-09-06 16:28:59 +01001195 !reservation_object_test_signaled_rcu(vma->resv, true))) {
Chris Wilson7dd4f672017-06-16 15:05:24 +01001196 const unsigned int gen = eb->reloc_cache.gen;
1197 unsigned int len;
1198 u32 *batch;
1199 u64 addr;
1200
1201 if (wide)
1202 len = offset & 7 ? 8 : 5;
1203 else if (gen >= 4)
1204 len = 4;
Chris Wilsonf2f5c062017-08-16 09:52:04 +01001205 else
Chris Wilson7dd4f672017-06-16 15:05:24 +01001206 len = 3;
Chris Wilson7dd4f672017-06-16 15:05:24 +01001207
1208 batch = reloc_gpu(eb, vma, len);
1209 if (IS_ERR(batch))
1210 goto repeat;
1211
1212 addr = gen8_canonical_addr(vma->node.start + offset);
1213 if (wide) {
1214 if (offset & 7) {
1215 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1216 *batch++ = lower_32_bits(addr);
1217 *batch++ = upper_32_bits(addr);
1218 *batch++ = lower_32_bits(target_offset);
1219
1220 addr = gen8_canonical_addr(addr + 4);
1221
1222 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1223 *batch++ = lower_32_bits(addr);
1224 *batch++ = upper_32_bits(addr);
1225 *batch++ = upper_32_bits(target_offset);
1226 } else {
1227 *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1;
1228 *batch++ = lower_32_bits(addr);
1229 *batch++ = upper_32_bits(addr);
1230 *batch++ = lower_32_bits(target_offset);
1231 *batch++ = upper_32_bits(target_offset);
1232 }
1233 } else if (gen >= 6) {
1234 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1235 *batch++ = 0;
1236 *batch++ = addr;
1237 *batch++ = target_offset;
1238 } else if (gen >= 4) {
1239 *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1240 *batch++ = 0;
1241 *batch++ = addr;
1242 *batch++ = target_offset;
1243 } else {
1244 *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
1245 *batch++ = addr;
1246 *batch++ = target_offset;
1247 }
1248
1249 goto out;
1250 }
1251
Chris Wilsond50415c2016-08-18 17:16:52 +01001252repeat:
Chris Wilson95ff7c72017-06-16 15:05:25 +01001253 vaddr = reloc_vaddr(vma->obj, &eb->reloc_cache, offset >> PAGE_SHIFT);
Chris Wilsond50415c2016-08-18 17:16:52 +01001254 if (IS_ERR(vaddr))
1255 return PTR_ERR(vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001256
Chris Wilsond50415c2016-08-18 17:16:52 +01001257 clflush_write32(vaddr + offset_in_page(offset),
1258 lower_32_bits(target_offset),
Chris Wilson2889caa2017-06-16 15:05:19 +01001259 eb->reloc_cache.vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001260
Chris Wilsond50415c2016-08-18 17:16:52 +01001261 if (wide) {
1262 offset += sizeof(u32);
1263 target_offset >>= 32;
1264 wide = false;
1265 goto repeat;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001266 }
Chris Wilsondabdfe02012-03-26 10:10:27 +02001267
Chris Wilson7dd4f672017-06-16 15:05:24 +01001268out:
Chris Wilson2889caa2017-06-16 15:05:19 +01001269 return target->node.start | UPDATE;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001270}
1271
Chris Wilson2889caa2017-06-16 15:05:19 +01001272static u64
1273eb_relocate_entry(struct i915_execbuffer *eb,
1274 struct i915_vma *vma,
1275 const struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001276{
Chris Wilson507d9772017-06-16 15:05:17 +01001277 struct i915_vma *target;
Chris Wilson2889caa2017-06-16 15:05:19 +01001278 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001279
Chris Wilson67731b82010-12-08 10:38:14 +00001280 /* we've already hold a reference to all valid objects */
Chris Wilson507d9772017-06-16 15:05:17 +01001281 target = eb_get_vma(eb, reloc->target_handle);
1282 if (unlikely(!target))
Chris Wilson54cf91d2010-11-25 18:00:26 +00001283 return -ENOENT;
Eric Anholte844b992012-07-31 15:35:01 -07001284
Chris Wilson54cf91d2010-11-25 18:00:26 +00001285 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001286 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001287 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001288 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001289 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001290 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001291 (int) reloc->offset,
1292 reloc->read_domains,
1293 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001294 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001295 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +01001296 if (unlikely((reloc->write_domain | reloc->read_domains)
1297 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001298 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001299 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001300 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001301 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001302 (int) reloc->offset,
1303 reloc->read_domains,
1304 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001305 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001306 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001307
Chris Wilson2889caa2017-06-16 15:05:19 +01001308 if (reloc->write_domain) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001309 *target->exec_flags |= EXEC_OBJECT_WRITE;
Chris Wilson507d9772017-06-16 15:05:17 +01001310
Chris Wilson2889caa2017-06-16 15:05:19 +01001311 /*
1312 * Sandybridge PPGTT errata: We need a global gtt mapping
1313 * for MI and pipe_control writes because the gpu doesn't
1314 * properly redirect them through the ppgtt for non_secure
1315 * batchbuffers.
1316 */
1317 if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
1318 IS_GEN6(eb->i915)) {
1319 err = i915_vma_bind(target, target->obj->cache_level,
1320 PIN_GLOBAL);
1321 if (WARN_ONCE(err,
1322 "Unexpected failure to bind target VMA!"))
1323 return err;
1324 }
Chris Wilson507d9772017-06-16 15:05:17 +01001325 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001326
Chris Wilson2889caa2017-06-16 15:05:19 +01001327 /*
1328 * If the relocation already has the right value in it, no
Chris Wilson54cf91d2010-11-25 18:00:26 +00001329 * more work needs to be done.
1330 */
Chris Wilson7dd4f672017-06-16 15:05:24 +01001331 if (!DBG_FORCE_RELOC &&
1332 gen8_canonical_addr(target->node.start) == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +00001333 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001334
1335 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001336 if (unlikely(reloc->offset >
Chris Wilson507d9772017-06-16 15:05:17 +01001337 vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001338 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson507d9772017-06-16 15:05:17 +01001339 "target %d offset %d size %d.\n",
1340 reloc->target_handle,
1341 (int)reloc->offset,
1342 (int)vma->size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001343 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001344 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001345 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001346 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson507d9772017-06-16 15:05:17 +01001347 "target %d offset %d.\n",
1348 reloc->target_handle,
1349 (int)reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001350 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001351 }
1352
Chris Wilson071750e2017-06-16 15:05:18 +01001353 /*
1354 * If we write into the object, we need to force the synchronisation
1355 * barrier, either with an asynchronous clflush or if we executed the
1356 * patching using the GPU (though that should be serialised by the
1357 * timeline). To be completely sure, and since we are required to
1358 * do relocations we are already stalling, disable the user's opt
Chris Wilson0519bcb2017-08-16 09:52:09 +01001359 * out of our synchronisation.
Chris Wilson071750e2017-06-16 15:05:18 +01001360 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001361 *vma->exec_flags &= ~EXEC_OBJECT_ASYNC;
Chris Wilson071750e2017-06-16 15:05:18 +01001362
Chris Wilson54cf91d2010-11-25 18:00:26 +00001363 /* and update the user's relocation entry */
Chris Wilson2889caa2017-06-16 15:05:19 +01001364 return relocate_entry(vma, reloc, eb, target);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001365}
1366
Chris Wilson2889caa2017-06-16 15:05:19 +01001367static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001368{
Chris Wilson1d83f442012-03-24 20:12:53 +00001369#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
Chris Wilson2889caa2017-06-16 15:05:19 +01001370 struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
1371 struct drm_i915_gem_relocation_entry __user *urelocs;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001372 const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001373 unsigned int remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001374
Chris Wilson2889caa2017-06-16 15:05:19 +01001375 urelocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson1d83f442012-03-24 20:12:53 +00001376 remain = entry->relocation_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01001377 if (unlikely(remain > N_RELOC(ULONG_MAX)))
1378 return -EINVAL;
Chris Wilsonebc08082016-10-18 13:02:51 +01001379
Chris Wilson2889caa2017-06-16 15:05:19 +01001380 /*
1381 * We must check that the entire relocation array is safe
1382 * to read. However, if the array is not writable the user loses
1383 * the updated relocation values.
1384 */
Imre Deakedd90032017-07-14 18:12:42 +03001385 if (unlikely(!access_ok(VERIFY_READ, urelocs, remain*sizeof(*urelocs))))
Chris Wilson2889caa2017-06-16 15:05:19 +01001386 return -EFAULT;
Chris Wilson1d83f442012-03-24 20:12:53 +00001387
Chris Wilson2889caa2017-06-16 15:05:19 +01001388 do {
1389 struct drm_i915_gem_relocation_entry *r = stack;
1390 unsigned int count =
1391 min_t(unsigned int, remain, ARRAY_SIZE(stack));
1392 unsigned int copied;
1393
1394 /*
1395 * This is the fast path and we cannot handle a pagefault
Chris Wilsonebc08082016-10-18 13:02:51 +01001396 * whilst holding the struct mutex lest the user pass in the
1397 * relocations contained within a mmaped bo. For in such a case
1398 * we, the page fault handler would call i915_gem_fault() and
1399 * we would try to acquire the struct mutex again. Obviously
1400 * this is bad and so lockdep complains vehemently.
1401 */
1402 pagefault_disable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001403 copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
Chris Wilsonebc08082016-10-18 13:02:51 +01001404 pagefault_enable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001405 if (unlikely(copied)) {
1406 remain = -EFAULT;
Chris Wilson31a39202016-08-18 17:16:46 +01001407 goto out;
1408 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001409
Chris Wilson2889caa2017-06-16 15:05:19 +01001410 remain -= count;
Chris Wilson1d83f442012-03-24 20:12:53 +00001411 do {
Chris Wilson2889caa2017-06-16 15:05:19 +01001412 u64 offset = eb_relocate_entry(eb, vma, r);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001413
Chris Wilson2889caa2017-06-16 15:05:19 +01001414 if (likely(offset == 0)) {
1415 } else if ((s64)offset < 0) {
1416 remain = (int)offset;
Chris Wilson31a39202016-08-18 17:16:46 +01001417 goto out;
Chris Wilson2889caa2017-06-16 15:05:19 +01001418 } else {
1419 /*
1420 * Note that reporting an error now
1421 * leaves everything in an inconsistent
1422 * state as we have *already* changed
1423 * the relocation value inside the
1424 * object. As we have not changed the
1425 * reloc.presumed_offset or will not
1426 * change the execobject.offset, on the
1427 * call we may not rewrite the value
1428 * inside the object, leaving it
1429 * dangling and causing a GPU hang. Unless
1430 * userspace dynamically rebuilds the
1431 * relocations on each execbuf rather than
1432 * presume a static tree.
1433 *
1434 * We did previously check if the relocations
1435 * were writable (access_ok), an error now
1436 * would be a strange race with mprotect,
1437 * having already demonstrated that we
1438 * can read from this userspace address.
1439 */
1440 offset = gen8_canonical_addr(offset & ~UPDATE);
1441 __put_user(offset,
1442 &urelocs[r-stack].presumed_offset);
Chris Wilson1d83f442012-03-24 20:12:53 +00001443 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001444 } while (r++, --count);
1445 urelocs += ARRAY_SIZE(stack);
1446 } while (remain);
Chris Wilson31a39202016-08-18 17:16:46 +01001447out:
Chris Wilson650bc632017-06-15 09:14:33 +01001448 reloc_cache_reset(&eb->reloc_cache);
Chris Wilson2889caa2017-06-16 15:05:19 +01001449 return remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001450}
1451
1452static int
Chris Wilson2889caa2017-06-16 15:05:19 +01001453eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001454{
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001455 const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001456 struct drm_i915_gem_relocation_entry *relocs =
1457 u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1458 unsigned int i;
1459 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001460
1461 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001462 u64 offset = eb_relocate_entry(eb, vma, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001463
Chris Wilson2889caa2017-06-16 15:05:19 +01001464 if ((s64)offset < 0) {
1465 err = (int)offset;
1466 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001467 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001468 }
1469 err = 0;
Chris Wilsona415d352013-11-26 11:23:15 +00001470err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001471 reloc_cache_reset(&eb->reloc_cache);
1472 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001473}
1474
Chris Wilson2889caa2017-06-16 15:05:19 +01001475static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001476{
Chris Wilson2889caa2017-06-16 15:05:19 +01001477 const char __user *addr, *end;
1478 unsigned long size;
1479 char __maybe_unused c;
Ben Widawsky27173f12013-08-14 11:38:36 +02001480
Chris Wilson2889caa2017-06-16 15:05:19 +01001481 size = entry->relocation_count;
1482 if (size == 0)
1483 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001484
Chris Wilson2889caa2017-06-16 15:05:19 +01001485 if (size > N_RELOC(ULONG_MAX))
1486 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001487
Chris Wilson2889caa2017-06-16 15:05:19 +01001488 addr = u64_to_user_ptr(entry->relocs_ptr);
1489 size *= sizeof(struct drm_i915_gem_relocation_entry);
1490 if (!access_ok(VERIFY_READ, addr, size))
1491 return -EFAULT;
1492
1493 end = addr + size;
1494 for (; addr < end; addr += PAGE_SIZE) {
1495 int err = __get_user(c, addr);
1496 if (err)
1497 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001498 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001499 return __get_user(c, end - 1);
1500}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001501
Chris Wilson2889caa2017-06-16 15:05:19 +01001502static int eb_copy_relocations(const struct i915_execbuffer *eb)
1503{
1504 const unsigned int count = eb->buffer_count;
1505 unsigned int i;
1506 int err;
1507
Chris Wilson54cf91d2010-11-25 18:00:26 +00001508 for (i = 0; i < count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001509 const unsigned int nreloc = eb->exec[i].relocation_count;
1510 struct drm_i915_gem_relocation_entry __user *urelocs;
1511 struct drm_i915_gem_relocation_entry *relocs;
1512 unsigned long size;
1513 unsigned long copied;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001514
Chris Wilson2889caa2017-06-16 15:05:19 +01001515 if (nreloc == 0)
1516 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001517
Chris Wilson2889caa2017-06-16 15:05:19 +01001518 err = check_relocations(&eb->exec[i]);
1519 if (err)
1520 goto err;
1521
1522 urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
1523 size = nreloc * sizeof(*relocs);
1524
1525 relocs = kvmalloc_array(size, 1, GFP_TEMPORARY);
1526 if (!relocs) {
1527 kvfree(relocs);
1528 err = -ENOMEM;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001529 goto err;
1530 }
1531
Chris Wilson2889caa2017-06-16 15:05:19 +01001532 /* copy_from_user is limited to < 4GiB */
1533 copied = 0;
1534 do {
1535 unsigned int len =
1536 min_t(u64, BIT_ULL(31), size - copied);
1537
1538 if (__copy_from_user((char *)relocs + copied,
Ville Syrjälä908a6102017-09-01 19:54:34 +03001539 (char __user *)urelocs + copied,
Chris Wilson2889caa2017-06-16 15:05:19 +01001540 len)) {
1541 kvfree(relocs);
1542 err = -EFAULT;
1543 goto err;
1544 }
1545
1546 copied += len;
1547 } while (copied < size);
1548
1549 /*
1550 * As we do not update the known relocation offsets after
Chris Wilson262b6d32013-01-15 16:17:54 +00001551 * relocating (due to the complexities in lock handling),
1552 * we need to mark them as invalid now so that we force the
1553 * relocation processing next time. Just in case the target
1554 * object is evicted and then rebound into its old
1555 * presumed_offset before the next execbuffer - if that
1556 * happened we would make the mistake of assuming that the
1557 * relocations were valid.
1558 */
Chris Wilson2889caa2017-06-16 15:05:19 +01001559 user_access_begin();
1560 for (copied = 0; copied < nreloc; copied++)
1561 unsafe_put_user(-1,
1562 &urelocs[copied].presumed_offset,
1563 end_user);
1564end_user:
1565 user_access_end();
Chris Wilson262b6d32013-01-15 16:17:54 +00001566
Chris Wilson2889caa2017-06-16 15:05:19 +01001567 eb->exec[i].relocs_ptr = (uintptr_t)relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001568 }
1569
Chris Wilson2889caa2017-06-16 15:05:19 +01001570 return 0;
1571
1572err:
1573 while (i--) {
1574 struct drm_i915_gem_relocation_entry *relocs =
1575 u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr);
1576 if (eb->exec[i].relocation_count)
1577 kvfree(relocs);
1578 }
1579 return err;
1580}
1581
1582static int eb_prefault_relocations(const struct i915_execbuffer *eb)
1583{
1584 const unsigned int count = eb->buffer_count;
1585 unsigned int i;
1586
1587 if (unlikely(i915.prefault_disable))
1588 return 0;
1589
1590 for (i = 0; i < count; i++) {
1591 int err;
1592
1593 err = check_relocations(&eb->exec[i]);
1594 if (err)
1595 return err;
1596 }
1597
1598 return 0;
1599}
1600
1601static noinline int eb_relocate_slow(struct i915_execbuffer *eb)
1602{
1603 struct drm_device *dev = &eb->i915->drm;
1604 bool have_copy = false;
1605 struct i915_vma *vma;
1606 int err = 0;
1607
1608repeat:
1609 if (signal_pending(current)) {
1610 err = -ERESTARTSYS;
1611 goto out;
1612 }
1613
1614 /* We may process another execbuffer during the unlock... */
1615 eb_reset_vmas(eb);
1616 mutex_unlock(&dev->struct_mutex);
1617
1618 /*
1619 * We take 3 passes through the slowpatch.
1620 *
1621 * 1 - we try to just prefault all the user relocation entries and
1622 * then attempt to reuse the atomic pagefault disabled fast path again.
1623 *
1624 * 2 - we copy the user entries to a local buffer here outside of the
1625 * local and allow ourselves to wait upon any rendering before
1626 * relocations
1627 *
1628 * 3 - we already have a local copy of the relocation entries, but
1629 * were interrupted (EAGAIN) whilst waiting for the objects, try again.
1630 */
1631 if (!err) {
1632 err = eb_prefault_relocations(eb);
1633 } else if (!have_copy) {
1634 err = eb_copy_relocations(eb);
1635 have_copy = err == 0;
1636 } else {
1637 cond_resched();
1638 err = 0;
1639 }
1640 if (err) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001641 mutex_lock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01001642 goto out;
1643 }
1644
Chris Wilson8a2421b2017-06-16 15:05:22 +01001645 /* A frequent cause for EAGAIN are currently unavailable client pages */
1646 flush_workqueue(eb->i915->mm.userptr_wq);
1647
Chris Wilson2889caa2017-06-16 15:05:19 +01001648 err = i915_mutex_lock_interruptible(dev);
1649 if (err) {
1650 mutex_lock(&dev->struct_mutex);
1651 goto out;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001652 }
1653
Chris Wilson67731b82010-12-08 10:38:14 +00001654 /* reacquire the objects */
Chris Wilson2889caa2017-06-16 15:05:19 +01001655 err = eb_lookup_vmas(eb);
1656 if (err)
Chris Wilson3b96eff2013-01-08 10:53:14 +00001657 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +00001658
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001659 GEM_BUG_ON(!eb->batch);
1660
Chris Wilson2889caa2017-06-16 15:05:19 +01001661 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1662 if (!have_copy) {
1663 pagefault_disable();
1664 err = eb_relocate_vma(eb, vma);
1665 pagefault_enable();
1666 if (err)
1667 goto repeat;
1668 } else {
1669 err = eb_relocate_vma_slow(eb, vma);
1670 if (err)
1671 goto err;
1672 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001673 }
1674
Chris Wilson2889caa2017-06-16 15:05:19 +01001675 /*
1676 * Leave the user relocations as are, this is the painfully slow path,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001677 * and we want to avoid the complication of dropping the lock whilst
1678 * having buffers reserved in the aperture and so causing spurious
1679 * ENOSPC for random operations.
1680 */
1681
1682err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001683 if (err == -EAGAIN)
1684 goto repeat;
1685
1686out:
1687 if (have_copy) {
1688 const unsigned int count = eb->buffer_count;
1689 unsigned int i;
1690
1691 for (i = 0; i < count; i++) {
1692 const struct drm_i915_gem_exec_object2 *entry =
1693 &eb->exec[i];
1694 struct drm_i915_gem_relocation_entry *relocs;
1695
1696 if (!entry->relocation_count)
1697 continue;
1698
1699 relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1700 kvfree(relocs);
1701 }
1702 }
1703
Chris Wilson1f727d92017-07-21 15:50:36 +01001704 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001705}
1706
Chris Wilson2889caa2017-06-16 15:05:19 +01001707static int eb_relocate(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001708{
Chris Wilson2889caa2017-06-16 15:05:19 +01001709 if (eb_lookup_vmas(eb))
1710 goto slow;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001711
Chris Wilson2889caa2017-06-16 15:05:19 +01001712 /* The objects are in their final locations, apply the relocations. */
1713 if (eb->args->flags & __EXEC_HAS_RELOC) {
1714 struct i915_vma *vma;
1715
1716 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1717 if (eb_relocate_vma(eb, vma))
1718 goto slow;
1719 }
1720 }
1721
1722 return 0;
1723
1724slow:
1725 return eb_relocate_slow(eb);
1726}
1727
Chris Wilson95ff7c72017-06-16 15:05:25 +01001728static void eb_export_fence(struct i915_vma *vma,
Chris Wilson2889caa2017-06-16 15:05:19 +01001729 struct drm_i915_gem_request *req,
1730 unsigned int flags)
1731{
Chris Wilson95ff7c72017-06-16 15:05:25 +01001732 struct reservation_object *resv = vma->resv;
Chris Wilson2889caa2017-06-16 15:05:19 +01001733
1734 /*
1735 * Ignore errors from failing to allocate the new fence, we can't
1736 * handle an error right now. Worst case should be missed
1737 * synchronisation leading to rendering corruption.
1738 */
1739 reservation_object_lock(resv, NULL);
1740 if (flags & EXEC_OBJECT_WRITE)
1741 reservation_object_add_excl_fence(resv, &req->fence);
1742 else if (reservation_object_reserve_shared(resv) == 0)
1743 reservation_object_add_shared_fence(resv, &req->fence);
1744 reservation_object_unlock(resv);
1745}
1746
1747static int eb_move_to_gpu(struct i915_execbuffer *eb)
1748{
1749 const unsigned int count = eb->buffer_count;
1750 unsigned int i;
1751 int err;
1752
1753 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001754 unsigned int flags = eb->flags[i];
1755 struct i915_vma *vma = eb->vma[i];
Ben Widawsky27173f12013-08-14 11:38:36 +02001756 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001757
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001758 if (flags & EXEC_OBJECT_CAPTURE) {
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001759 struct i915_gem_capture_list *capture;
1760
1761 capture = kmalloc(sizeof(*capture), GFP_KERNEL);
1762 if (unlikely(!capture))
1763 return -ENOMEM;
1764
Chris Wilson650bc632017-06-15 09:14:33 +01001765 capture->next = eb->request->capture_list;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001766 capture->vma = eb->vma[i];
Chris Wilson650bc632017-06-15 09:14:33 +01001767 eb->request->capture_list = capture;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001768 }
1769
Chris Wilsonb8f55be2017-08-11 12:11:16 +01001770 /*
1771 * If the GPU is not _reading_ through the CPU cache, we need
1772 * to make sure that any writes (both previous GPU writes from
1773 * before a change in snooping levels and normal CPU writes)
1774 * caught in that cache are flushed to main memory.
1775 *
1776 * We want to say
1777 * obj->cache_dirty &&
1778 * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
1779 * but gcc's optimiser doesn't handle that as well and emits
1780 * two jumps instead of one. Maybe one day...
1781 */
1782 if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) {
Chris Wilson0f46daa2017-07-21 15:50:37 +01001783 if (i915_gem_clflush_object(obj, 0))
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001784 flags &= ~EXEC_OBJECT_ASYNC;
Chris Wilson0f46daa2017-07-21 15:50:37 +01001785 }
1786
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001787 if (flags & EXEC_OBJECT_ASYNC)
1788 continue;
Chris Wilson77ae9952017-01-27 09:40:07 +00001789
Chris Wilson2889caa2017-06-16 15:05:19 +01001790 err = i915_gem_request_await_object
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001791 (eb->request, obj, flags & EXEC_OBJECT_WRITE);
Chris Wilson2889caa2017-06-16 15:05:19 +01001792 if (err)
1793 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001794 }
1795
Chris Wilson2889caa2017-06-16 15:05:19 +01001796 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001797 unsigned int flags = eb->flags[i];
1798 struct i915_vma *vma = eb->vma[i];
Chris Wilson2889caa2017-06-16 15:05:19 +01001799
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001800 i915_vma_move_to_active(vma, eb->request, flags);
1801 eb_export_fence(vma, eb->request, flags);
1802
1803 __eb_unreserve_vma(vma, flags);
1804 vma->exec_flags = NULL;
1805
1806 if (unlikely(flags & __EXEC_OBJECT_HAS_REF))
Chris Wilsondade2a62017-06-16 15:05:20 +01001807 i915_vma_put(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001808 }
1809 eb->exec = NULL;
1810
Chris Wilsondcd79932016-08-18 17:16:40 +01001811 /* Unconditionally flush any chipset caches (for streaming writes). */
Chris Wilson650bc632017-06-15 09:14:33 +01001812 i915_gem_chipset_flush(eb->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001813
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001814 /* Unconditionally invalidate GPU caches and TLBs. */
Chris Wilson650bc632017-06-15 09:14:33 +01001815 return eb->engine->emit_flush(eb->request, EMIT_INVALIDATE);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001816}
1817
Chris Wilson2889caa2017-06-16 15:05:19 +01001818static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001819{
Chris Wilson650bc632017-06-15 09:14:33 +01001820 if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
Daniel Vettered5982e2013-01-17 22:23:36 +01001821 return false;
1822
Chris Wilson2f5945b2015-10-06 11:39:55 +01001823 /* Kernel clipping was a DRI1 misfeature */
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01001824 if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
1825 if (exec->num_cliprects || exec->cliprects_ptr)
1826 return false;
1827 }
Chris Wilson2f5945b2015-10-06 11:39:55 +01001828
1829 if (exec->DR4 == 0xffffffff) {
1830 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1831 exec->DR4 = 0;
1832 }
1833 if (exec->DR1 || exec->DR4)
1834 return false;
1835
1836 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1837 return false;
1838
1839 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001840}
1841
Chris Wilson5cf3d282016-08-04 07:52:43 +01001842void i915_vma_move_to_active(struct i915_vma *vma,
1843 struct drm_i915_gem_request *req,
1844 unsigned int flags)
1845{
1846 struct drm_i915_gem_object *obj = vma->obj;
1847 const unsigned int idx = req->engine->id;
1848
Chris Wilson81147b02016-12-18 15:37:18 +00001849 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001850 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1851
Chris Wilson2889caa2017-06-16 15:05:19 +01001852 /*
1853 * Add a reference if we're newly entering the active list.
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001854 * The order in which we add operations to the retirement queue is
1855 * vital here: mark_active adds to the start of the callback list,
1856 * such that subsequent callbacks are called first. Therefore we
1857 * add the active reference first and queue for it to be dropped
1858 * *last*.
1859 */
Chris Wilsond07f0e52016-10-28 13:58:44 +01001860 if (!i915_vma_is_active(vma))
1861 obj->active_count++;
1862 i915_vma_set_active(vma, idx);
1863 i915_gem_active_set(&vma->last_read[idx], req);
1864 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001865
Chris Wilsone27ab732017-06-15 13:38:49 +01001866 obj->base.write_domain = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001867 if (flags & EXEC_OBJECT_WRITE) {
Chris Wilsone27ab732017-06-15 13:38:49 +01001868 obj->base.write_domain = I915_GEM_DOMAIN_RENDER;
1869
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00001870 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
1871 i915_gem_active_set(&obj->frontbuffer_write, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001872
Chris Wilsone27ab732017-06-15 13:38:49 +01001873 obj->base.read_domains = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001874 }
Chris Wilsone27ab732017-06-15 13:38:49 +01001875 obj->base.read_domains |= I915_GEM_GPU_DOMAINS;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001876
Chris Wilson49ef5292016-08-18 17:17:00 +01001877 if (flags & EXEC_OBJECT_NEEDS_FENCE)
1878 i915_gem_active_set(&vma->last_fence, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001879}
1880
Chris Wilson2889caa2017-06-16 15:05:19 +01001881static int i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001882{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001883 u32 *cs;
1884 int i;
Eric Anholtae662d32012-01-03 09:23:29 -08001885
Chris Wilsonb5321f32016-08-02 22:50:18 +01001886 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001887 DRM_DEBUG("sol reset is gen7/rcs only\n");
1888 return -EINVAL;
1889 }
Eric Anholtae662d32012-01-03 09:23:29 -08001890
Chris Wilson2889caa2017-06-16 15:05:19 +01001891 cs = intel_ring_begin(req, 4 * 2 + 2);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001892 if (IS_ERR(cs))
1893 return PTR_ERR(cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001894
Chris Wilson2889caa2017-06-16 15:05:19 +01001895 *cs++ = MI_LOAD_REGISTER_IMM(4);
Eric Anholtae662d32012-01-03 09:23:29 -08001896 for (i = 0; i < 4; i++) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001897 *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
1898 *cs++ = 0;
Eric Anholtae662d32012-01-03 09:23:29 -08001899 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001900 *cs++ = MI_NOOP;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001901 intel_ring_advance(req, cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001902
1903 return 0;
1904}
1905
Chris Wilson650bc632017-06-15 09:14:33 +01001906static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001907{
Brad Volkin71745372014-12-11 12:13:12 -08001908 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001909 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001910 int err;
Brad Volkin71745372014-12-11 12:13:12 -08001911
Chris Wilson650bc632017-06-15 09:14:33 +01001912 shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool,
1913 PAGE_ALIGN(eb->batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001914 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001915 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001916
Chris Wilson2889caa2017-06-16 15:05:19 +01001917 err = intel_engine_cmd_parser(eb->engine,
Chris Wilson650bc632017-06-15 09:14:33 +01001918 eb->batch->obj,
Chris Wilson33a051a2016-07-27 09:07:26 +01001919 shadow_batch_obj,
Chris Wilson650bc632017-06-15 09:14:33 +01001920 eb->batch_start_offset,
1921 eb->batch_len,
Chris Wilson33a051a2016-07-27 09:07:26 +01001922 is_master);
Chris Wilson2889caa2017-06-16 15:05:19 +01001923 if (err) {
1924 if (err == -EACCES) /* unhandled chained batch */
Chris Wilson058d88c2016-08-15 10:49:06 +01001925 vma = NULL;
1926 else
Chris Wilson2889caa2017-06-16 15:05:19 +01001927 vma = ERR_PTR(err);
Chris Wilson058d88c2016-08-15 10:49:06 +01001928 goto out;
1929 }
Brad Volkin71745372014-12-11 12:13:12 -08001930
Chris Wilson058d88c2016-08-15 10:49:06 +01001931 vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
1932 if (IS_ERR(vma))
1933 goto out;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001934
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001935 eb->vma[eb->buffer_count] = i915_vma_get(vma);
1936 eb->flags[eb->buffer_count] =
1937 __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF;
1938 vma->exec_flags = &eb->flags[eb->buffer_count];
1939 eb->buffer_count++;
Brad Volkin71745372014-12-11 12:13:12 -08001940
Chris Wilson058d88c2016-08-15 10:49:06 +01001941out:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001942 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson058d88c2016-08-15 10:49:06 +01001943 return vma;
Brad Volkin71745372014-12-11 12:13:12 -08001944}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001945
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001946static void
Chris Wilson2889caa2017-06-16 15:05:19 +01001947add_to_client(struct drm_i915_gem_request *req, struct drm_file *file)
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001948{
1949 req->file_priv = file->driver_priv;
1950 list_add_tail(&req->client_link, &req->file_priv->mm.request_list);
1951}
1952
Chris Wilson2889caa2017-06-16 15:05:19 +01001953static int eb_submit(struct i915_execbuffer *eb)
Oscar Mateo78382592014-07-03 16:28:05 +01001954{
Chris Wilson2889caa2017-06-16 15:05:19 +01001955 int err;
Oscar Mateo78382592014-07-03 16:28:05 +01001956
Chris Wilson2889caa2017-06-16 15:05:19 +01001957 err = eb_move_to_gpu(eb);
1958 if (err)
1959 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001960
Chris Wilson2889caa2017-06-16 15:05:19 +01001961 err = i915_switch_context(eb->request);
1962 if (err)
1963 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001964
Chris Wilson650bc632017-06-15 09:14:33 +01001965 if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001966 err = i915_reset_gen7_sol_offsets(eb->request);
1967 if (err)
1968 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001969 }
1970
Chris Wilson2889caa2017-06-16 15:05:19 +01001971 err = eb->engine->emit_bb_start(eb->request,
Chris Wilson650bc632017-06-15 09:14:33 +01001972 eb->batch->node.start +
1973 eb->batch_start_offset,
1974 eb->batch_len,
Chris Wilson2889caa2017-06-16 15:05:19 +01001975 eb->batch_flags);
1976 if (err)
1977 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001978
Chris Wilson2f5945b2015-10-06 11:39:55 +01001979 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001980}
1981
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001982/**
1983 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01001984 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001985 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001986static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01001987gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
1988 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001989{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001990 struct drm_i915_file_private *file_priv = file->driver_priv;
1991
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001992 /* Check whether the file_priv has already selected one ring. */
Joonas Lahtinen6f633402016-09-01 14:58:21 +03001993 if ((int)file_priv->bsd_engine < 0)
1994 file_priv->bsd_engine = atomic_fetch_xor(1,
1995 &dev_priv->mm.bsd_engine_dispatch_index);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001996
Chris Wilsonc80ff162016-07-27 09:07:27 +01001997 return file_priv->bsd_engine;
Chris Wilsond23db882014-05-23 08:48:08 +02001998}
1999
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002000#define I915_USER_RINGS (4)
2001
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002002static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002003 [I915_EXEC_DEFAULT] = RCS,
2004 [I915_EXEC_RENDER] = RCS,
2005 [I915_EXEC_BLT] = BCS,
2006 [I915_EXEC_BSD] = VCS,
2007 [I915_EXEC_VEBOX] = VECS
2008};
2009
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002010static struct intel_engine_cs *
2011eb_select_engine(struct drm_i915_private *dev_priv,
2012 struct drm_file *file,
2013 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002014{
2015 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002016 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002017
2018 if (user_ring_id > I915_USER_RINGS) {
2019 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002020 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002021 }
2022
2023 if ((user_ring_id != I915_EXEC_BSD) &&
2024 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
2025 DRM_DEBUG("execbuf with non bsd ring but with invalid "
2026 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002027 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002028 }
2029
2030 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
2031 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
2032
2033 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01002034 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002035 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
2036 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00002037 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002038 bsd_idx--;
2039 } else {
2040 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
2041 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002042 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002043 }
2044
Akash Goel3b3f1652016-10-13 22:44:48 +05302045 engine = dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002046 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +05302047 engine = dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002048 }
2049
Akash Goel3b3f1652016-10-13 22:44:48 +05302050 if (!engine) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002051 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002052 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002053 }
2054
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002055 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002056}
2057
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002058static void
2059__free_fence_array(struct drm_syncobj **fences, unsigned int n)
2060{
2061 while (n--)
2062 drm_syncobj_put(ptr_mask_bits(fences[n], 2));
2063 kvfree(fences);
2064}
2065
2066static struct drm_syncobj **
2067get_fence_array(struct drm_i915_gem_execbuffer2 *args,
2068 struct drm_file *file)
2069{
2070 const unsigned int nfences = args->num_cliprects;
2071 struct drm_i915_gem_exec_fence __user *user;
2072 struct drm_syncobj **fences;
2073 unsigned int n;
2074 int err;
2075
2076 if (!(args->flags & I915_EXEC_FENCE_ARRAY))
2077 return NULL;
2078
2079 if (nfences > SIZE_MAX / sizeof(*fences))
2080 return ERR_PTR(-EINVAL);
2081
2082 user = u64_to_user_ptr(args->cliprects_ptr);
2083 if (!access_ok(VERIFY_READ, user, nfences * 2 * sizeof(u32)))
2084 return ERR_PTR(-EFAULT);
2085
2086 fences = kvmalloc_array(args->num_cliprects, sizeof(*fences),
2087 __GFP_NOWARN | GFP_TEMPORARY);
2088 if (!fences)
2089 return ERR_PTR(-ENOMEM);
2090
2091 for (n = 0; n < nfences; n++) {
2092 struct drm_i915_gem_exec_fence fence;
2093 struct drm_syncobj *syncobj;
2094
2095 if (__copy_from_user(&fence, user++, sizeof(fence))) {
2096 err = -EFAULT;
2097 goto err;
2098 }
2099
2100 syncobj = drm_syncobj_find(file, fence.handle);
2101 if (!syncobj) {
2102 DRM_DEBUG("Invalid syncobj handle provided\n");
2103 err = -ENOENT;
2104 goto err;
2105 }
2106
2107 fences[n] = ptr_pack_bits(syncobj, fence.flags, 2);
2108 }
2109
2110 return fences;
2111
2112err:
2113 __free_fence_array(fences, n);
2114 return ERR_PTR(err);
2115}
2116
2117static void
2118put_fence_array(struct drm_i915_gem_execbuffer2 *args,
2119 struct drm_syncobj **fences)
2120{
2121 if (fences)
2122 __free_fence_array(fences, args->num_cliprects);
2123}
2124
2125static int
2126await_fence_array(struct i915_execbuffer *eb,
2127 struct drm_syncobj **fences)
2128{
2129 const unsigned int nfences = eb->args->num_cliprects;
2130 unsigned int n;
2131 int err;
2132
2133 for (n = 0; n < nfences; n++) {
2134 struct drm_syncobj *syncobj;
2135 struct dma_fence *fence;
2136 unsigned int flags;
2137
2138 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2139 if (!(flags & I915_EXEC_FENCE_WAIT))
2140 continue;
2141
Jason Ekstrandafca4212017-08-25 10:52:21 -07002142 fence = drm_syncobj_fence_get(syncobj);
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002143 if (!fence)
2144 return -EINVAL;
2145
2146 err = i915_gem_request_await_dma_fence(eb->request, fence);
2147 dma_fence_put(fence);
2148 if (err < 0)
2149 return err;
2150 }
2151
2152 return 0;
2153}
2154
2155static void
2156signal_fence_array(struct i915_execbuffer *eb,
2157 struct drm_syncobj **fences)
2158{
2159 const unsigned int nfences = eb->args->num_cliprects;
2160 struct dma_fence * const fence = &eb->request->fence;
2161 unsigned int n;
2162
2163 for (n = 0; n < nfences; n++) {
2164 struct drm_syncobj *syncobj;
2165 unsigned int flags;
2166
2167 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2168 if (!(flags & I915_EXEC_FENCE_SIGNAL))
2169 continue;
2170
2171 drm_syncobj_replace_fence(syncobj, fence);
2172 }
2173}
2174
Eric Anholtae662d32012-01-03 09:23:29 -08002175static int
Chris Wilson650bc632017-06-15 09:14:33 +01002176i915_gem_do_execbuffer(struct drm_device *dev,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002177 struct drm_file *file,
2178 struct drm_i915_gem_execbuffer2 *args,
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002179 struct drm_i915_gem_exec_object2 *exec,
2180 struct drm_syncobj **fences)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002181{
Chris Wilson650bc632017-06-15 09:14:33 +01002182 struct i915_execbuffer eb;
Chris Wilsonfec04452017-01-27 09:40:08 +00002183 struct dma_fence *in_fence = NULL;
2184 struct sync_file *out_fence = NULL;
2185 int out_fence_fd = -1;
Chris Wilson2889caa2017-06-16 15:05:19 +01002186 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002187
Chris Wilson2889caa2017-06-16 15:05:19 +01002188 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
2189 ~__EXEC_OBJECT_UNKNOWN_FLAGS);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002190
Chris Wilson650bc632017-06-15 09:14:33 +01002191 eb.i915 = to_i915(dev);
2192 eb.file = file;
2193 eb.args = args;
Chris Wilson7dd4f672017-06-16 15:05:24 +01002194 if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC))
Chris Wilson2889caa2017-06-16 15:05:19 +01002195 args->flags |= __EXEC_HAS_RELOC;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002196
Chris Wilson650bc632017-06-15 09:14:33 +01002197 eb.exec = exec;
Chris Wilson170fa292017-08-16 09:52:07 +01002198 eb.vma = (struct i915_vma **)(exec + args->buffer_count + 1);
2199 eb.vma[0] = NULL;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002200 eb.flags = (unsigned int *)(eb.vma + args->buffer_count + 1);
2201
Chris Wilson2889caa2017-06-16 15:05:19 +01002202 eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
2203 if (USES_FULL_PPGTT(eb.i915))
2204 eb.invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson650bc632017-06-15 09:14:33 +01002205 reloc_cache_init(&eb.reloc_cache, eb.i915);
2206
Chris Wilson2889caa2017-06-16 15:05:19 +01002207 eb.buffer_count = args->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +01002208 eb.batch_start_offset = args->batch_start_offset;
2209 eb.batch_len = args->batch_len;
2210
Chris Wilson2889caa2017-06-16 15:05:19 +01002211 eb.batch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002212 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02002213 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002214 return -EPERM;
2215
Chris Wilson2889caa2017-06-16 15:05:19 +01002216 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002217 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01002218 if (args->flags & I915_EXEC_IS_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +01002219 eb.batch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002220
Chris Wilson650bc632017-06-15 09:14:33 +01002221 eb.engine = eb_select_engine(eb.i915, file, args);
2222 if (!eb.engine)
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002223 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002224
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002225 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
Chris Wilson650bc632017-06-15 09:14:33 +01002226 if (!HAS_RESOURCE_STREAMER(eb.i915)) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002227 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
2228 return -EINVAL;
2229 }
Chris Wilson650bc632017-06-15 09:14:33 +01002230 if (eb.engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002231 DRM_DEBUG("RS is not available on %s\n",
Chris Wilson650bc632017-06-15 09:14:33 +01002232 eb.engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002233 return -EINVAL;
2234 }
2235
Chris Wilson2889caa2017-06-16 15:05:19 +01002236 eb.batch_flags |= I915_DISPATCH_RS;
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002237 }
2238
Chris Wilsonfec04452017-01-27 09:40:08 +00002239 if (args->flags & I915_EXEC_FENCE_IN) {
2240 in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002241 if (!in_fence)
2242 return -EINVAL;
Chris Wilsonfec04452017-01-27 09:40:08 +00002243 }
2244
2245 if (args->flags & I915_EXEC_FENCE_OUT) {
2246 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
2247 if (out_fence_fd < 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002248 err = out_fence_fd;
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002249 goto err_in_fence;
Chris Wilsonfec04452017-01-27 09:40:08 +00002250 }
2251 }
2252
Chris Wilson4d470f72017-06-29 16:04:25 +01002253 err = eb_create(&eb);
2254 if (err)
2255 goto err_out_fence;
2256
2257 GEM_BUG_ON(!eb.lut_size);
Chris Wilson2889caa2017-06-16 15:05:19 +01002258
Chris Wilson1acfc102017-06-20 12:05:47 +01002259 err = eb_select_context(&eb);
2260 if (unlikely(err))
2261 goto err_destroy;
2262
Chris Wilson2889caa2017-06-16 15:05:19 +01002263 /*
2264 * Take a local wakeref for preparing to dispatch the execbuf as
Chris Wilson67d97da2016-07-04 08:08:31 +01002265 * we expect to access the hardware fairly frequently in the
2266 * process. Upon first dispatch, we acquire another prolonged
2267 * wakeref that we hold until the GPU has been idle for at least
2268 * 100ms.
2269 */
Chris Wilson650bc632017-06-15 09:14:33 +01002270 intel_runtime_pm_get(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002271
Chris Wilson2889caa2017-06-16 15:05:19 +01002272 err = i915_mutex_lock_interruptible(dev);
2273 if (err)
2274 goto err_rpm;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002275
Chris Wilson2889caa2017-06-16 15:05:19 +01002276 err = eb_relocate(&eb);
Chris Wilson1f727d92017-07-21 15:50:36 +01002277 if (err) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002278 /*
2279 * If the user expects the execobject.offset and
2280 * reloc.presumed_offset to be an exact match,
2281 * as for using NO_RELOC, then we cannot update
2282 * the execobject.offset until we have completed
2283 * relocation.
2284 */
2285 args->flags &= ~__EXEC_HAS_RELOC;
Chris Wilson2889caa2017-06-16 15:05:19 +01002286 goto err_vma;
Chris Wilson1f727d92017-07-21 15:50:36 +01002287 }
Ben Widawsky41bde552013-12-06 14:11:21 -08002288
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002289 if (unlikely(*eb.batch->exec_flags & EXEC_OBJECT_WRITE)) {
Daniel Vetterff240192012-01-31 21:08:14 +01002290 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002291 err = -EINVAL;
2292 goto err_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002293 }
Chris Wilson650bc632017-06-15 09:14:33 +01002294 if (eb.batch_start_offset > eb.batch->size ||
2295 eb.batch_len > eb.batch->size - eb.batch_start_offset) {
Chris Wilson0b537272016-08-18 17:17:12 +01002296 DRM_DEBUG("Attempting to use out-of-bounds batch\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002297 err = -EINVAL;
2298 goto err_vma;
Chris Wilson0b537272016-08-18 17:17:12 +01002299 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002300
Chris Wilson3dbf26e2017-08-26 14:56:20 +01002301 if (eb_use_cmdparser(&eb)) {
Chris Wilson59bfa122016-08-04 16:32:31 +01002302 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002303
Chris Wilson650bc632017-06-15 09:14:33 +01002304 vma = eb_parse(&eb, drm_is_current_master(file));
Chris Wilson59bfa122016-08-04 16:32:31 +01002305 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002306 err = PTR_ERR(vma);
2307 goto err_vma;
Brad Volkin78a42372014-12-11 12:13:09 -08002308 }
Chris Wilson17cabf52015-01-14 11:20:57 +00002309
Chris Wilson59bfa122016-08-04 16:32:31 +01002310 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002311 /*
2312 * Batch parsed and accepted:
2313 *
2314 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
2315 * bit from MI_BATCH_BUFFER_START commands issued in
2316 * the dispatch_execbuffer implementations. We
2317 * specifically don't want that set on batches the
2318 * command parser has accepted.
2319 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002320 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilson650bc632017-06-15 09:14:33 +01002321 eb.batch_start_offset = 0;
2322 eb.batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002323 }
Brad Volkin351e3db2014-02-18 10:15:46 -08002324 }
2325
Chris Wilson650bc632017-06-15 09:14:33 +01002326 if (eb.batch_len == 0)
2327 eb.batch_len = eb.batch->size - eb.batch_start_offset;
Brad Volkin78a42372014-12-11 12:13:09 -08002328
Chris Wilson2889caa2017-06-16 15:05:19 +01002329 /*
2330 * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002331 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07002332 * hsw should have this fixed, but bdw mucks it up again. */
Chris Wilson2889caa2017-06-16 15:05:19 +01002333 if (eb.batch_flags & I915_DISPATCH_SECURE) {
Chris Wilson058d88c2016-08-15 10:49:06 +01002334 struct i915_vma *vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002335
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002336 /*
2337 * So on first glance it looks freaky that we pin the batch here
2338 * outside of the reservation loop. But:
2339 * - The batch is already pinned into the relevant ppgtt, so we
2340 * already have the backing storage fully allocated.
2341 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01002342 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002343 * fitting due to fragmentation.
2344 * So this is actually safe.
2345 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002346 vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0);
Chris Wilson058d88c2016-08-15 10:49:06 +01002347 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002348 err = PTR_ERR(vma);
2349 goto err_vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01002350 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002351
Chris Wilson650bc632017-06-15 09:14:33 +01002352 eb.batch = vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002353 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002354
Chris Wilson7dd4f672017-06-16 15:05:24 +01002355 /* All GPU relocation batches must be submitted prior to the user rq */
2356 GEM_BUG_ON(eb.reloc_cache.rq);
2357
John Harrison0c8dac82015-05-29 17:43:25 +01002358 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson650bc632017-06-15 09:14:33 +01002359 eb.request = i915_gem_request_alloc(eb.engine, eb.ctx);
2360 if (IS_ERR(eb.request)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002361 err = PTR_ERR(eb.request);
John Harrison0c8dac82015-05-29 17:43:25 +01002362 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00002363 }
John Harrison0c8dac82015-05-29 17:43:25 +01002364
Chris Wilsonfec04452017-01-27 09:40:08 +00002365 if (in_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002366 err = i915_gem_request_await_dma_fence(eb.request, in_fence);
2367 if (err < 0)
Chris Wilsonfec04452017-01-27 09:40:08 +00002368 goto err_request;
2369 }
2370
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002371 if (fences) {
2372 err = await_fence_array(&eb, fences);
2373 if (err)
2374 goto err_request;
2375 }
2376
Chris Wilsonfec04452017-01-27 09:40:08 +00002377 if (out_fence_fd != -1) {
Chris Wilson650bc632017-06-15 09:14:33 +01002378 out_fence = sync_file_create(&eb.request->fence);
Chris Wilsonfec04452017-01-27 09:40:08 +00002379 if (!out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002380 err = -ENOMEM;
Chris Wilsonfec04452017-01-27 09:40:08 +00002381 goto err_request;
2382 }
2383 }
2384
Chris Wilson2889caa2017-06-16 15:05:19 +01002385 /*
2386 * Whilst this request exists, batch_obj will be on the
Chris Wilson17f298cf2016-08-10 13:41:46 +01002387 * active_list, and so will hold the active reference. Only when this
2388 * request is retired will the the batch_obj be moved onto the
2389 * inactive_list and lose its active reference. Hence we do not need
2390 * to explicitly hold another reference here.
2391 */
Chris Wilson650bc632017-06-15 09:14:33 +01002392 eb.request->batch = eb.batch;
Chris Wilson17f298cf2016-08-10 13:41:46 +01002393
Chris Wilson2889caa2017-06-16 15:05:19 +01002394 trace_i915_gem_request_queue(eb.request, eb.batch_flags);
2395 err = eb_submit(&eb);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01002396err_request:
Chris Wilson2889caa2017-06-16 15:05:19 +01002397 __i915_add_request(eb.request, err == 0);
Chris Wilson650bc632017-06-15 09:14:33 +01002398 add_to_client(eb.request, file);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002399
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002400 if (fences)
2401 signal_fence_array(&eb, fences);
2402
Chris Wilsonfec04452017-01-27 09:40:08 +00002403 if (out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002404 if (err == 0) {
Chris Wilsonfec04452017-01-27 09:40:08 +00002405 fd_install(out_fence_fd, out_fence->file);
2406 args->rsvd2 &= GENMASK_ULL(0, 31); /* keep in-fence */
2407 args->rsvd2 |= (u64)out_fence_fd << 32;
2408 out_fence_fd = -1;
2409 } else {
2410 fput(out_fence->file);
2411 }
2412 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002413
John Harrison0c8dac82015-05-29 17:43:25 +01002414err_batch_unpin:
Chris Wilson2889caa2017-06-16 15:05:19 +01002415 if (eb.batch_flags & I915_DISPATCH_SECURE)
Chris Wilson650bc632017-06-15 09:14:33 +01002416 i915_vma_unpin(eb.batch);
Chris Wilson2889caa2017-06-16 15:05:19 +01002417err_vma:
2418 if (eb.exec)
2419 eb_release_vmas(&eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002420 mutex_unlock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01002421err_rpm:
Chris Wilson650bc632017-06-15 09:14:33 +01002422 intel_runtime_pm_put(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002423 i915_gem_context_put(eb.ctx);
2424err_destroy:
Chris Wilson2889caa2017-06-16 15:05:19 +01002425 eb_destroy(&eb);
Chris Wilson4d470f72017-06-29 16:04:25 +01002426err_out_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002427 if (out_fence_fd != -1)
2428 put_unused_fd(out_fence_fd);
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002429err_in_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002430 dma_fence_put(in_fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01002431 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002432}
2433
2434/*
2435 * Legacy execbuffer just creates an exec2 list from the original exec object
2436 * list array and passes it to the real function.
2437 */
2438int
2439i915_gem_execbuffer(struct drm_device *dev, void *data,
2440 struct drm_file *file)
2441{
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002442 const size_t sz = (sizeof(struct drm_i915_gem_exec_object2) +
2443 sizeof(struct i915_vma *) +
2444 sizeof(unsigned int));
Chris Wilson54cf91d2010-11-25 18:00:26 +00002445 struct drm_i915_gem_execbuffer *args = data;
2446 struct drm_i915_gem_execbuffer2 exec2;
2447 struct drm_i915_gem_exec_object *exec_list = NULL;
2448 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +01002449 unsigned int i;
2450 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002451
Chris Wilson2889caa2017-06-16 15:05:19 +01002452 if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) {
2453 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002454 return -EINVAL;
2455 }
2456
Chris Wilson2889caa2017-06-16 15:05:19 +01002457 exec2.buffers_ptr = args->buffers_ptr;
2458 exec2.buffer_count = args->buffer_count;
2459 exec2.batch_start_offset = args->batch_start_offset;
2460 exec2.batch_len = args->batch_len;
2461 exec2.DR1 = args->DR1;
2462 exec2.DR4 = args->DR4;
2463 exec2.num_cliprects = args->num_cliprects;
2464 exec2.cliprects_ptr = args->cliprects_ptr;
2465 exec2.flags = I915_EXEC_RENDER;
2466 i915_execbuffer2_set_context_id(exec2, 0);
2467
2468 if (!i915_gem_check_execbuffer(&exec2))
2469 return -EINVAL;
2470
Chris Wilson54cf91d2010-11-25 18:00:26 +00002471 /* Copy in the exec list from userland */
Chris Wilson2889caa2017-06-16 15:05:19 +01002472 exec_list = kvmalloc_array(args->buffer_count, sizeof(*exec_list),
2473 __GFP_NOWARN | GFP_TEMPORARY);
2474 exec2_list = kvmalloc_array(args->buffer_count + 1, sz,
2475 __GFP_NOWARN | GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002476 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002477 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002478 args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002479 kvfree(exec_list);
2480 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002481 return -ENOMEM;
2482 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002483 err = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002484 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00002485 sizeof(*exec_list) * args->buffer_count);
Chris Wilson2889caa2017-06-16 15:05:19 +01002486 if (err) {
Daniel Vetterff240192012-01-31 21:08:14 +01002487 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson2889caa2017-06-16 15:05:19 +01002488 args->buffer_count, err);
Michal Hocko20981052017-05-17 14:23:12 +02002489 kvfree(exec_list);
2490 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002491 return -EFAULT;
2492 }
2493
2494 for (i = 0; i < args->buffer_count; i++) {
2495 exec2_list[i].handle = exec_list[i].handle;
2496 exec2_list[i].relocation_count = exec_list[i].relocation_count;
2497 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
2498 exec2_list[i].alignment = exec_list[i].alignment;
2499 exec2_list[i].offset = exec_list[i].offset;
Tvrtko Ursulinf0836b72016-11-16 08:55:32 +00002500 if (INTEL_GEN(to_i915(dev)) < 4)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002501 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
2502 else
2503 exec2_list[i].flags = 0;
2504 }
2505
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002506 err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list, NULL);
Chris Wilson2889caa2017-06-16 15:05:19 +01002507 if (exec2.flags & __EXEC_HAS_RELOC) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002508 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002509 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002510
Chris Wilson54cf91d2010-11-25 18:00:26 +00002511 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002512 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002513 if (!(exec2_list[i].offset & UPDATE))
2514 continue;
2515
Michał Winiarski934acce2015-12-29 18:24:52 +01002516 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002517 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2518 exec2_list[i].offset &= PIN_OFFSET_MASK;
2519 if (__copy_to_user(&user_exec_list[i].offset,
2520 &exec2_list[i].offset,
2521 sizeof(user_exec_list[i].offset)))
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002522 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002523 }
2524 }
2525
Michal Hocko20981052017-05-17 14:23:12 +02002526 kvfree(exec_list);
2527 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002528 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002529}
2530
2531int
2532i915_gem_execbuffer2(struct drm_device *dev, void *data,
2533 struct drm_file *file)
2534{
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002535 const size_t sz = (sizeof(struct drm_i915_gem_exec_object2) +
2536 sizeof(struct i915_vma *) +
2537 sizeof(unsigned int));
Chris Wilson54cf91d2010-11-25 18:00:26 +00002538 struct drm_i915_gem_execbuffer2 *args = data;
Chris Wilson2889caa2017-06-16 15:05:19 +01002539 struct drm_i915_gem_exec_object2 *exec2_list;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002540 struct drm_syncobj **fences = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +01002541 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002542
Chris Wilson2889caa2017-06-16 15:05:19 +01002543 if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01002544 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002545 return -EINVAL;
2546 }
2547
Chris Wilson2889caa2017-06-16 15:05:19 +01002548 if (!i915_gem_check_execbuffer(args))
2549 return -EINVAL;
2550
2551 /* Allocate an extra slot for use by the command parser */
2552 exec2_list = kvmalloc_array(args->buffer_count + 1, sz,
2553 __GFP_NOWARN | GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002554 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002555 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002556 args->buffer_count);
2557 return -ENOMEM;
2558 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002559 if (copy_from_user(exec2_list,
2560 u64_to_user_ptr(args->buffers_ptr),
2561 sizeof(*exec2_list) * args->buffer_count)) {
2562 DRM_DEBUG("copy %d exec entries failed\n", args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002563 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002564 return -EFAULT;
2565 }
2566
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002567 if (args->flags & I915_EXEC_FENCE_ARRAY) {
2568 fences = get_fence_array(args, file);
2569 if (IS_ERR(fences)) {
2570 kvfree(exec2_list);
2571 return PTR_ERR(fences);
2572 }
2573 }
2574
2575 err = i915_gem_do_execbuffer(dev, file, args, exec2_list, fences);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002576
Chris Wilson2889caa2017-06-16 15:05:19 +01002577 /*
2578 * Now that we have begun execution of the batchbuffer, we ignore
2579 * any new error after this point. Also given that we have already
2580 * updated the associated relocations, we try to write out the current
2581 * object locations irrespective of any error.
2582 */
2583 if (args->flags & __EXEC_HAS_RELOC) {
2584 struct drm_i915_gem_exec_object2 __user *user_exec_list =
2585 u64_to_user_ptr(args->buffers_ptr);
2586 unsigned int i;
2587
2588 /* Copy the new buffer offsets back to the user's exec list. */
2589 user_access_begin();
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002590 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002591 if (!(exec2_list[i].offset & UPDATE))
2592 continue;
2593
Michał Winiarski934acce2015-12-29 18:24:52 +01002594 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002595 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2596 unsafe_put_user(exec2_list[i].offset,
2597 &user_exec_list[i].offset,
2598 end_user);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002599 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002600end_user:
2601 user_access_end();
Chris Wilson54cf91d2010-11-25 18:00:26 +00002602 }
2603
Chris Wilson2889caa2017-06-16 15:05:19 +01002604 args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002605 put_fence_array(args, fences);
Michal Hocko20981052017-05-17 14:23:12 +02002606 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002607 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002608}