blob: 22df17c8ca9b0fc75dc29189c6b7117002c5ad17 [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 *
Kevin Rogovin99d7e4e2018-04-06 11:05:57 +030084 * At the level of talking to the hardware, submitting a batchbuffer for the
85 * GPU to execute is to add content to a buffer from which the HW
86 * command streamer is reading.
87 *
88 * 1. Add a command to load the HW context. For Logical Ring Contexts, i.e.
89 * Execlists, this command is not placed on the same buffer as the
90 * remaining items.
91 *
92 * 2. Add a command to invalidate caches to the buffer.
93 *
94 * 3. Add a batchbuffer start command to the buffer; the start command is
95 * essentially a token together with the GPU address of the batchbuffer
96 * to be executed.
97 *
98 * 4. Add a pipeline flush to the buffer.
99 *
100 * 5. Add a memory write command to the buffer to record when the GPU
101 * is done executing the batchbuffer. The memory write writes the
102 * global sequence number of the request, ``i915_request::global_seqno``;
103 * the i915 driver uses the current value in the register to determine
104 * if the GPU has completed the batchbuffer.
105 *
106 * 6. Add a user interrupt command to the buffer. This command instructs
107 * the GPU to issue an interrupt when the command, pipeline flush and
108 * memory write are completed.
109 *
110 * 7. Inform the hardware of the additional commands added to the buffer
111 * (by updating the tail pointer).
112 *
Chris Wilson2889caa2017-06-16 15:05:19 +0100113 * Processing an execbuf ioctl is conceptually split up into a few phases.
114 *
115 * 1. Validation - Ensure all the pointers, handles and flags are valid.
116 * 2. Reservation - Assign GPU address space for every object
117 * 3. Relocation - Update any addresses to point to the final locations
118 * 4. Serialisation - Order the request with respect to its dependencies
119 * 5. Construction - Construct a request to execute the batchbuffer
120 * 6. Submission (at some point in the future execution)
121 *
122 * Reserving resources for the execbuf is the most complicated phase. We
123 * neither want to have to migrate the object in the address space, nor do
124 * we want to have to update any relocations pointing to this object. Ideally,
125 * we want to leave the object where it is and for all the existing relocations
126 * to match. If the object is given a new address, or if userspace thinks the
127 * object is elsewhere, we have to parse all the relocation entries and update
128 * the addresses. Userspace can set the I915_EXEC_NORELOC flag to hint that
129 * all the target addresses in all of its objects match the value in the
130 * relocation entries and that they all match the presumed offsets given by the
131 * list of execbuffer objects. Using this knowledge, we know that if we haven't
132 * moved any buffers, all the relocation entries are valid and we can skip
133 * the update. (If userspace is wrong, the likely outcome is an impromptu GPU
134 * hang.) The requirement for using I915_EXEC_NO_RELOC are:
135 *
136 * The addresses written in the objects must match the corresponding
137 * reloc.presumed_offset which in turn must match the corresponding
138 * execobject.offset.
139 *
140 * Any render targets written to in the batch must be flagged with
141 * EXEC_OBJECT_WRITE.
142 *
143 * To avoid stalling, execobject.offset should match the current
144 * address of that object within the active context.
145 *
146 * The reservation is done is multiple phases. First we try and keep any
147 * object already bound in its current location - so as long as meets the
148 * constraints imposed by the new execbuffer. Any object left unbound after the
149 * first pass is then fitted into any available idle space. If an object does
150 * not fit, all objects are removed from the reservation and the process rerun
151 * after sorting the objects into a priority order (more difficult to fit
152 * objects are tried first). Failing that, the entire VM is cleared and we try
153 * to fit the execbuf once last time before concluding that it simply will not
154 * fit.
155 *
156 * A small complication to all of this is that we allow userspace not only to
157 * specify an alignment and a size for the object in the address space, but
158 * we also allow userspace to specify the exact offset. This objects are
159 * simpler to place (the location is known a priori) all we have to do is make
160 * sure the space is available.
161 *
162 * Once all the objects are in place, patching up the buried pointers to point
163 * to the final locations is a fairly simple job of walking over the relocation
164 * entry arrays, looking up the right address and rewriting the value into
165 * the object. Simple! ... The relocation entries are stored in user memory
166 * and so to access them we have to copy them into a local buffer. That copy
167 * has to avoid taking any pagefaults as they may lead back to a GEM object
168 * requiring the struct_mutex (i.e. recursive deadlock). So once again we split
169 * the relocation into multiple passes. First we try to do everything within an
170 * atomic context (avoid the pagefaults) which requires that we never wait. If
171 * we detect that we may wait, or if we need to fault, then we have to fallback
172 * to a slower path. The slowpath has to drop the mutex. (Can you hear alarm
173 * bells yet?) Dropping the mutex means that we lose all the state we have
174 * built up so far for the execbuf and we must reset any global data. However,
175 * we do leave the objects pinned in their final locations - which is a
176 * potential issue for concurrent execbufs. Once we have left the mutex, we can
177 * allocate and copy all the relocation entries into a large array at our
178 * leisure, reacquire the mutex, reclaim all the objects and other state and
179 * then proceed to update any incorrect addresses with the objects.
180 *
181 * As we process the relocation entries, we maintain a record of whether the
182 * object is being written to. Using NORELOC, we expect userspace to provide
183 * this information instead. We also check whether we can skip the relocation
184 * by comparing the expected value inside the relocation entry with the target's
185 * final address. If they differ, we have to map the current object and rewrite
186 * the 4 or 8 byte pointer within.
187 *
188 * Serialising an execbuf is quite simple according to the rules of the GEM
189 * ABI. Execution within each context is ordered by the order of submission.
190 * Writes to any GEM object are in order of submission and are exclusive. Reads
191 * from a GEM object are unordered with respect to other reads, but ordered by
192 * writes. A write submitted after a read cannot occur before the read, and
193 * similarly any read submitted after a write cannot occur before the write.
194 * Writes are ordered between engines such that only one write occurs at any
195 * time (completing any reads beforehand) - using semaphores where available
196 * and CPU serialisation otherwise. Other GEM access obey the same rules, any
197 * write (either via mmaps using set-domain, or via pwrite) must flush all GPU
198 * reads before starting, and any read (either using set-domain or pread) must
199 * flush all GPU writes before starting. (Note we only employ a barrier before,
200 * we currently rely on userspace not concurrently starting a new execution
201 * whilst reading or writing to an object. This may be an advantage or not
202 * depending on how much you trust userspace not to shoot themselves in the
203 * foot.) Serialisation may just result in the request being inserted into
204 * a DAG awaiting its turn, but most simple is to wait on the CPU until
205 * all dependencies are resolved.
206 *
207 * After all of that, is just a matter of closing the request and handing it to
208 * the hardware (well, leaving it in a queue to be executed). However, we also
209 * offer the ability for batchbuffers to be run with elevated privileges so
210 * that they access otherwise hidden registers. (Used to adjust L3 cache etc.)
211 * Before any batch is given extra privileges we first must check that it
212 * contains no nefarious instructions, we check that each instruction is from
213 * our whitelist and all registers are also from an allowed list. We first
214 * copy the user's batchbuffer to a shadow (so that the user doesn't have
215 * access to it, either by the CPU or GPU as we scan it) and then parse each
216 * instruction. If everything is ok, we set a flag telling the hardware to run
217 * the batchbuffer in trusted mode, otherwise the ioctl is rejected.
218 */
219
Chris Wilson650bc632017-06-15 09:14:33 +0100220struct i915_execbuffer {
Chris Wilson2889caa2017-06-16 15:05:19 +0100221 struct drm_i915_private *i915; /** i915 backpointer */
222 struct drm_file *file; /** per-file lookup tables and limits */
223 struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */
224 struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100225 struct i915_vma **vma;
226 unsigned int *flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100227
228 struct intel_engine_cs *engine; /** engine to queue the request to */
229 struct i915_gem_context *ctx; /** context for building the request */
230 struct i915_address_space *vm; /** GTT and vma for the request */
231
Chris Wilsone61e0f52018-02-21 09:56:36 +0000232 struct i915_request *request; /** our request to build */
Chris Wilson2889caa2017-06-16 15:05:19 +0100233 struct i915_vma *batch; /** identity of the batch obj/vma */
234
235 /** actual size of execobj[] as we may extend it for the cmdparser */
236 unsigned int buffer_count;
237
238 /** list of vma not yet bound during reservation phase */
239 struct list_head unbound;
240
241 /** list of vma that have execobj.relocation_count */
242 struct list_head relocs;
243
244 /**
245 * Track the most recently used object for relocations, as we
246 * frequently have to perform multiple relocations within the same
247 * obj/page
248 */
Chris Wilson650bc632017-06-15 09:14:33 +0100249 struct reloc_cache {
Chris Wilson2889caa2017-06-16 15:05:19 +0100250 struct drm_mm_node node; /** temporary GTT binding */
251 unsigned long vaddr; /** Current kmap address */
252 unsigned long page; /** Currently mapped page index */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100253 unsigned int gen; /** Cached value of INTEL_GEN */
Chris Wilson650bc632017-06-15 09:14:33 +0100254 bool use_64bit_reloc : 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100255 bool has_llc : 1;
256 bool has_fence : 1;
257 bool needs_unfenced : 1;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100258
Chris Wilsone61e0f52018-02-21 09:56:36 +0000259 struct i915_request *rq;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100260 u32 *rq_cmd;
261 unsigned int rq_size;
Chris Wilson650bc632017-06-15 09:14:33 +0100262 } reloc_cache;
Chris Wilson2889caa2017-06-16 15:05:19 +0100263
264 u64 invalid_flags; /** Set of execobj.flags that are invalid */
265 u32 context_flags; /** Set of execobj.flags to insert from the ctx */
266
267 u32 batch_start_offset; /** Location within object of batch */
268 u32 batch_len; /** Length of batch within object */
269 u32 batch_flags; /** Flags composed for emit_bb_start() */
270
271 /**
272 * Indicate either the size of the hastable used to resolve
273 * relocation handles, or if negative that we are using a direct
274 * index into the execobj[].
275 */
276 int lut_size;
277 struct hlist_head *buckets; /** ht for relocation handles */
Chris Wilson67731b82010-12-08 10:38:14 +0000278};
279
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100280#define exec_entry(EB, VMA) (&(EB)->exec[(VMA)->exec_flags - (EB)->flags])
Chris Wilson4ff4b442017-06-16 15:05:16 +0100281
Chris Wilson2889caa2017-06-16 15:05:19 +0100282/*
283 * Used to convert any address to canonical form.
284 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
285 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
286 * addresses to be in a canonical form:
287 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
288 * canonical form [63:48] == [47]."
289 */
290#define GEN8_HIGH_ADDRESS_BIT 47
291static inline u64 gen8_canonical_addr(u64 address)
292{
293 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
294}
295
296static inline u64 gen8_noncanonical_addr(u64 address)
297{
298 return address & GENMASK_ULL(GEN8_HIGH_ADDRESS_BIT, 0);
299}
300
Chris Wilson3dbf26e2017-08-26 14:56:20 +0100301static inline bool eb_use_cmdparser(const struct i915_execbuffer *eb)
302{
Tvrtko Ursulin439e2ee2017-11-29 08:24:09 +0000303 return intel_engine_needs_cmd_parser(eb->engine) && eb->batch_len;
Chris Wilson3dbf26e2017-08-26 14:56:20 +0100304}
305
Chris Wilson650bc632017-06-15 09:14:33 +0100306static int eb_create(struct i915_execbuffer *eb)
Chris Wilson67731b82010-12-08 10:38:14 +0000307{
Chris Wilson2889caa2017-06-16 15:05:19 +0100308 if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) {
309 unsigned int size = 1 + ilog2(eb->buffer_count);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000310
Chris Wilson2889caa2017-06-16 15:05:19 +0100311 /*
312 * Without a 1:1 association between relocation handles and
313 * the execobject[] index, we instead create a hashtable.
314 * We size it dynamically based on available memory, starting
315 * first with 1:1 assocative hash and scaling back until
316 * the allocation succeeds.
317 *
318 * Later on we use a positive lut_size to indicate we are
319 * using this hashtable, and a negative value to indicate a
320 * direct lookup.
321 */
Chris Wilson4ff4b442017-06-16 15:05:16 +0100322 do {
Chris Wilson0d95c882017-09-01 15:57:28 +0100323 gfp_t flags;
Chris Wilson4d470f72017-06-29 16:04:25 +0100324
325 /* While we can still reduce the allocation size, don't
326 * raise a warning and allow the allocation to fail.
327 * On the last pass though, we want to try as hard
328 * as possible to perform the allocation and warn
329 * if it fails.
330 */
Michal Hocko0ee931c2017-09-13 16:28:29 -0700331 flags = GFP_KERNEL;
Chris Wilson4d470f72017-06-29 16:04:25 +0100332 if (size > 1)
333 flags |= __GFP_NORETRY | __GFP_NOWARN;
334
Chris Wilson4ff4b442017-06-16 15:05:16 +0100335 eb->buckets = kzalloc(sizeof(struct hlist_head) << size,
Chris Wilson4d470f72017-06-29 16:04:25 +0100336 flags);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100337 if (eb->buckets)
338 break;
339 } while (--size);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000340
Chris Wilson4d470f72017-06-29 16:04:25 +0100341 if (unlikely(!size))
342 return -ENOMEM;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100343
Chris Wilson2889caa2017-06-16 15:05:19 +0100344 eb->lut_size = size;
Chris Wilson650bc632017-06-15 09:14:33 +0100345 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +0100346 eb->lut_size = -eb->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +0100347 }
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000348
Chris Wilson650bc632017-06-15 09:14:33 +0100349 return 0;
Chris Wilson67731b82010-12-08 10:38:14 +0000350}
351
Chris Wilson2889caa2017-06-16 15:05:19 +0100352static bool
353eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry,
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100354 const struct i915_vma *vma,
355 unsigned int flags)
Chris Wilson2889caa2017-06-16 15:05:19 +0100356{
Chris Wilson2889caa2017-06-16 15:05:19 +0100357 if (vma->node.size < entry->pad_to_size)
358 return true;
359
360 if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment))
361 return true;
362
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100363 if (flags & EXEC_OBJECT_PINNED &&
Chris Wilson2889caa2017-06-16 15:05:19 +0100364 vma->node.start != entry->offset)
365 return true;
366
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100367 if (flags & __EXEC_OBJECT_NEEDS_BIAS &&
Chris Wilson2889caa2017-06-16 15:05:19 +0100368 vma->node.start < BATCH_OFFSET_BIAS)
369 return true;
370
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100371 if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) &&
Chris Wilson2889caa2017-06-16 15:05:19 +0100372 (vma->node.start + vma->node.size - 1) >> 32)
373 return true;
374
Chris Wilson1d033be2017-10-31 10:36:07 +0000375 if (flags & __EXEC_OBJECT_NEEDS_MAP &&
376 !i915_vma_is_map_and_fenceable(vma))
377 return true;
378
Chris Wilson2889caa2017-06-16 15:05:19 +0100379 return false;
380}
381
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100382static inline bool
Chris Wilson2889caa2017-06-16 15:05:19 +0100383eb_pin_vma(struct i915_execbuffer *eb,
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100384 const struct drm_i915_gem_exec_object2 *entry,
Chris Wilson2889caa2017-06-16 15:05:19 +0100385 struct i915_vma *vma)
386{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100387 unsigned int exec_flags = *vma->exec_flags;
388 u64 pin_flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100389
Chris Wilson616d9ce2017-06-16 15:05:21 +0100390 if (vma->node.size)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100391 pin_flags = vma->node.start;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100392 else
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100393 pin_flags = entry->offset & PIN_OFFSET_MASK;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100394
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100395 pin_flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED;
396 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_GTT))
397 pin_flags |= PIN_GLOBAL;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100398
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100399 if (unlikely(i915_vma_pin(vma, 0, 0, pin_flags)))
400 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100401
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100402 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) {
Chris Wilson3bd40732017-10-09 09:43:56 +0100403 if (unlikely(i915_vma_pin_fence(vma))) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100404 i915_vma_unpin(vma);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100405 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100406 }
407
Chris Wilson3bd40732017-10-09 09:43:56 +0100408 if (vma->fence)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100409 exec_flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100410 }
411
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100412 *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN;
413 return !eb_vma_misplaced(entry, vma, exec_flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100414}
415
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100416static inline void __eb_unreserve_vma(struct i915_vma *vma, unsigned int flags)
Chris Wilsond55495b2017-06-15 09:14:34 +0100417{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100418 GEM_BUG_ON(!(flags & __EXEC_OBJECT_HAS_PIN));
Chris Wilson2889caa2017-06-16 15:05:19 +0100419
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100420 if (unlikely(flags & __EXEC_OBJECT_HAS_FENCE))
Chris Wilson3bd40732017-10-09 09:43:56 +0100421 __i915_vma_unpin_fence(vma);
Chris Wilsond55495b2017-06-15 09:14:34 +0100422
Chris Wilson2889caa2017-06-16 15:05:19 +0100423 __i915_vma_unpin(vma);
Chris Wilsond55495b2017-06-15 09:14:34 +0100424}
425
Chris Wilson2889caa2017-06-16 15:05:19 +0100426static inline void
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100427eb_unreserve_vma(struct i915_vma *vma, unsigned int *flags)
Chris Wilsond55495b2017-06-15 09:14:34 +0100428{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100429 if (!(*flags & __EXEC_OBJECT_HAS_PIN))
Chris Wilson2889caa2017-06-16 15:05:19 +0100430 return;
Chris Wilsond55495b2017-06-15 09:14:34 +0100431
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100432 __eb_unreserve_vma(vma, *flags);
433 *flags &= ~__EXEC_OBJECT_RESERVED;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100434}
435
436static int
Chris Wilson2889caa2017-06-16 15:05:19 +0100437eb_validate_vma(struct i915_execbuffer *eb,
438 struct drm_i915_gem_exec_object2 *entry,
439 struct i915_vma *vma)
440{
441 if (unlikely(entry->flags & eb->invalid_flags))
442 return -EINVAL;
443
444 if (unlikely(entry->alignment && !is_power_of_2(entry->alignment)))
445 return -EINVAL;
446
447 /*
448 * Offset can be used as input (EXEC_OBJECT_PINNED), reject
449 * any non-page-aligned or non-canonical addresses.
450 */
451 if (unlikely(entry->flags & EXEC_OBJECT_PINNED &&
452 entry->offset != gen8_canonical_addr(entry->offset & PAGE_MASK)))
453 return -EINVAL;
454
455 /* pad_to_size was once a reserved field, so sanitize it */
456 if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) {
457 if (unlikely(offset_in_page(entry->pad_to_size)))
458 return -EINVAL;
459 } else {
460 entry->pad_to_size = 0;
461 }
462
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100463 if (unlikely(vma->exec_flags)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100464 DRM_DEBUG("Object [handle %d, index %d] appears more than once in object list\n",
465 entry->handle, (int)(entry - eb->exec));
466 return -EINVAL;
467 }
468
469 /*
470 * From drm_mm perspective address space is continuous,
471 * so from this point we're always using non-canonical
472 * form internally.
473 */
474 entry->offset = gen8_noncanonical_addr(entry->offset);
475
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100476 if (!eb->reloc_cache.has_fence) {
477 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
478 } else {
479 if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE ||
480 eb->reloc_cache.needs_unfenced) &&
481 i915_gem_object_is_tiled(vma->obj))
482 entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP;
483 }
484
485 if (!(entry->flags & EXEC_OBJECT_PINNED))
486 entry->flags |= eb->context_flags;
487
Chris Wilson2889caa2017-06-16 15:05:19 +0100488 return 0;
489}
490
491static int
Chris Wilson7ba33e12018-06-10 20:43:09 +0100492eb_add_vma(struct i915_execbuffer *eb,
493 unsigned int i, unsigned batch_idx,
494 struct i915_vma *vma)
Chris Wilson2889caa2017-06-16 15:05:19 +0100495{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100496 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100497 int err;
498
499 GEM_BUG_ON(i915_vma_is_closed(vma));
500
501 if (!(eb->args->flags & __EXEC_VALIDATED)) {
502 err = eb_validate_vma(eb, entry, vma);
503 if (unlikely(err))
504 return err;
505 }
506
Chris Wilson4d470f72017-06-29 16:04:25 +0100507 if (eb->lut_size > 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100508 vma->exec_handle = entry->handle;
509 hlist_add_head(&vma->exec_node,
510 &eb->buckets[hash_32(entry->handle,
511 eb->lut_size)]);
512 }
513
514 if (entry->relocation_count)
515 list_add_tail(&vma->reloc_link, &eb->relocs);
516
Chris Wilson2889caa2017-06-16 15:05:19 +0100517 /*
518 * Stash a pointer from the vma to execobj, so we can query its flags,
519 * size, alignment etc as provided by the user. Also we stash a pointer
520 * to the vma inside the execobj so that we can use a direct lookup
521 * to find the right target VMA when doing relocations.
522 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100523 eb->vma[i] = vma;
Chris Wilsond1b48c12017-08-16 09:52:08 +0100524 eb->flags[i] = entry->flags;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100525 vma->exec_flags = &eb->flags[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100526
Chris Wilson7ba33e12018-06-10 20:43:09 +0100527 /*
528 * SNA is doing fancy tricks with compressing batch buffers, which leads
529 * to negative relocation deltas. Usually that works out ok since the
530 * relocate address is still positive, except when the batch is placed
531 * very low in the GTT. Ensure this doesn't happen.
532 *
533 * Note that actual hangs have only been observed on gen7, but for
534 * paranoia do it everywhere.
535 */
536 if (i == batch_idx) {
537 if (!(eb->flags[i] & EXEC_OBJECT_PINNED))
538 eb->flags[i] |= __EXEC_OBJECT_NEEDS_BIAS;
539 if (eb->reloc_cache.has_fence)
540 eb->flags[i] |= EXEC_OBJECT_NEEDS_FENCE;
541
542 eb->batch = vma;
543 }
544
Chris Wilson2889caa2017-06-16 15:05:19 +0100545 err = 0;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100546 if (eb_pin_vma(eb, entry, vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100547 if (entry->offset != vma->node.start) {
548 entry->offset = vma->node.start | UPDATE;
549 eb->args->flags |= __EXEC_HAS_RELOC;
550 }
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100551 } else {
552 eb_unreserve_vma(vma, vma->exec_flags);
553
554 list_add_tail(&vma->exec_link, &eb->unbound);
555 if (drm_mm_node_allocated(&vma->node))
556 err = i915_vma_unbind(vma);
Chris Wilsoned2f3532018-02-19 14:01:44 +0000557 if (unlikely(err))
558 vma->exec_flags = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100559 }
560 return err;
561}
562
563static inline int use_cpu_reloc(const struct reloc_cache *cache,
564 const struct drm_i915_gem_object *obj)
565{
566 if (!i915_gem_object_has_struct_page(obj))
567 return false;
568
Chris Wilson7dd4f672017-06-16 15:05:24 +0100569 if (DBG_FORCE_RELOC == FORCE_CPU_RELOC)
570 return true;
571
572 if (DBG_FORCE_RELOC == FORCE_GTT_RELOC)
573 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100574
575 return (cache->has_llc ||
576 obj->cache_dirty ||
577 obj->cache_level != I915_CACHE_NONE);
578}
579
580static int eb_reserve_vma(const struct i915_execbuffer *eb,
581 struct i915_vma *vma)
582{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100583 struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
584 unsigned int exec_flags = *vma->exec_flags;
585 u64 pin_flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100586 int err;
587
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100588 pin_flags = PIN_USER | PIN_NONBLOCK;
589 if (exec_flags & EXEC_OBJECT_NEEDS_GTT)
590 pin_flags |= PIN_GLOBAL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100591
592 /*
593 * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
594 * limit address to the first 4GBs for unflagged objects.
595 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100596 if (!(exec_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
597 pin_flags |= PIN_ZONE_4G;
Chris Wilson2889caa2017-06-16 15:05:19 +0100598
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100599 if (exec_flags & __EXEC_OBJECT_NEEDS_MAP)
600 pin_flags |= PIN_MAPPABLE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100601
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100602 if (exec_flags & EXEC_OBJECT_PINNED) {
603 pin_flags |= entry->offset | PIN_OFFSET_FIXED;
604 pin_flags &= ~PIN_NONBLOCK; /* force overlapping checks */
605 } else if (exec_flags & __EXEC_OBJECT_NEEDS_BIAS) {
606 pin_flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson2889caa2017-06-16 15:05:19 +0100607 }
608
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100609 err = i915_vma_pin(vma,
610 entry->pad_to_size, entry->alignment,
611 pin_flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100612 if (err)
613 return err;
614
615 if (entry->offset != vma->node.start) {
616 entry->offset = vma->node.start | UPDATE;
617 eb->args->flags |= __EXEC_HAS_RELOC;
618 }
619
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100620 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) {
Chris Wilson3bd40732017-10-09 09:43:56 +0100621 err = i915_vma_pin_fence(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100622 if (unlikely(err)) {
623 i915_vma_unpin(vma);
624 return err;
625 }
626
Chris Wilson3bd40732017-10-09 09:43:56 +0100627 if (vma->fence)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100628 exec_flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100629 }
630
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100631 *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN;
632 GEM_BUG_ON(eb_vma_misplaced(entry, vma, exec_flags));
Chris Wilson1da7b542017-07-21 15:50:35 +0100633
Chris Wilson2889caa2017-06-16 15:05:19 +0100634 return 0;
635}
636
637static int eb_reserve(struct i915_execbuffer *eb)
638{
639 const unsigned int count = eb->buffer_count;
640 struct list_head last;
641 struct i915_vma *vma;
642 unsigned int i, pass;
643 int err;
644
645 /*
646 * Attempt to pin all of the buffers into the GTT.
647 * This is done in 3 phases:
648 *
649 * 1a. Unbind all objects that do not match the GTT constraints for
650 * the execbuffer (fenceable, mappable, alignment etc).
651 * 1b. Increment pin count for already bound objects.
652 * 2. Bind new objects.
653 * 3. Decrement pin count.
654 *
655 * This avoid unnecessary unbinding of later objects in order to make
656 * room for the earlier objects *unless* we need to defragment.
657 */
658
659 pass = 0;
660 err = 0;
661 do {
662 list_for_each_entry(vma, &eb->unbound, exec_link) {
663 err = eb_reserve_vma(eb, vma);
664 if (err)
665 break;
666 }
667 if (err != -ENOSPC)
668 return err;
669
670 /* Resort *all* the objects into priority order */
671 INIT_LIST_HEAD(&eb->unbound);
672 INIT_LIST_HEAD(&last);
673 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100674 unsigned int flags = eb->flags[i];
675 struct i915_vma *vma = eb->vma[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100676
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100677 if (flags & EXEC_OBJECT_PINNED &&
678 flags & __EXEC_OBJECT_HAS_PIN)
Chris Wilson2889caa2017-06-16 15:05:19 +0100679 continue;
680
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100681 eb_unreserve_vma(vma, &eb->flags[i]);
Chris Wilson2889caa2017-06-16 15:05:19 +0100682
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100683 if (flags & EXEC_OBJECT_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +0100684 list_add(&vma->exec_link, &eb->unbound);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100685 else if (flags & __EXEC_OBJECT_NEEDS_MAP)
Chris Wilson2889caa2017-06-16 15:05:19 +0100686 list_add_tail(&vma->exec_link, &eb->unbound);
687 else
688 list_add_tail(&vma->exec_link, &last);
689 }
690 list_splice_tail(&last, &eb->unbound);
691
692 switch (pass++) {
693 case 0:
694 break;
695
696 case 1:
697 /* Too fragmented, unbind everything and retry */
698 err = i915_gem_evict_vm(eb->vm);
699 if (err)
700 return err;
701 break;
702
703 default:
704 return -ENOSPC;
705 }
706 } while (1);
707}
708
Chris Wilson2889caa2017-06-16 15:05:19 +0100709static unsigned int eb_batch_index(const struct i915_execbuffer *eb)
710{
Chris Wilson1a71cf22017-06-16 15:05:23 +0100711 if (eb->args->flags & I915_EXEC_BATCH_FIRST)
712 return 0;
713 else
714 return eb->buffer_count - 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100715}
716
717static int eb_select_context(struct i915_execbuffer *eb)
718{
719 struct i915_gem_context *ctx;
720
721 ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1);
Chris Wilson1acfc102017-06-20 12:05:47 +0100722 if (unlikely(!ctx))
723 return -ENOENT;
Chris Wilson2889caa2017-06-16 15:05:19 +0100724
Chris Wilson1acfc102017-06-20 12:05:47 +0100725 eb->ctx = ctx;
Chris Wilson2889caa2017-06-16 15:05:19 +0100726 eb->vm = ctx->ppgtt ? &ctx->ppgtt->base : &eb->i915->ggtt.base;
727
728 eb->context_flags = 0;
729 if (ctx->flags & CONTEXT_NO_ZEROMAP)
730 eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS;
731
732 return 0;
733}
734
735static int eb_lookup_vmas(struct i915_execbuffer *eb)
Chris Wilson4ff4b442017-06-16 15:05:16 +0100736{
Chris Wilsond1b48c12017-08-16 09:52:08 +0100737 struct radix_tree_root *handles_vma = &eb->ctx->handles_vma;
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100738 struct drm_i915_gem_object *obj;
Chris Wilson7ba33e12018-06-10 20:43:09 +0100739 unsigned int i, batch;
Chris Wilson2889caa2017-06-16 15:05:19 +0100740 int err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100741
Chris Wilson8bcbfb12017-08-16 09:52:05 +0100742 if (unlikely(i915_gem_context_is_closed(eb->ctx)))
743 return -ENOENT;
744
745 if (unlikely(i915_gem_context_is_banned(eb->ctx)))
746 return -EIO;
747
Chris Wilson2889caa2017-06-16 15:05:19 +0100748 INIT_LIST_HEAD(&eb->relocs);
749 INIT_LIST_HEAD(&eb->unbound);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100750
Chris Wilson7ba33e12018-06-10 20:43:09 +0100751 batch = eb_batch_index(eb);
752
Chris Wilson170fa292017-08-16 09:52:07 +0100753 for (i = 0; i < eb->buffer_count; i++) {
754 u32 handle = eb->exec[i].handle;
Chris Wilsond1b48c12017-08-16 09:52:08 +0100755 struct i915_lut_handle *lut;
Chris Wilson170fa292017-08-16 09:52:07 +0100756 struct i915_vma *vma;
757
Chris Wilsond1b48c12017-08-16 09:52:08 +0100758 vma = radix_tree_lookup(handles_vma, handle);
759 if (likely(vma))
Chris Wilson170fa292017-08-16 09:52:07 +0100760 goto add_vma;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100761
Chris Wilson170fa292017-08-16 09:52:07 +0100762 obj = i915_gem_object_lookup(eb->file, handle);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100763 if (unlikely(!obj)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100764 err = -ENOENT;
Chris Wilson170fa292017-08-16 09:52:07 +0100765 goto err_vma;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100766 }
767
Chris Wilson4ff4b442017-06-16 15:05:16 +0100768 vma = i915_vma_instance(obj, eb->vm, NULL);
769 if (unlikely(IS_ERR(vma))) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100770 err = PTR_ERR(vma);
Chris Wilson170fa292017-08-16 09:52:07 +0100771 goto err_obj;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100772 }
773
Chris Wilsond1b48c12017-08-16 09:52:08 +0100774 lut = kmem_cache_alloc(eb->i915->luts, GFP_KERNEL);
775 if (unlikely(!lut)) {
776 err = -ENOMEM;
777 goto err_obj;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100778 }
779
Chris Wilsond1b48c12017-08-16 09:52:08 +0100780 err = radix_tree_insert(handles_vma, handle, vma);
781 if (unlikely(err)) {
Xidong Wang6be11872018-04-04 10:38:24 +0100782 kmem_cache_free(eb->i915->luts, lut);
Chris Wilsond1b48c12017-08-16 09:52:08 +0100783 goto err_obj;
784 }
785
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100786 /* transfer ref to ctx */
Chris Wilson3365e222018-05-03 20:51:14 +0100787 if (!vma->open_count++)
788 i915_vma_reopen(vma);
Chris Wilsond1b48c12017-08-16 09:52:08 +0100789 list_add(&lut->obj_link, &obj->lut_list);
790 list_add(&lut->ctx_link, &eb->ctx->handles_list);
791 lut->ctx = eb->ctx;
792 lut->handle = handle;
793
Chris Wilson170fa292017-08-16 09:52:07 +0100794add_vma:
Chris Wilson7ba33e12018-06-10 20:43:09 +0100795 err = eb_add_vma(eb, i, batch, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100796 if (unlikely(err))
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100797 goto err_vma;
Chris Wilsondade2a62017-06-16 15:05:20 +0100798
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100799 GEM_BUG_ON(vma != eb->vma[i]);
800 GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
Chris Wilson7ba33e12018-06-10 20:43:09 +0100801 GEM_BUG_ON(drm_mm_node_allocated(&vma->node) &&
802 eb_vma_misplaced(&eb->exec[i], vma, eb->flags[i]));
Chris Wilson4ff4b442017-06-16 15:05:16 +0100803 }
804
Chris Wilson2889caa2017-06-16 15:05:19 +0100805 eb->args->flags |= __EXEC_VALIDATED;
806 return eb_reserve(eb);
807
Chris Wilson170fa292017-08-16 09:52:07 +0100808err_obj:
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100809 i915_gem_object_put(obj);
Chris Wilson170fa292017-08-16 09:52:07 +0100810err_vma:
811 eb->vma[i] = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100812 return err;
Chris Wilson59bfa122016-08-04 16:32:31 +0100813}
814
Chris Wilson4ff4b442017-06-16 15:05:16 +0100815static struct i915_vma *
Chris Wilson2889caa2017-06-16 15:05:19 +0100816eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000817{
Chris Wilson2889caa2017-06-16 15:05:19 +0100818 if (eb->lut_size < 0) {
819 if (handle >= -eb->lut_size)
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000820 return NULL;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100821 return eb->vma[handle];
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000822 } else {
823 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800824 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000825
Chris Wilson2889caa2017-06-16 15:05:19 +0100826 head = &eb->buckets[hash_32(handle, eb->lut_size)];
Geliang Tangaa459502016-01-18 23:54:20 +0800827 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200828 if (vma->exec_handle == handle)
829 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000830 }
831 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000832 }
Chris Wilson67731b82010-12-08 10:38:14 +0000833}
834
Chris Wilson2889caa2017-06-16 15:05:19 +0100835static void eb_release_vmas(const struct i915_execbuffer *eb)
Chris Wilsona415d352013-11-26 11:23:15 +0000836{
Chris Wilson2889caa2017-06-16 15:05:19 +0100837 const unsigned int count = eb->buffer_count;
838 unsigned int i;
Chris Wilson650bc632017-06-15 09:14:33 +0100839
Chris Wilson2889caa2017-06-16 15:05:19 +0100840 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100841 struct i915_vma *vma = eb->vma[i];
842 unsigned int flags = eb->flags[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100843
844 if (!vma)
Chris Wilson170fa292017-08-16 09:52:07 +0100845 break;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000846
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100847 GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
848 vma->exec_flags = NULL;
849 eb->vma[i] = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100850
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100851 if (flags & __EXEC_OBJECT_HAS_PIN)
852 __eb_unreserve_vma(vma, flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100853
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100854 if (flags & __EXEC_OBJECT_HAS_REF)
Chris Wilsondade2a62017-06-16 15:05:20 +0100855 i915_vma_put(vma);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000856 }
Chris Wilson2889caa2017-06-16 15:05:19 +0100857}
Chris Wilsond55495b2017-06-15 09:14:34 +0100858
Chris Wilson2889caa2017-06-16 15:05:19 +0100859static void eb_reset_vmas(const struct i915_execbuffer *eb)
860{
861 eb_release_vmas(eb);
Chris Wilson4d470f72017-06-29 16:04:25 +0100862 if (eb->lut_size > 0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100863 memset(eb->buckets, 0,
864 sizeof(struct hlist_head) << eb->lut_size);
865}
Chris Wilsond55495b2017-06-15 09:14:34 +0100866
Chris Wilson2889caa2017-06-16 15:05:19 +0100867static void eb_destroy(const struct i915_execbuffer *eb)
868{
Chris Wilson7dd4f672017-06-16 15:05:24 +0100869 GEM_BUG_ON(eb->reloc_cache.rq);
870
Chris Wilson4d470f72017-06-29 16:04:25 +0100871 if (eb->lut_size > 0)
Chris Wilsond55495b2017-06-15 09:14:34 +0100872 kfree(eb->buckets);
Chris Wilson67731b82010-12-08 10:38:14 +0000873}
874
Chris Wilson2889caa2017-06-16 15:05:19 +0100875static inline u64
Chris Wilsond50415c2016-08-18 17:16:52 +0100876relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +0100877 const struct i915_vma *target)
Michał Winiarski934acce2015-12-29 18:24:52 +0100878{
Chris Wilson2889caa2017-06-16 15:05:19 +0100879 return gen8_canonical_addr((int)reloc->delta + target->node.start);
Michał Winiarski934acce2015-12-29 18:24:52 +0100880}
881
Chris Wilsond50415c2016-08-18 17:16:52 +0100882static void reloc_cache_init(struct reloc_cache *cache,
883 struct drm_i915_private *i915)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100884{
Chris Wilson31a39202016-08-18 17:16:46 +0100885 cache->page = -1;
Chris Wilsond50415c2016-08-18 17:16:52 +0100886 cache->vaddr = 0;
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200887 /* Must be a variable in the struct to allow GCC to unroll. */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100888 cache->gen = INTEL_GEN(i915);
Chris Wilson2889caa2017-06-16 15:05:19 +0100889 cache->has_llc = HAS_LLC(i915);
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200890 cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
Chris Wilson7dd4f672017-06-16 15:05:24 +0100891 cache->has_fence = cache->gen < 4;
892 cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
Chris Wilsone8cb9092016-08-18 17:16:53 +0100893 cache->node.allocated = false;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100894 cache->rq = NULL;
895 cache->rq_size = 0;
Chris Wilson31a39202016-08-18 17:16:46 +0100896}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100897
Chris Wilsond50415c2016-08-18 17:16:52 +0100898static inline void *unmask_page(unsigned long p)
899{
900 return (void *)(uintptr_t)(p & PAGE_MASK);
901}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100902
Chris Wilsond50415c2016-08-18 17:16:52 +0100903static inline unsigned int unmask_flags(unsigned long p)
904{
905 return p & ~PAGE_MASK;
906}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700907
Chris Wilsond50415c2016-08-18 17:16:52 +0100908#define KMAP 0x4 /* after CLFLUSH_FLAGS */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700909
Chris Wilson650bc632017-06-15 09:14:33 +0100910static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
911{
912 struct drm_i915_private *i915 =
913 container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
914 return &i915->ggtt;
915}
916
Chris Wilson7dd4f672017-06-16 15:05:24 +0100917static void reloc_gpu_flush(struct reloc_cache *cache)
918{
919 GEM_BUG_ON(cache->rq_size >= cache->rq->batch->obj->base.size / sizeof(u32));
920 cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END;
921 i915_gem_object_unpin_map(cache->rq->batch->obj);
922 i915_gem_chipset_flush(cache->rq->i915);
923
Chris Wilsone61e0f52018-02-21 09:56:36 +0000924 __i915_request_add(cache->rq, true);
Chris Wilson7dd4f672017-06-16 15:05:24 +0100925 cache->rq = NULL;
926}
927
Chris Wilson650bc632017-06-15 09:14:33 +0100928static void reloc_cache_reset(struct reloc_cache *cache)
Chris Wilson31a39202016-08-18 17:16:46 +0100929{
Chris Wilsond50415c2016-08-18 17:16:52 +0100930 void *vaddr;
931
Chris Wilson7dd4f672017-06-16 15:05:24 +0100932 if (cache->rq)
933 reloc_gpu_flush(cache);
934
Chris Wilson31a39202016-08-18 17:16:46 +0100935 if (!cache->vaddr)
936 return;
937
Chris Wilsond50415c2016-08-18 17:16:52 +0100938 vaddr = unmask_page(cache->vaddr);
939 if (cache->vaddr & KMAP) {
940 if (cache->vaddr & CLFLUSH_AFTER)
941 mb();
Chris Wilson31a39202016-08-18 17:16:46 +0100942
Chris Wilsond50415c2016-08-18 17:16:52 +0100943 kunmap_atomic(vaddr);
944 i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
945 } else {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100946 wmb();
Chris Wilsond50415c2016-08-18 17:16:52 +0100947 io_mapping_unmap_atomic((void __iomem *)vaddr);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100948 if (cache->node.allocated) {
Chris Wilson650bc632017-06-15 09:14:33 +0100949 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100950
951 ggtt->base.clear_range(&ggtt->base,
952 cache->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200953 cache->node.size);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100954 drm_mm_remove_node(&cache->node);
955 } else {
956 i915_vma_unpin((struct i915_vma *)cache->node.mm);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700957 }
Chris Wilson31a39202016-08-18 17:16:46 +0100958 }
Chris Wilson650bc632017-06-15 09:14:33 +0100959
960 cache->vaddr = 0;
961 cache->page = -1;
Chris Wilson31a39202016-08-18 17:16:46 +0100962}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700963
Chris Wilson31a39202016-08-18 17:16:46 +0100964static void *reloc_kmap(struct drm_i915_gem_object *obj,
965 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100966 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100967{
Chris Wilsond50415c2016-08-18 17:16:52 +0100968 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100969
Chris Wilsond50415c2016-08-18 17:16:52 +0100970 if (cache->vaddr) {
971 kunmap_atomic(unmask_page(cache->vaddr));
972 } else {
973 unsigned int flushes;
Chris Wilson2889caa2017-06-16 15:05:19 +0100974 int err;
Chris Wilson31a39202016-08-18 17:16:46 +0100975
Chris Wilson2889caa2017-06-16 15:05:19 +0100976 err = i915_gem_obj_prepare_shmem_write(obj, &flushes);
977 if (err)
978 return ERR_PTR(err);
Chris Wilsond50415c2016-08-18 17:16:52 +0100979
980 BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
981 BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
982
983 cache->vaddr = flushes | KMAP;
984 cache->node.mm = (void *)obj;
985 if (flushes)
986 mb();
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700987 }
988
Chris Wilsond50415c2016-08-18 17:16:52 +0100989 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
990 cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100991 cache->page = page;
Chris Wilson31a39202016-08-18 17:16:46 +0100992
Chris Wilsond50415c2016-08-18 17:16:52 +0100993 return vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100994}
995
Chris Wilsond50415c2016-08-18 17:16:52 +0100996static void *reloc_iomap(struct drm_i915_gem_object *obj,
Chris Wilson31a39202016-08-18 17:16:46 +0100997 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100998 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100999{
Chris Wilson650bc632017-06-15 09:14:33 +01001000 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001001 unsigned long offset;
Chris Wilsond50415c2016-08-18 17:16:52 +01001002 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +01001003
Chris Wilsond50415c2016-08-18 17:16:52 +01001004 if (cache->vaddr) {
Jani Nikula615e5002016-10-04 12:54:13 +03001005 io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
Chris Wilsond50415c2016-08-18 17:16:52 +01001006 } else {
1007 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001008 int err;
Chris Wilson31a39202016-08-18 17:16:46 +01001009
Chris Wilson2889caa2017-06-16 15:05:19 +01001010 if (use_cpu_reloc(cache, obj))
Chris Wilsond50415c2016-08-18 17:16:52 +01001011 return NULL;
Chris Wilson31a39202016-08-18 17:16:46 +01001012
Chris Wilson2889caa2017-06-16 15:05:19 +01001013 err = i915_gem_object_set_to_gtt_domain(obj, true);
1014 if (err)
1015 return ERR_PTR(err);
Chris Wilson31a39202016-08-18 17:16:46 +01001016
Chris Wilsond50415c2016-08-18 17:16:52 +01001017 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilson3c755c52017-10-09 09:43:59 +01001018 PIN_MAPPABLE |
1019 PIN_NONBLOCK |
1020 PIN_NONFAULT);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001021 if (IS_ERR(vma)) {
1022 memset(&cache->node, 0, sizeof(cache->node));
Chris Wilson2889caa2017-06-16 15:05:19 +01001023 err = drm_mm_insert_node_in_range
Chris Wilsone8cb9092016-08-18 17:16:53 +01001024 (&ggtt->base.mm, &cache->node,
Chris Wilsonf51455d2017-01-10 14:47:34 +00001025 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
Chris Wilsone8cb9092016-08-18 17:16:53 +01001026 0, ggtt->mappable_end,
Chris Wilson4e64e552017-02-02 21:04:38 +00001027 DRM_MM_INSERT_LOW);
Chris Wilson2889caa2017-06-16 15:05:19 +01001028 if (err) /* no inactive aperture space, use cpu reloc */
Chris Wilsonc92fa4f2016-10-07 07:53:25 +01001029 return NULL;
Chris Wilsone8cb9092016-08-18 17:16:53 +01001030 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +01001031 err = i915_vma_put_fence(vma);
1032 if (err) {
Chris Wilsone8cb9092016-08-18 17:16:53 +01001033 i915_vma_unpin(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001034 return ERR_PTR(err);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001035 }
Rafael Barbalho5032d872013-08-21 17:10:51 +01001036
Chris Wilsone8cb9092016-08-18 17:16:53 +01001037 cache->node.start = vma->node.start;
1038 cache->node.mm = (void *)vma;
Chris Wilsond50415c2016-08-18 17:16:52 +01001039 }
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001040 }
1041
Chris Wilsone8cb9092016-08-18 17:16:53 +01001042 offset = cache->node.start;
1043 if (cache->node.allocated) {
Chris Wilsonfc099092016-10-28 15:27:56 +01001044 wmb();
Chris Wilsone8cb9092016-08-18 17:16:53 +01001045 ggtt->base.insert_page(&ggtt->base,
1046 i915_gem_object_get_dma_address(obj, page),
1047 offset, I915_CACHE_NONE, 0);
1048 } else {
1049 offset += page << PAGE_SHIFT;
1050 }
1051
Matthew Auld73ebd502017-12-11 15:18:20 +00001052 vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->iomap,
Chris Wilson650bc632017-06-15 09:14:33 +01001053 offset);
Chris Wilsond50415c2016-08-18 17:16:52 +01001054 cache->page = page;
1055 cache->vaddr = (unsigned long)vaddr;
1056
1057 return vaddr;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001058}
1059
Chris Wilsond50415c2016-08-18 17:16:52 +01001060static void *reloc_vaddr(struct drm_i915_gem_object *obj,
1061 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001062 unsigned long page)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001063{
Chris Wilsond50415c2016-08-18 17:16:52 +01001064 void *vaddr;
1065
1066 if (cache->page == page) {
1067 vaddr = unmask_page(cache->vaddr);
1068 } else {
1069 vaddr = NULL;
1070 if ((cache->vaddr & KMAP) == 0)
1071 vaddr = reloc_iomap(obj, cache, page);
1072 if (!vaddr)
1073 vaddr = reloc_kmap(obj, cache, page);
1074 }
1075
1076 return vaddr;
1077}
1078
1079static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
1080{
1081 if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
1082 if (flushes & CLFLUSH_BEFORE) {
1083 clflushopt(addr);
1084 mb();
1085 }
1086
1087 *addr = value;
1088
Chris Wilson2889caa2017-06-16 15:05:19 +01001089 /*
1090 * Writes to the same cacheline are serialised by the CPU
Chris Wilsond50415c2016-08-18 17:16:52 +01001091 * (including clflush). On the write path, we only require
1092 * that it hits memory in an orderly fashion and place
1093 * mb barriers at the start and end of the relocation phase
1094 * to ensure ordering of clflush wrt to the system.
1095 */
1096 if (flushes & CLFLUSH_AFTER)
1097 clflushopt(addr);
1098 } else
1099 *addr = value;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001100}
1101
Chris Wilson7dd4f672017-06-16 15:05:24 +01001102static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
1103 struct i915_vma *vma,
1104 unsigned int len)
1105{
1106 struct reloc_cache *cache = &eb->reloc_cache;
1107 struct drm_i915_gem_object *obj;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001108 struct i915_request *rq;
Chris Wilson7dd4f672017-06-16 15:05:24 +01001109 struct i915_vma *batch;
1110 u32 *cmd;
1111 int err;
1112
Christian Königc0a51fd2018-02-16 13:43:38 +01001113 GEM_BUG_ON(vma->obj->write_domain & I915_GEM_DOMAIN_CPU);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001114
1115 obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, PAGE_SIZE);
1116 if (IS_ERR(obj))
1117 return PTR_ERR(obj);
1118
1119 cmd = i915_gem_object_pin_map(obj,
Chris Wilsona575c672017-08-28 11:46:31 +01001120 cache->has_llc ?
1121 I915_MAP_FORCE_WB :
1122 I915_MAP_FORCE_WC);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001123 i915_gem_object_unpin_pages(obj);
1124 if (IS_ERR(cmd))
1125 return PTR_ERR(cmd);
1126
1127 err = i915_gem_object_set_to_wc_domain(obj, false);
1128 if (err)
1129 goto err_unmap;
1130
1131 batch = i915_vma_instance(obj, vma->vm, NULL);
1132 if (IS_ERR(batch)) {
1133 err = PTR_ERR(batch);
1134 goto err_unmap;
1135 }
1136
1137 err = i915_vma_pin(batch, 0, 0, PIN_USER | PIN_NONBLOCK);
1138 if (err)
1139 goto err_unmap;
1140
Chris Wilsone61e0f52018-02-21 09:56:36 +00001141 rq = i915_request_alloc(eb->engine, eb->ctx);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001142 if (IS_ERR(rq)) {
1143 err = PTR_ERR(rq);
1144 goto err_unpin;
1145 }
1146
Chris Wilsone61e0f52018-02-21 09:56:36 +00001147 err = i915_request_await_object(rq, vma->obj, true);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001148 if (err)
1149 goto err_request;
1150
Chris Wilson7dd4f672017-06-16 15:05:24 +01001151 err = eb->engine->emit_bb_start(rq,
1152 batch->node.start, PAGE_SIZE,
1153 cache->gen > 5 ? 0 : I915_DISPATCH_SECURE);
1154 if (err)
1155 goto err_request;
1156
Chris Wilson95ff7c72017-06-16 15:05:25 +01001157 GEM_BUG_ON(!reservation_object_test_signaled_rcu(batch->resv, true));
Chris Wilson7dd4f672017-06-16 15:05:24 +01001158 i915_vma_move_to_active(batch, rq, 0);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001159 reservation_object_lock(batch->resv, NULL);
1160 reservation_object_add_excl_fence(batch->resv, &rq->fence);
1161 reservation_object_unlock(batch->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001162 i915_vma_unpin(batch);
1163
Chris Wilson25ffaa62017-06-20 13:43:20 +01001164 i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001165 reservation_object_lock(vma->resv, NULL);
1166 reservation_object_add_excl_fence(vma->resv, &rq->fence);
1167 reservation_object_unlock(vma->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001168
1169 rq->batch = batch;
1170
1171 cache->rq = rq;
1172 cache->rq_cmd = cmd;
1173 cache->rq_size = 0;
1174
1175 /* Return with batch mapping (cmd) still pinned */
1176 return 0;
1177
1178err_request:
Chris Wilsone61e0f52018-02-21 09:56:36 +00001179 i915_request_add(rq);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001180err_unpin:
1181 i915_vma_unpin(batch);
1182err_unmap:
1183 i915_gem_object_unpin_map(obj);
1184 return err;
1185}
1186
1187static u32 *reloc_gpu(struct i915_execbuffer *eb,
1188 struct i915_vma *vma,
1189 unsigned int len)
1190{
1191 struct reloc_cache *cache = &eb->reloc_cache;
1192 u32 *cmd;
1193
1194 if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1))
1195 reloc_gpu_flush(cache);
1196
1197 if (unlikely(!cache->rq)) {
1198 int err;
1199
Chris Wilson3dbf26e2017-08-26 14:56:20 +01001200 /* If we need to copy for the cmdparser, we will stall anyway */
1201 if (eb_use_cmdparser(eb))
1202 return ERR_PTR(-EWOULDBLOCK);
1203
Chris Wilson90cad092017-09-06 16:28:59 +01001204 if (!intel_engine_can_store_dword(eb->engine))
1205 return ERR_PTR(-ENODEV);
1206
Chris Wilson7dd4f672017-06-16 15:05:24 +01001207 err = __reloc_gpu_alloc(eb, vma, len);
1208 if (unlikely(err))
1209 return ERR_PTR(err);
1210 }
1211
1212 cmd = cache->rq_cmd + cache->rq_size;
1213 cache->rq_size += len;
1214
1215 return cmd;
1216}
1217
Chris Wilson2889caa2017-06-16 15:05:19 +01001218static u64
1219relocate_entry(struct i915_vma *vma,
Chris Wilsond50415c2016-08-18 17:16:52 +01001220 const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +01001221 struct i915_execbuffer *eb,
1222 const struct i915_vma *target)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001223{
Chris Wilsond50415c2016-08-18 17:16:52 +01001224 u64 offset = reloc->offset;
Chris Wilson2889caa2017-06-16 15:05:19 +01001225 u64 target_offset = relocation_target(reloc, target);
1226 bool wide = eb->reloc_cache.use_64bit_reloc;
Chris Wilsond50415c2016-08-18 17:16:52 +01001227 void *vaddr;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001228
Chris Wilson7dd4f672017-06-16 15:05:24 +01001229 if (!eb->reloc_cache.vaddr &&
1230 (DBG_FORCE_RELOC == FORCE_GPU_RELOC ||
Chris Wilson90cad092017-09-06 16:28:59 +01001231 !reservation_object_test_signaled_rcu(vma->resv, true))) {
Chris Wilson7dd4f672017-06-16 15:05:24 +01001232 const unsigned int gen = eb->reloc_cache.gen;
1233 unsigned int len;
1234 u32 *batch;
1235 u64 addr;
1236
1237 if (wide)
1238 len = offset & 7 ? 8 : 5;
1239 else if (gen >= 4)
1240 len = 4;
Chris Wilsonf2f5c062017-08-16 09:52:04 +01001241 else
Chris Wilson7dd4f672017-06-16 15:05:24 +01001242 len = 3;
Chris Wilson7dd4f672017-06-16 15:05:24 +01001243
1244 batch = reloc_gpu(eb, vma, len);
1245 if (IS_ERR(batch))
1246 goto repeat;
1247
1248 addr = gen8_canonical_addr(vma->node.start + offset);
1249 if (wide) {
1250 if (offset & 7) {
1251 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1252 *batch++ = lower_32_bits(addr);
1253 *batch++ = upper_32_bits(addr);
1254 *batch++ = lower_32_bits(target_offset);
1255
1256 addr = gen8_canonical_addr(addr + 4);
1257
1258 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1259 *batch++ = lower_32_bits(addr);
1260 *batch++ = upper_32_bits(addr);
1261 *batch++ = upper_32_bits(target_offset);
1262 } else {
1263 *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1;
1264 *batch++ = lower_32_bits(addr);
1265 *batch++ = upper_32_bits(addr);
1266 *batch++ = lower_32_bits(target_offset);
1267 *batch++ = upper_32_bits(target_offset);
1268 }
1269 } else if (gen >= 6) {
1270 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1271 *batch++ = 0;
1272 *batch++ = addr;
1273 *batch++ = target_offset;
1274 } else if (gen >= 4) {
1275 *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1276 *batch++ = 0;
1277 *batch++ = addr;
1278 *batch++ = target_offset;
1279 } else {
1280 *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
1281 *batch++ = addr;
1282 *batch++ = target_offset;
1283 }
1284
1285 goto out;
1286 }
1287
Chris Wilsond50415c2016-08-18 17:16:52 +01001288repeat:
Chris Wilson95ff7c72017-06-16 15:05:25 +01001289 vaddr = reloc_vaddr(vma->obj, &eb->reloc_cache, offset >> PAGE_SHIFT);
Chris Wilsond50415c2016-08-18 17:16:52 +01001290 if (IS_ERR(vaddr))
1291 return PTR_ERR(vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001292
Chris Wilsond50415c2016-08-18 17:16:52 +01001293 clflush_write32(vaddr + offset_in_page(offset),
1294 lower_32_bits(target_offset),
Chris Wilson2889caa2017-06-16 15:05:19 +01001295 eb->reloc_cache.vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001296
Chris Wilsond50415c2016-08-18 17:16:52 +01001297 if (wide) {
1298 offset += sizeof(u32);
1299 target_offset >>= 32;
1300 wide = false;
1301 goto repeat;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001302 }
Chris Wilsondabdfe02012-03-26 10:10:27 +02001303
Chris Wilson7dd4f672017-06-16 15:05:24 +01001304out:
Chris Wilson2889caa2017-06-16 15:05:19 +01001305 return target->node.start | UPDATE;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001306}
1307
Chris Wilson2889caa2017-06-16 15:05:19 +01001308static u64
1309eb_relocate_entry(struct i915_execbuffer *eb,
1310 struct i915_vma *vma,
1311 const struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001312{
Chris Wilson507d9772017-06-16 15:05:17 +01001313 struct i915_vma *target;
Chris Wilson2889caa2017-06-16 15:05:19 +01001314 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001315
Chris Wilson67731b82010-12-08 10:38:14 +00001316 /* we've already hold a reference to all valid objects */
Chris Wilson507d9772017-06-16 15:05:17 +01001317 target = eb_get_vma(eb, reloc->target_handle);
1318 if (unlikely(!target))
Chris Wilson54cf91d2010-11-25 18:00:26 +00001319 return -ENOENT;
Eric Anholte844b992012-07-31 15:35:01 -07001320
Chris Wilson54cf91d2010-11-25 18:00:26 +00001321 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001322 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001323 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001324 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001325 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001326 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001327 (int) reloc->offset,
1328 reloc->read_domains,
1329 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001330 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001331 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +01001332 if (unlikely((reloc->write_domain | reloc->read_domains)
1333 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001334 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001335 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001336 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001337 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001338 (int) reloc->offset,
1339 reloc->read_domains,
1340 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001341 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001342 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001343
Chris Wilson2889caa2017-06-16 15:05:19 +01001344 if (reloc->write_domain) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001345 *target->exec_flags |= EXEC_OBJECT_WRITE;
Chris Wilson507d9772017-06-16 15:05:17 +01001346
Chris Wilson2889caa2017-06-16 15:05:19 +01001347 /*
1348 * Sandybridge PPGTT errata: We need a global gtt mapping
1349 * for MI and pipe_control writes because the gpu doesn't
1350 * properly redirect them through the ppgtt for non_secure
1351 * batchbuffers.
1352 */
1353 if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
1354 IS_GEN6(eb->i915)) {
1355 err = i915_vma_bind(target, target->obj->cache_level,
1356 PIN_GLOBAL);
1357 if (WARN_ONCE(err,
1358 "Unexpected failure to bind target VMA!"))
1359 return err;
1360 }
Chris Wilson507d9772017-06-16 15:05:17 +01001361 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001362
Chris Wilson2889caa2017-06-16 15:05:19 +01001363 /*
1364 * If the relocation already has the right value in it, no
Chris Wilson54cf91d2010-11-25 18:00:26 +00001365 * more work needs to be done.
1366 */
Chris Wilson7dd4f672017-06-16 15:05:24 +01001367 if (!DBG_FORCE_RELOC &&
1368 gen8_canonical_addr(target->node.start) == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +00001369 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001370
1371 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001372 if (unlikely(reloc->offset >
Chris Wilson507d9772017-06-16 15:05:17 +01001373 vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001374 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson507d9772017-06-16 15:05:17 +01001375 "target %d offset %d size %d.\n",
1376 reloc->target_handle,
1377 (int)reloc->offset,
1378 (int)vma->size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001379 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001380 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001381 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001382 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson507d9772017-06-16 15:05:17 +01001383 "target %d offset %d.\n",
1384 reloc->target_handle,
1385 (int)reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001386 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001387 }
1388
Chris Wilson071750e2017-06-16 15:05:18 +01001389 /*
1390 * If we write into the object, we need to force the synchronisation
1391 * barrier, either with an asynchronous clflush or if we executed the
1392 * patching using the GPU (though that should be serialised by the
1393 * timeline). To be completely sure, and since we are required to
1394 * do relocations we are already stalling, disable the user's opt
Chris Wilson0519bcb2017-08-16 09:52:09 +01001395 * out of our synchronisation.
Chris Wilson071750e2017-06-16 15:05:18 +01001396 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001397 *vma->exec_flags &= ~EXEC_OBJECT_ASYNC;
Chris Wilson071750e2017-06-16 15:05:18 +01001398
Chris Wilson54cf91d2010-11-25 18:00:26 +00001399 /* and update the user's relocation entry */
Chris Wilson2889caa2017-06-16 15:05:19 +01001400 return relocate_entry(vma, reloc, eb, target);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001401}
1402
Chris Wilson2889caa2017-06-16 15:05:19 +01001403static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001404{
Chris Wilson1d83f442012-03-24 20:12:53 +00001405#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
Chris Wilson2889caa2017-06-16 15:05:19 +01001406 struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
1407 struct drm_i915_gem_relocation_entry __user *urelocs;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001408 const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001409 unsigned int remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001410
Chris Wilson2889caa2017-06-16 15:05:19 +01001411 urelocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson1d83f442012-03-24 20:12:53 +00001412 remain = entry->relocation_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01001413 if (unlikely(remain > N_RELOC(ULONG_MAX)))
1414 return -EINVAL;
Chris Wilsonebc08082016-10-18 13:02:51 +01001415
Chris Wilson2889caa2017-06-16 15:05:19 +01001416 /*
1417 * We must check that the entire relocation array is safe
1418 * to read. However, if the array is not writable the user loses
1419 * the updated relocation values.
1420 */
Imre Deakedd90032017-07-14 18:12:42 +03001421 if (unlikely(!access_ok(VERIFY_READ, urelocs, remain*sizeof(*urelocs))))
Chris Wilson2889caa2017-06-16 15:05:19 +01001422 return -EFAULT;
Chris Wilson1d83f442012-03-24 20:12:53 +00001423
Chris Wilson2889caa2017-06-16 15:05:19 +01001424 do {
1425 struct drm_i915_gem_relocation_entry *r = stack;
1426 unsigned int count =
1427 min_t(unsigned int, remain, ARRAY_SIZE(stack));
1428 unsigned int copied;
1429
1430 /*
1431 * This is the fast path and we cannot handle a pagefault
Chris Wilsonebc08082016-10-18 13:02:51 +01001432 * whilst holding the struct mutex lest the user pass in the
1433 * relocations contained within a mmaped bo. For in such a case
1434 * we, the page fault handler would call i915_gem_fault() and
1435 * we would try to acquire the struct mutex again. Obviously
1436 * this is bad and so lockdep complains vehemently.
1437 */
1438 pagefault_disable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001439 copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
Chris Wilsonebc08082016-10-18 13:02:51 +01001440 pagefault_enable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001441 if (unlikely(copied)) {
1442 remain = -EFAULT;
Chris Wilson31a39202016-08-18 17:16:46 +01001443 goto out;
1444 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001445
Chris Wilson2889caa2017-06-16 15:05:19 +01001446 remain -= count;
Chris Wilson1d83f442012-03-24 20:12:53 +00001447 do {
Chris Wilson2889caa2017-06-16 15:05:19 +01001448 u64 offset = eb_relocate_entry(eb, vma, r);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001449
Chris Wilson2889caa2017-06-16 15:05:19 +01001450 if (likely(offset == 0)) {
1451 } else if ((s64)offset < 0) {
1452 remain = (int)offset;
Chris Wilson31a39202016-08-18 17:16:46 +01001453 goto out;
Chris Wilson2889caa2017-06-16 15:05:19 +01001454 } else {
1455 /*
1456 * Note that reporting an error now
1457 * leaves everything in an inconsistent
1458 * state as we have *already* changed
1459 * the relocation value inside the
1460 * object. As we have not changed the
1461 * reloc.presumed_offset or will not
1462 * change the execobject.offset, on the
1463 * call we may not rewrite the value
1464 * inside the object, leaving it
1465 * dangling and causing a GPU hang. Unless
1466 * userspace dynamically rebuilds the
1467 * relocations on each execbuf rather than
1468 * presume a static tree.
1469 *
1470 * We did previously check if the relocations
1471 * were writable (access_ok), an error now
1472 * would be a strange race with mprotect,
1473 * having already demonstrated that we
1474 * can read from this userspace address.
1475 */
1476 offset = gen8_canonical_addr(offset & ~UPDATE);
1477 __put_user(offset,
1478 &urelocs[r-stack].presumed_offset);
Chris Wilson1d83f442012-03-24 20:12:53 +00001479 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001480 } while (r++, --count);
1481 urelocs += ARRAY_SIZE(stack);
1482 } while (remain);
Chris Wilson31a39202016-08-18 17:16:46 +01001483out:
Chris Wilson650bc632017-06-15 09:14:33 +01001484 reloc_cache_reset(&eb->reloc_cache);
Chris Wilson2889caa2017-06-16 15:05:19 +01001485 return remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001486}
1487
1488static int
Chris Wilson2889caa2017-06-16 15:05:19 +01001489eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001490{
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001491 const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001492 struct drm_i915_gem_relocation_entry *relocs =
1493 u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1494 unsigned int i;
1495 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001496
1497 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001498 u64 offset = eb_relocate_entry(eb, vma, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001499
Chris Wilson2889caa2017-06-16 15:05:19 +01001500 if ((s64)offset < 0) {
1501 err = (int)offset;
1502 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001503 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001504 }
1505 err = 0;
Chris Wilsona415d352013-11-26 11:23:15 +00001506err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001507 reloc_cache_reset(&eb->reloc_cache);
1508 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001509}
1510
Chris Wilson2889caa2017-06-16 15:05:19 +01001511static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001512{
Chris Wilson2889caa2017-06-16 15:05:19 +01001513 const char __user *addr, *end;
1514 unsigned long size;
1515 char __maybe_unused c;
Ben Widawsky27173f12013-08-14 11:38:36 +02001516
Chris Wilson2889caa2017-06-16 15:05:19 +01001517 size = entry->relocation_count;
1518 if (size == 0)
1519 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001520
Chris Wilson2889caa2017-06-16 15:05:19 +01001521 if (size > N_RELOC(ULONG_MAX))
1522 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001523
Chris Wilson2889caa2017-06-16 15:05:19 +01001524 addr = u64_to_user_ptr(entry->relocs_ptr);
1525 size *= sizeof(struct drm_i915_gem_relocation_entry);
1526 if (!access_ok(VERIFY_READ, addr, size))
1527 return -EFAULT;
1528
1529 end = addr + size;
1530 for (; addr < end; addr += PAGE_SIZE) {
1531 int err = __get_user(c, addr);
1532 if (err)
1533 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001534 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001535 return __get_user(c, end - 1);
1536}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001537
Chris Wilson2889caa2017-06-16 15:05:19 +01001538static int eb_copy_relocations(const struct i915_execbuffer *eb)
1539{
1540 const unsigned int count = eb->buffer_count;
1541 unsigned int i;
1542 int err;
1543
Chris Wilson54cf91d2010-11-25 18:00:26 +00001544 for (i = 0; i < count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001545 const unsigned int nreloc = eb->exec[i].relocation_count;
1546 struct drm_i915_gem_relocation_entry __user *urelocs;
1547 struct drm_i915_gem_relocation_entry *relocs;
1548 unsigned long size;
1549 unsigned long copied;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001550
Chris Wilson2889caa2017-06-16 15:05:19 +01001551 if (nreloc == 0)
1552 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001553
Chris Wilson2889caa2017-06-16 15:05:19 +01001554 err = check_relocations(&eb->exec[i]);
1555 if (err)
1556 goto err;
1557
1558 urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
1559 size = nreloc * sizeof(*relocs);
1560
Michal Hocko0ee931c2017-09-13 16:28:29 -07001561 relocs = kvmalloc_array(size, 1, GFP_KERNEL);
Chris Wilson2889caa2017-06-16 15:05:19 +01001562 if (!relocs) {
1563 kvfree(relocs);
1564 err = -ENOMEM;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001565 goto err;
1566 }
1567
Chris Wilson2889caa2017-06-16 15:05:19 +01001568 /* copy_from_user is limited to < 4GiB */
1569 copied = 0;
1570 do {
1571 unsigned int len =
1572 min_t(u64, BIT_ULL(31), size - copied);
1573
1574 if (__copy_from_user((char *)relocs + copied,
Ville Syrjälä908a6102017-09-01 19:54:34 +03001575 (char __user *)urelocs + copied,
Chris Wilson2889caa2017-06-16 15:05:19 +01001576 len)) {
1577 kvfree(relocs);
1578 err = -EFAULT;
1579 goto err;
1580 }
1581
1582 copied += len;
1583 } while (copied < size);
1584
1585 /*
1586 * As we do not update the known relocation offsets after
Chris Wilson262b6d32013-01-15 16:17:54 +00001587 * relocating (due to the complexities in lock handling),
1588 * we need to mark them as invalid now so that we force the
1589 * relocation processing next time. Just in case the target
1590 * object is evicted and then rebound into its old
1591 * presumed_offset before the next execbuffer - if that
1592 * happened we would make the mistake of assuming that the
1593 * relocations were valid.
1594 */
Chris Wilson2889caa2017-06-16 15:05:19 +01001595 user_access_begin();
1596 for (copied = 0; copied < nreloc; copied++)
1597 unsafe_put_user(-1,
1598 &urelocs[copied].presumed_offset,
1599 end_user);
1600end_user:
1601 user_access_end();
Chris Wilson262b6d32013-01-15 16:17:54 +00001602
Chris Wilson2889caa2017-06-16 15:05:19 +01001603 eb->exec[i].relocs_ptr = (uintptr_t)relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001604 }
1605
Chris Wilson2889caa2017-06-16 15:05:19 +01001606 return 0;
1607
1608err:
1609 while (i--) {
1610 struct drm_i915_gem_relocation_entry *relocs =
1611 u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr);
1612 if (eb->exec[i].relocation_count)
1613 kvfree(relocs);
1614 }
1615 return err;
1616}
1617
1618static int eb_prefault_relocations(const struct i915_execbuffer *eb)
1619{
1620 const unsigned int count = eb->buffer_count;
1621 unsigned int i;
1622
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001623 if (unlikely(i915_modparams.prefault_disable))
Chris Wilson2889caa2017-06-16 15:05:19 +01001624 return 0;
1625
1626 for (i = 0; i < count; i++) {
1627 int err;
1628
1629 err = check_relocations(&eb->exec[i]);
1630 if (err)
1631 return err;
1632 }
1633
1634 return 0;
1635}
1636
1637static noinline int eb_relocate_slow(struct i915_execbuffer *eb)
1638{
1639 struct drm_device *dev = &eb->i915->drm;
1640 bool have_copy = false;
1641 struct i915_vma *vma;
1642 int err = 0;
1643
1644repeat:
1645 if (signal_pending(current)) {
1646 err = -ERESTARTSYS;
1647 goto out;
1648 }
1649
1650 /* We may process another execbuffer during the unlock... */
1651 eb_reset_vmas(eb);
1652 mutex_unlock(&dev->struct_mutex);
1653
1654 /*
1655 * We take 3 passes through the slowpatch.
1656 *
1657 * 1 - we try to just prefault all the user relocation entries and
1658 * then attempt to reuse the atomic pagefault disabled fast path again.
1659 *
1660 * 2 - we copy the user entries to a local buffer here outside of the
1661 * local and allow ourselves to wait upon any rendering before
1662 * relocations
1663 *
1664 * 3 - we already have a local copy of the relocation entries, but
1665 * were interrupted (EAGAIN) whilst waiting for the objects, try again.
1666 */
1667 if (!err) {
1668 err = eb_prefault_relocations(eb);
1669 } else if (!have_copy) {
1670 err = eb_copy_relocations(eb);
1671 have_copy = err == 0;
1672 } else {
1673 cond_resched();
1674 err = 0;
1675 }
1676 if (err) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001677 mutex_lock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01001678 goto out;
1679 }
1680
Chris Wilson8a2421b2017-06-16 15:05:22 +01001681 /* A frequent cause for EAGAIN are currently unavailable client pages */
1682 flush_workqueue(eb->i915->mm.userptr_wq);
1683
Chris Wilson2889caa2017-06-16 15:05:19 +01001684 err = i915_mutex_lock_interruptible(dev);
1685 if (err) {
1686 mutex_lock(&dev->struct_mutex);
1687 goto out;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001688 }
1689
Chris Wilson67731b82010-12-08 10:38:14 +00001690 /* reacquire the objects */
Chris Wilson2889caa2017-06-16 15:05:19 +01001691 err = eb_lookup_vmas(eb);
1692 if (err)
Chris Wilson3b96eff2013-01-08 10:53:14 +00001693 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +00001694
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001695 GEM_BUG_ON(!eb->batch);
1696
Chris Wilson2889caa2017-06-16 15:05:19 +01001697 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1698 if (!have_copy) {
1699 pagefault_disable();
1700 err = eb_relocate_vma(eb, vma);
1701 pagefault_enable();
1702 if (err)
1703 goto repeat;
1704 } else {
1705 err = eb_relocate_vma_slow(eb, vma);
1706 if (err)
1707 goto err;
1708 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001709 }
1710
Chris Wilson2889caa2017-06-16 15:05:19 +01001711 /*
1712 * Leave the user relocations as are, this is the painfully slow path,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001713 * and we want to avoid the complication of dropping the lock whilst
1714 * having buffers reserved in the aperture and so causing spurious
1715 * ENOSPC for random operations.
1716 */
1717
1718err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001719 if (err == -EAGAIN)
1720 goto repeat;
1721
1722out:
1723 if (have_copy) {
1724 const unsigned int count = eb->buffer_count;
1725 unsigned int i;
1726
1727 for (i = 0; i < count; i++) {
1728 const struct drm_i915_gem_exec_object2 *entry =
1729 &eb->exec[i];
1730 struct drm_i915_gem_relocation_entry *relocs;
1731
1732 if (!entry->relocation_count)
1733 continue;
1734
1735 relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1736 kvfree(relocs);
1737 }
1738 }
1739
Chris Wilson1f727d92017-07-21 15:50:36 +01001740 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001741}
1742
Chris Wilson2889caa2017-06-16 15:05:19 +01001743static int eb_relocate(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001744{
Chris Wilson2889caa2017-06-16 15:05:19 +01001745 if (eb_lookup_vmas(eb))
1746 goto slow;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001747
Chris Wilson2889caa2017-06-16 15:05:19 +01001748 /* The objects are in their final locations, apply the relocations. */
1749 if (eb->args->flags & __EXEC_HAS_RELOC) {
1750 struct i915_vma *vma;
1751
1752 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1753 if (eb_relocate_vma(eb, vma))
1754 goto slow;
1755 }
1756 }
1757
1758 return 0;
1759
1760slow:
1761 return eb_relocate_slow(eb);
1762}
1763
Chris Wilson95ff7c72017-06-16 15:05:25 +01001764static void eb_export_fence(struct i915_vma *vma,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001765 struct i915_request *rq,
Chris Wilson2889caa2017-06-16 15:05:19 +01001766 unsigned int flags)
1767{
Chris Wilson95ff7c72017-06-16 15:05:25 +01001768 struct reservation_object *resv = vma->resv;
Chris Wilson2889caa2017-06-16 15:05:19 +01001769
1770 /*
1771 * Ignore errors from failing to allocate the new fence, we can't
1772 * handle an error right now. Worst case should be missed
1773 * synchronisation leading to rendering corruption.
1774 */
1775 reservation_object_lock(resv, NULL);
1776 if (flags & EXEC_OBJECT_WRITE)
Chris Wilsone61e0f52018-02-21 09:56:36 +00001777 reservation_object_add_excl_fence(resv, &rq->fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01001778 else if (reservation_object_reserve_shared(resv) == 0)
Chris Wilsone61e0f52018-02-21 09:56:36 +00001779 reservation_object_add_shared_fence(resv, &rq->fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01001780 reservation_object_unlock(resv);
1781}
1782
1783static int eb_move_to_gpu(struct i915_execbuffer *eb)
1784{
1785 const unsigned int count = eb->buffer_count;
1786 unsigned int i;
1787 int err;
1788
1789 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001790 unsigned int flags = eb->flags[i];
1791 struct i915_vma *vma = eb->vma[i];
Ben Widawsky27173f12013-08-14 11:38:36 +02001792 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001793
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001794 if (flags & EXEC_OBJECT_CAPTURE) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00001795 struct i915_capture_list *capture;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001796
1797 capture = kmalloc(sizeof(*capture), GFP_KERNEL);
1798 if (unlikely(!capture))
1799 return -ENOMEM;
1800
Chris Wilson650bc632017-06-15 09:14:33 +01001801 capture->next = eb->request->capture_list;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001802 capture->vma = eb->vma[i];
Chris Wilson650bc632017-06-15 09:14:33 +01001803 eb->request->capture_list = capture;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001804 }
1805
Chris Wilsonb8f55be2017-08-11 12:11:16 +01001806 /*
1807 * If the GPU is not _reading_ through the CPU cache, we need
1808 * to make sure that any writes (both previous GPU writes from
1809 * before a change in snooping levels and normal CPU writes)
1810 * caught in that cache are flushed to main memory.
1811 *
1812 * We want to say
1813 * obj->cache_dirty &&
1814 * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
1815 * but gcc's optimiser doesn't handle that as well and emits
1816 * two jumps instead of one. Maybe one day...
1817 */
1818 if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) {
Chris Wilson0f46daa2017-07-21 15:50:37 +01001819 if (i915_gem_clflush_object(obj, 0))
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001820 flags &= ~EXEC_OBJECT_ASYNC;
Chris Wilson0f46daa2017-07-21 15:50:37 +01001821 }
1822
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001823 if (flags & EXEC_OBJECT_ASYNC)
1824 continue;
Chris Wilson77ae9952017-01-27 09:40:07 +00001825
Chris Wilsone61e0f52018-02-21 09:56:36 +00001826 err = i915_request_await_object
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001827 (eb->request, obj, flags & EXEC_OBJECT_WRITE);
Chris Wilson2889caa2017-06-16 15:05:19 +01001828 if (err)
1829 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001830 }
1831
Chris Wilson2889caa2017-06-16 15:05:19 +01001832 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001833 unsigned int flags = eb->flags[i];
1834 struct i915_vma *vma = eb->vma[i];
Chris Wilson2889caa2017-06-16 15:05:19 +01001835
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001836 i915_vma_move_to_active(vma, eb->request, flags);
1837 eb_export_fence(vma, eb->request, flags);
1838
1839 __eb_unreserve_vma(vma, flags);
1840 vma->exec_flags = NULL;
1841
1842 if (unlikely(flags & __EXEC_OBJECT_HAS_REF))
Chris Wilsondade2a62017-06-16 15:05:20 +01001843 i915_vma_put(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001844 }
1845 eb->exec = NULL;
1846
Chris Wilsondcd79932016-08-18 17:16:40 +01001847 /* Unconditionally flush any chipset caches (for streaming writes). */
Chris Wilson650bc632017-06-15 09:14:33 +01001848 i915_gem_chipset_flush(eb->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001849
Chris Wilson21131842017-11-20 10:20:01 +00001850 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001851}
1852
Chris Wilson2889caa2017-06-16 15:05:19 +01001853static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001854{
Chris Wilson650bc632017-06-15 09:14:33 +01001855 if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
Daniel Vettered5982e2013-01-17 22:23:36 +01001856 return false;
1857
Chris Wilson2f5945b2015-10-06 11:39:55 +01001858 /* Kernel clipping was a DRI1 misfeature */
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01001859 if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
1860 if (exec->num_cliprects || exec->cliprects_ptr)
1861 return false;
1862 }
Chris Wilson2f5945b2015-10-06 11:39:55 +01001863
1864 if (exec->DR4 == 0xffffffff) {
1865 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1866 exec->DR4 = 0;
1867 }
1868 if (exec->DR1 || exec->DR4)
1869 return false;
1870
1871 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1872 return false;
1873
1874 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001875}
1876
Chris Wilson5cf3d282016-08-04 07:52:43 +01001877void i915_vma_move_to_active(struct i915_vma *vma,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001878 struct i915_request *rq,
Chris Wilson5cf3d282016-08-04 07:52:43 +01001879 unsigned int flags)
1880{
1881 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001882 const unsigned int idx = rq->engine->id;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001883
Chris Wilsone61e0f52018-02-21 09:56:36 +00001884 lockdep_assert_held(&rq->i915->drm.struct_mutex);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001885 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1886
Chris Wilson2889caa2017-06-16 15:05:19 +01001887 /*
1888 * Add a reference if we're newly entering the active list.
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001889 * The order in which we add operations to the retirement queue is
1890 * vital here: mark_active adds to the start of the callback list,
1891 * such that subsequent callbacks are called first. Therefore we
1892 * add the active reference first and queue for it to be dropped
1893 * *last*.
1894 */
Chris Wilsond07f0e52016-10-28 13:58:44 +01001895 if (!i915_vma_is_active(vma))
1896 obj->active_count++;
1897 i915_vma_set_active(vma, idx);
Chris Wilsone61e0f52018-02-21 09:56:36 +00001898 i915_gem_active_set(&vma->last_read[idx], rq);
Chris Wilsond07f0e52016-10-28 13:58:44 +01001899 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001900
Christian Königc0a51fd2018-02-16 13:43:38 +01001901 obj->write_domain = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001902 if (flags & EXEC_OBJECT_WRITE) {
Christian Königc0a51fd2018-02-16 13:43:38 +01001903 obj->write_domain = I915_GEM_DOMAIN_RENDER;
Chris Wilsone27ab732017-06-15 13:38:49 +01001904
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00001905 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
Chris Wilsone61e0f52018-02-21 09:56:36 +00001906 i915_gem_active_set(&obj->frontbuffer_write, rq);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001907
Christian Königc0a51fd2018-02-16 13:43:38 +01001908 obj->read_domains = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001909 }
Christian Königc0a51fd2018-02-16 13:43:38 +01001910 obj->read_domains |= I915_GEM_GPU_DOMAINS;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001911
Chris Wilson49ef5292016-08-18 17:17:00 +01001912 if (flags & EXEC_OBJECT_NEEDS_FENCE)
Chris Wilsone61e0f52018-02-21 09:56:36 +00001913 i915_gem_active_set(&vma->last_fence, rq);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001914}
1915
Chris Wilsone61e0f52018-02-21 09:56:36 +00001916static int i915_reset_gen7_sol_offsets(struct i915_request *rq)
Eric Anholtae662d32012-01-03 09:23:29 -08001917{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001918 u32 *cs;
1919 int i;
Eric Anholtae662d32012-01-03 09:23:29 -08001920
Chris Wilsone61e0f52018-02-21 09:56:36 +00001921 if (!IS_GEN7(rq->i915) || rq->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001922 DRM_DEBUG("sol reset is gen7/rcs only\n");
1923 return -EINVAL;
1924 }
Eric Anholtae662d32012-01-03 09:23:29 -08001925
Chris Wilsone61e0f52018-02-21 09:56:36 +00001926 cs = intel_ring_begin(rq, 4 * 2 + 2);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001927 if (IS_ERR(cs))
1928 return PTR_ERR(cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001929
Chris Wilson2889caa2017-06-16 15:05:19 +01001930 *cs++ = MI_LOAD_REGISTER_IMM(4);
Eric Anholtae662d32012-01-03 09:23:29 -08001931 for (i = 0; i < 4; i++) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001932 *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
1933 *cs++ = 0;
Eric Anholtae662d32012-01-03 09:23:29 -08001934 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001935 *cs++ = MI_NOOP;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001936 intel_ring_advance(rq, cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001937
1938 return 0;
1939}
1940
Chris Wilson650bc632017-06-15 09:14:33 +01001941static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001942{
Brad Volkin71745372014-12-11 12:13:12 -08001943 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001944 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001945 int err;
Brad Volkin71745372014-12-11 12:13:12 -08001946
Chris Wilson650bc632017-06-15 09:14:33 +01001947 shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool,
1948 PAGE_ALIGN(eb->batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001949 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001950 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001951
Chris Wilson2889caa2017-06-16 15:05:19 +01001952 err = intel_engine_cmd_parser(eb->engine,
Chris Wilson650bc632017-06-15 09:14:33 +01001953 eb->batch->obj,
Chris Wilson33a051a2016-07-27 09:07:26 +01001954 shadow_batch_obj,
Chris Wilson650bc632017-06-15 09:14:33 +01001955 eb->batch_start_offset,
1956 eb->batch_len,
Chris Wilson33a051a2016-07-27 09:07:26 +01001957 is_master);
Chris Wilson2889caa2017-06-16 15:05:19 +01001958 if (err) {
1959 if (err == -EACCES) /* unhandled chained batch */
Chris Wilson058d88c2016-08-15 10:49:06 +01001960 vma = NULL;
1961 else
Chris Wilson2889caa2017-06-16 15:05:19 +01001962 vma = ERR_PTR(err);
Chris Wilson058d88c2016-08-15 10:49:06 +01001963 goto out;
1964 }
Brad Volkin71745372014-12-11 12:13:12 -08001965
Chris Wilson058d88c2016-08-15 10:49:06 +01001966 vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
1967 if (IS_ERR(vma))
1968 goto out;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001969
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001970 eb->vma[eb->buffer_count] = i915_vma_get(vma);
1971 eb->flags[eb->buffer_count] =
1972 __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF;
1973 vma->exec_flags = &eb->flags[eb->buffer_count];
1974 eb->buffer_count++;
Brad Volkin71745372014-12-11 12:13:12 -08001975
Chris Wilson058d88c2016-08-15 10:49:06 +01001976out:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001977 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson058d88c2016-08-15 10:49:06 +01001978 return vma;
Brad Volkin71745372014-12-11 12:13:12 -08001979}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001980
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001981static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00001982add_to_client(struct i915_request *rq, struct drm_file *file)
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001983{
Chris Wilsone61e0f52018-02-21 09:56:36 +00001984 rq->file_priv = file->driver_priv;
1985 list_add_tail(&rq->client_link, &rq->file_priv->mm.request_list);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001986}
1987
Chris Wilson2889caa2017-06-16 15:05:19 +01001988static int eb_submit(struct i915_execbuffer *eb)
Oscar Mateo78382592014-07-03 16:28:05 +01001989{
Chris Wilson2889caa2017-06-16 15:05:19 +01001990 int err;
Oscar Mateo78382592014-07-03 16:28:05 +01001991
Chris Wilson2889caa2017-06-16 15:05:19 +01001992 err = eb_move_to_gpu(eb);
1993 if (err)
1994 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001995
Chris Wilson650bc632017-06-15 09:14:33 +01001996 if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001997 err = i915_reset_gen7_sol_offsets(eb->request);
1998 if (err)
1999 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01002000 }
2001
Chris Wilson2889caa2017-06-16 15:05:19 +01002002 err = eb->engine->emit_bb_start(eb->request,
Chris Wilson650bc632017-06-15 09:14:33 +01002003 eb->batch->node.start +
2004 eb->batch_start_offset,
2005 eb->batch_len,
Chris Wilson2889caa2017-06-16 15:05:19 +01002006 eb->batch_flags);
2007 if (err)
2008 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01002009
Chris Wilson2f5945b2015-10-06 11:39:55 +01002010 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01002011}
2012
Chris Wilson204bcfe2018-02-08 11:39:17 +00002013/*
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002014 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01002015 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002016 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002017static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01002018gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
2019 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002020{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002021 struct drm_i915_file_private *file_priv = file->driver_priv;
2022
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002023 /* Check whether the file_priv has already selected one ring. */
Joonas Lahtinen6f633402016-09-01 14:58:21 +03002024 if ((int)file_priv->bsd_engine < 0)
2025 file_priv->bsd_engine = atomic_fetch_xor(1,
2026 &dev_priv->mm.bsd_engine_dispatch_index);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002027
Chris Wilsonc80ff162016-07-27 09:07:27 +01002028 return file_priv->bsd_engine;
Chris Wilsond23db882014-05-23 08:48:08 +02002029}
2030
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002031#define I915_USER_RINGS (4)
2032
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002033static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002034 [I915_EXEC_DEFAULT] = RCS,
2035 [I915_EXEC_RENDER] = RCS,
2036 [I915_EXEC_BLT] = BCS,
2037 [I915_EXEC_BSD] = VCS,
2038 [I915_EXEC_VEBOX] = VECS
2039};
2040
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002041static struct intel_engine_cs *
2042eb_select_engine(struct drm_i915_private *dev_priv,
2043 struct drm_file *file,
2044 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002045{
2046 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002047 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002048
2049 if (user_ring_id > I915_USER_RINGS) {
2050 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002051 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002052 }
2053
2054 if ((user_ring_id != I915_EXEC_BSD) &&
2055 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
2056 DRM_DEBUG("execbuf with non bsd ring but with invalid "
2057 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002058 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002059 }
2060
2061 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
2062 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
2063
2064 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01002065 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002066 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
2067 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00002068 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002069 bsd_idx--;
2070 } else {
2071 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
2072 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002073 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002074 }
2075
Akash Goel3b3f1652016-10-13 22:44:48 +05302076 engine = dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002077 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +05302078 engine = dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002079 }
2080
Akash Goel3b3f1652016-10-13 22:44:48 +05302081 if (!engine) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002082 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002083 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002084 }
2085
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002086 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002087}
2088
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002089static void
2090__free_fence_array(struct drm_syncobj **fences, unsigned int n)
2091{
2092 while (n--)
2093 drm_syncobj_put(ptr_mask_bits(fences[n], 2));
2094 kvfree(fences);
2095}
2096
2097static struct drm_syncobj **
2098get_fence_array(struct drm_i915_gem_execbuffer2 *args,
2099 struct drm_file *file)
2100{
Chris Wilsond710fc12017-11-16 10:50:59 +00002101 const unsigned long nfences = args->num_cliprects;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002102 struct drm_i915_gem_exec_fence __user *user;
2103 struct drm_syncobj **fences;
Chris Wilsond710fc12017-11-16 10:50:59 +00002104 unsigned long n;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002105 int err;
2106
2107 if (!(args->flags & I915_EXEC_FENCE_ARRAY))
2108 return NULL;
2109
Chris Wilsond710fc12017-11-16 10:50:59 +00002110 /* Check multiplication overflow for access_ok() and kvmalloc_array() */
2111 BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long));
2112 if (nfences > min_t(unsigned long,
2113 ULONG_MAX / sizeof(*user),
2114 SIZE_MAX / sizeof(*fences)))
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002115 return ERR_PTR(-EINVAL);
2116
2117 user = u64_to_user_ptr(args->cliprects_ptr);
Chris Wilsond710fc12017-11-16 10:50:59 +00002118 if (!access_ok(VERIFY_READ, user, nfences * sizeof(*user)))
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002119 return ERR_PTR(-EFAULT);
2120
Chris Wilsond710fc12017-11-16 10:50:59 +00002121 fences = kvmalloc_array(nfences, sizeof(*fences),
Michal Hocko0ee931c2017-09-13 16:28:29 -07002122 __GFP_NOWARN | GFP_KERNEL);
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002123 if (!fences)
2124 return ERR_PTR(-ENOMEM);
2125
2126 for (n = 0; n < nfences; n++) {
2127 struct drm_i915_gem_exec_fence fence;
2128 struct drm_syncobj *syncobj;
2129
2130 if (__copy_from_user(&fence, user++, sizeof(fence))) {
2131 err = -EFAULT;
2132 goto err;
2133 }
2134
Tvrtko Ursulinebcaa1f2017-10-31 10:23:25 +00002135 if (fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS) {
2136 err = -EINVAL;
2137 goto err;
2138 }
2139
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002140 syncobj = drm_syncobj_find(file, fence.handle);
2141 if (!syncobj) {
2142 DRM_DEBUG("Invalid syncobj handle provided\n");
2143 err = -ENOENT;
2144 goto err;
2145 }
2146
Tvrtko Ursulinebcaa1f2017-10-31 10:23:25 +00002147 BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) &
2148 ~__I915_EXEC_FENCE_UNKNOWN_FLAGS);
2149
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002150 fences[n] = ptr_pack_bits(syncobj, fence.flags, 2);
2151 }
2152
2153 return fences;
2154
2155err:
2156 __free_fence_array(fences, n);
2157 return ERR_PTR(err);
2158}
2159
2160static void
2161put_fence_array(struct drm_i915_gem_execbuffer2 *args,
2162 struct drm_syncobj **fences)
2163{
2164 if (fences)
2165 __free_fence_array(fences, args->num_cliprects);
2166}
2167
2168static int
2169await_fence_array(struct i915_execbuffer *eb,
2170 struct drm_syncobj **fences)
2171{
2172 const unsigned int nfences = eb->args->num_cliprects;
2173 unsigned int n;
2174 int err;
2175
2176 for (n = 0; n < nfences; n++) {
2177 struct drm_syncobj *syncobj;
2178 struct dma_fence *fence;
2179 unsigned int flags;
2180
2181 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2182 if (!(flags & I915_EXEC_FENCE_WAIT))
2183 continue;
2184
Jason Ekstrandafca4212017-08-25 10:52:21 -07002185 fence = drm_syncobj_fence_get(syncobj);
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002186 if (!fence)
2187 return -EINVAL;
2188
Chris Wilsone61e0f52018-02-21 09:56:36 +00002189 err = i915_request_await_dma_fence(eb->request, fence);
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002190 dma_fence_put(fence);
2191 if (err < 0)
2192 return err;
2193 }
2194
2195 return 0;
2196}
2197
2198static void
2199signal_fence_array(struct i915_execbuffer *eb,
2200 struct drm_syncobj **fences)
2201{
2202 const unsigned int nfences = eb->args->num_cliprects;
2203 struct dma_fence * const fence = &eb->request->fence;
2204 unsigned int n;
2205
2206 for (n = 0; n < nfences; n++) {
2207 struct drm_syncobj *syncobj;
2208 unsigned int flags;
2209
2210 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2211 if (!(flags & I915_EXEC_FENCE_SIGNAL))
2212 continue;
2213
2214 drm_syncobj_replace_fence(syncobj, fence);
2215 }
2216}
2217
Eric Anholtae662d32012-01-03 09:23:29 -08002218static int
Chris Wilson650bc632017-06-15 09:14:33 +01002219i915_gem_do_execbuffer(struct drm_device *dev,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002220 struct drm_file *file,
2221 struct drm_i915_gem_execbuffer2 *args,
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002222 struct drm_i915_gem_exec_object2 *exec,
2223 struct drm_syncobj **fences)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002224{
Chris Wilson650bc632017-06-15 09:14:33 +01002225 struct i915_execbuffer eb;
Chris Wilsonfec04452017-01-27 09:40:08 +00002226 struct dma_fence *in_fence = NULL;
2227 struct sync_file *out_fence = NULL;
2228 int out_fence_fd = -1;
Chris Wilson2889caa2017-06-16 15:05:19 +01002229 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002230
Chris Wilson74c1c692017-09-21 12:01:35 +01002231 BUILD_BUG_ON(__EXEC_INTERNAL_FLAGS & ~__I915_EXEC_ILLEGAL_FLAGS);
Chris Wilson2889caa2017-06-16 15:05:19 +01002232 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
2233 ~__EXEC_OBJECT_UNKNOWN_FLAGS);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002234
Chris Wilson650bc632017-06-15 09:14:33 +01002235 eb.i915 = to_i915(dev);
2236 eb.file = file;
2237 eb.args = args;
Chris Wilson7dd4f672017-06-16 15:05:24 +01002238 if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC))
Chris Wilson2889caa2017-06-16 15:05:19 +01002239 args->flags |= __EXEC_HAS_RELOC;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002240
Chris Wilson650bc632017-06-15 09:14:33 +01002241 eb.exec = exec;
Chris Wilson170fa292017-08-16 09:52:07 +01002242 eb.vma = (struct i915_vma **)(exec + args->buffer_count + 1);
2243 eb.vma[0] = NULL;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002244 eb.flags = (unsigned int *)(eb.vma + args->buffer_count + 1);
2245
Chris Wilson2889caa2017-06-16 15:05:19 +01002246 eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
2247 if (USES_FULL_PPGTT(eb.i915))
2248 eb.invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson650bc632017-06-15 09:14:33 +01002249 reloc_cache_init(&eb.reloc_cache, eb.i915);
2250
Chris Wilson2889caa2017-06-16 15:05:19 +01002251 eb.buffer_count = args->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +01002252 eb.batch_start_offset = args->batch_start_offset;
2253 eb.batch_len = args->batch_len;
2254
Chris Wilson2889caa2017-06-16 15:05:19 +01002255 eb.batch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002256 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02002257 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002258 return -EPERM;
2259
Chris Wilson2889caa2017-06-16 15:05:19 +01002260 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002261 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01002262 if (args->flags & I915_EXEC_IS_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +01002263 eb.batch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002264
Chris Wilson650bc632017-06-15 09:14:33 +01002265 eb.engine = eb_select_engine(eb.i915, file, args);
2266 if (!eb.engine)
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002267 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002268
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002269 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
Chris Wilson650bc632017-06-15 09:14:33 +01002270 if (!HAS_RESOURCE_STREAMER(eb.i915)) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002271 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
2272 return -EINVAL;
2273 }
Chris Wilson650bc632017-06-15 09:14:33 +01002274 if (eb.engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002275 DRM_DEBUG("RS is not available on %s\n",
Chris Wilson650bc632017-06-15 09:14:33 +01002276 eb.engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002277 return -EINVAL;
2278 }
2279
Chris Wilson2889caa2017-06-16 15:05:19 +01002280 eb.batch_flags |= I915_DISPATCH_RS;
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002281 }
2282
Chris Wilsonfec04452017-01-27 09:40:08 +00002283 if (args->flags & I915_EXEC_FENCE_IN) {
2284 in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002285 if (!in_fence)
2286 return -EINVAL;
Chris Wilsonfec04452017-01-27 09:40:08 +00002287 }
2288
2289 if (args->flags & I915_EXEC_FENCE_OUT) {
2290 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
2291 if (out_fence_fd < 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002292 err = out_fence_fd;
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002293 goto err_in_fence;
Chris Wilsonfec04452017-01-27 09:40:08 +00002294 }
2295 }
2296
Chris Wilson4d470f72017-06-29 16:04:25 +01002297 err = eb_create(&eb);
2298 if (err)
2299 goto err_out_fence;
2300
2301 GEM_BUG_ON(!eb.lut_size);
Chris Wilson2889caa2017-06-16 15:05:19 +01002302
Chris Wilson1acfc102017-06-20 12:05:47 +01002303 err = eb_select_context(&eb);
2304 if (unlikely(err))
2305 goto err_destroy;
2306
Chris Wilson2889caa2017-06-16 15:05:19 +01002307 /*
2308 * Take a local wakeref for preparing to dispatch the execbuf as
Chris Wilson67d97da2016-07-04 08:08:31 +01002309 * we expect to access the hardware fairly frequently in the
2310 * process. Upon first dispatch, we acquire another prolonged
2311 * wakeref that we hold until the GPU has been idle for at least
2312 * 100ms.
2313 */
Chris Wilson650bc632017-06-15 09:14:33 +01002314 intel_runtime_pm_get(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002315
Chris Wilson2889caa2017-06-16 15:05:19 +01002316 err = i915_mutex_lock_interruptible(dev);
2317 if (err)
2318 goto err_rpm;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002319
Chris Wilson2889caa2017-06-16 15:05:19 +01002320 err = eb_relocate(&eb);
Chris Wilson1f727d92017-07-21 15:50:36 +01002321 if (err) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002322 /*
2323 * If the user expects the execobject.offset and
2324 * reloc.presumed_offset to be an exact match,
2325 * as for using NO_RELOC, then we cannot update
2326 * the execobject.offset until we have completed
2327 * relocation.
2328 */
2329 args->flags &= ~__EXEC_HAS_RELOC;
Chris Wilson2889caa2017-06-16 15:05:19 +01002330 goto err_vma;
Chris Wilson1f727d92017-07-21 15:50:36 +01002331 }
Ben Widawsky41bde552013-12-06 14:11:21 -08002332
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002333 if (unlikely(*eb.batch->exec_flags & EXEC_OBJECT_WRITE)) {
Daniel Vetterff240192012-01-31 21:08:14 +01002334 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002335 err = -EINVAL;
2336 goto err_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002337 }
Chris Wilson650bc632017-06-15 09:14:33 +01002338 if (eb.batch_start_offset > eb.batch->size ||
2339 eb.batch_len > eb.batch->size - eb.batch_start_offset) {
Chris Wilson0b537272016-08-18 17:17:12 +01002340 DRM_DEBUG("Attempting to use out-of-bounds batch\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002341 err = -EINVAL;
2342 goto err_vma;
Chris Wilson0b537272016-08-18 17:17:12 +01002343 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002344
Chris Wilson3dbf26e2017-08-26 14:56:20 +01002345 if (eb_use_cmdparser(&eb)) {
Chris Wilson59bfa122016-08-04 16:32:31 +01002346 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002347
Chris Wilson650bc632017-06-15 09:14:33 +01002348 vma = eb_parse(&eb, drm_is_current_master(file));
Chris Wilson59bfa122016-08-04 16:32:31 +01002349 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002350 err = PTR_ERR(vma);
2351 goto err_vma;
Brad Volkin78a42372014-12-11 12:13:09 -08002352 }
Chris Wilson17cabf52015-01-14 11:20:57 +00002353
Chris Wilson59bfa122016-08-04 16:32:31 +01002354 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002355 /*
2356 * Batch parsed and accepted:
2357 *
2358 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
2359 * bit from MI_BATCH_BUFFER_START commands issued in
2360 * the dispatch_execbuffer implementations. We
2361 * specifically don't want that set on batches the
2362 * command parser has accepted.
2363 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002364 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilson650bc632017-06-15 09:14:33 +01002365 eb.batch_start_offset = 0;
2366 eb.batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002367 }
Brad Volkin351e3db2014-02-18 10:15:46 -08002368 }
2369
Chris Wilson650bc632017-06-15 09:14:33 +01002370 if (eb.batch_len == 0)
2371 eb.batch_len = eb.batch->size - eb.batch_start_offset;
Brad Volkin78a42372014-12-11 12:13:09 -08002372
Chris Wilson2889caa2017-06-16 15:05:19 +01002373 /*
2374 * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002375 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07002376 * hsw should have this fixed, but bdw mucks it up again. */
Chris Wilson2889caa2017-06-16 15:05:19 +01002377 if (eb.batch_flags & I915_DISPATCH_SECURE) {
Chris Wilson058d88c2016-08-15 10:49:06 +01002378 struct i915_vma *vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002379
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002380 /*
2381 * So on first glance it looks freaky that we pin the batch here
2382 * outside of the reservation loop. But:
2383 * - The batch is already pinned into the relevant ppgtt, so we
2384 * already have the backing storage fully allocated.
2385 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01002386 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002387 * fitting due to fragmentation.
2388 * So this is actually safe.
2389 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002390 vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0);
Chris Wilson058d88c2016-08-15 10:49:06 +01002391 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002392 err = PTR_ERR(vma);
2393 goto err_vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01002394 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002395
Chris Wilson650bc632017-06-15 09:14:33 +01002396 eb.batch = vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002397 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002398
Chris Wilson7dd4f672017-06-16 15:05:24 +01002399 /* All GPU relocation batches must be submitted prior to the user rq */
2400 GEM_BUG_ON(eb.reloc_cache.rq);
2401
John Harrison0c8dac82015-05-29 17:43:25 +01002402 /* Allocate a request for this batch buffer nice and early. */
Chris Wilsone61e0f52018-02-21 09:56:36 +00002403 eb.request = i915_request_alloc(eb.engine, eb.ctx);
Chris Wilson650bc632017-06-15 09:14:33 +01002404 if (IS_ERR(eb.request)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002405 err = PTR_ERR(eb.request);
John Harrison0c8dac82015-05-29 17:43:25 +01002406 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00002407 }
John Harrison0c8dac82015-05-29 17:43:25 +01002408
Chris Wilsonfec04452017-01-27 09:40:08 +00002409 if (in_fence) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002410 err = i915_request_await_dma_fence(eb.request, in_fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01002411 if (err < 0)
Chris Wilsonfec04452017-01-27 09:40:08 +00002412 goto err_request;
2413 }
2414
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002415 if (fences) {
2416 err = await_fence_array(&eb, fences);
2417 if (err)
2418 goto err_request;
2419 }
2420
Chris Wilsonfec04452017-01-27 09:40:08 +00002421 if (out_fence_fd != -1) {
Chris Wilson650bc632017-06-15 09:14:33 +01002422 out_fence = sync_file_create(&eb.request->fence);
Chris Wilsonfec04452017-01-27 09:40:08 +00002423 if (!out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002424 err = -ENOMEM;
Chris Wilsonfec04452017-01-27 09:40:08 +00002425 goto err_request;
2426 }
2427 }
2428
Chris Wilson2889caa2017-06-16 15:05:19 +01002429 /*
2430 * Whilst this request exists, batch_obj will be on the
Chris Wilson17f298cf2016-08-10 13:41:46 +01002431 * active_list, and so will hold the active reference. Only when this
2432 * request is retired will the the batch_obj be moved onto the
2433 * inactive_list and lose its active reference. Hence we do not need
2434 * to explicitly hold another reference here.
2435 */
Chris Wilson650bc632017-06-15 09:14:33 +01002436 eb.request->batch = eb.batch;
Chris Wilson17f298cf2016-08-10 13:41:46 +01002437
Chris Wilsone61e0f52018-02-21 09:56:36 +00002438 trace_i915_request_queue(eb.request, eb.batch_flags);
Chris Wilson2889caa2017-06-16 15:05:19 +01002439 err = eb_submit(&eb);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01002440err_request:
Chris Wilsone61e0f52018-02-21 09:56:36 +00002441 __i915_request_add(eb.request, err == 0);
Chris Wilson650bc632017-06-15 09:14:33 +01002442 add_to_client(eb.request, file);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002443
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002444 if (fences)
2445 signal_fence_array(&eb, fences);
2446
Chris Wilsonfec04452017-01-27 09:40:08 +00002447 if (out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002448 if (err == 0) {
Chris Wilsonfec04452017-01-27 09:40:08 +00002449 fd_install(out_fence_fd, out_fence->file);
Daniele Ceraolo Spuriob6a88e42018-02-14 11:18:25 -08002450 args->rsvd2 &= GENMASK_ULL(31, 0); /* keep in-fence */
Chris Wilsonfec04452017-01-27 09:40:08 +00002451 args->rsvd2 |= (u64)out_fence_fd << 32;
2452 out_fence_fd = -1;
2453 } else {
2454 fput(out_fence->file);
2455 }
2456 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002457
John Harrison0c8dac82015-05-29 17:43:25 +01002458err_batch_unpin:
Chris Wilson2889caa2017-06-16 15:05:19 +01002459 if (eb.batch_flags & I915_DISPATCH_SECURE)
Chris Wilson650bc632017-06-15 09:14:33 +01002460 i915_vma_unpin(eb.batch);
Chris Wilson2889caa2017-06-16 15:05:19 +01002461err_vma:
2462 if (eb.exec)
2463 eb_release_vmas(&eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002464 mutex_unlock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01002465err_rpm:
Chris Wilson650bc632017-06-15 09:14:33 +01002466 intel_runtime_pm_put(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002467 i915_gem_context_put(eb.ctx);
2468err_destroy:
Chris Wilson2889caa2017-06-16 15:05:19 +01002469 eb_destroy(&eb);
Chris Wilson4d470f72017-06-29 16:04:25 +01002470err_out_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002471 if (out_fence_fd != -1)
2472 put_unused_fd(out_fence_fd);
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002473err_in_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002474 dma_fence_put(in_fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01002475 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002476}
2477
Chris Wilsond710fc12017-11-16 10:50:59 +00002478static size_t eb_element_size(void)
2479{
2480 return (sizeof(struct drm_i915_gem_exec_object2) +
2481 sizeof(struct i915_vma *) +
2482 sizeof(unsigned int));
2483}
2484
2485static bool check_buffer_count(size_t count)
2486{
2487 const size_t sz = eb_element_size();
2488
2489 /*
2490 * When using LUT_HANDLE, we impose a limit of INT_MAX for the lookup
2491 * array size (see eb_create()). Otherwise, we can accept an array as
2492 * large as can be addressed (though use large arrays at your peril)!
2493 */
2494
2495 return !(count < 1 || count > INT_MAX || count > SIZE_MAX / sz - 1);
2496}
2497
Chris Wilson54cf91d2010-11-25 18:00:26 +00002498/*
2499 * Legacy execbuffer just creates an exec2 list from the original exec object
2500 * list array and passes it to the real function.
2501 */
2502int
Ville Syrjälä6a20fe72018-02-07 18:48:41 +02002503i915_gem_execbuffer_ioctl(struct drm_device *dev, void *data,
2504 struct drm_file *file)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002505{
2506 struct drm_i915_gem_execbuffer *args = data;
2507 struct drm_i915_gem_execbuffer2 exec2;
2508 struct drm_i915_gem_exec_object *exec_list = NULL;
2509 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
Chris Wilsond710fc12017-11-16 10:50:59 +00002510 const size_t count = args->buffer_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01002511 unsigned int i;
2512 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002513
Chris Wilsond710fc12017-11-16 10:50:59 +00002514 if (!check_buffer_count(count)) {
2515 DRM_DEBUG("execbuf2 with %zd buffers\n", count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002516 return -EINVAL;
2517 }
2518
Chris Wilson2889caa2017-06-16 15:05:19 +01002519 exec2.buffers_ptr = args->buffers_ptr;
2520 exec2.buffer_count = args->buffer_count;
2521 exec2.batch_start_offset = args->batch_start_offset;
2522 exec2.batch_len = args->batch_len;
2523 exec2.DR1 = args->DR1;
2524 exec2.DR4 = args->DR4;
2525 exec2.num_cliprects = args->num_cliprects;
2526 exec2.cliprects_ptr = args->cliprects_ptr;
2527 exec2.flags = I915_EXEC_RENDER;
2528 i915_execbuffer2_set_context_id(exec2, 0);
2529
2530 if (!i915_gem_check_execbuffer(&exec2))
2531 return -EINVAL;
2532
Chris Wilson54cf91d2010-11-25 18:00:26 +00002533 /* Copy in the exec list from userland */
Chris Wilsond710fc12017-11-16 10:50:59 +00002534 exec_list = kvmalloc_array(count, sizeof(*exec_list),
Michal Hocko0ee931c2017-09-13 16:28:29 -07002535 __GFP_NOWARN | GFP_KERNEL);
Chris Wilsond710fc12017-11-16 10:50:59 +00002536 exec2_list = kvmalloc_array(count + 1, eb_element_size(),
Michal Hocko0ee931c2017-09-13 16:28:29 -07002537 __GFP_NOWARN | GFP_KERNEL);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002538 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002539 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002540 args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002541 kvfree(exec_list);
2542 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002543 return -ENOMEM;
2544 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002545 err = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002546 u64_to_user_ptr(args->buffers_ptr),
Chris Wilsond710fc12017-11-16 10:50:59 +00002547 sizeof(*exec_list) * count);
Chris Wilson2889caa2017-06-16 15:05:19 +01002548 if (err) {
Daniel Vetterff240192012-01-31 21:08:14 +01002549 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson2889caa2017-06-16 15:05:19 +01002550 args->buffer_count, err);
Michal Hocko20981052017-05-17 14:23:12 +02002551 kvfree(exec_list);
2552 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002553 return -EFAULT;
2554 }
2555
2556 for (i = 0; i < args->buffer_count; i++) {
2557 exec2_list[i].handle = exec_list[i].handle;
2558 exec2_list[i].relocation_count = exec_list[i].relocation_count;
2559 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
2560 exec2_list[i].alignment = exec_list[i].alignment;
2561 exec2_list[i].offset = exec_list[i].offset;
Tvrtko Ursulinf0836b72016-11-16 08:55:32 +00002562 if (INTEL_GEN(to_i915(dev)) < 4)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002563 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
2564 else
2565 exec2_list[i].flags = 0;
2566 }
2567
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002568 err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list, NULL);
Chris Wilson2889caa2017-06-16 15:05:19 +01002569 if (exec2.flags & __EXEC_HAS_RELOC) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002570 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002571 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002572
Chris Wilson54cf91d2010-11-25 18:00:26 +00002573 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002574 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002575 if (!(exec2_list[i].offset & UPDATE))
2576 continue;
2577
Michał Winiarski934acce2015-12-29 18:24:52 +01002578 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002579 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2580 exec2_list[i].offset &= PIN_OFFSET_MASK;
2581 if (__copy_to_user(&user_exec_list[i].offset,
2582 &exec2_list[i].offset,
2583 sizeof(user_exec_list[i].offset)))
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002584 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002585 }
2586 }
2587
Michal Hocko20981052017-05-17 14:23:12 +02002588 kvfree(exec_list);
2589 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002590 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002591}
2592
2593int
Ville Syrjälä6a20fe72018-02-07 18:48:41 +02002594i915_gem_execbuffer2_ioctl(struct drm_device *dev, void *data,
2595 struct drm_file *file)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002596{
2597 struct drm_i915_gem_execbuffer2 *args = data;
Chris Wilson2889caa2017-06-16 15:05:19 +01002598 struct drm_i915_gem_exec_object2 *exec2_list;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002599 struct drm_syncobj **fences = NULL;
Chris Wilsond710fc12017-11-16 10:50:59 +00002600 const size_t count = args->buffer_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01002601 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002602
Chris Wilsond710fc12017-11-16 10:50:59 +00002603 if (!check_buffer_count(count)) {
2604 DRM_DEBUG("execbuf2 with %zd buffers\n", count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002605 return -EINVAL;
2606 }
2607
Chris Wilson2889caa2017-06-16 15:05:19 +01002608 if (!i915_gem_check_execbuffer(args))
2609 return -EINVAL;
2610
2611 /* Allocate an extra slot for use by the command parser */
Chris Wilsond710fc12017-11-16 10:50:59 +00002612 exec2_list = kvmalloc_array(count + 1, eb_element_size(),
Michal Hocko0ee931c2017-09-13 16:28:29 -07002613 __GFP_NOWARN | GFP_KERNEL);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002614 if (exec2_list == NULL) {
Chris Wilsond710fc12017-11-16 10:50:59 +00002615 DRM_DEBUG("Failed to allocate exec list for %zd buffers\n",
2616 count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002617 return -ENOMEM;
2618 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002619 if (copy_from_user(exec2_list,
2620 u64_to_user_ptr(args->buffers_ptr),
Chris Wilsond710fc12017-11-16 10:50:59 +00002621 sizeof(*exec2_list) * count)) {
2622 DRM_DEBUG("copy %zd exec entries failed\n", count);
Michal Hocko20981052017-05-17 14:23:12 +02002623 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002624 return -EFAULT;
2625 }
2626
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002627 if (args->flags & I915_EXEC_FENCE_ARRAY) {
2628 fences = get_fence_array(args, file);
2629 if (IS_ERR(fences)) {
2630 kvfree(exec2_list);
2631 return PTR_ERR(fences);
2632 }
2633 }
2634
2635 err = i915_gem_do_execbuffer(dev, file, args, exec2_list, fences);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002636
Chris Wilson2889caa2017-06-16 15:05:19 +01002637 /*
2638 * Now that we have begun execution of the batchbuffer, we ignore
2639 * any new error after this point. Also given that we have already
2640 * updated the associated relocations, we try to write out the current
2641 * object locations irrespective of any error.
2642 */
2643 if (args->flags & __EXEC_HAS_RELOC) {
2644 struct drm_i915_gem_exec_object2 __user *user_exec_list =
2645 u64_to_user_ptr(args->buffers_ptr);
2646 unsigned int i;
2647
2648 /* Copy the new buffer offsets back to the user's exec list. */
2649 user_access_begin();
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002650 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002651 if (!(exec2_list[i].offset & UPDATE))
2652 continue;
2653
Michał Winiarski934acce2015-12-29 18:24:52 +01002654 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002655 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2656 unsafe_put_user(exec2_list[i].offset,
2657 &user_exec_list[i].offset,
2658 end_user);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002659 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002660end_user:
2661 user_access_end();
Chris Wilson54cf91d2010-11-25 18:00:26 +00002662 }
2663
Chris Wilson2889caa2017-06-16 15:05:19 +01002664 args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002665 put_fence_array(args, fences);
Michal Hocko20981052017-05-17 14:23:12 +02002666 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002667 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002668}