blob: 70ccd63cbf8ee101caa6fdfc43b9e3bc49c2b5f5 [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)
Chris Wilson74c1c692017-09-21 12:01:35 +010061#define __EXEC_INTERNAL_FLAGS (~0u << 30)
Chris Wilson2889caa2017-06-16 15:05:19 +010062#define UPDATE PIN_OFFSET_FIXED
Chris Wilsond23db882014-05-23 08:48:08 +020063
64#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000065
Chris Wilson650bc632017-06-15 09:14:33 +010066#define __I915_EXEC_ILLEGAL_FLAGS \
67 (__I915_EXEC_UNKNOWN_FLAGS | I915_EXEC_CONSTANTS_MASK)
Chris Wilson5b043f42016-08-02 22:50:38 +010068
Chris Wilson2889caa2017-06-16 15:05:19 +010069/**
70 * DOC: User command execution
71 *
72 * Userspace submits commands to be executed on the GPU as an instruction
73 * stream within a GEM object we call a batchbuffer. This instructions may
74 * refer to other GEM objects containing auxiliary state such as kernels,
75 * samplers, render targets and even secondary batchbuffers. Userspace does
76 * not know where in the GPU memory these objects reside and so before the
77 * batchbuffer is passed to the GPU for execution, those addresses in the
78 * batchbuffer and auxiliary objects are updated. This is known as relocation,
79 * or patching. To try and avoid having to relocate each object on the next
80 * execution, userspace is told the location of those objects in this pass,
81 * but this remains just a hint as the kernel may choose a new location for
82 * any object in the future.
83 *
84 * Processing an execbuf ioctl is conceptually split up into a few phases.
85 *
86 * 1. Validation - Ensure all the pointers, handles and flags are valid.
87 * 2. Reservation - Assign GPU address space for every object
88 * 3. Relocation - Update any addresses to point to the final locations
89 * 4. Serialisation - Order the request with respect to its dependencies
90 * 5. Construction - Construct a request to execute the batchbuffer
91 * 6. Submission (at some point in the future execution)
92 *
93 * Reserving resources for the execbuf is the most complicated phase. We
94 * neither want to have to migrate the object in the address space, nor do
95 * we want to have to update any relocations pointing to this object. Ideally,
96 * we want to leave the object where it is and for all the existing relocations
97 * to match. If the object is given a new address, or if userspace thinks the
98 * object is elsewhere, we have to parse all the relocation entries and update
99 * the addresses. Userspace can set the I915_EXEC_NORELOC flag to hint that
100 * all the target addresses in all of its objects match the value in the
101 * relocation entries and that they all match the presumed offsets given by the
102 * list of execbuffer objects. Using this knowledge, we know that if we haven't
103 * moved any buffers, all the relocation entries are valid and we can skip
104 * the update. (If userspace is wrong, the likely outcome is an impromptu GPU
105 * hang.) The requirement for using I915_EXEC_NO_RELOC are:
106 *
107 * The addresses written in the objects must match the corresponding
108 * reloc.presumed_offset which in turn must match the corresponding
109 * execobject.offset.
110 *
111 * Any render targets written to in the batch must be flagged with
112 * EXEC_OBJECT_WRITE.
113 *
114 * To avoid stalling, execobject.offset should match the current
115 * address of that object within the active context.
116 *
117 * The reservation is done is multiple phases. First we try and keep any
118 * object already bound in its current location - so as long as meets the
119 * constraints imposed by the new execbuffer. Any object left unbound after the
120 * first pass is then fitted into any available idle space. If an object does
121 * not fit, all objects are removed from the reservation and the process rerun
122 * after sorting the objects into a priority order (more difficult to fit
123 * objects are tried first). Failing that, the entire VM is cleared and we try
124 * to fit the execbuf once last time before concluding that it simply will not
125 * fit.
126 *
127 * A small complication to all of this is that we allow userspace not only to
128 * specify an alignment and a size for the object in the address space, but
129 * we also allow userspace to specify the exact offset. This objects are
130 * simpler to place (the location is known a priori) all we have to do is make
131 * sure the space is available.
132 *
133 * Once all the objects are in place, patching up the buried pointers to point
134 * to the final locations is a fairly simple job of walking over the relocation
135 * entry arrays, looking up the right address and rewriting the value into
136 * the object. Simple! ... The relocation entries are stored in user memory
137 * and so to access them we have to copy them into a local buffer. That copy
138 * has to avoid taking any pagefaults as they may lead back to a GEM object
139 * requiring the struct_mutex (i.e. recursive deadlock). So once again we split
140 * the relocation into multiple passes. First we try to do everything within an
141 * atomic context (avoid the pagefaults) which requires that we never wait. If
142 * we detect that we may wait, or if we need to fault, then we have to fallback
143 * to a slower path. The slowpath has to drop the mutex. (Can you hear alarm
144 * bells yet?) Dropping the mutex means that we lose all the state we have
145 * built up so far for the execbuf and we must reset any global data. However,
146 * we do leave the objects pinned in their final locations - which is a
147 * potential issue for concurrent execbufs. Once we have left the mutex, we can
148 * allocate and copy all the relocation entries into a large array at our
149 * leisure, reacquire the mutex, reclaim all the objects and other state and
150 * then proceed to update any incorrect addresses with the objects.
151 *
152 * As we process the relocation entries, we maintain a record of whether the
153 * object is being written to. Using NORELOC, we expect userspace to provide
154 * this information instead. We also check whether we can skip the relocation
155 * by comparing the expected value inside the relocation entry with the target's
156 * final address. If they differ, we have to map the current object and rewrite
157 * the 4 or 8 byte pointer within.
158 *
159 * Serialising an execbuf is quite simple according to the rules of the GEM
160 * ABI. Execution within each context is ordered by the order of submission.
161 * Writes to any GEM object are in order of submission and are exclusive. Reads
162 * from a GEM object are unordered with respect to other reads, but ordered by
163 * writes. A write submitted after a read cannot occur before the read, and
164 * similarly any read submitted after a write cannot occur before the write.
165 * Writes are ordered between engines such that only one write occurs at any
166 * time (completing any reads beforehand) - using semaphores where available
167 * and CPU serialisation otherwise. Other GEM access obey the same rules, any
168 * write (either via mmaps using set-domain, or via pwrite) must flush all GPU
169 * reads before starting, and any read (either using set-domain or pread) must
170 * flush all GPU writes before starting. (Note we only employ a barrier before,
171 * we currently rely on userspace not concurrently starting a new execution
172 * whilst reading or writing to an object. This may be an advantage or not
173 * depending on how much you trust userspace not to shoot themselves in the
174 * foot.) Serialisation may just result in the request being inserted into
175 * a DAG awaiting its turn, but most simple is to wait on the CPU until
176 * all dependencies are resolved.
177 *
178 * After all of that, is just a matter of closing the request and handing it to
179 * the hardware (well, leaving it in a queue to be executed). However, we also
180 * offer the ability for batchbuffers to be run with elevated privileges so
181 * that they access otherwise hidden registers. (Used to adjust L3 cache etc.)
182 * Before any batch is given extra privileges we first must check that it
183 * contains no nefarious instructions, we check that each instruction is from
184 * our whitelist and all registers are also from an allowed list. We first
185 * copy the user's batchbuffer to a shadow (so that the user doesn't have
186 * access to it, either by the CPU or GPU as we scan it) and then parse each
187 * instruction. If everything is ok, we set a flag telling the hardware to run
188 * the batchbuffer in trusted mode, otherwise the ioctl is rejected.
189 */
190
Chris Wilson650bc632017-06-15 09:14:33 +0100191struct i915_execbuffer {
Chris Wilson2889caa2017-06-16 15:05:19 +0100192 struct drm_i915_private *i915; /** i915 backpointer */
193 struct drm_file *file; /** per-file lookup tables and limits */
194 struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */
195 struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100196 struct i915_vma **vma;
197 unsigned int *flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100198
199 struct intel_engine_cs *engine; /** engine to queue the request to */
200 struct i915_gem_context *ctx; /** context for building the request */
201 struct i915_address_space *vm; /** GTT and vma for the request */
202
203 struct drm_i915_gem_request *request; /** our request to build */
204 struct i915_vma *batch; /** identity of the batch obj/vma */
205
206 /** actual size of execobj[] as we may extend it for the cmdparser */
207 unsigned int buffer_count;
208
209 /** list of vma not yet bound during reservation phase */
210 struct list_head unbound;
211
212 /** list of vma that have execobj.relocation_count */
213 struct list_head relocs;
214
215 /**
216 * Track the most recently used object for relocations, as we
217 * frequently have to perform multiple relocations within the same
218 * obj/page
219 */
Chris Wilson650bc632017-06-15 09:14:33 +0100220 struct reloc_cache {
Chris Wilson2889caa2017-06-16 15:05:19 +0100221 struct drm_mm_node node; /** temporary GTT binding */
222 unsigned long vaddr; /** Current kmap address */
223 unsigned long page; /** Currently mapped page index */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100224 unsigned int gen; /** Cached value of INTEL_GEN */
Chris Wilson650bc632017-06-15 09:14:33 +0100225 bool use_64bit_reloc : 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100226 bool has_llc : 1;
227 bool has_fence : 1;
228 bool needs_unfenced : 1;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100229
230 struct drm_i915_gem_request *rq;
231 u32 *rq_cmd;
232 unsigned int rq_size;
Chris Wilson650bc632017-06-15 09:14:33 +0100233 } reloc_cache;
Chris Wilson2889caa2017-06-16 15:05:19 +0100234
235 u64 invalid_flags; /** Set of execobj.flags that are invalid */
236 u32 context_flags; /** Set of execobj.flags to insert from the ctx */
237
238 u32 batch_start_offset; /** Location within object of batch */
239 u32 batch_len; /** Length of batch within object */
240 u32 batch_flags; /** Flags composed for emit_bb_start() */
241
242 /**
243 * Indicate either the size of the hastable used to resolve
244 * relocation handles, or if negative that we are using a direct
245 * index into the execobj[].
246 */
247 int lut_size;
248 struct hlist_head *buckets; /** ht for relocation handles */
Chris Wilson67731b82010-12-08 10:38:14 +0000249};
250
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100251#define exec_entry(EB, VMA) (&(EB)->exec[(VMA)->exec_flags - (EB)->flags])
Chris Wilson4ff4b442017-06-16 15:05:16 +0100252
Chris Wilson2889caa2017-06-16 15:05:19 +0100253/*
254 * Used to convert any address to canonical form.
255 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
256 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
257 * addresses to be in a canonical form:
258 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
259 * canonical form [63:48] == [47]."
260 */
261#define GEN8_HIGH_ADDRESS_BIT 47
262static inline u64 gen8_canonical_addr(u64 address)
263{
264 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
265}
266
267static inline u64 gen8_noncanonical_addr(u64 address)
268{
269 return address & GENMASK_ULL(GEN8_HIGH_ADDRESS_BIT, 0);
270}
271
Chris Wilson3dbf26e2017-08-26 14:56:20 +0100272static inline bool eb_use_cmdparser(const struct i915_execbuffer *eb)
273{
Tvrtko Ursulin439e2ee2017-11-29 08:24:09 +0000274 return intel_engine_needs_cmd_parser(eb->engine) && eb->batch_len;
Chris Wilson3dbf26e2017-08-26 14:56:20 +0100275}
276
Chris Wilson650bc632017-06-15 09:14:33 +0100277static int eb_create(struct i915_execbuffer *eb)
Chris Wilson67731b82010-12-08 10:38:14 +0000278{
Chris Wilson2889caa2017-06-16 15:05:19 +0100279 if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) {
280 unsigned int size = 1 + ilog2(eb->buffer_count);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000281
Chris Wilson2889caa2017-06-16 15:05:19 +0100282 /*
283 * Without a 1:1 association between relocation handles and
284 * the execobject[] index, we instead create a hashtable.
285 * We size it dynamically based on available memory, starting
286 * first with 1:1 assocative hash and scaling back until
287 * the allocation succeeds.
288 *
289 * Later on we use a positive lut_size to indicate we are
290 * using this hashtable, and a negative value to indicate a
291 * direct lookup.
292 */
Chris Wilson4ff4b442017-06-16 15:05:16 +0100293 do {
Chris Wilson0d95c882017-09-01 15:57:28 +0100294 gfp_t flags;
Chris Wilson4d470f72017-06-29 16:04:25 +0100295
296 /* While we can still reduce the allocation size, don't
297 * raise a warning and allow the allocation to fail.
298 * On the last pass though, we want to try as hard
299 * as possible to perform the allocation and warn
300 * if it fails.
301 */
Michal Hocko0ee931c2017-09-13 16:28:29 -0700302 flags = GFP_KERNEL;
Chris Wilson4d470f72017-06-29 16:04:25 +0100303 if (size > 1)
304 flags |= __GFP_NORETRY | __GFP_NOWARN;
305
Chris Wilson4ff4b442017-06-16 15:05:16 +0100306 eb->buckets = kzalloc(sizeof(struct hlist_head) << size,
Chris Wilson4d470f72017-06-29 16:04:25 +0100307 flags);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100308 if (eb->buckets)
309 break;
310 } while (--size);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000311
Chris Wilson4d470f72017-06-29 16:04:25 +0100312 if (unlikely(!size))
313 return -ENOMEM;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100314
Chris Wilson2889caa2017-06-16 15:05:19 +0100315 eb->lut_size = size;
Chris Wilson650bc632017-06-15 09:14:33 +0100316 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +0100317 eb->lut_size = -eb->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +0100318 }
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000319
Chris Wilson650bc632017-06-15 09:14:33 +0100320 return 0;
Chris Wilson67731b82010-12-08 10:38:14 +0000321}
322
Chris Wilson2889caa2017-06-16 15:05:19 +0100323static bool
324eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry,
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100325 const struct i915_vma *vma,
326 unsigned int flags)
Chris Wilson2889caa2017-06-16 15:05:19 +0100327{
Chris Wilson2889caa2017-06-16 15:05:19 +0100328 if (vma->node.size < entry->pad_to_size)
329 return true;
330
331 if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment))
332 return true;
333
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100334 if (flags & EXEC_OBJECT_PINNED &&
Chris Wilson2889caa2017-06-16 15:05:19 +0100335 vma->node.start != entry->offset)
336 return true;
337
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100338 if (flags & __EXEC_OBJECT_NEEDS_BIAS &&
Chris Wilson2889caa2017-06-16 15:05:19 +0100339 vma->node.start < BATCH_OFFSET_BIAS)
340 return true;
341
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100342 if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) &&
Chris Wilson2889caa2017-06-16 15:05:19 +0100343 (vma->node.start + vma->node.size - 1) >> 32)
344 return true;
345
Chris Wilson1d033be2017-10-31 10:36:07 +0000346 if (flags & __EXEC_OBJECT_NEEDS_MAP &&
347 !i915_vma_is_map_and_fenceable(vma))
348 return true;
349
Chris Wilson2889caa2017-06-16 15:05:19 +0100350 return false;
351}
352
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100353static inline bool
Chris Wilson2889caa2017-06-16 15:05:19 +0100354eb_pin_vma(struct i915_execbuffer *eb,
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100355 const struct drm_i915_gem_exec_object2 *entry,
Chris Wilson2889caa2017-06-16 15:05:19 +0100356 struct i915_vma *vma)
357{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100358 unsigned int exec_flags = *vma->exec_flags;
359 u64 pin_flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100360
Chris Wilson616d9ce2017-06-16 15:05:21 +0100361 if (vma->node.size)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100362 pin_flags = vma->node.start;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100363 else
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100364 pin_flags = entry->offset & PIN_OFFSET_MASK;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100365
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100366 pin_flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED;
367 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_GTT))
368 pin_flags |= PIN_GLOBAL;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100369
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100370 if (unlikely(i915_vma_pin(vma, 0, 0, pin_flags)))
371 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100372
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100373 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) {
Chris Wilson3bd40732017-10-09 09:43:56 +0100374 if (unlikely(i915_vma_pin_fence(vma))) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100375 i915_vma_unpin(vma);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100376 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100377 }
378
Chris Wilson3bd40732017-10-09 09:43:56 +0100379 if (vma->fence)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100380 exec_flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100381 }
382
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100383 *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN;
384 return !eb_vma_misplaced(entry, vma, exec_flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100385}
386
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100387static inline void __eb_unreserve_vma(struct i915_vma *vma, unsigned int flags)
Chris Wilsond55495b2017-06-15 09:14:34 +0100388{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100389 GEM_BUG_ON(!(flags & __EXEC_OBJECT_HAS_PIN));
Chris Wilson2889caa2017-06-16 15:05:19 +0100390
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100391 if (unlikely(flags & __EXEC_OBJECT_HAS_FENCE))
Chris Wilson3bd40732017-10-09 09:43:56 +0100392 __i915_vma_unpin_fence(vma);
Chris Wilsond55495b2017-06-15 09:14:34 +0100393
Chris Wilson2889caa2017-06-16 15:05:19 +0100394 __i915_vma_unpin(vma);
Chris Wilsond55495b2017-06-15 09:14:34 +0100395}
396
Chris Wilson2889caa2017-06-16 15:05:19 +0100397static inline void
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100398eb_unreserve_vma(struct i915_vma *vma, unsigned int *flags)
Chris Wilsond55495b2017-06-15 09:14:34 +0100399{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100400 if (!(*flags & __EXEC_OBJECT_HAS_PIN))
Chris Wilson2889caa2017-06-16 15:05:19 +0100401 return;
Chris Wilsond55495b2017-06-15 09:14:34 +0100402
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100403 __eb_unreserve_vma(vma, *flags);
404 *flags &= ~__EXEC_OBJECT_RESERVED;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100405}
406
407static int
Chris Wilson2889caa2017-06-16 15:05:19 +0100408eb_validate_vma(struct i915_execbuffer *eb,
409 struct drm_i915_gem_exec_object2 *entry,
410 struct i915_vma *vma)
411{
412 if (unlikely(entry->flags & eb->invalid_flags))
413 return -EINVAL;
414
415 if (unlikely(entry->alignment && !is_power_of_2(entry->alignment)))
416 return -EINVAL;
417
418 /*
419 * Offset can be used as input (EXEC_OBJECT_PINNED), reject
420 * any non-page-aligned or non-canonical addresses.
421 */
422 if (unlikely(entry->flags & EXEC_OBJECT_PINNED &&
423 entry->offset != gen8_canonical_addr(entry->offset & PAGE_MASK)))
424 return -EINVAL;
425
426 /* pad_to_size was once a reserved field, so sanitize it */
427 if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) {
428 if (unlikely(offset_in_page(entry->pad_to_size)))
429 return -EINVAL;
430 } else {
431 entry->pad_to_size = 0;
432 }
433
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100434 if (unlikely(vma->exec_flags)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100435 DRM_DEBUG("Object [handle %d, index %d] appears more than once in object list\n",
436 entry->handle, (int)(entry - eb->exec));
437 return -EINVAL;
438 }
439
440 /*
441 * From drm_mm perspective address space is continuous,
442 * so from this point we're always using non-canonical
443 * form internally.
444 */
445 entry->offset = gen8_noncanonical_addr(entry->offset);
446
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100447 if (!eb->reloc_cache.has_fence) {
448 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
449 } else {
450 if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE ||
451 eb->reloc_cache.needs_unfenced) &&
452 i915_gem_object_is_tiled(vma->obj))
453 entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP;
454 }
455
456 if (!(entry->flags & EXEC_OBJECT_PINNED))
457 entry->flags |= eb->context_flags;
458
Chris Wilson2889caa2017-06-16 15:05:19 +0100459 return 0;
460}
461
462static int
Chris Wilsond1b48c12017-08-16 09:52:08 +0100463eb_add_vma(struct i915_execbuffer *eb, unsigned int i, struct i915_vma *vma)
Chris Wilson2889caa2017-06-16 15:05:19 +0100464{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100465 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100466 int err;
467
468 GEM_BUG_ON(i915_vma_is_closed(vma));
469
470 if (!(eb->args->flags & __EXEC_VALIDATED)) {
471 err = eb_validate_vma(eb, entry, vma);
472 if (unlikely(err))
473 return err;
474 }
475
Chris Wilson4d470f72017-06-29 16:04:25 +0100476 if (eb->lut_size > 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100477 vma->exec_handle = entry->handle;
478 hlist_add_head(&vma->exec_node,
479 &eb->buckets[hash_32(entry->handle,
480 eb->lut_size)]);
481 }
482
483 if (entry->relocation_count)
484 list_add_tail(&vma->reloc_link, &eb->relocs);
485
Chris Wilson2889caa2017-06-16 15:05:19 +0100486 /*
487 * Stash a pointer from the vma to execobj, so we can query its flags,
488 * size, alignment etc as provided by the user. Also we stash a pointer
489 * to the vma inside the execobj so that we can use a direct lookup
490 * to find the right target VMA when doing relocations.
491 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100492 eb->vma[i] = vma;
Chris Wilsond1b48c12017-08-16 09:52:08 +0100493 eb->flags[i] = entry->flags;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100494 vma->exec_flags = &eb->flags[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100495
496 err = 0;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100497 if (eb_pin_vma(eb, entry, vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100498 if (entry->offset != vma->node.start) {
499 entry->offset = vma->node.start | UPDATE;
500 eb->args->flags |= __EXEC_HAS_RELOC;
501 }
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100502 } else {
503 eb_unreserve_vma(vma, vma->exec_flags);
504
505 list_add_tail(&vma->exec_link, &eb->unbound);
506 if (drm_mm_node_allocated(&vma->node))
507 err = i915_vma_unbind(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100508 }
509 return err;
510}
511
512static inline int use_cpu_reloc(const struct reloc_cache *cache,
513 const struct drm_i915_gem_object *obj)
514{
515 if (!i915_gem_object_has_struct_page(obj))
516 return false;
517
Chris Wilson7dd4f672017-06-16 15:05:24 +0100518 if (DBG_FORCE_RELOC == FORCE_CPU_RELOC)
519 return true;
520
521 if (DBG_FORCE_RELOC == FORCE_GTT_RELOC)
522 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100523
524 return (cache->has_llc ||
525 obj->cache_dirty ||
526 obj->cache_level != I915_CACHE_NONE);
527}
528
529static int eb_reserve_vma(const struct i915_execbuffer *eb,
530 struct i915_vma *vma)
531{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100532 struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
533 unsigned int exec_flags = *vma->exec_flags;
534 u64 pin_flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100535 int err;
536
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100537 pin_flags = PIN_USER | PIN_NONBLOCK;
538 if (exec_flags & EXEC_OBJECT_NEEDS_GTT)
539 pin_flags |= PIN_GLOBAL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100540
541 /*
542 * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
543 * limit address to the first 4GBs for unflagged objects.
544 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100545 if (!(exec_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
546 pin_flags |= PIN_ZONE_4G;
Chris Wilson2889caa2017-06-16 15:05:19 +0100547
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100548 if (exec_flags & __EXEC_OBJECT_NEEDS_MAP)
549 pin_flags |= PIN_MAPPABLE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100550
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100551 if (exec_flags & EXEC_OBJECT_PINNED) {
552 pin_flags |= entry->offset | PIN_OFFSET_FIXED;
553 pin_flags &= ~PIN_NONBLOCK; /* force overlapping checks */
554 } else if (exec_flags & __EXEC_OBJECT_NEEDS_BIAS) {
555 pin_flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson2889caa2017-06-16 15:05:19 +0100556 }
557
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100558 err = i915_vma_pin(vma,
559 entry->pad_to_size, entry->alignment,
560 pin_flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100561 if (err)
562 return err;
563
564 if (entry->offset != vma->node.start) {
565 entry->offset = vma->node.start | UPDATE;
566 eb->args->flags |= __EXEC_HAS_RELOC;
567 }
568
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100569 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) {
Chris Wilson3bd40732017-10-09 09:43:56 +0100570 err = i915_vma_pin_fence(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100571 if (unlikely(err)) {
572 i915_vma_unpin(vma);
573 return err;
574 }
575
Chris Wilson3bd40732017-10-09 09:43:56 +0100576 if (vma->fence)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100577 exec_flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100578 }
579
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100580 *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN;
581 GEM_BUG_ON(eb_vma_misplaced(entry, vma, exec_flags));
Chris Wilson1da7b542017-07-21 15:50:35 +0100582
Chris Wilson2889caa2017-06-16 15:05:19 +0100583 return 0;
584}
585
586static int eb_reserve(struct i915_execbuffer *eb)
587{
588 const unsigned int count = eb->buffer_count;
589 struct list_head last;
590 struct i915_vma *vma;
591 unsigned int i, pass;
592 int err;
593
594 /*
595 * Attempt to pin all of the buffers into the GTT.
596 * This is done in 3 phases:
597 *
598 * 1a. Unbind all objects that do not match the GTT constraints for
599 * the execbuffer (fenceable, mappable, alignment etc).
600 * 1b. Increment pin count for already bound objects.
601 * 2. Bind new objects.
602 * 3. Decrement pin count.
603 *
604 * This avoid unnecessary unbinding of later objects in order to make
605 * room for the earlier objects *unless* we need to defragment.
606 */
607
608 pass = 0;
609 err = 0;
610 do {
611 list_for_each_entry(vma, &eb->unbound, exec_link) {
612 err = eb_reserve_vma(eb, vma);
613 if (err)
614 break;
615 }
616 if (err != -ENOSPC)
617 return err;
618
619 /* Resort *all* the objects into priority order */
620 INIT_LIST_HEAD(&eb->unbound);
621 INIT_LIST_HEAD(&last);
622 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100623 unsigned int flags = eb->flags[i];
624 struct i915_vma *vma = eb->vma[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100625
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100626 if (flags & EXEC_OBJECT_PINNED &&
627 flags & __EXEC_OBJECT_HAS_PIN)
Chris Wilson2889caa2017-06-16 15:05:19 +0100628 continue;
629
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100630 eb_unreserve_vma(vma, &eb->flags[i]);
Chris Wilson2889caa2017-06-16 15:05:19 +0100631
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100632 if (flags & EXEC_OBJECT_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +0100633 list_add(&vma->exec_link, &eb->unbound);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100634 else if (flags & __EXEC_OBJECT_NEEDS_MAP)
Chris Wilson2889caa2017-06-16 15:05:19 +0100635 list_add_tail(&vma->exec_link, &eb->unbound);
636 else
637 list_add_tail(&vma->exec_link, &last);
638 }
639 list_splice_tail(&last, &eb->unbound);
640
641 switch (pass++) {
642 case 0:
643 break;
644
645 case 1:
646 /* Too fragmented, unbind everything and retry */
647 err = i915_gem_evict_vm(eb->vm);
648 if (err)
649 return err;
650 break;
651
652 default:
653 return -ENOSPC;
654 }
655 } while (1);
656}
657
Chris Wilson2889caa2017-06-16 15:05:19 +0100658static unsigned int eb_batch_index(const struct i915_execbuffer *eb)
659{
Chris Wilson1a71cf22017-06-16 15:05:23 +0100660 if (eb->args->flags & I915_EXEC_BATCH_FIRST)
661 return 0;
662 else
663 return eb->buffer_count - 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100664}
665
666static int eb_select_context(struct i915_execbuffer *eb)
667{
668 struct i915_gem_context *ctx;
669
670 ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1);
Chris Wilson1acfc102017-06-20 12:05:47 +0100671 if (unlikely(!ctx))
672 return -ENOENT;
Chris Wilson2889caa2017-06-16 15:05:19 +0100673
Chris Wilson1acfc102017-06-20 12:05:47 +0100674 eb->ctx = ctx;
Chris Wilson2889caa2017-06-16 15:05:19 +0100675 eb->vm = ctx->ppgtt ? &ctx->ppgtt->base : &eb->i915->ggtt.base;
676
677 eb->context_flags = 0;
678 if (ctx->flags & CONTEXT_NO_ZEROMAP)
679 eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS;
680
681 return 0;
682}
683
684static int eb_lookup_vmas(struct i915_execbuffer *eb)
Chris Wilson4ff4b442017-06-16 15:05:16 +0100685{
Chris Wilsond1b48c12017-08-16 09:52:08 +0100686 struct radix_tree_root *handles_vma = &eb->ctx->handles_vma;
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100687 struct drm_i915_gem_object *obj;
Chris Wilson2889caa2017-06-16 15:05:19 +0100688 unsigned int i;
Chris Wilson2889caa2017-06-16 15:05:19 +0100689 int err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100690
Chris Wilson8bcbfb12017-08-16 09:52:05 +0100691 if (unlikely(i915_gem_context_is_closed(eb->ctx)))
692 return -ENOENT;
693
694 if (unlikely(i915_gem_context_is_banned(eb->ctx)))
695 return -EIO;
696
Chris Wilson2889caa2017-06-16 15:05:19 +0100697 INIT_LIST_HEAD(&eb->relocs);
698 INIT_LIST_HEAD(&eb->unbound);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100699
Chris Wilson170fa292017-08-16 09:52:07 +0100700 for (i = 0; i < eb->buffer_count; i++) {
701 u32 handle = eb->exec[i].handle;
Chris Wilsond1b48c12017-08-16 09:52:08 +0100702 struct i915_lut_handle *lut;
Chris Wilson170fa292017-08-16 09:52:07 +0100703 struct i915_vma *vma;
704
Chris Wilsond1b48c12017-08-16 09:52:08 +0100705 vma = radix_tree_lookup(handles_vma, handle);
706 if (likely(vma))
Chris Wilson170fa292017-08-16 09:52:07 +0100707 goto add_vma;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100708
Chris Wilson170fa292017-08-16 09:52:07 +0100709 obj = i915_gem_object_lookup(eb->file, handle);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100710 if (unlikely(!obj)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100711 err = -ENOENT;
Chris Wilson170fa292017-08-16 09:52:07 +0100712 goto err_vma;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100713 }
714
Chris Wilson4ff4b442017-06-16 15:05:16 +0100715 vma = i915_vma_instance(obj, eb->vm, NULL);
716 if (unlikely(IS_ERR(vma))) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100717 err = PTR_ERR(vma);
Chris Wilson170fa292017-08-16 09:52:07 +0100718 goto err_obj;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100719 }
720
Chris Wilsond1b48c12017-08-16 09:52:08 +0100721 lut = kmem_cache_alloc(eb->i915->luts, GFP_KERNEL);
722 if (unlikely(!lut)) {
723 err = -ENOMEM;
724 goto err_obj;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100725 }
726
Chris Wilsond1b48c12017-08-16 09:52:08 +0100727 err = radix_tree_insert(handles_vma, handle, vma);
728 if (unlikely(err)) {
729 kfree(lut);
730 goto err_obj;
731 }
732
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100733 /* transfer ref to ctx */
Chris Wilson3ffff012017-08-22 12:05:17 +0100734 vma->open_count++;
Chris Wilsond1b48c12017-08-16 09:52:08 +0100735 list_add(&lut->obj_link, &obj->lut_list);
736 list_add(&lut->ctx_link, &eb->ctx->handles_list);
737 lut->ctx = eb->ctx;
738 lut->handle = handle;
739
Chris Wilson170fa292017-08-16 09:52:07 +0100740add_vma:
Chris Wilsond1b48c12017-08-16 09:52:08 +0100741 err = eb_add_vma(eb, i, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100742 if (unlikely(err))
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100743 goto err_vma;
Chris Wilsondade2a62017-06-16 15:05:20 +0100744
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100745 GEM_BUG_ON(vma != eb->vma[i]);
746 GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100747 }
748
Chris Wilson2889caa2017-06-16 15:05:19 +0100749 /* take note of the batch buffer before we might reorder the lists */
750 i = eb_batch_index(eb);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100751 eb->batch = eb->vma[i];
752 GEM_BUG_ON(eb->batch->exec_flags != &eb->flags[i]);
Chris Wilson59bfa122016-08-04 16:32:31 +0100753
754 /*
755 * SNA is doing fancy tricks with compressing batch buffers, which leads
756 * to negative relocation deltas. Usually that works out ok since the
757 * relocate address is still positive, except when the batch is placed
758 * very low in the GTT. Ensure this doesn't happen.
759 *
760 * Note that actual hangs have only been observed on gen7, but for
761 * paranoia do it everywhere.
762 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100763 if (!(eb->flags[i] & EXEC_OBJECT_PINNED))
764 eb->flags[i] |= __EXEC_OBJECT_NEEDS_BIAS;
Chris Wilson2889caa2017-06-16 15:05:19 +0100765 if (eb->reloc_cache.has_fence)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100766 eb->flags[i] |= EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson59bfa122016-08-04 16:32:31 +0100767
Chris Wilson2889caa2017-06-16 15:05:19 +0100768 eb->args->flags |= __EXEC_VALIDATED;
769 return eb_reserve(eb);
770
Chris Wilson170fa292017-08-16 09:52:07 +0100771err_obj:
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100772 i915_gem_object_put(obj);
Chris Wilson170fa292017-08-16 09:52:07 +0100773err_vma:
774 eb->vma[i] = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100775 return err;
Chris Wilson59bfa122016-08-04 16:32:31 +0100776}
777
Chris Wilson4ff4b442017-06-16 15:05:16 +0100778static struct i915_vma *
Chris Wilson2889caa2017-06-16 15:05:19 +0100779eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000780{
Chris Wilson2889caa2017-06-16 15:05:19 +0100781 if (eb->lut_size < 0) {
782 if (handle >= -eb->lut_size)
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000783 return NULL;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100784 return eb->vma[handle];
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000785 } else {
786 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800787 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000788
Chris Wilson2889caa2017-06-16 15:05:19 +0100789 head = &eb->buckets[hash_32(handle, eb->lut_size)];
Geliang Tangaa459502016-01-18 23:54:20 +0800790 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200791 if (vma->exec_handle == handle)
792 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000793 }
794 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000795 }
Chris Wilson67731b82010-12-08 10:38:14 +0000796}
797
Chris Wilson2889caa2017-06-16 15:05:19 +0100798static void eb_release_vmas(const struct i915_execbuffer *eb)
Chris Wilsona415d352013-11-26 11:23:15 +0000799{
Chris Wilson2889caa2017-06-16 15:05:19 +0100800 const unsigned int count = eb->buffer_count;
801 unsigned int i;
Chris Wilson650bc632017-06-15 09:14:33 +0100802
Chris Wilson2889caa2017-06-16 15:05:19 +0100803 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100804 struct i915_vma *vma = eb->vma[i];
805 unsigned int flags = eb->flags[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100806
807 if (!vma)
Chris Wilson170fa292017-08-16 09:52:07 +0100808 break;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000809
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100810 GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
811 vma->exec_flags = NULL;
812 eb->vma[i] = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100813
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100814 if (flags & __EXEC_OBJECT_HAS_PIN)
815 __eb_unreserve_vma(vma, flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100816
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100817 if (flags & __EXEC_OBJECT_HAS_REF)
Chris Wilsondade2a62017-06-16 15:05:20 +0100818 i915_vma_put(vma);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000819 }
Chris Wilson2889caa2017-06-16 15:05:19 +0100820}
Chris Wilsond55495b2017-06-15 09:14:34 +0100821
Chris Wilson2889caa2017-06-16 15:05:19 +0100822static void eb_reset_vmas(const struct i915_execbuffer *eb)
823{
824 eb_release_vmas(eb);
Chris Wilson4d470f72017-06-29 16:04:25 +0100825 if (eb->lut_size > 0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100826 memset(eb->buckets, 0,
827 sizeof(struct hlist_head) << eb->lut_size);
828}
Chris Wilsond55495b2017-06-15 09:14:34 +0100829
Chris Wilson2889caa2017-06-16 15:05:19 +0100830static void eb_destroy(const struct i915_execbuffer *eb)
831{
Chris Wilson7dd4f672017-06-16 15:05:24 +0100832 GEM_BUG_ON(eb->reloc_cache.rq);
833
Chris Wilson4d470f72017-06-29 16:04:25 +0100834 if (eb->lut_size > 0)
Chris Wilsond55495b2017-06-15 09:14:34 +0100835 kfree(eb->buckets);
Chris Wilson67731b82010-12-08 10:38:14 +0000836}
837
Chris Wilson2889caa2017-06-16 15:05:19 +0100838static inline u64
Chris Wilsond50415c2016-08-18 17:16:52 +0100839relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +0100840 const struct i915_vma *target)
Michał Winiarski934acce2015-12-29 18:24:52 +0100841{
Chris Wilson2889caa2017-06-16 15:05:19 +0100842 return gen8_canonical_addr((int)reloc->delta + target->node.start);
Michał Winiarski934acce2015-12-29 18:24:52 +0100843}
844
Chris Wilsond50415c2016-08-18 17:16:52 +0100845static void reloc_cache_init(struct reloc_cache *cache,
846 struct drm_i915_private *i915)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100847{
Chris Wilson31a39202016-08-18 17:16:46 +0100848 cache->page = -1;
Chris Wilsond50415c2016-08-18 17:16:52 +0100849 cache->vaddr = 0;
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200850 /* Must be a variable in the struct to allow GCC to unroll. */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100851 cache->gen = INTEL_GEN(i915);
Chris Wilson2889caa2017-06-16 15:05:19 +0100852 cache->has_llc = HAS_LLC(i915);
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200853 cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
Chris Wilson7dd4f672017-06-16 15:05:24 +0100854 cache->has_fence = cache->gen < 4;
855 cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
Chris Wilsone8cb9092016-08-18 17:16:53 +0100856 cache->node.allocated = false;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100857 cache->rq = NULL;
858 cache->rq_size = 0;
Chris Wilson31a39202016-08-18 17:16:46 +0100859}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100860
Chris Wilsond50415c2016-08-18 17:16:52 +0100861static inline void *unmask_page(unsigned long p)
862{
863 return (void *)(uintptr_t)(p & PAGE_MASK);
864}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100865
Chris Wilsond50415c2016-08-18 17:16:52 +0100866static inline unsigned int unmask_flags(unsigned long p)
867{
868 return p & ~PAGE_MASK;
869}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700870
Chris Wilsond50415c2016-08-18 17:16:52 +0100871#define KMAP 0x4 /* after CLFLUSH_FLAGS */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700872
Chris Wilson650bc632017-06-15 09:14:33 +0100873static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
874{
875 struct drm_i915_private *i915 =
876 container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
877 return &i915->ggtt;
878}
879
Chris Wilson7dd4f672017-06-16 15:05:24 +0100880static void reloc_gpu_flush(struct reloc_cache *cache)
881{
882 GEM_BUG_ON(cache->rq_size >= cache->rq->batch->obj->base.size / sizeof(u32));
883 cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END;
884 i915_gem_object_unpin_map(cache->rq->batch->obj);
885 i915_gem_chipset_flush(cache->rq->i915);
886
887 __i915_add_request(cache->rq, true);
888 cache->rq = NULL;
889}
890
Chris Wilson650bc632017-06-15 09:14:33 +0100891static void reloc_cache_reset(struct reloc_cache *cache)
Chris Wilson31a39202016-08-18 17:16:46 +0100892{
Chris Wilsond50415c2016-08-18 17:16:52 +0100893 void *vaddr;
894
Chris Wilson7dd4f672017-06-16 15:05:24 +0100895 if (cache->rq)
896 reloc_gpu_flush(cache);
897
Chris Wilson31a39202016-08-18 17:16:46 +0100898 if (!cache->vaddr)
899 return;
900
Chris Wilsond50415c2016-08-18 17:16:52 +0100901 vaddr = unmask_page(cache->vaddr);
902 if (cache->vaddr & KMAP) {
903 if (cache->vaddr & CLFLUSH_AFTER)
904 mb();
Chris Wilson31a39202016-08-18 17:16:46 +0100905
Chris Wilsond50415c2016-08-18 17:16:52 +0100906 kunmap_atomic(vaddr);
907 i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
908 } else {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100909 wmb();
Chris Wilsond50415c2016-08-18 17:16:52 +0100910 io_mapping_unmap_atomic((void __iomem *)vaddr);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100911 if (cache->node.allocated) {
Chris Wilson650bc632017-06-15 09:14:33 +0100912 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100913
914 ggtt->base.clear_range(&ggtt->base,
915 cache->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200916 cache->node.size);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100917 drm_mm_remove_node(&cache->node);
918 } else {
919 i915_vma_unpin((struct i915_vma *)cache->node.mm);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700920 }
Chris Wilson31a39202016-08-18 17:16:46 +0100921 }
Chris Wilson650bc632017-06-15 09:14:33 +0100922
923 cache->vaddr = 0;
924 cache->page = -1;
Chris Wilson31a39202016-08-18 17:16:46 +0100925}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700926
Chris Wilson31a39202016-08-18 17:16:46 +0100927static void *reloc_kmap(struct drm_i915_gem_object *obj,
928 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100929 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100930{
Chris Wilsond50415c2016-08-18 17:16:52 +0100931 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100932
Chris Wilsond50415c2016-08-18 17:16:52 +0100933 if (cache->vaddr) {
934 kunmap_atomic(unmask_page(cache->vaddr));
935 } else {
936 unsigned int flushes;
Chris Wilson2889caa2017-06-16 15:05:19 +0100937 int err;
Chris Wilson31a39202016-08-18 17:16:46 +0100938
Chris Wilson2889caa2017-06-16 15:05:19 +0100939 err = i915_gem_obj_prepare_shmem_write(obj, &flushes);
940 if (err)
941 return ERR_PTR(err);
Chris Wilsond50415c2016-08-18 17:16:52 +0100942
943 BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
944 BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
945
946 cache->vaddr = flushes | KMAP;
947 cache->node.mm = (void *)obj;
948 if (flushes)
949 mb();
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700950 }
951
Chris Wilsond50415c2016-08-18 17:16:52 +0100952 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
953 cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100954 cache->page = page;
Chris Wilson31a39202016-08-18 17:16:46 +0100955
Chris Wilsond50415c2016-08-18 17:16:52 +0100956 return vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100957}
958
Chris Wilsond50415c2016-08-18 17:16:52 +0100959static void *reloc_iomap(struct drm_i915_gem_object *obj,
Chris Wilson31a39202016-08-18 17:16:46 +0100960 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100961 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100962{
Chris Wilson650bc632017-06-15 09:14:33 +0100963 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100964 unsigned long offset;
Chris Wilsond50415c2016-08-18 17:16:52 +0100965 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100966
Chris Wilsond50415c2016-08-18 17:16:52 +0100967 if (cache->vaddr) {
Jani Nikula615e5002016-10-04 12:54:13 +0300968 io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
Chris Wilsond50415c2016-08-18 17:16:52 +0100969 } else {
970 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +0100971 int err;
Chris Wilson31a39202016-08-18 17:16:46 +0100972
Chris Wilson2889caa2017-06-16 15:05:19 +0100973 if (use_cpu_reloc(cache, obj))
Chris Wilsond50415c2016-08-18 17:16:52 +0100974 return NULL;
Chris Wilson31a39202016-08-18 17:16:46 +0100975
Chris Wilson2889caa2017-06-16 15:05:19 +0100976 err = i915_gem_object_set_to_gtt_domain(obj, true);
977 if (err)
978 return ERR_PTR(err);
Chris Wilson31a39202016-08-18 17:16:46 +0100979
Chris Wilsond50415c2016-08-18 17:16:52 +0100980 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilson3c755c52017-10-09 09:43:59 +0100981 PIN_MAPPABLE |
982 PIN_NONBLOCK |
983 PIN_NONFAULT);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100984 if (IS_ERR(vma)) {
985 memset(&cache->node, 0, sizeof(cache->node));
Chris Wilson2889caa2017-06-16 15:05:19 +0100986 err = drm_mm_insert_node_in_range
Chris Wilsone8cb9092016-08-18 17:16:53 +0100987 (&ggtt->base.mm, &cache->node,
Chris Wilsonf51455d2017-01-10 14:47:34 +0000988 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
Chris Wilsone8cb9092016-08-18 17:16:53 +0100989 0, ggtt->mappable_end,
Chris Wilson4e64e552017-02-02 21:04:38 +0000990 DRM_MM_INSERT_LOW);
Chris Wilson2889caa2017-06-16 15:05:19 +0100991 if (err) /* no inactive aperture space, use cpu reloc */
Chris Wilsonc92fa4f2016-10-07 07:53:25 +0100992 return NULL;
Chris Wilsone8cb9092016-08-18 17:16:53 +0100993 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +0100994 err = i915_vma_put_fence(vma);
995 if (err) {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100996 i915_vma_unpin(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100997 return ERR_PTR(err);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100998 }
Rafael Barbalho5032d872013-08-21 17:10:51 +0100999
Chris Wilsone8cb9092016-08-18 17:16:53 +01001000 cache->node.start = vma->node.start;
1001 cache->node.mm = (void *)vma;
Chris Wilsond50415c2016-08-18 17:16:52 +01001002 }
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001003 }
1004
Chris Wilsone8cb9092016-08-18 17:16:53 +01001005 offset = cache->node.start;
1006 if (cache->node.allocated) {
Chris Wilsonfc099092016-10-28 15:27:56 +01001007 wmb();
Chris Wilsone8cb9092016-08-18 17:16:53 +01001008 ggtt->base.insert_page(&ggtt->base,
1009 i915_gem_object_get_dma_address(obj, page),
1010 offset, I915_CACHE_NONE, 0);
1011 } else {
1012 offset += page << PAGE_SHIFT;
1013 }
1014
Chris Wilson650bc632017-06-15 09:14:33 +01001015 vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->mappable,
1016 offset);
Chris Wilsond50415c2016-08-18 17:16:52 +01001017 cache->page = page;
1018 cache->vaddr = (unsigned long)vaddr;
1019
1020 return vaddr;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001021}
1022
Chris Wilsond50415c2016-08-18 17:16:52 +01001023static void *reloc_vaddr(struct drm_i915_gem_object *obj,
1024 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001025 unsigned long page)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001026{
Chris Wilsond50415c2016-08-18 17:16:52 +01001027 void *vaddr;
1028
1029 if (cache->page == page) {
1030 vaddr = unmask_page(cache->vaddr);
1031 } else {
1032 vaddr = NULL;
1033 if ((cache->vaddr & KMAP) == 0)
1034 vaddr = reloc_iomap(obj, cache, page);
1035 if (!vaddr)
1036 vaddr = reloc_kmap(obj, cache, page);
1037 }
1038
1039 return vaddr;
1040}
1041
1042static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
1043{
1044 if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
1045 if (flushes & CLFLUSH_BEFORE) {
1046 clflushopt(addr);
1047 mb();
1048 }
1049
1050 *addr = value;
1051
Chris Wilson2889caa2017-06-16 15:05:19 +01001052 /*
1053 * Writes to the same cacheline are serialised by the CPU
Chris Wilsond50415c2016-08-18 17:16:52 +01001054 * (including clflush). On the write path, we only require
1055 * that it hits memory in an orderly fashion and place
1056 * mb barriers at the start and end of the relocation phase
1057 * to ensure ordering of clflush wrt to the system.
1058 */
1059 if (flushes & CLFLUSH_AFTER)
1060 clflushopt(addr);
1061 } else
1062 *addr = value;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001063}
1064
Chris Wilson7dd4f672017-06-16 15:05:24 +01001065static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
1066 struct i915_vma *vma,
1067 unsigned int len)
1068{
1069 struct reloc_cache *cache = &eb->reloc_cache;
1070 struct drm_i915_gem_object *obj;
1071 struct drm_i915_gem_request *rq;
1072 struct i915_vma *batch;
1073 u32 *cmd;
1074 int err;
1075
1076 GEM_BUG_ON(vma->obj->base.write_domain & I915_GEM_DOMAIN_CPU);
1077
1078 obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, PAGE_SIZE);
1079 if (IS_ERR(obj))
1080 return PTR_ERR(obj);
1081
1082 cmd = i915_gem_object_pin_map(obj,
Chris Wilsona575c672017-08-28 11:46:31 +01001083 cache->has_llc ?
1084 I915_MAP_FORCE_WB :
1085 I915_MAP_FORCE_WC);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001086 i915_gem_object_unpin_pages(obj);
1087 if (IS_ERR(cmd))
1088 return PTR_ERR(cmd);
1089
1090 err = i915_gem_object_set_to_wc_domain(obj, false);
1091 if (err)
1092 goto err_unmap;
1093
1094 batch = i915_vma_instance(obj, vma->vm, NULL);
1095 if (IS_ERR(batch)) {
1096 err = PTR_ERR(batch);
1097 goto err_unmap;
1098 }
1099
1100 err = i915_vma_pin(batch, 0, 0, PIN_USER | PIN_NONBLOCK);
1101 if (err)
1102 goto err_unmap;
1103
1104 rq = i915_gem_request_alloc(eb->engine, eb->ctx);
1105 if (IS_ERR(rq)) {
1106 err = PTR_ERR(rq);
1107 goto err_unpin;
1108 }
1109
1110 err = i915_gem_request_await_object(rq, vma->obj, true);
1111 if (err)
1112 goto err_request;
1113
Chris Wilson7dd4f672017-06-16 15:05:24 +01001114 err = eb->engine->emit_bb_start(rq,
1115 batch->node.start, PAGE_SIZE,
1116 cache->gen > 5 ? 0 : I915_DISPATCH_SECURE);
1117 if (err)
1118 goto err_request;
1119
Chris Wilson95ff7c72017-06-16 15:05:25 +01001120 GEM_BUG_ON(!reservation_object_test_signaled_rcu(batch->resv, true));
Chris Wilson7dd4f672017-06-16 15:05:24 +01001121 i915_vma_move_to_active(batch, rq, 0);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001122 reservation_object_lock(batch->resv, NULL);
1123 reservation_object_add_excl_fence(batch->resv, &rq->fence);
1124 reservation_object_unlock(batch->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001125 i915_vma_unpin(batch);
1126
Chris Wilson25ffaa62017-06-20 13:43:20 +01001127 i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001128 reservation_object_lock(vma->resv, NULL);
1129 reservation_object_add_excl_fence(vma->resv, &rq->fence);
1130 reservation_object_unlock(vma->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001131
1132 rq->batch = batch;
1133
1134 cache->rq = rq;
1135 cache->rq_cmd = cmd;
1136 cache->rq_size = 0;
1137
1138 /* Return with batch mapping (cmd) still pinned */
1139 return 0;
1140
1141err_request:
1142 i915_add_request(rq);
1143err_unpin:
1144 i915_vma_unpin(batch);
1145err_unmap:
1146 i915_gem_object_unpin_map(obj);
1147 return err;
1148}
1149
1150static u32 *reloc_gpu(struct i915_execbuffer *eb,
1151 struct i915_vma *vma,
1152 unsigned int len)
1153{
1154 struct reloc_cache *cache = &eb->reloc_cache;
1155 u32 *cmd;
1156
1157 if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1))
1158 reloc_gpu_flush(cache);
1159
1160 if (unlikely(!cache->rq)) {
1161 int err;
1162
Chris Wilson3dbf26e2017-08-26 14:56:20 +01001163 /* If we need to copy for the cmdparser, we will stall anyway */
1164 if (eb_use_cmdparser(eb))
1165 return ERR_PTR(-EWOULDBLOCK);
1166
Chris Wilson90cad092017-09-06 16:28:59 +01001167 if (!intel_engine_can_store_dword(eb->engine))
1168 return ERR_PTR(-ENODEV);
1169
Chris Wilson7dd4f672017-06-16 15:05:24 +01001170 err = __reloc_gpu_alloc(eb, vma, len);
1171 if (unlikely(err))
1172 return ERR_PTR(err);
1173 }
1174
1175 cmd = cache->rq_cmd + cache->rq_size;
1176 cache->rq_size += len;
1177
1178 return cmd;
1179}
1180
Chris Wilson2889caa2017-06-16 15:05:19 +01001181static u64
1182relocate_entry(struct i915_vma *vma,
Chris Wilsond50415c2016-08-18 17:16:52 +01001183 const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +01001184 struct i915_execbuffer *eb,
1185 const struct i915_vma *target)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001186{
Chris Wilsond50415c2016-08-18 17:16:52 +01001187 u64 offset = reloc->offset;
Chris Wilson2889caa2017-06-16 15:05:19 +01001188 u64 target_offset = relocation_target(reloc, target);
1189 bool wide = eb->reloc_cache.use_64bit_reloc;
Chris Wilsond50415c2016-08-18 17:16:52 +01001190 void *vaddr;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001191
Chris Wilson7dd4f672017-06-16 15:05:24 +01001192 if (!eb->reloc_cache.vaddr &&
1193 (DBG_FORCE_RELOC == FORCE_GPU_RELOC ||
Chris Wilson90cad092017-09-06 16:28:59 +01001194 !reservation_object_test_signaled_rcu(vma->resv, true))) {
Chris Wilson7dd4f672017-06-16 15:05:24 +01001195 const unsigned int gen = eb->reloc_cache.gen;
1196 unsigned int len;
1197 u32 *batch;
1198 u64 addr;
1199
1200 if (wide)
1201 len = offset & 7 ? 8 : 5;
1202 else if (gen >= 4)
1203 len = 4;
Chris Wilsonf2f5c062017-08-16 09:52:04 +01001204 else
Chris Wilson7dd4f672017-06-16 15:05:24 +01001205 len = 3;
Chris Wilson7dd4f672017-06-16 15:05:24 +01001206
1207 batch = reloc_gpu(eb, vma, len);
1208 if (IS_ERR(batch))
1209 goto repeat;
1210
1211 addr = gen8_canonical_addr(vma->node.start + offset);
1212 if (wide) {
1213 if (offset & 7) {
1214 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1215 *batch++ = lower_32_bits(addr);
1216 *batch++ = upper_32_bits(addr);
1217 *batch++ = lower_32_bits(target_offset);
1218
1219 addr = gen8_canonical_addr(addr + 4);
1220
1221 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1222 *batch++ = lower_32_bits(addr);
1223 *batch++ = upper_32_bits(addr);
1224 *batch++ = upper_32_bits(target_offset);
1225 } else {
1226 *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1;
1227 *batch++ = lower_32_bits(addr);
1228 *batch++ = upper_32_bits(addr);
1229 *batch++ = lower_32_bits(target_offset);
1230 *batch++ = upper_32_bits(target_offset);
1231 }
1232 } else if (gen >= 6) {
1233 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1234 *batch++ = 0;
1235 *batch++ = addr;
1236 *batch++ = target_offset;
1237 } else if (gen >= 4) {
1238 *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1239 *batch++ = 0;
1240 *batch++ = addr;
1241 *batch++ = target_offset;
1242 } else {
1243 *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
1244 *batch++ = addr;
1245 *batch++ = target_offset;
1246 }
1247
1248 goto out;
1249 }
1250
Chris Wilsond50415c2016-08-18 17:16:52 +01001251repeat:
Chris Wilson95ff7c72017-06-16 15:05:25 +01001252 vaddr = reloc_vaddr(vma->obj, &eb->reloc_cache, offset >> PAGE_SHIFT);
Chris Wilsond50415c2016-08-18 17:16:52 +01001253 if (IS_ERR(vaddr))
1254 return PTR_ERR(vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001255
Chris Wilsond50415c2016-08-18 17:16:52 +01001256 clflush_write32(vaddr + offset_in_page(offset),
1257 lower_32_bits(target_offset),
Chris Wilson2889caa2017-06-16 15:05:19 +01001258 eb->reloc_cache.vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001259
Chris Wilsond50415c2016-08-18 17:16:52 +01001260 if (wide) {
1261 offset += sizeof(u32);
1262 target_offset >>= 32;
1263 wide = false;
1264 goto repeat;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001265 }
Chris Wilsondabdfe02012-03-26 10:10:27 +02001266
Chris Wilson7dd4f672017-06-16 15:05:24 +01001267out:
Chris Wilson2889caa2017-06-16 15:05:19 +01001268 return target->node.start | UPDATE;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001269}
1270
Chris Wilson2889caa2017-06-16 15:05:19 +01001271static u64
1272eb_relocate_entry(struct i915_execbuffer *eb,
1273 struct i915_vma *vma,
1274 const struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001275{
Chris Wilson507d9772017-06-16 15:05:17 +01001276 struct i915_vma *target;
Chris Wilson2889caa2017-06-16 15:05:19 +01001277 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001278
Chris Wilson67731b82010-12-08 10:38:14 +00001279 /* we've already hold a reference to all valid objects */
Chris Wilson507d9772017-06-16 15:05:17 +01001280 target = eb_get_vma(eb, reloc->target_handle);
1281 if (unlikely(!target))
Chris Wilson54cf91d2010-11-25 18:00:26 +00001282 return -ENOENT;
Eric Anholte844b992012-07-31 15:35:01 -07001283
Chris Wilson54cf91d2010-11-25 18:00:26 +00001284 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001285 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001286 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001287 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001288 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001289 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001290 (int) reloc->offset,
1291 reloc->read_domains,
1292 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001293 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001294 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +01001295 if (unlikely((reloc->write_domain | reloc->read_domains)
1296 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001297 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001298 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001299 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001300 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001301 (int) reloc->offset,
1302 reloc->read_domains,
1303 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001304 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001305 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001306
Chris Wilson2889caa2017-06-16 15:05:19 +01001307 if (reloc->write_domain) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001308 *target->exec_flags |= EXEC_OBJECT_WRITE;
Chris Wilson507d9772017-06-16 15:05:17 +01001309
Chris Wilson2889caa2017-06-16 15:05:19 +01001310 /*
1311 * Sandybridge PPGTT errata: We need a global gtt mapping
1312 * for MI and pipe_control writes because the gpu doesn't
1313 * properly redirect them through the ppgtt for non_secure
1314 * batchbuffers.
1315 */
1316 if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
1317 IS_GEN6(eb->i915)) {
1318 err = i915_vma_bind(target, target->obj->cache_level,
1319 PIN_GLOBAL);
1320 if (WARN_ONCE(err,
1321 "Unexpected failure to bind target VMA!"))
1322 return err;
1323 }
Chris Wilson507d9772017-06-16 15:05:17 +01001324 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001325
Chris Wilson2889caa2017-06-16 15:05:19 +01001326 /*
1327 * If the relocation already has the right value in it, no
Chris Wilson54cf91d2010-11-25 18:00:26 +00001328 * more work needs to be done.
1329 */
Chris Wilson7dd4f672017-06-16 15:05:24 +01001330 if (!DBG_FORCE_RELOC &&
1331 gen8_canonical_addr(target->node.start) == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +00001332 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001333
1334 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001335 if (unlikely(reloc->offset >
Chris Wilson507d9772017-06-16 15:05:17 +01001336 vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001337 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson507d9772017-06-16 15:05:17 +01001338 "target %d offset %d size %d.\n",
1339 reloc->target_handle,
1340 (int)reloc->offset,
1341 (int)vma->size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001342 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001343 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001344 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001345 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson507d9772017-06-16 15:05:17 +01001346 "target %d offset %d.\n",
1347 reloc->target_handle,
1348 (int)reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001349 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001350 }
1351
Chris Wilson071750e2017-06-16 15:05:18 +01001352 /*
1353 * If we write into the object, we need to force the synchronisation
1354 * barrier, either with an asynchronous clflush or if we executed the
1355 * patching using the GPU (though that should be serialised by the
1356 * timeline). To be completely sure, and since we are required to
1357 * do relocations we are already stalling, disable the user's opt
Chris Wilson0519bcb2017-08-16 09:52:09 +01001358 * out of our synchronisation.
Chris Wilson071750e2017-06-16 15:05:18 +01001359 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001360 *vma->exec_flags &= ~EXEC_OBJECT_ASYNC;
Chris Wilson071750e2017-06-16 15:05:18 +01001361
Chris Wilson54cf91d2010-11-25 18:00:26 +00001362 /* and update the user's relocation entry */
Chris Wilson2889caa2017-06-16 15:05:19 +01001363 return relocate_entry(vma, reloc, eb, target);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001364}
1365
Chris Wilson2889caa2017-06-16 15:05:19 +01001366static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001367{
Chris Wilson1d83f442012-03-24 20:12:53 +00001368#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
Chris Wilson2889caa2017-06-16 15:05:19 +01001369 struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
1370 struct drm_i915_gem_relocation_entry __user *urelocs;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001371 const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001372 unsigned int remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001373
Chris Wilson2889caa2017-06-16 15:05:19 +01001374 urelocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson1d83f442012-03-24 20:12:53 +00001375 remain = entry->relocation_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01001376 if (unlikely(remain > N_RELOC(ULONG_MAX)))
1377 return -EINVAL;
Chris Wilsonebc08082016-10-18 13:02:51 +01001378
Chris Wilson2889caa2017-06-16 15:05:19 +01001379 /*
1380 * We must check that the entire relocation array is safe
1381 * to read. However, if the array is not writable the user loses
1382 * the updated relocation values.
1383 */
Imre Deakedd90032017-07-14 18:12:42 +03001384 if (unlikely(!access_ok(VERIFY_READ, urelocs, remain*sizeof(*urelocs))))
Chris Wilson2889caa2017-06-16 15:05:19 +01001385 return -EFAULT;
Chris Wilson1d83f442012-03-24 20:12:53 +00001386
Chris Wilson2889caa2017-06-16 15:05:19 +01001387 do {
1388 struct drm_i915_gem_relocation_entry *r = stack;
1389 unsigned int count =
1390 min_t(unsigned int, remain, ARRAY_SIZE(stack));
1391 unsigned int copied;
1392
1393 /*
1394 * This is the fast path and we cannot handle a pagefault
Chris Wilsonebc08082016-10-18 13:02:51 +01001395 * whilst holding the struct mutex lest the user pass in the
1396 * relocations contained within a mmaped bo. For in such a case
1397 * we, the page fault handler would call i915_gem_fault() and
1398 * we would try to acquire the struct mutex again. Obviously
1399 * this is bad and so lockdep complains vehemently.
1400 */
1401 pagefault_disable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001402 copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
Chris Wilsonebc08082016-10-18 13:02:51 +01001403 pagefault_enable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001404 if (unlikely(copied)) {
1405 remain = -EFAULT;
Chris Wilson31a39202016-08-18 17:16:46 +01001406 goto out;
1407 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001408
Chris Wilson2889caa2017-06-16 15:05:19 +01001409 remain -= count;
Chris Wilson1d83f442012-03-24 20:12:53 +00001410 do {
Chris Wilson2889caa2017-06-16 15:05:19 +01001411 u64 offset = eb_relocate_entry(eb, vma, r);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001412
Chris Wilson2889caa2017-06-16 15:05:19 +01001413 if (likely(offset == 0)) {
1414 } else if ((s64)offset < 0) {
1415 remain = (int)offset;
Chris Wilson31a39202016-08-18 17:16:46 +01001416 goto out;
Chris Wilson2889caa2017-06-16 15:05:19 +01001417 } else {
1418 /*
1419 * Note that reporting an error now
1420 * leaves everything in an inconsistent
1421 * state as we have *already* changed
1422 * the relocation value inside the
1423 * object. As we have not changed the
1424 * reloc.presumed_offset or will not
1425 * change the execobject.offset, on the
1426 * call we may not rewrite the value
1427 * inside the object, leaving it
1428 * dangling and causing a GPU hang. Unless
1429 * userspace dynamically rebuilds the
1430 * relocations on each execbuf rather than
1431 * presume a static tree.
1432 *
1433 * We did previously check if the relocations
1434 * were writable (access_ok), an error now
1435 * would be a strange race with mprotect,
1436 * having already demonstrated that we
1437 * can read from this userspace address.
1438 */
1439 offset = gen8_canonical_addr(offset & ~UPDATE);
1440 __put_user(offset,
1441 &urelocs[r-stack].presumed_offset);
Chris Wilson1d83f442012-03-24 20:12:53 +00001442 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001443 } while (r++, --count);
1444 urelocs += ARRAY_SIZE(stack);
1445 } while (remain);
Chris Wilson31a39202016-08-18 17:16:46 +01001446out:
Chris Wilson650bc632017-06-15 09:14:33 +01001447 reloc_cache_reset(&eb->reloc_cache);
Chris Wilson2889caa2017-06-16 15:05:19 +01001448 return remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001449}
1450
1451static int
Chris Wilson2889caa2017-06-16 15:05:19 +01001452eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001453{
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001454 const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001455 struct drm_i915_gem_relocation_entry *relocs =
1456 u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1457 unsigned int i;
1458 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001459
1460 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001461 u64 offset = eb_relocate_entry(eb, vma, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001462
Chris Wilson2889caa2017-06-16 15:05:19 +01001463 if ((s64)offset < 0) {
1464 err = (int)offset;
1465 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001466 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001467 }
1468 err = 0;
Chris Wilsona415d352013-11-26 11:23:15 +00001469err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001470 reloc_cache_reset(&eb->reloc_cache);
1471 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001472}
1473
Chris Wilson2889caa2017-06-16 15:05:19 +01001474static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001475{
Chris Wilson2889caa2017-06-16 15:05:19 +01001476 const char __user *addr, *end;
1477 unsigned long size;
1478 char __maybe_unused c;
Ben Widawsky27173f12013-08-14 11:38:36 +02001479
Chris Wilson2889caa2017-06-16 15:05:19 +01001480 size = entry->relocation_count;
1481 if (size == 0)
1482 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001483
Chris Wilson2889caa2017-06-16 15:05:19 +01001484 if (size > N_RELOC(ULONG_MAX))
1485 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001486
Chris Wilson2889caa2017-06-16 15:05:19 +01001487 addr = u64_to_user_ptr(entry->relocs_ptr);
1488 size *= sizeof(struct drm_i915_gem_relocation_entry);
1489 if (!access_ok(VERIFY_READ, addr, size))
1490 return -EFAULT;
1491
1492 end = addr + size;
1493 for (; addr < end; addr += PAGE_SIZE) {
1494 int err = __get_user(c, addr);
1495 if (err)
1496 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001497 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001498 return __get_user(c, end - 1);
1499}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001500
Chris Wilson2889caa2017-06-16 15:05:19 +01001501static int eb_copy_relocations(const struct i915_execbuffer *eb)
1502{
1503 const unsigned int count = eb->buffer_count;
1504 unsigned int i;
1505 int err;
1506
Chris Wilson54cf91d2010-11-25 18:00:26 +00001507 for (i = 0; i < count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001508 const unsigned int nreloc = eb->exec[i].relocation_count;
1509 struct drm_i915_gem_relocation_entry __user *urelocs;
1510 struct drm_i915_gem_relocation_entry *relocs;
1511 unsigned long size;
1512 unsigned long copied;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001513
Chris Wilson2889caa2017-06-16 15:05:19 +01001514 if (nreloc == 0)
1515 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001516
Chris Wilson2889caa2017-06-16 15:05:19 +01001517 err = check_relocations(&eb->exec[i]);
1518 if (err)
1519 goto err;
1520
1521 urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
1522 size = nreloc * sizeof(*relocs);
1523
Michal Hocko0ee931c2017-09-13 16:28:29 -07001524 relocs = kvmalloc_array(size, 1, GFP_KERNEL);
Chris Wilson2889caa2017-06-16 15:05:19 +01001525 if (!relocs) {
1526 kvfree(relocs);
1527 err = -ENOMEM;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001528 goto err;
1529 }
1530
Chris Wilson2889caa2017-06-16 15:05:19 +01001531 /* copy_from_user is limited to < 4GiB */
1532 copied = 0;
1533 do {
1534 unsigned int len =
1535 min_t(u64, BIT_ULL(31), size - copied);
1536
1537 if (__copy_from_user((char *)relocs + copied,
Ville Syrjälä908a6102017-09-01 19:54:34 +03001538 (char __user *)urelocs + copied,
Chris Wilson2889caa2017-06-16 15:05:19 +01001539 len)) {
1540 kvfree(relocs);
1541 err = -EFAULT;
1542 goto err;
1543 }
1544
1545 copied += len;
1546 } while (copied < size);
1547
1548 /*
1549 * As we do not update the known relocation offsets after
Chris Wilson262b6d32013-01-15 16:17:54 +00001550 * relocating (due to the complexities in lock handling),
1551 * we need to mark them as invalid now so that we force the
1552 * relocation processing next time. Just in case the target
1553 * object is evicted and then rebound into its old
1554 * presumed_offset before the next execbuffer - if that
1555 * happened we would make the mistake of assuming that the
1556 * relocations were valid.
1557 */
Chris Wilson2889caa2017-06-16 15:05:19 +01001558 user_access_begin();
1559 for (copied = 0; copied < nreloc; copied++)
1560 unsafe_put_user(-1,
1561 &urelocs[copied].presumed_offset,
1562 end_user);
1563end_user:
1564 user_access_end();
Chris Wilson262b6d32013-01-15 16:17:54 +00001565
Chris Wilson2889caa2017-06-16 15:05:19 +01001566 eb->exec[i].relocs_ptr = (uintptr_t)relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001567 }
1568
Chris Wilson2889caa2017-06-16 15:05:19 +01001569 return 0;
1570
1571err:
1572 while (i--) {
1573 struct drm_i915_gem_relocation_entry *relocs =
1574 u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr);
1575 if (eb->exec[i].relocation_count)
1576 kvfree(relocs);
1577 }
1578 return err;
1579}
1580
1581static int eb_prefault_relocations(const struct i915_execbuffer *eb)
1582{
1583 const unsigned int count = eb->buffer_count;
1584 unsigned int i;
1585
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001586 if (unlikely(i915_modparams.prefault_disable))
Chris Wilson2889caa2017-06-16 15:05:19 +01001587 return 0;
1588
1589 for (i = 0; i < count; i++) {
1590 int err;
1591
1592 err = check_relocations(&eb->exec[i]);
1593 if (err)
1594 return err;
1595 }
1596
1597 return 0;
1598}
1599
1600static noinline int eb_relocate_slow(struct i915_execbuffer *eb)
1601{
1602 struct drm_device *dev = &eb->i915->drm;
1603 bool have_copy = false;
1604 struct i915_vma *vma;
1605 int err = 0;
1606
1607repeat:
1608 if (signal_pending(current)) {
1609 err = -ERESTARTSYS;
1610 goto out;
1611 }
1612
1613 /* We may process another execbuffer during the unlock... */
1614 eb_reset_vmas(eb);
1615 mutex_unlock(&dev->struct_mutex);
1616
1617 /*
1618 * We take 3 passes through the slowpatch.
1619 *
1620 * 1 - we try to just prefault all the user relocation entries and
1621 * then attempt to reuse the atomic pagefault disabled fast path again.
1622 *
1623 * 2 - we copy the user entries to a local buffer here outside of the
1624 * local and allow ourselves to wait upon any rendering before
1625 * relocations
1626 *
1627 * 3 - we already have a local copy of the relocation entries, but
1628 * were interrupted (EAGAIN) whilst waiting for the objects, try again.
1629 */
1630 if (!err) {
1631 err = eb_prefault_relocations(eb);
1632 } else if (!have_copy) {
1633 err = eb_copy_relocations(eb);
1634 have_copy = err == 0;
1635 } else {
1636 cond_resched();
1637 err = 0;
1638 }
1639 if (err) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001640 mutex_lock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01001641 goto out;
1642 }
1643
Chris Wilson8a2421b2017-06-16 15:05:22 +01001644 /* A frequent cause for EAGAIN are currently unavailable client pages */
1645 flush_workqueue(eb->i915->mm.userptr_wq);
1646
Chris Wilson2889caa2017-06-16 15:05:19 +01001647 err = i915_mutex_lock_interruptible(dev);
1648 if (err) {
1649 mutex_lock(&dev->struct_mutex);
1650 goto out;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001651 }
1652
Chris Wilson67731b82010-12-08 10:38:14 +00001653 /* reacquire the objects */
Chris Wilson2889caa2017-06-16 15:05:19 +01001654 err = eb_lookup_vmas(eb);
1655 if (err)
Chris Wilson3b96eff2013-01-08 10:53:14 +00001656 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +00001657
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001658 GEM_BUG_ON(!eb->batch);
1659
Chris Wilson2889caa2017-06-16 15:05:19 +01001660 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1661 if (!have_copy) {
1662 pagefault_disable();
1663 err = eb_relocate_vma(eb, vma);
1664 pagefault_enable();
1665 if (err)
1666 goto repeat;
1667 } else {
1668 err = eb_relocate_vma_slow(eb, vma);
1669 if (err)
1670 goto err;
1671 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001672 }
1673
Chris Wilson2889caa2017-06-16 15:05:19 +01001674 /*
1675 * Leave the user relocations as are, this is the painfully slow path,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001676 * and we want to avoid the complication of dropping the lock whilst
1677 * having buffers reserved in the aperture and so causing spurious
1678 * ENOSPC for random operations.
1679 */
1680
1681err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001682 if (err == -EAGAIN)
1683 goto repeat;
1684
1685out:
1686 if (have_copy) {
1687 const unsigned int count = eb->buffer_count;
1688 unsigned int i;
1689
1690 for (i = 0; i < count; i++) {
1691 const struct drm_i915_gem_exec_object2 *entry =
1692 &eb->exec[i];
1693 struct drm_i915_gem_relocation_entry *relocs;
1694
1695 if (!entry->relocation_count)
1696 continue;
1697
1698 relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1699 kvfree(relocs);
1700 }
1701 }
1702
Chris Wilson1f727d92017-07-21 15:50:36 +01001703 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001704}
1705
Chris Wilson2889caa2017-06-16 15:05:19 +01001706static int eb_relocate(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001707{
Chris Wilson2889caa2017-06-16 15:05:19 +01001708 if (eb_lookup_vmas(eb))
1709 goto slow;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001710
Chris Wilson2889caa2017-06-16 15:05:19 +01001711 /* The objects are in their final locations, apply the relocations. */
1712 if (eb->args->flags & __EXEC_HAS_RELOC) {
1713 struct i915_vma *vma;
1714
1715 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1716 if (eb_relocate_vma(eb, vma))
1717 goto slow;
1718 }
1719 }
1720
1721 return 0;
1722
1723slow:
1724 return eb_relocate_slow(eb);
1725}
1726
Chris Wilson95ff7c72017-06-16 15:05:25 +01001727static void eb_export_fence(struct i915_vma *vma,
Chris Wilson2889caa2017-06-16 15:05:19 +01001728 struct drm_i915_gem_request *req,
1729 unsigned int flags)
1730{
Chris Wilson95ff7c72017-06-16 15:05:25 +01001731 struct reservation_object *resv = vma->resv;
Chris Wilson2889caa2017-06-16 15:05:19 +01001732
1733 /*
1734 * Ignore errors from failing to allocate the new fence, we can't
1735 * handle an error right now. Worst case should be missed
1736 * synchronisation leading to rendering corruption.
1737 */
1738 reservation_object_lock(resv, NULL);
1739 if (flags & EXEC_OBJECT_WRITE)
1740 reservation_object_add_excl_fence(resv, &req->fence);
1741 else if (reservation_object_reserve_shared(resv) == 0)
1742 reservation_object_add_shared_fence(resv, &req->fence);
1743 reservation_object_unlock(resv);
1744}
1745
1746static int eb_move_to_gpu(struct i915_execbuffer *eb)
1747{
1748 const unsigned int count = eb->buffer_count;
1749 unsigned int i;
1750 int err;
1751
1752 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001753 unsigned int flags = eb->flags[i];
1754 struct i915_vma *vma = eb->vma[i];
Ben Widawsky27173f12013-08-14 11:38:36 +02001755 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001756
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001757 if (flags & EXEC_OBJECT_CAPTURE) {
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001758 struct i915_gem_capture_list *capture;
1759
1760 capture = kmalloc(sizeof(*capture), GFP_KERNEL);
1761 if (unlikely(!capture))
1762 return -ENOMEM;
1763
Chris Wilson650bc632017-06-15 09:14:33 +01001764 capture->next = eb->request->capture_list;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001765 capture->vma = eb->vma[i];
Chris Wilson650bc632017-06-15 09:14:33 +01001766 eb->request->capture_list = capture;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001767 }
1768
Chris Wilsonb8f55be2017-08-11 12:11:16 +01001769 /*
1770 * If the GPU is not _reading_ through the CPU cache, we need
1771 * to make sure that any writes (both previous GPU writes from
1772 * before a change in snooping levels and normal CPU writes)
1773 * caught in that cache are flushed to main memory.
1774 *
1775 * We want to say
1776 * obj->cache_dirty &&
1777 * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
1778 * but gcc's optimiser doesn't handle that as well and emits
1779 * two jumps instead of one. Maybe one day...
1780 */
1781 if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) {
Chris Wilson0f46daa2017-07-21 15:50:37 +01001782 if (i915_gem_clflush_object(obj, 0))
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001783 flags &= ~EXEC_OBJECT_ASYNC;
Chris Wilson0f46daa2017-07-21 15:50:37 +01001784 }
1785
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001786 if (flags & EXEC_OBJECT_ASYNC)
1787 continue;
Chris Wilson77ae9952017-01-27 09:40:07 +00001788
Chris Wilson2889caa2017-06-16 15:05:19 +01001789 err = i915_gem_request_await_object
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001790 (eb->request, obj, flags & EXEC_OBJECT_WRITE);
Chris Wilson2889caa2017-06-16 15:05:19 +01001791 if (err)
1792 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001793 }
1794
Chris Wilson2889caa2017-06-16 15:05:19 +01001795 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001796 unsigned int flags = eb->flags[i];
1797 struct i915_vma *vma = eb->vma[i];
Chris Wilson2889caa2017-06-16 15:05:19 +01001798
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001799 i915_vma_move_to_active(vma, eb->request, flags);
1800 eb_export_fence(vma, eb->request, flags);
1801
1802 __eb_unreserve_vma(vma, flags);
1803 vma->exec_flags = NULL;
1804
1805 if (unlikely(flags & __EXEC_OBJECT_HAS_REF))
Chris Wilsondade2a62017-06-16 15:05:20 +01001806 i915_vma_put(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001807 }
1808 eb->exec = NULL;
1809
Chris Wilsondcd79932016-08-18 17:16:40 +01001810 /* Unconditionally flush any chipset caches (for streaming writes). */
Chris Wilson650bc632017-06-15 09:14:33 +01001811 i915_gem_chipset_flush(eb->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001812
Chris Wilson21131842017-11-20 10:20:01 +00001813 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001814}
1815
Chris Wilson2889caa2017-06-16 15:05:19 +01001816static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001817{
Chris Wilson650bc632017-06-15 09:14:33 +01001818 if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
Daniel Vettered5982e2013-01-17 22:23:36 +01001819 return false;
1820
Chris Wilson2f5945b2015-10-06 11:39:55 +01001821 /* Kernel clipping was a DRI1 misfeature */
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01001822 if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
1823 if (exec->num_cliprects || exec->cliprects_ptr)
1824 return false;
1825 }
Chris Wilson2f5945b2015-10-06 11:39:55 +01001826
1827 if (exec->DR4 == 0xffffffff) {
1828 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1829 exec->DR4 = 0;
1830 }
1831 if (exec->DR1 || exec->DR4)
1832 return false;
1833
1834 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1835 return false;
1836
1837 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001838}
1839
Chris Wilson5cf3d282016-08-04 07:52:43 +01001840void i915_vma_move_to_active(struct i915_vma *vma,
1841 struct drm_i915_gem_request *req,
1842 unsigned int flags)
1843{
1844 struct drm_i915_gem_object *obj = vma->obj;
1845 const unsigned int idx = req->engine->id;
1846
Chris Wilson81147b02016-12-18 15:37:18 +00001847 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001848 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1849
Chris Wilson2889caa2017-06-16 15:05:19 +01001850 /*
1851 * Add a reference if we're newly entering the active list.
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001852 * The order in which we add operations to the retirement queue is
1853 * vital here: mark_active adds to the start of the callback list,
1854 * such that subsequent callbacks are called first. Therefore we
1855 * add the active reference first and queue for it to be dropped
1856 * *last*.
1857 */
Chris Wilsond07f0e52016-10-28 13:58:44 +01001858 if (!i915_vma_is_active(vma))
1859 obj->active_count++;
1860 i915_vma_set_active(vma, idx);
1861 i915_gem_active_set(&vma->last_read[idx], req);
1862 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001863
Chris Wilsone27ab732017-06-15 13:38:49 +01001864 obj->base.write_domain = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001865 if (flags & EXEC_OBJECT_WRITE) {
Chris Wilsone27ab732017-06-15 13:38:49 +01001866 obj->base.write_domain = I915_GEM_DOMAIN_RENDER;
1867
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00001868 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
1869 i915_gem_active_set(&obj->frontbuffer_write, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001870
Chris Wilsone27ab732017-06-15 13:38:49 +01001871 obj->base.read_domains = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001872 }
Chris Wilsone27ab732017-06-15 13:38:49 +01001873 obj->base.read_domains |= I915_GEM_GPU_DOMAINS;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001874
Chris Wilson49ef5292016-08-18 17:17:00 +01001875 if (flags & EXEC_OBJECT_NEEDS_FENCE)
1876 i915_gem_active_set(&vma->last_fence, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001877}
1878
Chris Wilson2889caa2017-06-16 15:05:19 +01001879static int i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001880{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001881 u32 *cs;
1882 int i;
Eric Anholtae662d32012-01-03 09:23:29 -08001883
Chris Wilsonb5321f32016-08-02 22:50:18 +01001884 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001885 DRM_DEBUG("sol reset is gen7/rcs only\n");
1886 return -EINVAL;
1887 }
Eric Anholtae662d32012-01-03 09:23:29 -08001888
Chris Wilson2889caa2017-06-16 15:05:19 +01001889 cs = intel_ring_begin(req, 4 * 2 + 2);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001890 if (IS_ERR(cs))
1891 return PTR_ERR(cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001892
Chris Wilson2889caa2017-06-16 15:05:19 +01001893 *cs++ = MI_LOAD_REGISTER_IMM(4);
Eric Anholtae662d32012-01-03 09:23:29 -08001894 for (i = 0; i < 4; i++) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001895 *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
1896 *cs++ = 0;
Eric Anholtae662d32012-01-03 09:23:29 -08001897 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001898 *cs++ = MI_NOOP;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001899 intel_ring_advance(req, cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001900
1901 return 0;
1902}
1903
Chris Wilson650bc632017-06-15 09:14:33 +01001904static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001905{
Brad Volkin71745372014-12-11 12:13:12 -08001906 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001907 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001908 int err;
Brad Volkin71745372014-12-11 12:13:12 -08001909
Chris Wilson650bc632017-06-15 09:14:33 +01001910 shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool,
1911 PAGE_ALIGN(eb->batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001912 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001913 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001914
Chris Wilson2889caa2017-06-16 15:05:19 +01001915 err = intel_engine_cmd_parser(eb->engine,
Chris Wilson650bc632017-06-15 09:14:33 +01001916 eb->batch->obj,
Chris Wilson33a051a2016-07-27 09:07:26 +01001917 shadow_batch_obj,
Chris Wilson650bc632017-06-15 09:14:33 +01001918 eb->batch_start_offset,
1919 eb->batch_len,
Chris Wilson33a051a2016-07-27 09:07:26 +01001920 is_master);
Chris Wilson2889caa2017-06-16 15:05:19 +01001921 if (err) {
1922 if (err == -EACCES) /* unhandled chained batch */
Chris Wilson058d88c2016-08-15 10:49:06 +01001923 vma = NULL;
1924 else
Chris Wilson2889caa2017-06-16 15:05:19 +01001925 vma = ERR_PTR(err);
Chris Wilson058d88c2016-08-15 10:49:06 +01001926 goto out;
1927 }
Brad Volkin71745372014-12-11 12:13:12 -08001928
Chris Wilson058d88c2016-08-15 10:49:06 +01001929 vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
1930 if (IS_ERR(vma))
1931 goto out;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001932
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001933 eb->vma[eb->buffer_count] = i915_vma_get(vma);
1934 eb->flags[eb->buffer_count] =
1935 __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF;
1936 vma->exec_flags = &eb->flags[eb->buffer_count];
1937 eb->buffer_count++;
Brad Volkin71745372014-12-11 12:13:12 -08001938
Chris Wilson058d88c2016-08-15 10:49:06 +01001939out:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001940 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson058d88c2016-08-15 10:49:06 +01001941 return vma;
Brad Volkin71745372014-12-11 12:13:12 -08001942}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001943
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001944static void
Chris Wilson2889caa2017-06-16 15:05:19 +01001945add_to_client(struct drm_i915_gem_request *req, struct drm_file *file)
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001946{
1947 req->file_priv = file->driver_priv;
1948 list_add_tail(&req->client_link, &req->file_priv->mm.request_list);
1949}
1950
Chris Wilson2889caa2017-06-16 15:05:19 +01001951static int eb_submit(struct i915_execbuffer *eb)
Oscar Mateo78382592014-07-03 16:28:05 +01001952{
Chris Wilson2889caa2017-06-16 15:05:19 +01001953 int err;
Oscar Mateo78382592014-07-03 16:28:05 +01001954
Chris Wilson2889caa2017-06-16 15:05:19 +01001955 err = eb_move_to_gpu(eb);
1956 if (err)
1957 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001958
Chris Wilson650bc632017-06-15 09:14:33 +01001959 if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001960 err = i915_reset_gen7_sol_offsets(eb->request);
1961 if (err)
1962 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001963 }
1964
Chris Wilson2889caa2017-06-16 15:05:19 +01001965 err = eb->engine->emit_bb_start(eb->request,
Chris Wilson650bc632017-06-15 09:14:33 +01001966 eb->batch->node.start +
1967 eb->batch_start_offset,
1968 eb->batch_len,
Chris Wilson2889caa2017-06-16 15:05:19 +01001969 eb->batch_flags);
1970 if (err)
1971 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001972
Chris Wilson2f5945b2015-10-06 11:39:55 +01001973 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001974}
1975
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001976/**
1977 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01001978 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001979 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001980static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01001981gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
1982 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001983{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001984 struct drm_i915_file_private *file_priv = file->driver_priv;
1985
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001986 /* Check whether the file_priv has already selected one ring. */
Joonas Lahtinen6f633402016-09-01 14:58:21 +03001987 if ((int)file_priv->bsd_engine < 0)
1988 file_priv->bsd_engine = atomic_fetch_xor(1,
1989 &dev_priv->mm.bsd_engine_dispatch_index);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001990
Chris Wilsonc80ff162016-07-27 09:07:27 +01001991 return file_priv->bsd_engine;
Chris Wilsond23db882014-05-23 08:48:08 +02001992}
1993
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001994#define I915_USER_RINGS (4)
1995
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00001996static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001997 [I915_EXEC_DEFAULT] = RCS,
1998 [I915_EXEC_RENDER] = RCS,
1999 [I915_EXEC_BLT] = BCS,
2000 [I915_EXEC_BSD] = VCS,
2001 [I915_EXEC_VEBOX] = VECS
2002};
2003
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002004static struct intel_engine_cs *
2005eb_select_engine(struct drm_i915_private *dev_priv,
2006 struct drm_file *file,
2007 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002008{
2009 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002010 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002011
2012 if (user_ring_id > I915_USER_RINGS) {
2013 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002014 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002015 }
2016
2017 if ((user_ring_id != I915_EXEC_BSD) &&
2018 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
2019 DRM_DEBUG("execbuf with non bsd ring but with invalid "
2020 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002021 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002022 }
2023
2024 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
2025 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
2026
2027 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01002028 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002029 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
2030 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00002031 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002032 bsd_idx--;
2033 } else {
2034 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
2035 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002036 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002037 }
2038
Akash Goel3b3f1652016-10-13 22:44:48 +05302039 engine = dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002040 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +05302041 engine = dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002042 }
2043
Akash Goel3b3f1652016-10-13 22:44:48 +05302044 if (!engine) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002045 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002046 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002047 }
2048
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002049 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002050}
2051
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002052static void
2053__free_fence_array(struct drm_syncobj **fences, unsigned int n)
2054{
2055 while (n--)
2056 drm_syncobj_put(ptr_mask_bits(fences[n], 2));
2057 kvfree(fences);
2058}
2059
2060static struct drm_syncobj **
2061get_fence_array(struct drm_i915_gem_execbuffer2 *args,
2062 struct drm_file *file)
2063{
Chris Wilsond710fc12017-11-16 10:50:59 +00002064 const unsigned long nfences = args->num_cliprects;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002065 struct drm_i915_gem_exec_fence __user *user;
2066 struct drm_syncobj **fences;
Chris Wilsond710fc12017-11-16 10:50:59 +00002067 unsigned long n;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002068 int err;
2069
2070 if (!(args->flags & I915_EXEC_FENCE_ARRAY))
2071 return NULL;
2072
Chris Wilsond710fc12017-11-16 10:50:59 +00002073 /* Check multiplication overflow for access_ok() and kvmalloc_array() */
2074 BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long));
2075 if (nfences > min_t(unsigned long,
2076 ULONG_MAX / sizeof(*user),
2077 SIZE_MAX / sizeof(*fences)))
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002078 return ERR_PTR(-EINVAL);
2079
2080 user = u64_to_user_ptr(args->cliprects_ptr);
Chris Wilsond710fc12017-11-16 10:50:59 +00002081 if (!access_ok(VERIFY_READ, user, nfences * sizeof(*user)))
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002082 return ERR_PTR(-EFAULT);
2083
Chris Wilsond710fc12017-11-16 10:50:59 +00002084 fences = kvmalloc_array(nfences, sizeof(*fences),
Michal Hocko0ee931c2017-09-13 16:28:29 -07002085 __GFP_NOWARN | GFP_KERNEL);
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002086 if (!fences)
2087 return ERR_PTR(-ENOMEM);
2088
2089 for (n = 0; n < nfences; n++) {
2090 struct drm_i915_gem_exec_fence fence;
2091 struct drm_syncobj *syncobj;
2092
2093 if (__copy_from_user(&fence, user++, sizeof(fence))) {
2094 err = -EFAULT;
2095 goto err;
2096 }
2097
Tvrtko Ursulinebcaa1f2017-10-31 10:23:25 +00002098 if (fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS) {
2099 err = -EINVAL;
2100 goto err;
2101 }
2102
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002103 syncobj = drm_syncobj_find(file, fence.handle);
2104 if (!syncobj) {
2105 DRM_DEBUG("Invalid syncobj handle provided\n");
2106 err = -ENOENT;
2107 goto err;
2108 }
2109
Tvrtko Ursulinebcaa1f2017-10-31 10:23:25 +00002110 BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) &
2111 ~__I915_EXEC_FENCE_UNKNOWN_FLAGS);
2112
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002113 fences[n] = ptr_pack_bits(syncobj, fence.flags, 2);
2114 }
2115
2116 return fences;
2117
2118err:
2119 __free_fence_array(fences, n);
2120 return ERR_PTR(err);
2121}
2122
2123static void
2124put_fence_array(struct drm_i915_gem_execbuffer2 *args,
2125 struct drm_syncobj **fences)
2126{
2127 if (fences)
2128 __free_fence_array(fences, args->num_cliprects);
2129}
2130
2131static int
2132await_fence_array(struct i915_execbuffer *eb,
2133 struct drm_syncobj **fences)
2134{
2135 const unsigned int nfences = eb->args->num_cliprects;
2136 unsigned int n;
2137 int err;
2138
2139 for (n = 0; n < nfences; n++) {
2140 struct drm_syncobj *syncobj;
2141 struct dma_fence *fence;
2142 unsigned int flags;
2143
2144 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2145 if (!(flags & I915_EXEC_FENCE_WAIT))
2146 continue;
2147
Jason Ekstrandafca4212017-08-25 10:52:21 -07002148 fence = drm_syncobj_fence_get(syncobj);
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002149 if (!fence)
2150 return -EINVAL;
2151
2152 err = i915_gem_request_await_dma_fence(eb->request, fence);
2153 dma_fence_put(fence);
2154 if (err < 0)
2155 return err;
2156 }
2157
2158 return 0;
2159}
2160
2161static void
2162signal_fence_array(struct i915_execbuffer *eb,
2163 struct drm_syncobj **fences)
2164{
2165 const unsigned int nfences = eb->args->num_cliprects;
2166 struct dma_fence * const fence = &eb->request->fence;
2167 unsigned int n;
2168
2169 for (n = 0; n < nfences; n++) {
2170 struct drm_syncobj *syncobj;
2171 unsigned int flags;
2172
2173 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2174 if (!(flags & I915_EXEC_FENCE_SIGNAL))
2175 continue;
2176
2177 drm_syncobj_replace_fence(syncobj, fence);
2178 }
2179}
2180
Eric Anholtae662d32012-01-03 09:23:29 -08002181static int
Chris Wilson650bc632017-06-15 09:14:33 +01002182i915_gem_do_execbuffer(struct drm_device *dev,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002183 struct drm_file *file,
2184 struct drm_i915_gem_execbuffer2 *args,
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002185 struct drm_i915_gem_exec_object2 *exec,
2186 struct drm_syncobj **fences)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002187{
Chris Wilson650bc632017-06-15 09:14:33 +01002188 struct i915_execbuffer eb;
Chris Wilsonfec04452017-01-27 09:40:08 +00002189 struct dma_fence *in_fence = NULL;
2190 struct sync_file *out_fence = NULL;
2191 int out_fence_fd = -1;
Chris Wilson2889caa2017-06-16 15:05:19 +01002192 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002193
Chris Wilson74c1c692017-09-21 12:01:35 +01002194 BUILD_BUG_ON(__EXEC_INTERNAL_FLAGS & ~__I915_EXEC_ILLEGAL_FLAGS);
Chris Wilson2889caa2017-06-16 15:05:19 +01002195 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
2196 ~__EXEC_OBJECT_UNKNOWN_FLAGS);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002197
Chris Wilson650bc632017-06-15 09:14:33 +01002198 eb.i915 = to_i915(dev);
2199 eb.file = file;
2200 eb.args = args;
Chris Wilson7dd4f672017-06-16 15:05:24 +01002201 if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC))
Chris Wilson2889caa2017-06-16 15:05:19 +01002202 args->flags |= __EXEC_HAS_RELOC;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002203
Chris Wilson650bc632017-06-15 09:14:33 +01002204 eb.exec = exec;
Chris Wilson170fa292017-08-16 09:52:07 +01002205 eb.vma = (struct i915_vma **)(exec + args->buffer_count + 1);
2206 eb.vma[0] = NULL;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002207 eb.flags = (unsigned int *)(eb.vma + args->buffer_count + 1);
2208
Chris Wilson2889caa2017-06-16 15:05:19 +01002209 eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
2210 if (USES_FULL_PPGTT(eb.i915))
2211 eb.invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson650bc632017-06-15 09:14:33 +01002212 reloc_cache_init(&eb.reloc_cache, eb.i915);
2213
Chris Wilson2889caa2017-06-16 15:05:19 +01002214 eb.buffer_count = args->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +01002215 eb.batch_start_offset = args->batch_start_offset;
2216 eb.batch_len = args->batch_len;
2217
Chris Wilson2889caa2017-06-16 15:05:19 +01002218 eb.batch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002219 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02002220 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002221 return -EPERM;
2222
Chris Wilson2889caa2017-06-16 15:05:19 +01002223 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002224 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01002225 if (args->flags & I915_EXEC_IS_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +01002226 eb.batch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002227
Chris Wilson650bc632017-06-15 09:14:33 +01002228 eb.engine = eb_select_engine(eb.i915, file, args);
2229 if (!eb.engine)
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002230 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002231
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002232 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
Chris Wilson650bc632017-06-15 09:14:33 +01002233 if (!HAS_RESOURCE_STREAMER(eb.i915)) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002234 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
2235 return -EINVAL;
2236 }
Chris Wilson650bc632017-06-15 09:14:33 +01002237 if (eb.engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002238 DRM_DEBUG("RS is not available on %s\n",
Chris Wilson650bc632017-06-15 09:14:33 +01002239 eb.engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002240 return -EINVAL;
2241 }
2242
Chris Wilson2889caa2017-06-16 15:05:19 +01002243 eb.batch_flags |= I915_DISPATCH_RS;
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002244 }
2245
Chris Wilsonfec04452017-01-27 09:40:08 +00002246 if (args->flags & I915_EXEC_FENCE_IN) {
2247 in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002248 if (!in_fence)
2249 return -EINVAL;
Chris Wilsonfec04452017-01-27 09:40:08 +00002250 }
2251
2252 if (args->flags & I915_EXEC_FENCE_OUT) {
2253 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
2254 if (out_fence_fd < 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002255 err = out_fence_fd;
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002256 goto err_in_fence;
Chris Wilsonfec04452017-01-27 09:40:08 +00002257 }
2258 }
2259
Chris Wilson4d470f72017-06-29 16:04:25 +01002260 err = eb_create(&eb);
2261 if (err)
2262 goto err_out_fence;
2263
2264 GEM_BUG_ON(!eb.lut_size);
Chris Wilson2889caa2017-06-16 15:05:19 +01002265
Chris Wilson1acfc102017-06-20 12:05:47 +01002266 err = eb_select_context(&eb);
2267 if (unlikely(err))
2268 goto err_destroy;
2269
Chris Wilson2889caa2017-06-16 15:05:19 +01002270 /*
2271 * Take a local wakeref for preparing to dispatch the execbuf as
Chris Wilson67d97da2016-07-04 08:08:31 +01002272 * we expect to access the hardware fairly frequently in the
2273 * process. Upon first dispatch, we acquire another prolonged
2274 * wakeref that we hold until the GPU has been idle for at least
2275 * 100ms.
2276 */
Chris Wilson650bc632017-06-15 09:14:33 +01002277 intel_runtime_pm_get(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002278
Chris Wilson2889caa2017-06-16 15:05:19 +01002279 err = i915_mutex_lock_interruptible(dev);
2280 if (err)
2281 goto err_rpm;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002282
Chris Wilson2889caa2017-06-16 15:05:19 +01002283 err = eb_relocate(&eb);
Chris Wilson1f727d92017-07-21 15:50:36 +01002284 if (err) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002285 /*
2286 * If the user expects the execobject.offset and
2287 * reloc.presumed_offset to be an exact match,
2288 * as for using NO_RELOC, then we cannot update
2289 * the execobject.offset until we have completed
2290 * relocation.
2291 */
2292 args->flags &= ~__EXEC_HAS_RELOC;
Chris Wilson2889caa2017-06-16 15:05:19 +01002293 goto err_vma;
Chris Wilson1f727d92017-07-21 15:50:36 +01002294 }
Ben Widawsky41bde552013-12-06 14:11:21 -08002295
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002296 if (unlikely(*eb.batch->exec_flags & EXEC_OBJECT_WRITE)) {
Daniel Vetterff240192012-01-31 21:08:14 +01002297 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002298 err = -EINVAL;
2299 goto err_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002300 }
Chris Wilson650bc632017-06-15 09:14:33 +01002301 if (eb.batch_start_offset > eb.batch->size ||
2302 eb.batch_len > eb.batch->size - eb.batch_start_offset) {
Chris Wilson0b537272016-08-18 17:17:12 +01002303 DRM_DEBUG("Attempting to use out-of-bounds batch\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002304 err = -EINVAL;
2305 goto err_vma;
Chris Wilson0b537272016-08-18 17:17:12 +01002306 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002307
Chris Wilson3dbf26e2017-08-26 14:56:20 +01002308 if (eb_use_cmdparser(&eb)) {
Chris Wilson59bfa122016-08-04 16:32:31 +01002309 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002310
Chris Wilson650bc632017-06-15 09:14:33 +01002311 vma = eb_parse(&eb, drm_is_current_master(file));
Chris Wilson59bfa122016-08-04 16:32:31 +01002312 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002313 err = PTR_ERR(vma);
2314 goto err_vma;
Brad Volkin78a42372014-12-11 12:13:09 -08002315 }
Chris Wilson17cabf52015-01-14 11:20:57 +00002316
Chris Wilson59bfa122016-08-04 16:32:31 +01002317 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002318 /*
2319 * Batch parsed and accepted:
2320 *
2321 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
2322 * bit from MI_BATCH_BUFFER_START commands issued in
2323 * the dispatch_execbuffer implementations. We
2324 * specifically don't want that set on batches the
2325 * command parser has accepted.
2326 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002327 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilson650bc632017-06-15 09:14:33 +01002328 eb.batch_start_offset = 0;
2329 eb.batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002330 }
Brad Volkin351e3db2014-02-18 10:15:46 -08002331 }
2332
Chris Wilson650bc632017-06-15 09:14:33 +01002333 if (eb.batch_len == 0)
2334 eb.batch_len = eb.batch->size - eb.batch_start_offset;
Brad Volkin78a42372014-12-11 12:13:09 -08002335
Chris Wilson2889caa2017-06-16 15:05:19 +01002336 /*
2337 * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002338 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07002339 * hsw should have this fixed, but bdw mucks it up again. */
Chris Wilson2889caa2017-06-16 15:05:19 +01002340 if (eb.batch_flags & I915_DISPATCH_SECURE) {
Chris Wilson058d88c2016-08-15 10:49:06 +01002341 struct i915_vma *vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002342
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002343 /*
2344 * So on first glance it looks freaky that we pin the batch here
2345 * outside of the reservation loop. But:
2346 * - The batch is already pinned into the relevant ppgtt, so we
2347 * already have the backing storage fully allocated.
2348 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01002349 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002350 * fitting due to fragmentation.
2351 * So this is actually safe.
2352 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002353 vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0);
Chris Wilson058d88c2016-08-15 10:49:06 +01002354 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002355 err = PTR_ERR(vma);
2356 goto err_vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01002357 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002358
Chris Wilson650bc632017-06-15 09:14:33 +01002359 eb.batch = vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002360 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002361
Chris Wilson7dd4f672017-06-16 15:05:24 +01002362 /* All GPU relocation batches must be submitted prior to the user rq */
2363 GEM_BUG_ON(eb.reloc_cache.rq);
2364
John Harrison0c8dac82015-05-29 17:43:25 +01002365 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson650bc632017-06-15 09:14:33 +01002366 eb.request = i915_gem_request_alloc(eb.engine, eb.ctx);
2367 if (IS_ERR(eb.request)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002368 err = PTR_ERR(eb.request);
John Harrison0c8dac82015-05-29 17:43:25 +01002369 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00002370 }
John Harrison0c8dac82015-05-29 17:43:25 +01002371
Chris Wilsonfec04452017-01-27 09:40:08 +00002372 if (in_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002373 err = i915_gem_request_await_dma_fence(eb.request, in_fence);
2374 if (err < 0)
Chris Wilsonfec04452017-01-27 09:40:08 +00002375 goto err_request;
2376 }
2377
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002378 if (fences) {
2379 err = await_fence_array(&eb, fences);
2380 if (err)
2381 goto err_request;
2382 }
2383
Chris Wilsonfec04452017-01-27 09:40:08 +00002384 if (out_fence_fd != -1) {
Chris Wilson650bc632017-06-15 09:14:33 +01002385 out_fence = sync_file_create(&eb.request->fence);
Chris Wilsonfec04452017-01-27 09:40:08 +00002386 if (!out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002387 err = -ENOMEM;
Chris Wilsonfec04452017-01-27 09:40:08 +00002388 goto err_request;
2389 }
2390 }
2391
Chris Wilson2889caa2017-06-16 15:05:19 +01002392 /*
2393 * Whilst this request exists, batch_obj will be on the
Chris Wilson17f298cf2016-08-10 13:41:46 +01002394 * active_list, and so will hold the active reference. Only when this
2395 * request is retired will the the batch_obj be moved onto the
2396 * inactive_list and lose its active reference. Hence we do not need
2397 * to explicitly hold another reference here.
2398 */
Chris Wilson650bc632017-06-15 09:14:33 +01002399 eb.request->batch = eb.batch;
Chris Wilson17f298cf2016-08-10 13:41:46 +01002400
Chris Wilson2889caa2017-06-16 15:05:19 +01002401 trace_i915_gem_request_queue(eb.request, eb.batch_flags);
2402 err = eb_submit(&eb);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01002403err_request:
Chris Wilson2889caa2017-06-16 15:05:19 +01002404 __i915_add_request(eb.request, err == 0);
Chris Wilson650bc632017-06-15 09:14:33 +01002405 add_to_client(eb.request, file);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002406
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002407 if (fences)
2408 signal_fence_array(&eb, fences);
2409
Chris Wilsonfec04452017-01-27 09:40:08 +00002410 if (out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002411 if (err == 0) {
Chris Wilsonfec04452017-01-27 09:40:08 +00002412 fd_install(out_fence_fd, out_fence->file);
2413 args->rsvd2 &= GENMASK_ULL(0, 31); /* keep in-fence */
2414 args->rsvd2 |= (u64)out_fence_fd << 32;
2415 out_fence_fd = -1;
2416 } else {
2417 fput(out_fence->file);
2418 }
2419 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002420
John Harrison0c8dac82015-05-29 17:43:25 +01002421err_batch_unpin:
Chris Wilson2889caa2017-06-16 15:05:19 +01002422 if (eb.batch_flags & I915_DISPATCH_SECURE)
Chris Wilson650bc632017-06-15 09:14:33 +01002423 i915_vma_unpin(eb.batch);
Chris Wilson2889caa2017-06-16 15:05:19 +01002424err_vma:
2425 if (eb.exec)
2426 eb_release_vmas(&eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002427 mutex_unlock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01002428err_rpm:
Chris Wilson650bc632017-06-15 09:14:33 +01002429 intel_runtime_pm_put(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002430 i915_gem_context_put(eb.ctx);
2431err_destroy:
Chris Wilson2889caa2017-06-16 15:05:19 +01002432 eb_destroy(&eb);
Chris Wilson4d470f72017-06-29 16:04:25 +01002433err_out_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002434 if (out_fence_fd != -1)
2435 put_unused_fd(out_fence_fd);
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002436err_in_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002437 dma_fence_put(in_fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01002438 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002439}
2440
Chris Wilsond710fc12017-11-16 10:50:59 +00002441static size_t eb_element_size(void)
2442{
2443 return (sizeof(struct drm_i915_gem_exec_object2) +
2444 sizeof(struct i915_vma *) +
2445 sizeof(unsigned int));
2446}
2447
2448static bool check_buffer_count(size_t count)
2449{
2450 const size_t sz = eb_element_size();
2451
2452 /*
2453 * When using LUT_HANDLE, we impose a limit of INT_MAX for the lookup
2454 * array size (see eb_create()). Otherwise, we can accept an array as
2455 * large as can be addressed (though use large arrays at your peril)!
2456 */
2457
2458 return !(count < 1 || count > INT_MAX || count > SIZE_MAX / sz - 1);
2459}
2460
Chris Wilson54cf91d2010-11-25 18:00:26 +00002461/*
2462 * Legacy execbuffer just creates an exec2 list from the original exec object
2463 * list array and passes it to the real function.
2464 */
2465int
2466i915_gem_execbuffer(struct drm_device *dev, void *data,
2467 struct drm_file *file)
2468{
2469 struct drm_i915_gem_execbuffer *args = data;
2470 struct drm_i915_gem_execbuffer2 exec2;
2471 struct drm_i915_gem_exec_object *exec_list = NULL;
2472 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
Chris Wilsond710fc12017-11-16 10:50:59 +00002473 const size_t count = args->buffer_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01002474 unsigned int i;
2475 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002476
Chris Wilsond710fc12017-11-16 10:50:59 +00002477 if (!check_buffer_count(count)) {
2478 DRM_DEBUG("execbuf2 with %zd buffers\n", count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002479 return -EINVAL;
2480 }
2481
Chris Wilson2889caa2017-06-16 15:05:19 +01002482 exec2.buffers_ptr = args->buffers_ptr;
2483 exec2.buffer_count = args->buffer_count;
2484 exec2.batch_start_offset = args->batch_start_offset;
2485 exec2.batch_len = args->batch_len;
2486 exec2.DR1 = args->DR1;
2487 exec2.DR4 = args->DR4;
2488 exec2.num_cliprects = args->num_cliprects;
2489 exec2.cliprects_ptr = args->cliprects_ptr;
2490 exec2.flags = I915_EXEC_RENDER;
2491 i915_execbuffer2_set_context_id(exec2, 0);
2492
2493 if (!i915_gem_check_execbuffer(&exec2))
2494 return -EINVAL;
2495
Chris Wilson54cf91d2010-11-25 18:00:26 +00002496 /* Copy in the exec list from userland */
Chris Wilsond710fc12017-11-16 10:50:59 +00002497 exec_list = kvmalloc_array(count, sizeof(*exec_list),
Michal Hocko0ee931c2017-09-13 16:28:29 -07002498 __GFP_NOWARN | GFP_KERNEL);
Chris Wilsond710fc12017-11-16 10:50:59 +00002499 exec2_list = kvmalloc_array(count + 1, eb_element_size(),
Michal Hocko0ee931c2017-09-13 16:28:29 -07002500 __GFP_NOWARN | GFP_KERNEL);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002501 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002502 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002503 args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002504 kvfree(exec_list);
2505 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002506 return -ENOMEM;
2507 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002508 err = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002509 u64_to_user_ptr(args->buffers_ptr),
Chris Wilsond710fc12017-11-16 10:50:59 +00002510 sizeof(*exec_list) * count);
Chris Wilson2889caa2017-06-16 15:05:19 +01002511 if (err) {
Daniel Vetterff240192012-01-31 21:08:14 +01002512 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson2889caa2017-06-16 15:05:19 +01002513 args->buffer_count, err);
Michal Hocko20981052017-05-17 14:23:12 +02002514 kvfree(exec_list);
2515 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002516 return -EFAULT;
2517 }
2518
2519 for (i = 0; i < args->buffer_count; i++) {
2520 exec2_list[i].handle = exec_list[i].handle;
2521 exec2_list[i].relocation_count = exec_list[i].relocation_count;
2522 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
2523 exec2_list[i].alignment = exec_list[i].alignment;
2524 exec2_list[i].offset = exec_list[i].offset;
Tvrtko Ursulinf0836b72016-11-16 08:55:32 +00002525 if (INTEL_GEN(to_i915(dev)) < 4)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002526 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
2527 else
2528 exec2_list[i].flags = 0;
2529 }
2530
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002531 err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list, NULL);
Chris Wilson2889caa2017-06-16 15:05:19 +01002532 if (exec2.flags & __EXEC_HAS_RELOC) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002533 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002534 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002535
Chris Wilson54cf91d2010-11-25 18:00:26 +00002536 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002537 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002538 if (!(exec2_list[i].offset & UPDATE))
2539 continue;
2540
Michał Winiarski934acce2015-12-29 18:24:52 +01002541 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002542 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2543 exec2_list[i].offset &= PIN_OFFSET_MASK;
2544 if (__copy_to_user(&user_exec_list[i].offset,
2545 &exec2_list[i].offset,
2546 sizeof(user_exec_list[i].offset)))
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002547 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002548 }
2549 }
2550
Michal Hocko20981052017-05-17 14:23:12 +02002551 kvfree(exec_list);
2552 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002553 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002554}
2555
2556int
2557i915_gem_execbuffer2(struct drm_device *dev, void *data,
2558 struct drm_file *file)
2559{
2560 struct drm_i915_gem_execbuffer2 *args = data;
Chris Wilson2889caa2017-06-16 15:05:19 +01002561 struct drm_i915_gem_exec_object2 *exec2_list;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002562 struct drm_syncobj **fences = NULL;
Chris Wilsond710fc12017-11-16 10:50:59 +00002563 const size_t count = args->buffer_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01002564 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002565
Chris Wilsond710fc12017-11-16 10:50:59 +00002566 if (!check_buffer_count(count)) {
2567 DRM_DEBUG("execbuf2 with %zd buffers\n", count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002568 return -EINVAL;
2569 }
2570
Chris Wilson2889caa2017-06-16 15:05:19 +01002571 if (!i915_gem_check_execbuffer(args))
2572 return -EINVAL;
2573
2574 /* Allocate an extra slot for use by the command parser */
Chris Wilsond710fc12017-11-16 10:50:59 +00002575 exec2_list = kvmalloc_array(count + 1, eb_element_size(),
Michal Hocko0ee931c2017-09-13 16:28:29 -07002576 __GFP_NOWARN | GFP_KERNEL);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002577 if (exec2_list == NULL) {
Chris Wilsond710fc12017-11-16 10:50:59 +00002578 DRM_DEBUG("Failed to allocate exec list for %zd buffers\n",
2579 count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002580 return -ENOMEM;
2581 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002582 if (copy_from_user(exec2_list,
2583 u64_to_user_ptr(args->buffers_ptr),
Chris Wilsond710fc12017-11-16 10:50:59 +00002584 sizeof(*exec2_list) * count)) {
2585 DRM_DEBUG("copy %zd exec entries failed\n", count);
Michal Hocko20981052017-05-17 14:23:12 +02002586 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002587 return -EFAULT;
2588 }
2589
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002590 if (args->flags & I915_EXEC_FENCE_ARRAY) {
2591 fences = get_fence_array(args, file);
2592 if (IS_ERR(fences)) {
2593 kvfree(exec2_list);
2594 return PTR_ERR(fences);
2595 }
2596 }
2597
2598 err = i915_gem_do_execbuffer(dev, file, args, exec2_list, fences);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002599
Chris Wilson2889caa2017-06-16 15:05:19 +01002600 /*
2601 * Now that we have begun execution of the batchbuffer, we ignore
2602 * any new error after this point. Also given that we have already
2603 * updated the associated relocations, we try to write out the current
2604 * object locations irrespective of any error.
2605 */
2606 if (args->flags & __EXEC_HAS_RELOC) {
2607 struct drm_i915_gem_exec_object2 __user *user_exec_list =
2608 u64_to_user_ptr(args->buffers_ptr);
2609 unsigned int i;
2610
2611 /* Copy the new buffer offsets back to the user's exec list. */
2612 user_access_begin();
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002613 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002614 if (!(exec2_list[i].offset & UPDATE))
2615 continue;
2616
Michał Winiarski934acce2015-12-29 18:24:52 +01002617 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002618 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2619 unsafe_put_user(exec2_list[i].offset,
2620 &user_exec_list[i].offset,
2621 end_user);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002622 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002623end_user:
2624 user_access_end();
Chris Wilson54cf91d2010-11-25 18:00:26 +00002625 }
2626
Chris Wilson2889caa2017-06-16 15:05:19 +01002627 args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002628 put_fence_array(args, fences);
Michal Hocko20981052017-05-17 14:23:12 +02002629 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002630 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002631}