blob: c74f5df3fb5a91b234fb5ad3d3d45919b5fcbd44 [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 Wilsond1b48c12017-08-16 09:52:08 +0100492eb_add_vma(struct i915_execbuffer *eb, unsigned int i, struct i915_vma *vma)
Chris Wilson2889caa2017-06-16 15:05:19 +0100493{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100494 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100495 int err;
496
497 GEM_BUG_ON(i915_vma_is_closed(vma));
498
499 if (!(eb->args->flags & __EXEC_VALIDATED)) {
500 err = eb_validate_vma(eb, entry, vma);
501 if (unlikely(err))
502 return err;
503 }
504
Chris Wilson4d470f72017-06-29 16:04:25 +0100505 if (eb->lut_size > 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100506 vma->exec_handle = entry->handle;
507 hlist_add_head(&vma->exec_node,
508 &eb->buckets[hash_32(entry->handle,
509 eb->lut_size)]);
510 }
511
512 if (entry->relocation_count)
513 list_add_tail(&vma->reloc_link, &eb->relocs);
514
Chris Wilson2889caa2017-06-16 15:05:19 +0100515 /*
516 * Stash a pointer from the vma to execobj, so we can query its flags,
517 * size, alignment etc as provided by the user. Also we stash a pointer
518 * to the vma inside the execobj so that we can use a direct lookup
519 * to find the right target VMA when doing relocations.
520 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100521 eb->vma[i] = vma;
Chris Wilsond1b48c12017-08-16 09:52:08 +0100522 eb->flags[i] = entry->flags;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100523 vma->exec_flags = &eb->flags[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100524
525 err = 0;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100526 if (eb_pin_vma(eb, entry, vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100527 if (entry->offset != vma->node.start) {
528 entry->offset = vma->node.start | UPDATE;
529 eb->args->flags |= __EXEC_HAS_RELOC;
530 }
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100531 } else {
532 eb_unreserve_vma(vma, vma->exec_flags);
533
534 list_add_tail(&vma->exec_link, &eb->unbound);
535 if (drm_mm_node_allocated(&vma->node))
536 err = i915_vma_unbind(vma);
Chris Wilsoned2f3532018-02-19 14:01:44 +0000537 if (unlikely(err))
538 vma->exec_flags = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100539 }
540 return err;
541}
542
543static inline int use_cpu_reloc(const struct reloc_cache *cache,
544 const struct drm_i915_gem_object *obj)
545{
546 if (!i915_gem_object_has_struct_page(obj))
547 return false;
548
Chris Wilson7dd4f672017-06-16 15:05:24 +0100549 if (DBG_FORCE_RELOC == FORCE_CPU_RELOC)
550 return true;
551
552 if (DBG_FORCE_RELOC == FORCE_GTT_RELOC)
553 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100554
555 return (cache->has_llc ||
556 obj->cache_dirty ||
557 obj->cache_level != I915_CACHE_NONE);
558}
559
560static int eb_reserve_vma(const struct i915_execbuffer *eb,
561 struct i915_vma *vma)
562{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100563 struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
564 unsigned int exec_flags = *vma->exec_flags;
565 u64 pin_flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100566 int err;
567
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100568 pin_flags = PIN_USER | PIN_NONBLOCK;
569 if (exec_flags & EXEC_OBJECT_NEEDS_GTT)
570 pin_flags |= PIN_GLOBAL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100571
572 /*
573 * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
574 * limit address to the first 4GBs for unflagged objects.
575 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100576 if (!(exec_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
577 pin_flags |= PIN_ZONE_4G;
Chris Wilson2889caa2017-06-16 15:05:19 +0100578
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100579 if (exec_flags & __EXEC_OBJECT_NEEDS_MAP)
580 pin_flags |= PIN_MAPPABLE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100581
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100582 if (exec_flags & EXEC_OBJECT_PINNED) {
583 pin_flags |= entry->offset | PIN_OFFSET_FIXED;
584 pin_flags &= ~PIN_NONBLOCK; /* force overlapping checks */
585 } else if (exec_flags & __EXEC_OBJECT_NEEDS_BIAS) {
586 pin_flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson2889caa2017-06-16 15:05:19 +0100587 }
588
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100589 err = i915_vma_pin(vma,
590 entry->pad_to_size, entry->alignment,
591 pin_flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100592 if (err)
593 return err;
594
595 if (entry->offset != vma->node.start) {
596 entry->offset = vma->node.start | UPDATE;
597 eb->args->flags |= __EXEC_HAS_RELOC;
598 }
599
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100600 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) {
Chris Wilson3bd40732017-10-09 09:43:56 +0100601 err = i915_vma_pin_fence(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100602 if (unlikely(err)) {
603 i915_vma_unpin(vma);
604 return err;
605 }
606
Chris Wilson3bd40732017-10-09 09:43:56 +0100607 if (vma->fence)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100608 exec_flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100609 }
610
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100611 *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN;
612 GEM_BUG_ON(eb_vma_misplaced(entry, vma, exec_flags));
Chris Wilson1da7b542017-07-21 15:50:35 +0100613
Chris Wilson2889caa2017-06-16 15:05:19 +0100614 return 0;
615}
616
617static int eb_reserve(struct i915_execbuffer *eb)
618{
619 const unsigned int count = eb->buffer_count;
620 struct list_head last;
621 struct i915_vma *vma;
622 unsigned int i, pass;
623 int err;
624
625 /*
626 * Attempt to pin all of the buffers into the GTT.
627 * This is done in 3 phases:
628 *
629 * 1a. Unbind all objects that do not match the GTT constraints for
630 * the execbuffer (fenceable, mappable, alignment etc).
631 * 1b. Increment pin count for already bound objects.
632 * 2. Bind new objects.
633 * 3. Decrement pin count.
634 *
635 * This avoid unnecessary unbinding of later objects in order to make
636 * room for the earlier objects *unless* we need to defragment.
637 */
638
639 pass = 0;
640 err = 0;
641 do {
642 list_for_each_entry(vma, &eb->unbound, exec_link) {
643 err = eb_reserve_vma(eb, vma);
644 if (err)
645 break;
646 }
647 if (err != -ENOSPC)
648 return err;
649
650 /* Resort *all* the objects into priority order */
651 INIT_LIST_HEAD(&eb->unbound);
652 INIT_LIST_HEAD(&last);
653 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100654 unsigned int flags = eb->flags[i];
655 struct i915_vma *vma = eb->vma[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100656
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100657 if (flags & EXEC_OBJECT_PINNED &&
658 flags & __EXEC_OBJECT_HAS_PIN)
Chris Wilson2889caa2017-06-16 15:05:19 +0100659 continue;
660
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100661 eb_unreserve_vma(vma, &eb->flags[i]);
Chris Wilson2889caa2017-06-16 15:05:19 +0100662
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100663 if (flags & EXEC_OBJECT_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +0100664 list_add(&vma->exec_link, &eb->unbound);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100665 else if (flags & __EXEC_OBJECT_NEEDS_MAP)
Chris Wilson2889caa2017-06-16 15:05:19 +0100666 list_add_tail(&vma->exec_link, &eb->unbound);
667 else
668 list_add_tail(&vma->exec_link, &last);
669 }
670 list_splice_tail(&last, &eb->unbound);
671
672 switch (pass++) {
673 case 0:
674 break;
675
676 case 1:
677 /* Too fragmented, unbind everything and retry */
678 err = i915_gem_evict_vm(eb->vm);
679 if (err)
680 return err;
681 break;
682
683 default:
684 return -ENOSPC;
685 }
686 } while (1);
687}
688
Chris Wilson2889caa2017-06-16 15:05:19 +0100689static unsigned int eb_batch_index(const struct i915_execbuffer *eb)
690{
Chris Wilson1a71cf22017-06-16 15:05:23 +0100691 if (eb->args->flags & I915_EXEC_BATCH_FIRST)
692 return 0;
693 else
694 return eb->buffer_count - 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100695}
696
697static int eb_select_context(struct i915_execbuffer *eb)
698{
699 struct i915_gem_context *ctx;
700
701 ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1);
Chris Wilson1acfc102017-06-20 12:05:47 +0100702 if (unlikely(!ctx))
703 return -ENOENT;
Chris Wilson2889caa2017-06-16 15:05:19 +0100704
Chris Wilson1acfc102017-06-20 12:05:47 +0100705 eb->ctx = ctx;
Chris Wilson2889caa2017-06-16 15:05:19 +0100706 eb->vm = ctx->ppgtt ? &ctx->ppgtt->base : &eb->i915->ggtt.base;
707
708 eb->context_flags = 0;
709 if (ctx->flags & CONTEXT_NO_ZEROMAP)
710 eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS;
711
712 return 0;
713}
714
715static int eb_lookup_vmas(struct i915_execbuffer *eb)
Chris Wilson4ff4b442017-06-16 15:05:16 +0100716{
Chris Wilsond1b48c12017-08-16 09:52:08 +0100717 struct radix_tree_root *handles_vma = &eb->ctx->handles_vma;
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100718 struct drm_i915_gem_object *obj;
Chris Wilson2889caa2017-06-16 15:05:19 +0100719 unsigned int i;
Chris Wilson2889caa2017-06-16 15:05:19 +0100720 int err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100721
Chris Wilson8bcbfb12017-08-16 09:52:05 +0100722 if (unlikely(i915_gem_context_is_closed(eb->ctx)))
723 return -ENOENT;
724
725 if (unlikely(i915_gem_context_is_banned(eb->ctx)))
726 return -EIO;
727
Chris Wilson2889caa2017-06-16 15:05:19 +0100728 INIT_LIST_HEAD(&eb->relocs);
729 INIT_LIST_HEAD(&eb->unbound);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100730
Chris Wilson170fa292017-08-16 09:52:07 +0100731 for (i = 0; i < eb->buffer_count; i++) {
732 u32 handle = eb->exec[i].handle;
Chris Wilsond1b48c12017-08-16 09:52:08 +0100733 struct i915_lut_handle *lut;
Chris Wilson170fa292017-08-16 09:52:07 +0100734 struct i915_vma *vma;
735
Chris Wilsond1b48c12017-08-16 09:52:08 +0100736 vma = radix_tree_lookup(handles_vma, handle);
737 if (likely(vma))
Chris Wilson170fa292017-08-16 09:52:07 +0100738 goto add_vma;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100739
Chris Wilson170fa292017-08-16 09:52:07 +0100740 obj = i915_gem_object_lookup(eb->file, handle);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100741 if (unlikely(!obj)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100742 err = -ENOENT;
Chris Wilson170fa292017-08-16 09:52:07 +0100743 goto err_vma;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100744 }
745
Chris Wilson4ff4b442017-06-16 15:05:16 +0100746 vma = i915_vma_instance(obj, eb->vm, NULL);
747 if (unlikely(IS_ERR(vma))) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100748 err = PTR_ERR(vma);
Chris Wilson170fa292017-08-16 09:52:07 +0100749 goto err_obj;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100750 }
751
Chris Wilsond1b48c12017-08-16 09:52:08 +0100752 lut = kmem_cache_alloc(eb->i915->luts, GFP_KERNEL);
753 if (unlikely(!lut)) {
754 err = -ENOMEM;
755 goto err_obj;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100756 }
757
Chris Wilsond1b48c12017-08-16 09:52:08 +0100758 err = radix_tree_insert(handles_vma, handle, vma);
759 if (unlikely(err)) {
Xidong Wang6be11872018-04-04 10:38:24 +0100760 kmem_cache_free(eb->i915->luts, lut);
Chris Wilsond1b48c12017-08-16 09:52:08 +0100761 goto err_obj;
762 }
763
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100764 /* transfer ref to ctx */
Chris Wilson3ffff012017-08-22 12:05:17 +0100765 vma->open_count++;
Chris Wilsond1b48c12017-08-16 09:52:08 +0100766 list_add(&lut->obj_link, &obj->lut_list);
767 list_add(&lut->ctx_link, &eb->ctx->handles_list);
768 lut->ctx = eb->ctx;
769 lut->handle = handle;
770
Chris Wilson170fa292017-08-16 09:52:07 +0100771add_vma:
Chris Wilsond1b48c12017-08-16 09:52:08 +0100772 err = eb_add_vma(eb, i, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100773 if (unlikely(err))
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100774 goto err_vma;
Chris Wilsondade2a62017-06-16 15:05:20 +0100775
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100776 GEM_BUG_ON(vma != eb->vma[i]);
777 GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100778 }
779
Chris Wilson2889caa2017-06-16 15:05:19 +0100780 /* take note of the batch buffer before we might reorder the lists */
781 i = eb_batch_index(eb);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100782 eb->batch = eb->vma[i];
783 GEM_BUG_ON(eb->batch->exec_flags != &eb->flags[i]);
Chris Wilson59bfa122016-08-04 16:32:31 +0100784
785 /*
786 * SNA is doing fancy tricks with compressing batch buffers, which leads
787 * to negative relocation deltas. Usually that works out ok since the
788 * relocate address is still positive, except when the batch is placed
789 * very low in the GTT. Ensure this doesn't happen.
790 *
791 * Note that actual hangs have only been observed on gen7, but for
792 * paranoia do it everywhere.
793 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100794 if (!(eb->flags[i] & EXEC_OBJECT_PINNED))
795 eb->flags[i] |= __EXEC_OBJECT_NEEDS_BIAS;
Chris Wilson2889caa2017-06-16 15:05:19 +0100796 if (eb->reloc_cache.has_fence)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100797 eb->flags[i] |= EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson59bfa122016-08-04 16:32:31 +0100798
Chris Wilson2889caa2017-06-16 15:05:19 +0100799 eb->args->flags |= __EXEC_VALIDATED;
800 return eb_reserve(eb);
801
Chris Wilson170fa292017-08-16 09:52:07 +0100802err_obj:
Chris Wilsonac70ebe2017-09-12 16:07:52 +0100803 i915_gem_object_put(obj);
Chris Wilson170fa292017-08-16 09:52:07 +0100804err_vma:
805 eb->vma[i] = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100806 return err;
Chris Wilson59bfa122016-08-04 16:32:31 +0100807}
808
Chris Wilson4ff4b442017-06-16 15:05:16 +0100809static struct i915_vma *
Chris Wilson2889caa2017-06-16 15:05:19 +0100810eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000811{
Chris Wilson2889caa2017-06-16 15:05:19 +0100812 if (eb->lut_size < 0) {
813 if (handle >= -eb->lut_size)
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000814 return NULL;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100815 return eb->vma[handle];
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000816 } else {
817 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800818 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000819
Chris Wilson2889caa2017-06-16 15:05:19 +0100820 head = &eb->buckets[hash_32(handle, eb->lut_size)];
Geliang Tangaa459502016-01-18 23:54:20 +0800821 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200822 if (vma->exec_handle == handle)
823 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000824 }
825 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000826 }
Chris Wilson67731b82010-12-08 10:38:14 +0000827}
828
Chris Wilson2889caa2017-06-16 15:05:19 +0100829static void eb_release_vmas(const struct i915_execbuffer *eb)
Chris Wilsona415d352013-11-26 11:23:15 +0000830{
Chris Wilson2889caa2017-06-16 15:05:19 +0100831 const unsigned int count = eb->buffer_count;
832 unsigned int i;
Chris Wilson650bc632017-06-15 09:14:33 +0100833
Chris Wilson2889caa2017-06-16 15:05:19 +0100834 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100835 struct i915_vma *vma = eb->vma[i];
836 unsigned int flags = eb->flags[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100837
838 if (!vma)
Chris Wilson170fa292017-08-16 09:52:07 +0100839 break;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000840
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100841 GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
842 vma->exec_flags = NULL;
843 eb->vma[i] = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100844
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100845 if (flags & __EXEC_OBJECT_HAS_PIN)
846 __eb_unreserve_vma(vma, flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100847
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100848 if (flags & __EXEC_OBJECT_HAS_REF)
Chris Wilsondade2a62017-06-16 15:05:20 +0100849 i915_vma_put(vma);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000850 }
Chris Wilson2889caa2017-06-16 15:05:19 +0100851}
Chris Wilsond55495b2017-06-15 09:14:34 +0100852
Chris Wilson2889caa2017-06-16 15:05:19 +0100853static void eb_reset_vmas(const struct i915_execbuffer *eb)
854{
855 eb_release_vmas(eb);
Chris Wilson4d470f72017-06-29 16:04:25 +0100856 if (eb->lut_size > 0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100857 memset(eb->buckets, 0,
858 sizeof(struct hlist_head) << eb->lut_size);
859}
Chris Wilsond55495b2017-06-15 09:14:34 +0100860
Chris Wilson2889caa2017-06-16 15:05:19 +0100861static void eb_destroy(const struct i915_execbuffer *eb)
862{
Chris Wilson7dd4f672017-06-16 15:05:24 +0100863 GEM_BUG_ON(eb->reloc_cache.rq);
864
Chris Wilson4d470f72017-06-29 16:04:25 +0100865 if (eb->lut_size > 0)
Chris Wilsond55495b2017-06-15 09:14:34 +0100866 kfree(eb->buckets);
Chris Wilson67731b82010-12-08 10:38:14 +0000867}
868
Chris Wilson2889caa2017-06-16 15:05:19 +0100869static inline u64
Chris Wilsond50415c2016-08-18 17:16:52 +0100870relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +0100871 const struct i915_vma *target)
Michał Winiarski934acce2015-12-29 18:24:52 +0100872{
Chris Wilson2889caa2017-06-16 15:05:19 +0100873 return gen8_canonical_addr((int)reloc->delta + target->node.start);
Michał Winiarski934acce2015-12-29 18:24:52 +0100874}
875
Chris Wilsond50415c2016-08-18 17:16:52 +0100876static void reloc_cache_init(struct reloc_cache *cache,
877 struct drm_i915_private *i915)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100878{
Chris Wilson31a39202016-08-18 17:16:46 +0100879 cache->page = -1;
Chris Wilsond50415c2016-08-18 17:16:52 +0100880 cache->vaddr = 0;
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200881 /* Must be a variable in the struct to allow GCC to unroll. */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100882 cache->gen = INTEL_GEN(i915);
Chris Wilson2889caa2017-06-16 15:05:19 +0100883 cache->has_llc = HAS_LLC(i915);
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200884 cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
Chris Wilson7dd4f672017-06-16 15:05:24 +0100885 cache->has_fence = cache->gen < 4;
886 cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
Chris Wilsone8cb9092016-08-18 17:16:53 +0100887 cache->node.allocated = false;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100888 cache->rq = NULL;
889 cache->rq_size = 0;
Chris Wilson31a39202016-08-18 17:16:46 +0100890}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100891
Chris Wilsond50415c2016-08-18 17:16:52 +0100892static inline void *unmask_page(unsigned long p)
893{
894 return (void *)(uintptr_t)(p & PAGE_MASK);
895}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100896
Chris Wilsond50415c2016-08-18 17:16:52 +0100897static inline unsigned int unmask_flags(unsigned long p)
898{
899 return p & ~PAGE_MASK;
900}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700901
Chris Wilsond50415c2016-08-18 17:16:52 +0100902#define KMAP 0x4 /* after CLFLUSH_FLAGS */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700903
Chris Wilson650bc632017-06-15 09:14:33 +0100904static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
905{
906 struct drm_i915_private *i915 =
907 container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
908 return &i915->ggtt;
909}
910
Chris Wilson7dd4f672017-06-16 15:05:24 +0100911static void reloc_gpu_flush(struct reloc_cache *cache)
912{
913 GEM_BUG_ON(cache->rq_size >= cache->rq->batch->obj->base.size / sizeof(u32));
914 cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END;
915 i915_gem_object_unpin_map(cache->rq->batch->obj);
916 i915_gem_chipset_flush(cache->rq->i915);
917
Chris Wilsone61e0f52018-02-21 09:56:36 +0000918 __i915_request_add(cache->rq, true);
Chris Wilson7dd4f672017-06-16 15:05:24 +0100919 cache->rq = NULL;
920}
921
Chris Wilson650bc632017-06-15 09:14:33 +0100922static void reloc_cache_reset(struct reloc_cache *cache)
Chris Wilson31a39202016-08-18 17:16:46 +0100923{
Chris Wilsond50415c2016-08-18 17:16:52 +0100924 void *vaddr;
925
Chris Wilson7dd4f672017-06-16 15:05:24 +0100926 if (cache->rq)
927 reloc_gpu_flush(cache);
928
Chris Wilson31a39202016-08-18 17:16:46 +0100929 if (!cache->vaddr)
930 return;
931
Chris Wilsond50415c2016-08-18 17:16:52 +0100932 vaddr = unmask_page(cache->vaddr);
933 if (cache->vaddr & KMAP) {
934 if (cache->vaddr & CLFLUSH_AFTER)
935 mb();
Chris Wilson31a39202016-08-18 17:16:46 +0100936
Chris Wilsond50415c2016-08-18 17:16:52 +0100937 kunmap_atomic(vaddr);
938 i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
939 } else {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100940 wmb();
Chris Wilsond50415c2016-08-18 17:16:52 +0100941 io_mapping_unmap_atomic((void __iomem *)vaddr);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100942 if (cache->node.allocated) {
Chris Wilson650bc632017-06-15 09:14:33 +0100943 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100944
945 ggtt->base.clear_range(&ggtt->base,
946 cache->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200947 cache->node.size);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100948 drm_mm_remove_node(&cache->node);
949 } else {
950 i915_vma_unpin((struct i915_vma *)cache->node.mm);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700951 }
Chris Wilson31a39202016-08-18 17:16:46 +0100952 }
Chris Wilson650bc632017-06-15 09:14:33 +0100953
954 cache->vaddr = 0;
955 cache->page = -1;
Chris Wilson31a39202016-08-18 17:16:46 +0100956}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700957
Chris Wilson31a39202016-08-18 17:16:46 +0100958static void *reloc_kmap(struct drm_i915_gem_object *obj,
959 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100960 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100961{
Chris Wilsond50415c2016-08-18 17:16:52 +0100962 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100963
Chris Wilsond50415c2016-08-18 17:16:52 +0100964 if (cache->vaddr) {
965 kunmap_atomic(unmask_page(cache->vaddr));
966 } else {
967 unsigned int flushes;
Chris Wilson2889caa2017-06-16 15:05:19 +0100968 int err;
Chris Wilson31a39202016-08-18 17:16:46 +0100969
Chris Wilson2889caa2017-06-16 15:05:19 +0100970 err = i915_gem_obj_prepare_shmem_write(obj, &flushes);
971 if (err)
972 return ERR_PTR(err);
Chris Wilsond50415c2016-08-18 17:16:52 +0100973
974 BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
975 BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
976
977 cache->vaddr = flushes | KMAP;
978 cache->node.mm = (void *)obj;
979 if (flushes)
980 mb();
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700981 }
982
Chris Wilsond50415c2016-08-18 17:16:52 +0100983 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
984 cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100985 cache->page = page;
Chris Wilson31a39202016-08-18 17:16:46 +0100986
Chris Wilsond50415c2016-08-18 17:16:52 +0100987 return vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100988}
989
Chris Wilsond50415c2016-08-18 17:16:52 +0100990static void *reloc_iomap(struct drm_i915_gem_object *obj,
Chris Wilson31a39202016-08-18 17:16:46 +0100991 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100992 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100993{
Chris Wilson650bc632017-06-15 09:14:33 +0100994 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100995 unsigned long offset;
Chris Wilsond50415c2016-08-18 17:16:52 +0100996 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100997
Chris Wilsond50415c2016-08-18 17:16:52 +0100998 if (cache->vaddr) {
Jani Nikula615e5002016-10-04 12:54:13 +0300999 io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
Chris Wilsond50415c2016-08-18 17:16:52 +01001000 } else {
1001 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001002 int err;
Chris Wilson31a39202016-08-18 17:16:46 +01001003
Chris Wilson2889caa2017-06-16 15:05:19 +01001004 if (use_cpu_reloc(cache, obj))
Chris Wilsond50415c2016-08-18 17:16:52 +01001005 return NULL;
Chris Wilson31a39202016-08-18 17:16:46 +01001006
Chris Wilson2889caa2017-06-16 15:05:19 +01001007 err = i915_gem_object_set_to_gtt_domain(obj, true);
1008 if (err)
1009 return ERR_PTR(err);
Chris Wilson31a39202016-08-18 17:16:46 +01001010
Chris Wilsond50415c2016-08-18 17:16:52 +01001011 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilson3c755c52017-10-09 09:43:59 +01001012 PIN_MAPPABLE |
1013 PIN_NONBLOCK |
1014 PIN_NONFAULT);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001015 if (IS_ERR(vma)) {
1016 memset(&cache->node, 0, sizeof(cache->node));
Chris Wilson2889caa2017-06-16 15:05:19 +01001017 err = drm_mm_insert_node_in_range
Chris Wilsone8cb9092016-08-18 17:16:53 +01001018 (&ggtt->base.mm, &cache->node,
Chris Wilsonf51455d2017-01-10 14:47:34 +00001019 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
Chris Wilsone8cb9092016-08-18 17:16:53 +01001020 0, ggtt->mappable_end,
Chris Wilson4e64e552017-02-02 21:04:38 +00001021 DRM_MM_INSERT_LOW);
Chris Wilson2889caa2017-06-16 15:05:19 +01001022 if (err) /* no inactive aperture space, use cpu reloc */
Chris Wilsonc92fa4f2016-10-07 07:53:25 +01001023 return NULL;
Chris Wilsone8cb9092016-08-18 17:16:53 +01001024 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +01001025 err = i915_vma_put_fence(vma);
1026 if (err) {
Chris Wilsone8cb9092016-08-18 17:16:53 +01001027 i915_vma_unpin(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001028 return ERR_PTR(err);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001029 }
Rafael Barbalho5032d872013-08-21 17:10:51 +01001030
Chris Wilsone8cb9092016-08-18 17:16:53 +01001031 cache->node.start = vma->node.start;
1032 cache->node.mm = (void *)vma;
Chris Wilsond50415c2016-08-18 17:16:52 +01001033 }
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001034 }
1035
Chris Wilsone8cb9092016-08-18 17:16:53 +01001036 offset = cache->node.start;
1037 if (cache->node.allocated) {
Chris Wilsonfc099092016-10-28 15:27:56 +01001038 wmb();
Chris Wilsone8cb9092016-08-18 17:16:53 +01001039 ggtt->base.insert_page(&ggtt->base,
1040 i915_gem_object_get_dma_address(obj, page),
1041 offset, I915_CACHE_NONE, 0);
1042 } else {
1043 offset += page << PAGE_SHIFT;
1044 }
1045
Matthew Auld73ebd502017-12-11 15:18:20 +00001046 vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->iomap,
Chris Wilson650bc632017-06-15 09:14:33 +01001047 offset);
Chris Wilsond50415c2016-08-18 17:16:52 +01001048 cache->page = page;
1049 cache->vaddr = (unsigned long)vaddr;
1050
1051 return vaddr;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001052}
1053
Chris Wilsond50415c2016-08-18 17:16:52 +01001054static void *reloc_vaddr(struct drm_i915_gem_object *obj,
1055 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001056 unsigned long page)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001057{
Chris Wilsond50415c2016-08-18 17:16:52 +01001058 void *vaddr;
1059
1060 if (cache->page == page) {
1061 vaddr = unmask_page(cache->vaddr);
1062 } else {
1063 vaddr = NULL;
1064 if ((cache->vaddr & KMAP) == 0)
1065 vaddr = reloc_iomap(obj, cache, page);
1066 if (!vaddr)
1067 vaddr = reloc_kmap(obj, cache, page);
1068 }
1069
1070 return vaddr;
1071}
1072
1073static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
1074{
1075 if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
1076 if (flushes & CLFLUSH_BEFORE) {
1077 clflushopt(addr);
1078 mb();
1079 }
1080
1081 *addr = value;
1082
Chris Wilson2889caa2017-06-16 15:05:19 +01001083 /*
1084 * Writes to the same cacheline are serialised by the CPU
Chris Wilsond50415c2016-08-18 17:16:52 +01001085 * (including clflush). On the write path, we only require
1086 * that it hits memory in an orderly fashion and place
1087 * mb barriers at the start and end of the relocation phase
1088 * to ensure ordering of clflush wrt to the system.
1089 */
1090 if (flushes & CLFLUSH_AFTER)
1091 clflushopt(addr);
1092 } else
1093 *addr = value;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001094}
1095
Chris Wilson7dd4f672017-06-16 15:05:24 +01001096static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
1097 struct i915_vma *vma,
1098 unsigned int len)
1099{
1100 struct reloc_cache *cache = &eb->reloc_cache;
1101 struct drm_i915_gem_object *obj;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001102 struct i915_request *rq;
Chris Wilson7dd4f672017-06-16 15:05:24 +01001103 struct i915_vma *batch;
1104 u32 *cmd;
1105 int err;
1106
Christian Königc0a51fd2018-02-16 13:43:38 +01001107 GEM_BUG_ON(vma->obj->write_domain & I915_GEM_DOMAIN_CPU);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001108
1109 obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, PAGE_SIZE);
1110 if (IS_ERR(obj))
1111 return PTR_ERR(obj);
1112
1113 cmd = i915_gem_object_pin_map(obj,
Chris Wilsona575c672017-08-28 11:46:31 +01001114 cache->has_llc ?
1115 I915_MAP_FORCE_WB :
1116 I915_MAP_FORCE_WC);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001117 i915_gem_object_unpin_pages(obj);
1118 if (IS_ERR(cmd))
1119 return PTR_ERR(cmd);
1120
1121 err = i915_gem_object_set_to_wc_domain(obj, false);
1122 if (err)
1123 goto err_unmap;
1124
1125 batch = i915_vma_instance(obj, vma->vm, NULL);
1126 if (IS_ERR(batch)) {
1127 err = PTR_ERR(batch);
1128 goto err_unmap;
1129 }
1130
1131 err = i915_vma_pin(batch, 0, 0, PIN_USER | PIN_NONBLOCK);
1132 if (err)
1133 goto err_unmap;
1134
Chris Wilsone61e0f52018-02-21 09:56:36 +00001135 rq = i915_request_alloc(eb->engine, eb->ctx);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001136 if (IS_ERR(rq)) {
1137 err = PTR_ERR(rq);
1138 goto err_unpin;
1139 }
1140
Chris Wilsone61e0f52018-02-21 09:56:36 +00001141 err = i915_request_await_object(rq, vma->obj, true);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001142 if (err)
1143 goto err_request;
1144
Chris Wilson7dd4f672017-06-16 15:05:24 +01001145 err = eb->engine->emit_bb_start(rq,
1146 batch->node.start, PAGE_SIZE,
1147 cache->gen > 5 ? 0 : I915_DISPATCH_SECURE);
1148 if (err)
1149 goto err_request;
1150
Chris Wilson95ff7c72017-06-16 15:05:25 +01001151 GEM_BUG_ON(!reservation_object_test_signaled_rcu(batch->resv, true));
Chris Wilson7dd4f672017-06-16 15:05:24 +01001152 i915_vma_move_to_active(batch, rq, 0);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001153 reservation_object_lock(batch->resv, NULL);
1154 reservation_object_add_excl_fence(batch->resv, &rq->fence);
1155 reservation_object_unlock(batch->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001156 i915_vma_unpin(batch);
1157
Chris Wilson25ffaa62017-06-20 13:43:20 +01001158 i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001159 reservation_object_lock(vma->resv, NULL);
1160 reservation_object_add_excl_fence(vma->resv, &rq->fence);
1161 reservation_object_unlock(vma->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001162
1163 rq->batch = batch;
1164
1165 cache->rq = rq;
1166 cache->rq_cmd = cmd;
1167 cache->rq_size = 0;
1168
1169 /* Return with batch mapping (cmd) still pinned */
1170 return 0;
1171
1172err_request:
Chris Wilsone61e0f52018-02-21 09:56:36 +00001173 i915_request_add(rq);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001174err_unpin:
1175 i915_vma_unpin(batch);
1176err_unmap:
1177 i915_gem_object_unpin_map(obj);
1178 return err;
1179}
1180
1181static u32 *reloc_gpu(struct i915_execbuffer *eb,
1182 struct i915_vma *vma,
1183 unsigned int len)
1184{
1185 struct reloc_cache *cache = &eb->reloc_cache;
1186 u32 *cmd;
1187
1188 if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1))
1189 reloc_gpu_flush(cache);
1190
1191 if (unlikely(!cache->rq)) {
1192 int err;
1193
Chris Wilson3dbf26e2017-08-26 14:56:20 +01001194 /* If we need to copy for the cmdparser, we will stall anyway */
1195 if (eb_use_cmdparser(eb))
1196 return ERR_PTR(-EWOULDBLOCK);
1197
Chris Wilson90cad092017-09-06 16:28:59 +01001198 if (!intel_engine_can_store_dword(eb->engine))
1199 return ERR_PTR(-ENODEV);
1200
Chris Wilson7dd4f672017-06-16 15:05:24 +01001201 err = __reloc_gpu_alloc(eb, vma, len);
1202 if (unlikely(err))
1203 return ERR_PTR(err);
1204 }
1205
1206 cmd = cache->rq_cmd + cache->rq_size;
1207 cache->rq_size += len;
1208
1209 return cmd;
1210}
1211
Chris Wilson2889caa2017-06-16 15:05:19 +01001212static u64
1213relocate_entry(struct i915_vma *vma,
Chris Wilsond50415c2016-08-18 17:16:52 +01001214 const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +01001215 struct i915_execbuffer *eb,
1216 const struct i915_vma *target)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001217{
Chris Wilsond50415c2016-08-18 17:16:52 +01001218 u64 offset = reloc->offset;
Chris Wilson2889caa2017-06-16 15:05:19 +01001219 u64 target_offset = relocation_target(reloc, target);
1220 bool wide = eb->reloc_cache.use_64bit_reloc;
Chris Wilsond50415c2016-08-18 17:16:52 +01001221 void *vaddr;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001222
Chris Wilson7dd4f672017-06-16 15:05:24 +01001223 if (!eb->reloc_cache.vaddr &&
1224 (DBG_FORCE_RELOC == FORCE_GPU_RELOC ||
Chris Wilson90cad092017-09-06 16:28:59 +01001225 !reservation_object_test_signaled_rcu(vma->resv, true))) {
Chris Wilson7dd4f672017-06-16 15:05:24 +01001226 const unsigned int gen = eb->reloc_cache.gen;
1227 unsigned int len;
1228 u32 *batch;
1229 u64 addr;
1230
1231 if (wide)
1232 len = offset & 7 ? 8 : 5;
1233 else if (gen >= 4)
1234 len = 4;
Chris Wilsonf2f5c062017-08-16 09:52:04 +01001235 else
Chris Wilson7dd4f672017-06-16 15:05:24 +01001236 len = 3;
Chris Wilson7dd4f672017-06-16 15:05:24 +01001237
1238 batch = reloc_gpu(eb, vma, len);
1239 if (IS_ERR(batch))
1240 goto repeat;
1241
1242 addr = gen8_canonical_addr(vma->node.start + offset);
1243 if (wide) {
1244 if (offset & 7) {
1245 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1246 *batch++ = lower_32_bits(addr);
1247 *batch++ = upper_32_bits(addr);
1248 *batch++ = lower_32_bits(target_offset);
1249
1250 addr = gen8_canonical_addr(addr + 4);
1251
1252 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1253 *batch++ = lower_32_bits(addr);
1254 *batch++ = upper_32_bits(addr);
1255 *batch++ = upper_32_bits(target_offset);
1256 } else {
1257 *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1;
1258 *batch++ = lower_32_bits(addr);
1259 *batch++ = upper_32_bits(addr);
1260 *batch++ = lower_32_bits(target_offset);
1261 *batch++ = upper_32_bits(target_offset);
1262 }
1263 } else if (gen >= 6) {
1264 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1265 *batch++ = 0;
1266 *batch++ = addr;
1267 *batch++ = target_offset;
1268 } else if (gen >= 4) {
1269 *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1270 *batch++ = 0;
1271 *batch++ = addr;
1272 *batch++ = target_offset;
1273 } else {
1274 *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
1275 *batch++ = addr;
1276 *batch++ = target_offset;
1277 }
1278
1279 goto out;
1280 }
1281
Chris Wilsond50415c2016-08-18 17:16:52 +01001282repeat:
Chris Wilson95ff7c72017-06-16 15:05:25 +01001283 vaddr = reloc_vaddr(vma->obj, &eb->reloc_cache, offset >> PAGE_SHIFT);
Chris Wilsond50415c2016-08-18 17:16:52 +01001284 if (IS_ERR(vaddr))
1285 return PTR_ERR(vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001286
Chris Wilsond50415c2016-08-18 17:16:52 +01001287 clflush_write32(vaddr + offset_in_page(offset),
1288 lower_32_bits(target_offset),
Chris Wilson2889caa2017-06-16 15:05:19 +01001289 eb->reloc_cache.vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001290
Chris Wilsond50415c2016-08-18 17:16:52 +01001291 if (wide) {
1292 offset += sizeof(u32);
1293 target_offset >>= 32;
1294 wide = false;
1295 goto repeat;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001296 }
Chris Wilsondabdfe02012-03-26 10:10:27 +02001297
Chris Wilson7dd4f672017-06-16 15:05:24 +01001298out:
Chris Wilson2889caa2017-06-16 15:05:19 +01001299 return target->node.start | UPDATE;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001300}
1301
Chris Wilson2889caa2017-06-16 15:05:19 +01001302static u64
1303eb_relocate_entry(struct i915_execbuffer *eb,
1304 struct i915_vma *vma,
1305 const struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001306{
Chris Wilson507d9772017-06-16 15:05:17 +01001307 struct i915_vma *target;
Chris Wilson2889caa2017-06-16 15:05:19 +01001308 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001309
Chris Wilson67731b82010-12-08 10:38:14 +00001310 /* we've already hold a reference to all valid objects */
Chris Wilson507d9772017-06-16 15:05:17 +01001311 target = eb_get_vma(eb, reloc->target_handle);
1312 if (unlikely(!target))
Chris Wilson54cf91d2010-11-25 18:00:26 +00001313 return -ENOENT;
Eric Anholte844b992012-07-31 15:35:01 -07001314
Chris Wilson54cf91d2010-11-25 18:00:26 +00001315 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001316 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001317 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001318 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001319 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001320 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001321 (int) reloc->offset,
1322 reloc->read_domains,
1323 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001324 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001325 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +01001326 if (unlikely((reloc->write_domain | reloc->read_domains)
1327 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001328 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001329 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001330 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001331 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001332 (int) reloc->offset,
1333 reloc->read_domains,
1334 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001335 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001336 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001337
Chris Wilson2889caa2017-06-16 15:05:19 +01001338 if (reloc->write_domain) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001339 *target->exec_flags |= EXEC_OBJECT_WRITE;
Chris Wilson507d9772017-06-16 15:05:17 +01001340
Chris Wilson2889caa2017-06-16 15:05:19 +01001341 /*
1342 * Sandybridge PPGTT errata: We need a global gtt mapping
1343 * for MI and pipe_control writes because the gpu doesn't
1344 * properly redirect them through the ppgtt for non_secure
1345 * batchbuffers.
1346 */
1347 if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
1348 IS_GEN6(eb->i915)) {
1349 err = i915_vma_bind(target, target->obj->cache_level,
1350 PIN_GLOBAL);
1351 if (WARN_ONCE(err,
1352 "Unexpected failure to bind target VMA!"))
1353 return err;
1354 }
Chris Wilson507d9772017-06-16 15:05:17 +01001355 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001356
Chris Wilson2889caa2017-06-16 15:05:19 +01001357 /*
1358 * If the relocation already has the right value in it, no
Chris Wilson54cf91d2010-11-25 18:00:26 +00001359 * more work needs to be done.
1360 */
Chris Wilson7dd4f672017-06-16 15:05:24 +01001361 if (!DBG_FORCE_RELOC &&
1362 gen8_canonical_addr(target->node.start) == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +00001363 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001364
1365 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001366 if (unlikely(reloc->offset >
Chris Wilson507d9772017-06-16 15:05:17 +01001367 vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001368 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson507d9772017-06-16 15:05:17 +01001369 "target %d offset %d size %d.\n",
1370 reloc->target_handle,
1371 (int)reloc->offset,
1372 (int)vma->size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001373 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001374 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001375 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001376 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson507d9772017-06-16 15:05:17 +01001377 "target %d offset %d.\n",
1378 reloc->target_handle,
1379 (int)reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001380 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001381 }
1382
Chris Wilson071750e2017-06-16 15:05:18 +01001383 /*
1384 * If we write into the object, we need to force the synchronisation
1385 * barrier, either with an asynchronous clflush or if we executed the
1386 * patching using the GPU (though that should be serialised by the
1387 * timeline). To be completely sure, and since we are required to
1388 * do relocations we are already stalling, disable the user's opt
Chris Wilson0519bcb2017-08-16 09:52:09 +01001389 * out of our synchronisation.
Chris Wilson071750e2017-06-16 15:05:18 +01001390 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001391 *vma->exec_flags &= ~EXEC_OBJECT_ASYNC;
Chris Wilson071750e2017-06-16 15:05:18 +01001392
Chris Wilson54cf91d2010-11-25 18:00:26 +00001393 /* and update the user's relocation entry */
Chris Wilson2889caa2017-06-16 15:05:19 +01001394 return relocate_entry(vma, reloc, eb, target);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001395}
1396
Chris Wilson2889caa2017-06-16 15:05:19 +01001397static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001398{
Chris Wilson1d83f442012-03-24 20:12:53 +00001399#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
Chris Wilson2889caa2017-06-16 15:05:19 +01001400 struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
1401 struct drm_i915_gem_relocation_entry __user *urelocs;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001402 const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001403 unsigned int remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001404
Chris Wilson2889caa2017-06-16 15:05:19 +01001405 urelocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson1d83f442012-03-24 20:12:53 +00001406 remain = entry->relocation_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01001407 if (unlikely(remain > N_RELOC(ULONG_MAX)))
1408 return -EINVAL;
Chris Wilsonebc08082016-10-18 13:02:51 +01001409
Chris Wilson2889caa2017-06-16 15:05:19 +01001410 /*
1411 * We must check that the entire relocation array is safe
1412 * to read. However, if the array is not writable the user loses
1413 * the updated relocation values.
1414 */
Imre Deakedd90032017-07-14 18:12:42 +03001415 if (unlikely(!access_ok(VERIFY_READ, urelocs, remain*sizeof(*urelocs))))
Chris Wilson2889caa2017-06-16 15:05:19 +01001416 return -EFAULT;
Chris Wilson1d83f442012-03-24 20:12:53 +00001417
Chris Wilson2889caa2017-06-16 15:05:19 +01001418 do {
1419 struct drm_i915_gem_relocation_entry *r = stack;
1420 unsigned int count =
1421 min_t(unsigned int, remain, ARRAY_SIZE(stack));
1422 unsigned int copied;
1423
1424 /*
1425 * This is the fast path and we cannot handle a pagefault
Chris Wilsonebc08082016-10-18 13:02:51 +01001426 * whilst holding the struct mutex lest the user pass in the
1427 * relocations contained within a mmaped bo. For in such a case
1428 * we, the page fault handler would call i915_gem_fault() and
1429 * we would try to acquire the struct mutex again. Obviously
1430 * this is bad and so lockdep complains vehemently.
1431 */
1432 pagefault_disable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001433 copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
Chris Wilsonebc08082016-10-18 13:02:51 +01001434 pagefault_enable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001435 if (unlikely(copied)) {
1436 remain = -EFAULT;
Chris Wilson31a39202016-08-18 17:16:46 +01001437 goto out;
1438 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001439
Chris Wilson2889caa2017-06-16 15:05:19 +01001440 remain -= count;
Chris Wilson1d83f442012-03-24 20:12:53 +00001441 do {
Chris Wilson2889caa2017-06-16 15:05:19 +01001442 u64 offset = eb_relocate_entry(eb, vma, r);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001443
Chris Wilson2889caa2017-06-16 15:05:19 +01001444 if (likely(offset == 0)) {
1445 } else if ((s64)offset < 0) {
1446 remain = (int)offset;
Chris Wilson31a39202016-08-18 17:16:46 +01001447 goto out;
Chris Wilson2889caa2017-06-16 15:05:19 +01001448 } else {
1449 /*
1450 * Note that reporting an error now
1451 * leaves everything in an inconsistent
1452 * state as we have *already* changed
1453 * the relocation value inside the
1454 * object. As we have not changed the
1455 * reloc.presumed_offset or will not
1456 * change the execobject.offset, on the
1457 * call we may not rewrite the value
1458 * inside the object, leaving it
1459 * dangling and causing a GPU hang. Unless
1460 * userspace dynamically rebuilds the
1461 * relocations on each execbuf rather than
1462 * presume a static tree.
1463 *
1464 * We did previously check if the relocations
1465 * were writable (access_ok), an error now
1466 * would be a strange race with mprotect,
1467 * having already demonstrated that we
1468 * can read from this userspace address.
1469 */
1470 offset = gen8_canonical_addr(offset & ~UPDATE);
1471 __put_user(offset,
1472 &urelocs[r-stack].presumed_offset);
Chris Wilson1d83f442012-03-24 20:12:53 +00001473 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001474 } while (r++, --count);
1475 urelocs += ARRAY_SIZE(stack);
1476 } while (remain);
Chris Wilson31a39202016-08-18 17:16:46 +01001477out:
Chris Wilson650bc632017-06-15 09:14:33 +01001478 reloc_cache_reset(&eb->reloc_cache);
Chris Wilson2889caa2017-06-16 15:05:19 +01001479 return remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001480}
1481
1482static int
Chris Wilson2889caa2017-06-16 15:05:19 +01001483eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001484{
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001485 const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001486 struct drm_i915_gem_relocation_entry *relocs =
1487 u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1488 unsigned int i;
1489 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001490
1491 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001492 u64 offset = eb_relocate_entry(eb, vma, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001493
Chris Wilson2889caa2017-06-16 15:05:19 +01001494 if ((s64)offset < 0) {
1495 err = (int)offset;
1496 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001497 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001498 }
1499 err = 0;
Chris Wilsona415d352013-11-26 11:23:15 +00001500err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001501 reloc_cache_reset(&eb->reloc_cache);
1502 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001503}
1504
Chris Wilson2889caa2017-06-16 15:05:19 +01001505static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001506{
Chris Wilson2889caa2017-06-16 15:05:19 +01001507 const char __user *addr, *end;
1508 unsigned long size;
1509 char __maybe_unused c;
Ben Widawsky27173f12013-08-14 11:38:36 +02001510
Chris Wilson2889caa2017-06-16 15:05:19 +01001511 size = entry->relocation_count;
1512 if (size == 0)
1513 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001514
Chris Wilson2889caa2017-06-16 15:05:19 +01001515 if (size > N_RELOC(ULONG_MAX))
1516 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001517
Chris Wilson2889caa2017-06-16 15:05:19 +01001518 addr = u64_to_user_ptr(entry->relocs_ptr);
1519 size *= sizeof(struct drm_i915_gem_relocation_entry);
1520 if (!access_ok(VERIFY_READ, addr, size))
1521 return -EFAULT;
1522
1523 end = addr + size;
1524 for (; addr < end; addr += PAGE_SIZE) {
1525 int err = __get_user(c, addr);
1526 if (err)
1527 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001528 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001529 return __get_user(c, end - 1);
1530}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001531
Chris Wilson2889caa2017-06-16 15:05:19 +01001532static int eb_copy_relocations(const struct i915_execbuffer *eb)
1533{
1534 const unsigned int count = eb->buffer_count;
1535 unsigned int i;
1536 int err;
1537
Chris Wilson54cf91d2010-11-25 18:00:26 +00001538 for (i = 0; i < count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001539 const unsigned int nreloc = eb->exec[i].relocation_count;
1540 struct drm_i915_gem_relocation_entry __user *urelocs;
1541 struct drm_i915_gem_relocation_entry *relocs;
1542 unsigned long size;
1543 unsigned long copied;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001544
Chris Wilson2889caa2017-06-16 15:05:19 +01001545 if (nreloc == 0)
1546 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001547
Chris Wilson2889caa2017-06-16 15:05:19 +01001548 err = check_relocations(&eb->exec[i]);
1549 if (err)
1550 goto err;
1551
1552 urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
1553 size = nreloc * sizeof(*relocs);
1554
Michal Hocko0ee931c2017-09-13 16:28:29 -07001555 relocs = kvmalloc_array(size, 1, GFP_KERNEL);
Chris Wilson2889caa2017-06-16 15:05:19 +01001556 if (!relocs) {
1557 kvfree(relocs);
1558 err = -ENOMEM;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001559 goto err;
1560 }
1561
Chris Wilson2889caa2017-06-16 15:05:19 +01001562 /* copy_from_user is limited to < 4GiB */
1563 copied = 0;
1564 do {
1565 unsigned int len =
1566 min_t(u64, BIT_ULL(31), size - copied);
1567
1568 if (__copy_from_user((char *)relocs + copied,
Ville Syrjälä908a6102017-09-01 19:54:34 +03001569 (char __user *)urelocs + copied,
Chris Wilson2889caa2017-06-16 15:05:19 +01001570 len)) {
1571 kvfree(relocs);
1572 err = -EFAULT;
1573 goto err;
1574 }
1575
1576 copied += len;
1577 } while (copied < size);
1578
1579 /*
1580 * As we do not update the known relocation offsets after
Chris Wilson262b6d32013-01-15 16:17:54 +00001581 * relocating (due to the complexities in lock handling),
1582 * we need to mark them as invalid now so that we force the
1583 * relocation processing next time. Just in case the target
1584 * object is evicted and then rebound into its old
1585 * presumed_offset before the next execbuffer - if that
1586 * happened we would make the mistake of assuming that the
1587 * relocations were valid.
1588 */
Chris Wilson2889caa2017-06-16 15:05:19 +01001589 user_access_begin();
1590 for (copied = 0; copied < nreloc; copied++)
1591 unsafe_put_user(-1,
1592 &urelocs[copied].presumed_offset,
1593 end_user);
1594end_user:
1595 user_access_end();
Chris Wilson262b6d32013-01-15 16:17:54 +00001596
Chris Wilson2889caa2017-06-16 15:05:19 +01001597 eb->exec[i].relocs_ptr = (uintptr_t)relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001598 }
1599
Chris Wilson2889caa2017-06-16 15:05:19 +01001600 return 0;
1601
1602err:
1603 while (i--) {
1604 struct drm_i915_gem_relocation_entry *relocs =
1605 u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr);
1606 if (eb->exec[i].relocation_count)
1607 kvfree(relocs);
1608 }
1609 return err;
1610}
1611
1612static int eb_prefault_relocations(const struct i915_execbuffer *eb)
1613{
1614 const unsigned int count = eb->buffer_count;
1615 unsigned int i;
1616
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001617 if (unlikely(i915_modparams.prefault_disable))
Chris Wilson2889caa2017-06-16 15:05:19 +01001618 return 0;
1619
1620 for (i = 0; i < count; i++) {
1621 int err;
1622
1623 err = check_relocations(&eb->exec[i]);
1624 if (err)
1625 return err;
1626 }
1627
1628 return 0;
1629}
1630
1631static noinline int eb_relocate_slow(struct i915_execbuffer *eb)
1632{
1633 struct drm_device *dev = &eb->i915->drm;
1634 bool have_copy = false;
1635 struct i915_vma *vma;
1636 int err = 0;
1637
1638repeat:
1639 if (signal_pending(current)) {
1640 err = -ERESTARTSYS;
1641 goto out;
1642 }
1643
1644 /* We may process another execbuffer during the unlock... */
1645 eb_reset_vmas(eb);
1646 mutex_unlock(&dev->struct_mutex);
1647
1648 /*
1649 * We take 3 passes through the slowpatch.
1650 *
1651 * 1 - we try to just prefault all the user relocation entries and
1652 * then attempt to reuse the atomic pagefault disabled fast path again.
1653 *
1654 * 2 - we copy the user entries to a local buffer here outside of the
1655 * local and allow ourselves to wait upon any rendering before
1656 * relocations
1657 *
1658 * 3 - we already have a local copy of the relocation entries, but
1659 * were interrupted (EAGAIN) whilst waiting for the objects, try again.
1660 */
1661 if (!err) {
1662 err = eb_prefault_relocations(eb);
1663 } else if (!have_copy) {
1664 err = eb_copy_relocations(eb);
1665 have_copy = err == 0;
1666 } else {
1667 cond_resched();
1668 err = 0;
1669 }
1670 if (err) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001671 mutex_lock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01001672 goto out;
1673 }
1674
Chris Wilson8a2421b2017-06-16 15:05:22 +01001675 /* A frequent cause for EAGAIN are currently unavailable client pages */
1676 flush_workqueue(eb->i915->mm.userptr_wq);
1677
Chris Wilson2889caa2017-06-16 15:05:19 +01001678 err = i915_mutex_lock_interruptible(dev);
1679 if (err) {
1680 mutex_lock(&dev->struct_mutex);
1681 goto out;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001682 }
1683
Chris Wilson67731b82010-12-08 10:38:14 +00001684 /* reacquire the objects */
Chris Wilson2889caa2017-06-16 15:05:19 +01001685 err = eb_lookup_vmas(eb);
1686 if (err)
Chris Wilson3b96eff2013-01-08 10:53:14 +00001687 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +00001688
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001689 GEM_BUG_ON(!eb->batch);
1690
Chris Wilson2889caa2017-06-16 15:05:19 +01001691 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1692 if (!have_copy) {
1693 pagefault_disable();
1694 err = eb_relocate_vma(eb, vma);
1695 pagefault_enable();
1696 if (err)
1697 goto repeat;
1698 } else {
1699 err = eb_relocate_vma_slow(eb, vma);
1700 if (err)
1701 goto err;
1702 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001703 }
1704
Chris Wilson2889caa2017-06-16 15:05:19 +01001705 /*
1706 * Leave the user relocations as are, this is the painfully slow path,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001707 * and we want to avoid the complication of dropping the lock whilst
1708 * having buffers reserved in the aperture and so causing spurious
1709 * ENOSPC for random operations.
1710 */
1711
1712err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001713 if (err == -EAGAIN)
1714 goto repeat;
1715
1716out:
1717 if (have_copy) {
1718 const unsigned int count = eb->buffer_count;
1719 unsigned int i;
1720
1721 for (i = 0; i < count; i++) {
1722 const struct drm_i915_gem_exec_object2 *entry =
1723 &eb->exec[i];
1724 struct drm_i915_gem_relocation_entry *relocs;
1725
1726 if (!entry->relocation_count)
1727 continue;
1728
1729 relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1730 kvfree(relocs);
1731 }
1732 }
1733
Chris Wilson1f727d92017-07-21 15:50:36 +01001734 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001735}
1736
Chris Wilson2889caa2017-06-16 15:05:19 +01001737static int eb_relocate(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001738{
Chris Wilson2889caa2017-06-16 15:05:19 +01001739 if (eb_lookup_vmas(eb))
1740 goto slow;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001741
Chris Wilson2889caa2017-06-16 15:05:19 +01001742 /* The objects are in their final locations, apply the relocations. */
1743 if (eb->args->flags & __EXEC_HAS_RELOC) {
1744 struct i915_vma *vma;
1745
1746 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1747 if (eb_relocate_vma(eb, vma))
1748 goto slow;
1749 }
1750 }
1751
1752 return 0;
1753
1754slow:
1755 return eb_relocate_slow(eb);
1756}
1757
Chris Wilson95ff7c72017-06-16 15:05:25 +01001758static void eb_export_fence(struct i915_vma *vma,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001759 struct i915_request *rq,
Chris Wilson2889caa2017-06-16 15:05:19 +01001760 unsigned int flags)
1761{
Chris Wilson95ff7c72017-06-16 15:05:25 +01001762 struct reservation_object *resv = vma->resv;
Chris Wilson2889caa2017-06-16 15:05:19 +01001763
1764 /*
1765 * Ignore errors from failing to allocate the new fence, we can't
1766 * handle an error right now. Worst case should be missed
1767 * synchronisation leading to rendering corruption.
1768 */
1769 reservation_object_lock(resv, NULL);
1770 if (flags & EXEC_OBJECT_WRITE)
Chris Wilsone61e0f52018-02-21 09:56:36 +00001771 reservation_object_add_excl_fence(resv, &rq->fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01001772 else if (reservation_object_reserve_shared(resv) == 0)
Chris Wilsone61e0f52018-02-21 09:56:36 +00001773 reservation_object_add_shared_fence(resv, &rq->fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01001774 reservation_object_unlock(resv);
1775}
1776
1777static int eb_move_to_gpu(struct i915_execbuffer *eb)
1778{
1779 const unsigned int count = eb->buffer_count;
1780 unsigned int i;
1781 int err;
1782
1783 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001784 unsigned int flags = eb->flags[i];
1785 struct i915_vma *vma = eb->vma[i];
Ben Widawsky27173f12013-08-14 11:38:36 +02001786 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001787
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001788 if (flags & EXEC_OBJECT_CAPTURE) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00001789 struct i915_capture_list *capture;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001790
1791 capture = kmalloc(sizeof(*capture), GFP_KERNEL);
1792 if (unlikely(!capture))
1793 return -ENOMEM;
1794
Chris Wilson650bc632017-06-15 09:14:33 +01001795 capture->next = eb->request->capture_list;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001796 capture->vma = eb->vma[i];
Chris Wilson650bc632017-06-15 09:14:33 +01001797 eb->request->capture_list = capture;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001798 }
1799
Chris Wilsonb8f55be2017-08-11 12:11:16 +01001800 /*
1801 * If the GPU is not _reading_ through the CPU cache, we need
1802 * to make sure that any writes (both previous GPU writes from
1803 * before a change in snooping levels and normal CPU writes)
1804 * caught in that cache are flushed to main memory.
1805 *
1806 * We want to say
1807 * obj->cache_dirty &&
1808 * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
1809 * but gcc's optimiser doesn't handle that as well and emits
1810 * two jumps instead of one. Maybe one day...
1811 */
1812 if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) {
Chris Wilson0f46daa2017-07-21 15:50:37 +01001813 if (i915_gem_clflush_object(obj, 0))
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001814 flags &= ~EXEC_OBJECT_ASYNC;
Chris Wilson0f46daa2017-07-21 15:50:37 +01001815 }
1816
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001817 if (flags & EXEC_OBJECT_ASYNC)
1818 continue;
Chris Wilson77ae9952017-01-27 09:40:07 +00001819
Chris Wilsone61e0f52018-02-21 09:56:36 +00001820 err = i915_request_await_object
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001821 (eb->request, obj, flags & EXEC_OBJECT_WRITE);
Chris Wilson2889caa2017-06-16 15:05:19 +01001822 if (err)
1823 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001824 }
1825
Chris Wilson2889caa2017-06-16 15:05:19 +01001826 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001827 unsigned int flags = eb->flags[i];
1828 struct i915_vma *vma = eb->vma[i];
Chris Wilson2889caa2017-06-16 15:05:19 +01001829
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001830 i915_vma_move_to_active(vma, eb->request, flags);
1831 eb_export_fence(vma, eb->request, flags);
1832
1833 __eb_unreserve_vma(vma, flags);
1834 vma->exec_flags = NULL;
1835
1836 if (unlikely(flags & __EXEC_OBJECT_HAS_REF))
Chris Wilsondade2a62017-06-16 15:05:20 +01001837 i915_vma_put(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001838 }
1839 eb->exec = NULL;
1840
Chris Wilsondcd79932016-08-18 17:16:40 +01001841 /* Unconditionally flush any chipset caches (for streaming writes). */
Chris Wilson650bc632017-06-15 09:14:33 +01001842 i915_gem_chipset_flush(eb->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001843
Chris Wilson21131842017-11-20 10:20:01 +00001844 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001845}
1846
Chris Wilson2889caa2017-06-16 15:05:19 +01001847static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001848{
Chris Wilson650bc632017-06-15 09:14:33 +01001849 if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
Daniel Vettered5982e2013-01-17 22:23:36 +01001850 return false;
1851
Chris Wilson2f5945b2015-10-06 11:39:55 +01001852 /* Kernel clipping was a DRI1 misfeature */
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01001853 if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
1854 if (exec->num_cliprects || exec->cliprects_ptr)
1855 return false;
1856 }
Chris Wilson2f5945b2015-10-06 11:39:55 +01001857
1858 if (exec->DR4 == 0xffffffff) {
1859 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1860 exec->DR4 = 0;
1861 }
1862 if (exec->DR1 || exec->DR4)
1863 return false;
1864
1865 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1866 return false;
1867
1868 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001869}
1870
Chris Wilson5cf3d282016-08-04 07:52:43 +01001871void i915_vma_move_to_active(struct i915_vma *vma,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001872 struct i915_request *rq,
Chris Wilson5cf3d282016-08-04 07:52:43 +01001873 unsigned int flags)
1874{
1875 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001876 const unsigned int idx = rq->engine->id;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001877
Chris Wilsone61e0f52018-02-21 09:56:36 +00001878 lockdep_assert_held(&rq->i915->drm.struct_mutex);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001879 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1880
Chris Wilson2889caa2017-06-16 15:05:19 +01001881 /*
1882 * Add a reference if we're newly entering the active list.
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001883 * The order in which we add operations to the retirement queue is
1884 * vital here: mark_active adds to the start of the callback list,
1885 * such that subsequent callbacks are called first. Therefore we
1886 * add the active reference first and queue for it to be dropped
1887 * *last*.
1888 */
Chris Wilsond07f0e52016-10-28 13:58:44 +01001889 if (!i915_vma_is_active(vma))
1890 obj->active_count++;
1891 i915_vma_set_active(vma, idx);
Chris Wilsone61e0f52018-02-21 09:56:36 +00001892 i915_gem_active_set(&vma->last_read[idx], rq);
Chris Wilsond07f0e52016-10-28 13:58:44 +01001893 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001894
Christian Königc0a51fd2018-02-16 13:43:38 +01001895 obj->write_domain = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001896 if (flags & EXEC_OBJECT_WRITE) {
Christian Königc0a51fd2018-02-16 13:43:38 +01001897 obj->write_domain = I915_GEM_DOMAIN_RENDER;
Chris Wilsone27ab732017-06-15 13:38:49 +01001898
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00001899 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
Chris Wilsone61e0f52018-02-21 09:56:36 +00001900 i915_gem_active_set(&obj->frontbuffer_write, rq);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001901
Christian Königc0a51fd2018-02-16 13:43:38 +01001902 obj->read_domains = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001903 }
Christian Königc0a51fd2018-02-16 13:43:38 +01001904 obj->read_domains |= I915_GEM_GPU_DOMAINS;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001905
Chris Wilson49ef5292016-08-18 17:17:00 +01001906 if (flags & EXEC_OBJECT_NEEDS_FENCE)
Chris Wilsone61e0f52018-02-21 09:56:36 +00001907 i915_gem_active_set(&vma->last_fence, rq);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001908}
1909
Chris Wilsone61e0f52018-02-21 09:56:36 +00001910static int i915_reset_gen7_sol_offsets(struct i915_request *rq)
Eric Anholtae662d32012-01-03 09:23:29 -08001911{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001912 u32 *cs;
1913 int i;
Eric Anholtae662d32012-01-03 09:23:29 -08001914
Chris Wilsone61e0f52018-02-21 09:56:36 +00001915 if (!IS_GEN7(rq->i915) || rq->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001916 DRM_DEBUG("sol reset is gen7/rcs only\n");
1917 return -EINVAL;
1918 }
Eric Anholtae662d32012-01-03 09:23:29 -08001919
Chris Wilsone61e0f52018-02-21 09:56:36 +00001920 cs = intel_ring_begin(rq, 4 * 2 + 2);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001921 if (IS_ERR(cs))
1922 return PTR_ERR(cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001923
Chris Wilson2889caa2017-06-16 15:05:19 +01001924 *cs++ = MI_LOAD_REGISTER_IMM(4);
Eric Anholtae662d32012-01-03 09:23:29 -08001925 for (i = 0; i < 4; i++) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001926 *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
1927 *cs++ = 0;
Eric Anholtae662d32012-01-03 09:23:29 -08001928 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001929 *cs++ = MI_NOOP;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001930 intel_ring_advance(rq, cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001931
1932 return 0;
1933}
1934
Chris Wilson650bc632017-06-15 09:14:33 +01001935static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001936{
Brad Volkin71745372014-12-11 12:13:12 -08001937 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001938 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001939 int err;
Brad Volkin71745372014-12-11 12:13:12 -08001940
Chris Wilson650bc632017-06-15 09:14:33 +01001941 shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool,
1942 PAGE_ALIGN(eb->batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001943 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001944 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001945
Chris Wilson2889caa2017-06-16 15:05:19 +01001946 err = intel_engine_cmd_parser(eb->engine,
Chris Wilson650bc632017-06-15 09:14:33 +01001947 eb->batch->obj,
Chris Wilson33a051a2016-07-27 09:07:26 +01001948 shadow_batch_obj,
Chris Wilson650bc632017-06-15 09:14:33 +01001949 eb->batch_start_offset,
1950 eb->batch_len,
Chris Wilson33a051a2016-07-27 09:07:26 +01001951 is_master);
Chris Wilson2889caa2017-06-16 15:05:19 +01001952 if (err) {
1953 if (err == -EACCES) /* unhandled chained batch */
Chris Wilson058d88c2016-08-15 10:49:06 +01001954 vma = NULL;
1955 else
Chris Wilson2889caa2017-06-16 15:05:19 +01001956 vma = ERR_PTR(err);
Chris Wilson058d88c2016-08-15 10:49:06 +01001957 goto out;
1958 }
Brad Volkin71745372014-12-11 12:13:12 -08001959
Chris Wilson058d88c2016-08-15 10:49:06 +01001960 vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
1961 if (IS_ERR(vma))
1962 goto out;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001963
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001964 eb->vma[eb->buffer_count] = i915_vma_get(vma);
1965 eb->flags[eb->buffer_count] =
1966 __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF;
1967 vma->exec_flags = &eb->flags[eb->buffer_count];
1968 eb->buffer_count++;
Brad Volkin71745372014-12-11 12:13:12 -08001969
Chris Wilson058d88c2016-08-15 10:49:06 +01001970out:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001971 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson058d88c2016-08-15 10:49:06 +01001972 return vma;
Brad Volkin71745372014-12-11 12:13:12 -08001973}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001974
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001975static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00001976add_to_client(struct i915_request *rq, struct drm_file *file)
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001977{
Chris Wilsone61e0f52018-02-21 09:56:36 +00001978 rq->file_priv = file->driver_priv;
1979 list_add_tail(&rq->client_link, &rq->file_priv->mm.request_list);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001980}
1981
Chris Wilson2889caa2017-06-16 15:05:19 +01001982static int eb_submit(struct i915_execbuffer *eb)
Oscar Mateo78382592014-07-03 16:28:05 +01001983{
Chris Wilson2889caa2017-06-16 15:05:19 +01001984 int err;
Oscar Mateo78382592014-07-03 16:28:05 +01001985
Chris Wilson2889caa2017-06-16 15:05:19 +01001986 err = eb_move_to_gpu(eb);
1987 if (err)
1988 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001989
Chris Wilson650bc632017-06-15 09:14:33 +01001990 if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001991 err = i915_reset_gen7_sol_offsets(eb->request);
1992 if (err)
1993 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001994 }
1995
Chris Wilson2889caa2017-06-16 15:05:19 +01001996 err = eb->engine->emit_bb_start(eb->request,
Chris Wilson650bc632017-06-15 09:14:33 +01001997 eb->batch->node.start +
1998 eb->batch_start_offset,
1999 eb->batch_len,
Chris Wilson2889caa2017-06-16 15:05:19 +01002000 eb->batch_flags);
2001 if (err)
2002 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01002003
Chris Wilson2f5945b2015-10-06 11:39:55 +01002004 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01002005}
2006
Chris Wilson204bcfe2018-02-08 11:39:17 +00002007/*
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002008 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01002009 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002010 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002011static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01002012gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
2013 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002014{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002015 struct drm_i915_file_private *file_priv = file->driver_priv;
2016
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002017 /* Check whether the file_priv has already selected one ring. */
Joonas Lahtinen6f633402016-09-01 14:58:21 +03002018 if ((int)file_priv->bsd_engine < 0)
2019 file_priv->bsd_engine = atomic_fetch_xor(1,
2020 &dev_priv->mm.bsd_engine_dispatch_index);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002021
Chris Wilsonc80ff162016-07-27 09:07:27 +01002022 return file_priv->bsd_engine;
Chris Wilsond23db882014-05-23 08:48:08 +02002023}
2024
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002025#define I915_USER_RINGS (4)
2026
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002027static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002028 [I915_EXEC_DEFAULT] = RCS,
2029 [I915_EXEC_RENDER] = RCS,
2030 [I915_EXEC_BLT] = BCS,
2031 [I915_EXEC_BSD] = VCS,
2032 [I915_EXEC_VEBOX] = VECS
2033};
2034
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002035static struct intel_engine_cs *
2036eb_select_engine(struct drm_i915_private *dev_priv,
2037 struct drm_file *file,
2038 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002039{
2040 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002041 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002042
2043 if (user_ring_id > I915_USER_RINGS) {
2044 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002045 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002046 }
2047
2048 if ((user_ring_id != I915_EXEC_BSD) &&
2049 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
2050 DRM_DEBUG("execbuf with non bsd ring but with invalid "
2051 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002052 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002053 }
2054
2055 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
2056 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
2057
2058 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01002059 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002060 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
2061 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00002062 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002063 bsd_idx--;
2064 } else {
2065 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
2066 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002067 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002068 }
2069
Akash Goel3b3f1652016-10-13 22:44:48 +05302070 engine = dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002071 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +05302072 engine = dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002073 }
2074
Akash Goel3b3f1652016-10-13 22:44:48 +05302075 if (!engine) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002076 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002077 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002078 }
2079
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002080 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002081}
2082
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002083static void
2084__free_fence_array(struct drm_syncobj **fences, unsigned int n)
2085{
2086 while (n--)
2087 drm_syncobj_put(ptr_mask_bits(fences[n], 2));
2088 kvfree(fences);
2089}
2090
2091static struct drm_syncobj **
2092get_fence_array(struct drm_i915_gem_execbuffer2 *args,
2093 struct drm_file *file)
2094{
Chris Wilsond710fc12017-11-16 10:50:59 +00002095 const unsigned long nfences = args->num_cliprects;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002096 struct drm_i915_gem_exec_fence __user *user;
2097 struct drm_syncobj **fences;
Chris Wilsond710fc12017-11-16 10:50:59 +00002098 unsigned long n;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002099 int err;
2100
2101 if (!(args->flags & I915_EXEC_FENCE_ARRAY))
2102 return NULL;
2103
Chris Wilsond710fc12017-11-16 10:50:59 +00002104 /* Check multiplication overflow for access_ok() and kvmalloc_array() */
2105 BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long));
2106 if (nfences > min_t(unsigned long,
2107 ULONG_MAX / sizeof(*user),
2108 SIZE_MAX / sizeof(*fences)))
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002109 return ERR_PTR(-EINVAL);
2110
2111 user = u64_to_user_ptr(args->cliprects_ptr);
Chris Wilsond710fc12017-11-16 10:50:59 +00002112 if (!access_ok(VERIFY_READ, user, nfences * sizeof(*user)))
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002113 return ERR_PTR(-EFAULT);
2114
Chris Wilsond710fc12017-11-16 10:50:59 +00002115 fences = kvmalloc_array(nfences, sizeof(*fences),
Michal Hocko0ee931c2017-09-13 16:28:29 -07002116 __GFP_NOWARN | GFP_KERNEL);
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002117 if (!fences)
2118 return ERR_PTR(-ENOMEM);
2119
2120 for (n = 0; n < nfences; n++) {
2121 struct drm_i915_gem_exec_fence fence;
2122 struct drm_syncobj *syncobj;
2123
2124 if (__copy_from_user(&fence, user++, sizeof(fence))) {
2125 err = -EFAULT;
2126 goto err;
2127 }
2128
Tvrtko Ursulinebcaa1f2017-10-31 10:23:25 +00002129 if (fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS) {
2130 err = -EINVAL;
2131 goto err;
2132 }
2133
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002134 syncobj = drm_syncobj_find(file, fence.handle);
2135 if (!syncobj) {
2136 DRM_DEBUG("Invalid syncobj handle provided\n");
2137 err = -ENOENT;
2138 goto err;
2139 }
2140
Tvrtko Ursulinebcaa1f2017-10-31 10:23:25 +00002141 BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) &
2142 ~__I915_EXEC_FENCE_UNKNOWN_FLAGS);
2143
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002144 fences[n] = ptr_pack_bits(syncobj, fence.flags, 2);
2145 }
2146
2147 return fences;
2148
2149err:
2150 __free_fence_array(fences, n);
2151 return ERR_PTR(err);
2152}
2153
2154static void
2155put_fence_array(struct drm_i915_gem_execbuffer2 *args,
2156 struct drm_syncobj **fences)
2157{
2158 if (fences)
2159 __free_fence_array(fences, args->num_cliprects);
2160}
2161
2162static int
2163await_fence_array(struct i915_execbuffer *eb,
2164 struct drm_syncobj **fences)
2165{
2166 const unsigned int nfences = eb->args->num_cliprects;
2167 unsigned int n;
2168 int err;
2169
2170 for (n = 0; n < nfences; n++) {
2171 struct drm_syncobj *syncobj;
2172 struct dma_fence *fence;
2173 unsigned int flags;
2174
2175 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2176 if (!(flags & I915_EXEC_FENCE_WAIT))
2177 continue;
2178
Jason Ekstrandafca4212017-08-25 10:52:21 -07002179 fence = drm_syncobj_fence_get(syncobj);
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002180 if (!fence)
2181 return -EINVAL;
2182
Chris Wilsone61e0f52018-02-21 09:56:36 +00002183 err = i915_request_await_dma_fence(eb->request, fence);
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002184 dma_fence_put(fence);
2185 if (err < 0)
2186 return err;
2187 }
2188
2189 return 0;
2190}
2191
2192static void
2193signal_fence_array(struct i915_execbuffer *eb,
2194 struct drm_syncobj **fences)
2195{
2196 const unsigned int nfences = eb->args->num_cliprects;
2197 struct dma_fence * const fence = &eb->request->fence;
2198 unsigned int n;
2199
2200 for (n = 0; n < nfences; n++) {
2201 struct drm_syncobj *syncobj;
2202 unsigned int flags;
2203
2204 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2205 if (!(flags & I915_EXEC_FENCE_SIGNAL))
2206 continue;
2207
2208 drm_syncobj_replace_fence(syncobj, fence);
2209 }
2210}
2211
Eric Anholtae662d32012-01-03 09:23:29 -08002212static int
Chris Wilson650bc632017-06-15 09:14:33 +01002213i915_gem_do_execbuffer(struct drm_device *dev,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002214 struct drm_file *file,
2215 struct drm_i915_gem_execbuffer2 *args,
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002216 struct drm_i915_gem_exec_object2 *exec,
2217 struct drm_syncobj **fences)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002218{
Chris Wilson650bc632017-06-15 09:14:33 +01002219 struct i915_execbuffer eb;
Chris Wilsonfec04452017-01-27 09:40:08 +00002220 struct dma_fence *in_fence = NULL;
2221 struct sync_file *out_fence = NULL;
2222 int out_fence_fd = -1;
Chris Wilson2889caa2017-06-16 15:05:19 +01002223 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002224
Chris Wilson74c1c692017-09-21 12:01:35 +01002225 BUILD_BUG_ON(__EXEC_INTERNAL_FLAGS & ~__I915_EXEC_ILLEGAL_FLAGS);
Chris Wilson2889caa2017-06-16 15:05:19 +01002226 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
2227 ~__EXEC_OBJECT_UNKNOWN_FLAGS);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002228
Chris Wilson650bc632017-06-15 09:14:33 +01002229 eb.i915 = to_i915(dev);
2230 eb.file = file;
2231 eb.args = args;
Chris Wilson7dd4f672017-06-16 15:05:24 +01002232 if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC))
Chris Wilson2889caa2017-06-16 15:05:19 +01002233 args->flags |= __EXEC_HAS_RELOC;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002234
Chris Wilson650bc632017-06-15 09:14:33 +01002235 eb.exec = exec;
Chris Wilson170fa292017-08-16 09:52:07 +01002236 eb.vma = (struct i915_vma **)(exec + args->buffer_count + 1);
2237 eb.vma[0] = NULL;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002238 eb.flags = (unsigned int *)(eb.vma + args->buffer_count + 1);
2239
Chris Wilson2889caa2017-06-16 15:05:19 +01002240 eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
2241 if (USES_FULL_PPGTT(eb.i915))
2242 eb.invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson650bc632017-06-15 09:14:33 +01002243 reloc_cache_init(&eb.reloc_cache, eb.i915);
2244
Chris Wilson2889caa2017-06-16 15:05:19 +01002245 eb.buffer_count = args->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +01002246 eb.batch_start_offset = args->batch_start_offset;
2247 eb.batch_len = args->batch_len;
2248
Chris Wilson2889caa2017-06-16 15:05:19 +01002249 eb.batch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002250 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02002251 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002252 return -EPERM;
2253
Chris Wilson2889caa2017-06-16 15:05:19 +01002254 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002255 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01002256 if (args->flags & I915_EXEC_IS_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +01002257 eb.batch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002258
Chris Wilson650bc632017-06-15 09:14:33 +01002259 eb.engine = eb_select_engine(eb.i915, file, args);
2260 if (!eb.engine)
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002261 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002262
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002263 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
Chris Wilson650bc632017-06-15 09:14:33 +01002264 if (!HAS_RESOURCE_STREAMER(eb.i915)) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002265 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
2266 return -EINVAL;
2267 }
Chris Wilson650bc632017-06-15 09:14:33 +01002268 if (eb.engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002269 DRM_DEBUG("RS is not available on %s\n",
Chris Wilson650bc632017-06-15 09:14:33 +01002270 eb.engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002271 return -EINVAL;
2272 }
2273
Chris Wilson2889caa2017-06-16 15:05:19 +01002274 eb.batch_flags |= I915_DISPATCH_RS;
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002275 }
2276
Chris Wilsonfec04452017-01-27 09:40:08 +00002277 if (args->flags & I915_EXEC_FENCE_IN) {
2278 in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002279 if (!in_fence)
2280 return -EINVAL;
Chris Wilsonfec04452017-01-27 09:40:08 +00002281 }
2282
2283 if (args->flags & I915_EXEC_FENCE_OUT) {
2284 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
2285 if (out_fence_fd < 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002286 err = out_fence_fd;
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002287 goto err_in_fence;
Chris Wilsonfec04452017-01-27 09:40:08 +00002288 }
2289 }
2290
Chris Wilson4d470f72017-06-29 16:04:25 +01002291 err = eb_create(&eb);
2292 if (err)
2293 goto err_out_fence;
2294
2295 GEM_BUG_ON(!eb.lut_size);
Chris Wilson2889caa2017-06-16 15:05:19 +01002296
Chris Wilson1acfc102017-06-20 12:05:47 +01002297 err = eb_select_context(&eb);
2298 if (unlikely(err))
2299 goto err_destroy;
2300
Chris Wilson2889caa2017-06-16 15:05:19 +01002301 /*
2302 * Take a local wakeref for preparing to dispatch the execbuf as
Chris Wilson67d97da2016-07-04 08:08:31 +01002303 * we expect to access the hardware fairly frequently in the
2304 * process. Upon first dispatch, we acquire another prolonged
2305 * wakeref that we hold until the GPU has been idle for at least
2306 * 100ms.
2307 */
Chris Wilson650bc632017-06-15 09:14:33 +01002308 intel_runtime_pm_get(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002309
Chris Wilson2889caa2017-06-16 15:05:19 +01002310 err = i915_mutex_lock_interruptible(dev);
2311 if (err)
2312 goto err_rpm;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002313
Chris Wilson2889caa2017-06-16 15:05:19 +01002314 err = eb_relocate(&eb);
Chris Wilson1f727d92017-07-21 15:50:36 +01002315 if (err) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002316 /*
2317 * If the user expects the execobject.offset and
2318 * reloc.presumed_offset to be an exact match,
2319 * as for using NO_RELOC, then we cannot update
2320 * the execobject.offset until we have completed
2321 * relocation.
2322 */
2323 args->flags &= ~__EXEC_HAS_RELOC;
Chris Wilson2889caa2017-06-16 15:05:19 +01002324 goto err_vma;
Chris Wilson1f727d92017-07-21 15:50:36 +01002325 }
Ben Widawsky41bde552013-12-06 14:11:21 -08002326
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002327 if (unlikely(*eb.batch->exec_flags & EXEC_OBJECT_WRITE)) {
Daniel Vetterff240192012-01-31 21:08:14 +01002328 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002329 err = -EINVAL;
2330 goto err_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002331 }
Chris Wilson650bc632017-06-15 09:14:33 +01002332 if (eb.batch_start_offset > eb.batch->size ||
2333 eb.batch_len > eb.batch->size - eb.batch_start_offset) {
Chris Wilson0b537272016-08-18 17:17:12 +01002334 DRM_DEBUG("Attempting to use out-of-bounds batch\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002335 err = -EINVAL;
2336 goto err_vma;
Chris Wilson0b537272016-08-18 17:17:12 +01002337 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002338
Chris Wilson3dbf26e2017-08-26 14:56:20 +01002339 if (eb_use_cmdparser(&eb)) {
Chris Wilson59bfa122016-08-04 16:32:31 +01002340 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002341
Chris Wilson650bc632017-06-15 09:14:33 +01002342 vma = eb_parse(&eb, drm_is_current_master(file));
Chris Wilson59bfa122016-08-04 16:32:31 +01002343 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002344 err = PTR_ERR(vma);
2345 goto err_vma;
Brad Volkin78a42372014-12-11 12:13:09 -08002346 }
Chris Wilson17cabf52015-01-14 11:20:57 +00002347
Chris Wilson59bfa122016-08-04 16:32:31 +01002348 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002349 /*
2350 * Batch parsed and accepted:
2351 *
2352 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
2353 * bit from MI_BATCH_BUFFER_START commands issued in
2354 * the dispatch_execbuffer implementations. We
2355 * specifically don't want that set on batches the
2356 * command parser has accepted.
2357 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002358 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilson650bc632017-06-15 09:14:33 +01002359 eb.batch_start_offset = 0;
2360 eb.batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002361 }
Brad Volkin351e3db2014-02-18 10:15:46 -08002362 }
2363
Chris Wilson650bc632017-06-15 09:14:33 +01002364 if (eb.batch_len == 0)
2365 eb.batch_len = eb.batch->size - eb.batch_start_offset;
Brad Volkin78a42372014-12-11 12:13:09 -08002366
Chris Wilson2889caa2017-06-16 15:05:19 +01002367 /*
2368 * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002369 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07002370 * hsw should have this fixed, but bdw mucks it up again. */
Chris Wilson2889caa2017-06-16 15:05:19 +01002371 if (eb.batch_flags & I915_DISPATCH_SECURE) {
Chris Wilson058d88c2016-08-15 10:49:06 +01002372 struct i915_vma *vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002373
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002374 /*
2375 * So on first glance it looks freaky that we pin the batch here
2376 * outside of the reservation loop. But:
2377 * - The batch is already pinned into the relevant ppgtt, so we
2378 * already have the backing storage fully allocated.
2379 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01002380 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002381 * fitting due to fragmentation.
2382 * So this is actually safe.
2383 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002384 vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0);
Chris Wilson058d88c2016-08-15 10:49:06 +01002385 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002386 err = PTR_ERR(vma);
2387 goto err_vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01002388 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002389
Chris Wilson650bc632017-06-15 09:14:33 +01002390 eb.batch = vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002391 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002392
Chris Wilson7dd4f672017-06-16 15:05:24 +01002393 /* All GPU relocation batches must be submitted prior to the user rq */
2394 GEM_BUG_ON(eb.reloc_cache.rq);
2395
John Harrison0c8dac82015-05-29 17:43:25 +01002396 /* Allocate a request for this batch buffer nice and early. */
Chris Wilsone61e0f52018-02-21 09:56:36 +00002397 eb.request = i915_request_alloc(eb.engine, eb.ctx);
Chris Wilson650bc632017-06-15 09:14:33 +01002398 if (IS_ERR(eb.request)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002399 err = PTR_ERR(eb.request);
John Harrison0c8dac82015-05-29 17:43:25 +01002400 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00002401 }
John Harrison0c8dac82015-05-29 17:43:25 +01002402
Chris Wilsonfec04452017-01-27 09:40:08 +00002403 if (in_fence) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002404 err = i915_request_await_dma_fence(eb.request, in_fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01002405 if (err < 0)
Chris Wilsonfec04452017-01-27 09:40:08 +00002406 goto err_request;
2407 }
2408
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002409 if (fences) {
2410 err = await_fence_array(&eb, fences);
2411 if (err)
2412 goto err_request;
2413 }
2414
Chris Wilsonfec04452017-01-27 09:40:08 +00002415 if (out_fence_fd != -1) {
Chris Wilson650bc632017-06-15 09:14:33 +01002416 out_fence = sync_file_create(&eb.request->fence);
Chris Wilsonfec04452017-01-27 09:40:08 +00002417 if (!out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002418 err = -ENOMEM;
Chris Wilsonfec04452017-01-27 09:40:08 +00002419 goto err_request;
2420 }
2421 }
2422
Chris Wilson2889caa2017-06-16 15:05:19 +01002423 /*
2424 * Whilst this request exists, batch_obj will be on the
Chris Wilson17f298cf2016-08-10 13:41:46 +01002425 * active_list, and so will hold the active reference. Only when this
2426 * request is retired will the the batch_obj be moved onto the
2427 * inactive_list and lose its active reference. Hence we do not need
2428 * to explicitly hold another reference here.
2429 */
Chris Wilson650bc632017-06-15 09:14:33 +01002430 eb.request->batch = eb.batch;
Chris Wilson17f298cf2016-08-10 13:41:46 +01002431
Chris Wilsone61e0f52018-02-21 09:56:36 +00002432 trace_i915_request_queue(eb.request, eb.batch_flags);
Chris Wilson2889caa2017-06-16 15:05:19 +01002433 err = eb_submit(&eb);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01002434err_request:
Chris Wilsone61e0f52018-02-21 09:56:36 +00002435 __i915_request_add(eb.request, err == 0);
Chris Wilson650bc632017-06-15 09:14:33 +01002436 add_to_client(eb.request, file);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002437
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002438 if (fences)
2439 signal_fence_array(&eb, fences);
2440
Chris Wilsonfec04452017-01-27 09:40:08 +00002441 if (out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002442 if (err == 0) {
Chris Wilsonfec04452017-01-27 09:40:08 +00002443 fd_install(out_fence_fd, out_fence->file);
Daniele Ceraolo Spuriob6a88e42018-02-14 11:18:25 -08002444 args->rsvd2 &= GENMASK_ULL(31, 0); /* keep in-fence */
Chris Wilsonfec04452017-01-27 09:40:08 +00002445 args->rsvd2 |= (u64)out_fence_fd << 32;
2446 out_fence_fd = -1;
2447 } else {
2448 fput(out_fence->file);
2449 }
2450 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002451
John Harrison0c8dac82015-05-29 17:43:25 +01002452err_batch_unpin:
Chris Wilson2889caa2017-06-16 15:05:19 +01002453 if (eb.batch_flags & I915_DISPATCH_SECURE)
Chris Wilson650bc632017-06-15 09:14:33 +01002454 i915_vma_unpin(eb.batch);
Chris Wilson2889caa2017-06-16 15:05:19 +01002455err_vma:
2456 if (eb.exec)
2457 eb_release_vmas(&eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002458 mutex_unlock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01002459err_rpm:
Chris Wilson650bc632017-06-15 09:14:33 +01002460 intel_runtime_pm_put(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002461 i915_gem_context_put(eb.ctx);
2462err_destroy:
Chris Wilson2889caa2017-06-16 15:05:19 +01002463 eb_destroy(&eb);
Chris Wilson4d470f72017-06-29 16:04:25 +01002464err_out_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002465 if (out_fence_fd != -1)
2466 put_unused_fd(out_fence_fd);
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002467err_in_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002468 dma_fence_put(in_fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01002469 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002470}
2471
Chris Wilsond710fc12017-11-16 10:50:59 +00002472static size_t eb_element_size(void)
2473{
2474 return (sizeof(struct drm_i915_gem_exec_object2) +
2475 sizeof(struct i915_vma *) +
2476 sizeof(unsigned int));
2477}
2478
2479static bool check_buffer_count(size_t count)
2480{
2481 const size_t sz = eb_element_size();
2482
2483 /*
2484 * When using LUT_HANDLE, we impose a limit of INT_MAX for the lookup
2485 * array size (see eb_create()). Otherwise, we can accept an array as
2486 * large as can be addressed (though use large arrays at your peril)!
2487 */
2488
2489 return !(count < 1 || count > INT_MAX || count > SIZE_MAX / sz - 1);
2490}
2491
Chris Wilson54cf91d2010-11-25 18:00:26 +00002492/*
2493 * Legacy execbuffer just creates an exec2 list from the original exec object
2494 * list array and passes it to the real function.
2495 */
2496int
Ville Syrjälä6a20fe72018-02-07 18:48:41 +02002497i915_gem_execbuffer_ioctl(struct drm_device *dev, void *data,
2498 struct drm_file *file)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002499{
2500 struct drm_i915_gem_execbuffer *args = data;
2501 struct drm_i915_gem_execbuffer2 exec2;
2502 struct drm_i915_gem_exec_object *exec_list = NULL;
2503 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
Chris Wilsond710fc12017-11-16 10:50:59 +00002504 const size_t count = args->buffer_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01002505 unsigned int i;
2506 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002507
Chris Wilsond710fc12017-11-16 10:50:59 +00002508 if (!check_buffer_count(count)) {
2509 DRM_DEBUG("execbuf2 with %zd buffers\n", count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002510 return -EINVAL;
2511 }
2512
Chris Wilson2889caa2017-06-16 15:05:19 +01002513 exec2.buffers_ptr = args->buffers_ptr;
2514 exec2.buffer_count = args->buffer_count;
2515 exec2.batch_start_offset = args->batch_start_offset;
2516 exec2.batch_len = args->batch_len;
2517 exec2.DR1 = args->DR1;
2518 exec2.DR4 = args->DR4;
2519 exec2.num_cliprects = args->num_cliprects;
2520 exec2.cliprects_ptr = args->cliprects_ptr;
2521 exec2.flags = I915_EXEC_RENDER;
2522 i915_execbuffer2_set_context_id(exec2, 0);
2523
2524 if (!i915_gem_check_execbuffer(&exec2))
2525 return -EINVAL;
2526
Chris Wilson54cf91d2010-11-25 18:00:26 +00002527 /* Copy in the exec list from userland */
Chris Wilsond710fc12017-11-16 10:50:59 +00002528 exec_list = kvmalloc_array(count, sizeof(*exec_list),
Michal Hocko0ee931c2017-09-13 16:28:29 -07002529 __GFP_NOWARN | GFP_KERNEL);
Chris Wilsond710fc12017-11-16 10:50:59 +00002530 exec2_list = kvmalloc_array(count + 1, eb_element_size(),
Michal Hocko0ee931c2017-09-13 16:28:29 -07002531 __GFP_NOWARN | GFP_KERNEL);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002532 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002533 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002534 args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002535 kvfree(exec_list);
2536 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002537 return -ENOMEM;
2538 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002539 err = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002540 u64_to_user_ptr(args->buffers_ptr),
Chris Wilsond710fc12017-11-16 10:50:59 +00002541 sizeof(*exec_list) * count);
Chris Wilson2889caa2017-06-16 15:05:19 +01002542 if (err) {
Daniel Vetterff240192012-01-31 21:08:14 +01002543 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson2889caa2017-06-16 15:05:19 +01002544 args->buffer_count, err);
Michal Hocko20981052017-05-17 14:23:12 +02002545 kvfree(exec_list);
2546 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002547 return -EFAULT;
2548 }
2549
2550 for (i = 0; i < args->buffer_count; i++) {
2551 exec2_list[i].handle = exec_list[i].handle;
2552 exec2_list[i].relocation_count = exec_list[i].relocation_count;
2553 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
2554 exec2_list[i].alignment = exec_list[i].alignment;
2555 exec2_list[i].offset = exec_list[i].offset;
Tvrtko Ursulinf0836b72016-11-16 08:55:32 +00002556 if (INTEL_GEN(to_i915(dev)) < 4)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002557 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
2558 else
2559 exec2_list[i].flags = 0;
2560 }
2561
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002562 err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list, NULL);
Chris Wilson2889caa2017-06-16 15:05:19 +01002563 if (exec2.flags & __EXEC_HAS_RELOC) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002564 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002565 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002566
Chris Wilson54cf91d2010-11-25 18:00:26 +00002567 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002568 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002569 if (!(exec2_list[i].offset & UPDATE))
2570 continue;
2571
Michał Winiarski934acce2015-12-29 18:24:52 +01002572 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002573 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2574 exec2_list[i].offset &= PIN_OFFSET_MASK;
2575 if (__copy_to_user(&user_exec_list[i].offset,
2576 &exec2_list[i].offset,
2577 sizeof(user_exec_list[i].offset)))
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002578 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002579 }
2580 }
2581
Michal Hocko20981052017-05-17 14:23:12 +02002582 kvfree(exec_list);
2583 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002584 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002585}
2586
2587int
Ville Syrjälä6a20fe72018-02-07 18:48:41 +02002588i915_gem_execbuffer2_ioctl(struct drm_device *dev, void *data,
2589 struct drm_file *file)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002590{
2591 struct drm_i915_gem_execbuffer2 *args = data;
Chris Wilson2889caa2017-06-16 15:05:19 +01002592 struct drm_i915_gem_exec_object2 *exec2_list;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002593 struct drm_syncobj **fences = NULL;
Chris Wilsond710fc12017-11-16 10:50:59 +00002594 const size_t count = args->buffer_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01002595 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002596
Chris Wilsond710fc12017-11-16 10:50:59 +00002597 if (!check_buffer_count(count)) {
2598 DRM_DEBUG("execbuf2 with %zd buffers\n", count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002599 return -EINVAL;
2600 }
2601
Chris Wilson2889caa2017-06-16 15:05:19 +01002602 if (!i915_gem_check_execbuffer(args))
2603 return -EINVAL;
2604
2605 /* Allocate an extra slot for use by the command parser */
Chris Wilsond710fc12017-11-16 10:50:59 +00002606 exec2_list = kvmalloc_array(count + 1, eb_element_size(),
Michal Hocko0ee931c2017-09-13 16:28:29 -07002607 __GFP_NOWARN | GFP_KERNEL);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002608 if (exec2_list == NULL) {
Chris Wilsond710fc12017-11-16 10:50:59 +00002609 DRM_DEBUG("Failed to allocate exec list for %zd buffers\n",
2610 count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002611 return -ENOMEM;
2612 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002613 if (copy_from_user(exec2_list,
2614 u64_to_user_ptr(args->buffers_ptr),
Chris Wilsond710fc12017-11-16 10:50:59 +00002615 sizeof(*exec2_list) * count)) {
2616 DRM_DEBUG("copy %zd exec entries failed\n", count);
Michal Hocko20981052017-05-17 14:23:12 +02002617 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002618 return -EFAULT;
2619 }
2620
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002621 if (args->flags & I915_EXEC_FENCE_ARRAY) {
2622 fences = get_fence_array(args, file);
2623 if (IS_ERR(fences)) {
2624 kvfree(exec2_list);
2625 return PTR_ERR(fences);
2626 }
2627 }
2628
2629 err = i915_gem_do_execbuffer(dev, file, args, exec2_list, fences);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002630
Chris Wilson2889caa2017-06-16 15:05:19 +01002631 /*
2632 * Now that we have begun execution of the batchbuffer, we ignore
2633 * any new error after this point. Also given that we have already
2634 * updated the associated relocations, we try to write out the current
2635 * object locations irrespective of any error.
2636 */
2637 if (args->flags & __EXEC_HAS_RELOC) {
2638 struct drm_i915_gem_exec_object2 __user *user_exec_list =
2639 u64_to_user_ptr(args->buffers_ptr);
2640 unsigned int i;
2641
2642 /* Copy the new buffer offsets back to the user's exec list. */
2643 user_access_begin();
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002644 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002645 if (!(exec2_list[i].offset & UPDATE))
2646 continue;
2647
Michał Winiarski934acce2015-12-29 18:24:52 +01002648 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002649 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2650 unsafe_put_user(exec2_list[i].offset,
2651 &user_exec_list[i].offset,
2652 end_user);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002653 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002654end_user:
2655 user_access_end();
Chris Wilson54cf91d2010-11-25 18:00:26 +00002656 }
2657
Chris Wilson2889caa2017-06-16 15:05:19 +01002658 args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002659 put_fence_array(args, fences);
Michal Hocko20981052017-05-17 14:23:12 +02002660 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002661 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002662}