blob: 95e461259d242d69f97ef30b432302ea0bb762b0 [file] [log] [blame]
Chris Wilson54cf91d2010-11-25 18:00:26 +00001/*
2 * Copyright © 2008,2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
26 *
27 */
28
Eugeni Dodonovf45b5552011-12-09 17:16:37 -080029#include <linux/dma_remapping.h>
Chris Wilsonad778f82016-08-04 16:32:42 +010030#include <linux/reservation.h>
Chris Wilsonfec04452017-01-27 09:40:08 +000031#include <linux/sync_file.h>
David Hildenbrand32d82062015-05-11 17:52:12 +020032#include <linux/uaccess.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000033
Chris Wilson54cf91d2010-11-25 18:00:26 +000034#include <drm/drmP.h>
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +010035#include <drm/drm_syncobj.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000036#include <drm/i915_drm.h>
Chris Wilsonad778f82016-08-04 16:32:42 +010037
Chris Wilson54cf91d2010-11-25 18:00:26 +000038#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000039#include "i915_gem_clflush.h"
Chris Wilson54cf91d2010-11-25 18:00:26 +000040#include "i915_trace.h"
41#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010042#include "intel_frontbuffer.h"
Chris Wilson54cf91d2010-11-25 18:00:26 +000043
Chris Wilson7dd4f672017-06-16 15:05:24 +010044enum {
45 FORCE_CPU_RELOC = 1,
46 FORCE_GTT_RELOC,
47 FORCE_GPU_RELOC,
48#define DBG_FORCE_RELOC 0 /* choose one of the above! */
49};
Chris Wilsond50415c2016-08-18 17:16:52 +010050
Chris Wilsondade2a62017-06-16 15:05:20 +010051#define __EXEC_OBJECT_HAS_REF BIT(31)
52#define __EXEC_OBJECT_HAS_PIN BIT(30)
53#define __EXEC_OBJECT_HAS_FENCE BIT(29)
54#define __EXEC_OBJECT_NEEDS_MAP BIT(28)
55#define __EXEC_OBJECT_NEEDS_BIAS BIT(27)
56#define __EXEC_OBJECT_INTERNAL_FLAGS (~0u << 27) /* all of the above */
Chris Wilson2889caa2017-06-16 15:05:19 +010057#define __EXEC_OBJECT_RESERVED (__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_FENCE)
58
59#define __EXEC_HAS_RELOC BIT(31)
60#define __EXEC_VALIDATED BIT(30)
61#define UPDATE PIN_OFFSET_FIXED
Chris Wilsond23db882014-05-23 08:48:08 +020062
63#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000064
Chris Wilson650bc632017-06-15 09:14:33 +010065#define __I915_EXEC_ILLEGAL_FLAGS \
66 (__I915_EXEC_UNKNOWN_FLAGS | I915_EXEC_CONSTANTS_MASK)
Chris Wilson5b043f42016-08-02 22:50:38 +010067
Chris Wilson2889caa2017-06-16 15:05:19 +010068/**
69 * DOC: User command execution
70 *
71 * Userspace submits commands to be executed on the GPU as an instruction
72 * stream within a GEM object we call a batchbuffer. This instructions may
73 * refer to other GEM objects containing auxiliary state such as kernels,
74 * samplers, render targets and even secondary batchbuffers. Userspace does
75 * not know where in the GPU memory these objects reside and so before the
76 * batchbuffer is passed to the GPU for execution, those addresses in the
77 * batchbuffer and auxiliary objects are updated. This is known as relocation,
78 * or patching. To try and avoid having to relocate each object on the next
79 * execution, userspace is told the location of those objects in this pass,
80 * but this remains just a hint as the kernel may choose a new location for
81 * any object in the future.
82 *
83 * Processing an execbuf ioctl is conceptually split up into a few phases.
84 *
85 * 1. Validation - Ensure all the pointers, handles and flags are valid.
86 * 2. Reservation - Assign GPU address space for every object
87 * 3. Relocation - Update any addresses to point to the final locations
88 * 4. Serialisation - Order the request with respect to its dependencies
89 * 5. Construction - Construct a request to execute the batchbuffer
90 * 6. Submission (at some point in the future execution)
91 *
92 * Reserving resources for the execbuf is the most complicated phase. We
93 * neither want to have to migrate the object in the address space, nor do
94 * we want to have to update any relocations pointing to this object. Ideally,
95 * we want to leave the object where it is and for all the existing relocations
96 * to match. If the object is given a new address, or if userspace thinks the
97 * object is elsewhere, we have to parse all the relocation entries and update
98 * the addresses. Userspace can set the I915_EXEC_NORELOC flag to hint that
99 * all the target addresses in all of its objects match the value in the
100 * relocation entries and that they all match the presumed offsets given by the
101 * list of execbuffer objects. Using this knowledge, we know that if we haven't
102 * moved any buffers, all the relocation entries are valid and we can skip
103 * the update. (If userspace is wrong, the likely outcome is an impromptu GPU
104 * hang.) The requirement for using I915_EXEC_NO_RELOC are:
105 *
106 * The addresses written in the objects must match the corresponding
107 * reloc.presumed_offset which in turn must match the corresponding
108 * execobject.offset.
109 *
110 * Any render targets written to in the batch must be flagged with
111 * EXEC_OBJECT_WRITE.
112 *
113 * To avoid stalling, execobject.offset should match the current
114 * address of that object within the active context.
115 *
116 * The reservation is done is multiple phases. First we try and keep any
117 * object already bound in its current location - so as long as meets the
118 * constraints imposed by the new execbuffer. Any object left unbound after the
119 * first pass is then fitted into any available idle space. If an object does
120 * not fit, all objects are removed from the reservation and the process rerun
121 * after sorting the objects into a priority order (more difficult to fit
122 * objects are tried first). Failing that, the entire VM is cleared and we try
123 * to fit the execbuf once last time before concluding that it simply will not
124 * fit.
125 *
126 * A small complication to all of this is that we allow userspace not only to
127 * specify an alignment and a size for the object in the address space, but
128 * we also allow userspace to specify the exact offset. This objects are
129 * simpler to place (the location is known a priori) all we have to do is make
130 * sure the space is available.
131 *
132 * Once all the objects are in place, patching up the buried pointers to point
133 * to the final locations is a fairly simple job of walking over the relocation
134 * entry arrays, looking up the right address and rewriting the value into
135 * the object. Simple! ... The relocation entries are stored in user memory
136 * and so to access them we have to copy them into a local buffer. That copy
137 * has to avoid taking any pagefaults as they may lead back to a GEM object
138 * requiring the struct_mutex (i.e. recursive deadlock). So once again we split
139 * the relocation into multiple passes. First we try to do everything within an
140 * atomic context (avoid the pagefaults) which requires that we never wait. If
141 * we detect that we may wait, or if we need to fault, then we have to fallback
142 * to a slower path. The slowpath has to drop the mutex. (Can you hear alarm
143 * bells yet?) Dropping the mutex means that we lose all the state we have
144 * built up so far for the execbuf and we must reset any global data. However,
145 * we do leave the objects pinned in their final locations - which is a
146 * potential issue for concurrent execbufs. Once we have left the mutex, we can
147 * allocate and copy all the relocation entries into a large array at our
148 * leisure, reacquire the mutex, reclaim all the objects and other state and
149 * then proceed to update any incorrect addresses with the objects.
150 *
151 * As we process the relocation entries, we maintain a record of whether the
152 * object is being written to. Using NORELOC, we expect userspace to provide
153 * this information instead. We also check whether we can skip the relocation
154 * by comparing the expected value inside the relocation entry with the target's
155 * final address. If they differ, we have to map the current object and rewrite
156 * the 4 or 8 byte pointer within.
157 *
158 * Serialising an execbuf is quite simple according to the rules of the GEM
159 * ABI. Execution within each context is ordered by the order of submission.
160 * Writes to any GEM object are in order of submission and are exclusive. Reads
161 * from a GEM object are unordered with respect to other reads, but ordered by
162 * writes. A write submitted after a read cannot occur before the read, and
163 * similarly any read submitted after a write cannot occur before the write.
164 * Writes are ordered between engines such that only one write occurs at any
165 * time (completing any reads beforehand) - using semaphores where available
166 * and CPU serialisation otherwise. Other GEM access obey the same rules, any
167 * write (either via mmaps using set-domain, or via pwrite) must flush all GPU
168 * reads before starting, and any read (either using set-domain or pread) must
169 * flush all GPU writes before starting. (Note we only employ a barrier before,
170 * we currently rely on userspace not concurrently starting a new execution
171 * whilst reading or writing to an object. This may be an advantage or not
172 * depending on how much you trust userspace not to shoot themselves in the
173 * foot.) Serialisation may just result in the request being inserted into
174 * a DAG awaiting its turn, but most simple is to wait on the CPU until
175 * all dependencies are resolved.
176 *
177 * After all of that, is just a matter of closing the request and handing it to
178 * the hardware (well, leaving it in a queue to be executed). However, we also
179 * offer the ability for batchbuffers to be run with elevated privileges so
180 * that they access otherwise hidden registers. (Used to adjust L3 cache etc.)
181 * Before any batch is given extra privileges we first must check that it
182 * contains no nefarious instructions, we check that each instruction is from
183 * our whitelist and all registers are also from an allowed list. We first
184 * copy the user's batchbuffer to a shadow (so that the user doesn't have
185 * access to it, either by the CPU or GPU as we scan it) and then parse each
186 * instruction. If everything is ok, we set a flag telling the hardware to run
187 * the batchbuffer in trusted mode, otherwise the ioctl is rejected.
188 */
189
Chris Wilson650bc632017-06-15 09:14:33 +0100190struct i915_execbuffer {
Chris Wilson2889caa2017-06-16 15:05:19 +0100191 struct drm_i915_private *i915; /** i915 backpointer */
192 struct drm_file *file; /** per-file lookup tables and limits */
193 struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */
194 struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100195 struct i915_vma **vma;
196 unsigned int *flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100197
198 struct intel_engine_cs *engine; /** engine to queue the request to */
199 struct i915_gem_context *ctx; /** context for building the request */
200 struct i915_address_space *vm; /** GTT and vma for the request */
201
202 struct drm_i915_gem_request *request; /** our request to build */
203 struct i915_vma *batch; /** identity of the batch obj/vma */
204
205 /** actual size of execobj[] as we may extend it for the cmdparser */
206 unsigned int buffer_count;
207
208 /** list of vma not yet bound during reservation phase */
209 struct list_head unbound;
210
211 /** list of vma that have execobj.relocation_count */
212 struct list_head relocs;
213
214 /**
215 * Track the most recently used object for relocations, as we
216 * frequently have to perform multiple relocations within the same
217 * obj/page
218 */
Chris Wilson650bc632017-06-15 09:14:33 +0100219 struct reloc_cache {
Chris Wilson2889caa2017-06-16 15:05:19 +0100220 struct drm_mm_node node; /** temporary GTT binding */
221 unsigned long vaddr; /** Current kmap address */
222 unsigned long page; /** Currently mapped page index */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100223 unsigned int gen; /** Cached value of INTEL_GEN */
Chris Wilson650bc632017-06-15 09:14:33 +0100224 bool use_64bit_reloc : 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100225 bool has_llc : 1;
226 bool has_fence : 1;
227 bool needs_unfenced : 1;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100228
229 struct drm_i915_gem_request *rq;
230 u32 *rq_cmd;
231 unsigned int rq_size;
Chris Wilson650bc632017-06-15 09:14:33 +0100232 } reloc_cache;
Chris Wilson2889caa2017-06-16 15:05:19 +0100233
234 u64 invalid_flags; /** Set of execobj.flags that are invalid */
235 u32 context_flags; /** Set of execobj.flags to insert from the ctx */
236
237 u32 batch_start_offset; /** Location within object of batch */
238 u32 batch_len; /** Length of batch within object */
239 u32 batch_flags; /** Flags composed for emit_bb_start() */
240
241 /**
242 * Indicate either the size of the hastable used to resolve
243 * relocation handles, or if negative that we are using a direct
244 * index into the execobj[].
245 */
246 int lut_size;
247 struct hlist_head *buckets; /** ht for relocation handles */
Chris Wilson67731b82010-12-08 10:38:14 +0000248};
249
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100250#define exec_entry(EB, VMA) (&(EB)->exec[(VMA)->exec_flags - (EB)->flags])
Chris Wilson4ff4b442017-06-16 15:05:16 +0100251
Chris Wilson2889caa2017-06-16 15:05:19 +0100252/*
253 * Used to convert any address to canonical form.
254 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
255 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
256 * addresses to be in a canonical form:
257 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
258 * canonical form [63:48] == [47]."
259 */
260#define GEN8_HIGH_ADDRESS_BIT 47
261static inline u64 gen8_canonical_addr(u64 address)
262{
263 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
264}
265
266static inline u64 gen8_noncanonical_addr(u64 address)
267{
268 return address & GENMASK_ULL(GEN8_HIGH_ADDRESS_BIT, 0);
269}
270
Chris Wilson650bc632017-06-15 09:14:33 +0100271static int eb_create(struct i915_execbuffer *eb)
Chris Wilson67731b82010-12-08 10:38:14 +0000272{
Chris Wilson2889caa2017-06-16 15:05:19 +0100273 if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) {
274 unsigned int size = 1 + ilog2(eb->buffer_count);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000275
Chris Wilson2889caa2017-06-16 15:05:19 +0100276 /*
277 * Without a 1:1 association between relocation handles and
278 * the execobject[] index, we instead create a hashtable.
279 * We size it dynamically based on available memory, starting
280 * first with 1:1 assocative hash and scaling back until
281 * the allocation succeeds.
282 *
283 * Later on we use a positive lut_size to indicate we are
284 * using this hashtable, and a negative value to indicate a
285 * direct lookup.
286 */
Chris Wilson4ff4b442017-06-16 15:05:16 +0100287 do {
Chris Wilson4d470f72017-06-29 16:04:25 +0100288 unsigned int flags;
289
290 /* While we can still reduce the allocation size, don't
291 * raise a warning and allow the allocation to fail.
292 * On the last pass though, we want to try as hard
293 * as possible to perform the allocation and warn
294 * if it fails.
295 */
296 flags = GFP_TEMPORARY;
297 if (size > 1)
298 flags |= __GFP_NORETRY | __GFP_NOWARN;
299
Chris Wilson4ff4b442017-06-16 15:05:16 +0100300 eb->buckets = kzalloc(sizeof(struct hlist_head) << size,
Chris Wilson4d470f72017-06-29 16:04:25 +0100301 flags);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100302 if (eb->buckets)
303 break;
304 } while (--size);
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000305
Chris Wilson4d470f72017-06-29 16:04:25 +0100306 if (unlikely(!size))
307 return -ENOMEM;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100308
Chris Wilson2889caa2017-06-16 15:05:19 +0100309 eb->lut_size = size;
Chris Wilson650bc632017-06-15 09:14:33 +0100310 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +0100311 eb->lut_size = -eb->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +0100312 }
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000313
Chris Wilson650bc632017-06-15 09:14:33 +0100314 return 0;
Chris Wilson67731b82010-12-08 10:38:14 +0000315}
316
Chris Wilson2889caa2017-06-16 15:05:19 +0100317static bool
318eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry,
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100319 const struct i915_vma *vma,
320 unsigned int flags)
Chris Wilson2889caa2017-06-16 15:05:19 +0100321{
Chris Wilson2889caa2017-06-16 15:05:19 +0100322 if (vma->node.size < entry->pad_to_size)
323 return true;
324
325 if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment))
326 return true;
327
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100328 if (flags & EXEC_OBJECT_PINNED &&
Chris Wilson2889caa2017-06-16 15:05:19 +0100329 vma->node.start != entry->offset)
330 return true;
331
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100332 if (flags & __EXEC_OBJECT_NEEDS_BIAS &&
Chris Wilson2889caa2017-06-16 15:05:19 +0100333 vma->node.start < BATCH_OFFSET_BIAS)
334 return true;
335
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100336 if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) &&
Chris Wilson2889caa2017-06-16 15:05:19 +0100337 (vma->node.start + vma->node.size - 1) >> 32)
338 return true;
339
340 return false;
341}
342
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100343static inline bool
Chris Wilson2889caa2017-06-16 15:05:19 +0100344eb_pin_vma(struct i915_execbuffer *eb,
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100345 const struct drm_i915_gem_exec_object2 *entry,
Chris Wilson2889caa2017-06-16 15:05:19 +0100346 struct i915_vma *vma)
347{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100348 unsigned int exec_flags = *vma->exec_flags;
349 u64 pin_flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100350
Chris Wilson616d9ce2017-06-16 15:05:21 +0100351 if (vma->node.size)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100352 pin_flags = vma->node.start;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100353 else
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100354 pin_flags = entry->offset & PIN_OFFSET_MASK;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100355
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100356 pin_flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED;
357 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_GTT))
358 pin_flags |= PIN_GLOBAL;
Chris Wilson616d9ce2017-06-16 15:05:21 +0100359
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100360 if (unlikely(i915_vma_pin(vma, 0, 0, pin_flags)))
361 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100362
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100363 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100364 if (unlikely(i915_vma_get_fence(vma))) {
365 i915_vma_unpin(vma);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100366 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100367 }
368
369 if (i915_vma_pin_fence(vma))
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100370 exec_flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100371 }
372
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100373 *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN;
374 return !eb_vma_misplaced(entry, vma, exec_flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100375}
376
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100377static inline void __eb_unreserve_vma(struct i915_vma *vma, unsigned int flags)
Chris Wilsond55495b2017-06-15 09:14:34 +0100378{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100379 GEM_BUG_ON(!(flags & __EXEC_OBJECT_HAS_PIN));
Chris Wilson2889caa2017-06-16 15:05:19 +0100380
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100381 if (unlikely(flags & __EXEC_OBJECT_HAS_FENCE))
Chris Wilsond55495b2017-06-15 09:14:34 +0100382 i915_vma_unpin_fence(vma);
383
Chris Wilson2889caa2017-06-16 15:05:19 +0100384 __i915_vma_unpin(vma);
Chris Wilsond55495b2017-06-15 09:14:34 +0100385}
386
Chris Wilson2889caa2017-06-16 15:05:19 +0100387static inline void
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100388eb_unreserve_vma(struct i915_vma *vma, unsigned int *flags)
Chris Wilsond55495b2017-06-15 09:14:34 +0100389{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100390 if (!(*flags & __EXEC_OBJECT_HAS_PIN))
Chris Wilson2889caa2017-06-16 15:05:19 +0100391 return;
Chris Wilsond55495b2017-06-15 09:14:34 +0100392
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100393 __eb_unreserve_vma(vma, *flags);
394 *flags &= ~__EXEC_OBJECT_RESERVED;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100395}
396
397static int
Chris Wilson2889caa2017-06-16 15:05:19 +0100398eb_validate_vma(struct i915_execbuffer *eb,
399 struct drm_i915_gem_exec_object2 *entry,
400 struct i915_vma *vma)
401{
402 if (unlikely(entry->flags & eb->invalid_flags))
403 return -EINVAL;
404
405 if (unlikely(entry->alignment && !is_power_of_2(entry->alignment)))
406 return -EINVAL;
407
408 /*
409 * Offset can be used as input (EXEC_OBJECT_PINNED), reject
410 * any non-page-aligned or non-canonical addresses.
411 */
412 if (unlikely(entry->flags & EXEC_OBJECT_PINNED &&
413 entry->offset != gen8_canonical_addr(entry->offset & PAGE_MASK)))
414 return -EINVAL;
415
416 /* pad_to_size was once a reserved field, so sanitize it */
417 if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) {
418 if (unlikely(offset_in_page(entry->pad_to_size)))
419 return -EINVAL;
420 } else {
421 entry->pad_to_size = 0;
422 }
423
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100424 if (unlikely(vma->exec_flags)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100425 DRM_DEBUG("Object [handle %d, index %d] appears more than once in object list\n",
426 entry->handle, (int)(entry - eb->exec));
427 return -EINVAL;
428 }
429
430 /*
431 * From drm_mm perspective address space is continuous,
432 * so from this point we're always using non-canonical
433 * form internally.
434 */
435 entry->offset = gen8_noncanonical_addr(entry->offset);
436
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100437 if (!eb->reloc_cache.has_fence) {
438 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
439 } else {
440 if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE ||
441 eb->reloc_cache.needs_unfenced) &&
442 i915_gem_object_is_tiled(vma->obj))
443 entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP;
444 }
445
446 if (!(entry->flags & EXEC_OBJECT_PINNED))
447 entry->flags |= eb->context_flags;
448
Chris Wilson2889caa2017-06-16 15:05:19 +0100449 return 0;
450}
451
452static int
Chris Wilson170fa292017-08-16 09:52:07 +0100453eb_add_vma(struct i915_execbuffer *eb,
454 unsigned int i, struct i915_vma *vma,
455 unsigned int flags)
Chris Wilson2889caa2017-06-16 15:05:19 +0100456{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100457 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100458 int err;
459
460 GEM_BUG_ON(i915_vma_is_closed(vma));
461
462 if (!(eb->args->flags & __EXEC_VALIDATED)) {
463 err = eb_validate_vma(eb, entry, vma);
464 if (unlikely(err))
465 return err;
466 }
467
Chris Wilson4d470f72017-06-29 16:04:25 +0100468 if (eb->lut_size > 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100469 vma->exec_handle = entry->handle;
470 hlist_add_head(&vma->exec_node,
471 &eb->buckets[hash_32(entry->handle,
472 eb->lut_size)]);
473 }
474
475 if (entry->relocation_count)
476 list_add_tail(&vma->reloc_link, &eb->relocs);
477
Chris Wilson2889caa2017-06-16 15:05:19 +0100478 /*
479 * Stash a pointer from the vma to execobj, so we can query its flags,
480 * size, alignment etc as provided by the user. Also we stash a pointer
481 * to the vma inside the execobj so that we can use a direct lookup
482 * to find the right target VMA when doing relocations.
483 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100484 eb->vma[i] = vma;
Chris Wilson170fa292017-08-16 09:52:07 +0100485 eb->flags[i] = entry->flags | flags;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100486 vma->exec_flags = &eb->flags[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100487
488 err = 0;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100489 if (eb_pin_vma(eb, entry, vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100490 if (entry->offset != vma->node.start) {
491 entry->offset = vma->node.start | UPDATE;
492 eb->args->flags |= __EXEC_HAS_RELOC;
493 }
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100494 } else {
495 eb_unreserve_vma(vma, vma->exec_flags);
496
497 list_add_tail(&vma->exec_link, &eb->unbound);
498 if (drm_mm_node_allocated(&vma->node))
499 err = i915_vma_unbind(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +0100500 }
501 return err;
502}
503
504static inline int use_cpu_reloc(const struct reloc_cache *cache,
505 const struct drm_i915_gem_object *obj)
506{
507 if (!i915_gem_object_has_struct_page(obj))
508 return false;
509
Chris Wilson7dd4f672017-06-16 15:05:24 +0100510 if (DBG_FORCE_RELOC == FORCE_CPU_RELOC)
511 return true;
512
513 if (DBG_FORCE_RELOC == FORCE_GTT_RELOC)
514 return false;
Chris Wilson2889caa2017-06-16 15:05:19 +0100515
516 return (cache->has_llc ||
517 obj->cache_dirty ||
518 obj->cache_level != I915_CACHE_NONE);
519}
520
521static int eb_reserve_vma(const struct i915_execbuffer *eb,
522 struct i915_vma *vma)
523{
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100524 struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
525 unsigned int exec_flags = *vma->exec_flags;
526 u64 pin_flags;
Chris Wilson2889caa2017-06-16 15:05:19 +0100527 int err;
528
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100529 pin_flags = PIN_USER | PIN_NONBLOCK;
530 if (exec_flags & EXEC_OBJECT_NEEDS_GTT)
531 pin_flags |= PIN_GLOBAL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100532
533 /*
534 * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
535 * limit address to the first 4GBs for unflagged objects.
536 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100537 if (!(exec_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS))
538 pin_flags |= PIN_ZONE_4G;
Chris Wilson2889caa2017-06-16 15:05:19 +0100539
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100540 if (exec_flags & __EXEC_OBJECT_NEEDS_MAP)
541 pin_flags |= PIN_MAPPABLE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100542
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100543 if (exec_flags & EXEC_OBJECT_PINNED) {
544 pin_flags |= entry->offset | PIN_OFFSET_FIXED;
545 pin_flags &= ~PIN_NONBLOCK; /* force overlapping checks */
546 } else if (exec_flags & __EXEC_OBJECT_NEEDS_BIAS) {
547 pin_flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson2889caa2017-06-16 15:05:19 +0100548 }
549
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100550 err = i915_vma_pin(vma,
551 entry->pad_to_size, entry->alignment,
552 pin_flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100553 if (err)
554 return err;
555
556 if (entry->offset != vma->node.start) {
557 entry->offset = vma->node.start | UPDATE;
558 eb->args->flags |= __EXEC_HAS_RELOC;
559 }
560
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100561 if (unlikely(exec_flags & EXEC_OBJECT_NEEDS_FENCE)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100562 err = i915_vma_get_fence(vma);
563 if (unlikely(err)) {
564 i915_vma_unpin(vma);
565 return err;
566 }
567
568 if (i915_vma_pin_fence(vma))
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100569 exec_flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson2889caa2017-06-16 15:05:19 +0100570 }
571
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100572 *vma->exec_flags = exec_flags | __EXEC_OBJECT_HAS_PIN;
573 GEM_BUG_ON(eb_vma_misplaced(entry, vma, exec_flags));
Chris Wilson1da7b542017-07-21 15:50:35 +0100574
Chris Wilson2889caa2017-06-16 15:05:19 +0100575 return 0;
576}
577
578static int eb_reserve(struct i915_execbuffer *eb)
579{
580 const unsigned int count = eb->buffer_count;
581 struct list_head last;
582 struct i915_vma *vma;
583 unsigned int i, pass;
584 int err;
585
586 /*
587 * Attempt to pin all of the buffers into the GTT.
588 * This is done in 3 phases:
589 *
590 * 1a. Unbind all objects that do not match the GTT constraints for
591 * the execbuffer (fenceable, mappable, alignment etc).
592 * 1b. Increment pin count for already bound objects.
593 * 2. Bind new objects.
594 * 3. Decrement pin count.
595 *
596 * This avoid unnecessary unbinding of later objects in order to make
597 * room for the earlier objects *unless* we need to defragment.
598 */
599
600 pass = 0;
601 err = 0;
602 do {
603 list_for_each_entry(vma, &eb->unbound, exec_link) {
604 err = eb_reserve_vma(eb, vma);
605 if (err)
606 break;
607 }
608 if (err != -ENOSPC)
609 return err;
610
611 /* Resort *all* the objects into priority order */
612 INIT_LIST_HEAD(&eb->unbound);
613 INIT_LIST_HEAD(&last);
614 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100615 unsigned int flags = eb->flags[i];
616 struct i915_vma *vma = eb->vma[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100617
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100618 if (flags & EXEC_OBJECT_PINNED &&
619 flags & __EXEC_OBJECT_HAS_PIN)
Chris Wilson2889caa2017-06-16 15:05:19 +0100620 continue;
621
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100622 eb_unreserve_vma(vma, &eb->flags[i]);
Chris Wilson2889caa2017-06-16 15:05:19 +0100623
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100624 if (flags & EXEC_OBJECT_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +0100625 list_add(&vma->exec_link, &eb->unbound);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100626 else if (flags & __EXEC_OBJECT_NEEDS_MAP)
Chris Wilson2889caa2017-06-16 15:05:19 +0100627 list_add_tail(&vma->exec_link, &eb->unbound);
628 else
629 list_add_tail(&vma->exec_link, &last);
630 }
631 list_splice_tail(&last, &eb->unbound);
632
633 switch (pass++) {
634 case 0:
635 break;
636
637 case 1:
638 /* Too fragmented, unbind everything and retry */
639 err = i915_gem_evict_vm(eb->vm);
640 if (err)
641 return err;
642 break;
643
644 default:
645 return -ENOSPC;
646 }
647 } while (1);
648}
649
650static inline struct hlist_head *
651ht_head(const struct i915_gem_context_vma_lut *lut, u32 handle)
652{
653 return &lut->ht[hash_32(handle, lut->ht_bits)];
654}
655
656static inline bool
657ht_needs_resize(const struct i915_gem_context_vma_lut *lut)
658{
659 return (4*lut->ht_count > 3*lut->ht_size ||
660 4*lut->ht_count + 1 < lut->ht_size);
661}
662
663static unsigned int eb_batch_index(const struct i915_execbuffer *eb)
664{
Chris Wilson1a71cf22017-06-16 15:05:23 +0100665 if (eb->args->flags & I915_EXEC_BATCH_FIRST)
666 return 0;
667 else
668 return eb->buffer_count - 1;
Chris Wilson2889caa2017-06-16 15:05:19 +0100669}
670
671static int eb_select_context(struct i915_execbuffer *eb)
672{
673 struct i915_gem_context *ctx;
674
675 ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1);
Chris Wilson1acfc102017-06-20 12:05:47 +0100676 if (unlikely(!ctx))
677 return -ENOENT;
Chris Wilson2889caa2017-06-16 15:05:19 +0100678
Chris Wilson1acfc102017-06-20 12:05:47 +0100679 eb->ctx = ctx;
Chris Wilson2889caa2017-06-16 15:05:19 +0100680 eb->vm = ctx->ppgtt ? &ctx->ppgtt->base : &eb->i915->ggtt.base;
681
682 eb->context_flags = 0;
683 if (ctx->flags & CONTEXT_NO_ZEROMAP)
684 eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS;
685
686 return 0;
687}
688
689static int eb_lookup_vmas(struct i915_execbuffer *eb)
Chris Wilson4ff4b442017-06-16 15:05:16 +0100690{
Chris Wilson2889caa2017-06-16 15:05:19 +0100691 struct i915_gem_context_vma_lut *lut = &eb->ctx->vma_lut;
Chris Wilson170fa292017-08-16 09:52:07 +0100692 struct drm_i915_gem_object *uninitialized_var(obj);
Chris Wilson2889caa2017-06-16 15:05:19 +0100693 unsigned int i;
Chris Wilson2889caa2017-06-16 15:05:19 +0100694 int err;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100695
Chris Wilson8bcbfb12017-08-16 09:52:05 +0100696 if (unlikely(i915_gem_context_is_closed(eb->ctx)))
697 return -ENOENT;
698
699 if (unlikely(i915_gem_context_is_banned(eb->ctx)))
700 return -EIO;
701
Chris Wilson2889caa2017-06-16 15:05:19 +0100702 INIT_LIST_HEAD(&eb->relocs);
703 INIT_LIST_HEAD(&eb->unbound);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100704
Chris Wilson2889caa2017-06-16 15:05:19 +0100705 if (unlikely(lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS))
706 flush_work(&lut->resize);
707 GEM_BUG_ON(lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100708
Chris Wilson170fa292017-08-16 09:52:07 +0100709 for (i = 0; i < eb->buffer_count; i++) {
710 u32 handle = eb->exec[i].handle;
711 struct hlist_head *hl = ht_head(lut, handle);
712 unsigned int flags = 0;
713 struct i915_vma *vma;
714
715 hlist_for_each_entry(vma, hl, ctx_node) {
716 GEM_BUG_ON(vma->ctx != eb->ctx);
717
718 if (vma->ctx_handle != handle)
Chris Wilson4ff4b442017-06-16 15:05:16 +0100719 continue;
720
Chris Wilson170fa292017-08-16 09:52:07 +0100721 goto add_vma;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100722 }
723
Chris Wilson170fa292017-08-16 09:52:07 +0100724 obj = i915_gem_object_lookup(eb->file, handle);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100725 if (unlikely(!obj)) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100726 err = -ENOENT;
Chris Wilson170fa292017-08-16 09:52:07 +0100727 goto err_vma;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100728 }
729
Chris Wilson4ff4b442017-06-16 15:05:16 +0100730 vma = i915_vma_instance(obj, eb->vm, NULL);
731 if (unlikely(IS_ERR(vma))) {
Chris Wilson2889caa2017-06-16 15:05:19 +0100732 err = PTR_ERR(vma);
Chris Wilson170fa292017-08-16 09:52:07 +0100733 goto err_obj;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100734 }
735
736 /* First come, first served */
737 if (!vma->ctx) {
738 vma->ctx = eb->ctx;
Chris Wilson170fa292017-08-16 09:52:07 +0100739 vma->ctx_handle = handle;
740 hlist_add_head(&vma->ctx_node, hl);
Chris Wilson2889caa2017-06-16 15:05:19 +0100741 lut->ht_count++;
742 lut->ht_size |= I915_CTX_RESIZE_IN_PROGRESS;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100743 if (i915_vma_is_ggtt(vma)) {
744 GEM_BUG_ON(obj->vma_hashed);
745 obj->vma_hashed = vma;
746 }
Chris Wilsondade2a62017-06-16 15:05:20 +0100747
Chris Wilson170fa292017-08-16 09:52:07 +0100748 /* transfer ref to ctx */
749 obj = NULL;
750 } else {
751 flags = __EXEC_OBJECT_HAS_REF;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100752 }
753
Chris Wilson170fa292017-08-16 09:52:07 +0100754add_vma:
755 err = eb_add_vma(eb, i, vma, flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100756 if (unlikely(err))
Chris Wilson170fa292017-08-16 09:52:07 +0100757 goto err_obj;
Chris Wilsondade2a62017-06-16 15:05:20 +0100758
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100759 GEM_BUG_ON(vma != eb->vma[i]);
760 GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100761 }
762
Chris Wilson2889caa2017-06-16 15:05:19 +0100763 if (lut->ht_size & I915_CTX_RESIZE_IN_PROGRESS) {
764 if (ht_needs_resize(lut))
765 queue_work(system_highpri_wq, &lut->resize);
766 else
767 lut->ht_size &= ~I915_CTX_RESIZE_IN_PROGRESS;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100768 }
769
Chris Wilson2889caa2017-06-16 15:05:19 +0100770 /* take note of the batch buffer before we might reorder the lists */
771 i = eb_batch_index(eb);
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100772 eb->batch = eb->vma[i];
773 GEM_BUG_ON(eb->batch->exec_flags != &eb->flags[i]);
Chris Wilson59bfa122016-08-04 16:32:31 +0100774
775 /*
776 * SNA is doing fancy tricks with compressing batch buffers, which leads
777 * to negative relocation deltas. Usually that works out ok since the
778 * relocate address is still positive, except when the batch is placed
779 * very low in the GTT. Ensure this doesn't happen.
780 *
781 * Note that actual hangs have only been observed on gen7, but for
782 * paranoia do it everywhere.
783 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100784 if (!(eb->flags[i] & EXEC_OBJECT_PINNED))
785 eb->flags[i] |= __EXEC_OBJECT_NEEDS_BIAS;
Chris Wilson2889caa2017-06-16 15:05:19 +0100786 if (eb->reloc_cache.has_fence)
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100787 eb->flags[i] |= EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson59bfa122016-08-04 16:32:31 +0100788
Chris Wilson2889caa2017-06-16 15:05:19 +0100789 eb->args->flags |= __EXEC_VALIDATED;
790 return eb_reserve(eb);
791
Chris Wilson170fa292017-08-16 09:52:07 +0100792err_obj:
793 if (obj)
794 i915_gem_object_put(obj);
795err_vma:
796 eb->vma[i] = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100797 lut->ht_size &= ~I915_CTX_RESIZE_IN_PROGRESS;
798 return err;
Chris Wilson59bfa122016-08-04 16:32:31 +0100799}
800
Chris Wilson4ff4b442017-06-16 15:05:16 +0100801static struct i915_vma *
Chris Wilson2889caa2017-06-16 15:05:19 +0100802eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000803{
Chris Wilson2889caa2017-06-16 15:05:19 +0100804 if (eb->lut_size < 0) {
805 if (handle >= -eb->lut_size)
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000806 return NULL;
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100807 return eb->vma[handle];
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000808 } else {
809 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800810 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000811
Chris Wilson2889caa2017-06-16 15:05:19 +0100812 head = &eb->buckets[hash_32(handle, eb->lut_size)];
Geliang Tangaa459502016-01-18 23:54:20 +0800813 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200814 if (vma->exec_handle == handle)
815 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000816 }
817 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000818 }
Chris Wilson67731b82010-12-08 10:38:14 +0000819}
820
Chris Wilson2889caa2017-06-16 15:05:19 +0100821static void eb_release_vmas(const struct i915_execbuffer *eb)
Chris Wilsona415d352013-11-26 11:23:15 +0000822{
Chris Wilson2889caa2017-06-16 15:05:19 +0100823 const unsigned int count = eb->buffer_count;
824 unsigned int i;
Chris Wilson650bc632017-06-15 09:14:33 +0100825
Chris Wilson2889caa2017-06-16 15:05:19 +0100826 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100827 struct i915_vma *vma = eb->vma[i];
828 unsigned int flags = eb->flags[i];
Chris Wilson2889caa2017-06-16 15:05:19 +0100829
830 if (!vma)
Chris Wilson170fa292017-08-16 09:52:07 +0100831 break;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000832
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100833 GEM_BUG_ON(vma->exec_flags != &eb->flags[i]);
834 vma->exec_flags = NULL;
835 eb->vma[i] = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +0100836
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100837 if (flags & __EXEC_OBJECT_HAS_PIN)
838 __eb_unreserve_vma(vma, flags);
Chris Wilson2889caa2017-06-16 15:05:19 +0100839
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100840 if (flags & __EXEC_OBJECT_HAS_REF)
Chris Wilsondade2a62017-06-16 15:05:20 +0100841 i915_vma_put(vma);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000842 }
Chris Wilson2889caa2017-06-16 15:05:19 +0100843}
Chris Wilsond55495b2017-06-15 09:14:34 +0100844
Chris Wilson2889caa2017-06-16 15:05:19 +0100845static void eb_reset_vmas(const struct i915_execbuffer *eb)
846{
847 eb_release_vmas(eb);
Chris Wilson4d470f72017-06-29 16:04:25 +0100848 if (eb->lut_size > 0)
Chris Wilson2889caa2017-06-16 15:05:19 +0100849 memset(eb->buckets, 0,
850 sizeof(struct hlist_head) << eb->lut_size);
851}
Chris Wilsond55495b2017-06-15 09:14:34 +0100852
Chris Wilson2889caa2017-06-16 15:05:19 +0100853static void eb_destroy(const struct i915_execbuffer *eb)
854{
Chris Wilson7dd4f672017-06-16 15:05:24 +0100855 GEM_BUG_ON(eb->reloc_cache.rq);
856
Chris Wilson4d470f72017-06-29 16:04:25 +0100857 if (eb->lut_size > 0)
Chris Wilsond55495b2017-06-15 09:14:34 +0100858 kfree(eb->buckets);
Chris Wilson67731b82010-12-08 10:38:14 +0000859}
860
Chris Wilson2889caa2017-06-16 15:05:19 +0100861static inline u64
Chris Wilsond50415c2016-08-18 17:16:52 +0100862relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +0100863 const struct i915_vma *target)
Michał Winiarski934acce2015-12-29 18:24:52 +0100864{
Chris Wilson2889caa2017-06-16 15:05:19 +0100865 return gen8_canonical_addr((int)reloc->delta + target->node.start);
Michał Winiarski934acce2015-12-29 18:24:52 +0100866}
867
Chris Wilsond50415c2016-08-18 17:16:52 +0100868static void reloc_cache_init(struct reloc_cache *cache,
869 struct drm_i915_private *i915)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100870{
Chris Wilson31a39202016-08-18 17:16:46 +0100871 cache->page = -1;
Chris Wilsond50415c2016-08-18 17:16:52 +0100872 cache->vaddr = 0;
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200873 /* Must be a variable in the struct to allow GCC to unroll. */
Chris Wilson7dd4f672017-06-16 15:05:24 +0100874 cache->gen = INTEL_GEN(i915);
Chris Wilson2889caa2017-06-16 15:05:19 +0100875 cache->has_llc = HAS_LLC(i915);
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200876 cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
Chris Wilson7dd4f672017-06-16 15:05:24 +0100877 cache->has_fence = cache->gen < 4;
878 cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment;
Chris Wilsone8cb9092016-08-18 17:16:53 +0100879 cache->node.allocated = false;
Chris Wilson7dd4f672017-06-16 15:05:24 +0100880 cache->rq = NULL;
881 cache->rq_size = 0;
Chris Wilson31a39202016-08-18 17:16:46 +0100882}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100883
Chris Wilsond50415c2016-08-18 17:16:52 +0100884static inline void *unmask_page(unsigned long p)
885{
886 return (void *)(uintptr_t)(p & PAGE_MASK);
887}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100888
Chris Wilsond50415c2016-08-18 17:16:52 +0100889static inline unsigned int unmask_flags(unsigned long p)
890{
891 return p & ~PAGE_MASK;
892}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700893
Chris Wilsond50415c2016-08-18 17:16:52 +0100894#define KMAP 0x4 /* after CLFLUSH_FLAGS */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700895
Chris Wilson650bc632017-06-15 09:14:33 +0100896static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
897{
898 struct drm_i915_private *i915 =
899 container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
900 return &i915->ggtt;
901}
902
Chris Wilson7dd4f672017-06-16 15:05:24 +0100903static void reloc_gpu_flush(struct reloc_cache *cache)
904{
905 GEM_BUG_ON(cache->rq_size >= cache->rq->batch->obj->base.size / sizeof(u32));
906 cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END;
907 i915_gem_object_unpin_map(cache->rq->batch->obj);
908 i915_gem_chipset_flush(cache->rq->i915);
909
910 __i915_add_request(cache->rq, true);
911 cache->rq = NULL;
912}
913
Chris Wilson650bc632017-06-15 09:14:33 +0100914static void reloc_cache_reset(struct reloc_cache *cache)
Chris Wilson31a39202016-08-18 17:16:46 +0100915{
Chris Wilsond50415c2016-08-18 17:16:52 +0100916 void *vaddr;
917
Chris Wilson7dd4f672017-06-16 15:05:24 +0100918 if (cache->rq)
919 reloc_gpu_flush(cache);
920
Chris Wilson31a39202016-08-18 17:16:46 +0100921 if (!cache->vaddr)
922 return;
923
Chris Wilsond50415c2016-08-18 17:16:52 +0100924 vaddr = unmask_page(cache->vaddr);
925 if (cache->vaddr & KMAP) {
926 if (cache->vaddr & CLFLUSH_AFTER)
927 mb();
Chris Wilson31a39202016-08-18 17:16:46 +0100928
Chris Wilsond50415c2016-08-18 17:16:52 +0100929 kunmap_atomic(vaddr);
930 i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
931 } else {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100932 wmb();
Chris Wilsond50415c2016-08-18 17:16:52 +0100933 io_mapping_unmap_atomic((void __iomem *)vaddr);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100934 if (cache->node.allocated) {
Chris Wilson650bc632017-06-15 09:14:33 +0100935 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100936
937 ggtt->base.clear_range(&ggtt->base,
938 cache->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200939 cache->node.size);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100940 drm_mm_remove_node(&cache->node);
941 } else {
942 i915_vma_unpin((struct i915_vma *)cache->node.mm);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700943 }
Chris Wilson31a39202016-08-18 17:16:46 +0100944 }
Chris Wilson650bc632017-06-15 09:14:33 +0100945
946 cache->vaddr = 0;
947 cache->page = -1;
Chris Wilson31a39202016-08-18 17:16:46 +0100948}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700949
Chris Wilson31a39202016-08-18 17:16:46 +0100950static void *reloc_kmap(struct drm_i915_gem_object *obj,
951 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100952 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100953{
Chris Wilsond50415c2016-08-18 17:16:52 +0100954 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100955
Chris Wilsond50415c2016-08-18 17:16:52 +0100956 if (cache->vaddr) {
957 kunmap_atomic(unmask_page(cache->vaddr));
958 } else {
959 unsigned int flushes;
Chris Wilson2889caa2017-06-16 15:05:19 +0100960 int err;
Chris Wilson31a39202016-08-18 17:16:46 +0100961
Chris Wilson2889caa2017-06-16 15:05:19 +0100962 err = i915_gem_obj_prepare_shmem_write(obj, &flushes);
963 if (err)
964 return ERR_PTR(err);
Chris Wilsond50415c2016-08-18 17:16:52 +0100965
966 BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
967 BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
968
969 cache->vaddr = flushes | KMAP;
970 cache->node.mm = (void *)obj;
971 if (flushes)
972 mb();
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700973 }
974
Chris Wilsond50415c2016-08-18 17:16:52 +0100975 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
976 cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100977 cache->page = page;
Chris Wilson31a39202016-08-18 17:16:46 +0100978
Chris Wilsond50415c2016-08-18 17:16:52 +0100979 return vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100980}
981
Chris Wilsond50415c2016-08-18 17:16:52 +0100982static void *reloc_iomap(struct drm_i915_gem_object *obj,
Chris Wilson31a39202016-08-18 17:16:46 +0100983 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +0100984 unsigned long page)
Chris Wilson31a39202016-08-18 17:16:46 +0100985{
Chris Wilson650bc632017-06-15 09:14:33 +0100986 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100987 unsigned long offset;
Chris Wilsond50415c2016-08-18 17:16:52 +0100988 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100989
Chris Wilsond50415c2016-08-18 17:16:52 +0100990 if (cache->vaddr) {
Jani Nikula615e5002016-10-04 12:54:13 +0300991 io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
Chris Wilsond50415c2016-08-18 17:16:52 +0100992 } else {
993 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +0100994 int err;
Chris Wilson31a39202016-08-18 17:16:46 +0100995
Chris Wilson2889caa2017-06-16 15:05:19 +0100996 if (use_cpu_reloc(cache, obj))
Chris Wilsond50415c2016-08-18 17:16:52 +0100997 return NULL;
Chris Wilson31a39202016-08-18 17:16:46 +0100998
Chris Wilson2889caa2017-06-16 15:05:19 +0100999 err = i915_gem_object_set_to_gtt_domain(obj, true);
1000 if (err)
1001 return ERR_PTR(err);
Chris Wilson31a39202016-08-18 17:16:46 +01001002
Chris Wilsond50415c2016-08-18 17:16:52 +01001003 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1004 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001005 if (IS_ERR(vma)) {
1006 memset(&cache->node, 0, sizeof(cache->node));
Chris Wilson2889caa2017-06-16 15:05:19 +01001007 err = drm_mm_insert_node_in_range
Chris Wilsone8cb9092016-08-18 17:16:53 +01001008 (&ggtt->base.mm, &cache->node,
Chris Wilsonf51455d2017-01-10 14:47:34 +00001009 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
Chris Wilsone8cb9092016-08-18 17:16:53 +01001010 0, ggtt->mappable_end,
Chris Wilson4e64e552017-02-02 21:04:38 +00001011 DRM_MM_INSERT_LOW);
Chris Wilson2889caa2017-06-16 15:05:19 +01001012 if (err) /* no inactive aperture space, use cpu reloc */
Chris Wilsonc92fa4f2016-10-07 07:53:25 +01001013 return NULL;
Chris Wilsone8cb9092016-08-18 17:16:53 +01001014 } else {
Chris Wilson2889caa2017-06-16 15:05:19 +01001015 err = i915_vma_put_fence(vma);
1016 if (err) {
Chris Wilsone8cb9092016-08-18 17:16:53 +01001017 i915_vma_unpin(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001018 return ERR_PTR(err);
Chris Wilsone8cb9092016-08-18 17:16:53 +01001019 }
Rafael Barbalho5032d872013-08-21 17:10:51 +01001020
Chris Wilsone8cb9092016-08-18 17:16:53 +01001021 cache->node.start = vma->node.start;
1022 cache->node.mm = (void *)vma;
Chris Wilsond50415c2016-08-18 17:16:52 +01001023 }
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001024 }
1025
Chris Wilsone8cb9092016-08-18 17:16:53 +01001026 offset = cache->node.start;
1027 if (cache->node.allocated) {
Chris Wilsonfc099092016-10-28 15:27:56 +01001028 wmb();
Chris Wilsone8cb9092016-08-18 17:16:53 +01001029 ggtt->base.insert_page(&ggtt->base,
1030 i915_gem_object_get_dma_address(obj, page),
1031 offset, I915_CACHE_NONE, 0);
1032 } else {
1033 offset += page << PAGE_SHIFT;
1034 }
1035
Chris Wilson650bc632017-06-15 09:14:33 +01001036 vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->mappable,
1037 offset);
Chris Wilsond50415c2016-08-18 17:16:52 +01001038 cache->page = page;
1039 cache->vaddr = (unsigned long)vaddr;
1040
1041 return vaddr;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001042}
1043
Chris Wilsond50415c2016-08-18 17:16:52 +01001044static void *reloc_vaddr(struct drm_i915_gem_object *obj,
1045 struct reloc_cache *cache,
Chris Wilson2889caa2017-06-16 15:05:19 +01001046 unsigned long page)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001047{
Chris Wilsond50415c2016-08-18 17:16:52 +01001048 void *vaddr;
1049
1050 if (cache->page == page) {
1051 vaddr = unmask_page(cache->vaddr);
1052 } else {
1053 vaddr = NULL;
1054 if ((cache->vaddr & KMAP) == 0)
1055 vaddr = reloc_iomap(obj, cache, page);
1056 if (!vaddr)
1057 vaddr = reloc_kmap(obj, cache, page);
1058 }
1059
1060 return vaddr;
1061}
1062
1063static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
1064{
1065 if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
1066 if (flushes & CLFLUSH_BEFORE) {
1067 clflushopt(addr);
1068 mb();
1069 }
1070
1071 *addr = value;
1072
Chris Wilson2889caa2017-06-16 15:05:19 +01001073 /*
1074 * Writes to the same cacheline are serialised by the CPU
Chris Wilsond50415c2016-08-18 17:16:52 +01001075 * (including clflush). On the write path, we only require
1076 * that it hits memory in an orderly fashion and place
1077 * mb barriers at the start and end of the relocation phase
1078 * to ensure ordering of clflush wrt to the system.
1079 */
1080 if (flushes & CLFLUSH_AFTER)
1081 clflushopt(addr);
1082 } else
1083 *addr = value;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001084}
1085
Chris Wilson7dd4f672017-06-16 15:05:24 +01001086static int __reloc_gpu_alloc(struct i915_execbuffer *eb,
1087 struct i915_vma *vma,
1088 unsigned int len)
1089{
1090 struct reloc_cache *cache = &eb->reloc_cache;
1091 struct drm_i915_gem_object *obj;
1092 struct drm_i915_gem_request *rq;
1093 struct i915_vma *batch;
1094 u32 *cmd;
1095 int err;
1096
1097 GEM_BUG_ON(vma->obj->base.write_domain & I915_GEM_DOMAIN_CPU);
1098
1099 obj = i915_gem_batch_pool_get(&eb->engine->batch_pool, PAGE_SIZE);
1100 if (IS_ERR(obj))
1101 return PTR_ERR(obj);
1102
1103 cmd = i915_gem_object_pin_map(obj,
1104 cache->has_llc ? I915_MAP_WB : I915_MAP_WC);
1105 i915_gem_object_unpin_pages(obj);
1106 if (IS_ERR(cmd))
1107 return PTR_ERR(cmd);
1108
1109 err = i915_gem_object_set_to_wc_domain(obj, false);
1110 if (err)
1111 goto err_unmap;
1112
1113 batch = i915_vma_instance(obj, vma->vm, NULL);
1114 if (IS_ERR(batch)) {
1115 err = PTR_ERR(batch);
1116 goto err_unmap;
1117 }
1118
1119 err = i915_vma_pin(batch, 0, 0, PIN_USER | PIN_NONBLOCK);
1120 if (err)
1121 goto err_unmap;
1122
1123 rq = i915_gem_request_alloc(eb->engine, eb->ctx);
1124 if (IS_ERR(rq)) {
1125 err = PTR_ERR(rq);
1126 goto err_unpin;
1127 }
1128
1129 err = i915_gem_request_await_object(rq, vma->obj, true);
1130 if (err)
1131 goto err_request;
1132
1133 err = eb->engine->emit_flush(rq, EMIT_INVALIDATE);
1134 if (err)
1135 goto err_request;
1136
1137 err = i915_switch_context(rq);
1138 if (err)
1139 goto err_request;
1140
1141 err = eb->engine->emit_bb_start(rq,
1142 batch->node.start, PAGE_SIZE,
1143 cache->gen > 5 ? 0 : I915_DISPATCH_SECURE);
1144 if (err)
1145 goto err_request;
1146
Chris Wilson95ff7c72017-06-16 15:05:25 +01001147 GEM_BUG_ON(!reservation_object_test_signaled_rcu(batch->resv, true));
Chris Wilson7dd4f672017-06-16 15:05:24 +01001148 i915_vma_move_to_active(batch, rq, 0);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001149 reservation_object_lock(batch->resv, NULL);
1150 reservation_object_add_excl_fence(batch->resv, &rq->fence);
1151 reservation_object_unlock(batch->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001152 i915_vma_unpin(batch);
1153
Chris Wilson25ffaa62017-06-20 13:43:20 +01001154 i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
Chris Wilson95ff7c72017-06-16 15:05:25 +01001155 reservation_object_lock(vma->resv, NULL);
1156 reservation_object_add_excl_fence(vma->resv, &rq->fence);
1157 reservation_object_unlock(vma->resv);
Chris Wilson7dd4f672017-06-16 15:05:24 +01001158
1159 rq->batch = batch;
1160
1161 cache->rq = rq;
1162 cache->rq_cmd = cmd;
1163 cache->rq_size = 0;
1164
1165 /* Return with batch mapping (cmd) still pinned */
1166 return 0;
1167
1168err_request:
1169 i915_add_request(rq);
1170err_unpin:
1171 i915_vma_unpin(batch);
1172err_unmap:
1173 i915_gem_object_unpin_map(obj);
1174 return err;
1175}
1176
1177static u32 *reloc_gpu(struct i915_execbuffer *eb,
1178 struct i915_vma *vma,
1179 unsigned int len)
1180{
1181 struct reloc_cache *cache = &eb->reloc_cache;
1182 u32 *cmd;
1183
1184 if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1))
1185 reloc_gpu_flush(cache);
1186
1187 if (unlikely(!cache->rq)) {
1188 int err;
1189
1190 err = __reloc_gpu_alloc(eb, vma, len);
1191 if (unlikely(err))
1192 return ERR_PTR(err);
1193 }
1194
1195 cmd = cache->rq_cmd + cache->rq_size;
1196 cache->rq_size += len;
1197
1198 return cmd;
1199}
1200
Chris Wilson2889caa2017-06-16 15:05:19 +01001201static u64
1202relocate_entry(struct i915_vma *vma,
Chris Wilsond50415c2016-08-18 17:16:52 +01001203 const struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson2889caa2017-06-16 15:05:19 +01001204 struct i915_execbuffer *eb,
1205 const struct i915_vma *target)
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001206{
Chris Wilsond50415c2016-08-18 17:16:52 +01001207 u64 offset = reloc->offset;
Chris Wilson2889caa2017-06-16 15:05:19 +01001208 u64 target_offset = relocation_target(reloc, target);
1209 bool wide = eb->reloc_cache.use_64bit_reloc;
Chris Wilsond50415c2016-08-18 17:16:52 +01001210 void *vaddr;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001211
Chris Wilson7dd4f672017-06-16 15:05:24 +01001212 if (!eb->reloc_cache.vaddr &&
1213 (DBG_FORCE_RELOC == FORCE_GPU_RELOC ||
Chris Wilsonf2f5c062017-08-16 09:52:04 +01001214 !reservation_object_test_signaled_rcu(vma->resv, true)) &&
1215 __intel_engine_can_store_dword(eb->reloc_cache.gen,
1216 eb->engine->class)) {
Chris Wilson7dd4f672017-06-16 15:05:24 +01001217 const unsigned int gen = eb->reloc_cache.gen;
1218 unsigned int len;
1219 u32 *batch;
1220 u64 addr;
1221
1222 if (wide)
1223 len = offset & 7 ? 8 : 5;
1224 else if (gen >= 4)
1225 len = 4;
Chris Wilsonf2f5c062017-08-16 09:52:04 +01001226 else
Chris Wilson7dd4f672017-06-16 15:05:24 +01001227 len = 3;
Chris Wilson7dd4f672017-06-16 15:05:24 +01001228
1229 batch = reloc_gpu(eb, vma, len);
1230 if (IS_ERR(batch))
1231 goto repeat;
1232
1233 addr = gen8_canonical_addr(vma->node.start + offset);
1234 if (wide) {
1235 if (offset & 7) {
1236 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1237 *batch++ = lower_32_bits(addr);
1238 *batch++ = upper_32_bits(addr);
1239 *batch++ = lower_32_bits(target_offset);
1240
1241 addr = gen8_canonical_addr(addr + 4);
1242
1243 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1244 *batch++ = lower_32_bits(addr);
1245 *batch++ = upper_32_bits(addr);
1246 *batch++ = upper_32_bits(target_offset);
1247 } else {
1248 *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1;
1249 *batch++ = lower_32_bits(addr);
1250 *batch++ = upper_32_bits(addr);
1251 *batch++ = lower_32_bits(target_offset);
1252 *batch++ = upper_32_bits(target_offset);
1253 }
1254 } else if (gen >= 6) {
1255 *batch++ = MI_STORE_DWORD_IMM_GEN4;
1256 *batch++ = 0;
1257 *batch++ = addr;
1258 *batch++ = target_offset;
1259 } else if (gen >= 4) {
1260 *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1261 *batch++ = 0;
1262 *batch++ = addr;
1263 *batch++ = target_offset;
1264 } else {
1265 *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
1266 *batch++ = addr;
1267 *batch++ = target_offset;
1268 }
1269
1270 goto out;
1271 }
1272
Chris Wilsond50415c2016-08-18 17:16:52 +01001273repeat:
Chris Wilson95ff7c72017-06-16 15:05:25 +01001274 vaddr = reloc_vaddr(vma->obj, &eb->reloc_cache, offset >> PAGE_SHIFT);
Chris Wilsond50415c2016-08-18 17:16:52 +01001275 if (IS_ERR(vaddr))
1276 return PTR_ERR(vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001277
Chris Wilsond50415c2016-08-18 17:16:52 +01001278 clflush_write32(vaddr + offset_in_page(offset),
1279 lower_32_bits(target_offset),
Chris Wilson2889caa2017-06-16 15:05:19 +01001280 eb->reloc_cache.vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001281
Chris Wilsond50415c2016-08-18 17:16:52 +01001282 if (wide) {
1283 offset += sizeof(u32);
1284 target_offset >>= 32;
1285 wide = false;
1286 goto repeat;
Chris Wilsonedf4427b2015-01-14 11:20:56 +00001287 }
Chris Wilsondabdfe02012-03-26 10:10:27 +02001288
Chris Wilson7dd4f672017-06-16 15:05:24 +01001289out:
Chris Wilson2889caa2017-06-16 15:05:19 +01001290 return target->node.start | UPDATE;
Rafael Barbalho5032d872013-08-21 17:10:51 +01001291}
1292
Chris Wilson2889caa2017-06-16 15:05:19 +01001293static u64
1294eb_relocate_entry(struct i915_execbuffer *eb,
1295 struct i915_vma *vma,
1296 const struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001297{
Chris Wilson507d9772017-06-16 15:05:17 +01001298 struct i915_vma *target;
Chris Wilson2889caa2017-06-16 15:05:19 +01001299 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001300
Chris Wilson67731b82010-12-08 10:38:14 +00001301 /* we've already hold a reference to all valid objects */
Chris Wilson507d9772017-06-16 15:05:17 +01001302 target = eb_get_vma(eb, reloc->target_handle);
1303 if (unlikely(!target))
Chris Wilson54cf91d2010-11-25 18:00:26 +00001304 return -ENOENT;
Eric Anholte844b992012-07-31 15:35:01 -07001305
Chris Wilson54cf91d2010-11-25 18:00:26 +00001306 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001307 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001308 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001309 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001310 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001311 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001312 (int) reloc->offset,
1313 reloc->read_domains,
1314 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001315 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001316 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +01001317 if (unlikely((reloc->write_domain | reloc->read_domains)
1318 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001319 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson507d9772017-06-16 15:05:17 +01001320 "target %d offset %d "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001321 "read %08x write %08x",
Chris Wilson507d9772017-06-16 15:05:17 +01001322 reloc->target_handle,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001323 (int) reloc->offset,
1324 reloc->read_domains,
1325 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001326 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001327 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001328
Chris Wilson2889caa2017-06-16 15:05:19 +01001329 if (reloc->write_domain) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001330 *target->exec_flags |= EXEC_OBJECT_WRITE;
Chris Wilson507d9772017-06-16 15:05:17 +01001331
Chris Wilson2889caa2017-06-16 15:05:19 +01001332 /*
1333 * Sandybridge PPGTT errata: We need a global gtt mapping
1334 * for MI and pipe_control writes because the gpu doesn't
1335 * properly redirect them through the ppgtt for non_secure
1336 * batchbuffers.
1337 */
1338 if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
1339 IS_GEN6(eb->i915)) {
1340 err = i915_vma_bind(target, target->obj->cache_level,
1341 PIN_GLOBAL);
1342 if (WARN_ONCE(err,
1343 "Unexpected failure to bind target VMA!"))
1344 return err;
1345 }
Chris Wilson507d9772017-06-16 15:05:17 +01001346 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001347
Chris Wilson2889caa2017-06-16 15:05:19 +01001348 /*
1349 * If the relocation already has the right value in it, no
Chris Wilson54cf91d2010-11-25 18:00:26 +00001350 * more work needs to be done.
1351 */
Chris Wilson7dd4f672017-06-16 15:05:24 +01001352 if (!DBG_FORCE_RELOC &&
1353 gen8_canonical_addr(target->node.start) == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +00001354 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001355
1356 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -07001357 if (unlikely(reloc->offset >
Chris Wilson507d9772017-06-16 15:05:17 +01001358 vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +01001359 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson507d9772017-06-16 15:05:17 +01001360 "target %d offset %d size %d.\n",
1361 reloc->target_handle,
1362 (int)reloc->offset,
1363 (int)vma->size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001364 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001365 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +00001366 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001367 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson507d9772017-06-16 15:05:17 +01001368 "target %d offset %d.\n",
1369 reloc->target_handle,
1370 (int)reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -08001371 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001372 }
1373
Chris Wilson071750e2017-06-16 15:05:18 +01001374 /*
1375 * If we write into the object, we need to force the synchronisation
1376 * barrier, either with an asynchronous clflush or if we executed the
1377 * patching using the GPU (though that should be serialised by the
1378 * timeline). To be completely sure, and since we are required to
1379 * do relocations we are already stalling, disable the user's opt
1380 * of our synchronisation.
1381 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001382 *vma->exec_flags &= ~EXEC_OBJECT_ASYNC;
Chris Wilson071750e2017-06-16 15:05:18 +01001383
Chris Wilson54cf91d2010-11-25 18:00:26 +00001384 /* and update the user's relocation entry */
Chris Wilson2889caa2017-06-16 15:05:19 +01001385 return relocate_entry(vma, reloc, eb, target);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001386}
1387
Chris Wilson2889caa2017-06-16 15:05:19 +01001388static int eb_relocate_vma(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001389{
Chris Wilson1d83f442012-03-24 20:12:53 +00001390#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
Chris Wilson2889caa2017-06-16 15:05:19 +01001391 struct drm_i915_gem_relocation_entry stack[N_RELOC(512)];
1392 struct drm_i915_gem_relocation_entry __user *urelocs;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001393 const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001394 unsigned int remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001395
Chris Wilson2889caa2017-06-16 15:05:19 +01001396 urelocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson1d83f442012-03-24 20:12:53 +00001397 remain = entry->relocation_count;
Chris Wilson2889caa2017-06-16 15:05:19 +01001398 if (unlikely(remain > N_RELOC(ULONG_MAX)))
1399 return -EINVAL;
Chris Wilsonebc08082016-10-18 13:02:51 +01001400
Chris Wilson2889caa2017-06-16 15:05:19 +01001401 /*
1402 * We must check that the entire relocation array is safe
1403 * to read. However, if the array is not writable the user loses
1404 * the updated relocation values.
1405 */
Imre Deakedd90032017-07-14 18:12:42 +03001406 if (unlikely(!access_ok(VERIFY_READ, urelocs, remain*sizeof(*urelocs))))
Chris Wilson2889caa2017-06-16 15:05:19 +01001407 return -EFAULT;
Chris Wilson1d83f442012-03-24 20:12:53 +00001408
Chris Wilson2889caa2017-06-16 15:05:19 +01001409 do {
1410 struct drm_i915_gem_relocation_entry *r = stack;
1411 unsigned int count =
1412 min_t(unsigned int, remain, ARRAY_SIZE(stack));
1413 unsigned int copied;
1414
1415 /*
1416 * This is the fast path and we cannot handle a pagefault
Chris Wilsonebc08082016-10-18 13:02:51 +01001417 * whilst holding the struct mutex lest the user pass in the
1418 * relocations contained within a mmaped bo. For in such a case
1419 * we, the page fault handler would call i915_gem_fault() and
1420 * we would try to acquire the struct mutex again. Obviously
1421 * this is bad and so lockdep complains vehemently.
1422 */
1423 pagefault_disable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001424 copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0]));
Chris Wilsonebc08082016-10-18 13:02:51 +01001425 pagefault_enable();
Chris Wilson2889caa2017-06-16 15:05:19 +01001426 if (unlikely(copied)) {
1427 remain = -EFAULT;
Chris Wilson31a39202016-08-18 17:16:46 +01001428 goto out;
1429 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001430
Chris Wilson2889caa2017-06-16 15:05:19 +01001431 remain -= count;
Chris Wilson1d83f442012-03-24 20:12:53 +00001432 do {
Chris Wilson2889caa2017-06-16 15:05:19 +01001433 u64 offset = eb_relocate_entry(eb, vma, r);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001434
Chris Wilson2889caa2017-06-16 15:05:19 +01001435 if (likely(offset == 0)) {
1436 } else if ((s64)offset < 0) {
1437 remain = (int)offset;
Chris Wilson31a39202016-08-18 17:16:46 +01001438 goto out;
Chris Wilson2889caa2017-06-16 15:05:19 +01001439 } else {
1440 /*
1441 * Note that reporting an error now
1442 * leaves everything in an inconsistent
1443 * state as we have *already* changed
1444 * the relocation value inside the
1445 * object. As we have not changed the
1446 * reloc.presumed_offset or will not
1447 * change the execobject.offset, on the
1448 * call we may not rewrite the value
1449 * inside the object, leaving it
1450 * dangling and causing a GPU hang. Unless
1451 * userspace dynamically rebuilds the
1452 * relocations on each execbuf rather than
1453 * presume a static tree.
1454 *
1455 * We did previously check if the relocations
1456 * were writable (access_ok), an error now
1457 * would be a strange race with mprotect,
1458 * having already demonstrated that we
1459 * can read from this userspace address.
1460 */
1461 offset = gen8_canonical_addr(offset & ~UPDATE);
1462 __put_user(offset,
1463 &urelocs[r-stack].presumed_offset);
Chris Wilson1d83f442012-03-24 20:12:53 +00001464 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001465 } while (r++, --count);
1466 urelocs += ARRAY_SIZE(stack);
1467 } while (remain);
Chris Wilson31a39202016-08-18 17:16:46 +01001468out:
Chris Wilson650bc632017-06-15 09:14:33 +01001469 reloc_cache_reset(&eb->reloc_cache);
Chris Wilson2889caa2017-06-16 15:05:19 +01001470 return remain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001471}
1472
1473static int
Chris Wilson2889caa2017-06-16 15:05:19 +01001474eb_relocate_vma_slow(struct i915_execbuffer *eb, struct i915_vma *vma)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001475{
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001476 const struct drm_i915_gem_exec_object2 *entry = exec_entry(eb, vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001477 struct drm_i915_gem_relocation_entry *relocs =
1478 u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1479 unsigned int i;
1480 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001481
1482 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001483 u64 offset = eb_relocate_entry(eb, vma, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001484
Chris Wilson2889caa2017-06-16 15:05:19 +01001485 if ((s64)offset < 0) {
1486 err = (int)offset;
1487 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001488 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001489 }
1490 err = 0;
Chris Wilsona415d352013-11-26 11:23:15 +00001491err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001492 reloc_cache_reset(&eb->reloc_cache);
1493 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001494}
1495
Chris Wilson2889caa2017-06-16 15:05:19 +01001496static int check_relocations(const struct drm_i915_gem_exec_object2 *entry)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001497{
Chris Wilson2889caa2017-06-16 15:05:19 +01001498 const char __user *addr, *end;
1499 unsigned long size;
1500 char __maybe_unused c;
Ben Widawsky27173f12013-08-14 11:38:36 +02001501
Chris Wilson2889caa2017-06-16 15:05:19 +01001502 size = entry->relocation_count;
1503 if (size == 0)
1504 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001505
Chris Wilson2889caa2017-06-16 15:05:19 +01001506 if (size > N_RELOC(ULONG_MAX))
1507 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001508
Chris Wilson2889caa2017-06-16 15:05:19 +01001509 addr = u64_to_user_ptr(entry->relocs_ptr);
1510 size *= sizeof(struct drm_i915_gem_relocation_entry);
1511 if (!access_ok(VERIFY_READ, addr, size))
1512 return -EFAULT;
1513
1514 end = addr + size;
1515 for (; addr < end; addr += PAGE_SIZE) {
1516 int err = __get_user(c, addr);
1517 if (err)
1518 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001519 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001520 return __get_user(c, end - 1);
1521}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001522
Chris Wilson2889caa2017-06-16 15:05:19 +01001523static int eb_copy_relocations(const struct i915_execbuffer *eb)
1524{
1525 const unsigned int count = eb->buffer_count;
1526 unsigned int i;
1527 int err;
1528
Chris Wilson54cf91d2010-11-25 18:00:26 +00001529 for (i = 0; i < count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001530 const unsigned int nreloc = eb->exec[i].relocation_count;
1531 struct drm_i915_gem_relocation_entry __user *urelocs;
1532 struct drm_i915_gem_relocation_entry *relocs;
1533 unsigned long size;
1534 unsigned long copied;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001535
Chris Wilson2889caa2017-06-16 15:05:19 +01001536 if (nreloc == 0)
1537 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001538
Chris Wilson2889caa2017-06-16 15:05:19 +01001539 err = check_relocations(&eb->exec[i]);
1540 if (err)
1541 goto err;
1542
1543 urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
1544 size = nreloc * sizeof(*relocs);
1545
1546 relocs = kvmalloc_array(size, 1, GFP_TEMPORARY);
1547 if (!relocs) {
1548 kvfree(relocs);
1549 err = -ENOMEM;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001550 goto err;
1551 }
1552
Chris Wilson2889caa2017-06-16 15:05:19 +01001553 /* copy_from_user is limited to < 4GiB */
1554 copied = 0;
1555 do {
1556 unsigned int len =
1557 min_t(u64, BIT_ULL(31), size - copied);
1558
1559 if (__copy_from_user((char *)relocs + copied,
1560 (char *)urelocs + copied,
1561 len)) {
1562 kvfree(relocs);
1563 err = -EFAULT;
1564 goto err;
1565 }
1566
1567 copied += len;
1568 } while (copied < size);
1569
1570 /*
1571 * As we do not update the known relocation offsets after
Chris Wilson262b6d32013-01-15 16:17:54 +00001572 * relocating (due to the complexities in lock handling),
1573 * we need to mark them as invalid now so that we force the
1574 * relocation processing next time. Just in case the target
1575 * object is evicted and then rebound into its old
1576 * presumed_offset before the next execbuffer - if that
1577 * happened we would make the mistake of assuming that the
1578 * relocations were valid.
1579 */
Chris Wilson2889caa2017-06-16 15:05:19 +01001580 user_access_begin();
1581 for (copied = 0; copied < nreloc; copied++)
1582 unsafe_put_user(-1,
1583 &urelocs[copied].presumed_offset,
1584 end_user);
1585end_user:
1586 user_access_end();
Chris Wilson262b6d32013-01-15 16:17:54 +00001587
Chris Wilson2889caa2017-06-16 15:05:19 +01001588 eb->exec[i].relocs_ptr = (uintptr_t)relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001589 }
1590
Chris Wilson2889caa2017-06-16 15:05:19 +01001591 return 0;
1592
1593err:
1594 while (i--) {
1595 struct drm_i915_gem_relocation_entry *relocs =
1596 u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr);
1597 if (eb->exec[i].relocation_count)
1598 kvfree(relocs);
1599 }
1600 return err;
1601}
1602
1603static int eb_prefault_relocations(const struct i915_execbuffer *eb)
1604{
1605 const unsigned int count = eb->buffer_count;
1606 unsigned int i;
1607
1608 if (unlikely(i915.prefault_disable))
1609 return 0;
1610
1611 for (i = 0; i < count; i++) {
1612 int err;
1613
1614 err = check_relocations(&eb->exec[i]);
1615 if (err)
1616 return err;
1617 }
1618
1619 return 0;
1620}
1621
1622static noinline int eb_relocate_slow(struct i915_execbuffer *eb)
1623{
1624 struct drm_device *dev = &eb->i915->drm;
1625 bool have_copy = false;
1626 struct i915_vma *vma;
1627 int err = 0;
1628
1629repeat:
1630 if (signal_pending(current)) {
1631 err = -ERESTARTSYS;
1632 goto out;
1633 }
1634
1635 /* We may process another execbuffer during the unlock... */
1636 eb_reset_vmas(eb);
1637 mutex_unlock(&dev->struct_mutex);
1638
1639 /*
1640 * We take 3 passes through the slowpatch.
1641 *
1642 * 1 - we try to just prefault all the user relocation entries and
1643 * then attempt to reuse the atomic pagefault disabled fast path again.
1644 *
1645 * 2 - we copy the user entries to a local buffer here outside of the
1646 * local and allow ourselves to wait upon any rendering before
1647 * relocations
1648 *
1649 * 3 - we already have a local copy of the relocation entries, but
1650 * were interrupted (EAGAIN) whilst waiting for the objects, try again.
1651 */
1652 if (!err) {
1653 err = eb_prefault_relocations(eb);
1654 } else if (!have_copy) {
1655 err = eb_copy_relocations(eb);
1656 have_copy = err == 0;
1657 } else {
1658 cond_resched();
1659 err = 0;
1660 }
1661 if (err) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001662 mutex_lock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01001663 goto out;
1664 }
1665
Chris Wilson8a2421b2017-06-16 15:05:22 +01001666 /* A frequent cause for EAGAIN are currently unavailable client pages */
1667 flush_workqueue(eb->i915->mm.userptr_wq);
1668
Chris Wilson2889caa2017-06-16 15:05:19 +01001669 err = i915_mutex_lock_interruptible(dev);
1670 if (err) {
1671 mutex_lock(&dev->struct_mutex);
1672 goto out;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001673 }
1674
Chris Wilson67731b82010-12-08 10:38:14 +00001675 /* reacquire the objects */
Chris Wilson2889caa2017-06-16 15:05:19 +01001676 err = eb_lookup_vmas(eb);
1677 if (err)
Chris Wilson3b96eff2013-01-08 10:53:14 +00001678 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +00001679
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001680 GEM_BUG_ON(!eb->batch);
1681
Chris Wilson2889caa2017-06-16 15:05:19 +01001682 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1683 if (!have_copy) {
1684 pagefault_disable();
1685 err = eb_relocate_vma(eb, vma);
1686 pagefault_enable();
1687 if (err)
1688 goto repeat;
1689 } else {
1690 err = eb_relocate_vma_slow(eb, vma);
1691 if (err)
1692 goto err;
1693 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001694 }
1695
Chris Wilson2889caa2017-06-16 15:05:19 +01001696 /*
1697 * Leave the user relocations as are, this is the painfully slow path,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001698 * and we want to avoid the complication of dropping the lock whilst
1699 * having buffers reserved in the aperture and so causing spurious
1700 * ENOSPC for random operations.
1701 */
1702
1703err:
Chris Wilson2889caa2017-06-16 15:05:19 +01001704 if (err == -EAGAIN)
1705 goto repeat;
1706
1707out:
1708 if (have_copy) {
1709 const unsigned int count = eb->buffer_count;
1710 unsigned int i;
1711
1712 for (i = 0; i < count; i++) {
1713 const struct drm_i915_gem_exec_object2 *entry =
1714 &eb->exec[i];
1715 struct drm_i915_gem_relocation_entry *relocs;
1716
1717 if (!entry->relocation_count)
1718 continue;
1719
1720 relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr);
1721 kvfree(relocs);
1722 }
1723 }
1724
Chris Wilson1f727d92017-07-21 15:50:36 +01001725 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001726}
1727
Chris Wilson2889caa2017-06-16 15:05:19 +01001728static int eb_relocate(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001729{
Chris Wilson2889caa2017-06-16 15:05:19 +01001730 if (eb_lookup_vmas(eb))
1731 goto slow;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001732
Chris Wilson2889caa2017-06-16 15:05:19 +01001733 /* The objects are in their final locations, apply the relocations. */
1734 if (eb->args->flags & __EXEC_HAS_RELOC) {
1735 struct i915_vma *vma;
1736
1737 list_for_each_entry(vma, &eb->relocs, reloc_link) {
1738 if (eb_relocate_vma(eb, vma))
1739 goto slow;
1740 }
1741 }
1742
1743 return 0;
1744
1745slow:
1746 return eb_relocate_slow(eb);
1747}
1748
Chris Wilson95ff7c72017-06-16 15:05:25 +01001749static void eb_export_fence(struct i915_vma *vma,
Chris Wilson2889caa2017-06-16 15:05:19 +01001750 struct drm_i915_gem_request *req,
1751 unsigned int flags)
1752{
Chris Wilson95ff7c72017-06-16 15:05:25 +01001753 struct reservation_object *resv = vma->resv;
Chris Wilson2889caa2017-06-16 15:05:19 +01001754
1755 /*
1756 * Ignore errors from failing to allocate the new fence, we can't
1757 * handle an error right now. Worst case should be missed
1758 * synchronisation leading to rendering corruption.
1759 */
1760 reservation_object_lock(resv, NULL);
1761 if (flags & EXEC_OBJECT_WRITE)
1762 reservation_object_add_excl_fence(resv, &req->fence);
1763 else if (reservation_object_reserve_shared(resv) == 0)
1764 reservation_object_add_shared_fence(resv, &req->fence);
1765 reservation_object_unlock(resv);
1766}
1767
1768static int eb_move_to_gpu(struct i915_execbuffer *eb)
1769{
1770 const unsigned int count = eb->buffer_count;
1771 unsigned int i;
1772 int err;
1773
1774 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001775 unsigned int flags = eb->flags[i];
1776 struct i915_vma *vma = eb->vma[i];
Ben Widawsky27173f12013-08-14 11:38:36 +02001777 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001778
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001779 if (flags & EXEC_OBJECT_CAPTURE) {
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001780 struct i915_gem_capture_list *capture;
1781
1782 capture = kmalloc(sizeof(*capture), GFP_KERNEL);
1783 if (unlikely(!capture))
1784 return -ENOMEM;
1785
Chris Wilson650bc632017-06-15 09:14:33 +01001786 capture->next = eb->request->capture_list;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001787 capture->vma = eb->vma[i];
Chris Wilson650bc632017-06-15 09:14:33 +01001788 eb->request->capture_list = capture;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001789 }
1790
Chris Wilsonb8f55be2017-08-11 12:11:16 +01001791 /*
1792 * If the GPU is not _reading_ through the CPU cache, we need
1793 * to make sure that any writes (both previous GPU writes from
1794 * before a change in snooping levels and normal CPU writes)
1795 * caught in that cache are flushed to main memory.
1796 *
1797 * We want to say
1798 * obj->cache_dirty &&
1799 * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
1800 * but gcc's optimiser doesn't handle that as well and emits
1801 * two jumps instead of one. Maybe one day...
1802 */
1803 if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) {
Chris Wilson0f46daa2017-07-21 15:50:37 +01001804 if (i915_gem_clflush_object(obj, 0))
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001805 flags &= ~EXEC_OBJECT_ASYNC;
Chris Wilson0f46daa2017-07-21 15:50:37 +01001806 }
1807
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001808 if (flags & EXEC_OBJECT_ASYNC)
1809 continue;
Chris Wilson77ae9952017-01-27 09:40:07 +00001810
Chris Wilson2889caa2017-06-16 15:05:19 +01001811 err = i915_gem_request_await_object
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001812 (eb->request, obj, flags & EXEC_OBJECT_WRITE);
Chris Wilson2889caa2017-06-16 15:05:19 +01001813 if (err)
1814 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001815 }
1816
Chris Wilson2889caa2017-06-16 15:05:19 +01001817 for (i = 0; i < count; i++) {
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001818 unsigned int flags = eb->flags[i];
1819 struct i915_vma *vma = eb->vma[i];
Chris Wilson2889caa2017-06-16 15:05:19 +01001820
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001821 i915_vma_move_to_active(vma, eb->request, flags);
1822 eb_export_fence(vma, eb->request, flags);
1823
1824 __eb_unreserve_vma(vma, flags);
1825 vma->exec_flags = NULL;
1826
1827 if (unlikely(flags & __EXEC_OBJECT_HAS_REF))
Chris Wilsondade2a62017-06-16 15:05:20 +01001828 i915_vma_put(vma);
Chris Wilson2889caa2017-06-16 15:05:19 +01001829 }
1830 eb->exec = NULL;
1831
Chris Wilsondcd79932016-08-18 17:16:40 +01001832 /* Unconditionally flush any chipset caches (for streaming writes). */
Chris Wilson650bc632017-06-15 09:14:33 +01001833 i915_gem_chipset_flush(eb->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001834
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001835 /* Unconditionally invalidate GPU caches and TLBs. */
Chris Wilson650bc632017-06-15 09:14:33 +01001836 return eb->engine->emit_flush(eb->request, EMIT_INVALIDATE);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001837}
1838
Chris Wilson2889caa2017-06-16 15:05:19 +01001839static bool i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001840{
Chris Wilson650bc632017-06-15 09:14:33 +01001841 if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
Daniel Vettered5982e2013-01-17 22:23:36 +01001842 return false;
1843
Chris Wilson2f5945b2015-10-06 11:39:55 +01001844 /* Kernel clipping was a DRI1 misfeature */
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01001845 if (!(exec->flags & I915_EXEC_FENCE_ARRAY)) {
1846 if (exec->num_cliprects || exec->cliprects_ptr)
1847 return false;
1848 }
Chris Wilson2f5945b2015-10-06 11:39:55 +01001849
1850 if (exec->DR4 == 0xffffffff) {
1851 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1852 exec->DR4 = 0;
1853 }
1854 if (exec->DR1 || exec->DR4)
1855 return false;
1856
1857 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1858 return false;
1859
1860 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001861}
1862
Chris Wilson5cf3d282016-08-04 07:52:43 +01001863void i915_vma_move_to_active(struct i915_vma *vma,
1864 struct drm_i915_gem_request *req,
1865 unsigned int flags)
1866{
1867 struct drm_i915_gem_object *obj = vma->obj;
1868 const unsigned int idx = req->engine->id;
1869
Chris Wilson81147b02016-12-18 15:37:18 +00001870 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001871 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1872
Chris Wilson2889caa2017-06-16 15:05:19 +01001873 /*
1874 * Add a reference if we're newly entering the active list.
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001875 * The order in which we add operations to the retirement queue is
1876 * vital here: mark_active adds to the start of the callback list,
1877 * such that subsequent callbacks are called first. Therefore we
1878 * add the active reference first and queue for it to be dropped
1879 * *last*.
1880 */
Chris Wilsond07f0e52016-10-28 13:58:44 +01001881 if (!i915_vma_is_active(vma))
1882 obj->active_count++;
1883 i915_vma_set_active(vma, idx);
1884 i915_gem_active_set(&vma->last_read[idx], req);
1885 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001886
Chris Wilsone27ab732017-06-15 13:38:49 +01001887 obj->base.write_domain = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001888 if (flags & EXEC_OBJECT_WRITE) {
Chris Wilsone27ab732017-06-15 13:38:49 +01001889 obj->base.write_domain = I915_GEM_DOMAIN_RENDER;
1890
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00001891 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
1892 i915_gem_active_set(&obj->frontbuffer_write, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001893
Chris Wilsone27ab732017-06-15 13:38:49 +01001894 obj->base.read_domains = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001895 }
Chris Wilsone27ab732017-06-15 13:38:49 +01001896 obj->base.read_domains |= I915_GEM_GPU_DOMAINS;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001897
Chris Wilson49ef5292016-08-18 17:17:00 +01001898 if (flags & EXEC_OBJECT_NEEDS_FENCE)
1899 i915_gem_active_set(&vma->last_fence, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001900}
1901
Chris Wilson2889caa2017-06-16 15:05:19 +01001902static int i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001903{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001904 u32 *cs;
1905 int i;
Eric Anholtae662d32012-01-03 09:23:29 -08001906
Chris Wilsonb5321f32016-08-02 22:50:18 +01001907 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001908 DRM_DEBUG("sol reset is gen7/rcs only\n");
1909 return -EINVAL;
1910 }
Eric Anholtae662d32012-01-03 09:23:29 -08001911
Chris Wilson2889caa2017-06-16 15:05:19 +01001912 cs = intel_ring_begin(req, 4 * 2 + 2);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001913 if (IS_ERR(cs))
1914 return PTR_ERR(cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001915
Chris Wilson2889caa2017-06-16 15:05:19 +01001916 *cs++ = MI_LOAD_REGISTER_IMM(4);
Eric Anholtae662d32012-01-03 09:23:29 -08001917 for (i = 0; i < 4; i++) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001918 *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
1919 *cs++ = 0;
Eric Anholtae662d32012-01-03 09:23:29 -08001920 }
Chris Wilson2889caa2017-06-16 15:05:19 +01001921 *cs++ = MI_NOOP;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001922 intel_ring_advance(req, cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001923
1924 return 0;
1925}
1926
Chris Wilson650bc632017-06-15 09:14:33 +01001927static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001928{
Brad Volkin71745372014-12-11 12:13:12 -08001929 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001930 struct i915_vma *vma;
Chris Wilson2889caa2017-06-16 15:05:19 +01001931 int err;
Brad Volkin71745372014-12-11 12:13:12 -08001932
Chris Wilson650bc632017-06-15 09:14:33 +01001933 shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool,
1934 PAGE_ALIGN(eb->batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001935 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001936 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001937
Chris Wilson2889caa2017-06-16 15:05:19 +01001938 err = intel_engine_cmd_parser(eb->engine,
Chris Wilson650bc632017-06-15 09:14:33 +01001939 eb->batch->obj,
Chris Wilson33a051a2016-07-27 09:07:26 +01001940 shadow_batch_obj,
Chris Wilson650bc632017-06-15 09:14:33 +01001941 eb->batch_start_offset,
1942 eb->batch_len,
Chris Wilson33a051a2016-07-27 09:07:26 +01001943 is_master);
Chris Wilson2889caa2017-06-16 15:05:19 +01001944 if (err) {
1945 if (err == -EACCES) /* unhandled chained batch */
Chris Wilson058d88c2016-08-15 10:49:06 +01001946 vma = NULL;
1947 else
Chris Wilson2889caa2017-06-16 15:05:19 +01001948 vma = ERR_PTR(err);
Chris Wilson058d88c2016-08-15 10:49:06 +01001949 goto out;
1950 }
Brad Volkin71745372014-12-11 12:13:12 -08001951
Chris Wilson058d88c2016-08-15 10:49:06 +01001952 vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
1953 if (IS_ERR(vma))
1954 goto out;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001955
Chris Wilsonc7c6e462017-08-16 09:52:06 +01001956 eb->vma[eb->buffer_count] = i915_vma_get(vma);
1957 eb->flags[eb->buffer_count] =
1958 __EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_REF;
1959 vma->exec_flags = &eb->flags[eb->buffer_count];
1960 eb->buffer_count++;
Brad Volkin71745372014-12-11 12:13:12 -08001961
Chris Wilson058d88c2016-08-15 10:49:06 +01001962out:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001963 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson058d88c2016-08-15 10:49:06 +01001964 return vma;
Brad Volkin71745372014-12-11 12:13:12 -08001965}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001966
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001967static void
Chris Wilson2889caa2017-06-16 15:05:19 +01001968add_to_client(struct drm_i915_gem_request *req, struct drm_file *file)
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001969{
1970 req->file_priv = file->driver_priv;
1971 list_add_tail(&req->client_link, &req->file_priv->mm.request_list);
1972}
1973
Chris Wilson2889caa2017-06-16 15:05:19 +01001974static int eb_submit(struct i915_execbuffer *eb)
Oscar Mateo78382592014-07-03 16:28:05 +01001975{
Chris Wilson2889caa2017-06-16 15:05:19 +01001976 int err;
Oscar Mateo78382592014-07-03 16:28:05 +01001977
Chris Wilson2889caa2017-06-16 15:05:19 +01001978 err = eb_move_to_gpu(eb);
1979 if (err)
1980 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001981
Chris Wilson2889caa2017-06-16 15:05:19 +01001982 err = i915_switch_context(eb->request);
1983 if (err)
1984 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001985
Chris Wilson650bc632017-06-15 09:14:33 +01001986 if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilson2889caa2017-06-16 15:05:19 +01001987 err = i915_reset_gen7_sol_offsets(eb->request);
1988 if (err)
1989 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001990 }
1991
Chris Wilson2889caa2017-06-16 15:05:19 +01001992 err = eb->engine->emit_bb_start(eb->request,
Chris Wilson650bc632017-06-15 09:14:33 +01001993 eb->batch->node.start +
1994 eb->batch_start_offset,
1995 eb->batch_len,
Chris Wilson2889caa2017-06-16 15:05:19 +01001996 eb->batch_flags);
1997 if (err)
1998 return err;
Oscar Mateo78382592014-07-03 16:28:05 +01001999
Chris Wilson2f5945b2015-10-06 11:39:55 +01002000 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01002001}
2002
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002003/**
2004 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01002005 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002006 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002007static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01002008gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
2009 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002010{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08002011 struct drm_i915_file_private *file_priv = file->driver_priv;
2012
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002013 /* Check whether the file_priv has already selected one ring. */
Joonas Lahtinen6f633402016-09-01 14:58:21 +03002014 if ((int)file_priv->bsd_engine < 0)
2015 file_priv->bsd_engine = atomic_fetch_xor(1,
2016 &dev_priv->mm.bsd_engine_dispatch_index);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002017
Chris Wilsonc80ff162016-07-27 09:07:27 +01002018 return file_priv->bsd_engine;
Chris Wilsond23db882014-05-23 08:48:08 +02002019}
2020
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002021#define I915_USER_RINGS (4)
2022
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002023static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002024 [I915_EXEC_DEFAULT] = RCS,
2025 [I915_EXEC_RENDER] = RCS,
2026 [I915_EXEC_BLT] = BCS,
2027 [I915_EXEC_BSD] = VCS,
2028 [I915_EXEC_VEBOX] = VECS
2029};
2030
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002031static struct intel_engine_cs *
2032eb_select_engine(struct drm_i915_private *dev_priv,
2033 struct drm_file *file,
2034 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002035{
2036 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002037 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002038
2039 if (user_ring_id > I915_USER_RINGS) {
2040 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002041 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002042 }
2043
2044 if ((user_ring_id != I915_EXEC_BSD) &&
2045 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
2046 DRM_DEBUG("execbuf with non bsd ring but with invalid "
2047 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002048 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002049 }
2050
2051 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
2052 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
2053
2054 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01002055 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002056 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
2057 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00002058 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002059 bsd_idx--;
2060 } else {
2061 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
2062 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002063 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002064 }
2065
Akash Goel3b3f1652016-10-13 22:44:48 +05302066 engine = dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002067 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +05302068 engine = dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002069 }
2070
Akash Goel3b3f1652016-10-13 22:44:48 +05302071 if (!engine) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002072 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002073 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002074 }
2075
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002076 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00002077}
2078
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002079static void
2080__free_fence_array(struct drm_syncobj **fences, unsigned int n)
2081{
2082 while (n--)
2083 drm_syncobj_put(ptr_mask_bits(fences[n], 2));
2084 kvfree(fences);
2085}
2086
2087static struct drm_syncobj **
2088get_fence_array(struct drm_i915_gem_execbuffer2 *args,
2089 struct drm_file *file)
2090{
2091 const unsigned int nfences = args->num_cliprects;
2092 struct drm_i915_gem_exec_fence __user *user;
2093 struct drm_syncobj **fences;
2094 unsigned int n;
2095 int err;
2096
2097 if (!(args->flags & I915_EXEC_FENCE_ARRAY))
2098 return NULL;
2099
2100 if (nfences > SIZE_MAX / sizeof(*fences))
2101 return ERR_PTR(-EINVAL);
2102
2103 user = u64_to_user_ptr(args->cliprects_ptr);
2104 if (!access_ok(VERIFY_READ, user, nfences * 2 * sizeof(u32)))
2105 return ERR_PTR(-EFAULT);
2106
2107 fences = kvmalloc_array(args->num_cliprects, sizeof(*fences),
2108 __GFP_NOWARN | GFP_TEMPORARY);
2109 if (!fences)
2110 return ERR_PTR(-ENOMEM);
2111
2112 for (n = 0; n < nfences; n++) {
2113 struct drm_i915_gem_exec_fence fence;
2114 struct drm_syncobj *syncobj;
2115
2116 if (__copy_from_user(&fence, user++, sizeof(fence))) {
2117 err = -EFAULT;
2118 goto err;
2119 }
2120
2121 syncobj = drm_syncobj_find(file, fence.handle);
2122 if (!syncobj) {
2123 DRM_DEBUG("Invalid syncobj handle provided\n");
2124 err = -ENOENT;
2125 goto err;
2126 }
2127
2128 fences[n] = ptr_pack_bits(syncobj, fence.flags, 2);
2129 }
2130
2131 return fences;
2132
2133err:
2134 __free_fence_array(fences, n);
2135 return ERR_PTR(err);
2136}
2137
2138static void
2139put_fence_array(struct drm_i915_gem_execbuffer2 *args,
2140 struct drm_syncobj **fences)
2141{
2142 if (fences)
2143 __free_fence_array(fences, args->num_cliprects);
2144}
2145
2146static int
2147await_fence_array(struct i915_execbuffer *eb,
2148 struct drm_syncobj **fences)
2149{
2150 const unsigned int nfences = eb->args->num_cliprects;
2151 unsigned int n;
2152 int err;
2153
2154 for (n = 0; n < nfences; n++) {
2155 struct drm_syncobj *syncobj;
2156 struct dma_fence *fence;
2157 unsigned int flags;
2158
2159 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2160 if (!(flags & I915_EXEC_FENCE_WAIT))
2161 continue;
2162
2163 rcu_read_lock();
2164 fence = dma_fence_get_rcu_safe(&syncobj->fence);
2165 rcu_read_unlock();
2166 if (!fence)
2167 return -EINVAL;
2168
2169 err = i915_gem_request_await_dma_fence(eb->request, fence);
2170 dma_fence_put(fence);
2171 if (err < 0)
2172 return err;
2173 }
2174
2175 return 0;
2176}
2177
2178static void
2179signal_fence_array(struct i915_execbuffer *eb,
2180 struct drm_syncobj **fences)
2181{
2182 const unsigned int nfences = eb->args->num_cliprects;
2183 struct dma_fence * const fence = &eb->request->fence;
2184 unsigned int n;
2185
2186 for (n = 0; n < nfences; n++) {
2187 struct drm_syncobj *syncobj;
2188 unsigned int flags;
2189
2190 syncobj = ptr_unpack_bits(fences[n], &flags, 2);
2191 if (!(flags & I915_EXEC_FENCE_SIGNAL))
2192 continue;
2193
2194 drm_syncobj_replace_fence(syncobj, fence);
2195 }
2196}
2197
Eric Anholtae662d32012-01-03 09:23:29 -08002198static int
Chris Wilson650bc632017-06-15 09:14:33 +01002199i915_gem_do_execbuffer(struct drm_device *dev,
Chris Wilson54cf91d2010-11-25 18:00:26 +00002200 struct drm_file *file,
2201 struct drm_i915_gem_execbuffer2 *args,
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002202 struct drm_i915_gem_exec_object2 *exec,
2203 struct drm_syncobj **fences)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002204{
Chris Wilson650bc632017-06-15 09:14:33 +01002205 struct i915_execbuffer eb;
Chris Wilsonfec04452017-01-27 09:40:08 +00002206 struct dma_fence *in_fence = NULL;
2207 struct sync_file *out_fence = NULL;
2208 int out_fence_fd = -1;
Chris Wilson2889caa2017-06-16 15:05:19 +01002209 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002210
Chris Wilson2889caa2017-06-16 15:05:19 +01002211 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS &
2212 ~__EXEC_OBJECT_UNKNOWN_FLAGS);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002213
Chris Wilson650bc632017-06-15 09:14:33 +01002214 eb.i915 = to_i915(dev);
2215 eb.file = file;
2216 eb.args = args;
Chris Wilson7dd4f672017-06-16 15:05:24 +01002217 if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC))
Chris Wilson2889caa2017-06-16 15:05:19 +01002218 args->flags |= __EXEC_HAS_RELOC;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002219
Chris Wilson650bc632017-06-15 09:14:33 +01002220 eb.exec = exec;
Chris Wilson170fa292017-08-16 09:52:07 +01002221 eb.vma = (struct i915_vma **)(exec + args->buffer_count + 1);
2222 eb.vma[0] = NULL;
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002223 eb.flags = (unsigned int *)(eb.vma + args->buffer_count + 1);
2224
Chris Wilson2889caa2017-06-16 15:05:19 +01002225 eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
2226 if (USES_FULL_PPGTT(eb.i915))
2227 eb.invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson650bc632017-06-15 09:14:33 +01002228 reloc_cache_init(&eb.reloc_cache, eb.i915);
2229
Chris Wilson2889caa2017-06-16 15:05:19 +01002230 eb.buffer_count = args->buffer_count;
Chris Wilson650bc632017-06-15 09:14:33 +01002231 eb.batch_start_offset = args->batch_start_offset;
2232 eb.batch_len = args->batch_len;
2233
Chris Wilson2889caa2017-06-16 15:05:19 +01002234 eb.batch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002235 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02002236 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002237 return -EPERM;
2238
Chris Wilson2889caa2017-06-16 15:05:19 +01002239 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002240 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01002241 if (args->flags & I915_EXEC_IS_PINNED)
Chris Wilson2889caa2017-06-16 15:05:19 +01002242 eb.batch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002243
Chris Wilson650bc632017-06-15 09:14:33 +01002244 eb.engine = eb_select_engine(eb.i915, file, args);
2245 if (!eb.engine)
Dave Gordonf8ca0c02016-07-20 18:16:07 +01002246 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002247
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002248 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
Chris Wilson650bc632017-06-15 09:14:33 +01002249 if (!HAS_RESOURCE_STREAMER(eb.i915)) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002250 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
2251 return -EINVAL;
2252 }
Chris Wilson650bc632017-06-15 09:14:33 +01002253 if (eb.engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002254 DRM_DEBUG("RS is not available on %s\n",
Chris Wilson650bc632017-06-15 09:14:33 +01002255 eb.engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002256 return -EINVAL;
2257 }
2258
Chris Wilson2889caa2017-06-16 15:05:19 +01002259 eb.batch_flags |= I915_DISPATCH_RS;
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03002260 }
2261
Chris Wilsonfec04452017-01-27 09:40:08 +00002262 if (args->flags & I915_EXEC_FENCE_IN) {
2263 in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002264 if (!in_fence)
2265 return -EINVAL;
Chris Wilsonfec04452017-01-27 09:40:08 +00002266 }
2267
2268 if (args->flags & I915_EXEC_FENCE_OUT) {
2269 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
2270 if (out_fence_fd < 0) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002271 err = out_fence_fd;
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002272 goto err_in_fence;
Chris Wilsonfec04452017-01-27 09:40:08 +00002273 }
2274 }
2275
Chris Wilson4d470f72017-06-29 16:04:25 +01002276 err = eb_create(&eb);
2277 if (err)
2278 goto err_out_fence;
2279
2280 GEM_BUG_ON(!eb.lut_size);
Chris Wilson2889caa2017-06-16 15:05:19 +01002281
Chris Wilson1acfc102017-06-20 12:05:47 +01002282 err = eb_select_context(&eb);
2283 if (unlikely(err))
2284 goto err_destroy;
2285
Chris Wilson2889caa2017-06-16 15:05:19 +01002286 /*
2287 * Take a local wakeref for preparing to dispatch the execbuf as
Chris Wilson67d97da2016-07-04 08:08:31 +01002288 * we expect to access the hardware fairly frequently in the
2289 * process. Upon first dispatch, we acquire another prolonged
2290 * wakeref that we hold until the GPU has been idle for at least
2291 * 100ms.
2292 */
Chris Wilson650bc632017-06-15 09:14:33 +01002293 intel_runtime_pm_get(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002294
Chris Wilson2889caa2017-06-16 15:05:19 +01002295 err = i915_mutex_lock_interruptible(dev);
2296 if (err)
2297 goto err_rpm;
Paulo Zanonif65c9162013-11-27 18:20:34 -02002298
Chris Wilson2889caa2017-06-16 15:05:19 +01002299 err = eb_relocate(&eb);
Chris Wilson1f727d92017-07-21 15:50:36 +01002300 if (err) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002301 /*
2302 * If the user expects the execobject.offset and
2303 * reloc.presumed_offset to be an exact match,
2304 * as for using NO_RELOC, then we cannot update
2305 * the execobject.offset until we have completed
2306 * relocation.
2307 */
2308 args->flags &= ~__EXEC_HAS_RELOC;
Chris Wilson2889caa2017-06-16 15:05:19 +01002309 goto err_vma;
Chris Wilson1f727d92017-07-21 15:50:36 +01002310 }
Ben Widawsky41bde552013-12-06 14:11:21 -08002311
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002312 if (unlikely(*eb.batch->exec_flags & EXEC_OBJECT_WRITE)) {
Daniel Vetterff240192012-01-31 21:08:14 +01002313 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002314 err = -EINVAL;
2315 goto err_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002316 }
Chris Wilson650bc632017-06-15 09:14:33 +01002317 if (eb.batch_start_offset > eb.batch->size ||
2318 eb.batch_len > eb.batch->size - eb.batch_start_offset) {
Chris Wilson0b537272016-08-18 17:17:12 +01002319 DRM_DEBUG("Attempting to use out-of-bounds batch\n");
Chris Wilson2889caa2017-06-16 15:05:19 +01002320 err = -EINVAL;
2321 goto err_vma;
Chris Wilson0b537272016-08-18 17:17:12 +01002322 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002323
Chris Wilson650bc632017-06-15 09:14:33 +01002324 if (eb.engine->needs_cmd_parser && eb.batch_len) {
Chris Wilson59bfa122016-08-04 16:32:31 +01002325 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002326
Chris Wilson650bc632017-06-15 09:14:33 +01002327 vma = eb_parse(&eb, drm_is_current_master(file));
Chris Wilson59bfa122016-08-04 16:32:31 +01002328 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002329 err = PTR_ERR(vma);
2330 goto err_vma;
Brad Volkin78a42372014-12-11 12:13:09 -08002331 }
Chris Wilson17cabf52015-01-14 11:20:57 +00002332
Chris Wilson59bfa122016-08-04 16:32:31 +01002333 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002334 /*
2335 * Batch parsed and accepted:
2336 *
2337 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
2338 * bit from MI_BATCH_BUFFER_START commands issued in
2339 * the dispatch_execbuffer implementations. We
2340 * specifically don't want that set on batches the
2341 * command parser has accepted.
2342 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002343 eb.batch_flags |= I915_DISPATCH_SECURE;
Chris Wilson650bc632017-06-15 09:14:33 +01002344 eb.batch_start_offset = 0;
2345 eb.batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01002346 }
Brad Volkin351e3db2014-02-18 10:15:46 -08002347 }
2348
Chris Wilson650bc632017-06-15 09:14:33 +01002349 if (eb.batch_len == 0)
2350 eb.batch_len = eb.batch->size - eb.batch_start_offset;
Brad Volkin78a42372014-12-11 12:13:09 -08002351
Chris Wilson2889caa2017-06-16 15:05:19 +01002352 /*
2353 * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002354 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07002355 * hsw should have this fixed, but bdw mucks it up again. */
Chris Wilson2889caa2017-06-16 15:05:19 +01002356 if (eb.batch_flags & I915_DISPATCH_SECURE) {
Chris Wilson058d88c2016-08-15 10:49:06 +01002357 struct i915_vma *vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002358
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002359 /*
2360 * So on first glance it looks freaky that we pin the batch here
2361 * outside of the reservation loop. But:
2362 * - The batch is already pinned into the relevant ppgtt, so we
2363 * already have the backing storage fully allocated.
2364 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01002365 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02002366 * fitting due to fragmentation.
2367 * So this is actually safe.
2368 */
Chris Wilson2889caa2017-06-16 15:05:19 +01002369 vma = i915_gem_object_ggtt_pin(eb.batch->obj, NULL, 0, 0, 0);
Chris Wilson058d88c2016-08-15 10:49:06 +01002370 if (IS_ERR(vma)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002371 err = PTR_ERR(vma);
2372 goto err_vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01002373 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002374
Chris Wilson650bc632017-06-15 09:14:33 +01002375 eb.batch = vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01002376 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002377
Chris Wilson7dd4f672017-06-16 15:05:24 +01002378 /* All GPU relocation batches must be submitted prior to the user rq */
2379 GEM_BUG_ON(eb.reloc_cache.rq);
2380
John Harrison0c8dac82015-05-29 17:43:25 +01002381 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson650bc632017-06-15 09:14:33 +01002382 eb.request = i915_gem_request_alloc(eb.engine, eb.ctx);
2383 if (IS_ERR(eb.request)) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002384 err = PTR_ERR(eb.request);
John Harrison0c8dac82015-05-29 17:43:25 +01002385 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00002386 }
John Harrison0c8dac82015-05-29 17:43:25 +01002387
Chris Wilsonfec04452017-01-27 09:40:08 +00002388 if (in_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002389 err = i915_gem_request_await_dma_fence(eb.request, in_fence);
2390 if (err < 0)
Chris Wilsonfec04452017-01-27 09:40:08 +00002391 goto err_request;
2392 }
2393
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002394 if (fences) {
2395 err = await_fence_array(&eb, fences);
2396 if (err)
2397 goto err_request;
2398 }
2399
Chris Wilsonfec04452017-01-27 09:40:08 +00002400 if (out_fence_fd != -1) {
Chris Wilson650bc632017-06-15 09:14:33 +01002401 out_fence = sync_file_create(&eb.request->fence);
Chris Wilsonfec04452017-01-27 09:40:08 +00002402 if (!out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002403 err = -ENOMEM;
Chris Wilsonfec04452017-01-27 09:40:08 +00002404 goto err_request;
2405 }
2406 }
2407
Chris Wilson2889caa2017-06-16 15:05:19 +01002408 /*
2409 * Whilst this request exists, batch_obj will be on the
Chris Wilson17f298cf2016-08-10 13:41:46 +01002410 * active_list, and so will hold the active reference. Only when this
2411 * request is retired will the the batch_obj be moved onto the
2412 * inactive_list and lose its active reference. Hence we do not need
2413 * to explicitly hold another reference here.
2414 */
Chris Wilson650bc632017-06-15 09:14:33 +01002415 eb.request->batch = eb.batch;
Chris Wilson17f298cf2016-08-10 13:41:46 +01002416
Chris Wilson2889caa2017-06-16 15:05:19 +01002417 trace_i915_gem_request_queue(eb.request, eb.batch_flags);
2418 err = eb_submit(&eb);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01002419err_request:
Chris Wilson2889caa2017-06-16 15:05:19 +01002420 __i915_add_request(eb.request, err == 0);
Chris Wilson650bc632017-06-15 09:14:33 +01002421 add_to_client(eb.request, file);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00002422
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002423 if (fences)
2424 signal_fence_array(&eb, fences);
2425
Chris Wilsonfec04452017-01-27 09:40:08 +00002426 if (out_fence) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002427 if (err == 0) {
Chris Wilsonfec04452017-01-27 09:40:08 +00002428 fd_install(out_fence_fd, out_fence->file);
2429 args->rsvd2 &= GENMASK_ULL(0, 31); /* keep in-fence */
2430 args->rsvd2 |= (u64)out_fence_fd << 32;
2431 out_fence_fd = -1;
2432 } else {
2433 fput(out_fence->file);
2434 }
2435 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00002436
John Harrison0c8dac82015-05-29 17:43:25 +01002437err_batch_unpin:
Chris Wilson2889caa2017-06-16 15:05:19 +01002438 if (eb.batch_flags & I915_DISPATCH_SECURE)
Chris Wilson650bc632017-06-15 09:14:33 +01002439 i915_vma_unpin(eb.batch);
Chris Wilson2889caa2017-06-16 15:05:19 +01002440err_vma:
2441 if (eb.exec)
2442 eb_release_vmas(&eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002443 mutex_unlock(&dev->struct_mutex);
Chris Wilson2889caa2017-06-16 15:05:19 +01002444err_rpm:
Chris Wilson650bc632017-06-15 09:14:33 +01002445 intel_runtime_pm_put(eb.i915);
Chris Wilson1acfc102017-06-20 12:05:47 +01002446 i915_gem_context_put(eb.ctx);
2447err_destroy:
Chris Wilson2889caa2017-06-16 15:05:19 +01002448 eb_destroy(&eb);
Chris Wilson4d470f72017-06-29 16:04:25 +01002449err_out_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002450 if (out_fence_fd != -1)
2451 put_unused_fd(out_fence_fd);
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08002452err_in_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00002453 dma_fence_put(in_fence);
Chris Wilson2889caa2017-06-16 15:05:19 +01002454 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002455}
2456
2457/*
2458 * Legacy execbuffer just creates an exec2 list from the original exec object
2459 * list array and passes it to the real function.
2460 */
2461int
2462i915_gem_execbuffer(struct drm_device *dev, void *data,
2463 struct drm_file *file)
2464{
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002465 const size_t sz = (sizeof(struct drm_i915_gem_exec_object2) +
2466 sizeof(struct i915_vma *) +
2467 sizeof(unsigned int));
Chris Wilson54cf91d2010-11-25 18:00:26 +00002468 struct drm_i915_gem_execbuffer *args = data;
2469 struct drm_i915_gem_execbuffer2 exec2;
2470 struct drm_i915_gem_exec_object *exec_list = NULL;
2471 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +01002472 unsigned int i;
2473 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002474
Chris Wilson2889caa2017-06-16 15:05:19 +01002475 if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) {
2476 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002477 return -EINVAL;
2478 }
2479
Chris Wilson2889caa2017-06-16 15:05:19 +01002480 exec2.buffers_ptr = args->buffers_ptr;
2481 exec2.buffer_count = args->buffer_count;
2482 exec2.batch_start_offset = args->batch_start_offset;
2483 exec2.batch_len = args->batch_len;
2484 exec2.DR1 = args->DR1;
2485 exec2.DR4 = args->DR4;
2486 exec2.num_cliprects = args->num_cliprects;
2487 exec2.cliprects_ptr = args->cliprects_ptr;
2488 exec2.flags = I915_EXEC_RENDER;
2489 i915_execbuffer2_set_context_id(exec2, 0);
2490
2491 if (!i915_gem_check_execbuffer(&exec2))
2492 return -EINVAL;
2493
Chris Wilson54cf91d2010-11-25 18:00:26 +00002494 /* Copy in the exec list from userland */
Chris Wilson2889caa2017-06-16 15:05:19 +01002495 exec_list = kvmalloc_array(args->buffer_count, sizeof(*exec_list),
2496 __GFP_NOWARN | GFP_TEMPORARY);
2497 exec2_list = kvmalloc_array(args->buffer_count + 1, sz,
2498 __GFP_NOWARN | GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002499 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002500 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002501 args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002502 kvfree(exec_list);
2503 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002504 return -ENOMEM;
2505 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002506 err = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002507 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00002508 sizeof(*exec_list) * args->buffer_count);
Chris Wilson2889caa2017-06-16 15:05:19 +01002509 if (err) {
Daniel Vetterff240192012-01-31 21:08:14 +01002510 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson2889caa2017-06-16 15:05:19 +01002511 args->buffer_count, err);
Michal Hocko20981052017-05-17 14:23:12 +02002512 kvfree(exec_list);
2513 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002514 return -EFAULT;
2515 }
2516
2517 for (i = 0; i < args->buffer_count; i++) {
2518 exec2_list[i].handle = exec_list[i].handle;
2519 exec2_list[i].relocation_count = exec_list[i].relocation_count;
2520 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
2521 exec2_list[i].alignment = exec_list[i].alignment;
2522 exec2_list[i].offset = exec_list[i].offset;
Tvrtko Ursulinf0836b72016-11-16 08:55:32 +00002523 if (INTEL_GEN(to_i915(dev)) < 4)
Chris Wilson54cf91d2010-11-25 18:00:26 +00002524 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
2525 else
2526 exec2_list[i].flags = 0;
2527 }
2528
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002529 err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list, NULL);
Chris Wilson2889caa2017-06-16 15:05:19 +01002530 if (exec2.flags & __EXEC_HAS_RELOC) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002531 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03002532 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002533
Chris Wilson54cf91d2010-11-25 18:00:26 +00002534 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002535 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002536 if (!(exec2_list[i].offset & UPDATE))
2537 continue;
2538
Michał Winiarski934acce2015-12-29 18:24:52 +01002539 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002540 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2541 exec2_list[i].offset &= PIN_OFFSET_MASK;
2542 if (__copy_to_user(&user_exec_list[i].offset,
2543 &exec2_list[i].offset,
2544 sizeof(user_exec_list[i].offset)))
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002545 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002546 }
2547 }
2548
Michal Hocko20981052017-05-17 14:23:12 +02002549 kvfree(exec_list);
2550 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002551 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002552}
2553
2554int
2555i915_gem_execbuffer2(struct drm_device *dev, void *data,
2556 struct drm_file *file)
2557{
Chris Wilsonc7c6e462017-08-16 09:52:06 +01002558 const size_t sz = (sizeof(struct drm_i915_gem_exec_object2) +
2559 sizeof(struct i915_vma *) +
2560 sizeof(unsigned int));
Chris Wilson54cf91d2010-11-25 18:00:26 +00002561 struct drm_i915_gem_execbuffer2 *args = data;
Chris Wilson2889caa2017-06-16 15:05:19 +01002562 struct drm_i915_gem_exec_object2 *exec2_list;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002563 struct drm_syncobj **fences = NULL;
Chris Wilson2889caa2017-06-16 15:05:19 +01002564 int err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002565
Chris Wilson2889caa2017-06-16 15:05:19 +01002566 if (args->buffer_count < 1 || args->buffer_count > SIZE_MAX / sz - 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01002567 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002568 return -EINVAL;
2569 }
2570
Chris Wilson2889caa2017-06-16 15:05:19 +01002571 if (!i915_gem_check_execbuffer(args))
2572 return -EINVAL;
2573
2574 /* Allocate an extra slot for use by the command parser */
2575 exec2_list = kvmalloc_array(args->buffer_count + 1, sz,
2576 __GFP_NOWARN | GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002577 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01002578 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00002579 args->buffer_count);
2580 return -ENOMEM;
2581 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002582 if (copy_from_user(exec2_list,
2583 u64_to_user_ptr(args->buffers_ptr),
2584 sizeof(*exec2_list) * args->buffer_count)) {
2585 DRM_DEBUG("copy %d exec entries failed\n", args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02002586 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002587 return -EFAULT;
2588 }
2589
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002590 if (args->flags & I915_EXEC_FENCE_ARRAY) {
2591 fences = get_fence_array(args, file);
2592 if (IS_ERR(fences)) {
2593 kvfree(exec2_list);
2594 return PTR_ERR(fences);
2595 }
2596 }
2597
2598 err = i915_gem_do_execbuffer(dev, file, args, exec2_list, fences);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002599
Chris Wilson2889caa2017-06-16 15:05:19 +01002600 /*
2601 * Now that we have begun execution of the batchbuffer, we ignore
2602 * any new error after this point. Also given that we have already
2603 * updated the associated relocations, we try to write out the current
2604 * object locations irrespective of any error.
2605 */
2606 if (args->flags & __EXEC_HAS_RELOC) {
2607 struct drm_i915_gem_exec_object2 __user *user_exec_list =
2608 u64_to_user_ptr(args->buffers_ptr);
2609 unsigned int i;
2610
2611 /* Copy the new buffer offsets back to the user's exec list. */
2612 user_access_begin();
Chris Wilson9aab8bf2014-05-23 10:45:52 +01002613 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson2889caa2017-06-16 15:05:19 +01002614 if (!(exec2_list[i].offset & UPDATE))
2615 continue;
2616
Michał Winiarski934acce2015-12-29 18:24:52 +01002617 exec2_list[i].offset =
Chris Wilson2889caa2017-06-16 15:05:19 +01002618 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK);
2619 unsafe_put_user(exec2_list[i].offset,
2620 &user_exec_list[i].offset,
2621 end_user);
Chris Wilson54cf91d2010-11-25 18:00:26 +00002622 }
Chris Wilson2889caa2017-06-16 15:05:19 +01002623end_user:
2624 user_access_end();
Chris Wilson54cf91d2010-11-25 18:00:26 +00002625 }
2626
Chris Wilson2889caa2017-06-16 15:05:19 +01002627 args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS;
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002628 put_fence_array(args, fences);
Michal Hocko20981052017-05-17 14:23:12 +02002629 kvfree(exec2_list);
Chris Wilson2889caa2017-06-16 15:05:19 +01002630 return err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002631}