blob: 2a9aed5640e267f348f1368f883e53189dac85a7 [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>
35#include <drm/i915_drm.h>
Chris Wilsonad778f82016-08-04 16:32:42 +010036
Chris Wilson54cf91d2010-11-25 18:00:26 +000037#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000038#include "i915_gem_clflush.h"
Chris Wilson54cf91d2010-11-25 18:00:26 +000039#include "i915_trace.h"
40#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010041#include "intel_frontbuffer.h"
Chris Wilson54cf91d2010-11-25 18:00:26 +000042
Chris Wilsond50415c2016-08-18 17:16:52 +010043#define DBG_USE_CPU_RELOC 0 /* -1 force GTT relocs; 1 force CPU relocs */
44
Dave Gordon9e2793f62016-07-14 14:52:03 +010045#define __EXEC_OBJECT_HAS_PIN (1<<31)
46#define __EXEC_OBJECT_HAS_FENCE (1<<30)
47#define __EXEC_OBJECT_NEEDS_MAP (1<<29)
48#define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
49#define __EXEC_OBJECT_INTERNAL_FLAGS (0xf<<28) /* all of the above */
Chris Wilsond23db882014-05-23 08:48:08 +020050
51#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000052
Chris Wilson650bc632017-06-15 09:14:33 +010053#define __I915_EXEC_ILLEGAL_FLAGS \
54 (__I915_EXEC_UNKNOWN_FLAGS | I915_EXEC_CONSTANTS_MASK)
Chris Wilson5b043f42016-08-02 22:50:38 +010055
Chris Wilson650bc632017-06-15 09:14:33 +010056struct i915_execbuffer {
Chris Wilsond50415c2016-08-18 17:16:52 +010057 struct drm_i915_private *i915;
Chris Wilson650bc632017-06-15 09:14:33 +010058 struct drm_file *file;
59 struct drm_i915_gem_execbuffer2 *args;
60 struct drm_i915_gem_exec_object2 *exec;
61 struct intel_engine_cs *engine;
62 struct i915_gem_context *ctx;
63 struct i915_address_space *vm;
64 struct i915_vma *batch;
65 struct drm_i915_gem_request *request;
66 u32 batch_start_offset;
67 u32 batch_len;
68 unsigned int dispatch_flags;
69 struct drm_i915_gem_exec_object2 shadow_exec_entry;
70 bool need_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +020071 struct list_head vmas;
Chris Wilson650bc632017-06-15 09:14:33 +010072 struct reloc_cache {
73 struct drm_mm_node node;
74 unsigned long vaddr;
75 unsigned int page;
76 bool use_64bit_reloc : 1;
77 } reloc_cache;
Chris Wilson67731b82010-12-08 10:38:14 +000078 int and;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000079 union {
Chris Wilson650bc632017-06-15 09:14:33 +010080 struct i915_vma **lut;
81 struct hlist_head *buckets;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000082 };
Chris Wilson67731b82010-12-08 10:38:14 +000083};
84
Chris Wilson650bc632017-06-15 09:14:33 +010085static int eb_create(struct i915_execbuffer *eb)
Chris Wilson67731b82010-12-08 10:38:14 +000086{
Chris Wilson650bc632017-06-15 09:14:33 +010087 eb->lut = NULL;
88 if (eb->args->flags & I915_EXEC_HANDLE_LUT) {
89 unsigned int size = eb->args->buffer_count;
Ben Widawsky27173f12013-08-14 11:38:36 +020090 size *= sizeof(struct i915_vma *);
Chris Wilson650bc632017-06-15 09:14:33 +010091 eb->lut = kmalloc(size,
92 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilsoneef90cc2013-01-08 10:53:17 +000093 }
94
Chris Wilson650bc632017-06-15 09:14:33 +010095 if (!eb->lut) {
96 unsigned int size = eb->args->buffer_count;
97 unsigned int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Lauri Kasanen27b7c632013-03-27 15:04:55 +020098 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
Chris Wilsoneef90cc2013-01-08 10:53:17 +000099 while (count > 2*size)
100 count >>= 1;
Chris Wilson650bc632017-06-15 09:14:33 +0100101 eb->lut = kzalloc(count * sizeof(struct hlist_head),
102 GFP_TEMPORARY);
103 if (!eb->lut)
104 return -ENOMEM;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000105
106 eb->and = count - 1;
Chris Wilson650bc632017-06-15 09:14:33 +0100107 } else {
108 eb->and = -eb->args->buffer_count;
109 }
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000110
Chris Wilson650bc632017-06-15 09:14:33 +0100111 return 0;
Chris Wilson67731b82010-12-08 10:38:14 +0000112}
113
Chris Wilsond55495b2017-06-15 09:14:34 +0100114static inline void
115__eb_unreserve_vma(struct i915_vma *vma,
116 const struct drm_i915_gem_exec_object2 *entry)
117{
118 if (unlikely(entry->flags & __EXEC_OBJECT_HAS_FENCE))
119 i915_vma_unpin_fence(vma);
120
121 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
122 __i915_vma_unpin(vma);
123}
124
125static void
126eb_unreserve_vma(struct i915_vma *vma)
127{
128 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
129
130 __eb_unreserve_vma(vma, entry);
131 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
132}
133
Chris Wilson67731b82010-12-08 10:38:14 +0000134static void
Chris Wilson650bc632017-06-15 09:14:33 +0100135eb_reset(struct i915_execbuffer *eb)
Chris Wilson67731b82010-12-08 10:38:14 +0000136{
Chris Wilsond55495b2017-06-15 09:14:34 +0100137 struct i915_vma *vma;
138
Chris Wilson8c45cec2017-06-15 09:14:35 +0100139 list_for_each_entry(vma, &eb->vmas, exec_link) {
Chris Wilsond55495b2017-06-15 09:14:34 +0100140 eb_unreserve_vma(vma);
141 i915_vma_put(vma);
142 vma->exec_entry = NULL;
143 }
144
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000145 if (eb->and >= 0)
146 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
Chris Wilson67731b82010-12-08 10:38:14 +0000147}
148
Chris Wilson59bfa122016-08-04 16:32:31 +0100149static struct i915_vma *
Chris Wilson650bc632017-06-15 09:14:33 +0100150eb_get_batch(struct i915_execbuffer *eb)
Chris Wilson59bfa122016-08-04 16:32:31 +0100151{
Chris Wilson8c45cec2017-06-15 09:14:35 +0100152 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_link);
Chris Wilson59bfa122016-08-04 16:32:31 +0100153
154 /*
155 * SNA is doing fancy tricks with compressing batch buffers, which leads
156 * to negative relocation deltas. Usually that works out ok since the
157 * relocate address is still positive, except when the batch is placed
158 * very low in the GTT. Ensure this doesn't happen.
159 *
160 * Note that actual hangs have only been observed on gen7, but for
161 * paranoia do it everywhere.
162 */
163 if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
164 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
165
166 return vma;
167}
168
Chris Wilson3b96eff2013-01-08 10:53:14 +0000169static int
Chris Wilson650bc632017-06-15 09:14:33 +0100170eb_lookup_vmas(struct i915_execbuffer *eb)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000171{
Ben Widawsky27173f12013-08-14 11:38:36 +0200172 struct drm_i915_gem_object *obj;
173 struct list_head objects;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000174 int i, ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000175
Chris Wilsond55495b2017-06-15 09:14:34 +0100176 INIT_LIST_HEAD(&eb->vmas);
177
Ben Widawsky27173f12013-08-14 11:38:36 +0200178 INIT_LIST_HEAD(&objects);
Chris Wilson650bc632017-06-15 09:14:33 +0100179 spin_lock(&eb->file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +0200180 /* Grab a reference to the object and release the lock so we can lookup
181 * or create the VMA without using GFP_ATOMIC */
Chris Wilson650bc632017-06-15 09:14:33 +0100182 for (i = 0; i < eb->args->buffer_count; i++) {
183 obj = to_intel_bo(idr_find(&eb->file->object_idr, eb->exec[i].handle));
Chris Wilson3b96eff2013-01-08 10:53:14 +0000184 if (obj == NULL) {
Chris Wilson650bc632017-06-15 09:14:33 +0100185 spin_unlock(&eb->file->table_lock);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000186 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson650bc632017-06-15 09:14:33 +0100187 eb->exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200188 ret = -ENOENT;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000189 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000190 }
191
Ben Widawsky27173f12013-08-14 11:38:36 +0200192 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson650bc632017-06-15 09:14:33 +0100193 spin_unlock(&eb->file->table_lock);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000194 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
Chris Wilson650bc632017-06-15 09:14:33 +0100195 obj, eb->exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200196 ret = -EINVAL;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000197 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000198 }
199
Chris Wilson25dc5562016-07-20 13:31:52 +0100200 i915_gem_object_get(obj);
Ben Widawsky27173f12013-08-14 11:38:36 +0200201 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000202 }
Chris Wilson650bc632017-06-15 09:14:33 +0100203 spin_unlock(&eb->file->table_lock);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000204
Ben Widawsky27173f12013-08-14 11:38:36 +0200205 i = 0;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000206 while (!list_empty(&objects)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200207 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800208
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000209 obj = list_first_entry(&objects,
210 struct drm_i915_gem_object,
211 obj_exec_link);
212
Daniel Vettere656a6c2013-08-14 14:14:04 +0200213 /*
214 * NOTE: We can leak any vmas created here when something fails
215 * later on. But that's no issue since vma_unbind can deal with
216 * vmas which are not actually bound. And since only
217 * lookup_or_create exists as an interface to get at the vma
218 * from the (obj, vm) we don't run the risk of creating
219 * duplicated vmas for the same vm.
220 */
Chris Wilson650bc632017-06-15 09:14:33 +0100221 vma = i915_vma_instance(obj, eb->vm, NULL);
Chris Wilson058d88c2016-08-15 10:49:06 +0100222 if (unlikely(IS_ERR(vma))) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200223 DRM_DEBUG("Failed to lookup VMA\n");
224 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000225 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200226 }
227
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000228 /* Transfer ownership from the objects list to the vmas list. */
Chris Wilson8c45cec2017-06-15 09:14:35 +0100229 list_add_tail(&vma->exec_link, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000230 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200231
Chris Wilson650bc632017-06-15 09:14:33 +0100232 vma->exec_entry = &eb->exec[i];
Ben Widawsky27173f12013-08-14 11:38:36 +0200233 if (eb->and < 0) {
234 eb->lut[i] = vma;
235 } else {
Chris Wilson650bc632017-06-15 09:14:33 +0100236 u32 handle =
237 eb->args->flags & I915_EXEC_HANDLE_LUT ?
238 i : eb->exec[i].handle;
Ben Widawsky27173f12013-08-14 11:38:36 +0200239 vma->exec_handle = handle;
240 hlist_add_head(&vma->exec_node,
241 &eb->buckets[handle & eb->and]);
242 }
243 ++i;
244 }
245
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000246 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200247
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000248
249err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200250 while (!list_empty(&objects)) {
251 obj = list_first_entry(&objects,
252 struct drm_i915_gem_object,
253 obj_exec_link);
254 list_del_init(&obj->obj_exec_link);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100255 i915_gem_object_put(obj);
Ben Widawsky27173f12013-08-14 11:38:36 +0200256 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000257 /*
258 * Objects already transfered to the vmas list will be unreferenced by
259 * eb_destroy.
260 */
261
Ben Widawsky27173f12013-08-14 11:38:36 +0200262 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000263}
264
Chris Wilson650bc632017-06-15 09:14:33 +0100265static struct i915_vma *eb_get_vma(struct i915_execbuffer *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000266{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000267 if (eb->and < 0) {
268 if (handle >= -eb->and)
269 return NULL;
270 return eb->lut[handle];
271 } else {
272 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800273 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000274
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000275 head = &eb->buckets[handle & eb->and];
Geliang Tangaa459502016-01-18 23:54:20 +0800276 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200277 if (vma->exec_handle == handle)
278 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000279 }
280 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000281 }
Chris Wilson67731b82010-12-08 10:38:14 +0000282}
283
Chris Wilson650bc632017-06-15 09:14:33 +0100284static void eb_destroy(struct i915_execbuffer *eb)
Chris Wilsona415d352013-11-26 11:23:15 +0000285{
Chris Wilsond55495b2017-06-15 09:14:34 +0100286 struct i915_vma *vma;
Chris Wilson650bc632017-06-15 09:14:33 +0100287
Chris Wilson8c45cec2017-06-15 09:14:35 +0100288 list_for_each_entry(vma, &eb->vmas, exec_link) {
Chris Wilsond55495b2017-06-15 09:14:34 +0100289 if (!vma->exec_entry)
290 continue;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000291
Chris Wilsond55495b2017-06-15 09:14:34 +0100292 __eb_unreserve_vma(vma, vma->exec_entry);
Chris Wilson172ae5b2016-12-05 14:29:37 +0000293 vma->exec_entry = NULL;
Chris Wilson624192c2016-08-15 10:48:50 +0100294 i915_vma_put(vma);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000295 }
Chris Wilsond55495b2017-06-15 09:14:34 +0100296
297 i915_gem_context_put(eb->ctx);
298
299 if (eb->buckets)
300 kfree(eb->buckets);
Chris Wilson67731b82010-12-08 10:38:14 +0000301}
302
Chris Wilsondabdfe02012-03-26 10:10:27 +0200303static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
304{
Chris Wilson9e53d9b2016-08-18 17:16:54 +0100305 if (!i915_gem_object_has_struct_page(obj))
306 return false;
307
Chris Wilsond50415c2016-08-18 17:16:52 +0100308 if (DBG_USE_CPU_RELOC)
309 return DBG_USE_CPU_RELOC > 0;
310
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +0000311 return (HAS_LLC(to_i915(obj->base.dev)) ||
Chris Wilsone27ab732017-06-15 13:38:49 +0100312 obj->cache_dirty ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200313 obj->cache_level != I915_CACHE_NONE);
314}
315
Michał Winiarski934acce2015-12-29 18:24:52 +0100316/* Used to convert any address to canonical form.
317 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
318 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
319 * addresses to be in a canonical form:
320 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
321 * canonical form [63:48] == [47]."
322 */
323#define GEN8_HIGH_ADDRESS_BIT 47
324static inline uint64_t gen8_canonical_addr(uint64_t address)
325{
326 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
327}
328
329static inline uint64_t gen8_noncanonical_addr(uint64_t address)
330{
331 return address & ((1ULL << (GEN8_HIGH_ADDRESS_BIT + 1)) - 1);
332}
333
334static inline uint64_t
Chris Wilsond50415c2016-08-18 17:16:52 +0100335relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
Michał Winiarski934acce2015-12-29 18:24:52 +0100336 uint64_t target_offset)
337{
338 return gen8_canonical_addr((int)reloc->delta + target_offset);
339}
340
Chris Wilsond50415c2016-08-18 17:16:52 +0100341static void reloc_cache_init(struct reloc_cache *cache,
342 struct drm_i915_private *i915)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100343{
Chris Wilson31a39202016-08-18 17:16:46 +0100344 cache->page = -1;
Chris Wilsond50415c2016-08-18 17:16:52 +0100345 cache->vaddr = 0;
Joonas Lahtinendfc51482016-11-03 10:39:46 +0200346 /* Must be a variable in the struct to allow GCC to unroll. */
347 cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100348 cache->node.allocated = false;
Chris Wilson31a39202016-08-18 17:16:46 +0100349}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100350
Chris Wilsond50415c2016-08-18 17:16:52 +0100351static inline void *unmask_page(unsigned long p)
352{
353 return (void *)(uintptr_t)(p & PAGE_MASK);
354}
Rafael Barbalho5032d872013-08-21 17:10:51 +0100355
Chris Wilsond50415c2016-08-18 17:16:52 +0100356static inline unsigned int unmask_flags(unsigned long p)
357{
358 return p & ~PAGE_MASK;
359}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700360
Chris Wilsond50415c2016-08-18 17:16:52 +0100361#define KMAP 0x4 /* after CLFLUSH_FLAGS */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700362
Chris Wilson650bc632017-06-15 09:14:33 +0100363static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache)
364{
365 struct drm_i915_private *i915 =
366 container_of(cache, struct i915_execbuffer, reloc_cache)->i915;
367 return &i915->ggtt;
368}
369
370static void reloc_cache_reset(struct reloc_cache *cache)
Chris Wilson31a39202016-08-18 17:16:46 +0100371{
Chris Wilsond50415c2016-08-18 17:16:52 +0100372 void *vaddr;
373
Chris Wilson31a39202016-08-18 17:16:46 +0100374 if (!cache->vaddr)
375 return;
376
Chris Wilsond50415c2016-08-18 17:16:52 +0100377 vaddr = unmask_page(cache->vaddr);
378 if (cache->vaddr & KMAP) {
379 if (cache->vaddr & CLFLUSH_AFTER)
380 mb();
Chris Wilson31a39202016-08-18 17:16:46 +0100381
Chris Wilsond50415c2016-08-18 17:16:52 +0100382 kunmap_atomic(vaddr);
383 i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
384 } else {
Chris Wilsone8cb9092016-08-18 17:16:53 +0100385 wmb();
Chris Wilsond50415c2016-08-18 17:16:52 +0100386 io_mapping_unmap_atomic((void __iomem *)vaddr);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100387 if (cache->node.allocated) {
Chris Wilson650bc632017-06-15 09:14:33 +0100388 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100389
390 ggtt->base.clear_range(&ggtt->base,
391 cache->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200392 cache->node.size);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100393 drm_mm_remove_node(&cache->node);
394 } else {
395 i915_vma_unpin((struct i915_vma *)cache->node.mm);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700396 }
Chris Wilson31a39202016-08-18 17:16:46 +0100397 }
Chris Wilson650bc632017-06-15 09:14:33 +0100398
399 cache->vaddr = 0;
400 cache->page = -1;
Chris Wilson31a39202016-08-18 17:16:46 +0100401}
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700402
Chris Wilson31a39202016-08-18 17:16:46 +0100403static void *reloc_kmap(struct drm_i915_gem_object *obj,
404 struct reloc_cache *cache,
405 int page)
406{
Chris Wilsond50415c2016-08-18 17:16:52 +0100407 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100408
Chris Wilsond50415c2016-08-18 17:16:52 +0100409 if (cache->vaddr) {
410 kunmap_atomic(unmask_page(cache->vaddr));
411 } else {
412 unsigned int flushes;
413 int ret;
Chris Wilson31a39202016-08-18 17:16:46 +0100414
Chris Wilsond50415c2016-08-18 17:16:52 +0100415 ret = i915_gem_obj_prepare_shmem_write(obj, &flushes);
416 if (ret)
417 return ERR_PTR(ret);
418
419 BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
420 BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
421
422 cache->vaddr = flushes | KMAP;
423 cache->node.mm = (void *)obj;
424 if (flushes)
425 mb();
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700426 }
427
Chris Wilsond50415c2016-08-18 17:16:52 +0100428 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
429 cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100430 cache->page = page;
Chris Wilson31a39202016-08-18 17:16:46 +0100431
Chris Wilsond50415c2016-08-18 17:16:52 +0100432 return vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100433}
434
Chris Wilsond50415c2016-08-18 17:16:52 +0100435static void *reloc_iomap(struct drm_i915_gem_object *obj,
Chris Wilson31a39202016-08-18 17:16:46 +0100436 struct reloc_cache *cache,
Chris Wilsond50415c2016-08-18 17:16:52 +0100437 int page)
Chris Wilson31a39202016-08-18 17:16:46 +0100438{
Chris Wilson650bc632017-06-15 09:14:33 +0100439 struct i915_ggtt *ggtt = cache_to_ggtt(cache);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100440 unsigned long offset;
Chris Wilsond50415c2016-08-18 17:16:52 +0100441 void *vaddr;
Chris Wilson31a39202016-08-18 17:16:46 +0100442
Chris Wilsond50415c2016-08-18 17:16:52 +0100443 if (cache->vaddr) {
Jani Nikula615e5002016-10-04 12:54:13 +0300444 io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
Chris Wilsond50415c2016-08-18 17:16:52 +0100445 } else {
446 struct i915_vma *vma;
447 int ret;
Chris Wilson31a39202016-08-18 17:16:46 +0100448
Chris Wilsond50415c2016-08-18 17:16:52 +0100449 if (use_cpu_reloc(obj))
450 return NULL;
Chris Wilson31a39202016-08-18 17:16:46 +0100451
Chris Wilsond50415c2016-08-18 17:16:52 +0100452 ret = i915_gem_object_set_to_gtt_domain(obj, true);
453 if (ret)
454 return ERR_PTR(ret);
Chris Wilson31a39202016-08-18 17:16:46 +0100455
Chris Wilsond50415c2016-08-18 17:16:52 +0100456 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
457 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100458 if (IS_ERR(vma)) {
459 memset(&cache->node, 0, sizeof(cache->node));
Chris Wilson4e64e552017-02-02 21:04:38 +0000460 ret = drm_mm_insert_node_in_range
Chris Wilsone8cb9092016-08-18 17:16:53 +0100461 (&ggtt->base.mm, &cache->node,
Chris Wilsonf51455d2017-01-10 14:47:34 +0000462 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
Chris Wilsone8cb9092016-08-18 17:16:53 +0100463 0, ggtt->mappable_end,
Chris Wilson4e64e552017-02-02 21:04:38 +0000464 DRM_MM_INSERT_LOW);
Chris Wilsonc92fa4f2016-10-07 07:53:25 +0100465 if (ret) /* no inactive aperture space, use cpu reloc */
466 return NULL;
Chris Wilsone8cb9092016-08-18 17:16:53 +0100467 } else {
Chris Wilson49ef5292016-08-18 17:17:00 +0100468 ret = i915_vma_put_fence(vma);
Chris Wilsone8cb9092016-08-18 17:16:53 +0100469 if (ret) {
470 i915_vma_unpin(vma);
471 return ERR_PTR(ret);
472 }
Rafael Barbalho5032d872013-08-21 17:10:51 +0100473
Chris Wilsone8cb9092016-08-18 17:16:53 +0100474 cache->node.start = vma->node.start;
475 cache->node.mm = (void *)vma;
Chris Wilsond50415c2016-08-18 17:16:52 +0100476 }
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700477 }
478
Chris Wilsone8cb9092016-08-18 17:16:53 +0100479 offset = cache->node.start;
480 if (cache->node.allocated) {
Chris Wilsonfc099092016-10-28 15:27:56 +0100481 wmb();
Chris Wilsone8cb9092016-08-18 17:16:53 +0100482 ggtt->base.insert_page(&ggtt->base,
483 i915_gem_object_get_dma_address(obj, page),
484 offset, I915_CACHE_NONE, 0);
485 } else {
486 offset += page << PAGE_SHIFT;
487 }
488
Chris Wilson650bc632017-06-15 09:14:33 +0100489 vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->mappable,
490 offset);
Chris Wilsond50415c2016-08-18 17:16:52 +0100491 cache->page = page;
492 cache->vaddr = (unsigned long)vaddr;
493
494 return vaddr;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100495}
496
Chris Wilsond50415c2016-08-18 17:16:52 +0100497static void *reloc_vaddr(struct drm_i915_gem_object *obj,
498 struct reloc_cache *cache,
499 int page)
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000500{
Chris Wilsond50415c2016-08-18 17:16:52 +0100501 void *vaddr;
502
503 if (cache->page == page) {
504 vaddr = unmask_page(cache->vaddr);
505 } else {
506 vaddr = NULL;
507 if ((cache->vaddr & KMAP) == 0)
508 vaddr = reloc_iomap(obj, cache, page);
509 if (!vaddr)
510 vaddr = reloc_kmap(obj, cache, page);
511 }
512
513 return vaddr;
514}
515
516static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
517{
518 if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
519 if (flushes & CLFLUSH_BEFORE) {
520 clflushopt(addr);
521 mb();
522 }
523
524 *addr = value;
525
526 /* Writes to the same cacheline are serialised by the CPU
527 * (including clflush). On the write path, we only require
528 * that it hits memory in an orderly fashion and place
529 * mb barriers at the start and end of the relocation phase
530 * to ensure ordering of clflush wrt to the system.
531 */
532 if (flushes & CLFLUSH_AFTER)
533 clflushopt(addr);
534 } else
535 *addr = value;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000536}
537
538static int
Chris Wilsond50415c2016-08-18 17:16:52 +0100539relocate_entry(struct drm_i915_gem_object *obj,
540 const struct drm_i915_gem_relocation_entry *reloc,
541 struct reloc_cache *cache,
542 u64 target_offset)
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000543{
Chris Wilsond50415c2016-08-18 17:16:52 +0100544 u64 offset = reloc->offset;
545 bool wide = cache->use_64bit_reloc;
546 void *vaddr;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000547
Chris Wilsond50415c2016-08-18 17:16:52 +0100548 target_offset = relocation_target(reloc, target_offset);
549repeat:
550 vaddr = reloc_vaddr(obj, cache, offset >> PAGE_SHIFT);
551 if (IS_ERR(vaddr))
552 return PTR_ERR(vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000553
Chris Wilsond50415c2016-08-18 17:16:52 +0100554 clflush_write32(vaddr + offset_in_page(offset),
555 lower_32_bits(target_offset),
556 cache->vaddr);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000557
Chris Wilsond50415c2016-08-18 17:16:52 +0100558 if (wide) {
559 offset += sizeof(u32);
560 target_offset >>= 32;
561 wide = false;
562 goto repeat;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000563 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000564
565 return 0;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100566}
567
Rafael Barbalho5032d872013-08-21 17:10:51 +0100568static int
Chris Wilson650bc632017-06-15 09:14:33 +0100569eb_relocate_entry(struct drm_i915_gem_object *obj,
570 struct i915_execbuffer *eb,
571 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000572{
Chris Wilson54cf91d2010-11-25 18:00:26 +0000573 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100574 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200575 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700576 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800577 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000578
Chris Wilson67731b82010-12-08 10:38:14 +0000579 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200580 target_vma = eb_get_vma(eb, reloc->target_handle);
581 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000582 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200583 target_i915_obj = target_vma->obj;
584 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000585
Michał Winiarski934acce2015-12-29 18:24:52 +0100586 target_offset = gen8_canonical_addr(target_vma->node.start);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000587
Eric Anholte844b992012-07-31 15:35:01 -0700588 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
589 * pipe_control writes because the gpu doesn't properly redirect them
590 * through the ppgtt for non_secure batchbuffers. */
Chris Wilson650bc632017-06-15 09:14:33 +0100591 if (unlikely(IS_GEN6(eb->i915) &&
592 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000593 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
Daniel Vetter08755462015-04-20 09:04:05 -0700594 PIN_GLOBAL);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000595 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
596 return ret;
597 }
Eric Anholte844b992012-07-31 15:35:01 -0700598
Chris Wilson54cf91d2010-11-25 18:00:26 +0000599 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000600 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100601 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000602 "obj %p target %d offset %d "
603 "read %08x write %08x",
604 obj, reloc->target_handle,
605 (int) reloc->offset,
606 reloc->read_domains,
607 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800608 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000609 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100610 if (unlikely((reloc->write_domain | reloc->read_domains)
611 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100612 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000613 "obj %p target %d offset %d "
614 "read %08x write %08x",
615 obj, reloc->target_handle,
616 (int) reloc->offset,
617 reloc->read_domains,
618 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800619 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000620 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000621
622 target_obj->pending_read_domains |= reloc->read_domains;
623 target_obj->pending_write_domain |= reloc->write_domain;
624
625 /* If the relocation already has the right value in it, no
626 * more work needs to be done.
627 */
628 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000629 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000630
631 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700632 if (unlikely(reloc->offset >
Chris Wilson650bc632017-06-15 09:14:33 +0100633 obj->base.size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100634 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000635 "obj %p target %d offset %d size %d.\n",
636 obj, reloc->target_handle,
637 (int) reloc->offset,
638 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800639 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000640 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000641 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100642 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000643 "obj %p target %d offset %d.\n",
644 obj, reloc->target_handle,
645 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800646 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000647 }
648
Chris Wilson650bc632017-06-15 09:14:33 +0100649 ret = relocate_entry(obj, reloc, &eb->reloc_cache, target_offset);
Daniel Vetterd4d36012013-09-02 20:56:23 +0200650 if (ret)
651 return ret;
652
Chris Wilson54cf91d2010-11-25 18:00:26 +0000653 /* and update the user's relocation entry */
654 reloc->presumed_offset = target_offset;
Chris Wilson67731b82010-12-08 10:38:14 +0000655 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000656}
657
Chris Wilson650bc632017-06-15 09:14:33 +0100658static int eb_relocate_vma(struct i915_vma *vma, struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000659{
Chris Wilson1d83f442012-03-24 20:12:53 +0000660#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
661 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000662 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200663 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson31a39202016-08-18 17:16:46 +0100664 int remain, ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000665
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300666 user_relocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000667
Chris Wilson1d83f442012-03-24 20:12:53 +0000668 remain = entry->relocation_count;
669 while (remain) {
670 struct drm_i915_gem_relocation_entry *r = stack_reloc;
Chris Wilsonebc08082016-10-18 13:02:51 +0100671 unsigned long unwritten;
672 unsigned int count;
673
674 count = min_t(unsigned int, remain, ARRAY_SIZE(stack_reloc));
Chris Wilson1d83f442012-03-24 20:12:53 +0000675 remain -= count;
676
Chris Wilsonebc08082016-10-18 13:02:51 +0100677 /* This is the fast path and we cannot handle a pagefault
678 * whilst holding the struct mutex lest the user pass in the
679 * relocations contained within a mmaped bo. For in such a case
680 * we, the page fault handler would call i915_gem_fault() and
681 * we would try to acquire the struct mutex again. Obviously
682 * this is bad and so lockdep complains vehemently.
683 */
684 pagefault_disable();
685 unwritten = __copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0]));
686 pagefault_enable();
687 if (unlikely(unwritten)) {
Chris Wilson31a39202016-08-18 17:16:46 +0100688 ret = -EFAULT;
689 goto out;
690 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000691
Chris Wilson1d83f442012-03-24 20:12:53 +0000692 do {
693 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000694
Chris Wilson650bc632017-06-15 09:14:33 +0100695 ret = eb_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000696 if (ret)
Chris Wilson31a39202016-08-18 17:16:46 +0100697 goto out;
Chris Wilson1d83f442012-03-24 20:12:53 +0000698
Chris Wilsonebc08082016-10-18 13:02:51 +0100699 if (r->presumed_offset != offset) {
700 pagefault_disable();
701 unwritten = __put_user(r->presumed_offset,
702 &user_relocs->presumed_offset);
703 pagefault_enable();
704 if (unlikely(unwritten)) {
705 /* Note that reporting an error now
706 * leaves everything in an inconsistent
707 * state as we have *already* changed
708 * the relocation value inside the
709 * object. As we have not changed the
710 * reloc.presumed_offset or will not
711 * change the execobject.offset, on the
712 * call we may not rewrite the value
713 * inside the object, leaving it
714 * dangling and causing a GPU hang.
715 */
716 ret = -EFAULT;
717 goto out;
718 }
Chris Wilson1d83f442012-03-24 20:12:53 +0000719 }
720
721 user_relocs++;
722 r++;
723 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000724 }
725
Chris Wilson31a39202016-08-18 17:16:46 +0100726out:
Chris Wilson650bc632017-06-15 09:14:33 +0100727 reloc_cache_reset(&eb->reloc_cache);
Chris Wilson31a39202016-08-18 17:16:46 +0100728 return ret;
Chris Wilson1d83f442012-03-24 20:12:53 +0000729#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000730}
731
732static int
Chris Wilson650bc632017-06-15 09:14:33 +0100733eb_relocate_vma_slow(struct i915_vma *vma,
734 struct i915_execbuffer *eb,
735 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000736{
Ben Widawsky27173f12013-08-14 11:38:36 +0200737 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson31a39202016-08-18 17:16:46 +0100738 int i, ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000739
740 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson650bc632017-06-15 09:14:33 +0100741 ret = eb_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000742 if (ret)
Chris Wilson31a39202016-08-18 17:16:46 +0100743 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000744 }
Chris Wilson650bc632017-06-15 09:14:33 +0100745 reloc_cache_reset(&eb->reloc_cache);
Chris Wilson31a39202016-08-18 17:16:46 +0100746 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000747}
748
Chris Wilson650bc632017-06-15 09:14:33 +0100749static int eb_relocate(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000750{
Ben Widawsky27173f12013-08-14 11:38:36 +0200751 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000752 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000753
Chris Wilson8c45cec2017-06-15 09:14:35 +0100754 list_for_each_entry(vma, &eb->vmas, exec_link) {
Chris Wilson650bc632017-06-15 09:14:33 +0100755 ret = eb_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000756 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000757 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000758 }
759
Chris Wilsond4aeee72011-03-14 15:11:24 +0000760 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000761}
762
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000763static bool only_mappable_for_reloc(unsigned int flags)
764{
765 return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
766 __EXEC_OBJECT_NEEDS_MAP;
767}
768
Chris Wilson1690e1e2011-12-14 13:57:08 +0100769static int
Chris Wilson650bc632017-06-15 09:14:33 +0100770eb_reserve_vma(struct i915_vma *vma,
771 struct intel_engine_cs *engine,
772 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100773{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800774 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200775 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200776 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100777 int ret;
778
Daniel Vetter08755462015-04-20 09:04:05 -0700779 flags = PIN_USER;
Daniel Vetter0229da32015-04-14 19:01:54 +0200780 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
781 flags |= PIN_GLOBAL;
782
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000783 if (!drm_mm_node_allocated(&vma->node)) {
Michel Thierry101b5062015-10-01 13:33:57 +0100784 /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
785 * limit address to the first 4GBs for unflagged objects.
786 */
787 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
788 flags |= PIN_ZONE_4G;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000789 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
790 flags |= PIN_GLOBAL | PIN_MAPPABLE;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000791 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
792 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson506a8e82015-12-08 11:55:07 +0000793 if (entry->flags & EXEC_OBJECT_PINNED)
794 flags |= entry->offset | PIN_OFFSET_FIXED;
Michel Thierry101b5062015-10-01 13:33:57 +0100795 if ((flags & PIN_MAPPABLE) == 0)
796 flags |= PIN_HIGH;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000797 }
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100798
Chris Wilson59bfa122016-08-04 16:32:31 +0100799 ret = i915_vma_pin(vma,
800 entry->pad_to_size,
801 entry->alignment,
802 flags);
803 if ((ret == -ENOSPC || ret == -E2BIG) &&
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000804 only_mappable_for_reloc(entry->flags))
Chris Wilson59bfa122016-08-04 16:32:31 +0100805 ret = i915_vma_pin(vma,
806 entry->pad_to_size,
807 entry->alignment,
808 flags & ~PIN_MAPPABLE);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100809 if (ret)
810 return ret;
811
Chris Wilson7788a762012-08-24 19:18:18 +0100812 entry->flags |= __EXEC_OBJECT_HAS_PIN;
813
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100814 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson49ef5292016-08-18 17:17:00 +0100815 ret = i915_vma_get_fence(vma);
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100816 if (ret)
817 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100818
Chris Wilson49ef5292016-08-18 17:17:00 +0100819 if (i915_vma_pin_fence(vma))
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100820 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100821 }
822
Ben Widawsky27173f12013-08-14 11:38:36 +0200823 if (entry->offset != vma->node.start) {
824 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100825 *need_reloc = true;
826 }
827
828 if (entry->flags & EXEC_OBJECT_WRITE) {
829 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
830 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
831 }
832
Chris Wilson1690e1e2011-12-14 13:57:08 +0100833 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100834}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100835
Chris Wilsond23db882014-05-23 08:48:08 +0200836static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200837need_reloc_mappable(struct i915_vma *vma)
838{
839 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
840
841 if (entry->relocation_count == 0)
842 return false;
843
Chris Wilson3272db52016-08-04 16:32:32 +0100844 if (!i915_vma_is_ggtt(vma))
Chris Wilsone6a84462014-08-11 12:00:12 +0200845 return false;
846
847 /* See also use_cpu_reloc() */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +0000848 if (HAS_LLC(to_i915(vma->obj->base.dev)))
Chris Wilsone6a84462014-08-11 12:00:12 +0200849 return false;
850
851 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
852 return false;
853
854 return true;
855}
856
857static bool
858eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200859{
860 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200861
Chris Wilson3272db52016-08-04 16:32:32 +0100862 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
863 !i915_vma_is_ggtt(vma));
Chris Wilsond23db882014-05-23 08:48:08 +0200864
Chris Wilsonf51455d2017-01-10 14:47:34 +0000865 if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment))
Chris Wilsond23db882014-05-23 08:48:08 +0200866 return true;
867
Chris Wilson91b2db62016-08-04 16:32:23 +0100868 if (vma->node.size < entry->pad_to_size)
869 return true;
870
Chris Wilson506a8e82015-12-08 11:55:07 +0000871 if (entry->flags & EXEC_OBJECT_PINNED &&
872 vma->node.start != entry->offset)
873 return true;
874
Chris Wilsond23db882014-05-23 08:48:08 +0200875 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
876 vma->node.start < BATCH_OFFSET_BIAS)
877 return true;
878
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000879 /* avoid costly ping-pong once a batch bo ended up non-mappable */
Chris Wilson05a20d02016-08-18 17:16:55 +0100880 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
881 !i915_vma_is_map_and_fenceable(vma))
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000882 return !only_mappable_for_reloc(entry->flags);
883
Michel Thierry101b5062015-10-01 13:33:57 +0100884 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
885 (vma->node.start + vma->node.size - 1) >> 32)
886 return true;
887
Chris Wilsond23db882014-05-23 08:48:08 +0200888 return false;
889}
890
Chris Wilson650bc632017-06-15 09:14:33 +0100891static int eb_reserve(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000892{
Chris Wilson650bc632017-06-15 09:14:33 +0100893 const bool has_fenced_gpu_access = INTEL_GEN(eb->i915) < 4;
894 const bool needs_unfenced_map = INTEL_INFO(eb->i915)->unfenced_needs_alignment;
Chris Wilson432e58e2010-11-25 19:32:06 +0000895 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200896 struct i915_vma *vma;
897 struct list_head ordered_vmas;
Chris Wilson506a8e82015-12-08 11:55:07 +0000898 struct list_head pinned_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100899 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000900
Ben Widawsky27173f12013-08-14 11:38:36 +0200901 INIT_LIST_HEAD(&ordered_vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000902 INIT_LIST_HEAD(&pinned_vmas);
Chris Wilson650bc632017-06-15 09:14:33 +0100903 while (!list_empty(&eb->vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000904 struct drm_i915_gem_exec_object2 *entry;
905 bool need_fence, need_mappable;
906
Chris Wilson8c45cec2017-06-15 09:14:35 +0100907 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200908 obj = vma->obj;
909 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000910
Chris Wilson650bc632017-06-15 09:14:33 +0100911 if (eb->ctx->flags & CONTEXT_NO_ZEROMAP)
David Weinehallb1b38272015-05-20 17:00:13 +0300912 entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
913
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100914 if (!has_fenced_gpu_access)
915 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000916 need_fence =
Chris Wilsonf4ce7662017-03-25 11:32:43 +0000917 (entry->flags & EXEC_OBJECT_NEEDS_FENCE ||
918 needs_unfenced_map) &&
Chris Wilson3e510a82016-08-05 10:14:23 +0100919 i915_gem_object_is_tiled(obj);
Ben Widawsky27173f12013-08-14 11:38:36 +0200920 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000921
Chris Wilson506a8e82015-12-08 11:55:07 +0000922 if (entry->flags & EXEC_OBJECT_PINNED)
Chris Wilson8c45cec2017-06-15 09:14:35 +0100923 list_move_tail(&vma->exec_link, &pinned_vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000924 else if (need_mappable) {
Chris Wilsone6a84462014-08-11 12:00:12 +0200925 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Chris Wilson8c45cec2017-06-15 09:14:35 +0100926 list_move(&vma->exec_link, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200927 } else
Chris Wilson8c45cec2017-06-15 09:14:35 +0100928 list_move_tail(&vma->exec_link, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000929
Daniel Vettered5982e2013-01-17 22:23:36 +0100930 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000931 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000932 }
Chris Wilson650bc632017-06-15 09:14:33 +0100933 list_splice(&ordered_vmas, &eb->vmas);
934 list_splice(&pinned_vmas, &eb->vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000935
936 /* Attempt to pin all of the buffers into the GTT.
937 * This is done in 3 phases:
938 *
939 * 1a. Unbind all objects that do not match the GTT constraints for
940 * the execbuffer (fenceable, mappable, alignment etc).
941 * 1b. Increment pin count for already bound objects.
942 * 2. Bind new objects.
943 * 3. Decrement pin count.
944 *
Chris Wilson7788a762012-08-24 19:18:18 +0100945 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000946 * room for the earlier objects *unless* we need to defragment.
947 */
948 retry = 0;
949 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100950 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000951
952 /* Unbind any ill-fitting objects or pin. */
Chris Wilson8c45cec2017-06-15 09:14:35 +0100953 list_for_each_entry(vma, &eb->vmas, exec_link) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200954 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000955 continue;
956
Chris Wilsone6a84462014-08-11 12:00:12 +0200957 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200958 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000959 else
Chris Wilson650bc632017-06-15 09:14:33 +0100960 ret = eb_reserve_vma(vma, eb->engine, &eb->need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000961 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000962 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000963 }
964
965 /* Bind fresh objects */
Chris Wilson8c45cec2017-06-15 09:14:35 +0100966 list_for_each_entry(vma, &eb->vmas, exec_link) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200967 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100968 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000969
Chris Wilson650bc632017-06-15 09:14:33 +0100970 ret = eb_reserve_vma(vma, eb->engine, &eb->need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100971 if (ret)
972 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000973 }
974
Chris Wilsona415d352013-11-26 11:23:15 +0000975err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200976 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000977 return ret;
978
Chris Wilsona415d352013-11-26 11:23:15 +0000979 /* Decrement pin count for bound objects */
Chris Wilson8c45cec2017-06-15 09:14:35 +0100980 list_for_each_entry(vma, &eb->vmas, exec_link)
Chris Wilson650bc632017-06-15 09:14:33 +0100981 eb_unreserve_vma(vma);
Chris Wilsona415d352013-11-26 11:23:15 +0000982
Chris Wilson650bc632017-06-15 09:14:33 +0100983 ret = i915_gem_evict_vm(eb->vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000984 if (ret)
985 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000986 } while (1);
987}
988
989static int
Chris Wilson650bc632017-06-15 09:14:33 +0100990eb_relocate_slow(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000991{
Chris Wilson650bc632017-06-15 09:14:33 +0100992 const unsigned int count = eb->args->buffer_count;
993 struct drm_device *dev = &eb->i915->drm;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000994 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200995 struct i915_vma *vma;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000996 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000997 int i, total, ret;
Ben Widawsky27173f12013-08-14 11:38:36 +0200998
Chris Wilson67731b82010-12-08 10:38:14 +0000999 /* We may process another execbuffer during the unlock... */
Chris Wilsond55495b2017-06-15 09:14:34 +01001000 eb_reset(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001001 mutex_unlock(&dev->struct_mutex);
1002
1003 total = 0;
1004 for (i = 0; i < count; i++)
Chris Wilson650bc632017-06-15 09:14:33 +01001005 total += eb->exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001006
Michal Hocko20981052017-05-17 14:23:12 +02001007 reloc_offset = kvmalloc_array(count, sizeof(*reloc_offset), GFP_KERNEL);
1008 reloc = kvmalloc_array(total, sizeof(*reloc), GFP_KERNEL);
Chris Wilsondd6864a2011-01-12 23:49:13 +00001009 if (reloc == NULL || reloc_offset == NULL) {
Michal Hocko20981052017-05-17 14:23:12 +02001010 kvfree(reloc);
1011 kvfree(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001012 mutex_lock(&dev->struct_mutex);
1013 return -ENOMEM;
1014 }
1015
1016 total = 0;
1017 for (i = 0; i < count; i++) {
1018 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +00001019 u64 invalid_offset = (u64)-1;
1020 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001021
Chris Wilson650bc632017-06-15 09:14:33 +01001022 user_relocs = u64_to_user_ptr(eb->exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001023
1024 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson650bc632017-06-15 09:14:33 +01001025 eb->exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001026 ret = -EFAULT;
1027 mutex_lock(&dev->struct_mutex);
1028 goto err;
1029 }
1030
Chris Wilson262b6d32013-01-15 16:17:54 +00001031 /* As we do not update the known relocation offsets after
1032 * relocating (due to the complexities in lock handling),
1033 * we need to mark them as invalid now so that we force the
1034 * relocation processing next time. Just in case the target
1035 * object is evicted and then rebound into its old
1036 * presumed_offset before the next execbuffer - if that
1037 * happened we would make the mistake of assuming that the
1038 * relocations were valid.
1039 */
Chris Wilson650bc632017-06-15 09:14:33 +01001040 for (j = 0; j < eb->exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001041 if (__copy_to_user(&user_relocs[j].presumed_offset,
1042 &invalid_offset,
1043 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +00001044 ret = -EFAULT;
1045 mutex_lock(&dev->struct_mutex);
1046 goto err;
1047 }
1048 }
1049
Chris Wilsondd6864a2011-01-12 23:49:13 +00001050 reloc_offset[i] = total;
Chris Wilson650bc632017-06-15 09:14:33 +01001051 total += eb->exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001052 }
1053
1054 ret = i915_mutex_lock_interruptible(dev);
1055 if (ret) {
1056 mutex_lock(&dev->struct_mutex);
1057 goto err;
1058 }
1059
Chris Wilson67731b82010-12-08 10:38:14 +00001060 /* reacquire the objects */
Chris Wilson650bc632017-06-15 09:14:33 +01001061 ret = eb_lookup_vmas(eb);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001062 if (ret)
1063 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +00001064
Chris Wilson650bc632017-06-15 09:14:33 +01001065 ret = eb_reserve(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001066 if (ret)
1067 goto err;
1068
Chris Wilson8c45cec2017-06-15 09:14:35 +01001069 list_for_each_entry(vma, &eb->vmas, exec_link) {
Chris Wilson650bc632017-06-15 09:14:33 +01001070 int idx = vma->exec_entry - eb->exec;
1071
1072 ret = eb_relocate_vma_slow(vma, eb, reloc + reloc_offset[idx]);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001073 if (ret)
1074 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001075 }
1076
1077 /* Leave the user relocations as are, this is the painfully slow path,
1078 * and we want to avoid the complication of dropping the lock whilst
1079 * having buffers reserved in the aperture and so causing spurious
1080 * ENOSPC for random operations.
1081 */
1082
1083err:
Michal Hocko20981052017-05-17 14:23:12 +02001084 kvfree(reloc);
1085 kvfree(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001086 return ret;
1087}
1088
Chris Wilson54cf91d2010-11-25 18:00:26 +00001089static int
Chris Wilson650bc632017-06-15 09:14:33 +01001090eb_move_to_gpu(struct i915_execbuffer *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001091{
Ben Widawsky27173f12013-08-14 11:38:36 +02001092 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +00001093 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001094
Chris Wilson8c45cec2017-06-15 09:14:35 +01001095 list_for_each_entry(vma, &eb->vmas, exec_link) {
Ben Widawsky27173f12013-08-14 11:38:36 +02001096 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001097
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001098 if (vma->exec_entry->flags & EXEC_OBJECT_CAPTURE) {
1099 struct i915_gem_capture_list *capture;
1100
1101 capture = kmalloc(sizeof(*capture), GFP_KERNEL);
1102 if (unlikely(!capture))
1103 return -ENOMEM;
1104
Chris Wilson650bc632017-06-15 09:14:33 +01001105 capture->next = eb->request->capture_list;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001106 capture->vma = vma;
Chris Wilson650bc632017-06-15 09:14:33 +01001107 eb->request->capture_list = capture;
Chris Wilsonb0fd47a2017-04-15 10:39:02 +01001108 }
1109
Chris Wilson77ae9952017-01-27 09:40:07 +00001110 if (vma->exec_entry->flags & EXEC_OBJECT_ASYNC)
1111 continue;
1112
Chris Wilsone27ab732017-06-15 13:38:49 +01001113 if (obj->cache_dirty)
Chris Wilson57822dc2017-02-22 11:40:48 +00001114 i915_gem_clflush_object(obj, 0);
Chris Wilson57822dc2017-02-22 11:40:48 +00001115
Chris Wilsond07f0e52016-10-28 13:58:44 +01001116 ret = i915_gem_request_await_object
Chris Wilson650bc632017-06-15 09:14:33 +01001117 (eb->request, obj, obj->base.pending_write_domain);
Chris Wilsond07f0e52016-10-28 13:58:44 +01001118 if (ret)
1119 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001120 }
1121
Chris Wilsondcd79932016-08-18 17:16:40 +01001122 /* Unconditionally flush any chipset caches (for streaming writes). */
Chris Wilson650bc632017-06-15 09:14:33 +01001123 i915_gem_chipset_flush(eb->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001124
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001125 /* Unconditionally invalidate GPU caches and TLBs. */
Chris Wilson650bc632017-06-15 09:14:33 +01001126 return eb->engine->emit_flush(eb->request, EMIT_INVALIDATE);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001127}
1128
Chris Wilson432e58e2010-11-25 19:32:06 +00001129static bool
1130i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001131{
Chris Wilson650bc632017-06-15 09:14:33 +01001132 if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS)
Daniel Vettered5982e2013-01-17 22:23:36 +01001133 return false;
1134
Chris Wilson2f5945b2015-10-06 11:39:55 +01001135 /* Kernel clipping was a DRI1 misfeature */
1136 if (exec->num_cliprects || exec->cliprects_ptr)
1137 return false;
1138
1139 if (exec->DR4 == 0xffffffff) {
1140 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1141 exec->DR4 = 0;
1142 }
1143 if (exec->DR1 || exec->DR4)
1144 return false;
1145
1146 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1147 return false;
1148
1149 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001150}
1151
1152static int
Chris Wilsonad19f102014-08-10 06:29:08 +01001153validate_exec_list(struct drm_device *dev,
1154 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001155 int count)
1156{
Daniel Vetterb205ca52013-09-19 14:00:11 +02001157 unsigned relocs_total = 0;
1158 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +01001159 unsigned invalid_flags;
1160 int i;
1161
Dave Gordon9e2793f62016-07-14 14:52:03 +01001162 /* INTERNAL flags must not overlap with external ones */
1163 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
1164
Chris Wilsonad19f102014-08-10 06:29:08 +01001165 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
1166 if (USES_FULL_PPGTT(dev))
1167 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001168
1169 for (i = 0; i < count; i++) {
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001170 char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001171 int length; /* limited by fault_in_pages_readable() */
1172
Chris Wilsonad19f102014-08-10 06:29:08 +01001173 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +01001174 return -EINVAL;
1175
Michał Winiarski934acce2015-12-29 18:24:52 +01001176 /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
1177 * any non-page-aligned or non-canonical addresses.
1178 */
1179 if (exec[i].flags & EXEC_OBJECT_PINNED) {
1180 if (exec[i].offset !=
1181 gen8_canonical_addr(exec[i].offset & PAGE_MASK))
1182 return -EINVAL;
Michał Winiarski934acce2015-12-29 18:24:52 +01001183 }
1184
Michał Winiarski038c95a2017-02-07 20:55:59 +01001185 /* From drm_mm perspective address space is continuous,
1186 * so from this point we're always using non-canonical
1187 * form internally.
1188 */
1189 exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
1190
Chris Wilson55a97852015-06-19 13:59:46 +01001191 if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
1192 return -EINVAL;
1193
Chris Wilson91b2db62016-08-04 16:32:23 +01001194 /* pad_to_size was once a reserved field, so sanitize it */
1195 if (exec[i].flags & EXEC_OBJECT_PAD_TO_SIZE) {
1196 if (offset_in_page(exec[i].pad_to_size))
1197 return -EINVAL;
1198 } else {
1199 exec[i].pad_to_size = 0;
1200 }
1201
Kees Cook3118a4f2013-03-11 17:31:45 -07001202 /* First check for malicious input causing overflow in
1203 * the worst case where we need to allocate the entire
1204 * relocation tree as a single array.
1205 */
1206 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001207 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -07001208 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001209
1210 length = exec[i].relocation_count *
1211 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -07001212 /*
1213 * We must check that the entire relocation array is safe
1214 * to read, but since we may need to update the presumed
1215 * offsets during execution, check for full write access.
1216 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001217 if (!access_ok(VERIFY_WRITE, ptr, length))
1218 return -EFAULT;
1219
Jani Nikulad330a952014-01-21 11:24:25 +02001220 if (likely(!i915.prefault_disable)) {
Al Viro4bce9f62016-09-17 18:02:44 -04001221 if (fault_in_pages_readable(ptr, length))
Xiong Zhang0b74b502013-07-19 13:51:24 +08001222 return -EFAULT;
1223 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001224 }
1225
1226 return 0;
1227}
1228
Chris Wilson650bc632017-06-15 09:14:33 +01001229static int eb_select_context(struct i915_execbuffer *eb)
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001230{
Chris Wilson650bc632017-06-15 09:14:33 +01001231 unsigned int ctx_id = i915_execbuffer2_get_context_id(*eb->args);
Chris Wilsonf7978a02016-08-22 09:03:36 +01001232 struct i915_gem_context *ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001233
Chris Wilson650bc632017-06-15 09:14:33 +01001234 ctx = i915_gem_context_lookup(eb->file->driver_priv, ctx_id);
1235 if (unlikely(IS_ERR(ctx)))
1236 return PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001237
Chris Wilson650bc632017-06-15 09:14:33 +01001238 if (unlikely(i915_gem_context_is_banned(ctx))) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001239 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Chris Wilson650bc632017-06-15 09:14:33 +01001240 return -EIO;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001241 }
1242
Chris Wilson650bc632017-06-15 09:14:33 +01001243 eb->ctx = i915_gem_context_get(ctx);
1244 eb->vm = ctx->ppgtt ? &ctx->ppgtt->base : &eb->i915->ggtt.base;
1245
1246 return 0;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001247}
1248
Chris Wilson5cf3d282016-08-04 07:52:43 +01001249void i915_vma_move_to_active(struct i915_vma *vma,
1250 struct drm_i915_gem_request *req,
1251 unsigned int flags)
1252{
1253 struct drm_i915_gem_object *obj = vma->obj;
1254 const unsigned int idx = req->engine->id;
1255
Chris Wilson81147b02016-12-18 15:37:18 +00001256 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001257 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1258
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001259 /* Add a reference if we're newly entering the active list.
1260 * The order in which we add operations to the retirement queue is
1261 * vital here: mark_active adds to the start of the callback list,
1262 * such that subsequent callbacks are called first. Therefore we
1263 * add the active reference first and queue for it to be dropped
1264 * *last*.
1265 */
Chris Wilsond07f0e52016-10-28 13:58:44 +01001266 if (!i915_vma_is_active(vma))
1267 obj->active_count++;
1268 i915_vma_set_active(vma, idx);
1269 i915_gem_active_set(&vma->last_read[idx], req);
1270 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001271
Chris Wilsone27ab732017-06-15 13:38:49 +01001272 obj->base.write_domain = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001273 if (flags & EXEC_OBJECT_WRITE) {
Chris Wilsone27ab732017-06-15 13:38:49 +01001274 obj->base.write_domain = I915_GEM_DOMAIN_RENDER;
1275
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00001276 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
1277 i915_gem_active_set(&obj->frontbuffer_write, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001278
Chris Wilsone27ab732017-06-15 13:38:49 +01001279 obj->base.read_domains = 0;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001280 }
Chris Wilsone27ab732017-06-15 13:38:49 +01001281 obj->base.read_domains |= I915_GEM_GPU_DOMAINS;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001282
Chris Wilson49ef5292016-08-18 17:17:00 +01001283 if (flags & EXEC_OBJECT_NEEDS_FENCE)
1284 i915_gem_active_set(&vma->last_fence, req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001285}
1286
Chris Wilsonad778f82016-08-04 16:32:42 +01001287static void eb_export_fence(struct drm_i915_gem_object *obj,
1288 struct drm_i915_gem_request *req,
1289 unsigned int flags)
1290{
Chris Wilsond07f0e52016-10-28 13:58:44 +01001291 struct reservation_object *resv = obj->resv;
Chris Wilsonad778f82016-08-04 16:32:42 +01001292
1293 /* Ignore errors from failing to allocate the new fence, we can't
1294 * handle an error right now. Worst case should be missed
1295 * synchronisation leading to rendering corruption.
1296 */
Chris Wilsone2989f12017-02-21 09:17:23 +00001297 reservation_object_lock(resv, NULL);
Chris Wilsonad778f82016-08-04 16:32:42 +01001298 if (flags & EXEC_OBJECT_WRITE)
1299 reservation_object_add_excl_fence(resv, &req->fence);
1300 else if (reservation_object_reserve_shared(resv) == 0)
1301 reservation_object_add_shared_fence(resv, &req->fence);
Chris Wilsone2989f12017-02-21 09:17:23 +00001302 reservation_object_unlock(resv);
Chris Wilsonad778f82016-08-04 16:32:42 +01001303}
1304
Chris Wilson5b043f42016-08-02 22:50:38 +01001305static void
Chris Wilson650bc632017-06-15 09:14:33 +01001306eb_move_to_active(struct i915_execbuffer *eb)
Chris Wilson432e58e2010-11-25 19:32:06 +00001307{
Ben Widawsky27173f12013-08-14 11:38:36 +02001308 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +00001309
Chris Wilson8c45cec2017-06-15 09:14:35 +01001310 list_for_each_entry(vma, &eb->vmas, exec_link) {
Ben Widawsky27173f12013-08-14 11:38:36 +02001311 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsondb53a302011-02-03 11:57:46 +00001312
Chris Wilson432e58e2010-11-25 19:32:06 +00001313 obj->base.write_domain = obj->base.pending_write_domain;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001314 if (obj->base.write_domain)
1315 vma->exec_entry->flags |= EXEC_OBJECT_WRITE;
1316 else
Daniel Vettered5982e2013-01-17 22:23:36 +01001317 obj->base.pending_read_domains |= obj->base.read_domains;
1318 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +00001319
Chris Wilson650bc632017-06-15 09:14:33 +01001320 i915_vma_move_to_active(vma, eb->request, vma->exec_entry->flags);
1321 eb_export_fence(obj, eb->request, vma->exec_entry->flags);
Chris Wilson432e58e2010-11-25 19:32:06 +00001322 }
1323}
1324
Chris Wilson54cf91d2010-11-25 18:00:26 +00001325static int
Chris Wilsonb5321f32016-08-02 22:50:18 +01001326i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001327{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001328 u32 *cs;
1329 int i;
Eric Anholtae662d32012-01-03 09:23:29 -08001330
Chris Wilsonb5321f32016-08-02 22:50:18 +01001331 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001332 DRM_DEBUG("sol reset is gen7/rcs only\n");
1333 return -EINVAL;
1334 }
Eric Anholtae662d32012-01-03 09:23:29 -08001335
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001336 cs = intel_ring_begin(req, 4 * 3);
1337 if (IS_ERR(cs))
1338 return PTR_ERR(cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001339
1340 for (i = 0; i < 4; i++) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001341 *cs++ = MI_LOAD_REGISTER_IMM(1);
1342 *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
1343 *cs++ = 0;
Eric Anholtae662d32012-01-03 09:23:29 -08001344 }
1345
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001346 intel_ring_advance(req, cs);
Eric Anholtae662d32012-01-03 09:23:29 -08001347
1348 return 0;
1349}
1350
Chris Wilson650bc632017-06-15 09:14:33 +01001351static struct i915_vma *eb_parse(struct i915_execbuffer *eb, bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001352{
Brad Volkin71745372014-12-11 12:13:12 -08001353 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001354 struct i915_vma *vma;
Brad Volkin71745372014-12-11 12:13:12 -08001355 int ret;
1356
Chris Wilson650bc632017-06-15 09:14:33 +01001357 shadow_batch_obj = i915_gem_batch_pool_get(&eb->engine->batch_pool,
1358 PAGE_ALIGN(eb->batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001359 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001360 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001361
Chris Wilson650bc632017-06-15 09:14:33 +01001362 ret = intel_engine_cmd_parser(eb->engine,
1363 eb->batch->obj,
Chris Wilson33a051a2016-07-27 09:07:26 +01001364 shadow_batch_obj,
Chris Wilson650bc632017-06-15 09:14:33 +01001365 eb->batch_start_offset,
1366 eb->batch_len,
Chris Wilson33a051a2016-07-27 09:07:26 +01001367 is_master);
Chris Wilson058d88c2016-08-15 10:49:06 +01001368 if (ret) {
1369 if (ret == -EACCES) /* unhandled chained batch */
1370 vma = NULL;
1371 else
1372 vma = ERR_PTR(ret);
1373 goto out;
1374 }
Brad Volkin71745372014-12-11 12:13:12 -08001375
Chris Wilson058d88c2016-08-15 10:49:06 +01001376 vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
1377 if (IS_ERR(vma))
1378 goto out;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001379
Chris Wilson650bc632017-06-15 09:14:33 +01001380 vma->exec_entry =
1381 memset(&eb->shadow_exec_entry, 0, sizeof(*vma->exec_entry));
Chris Wilsonde4e7832015-04-07 16:20:35 +01001382 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
Chris Wilson25dc5562016-07-20 13:31:52 +01001383 i915_gem_object_get(shadow_batch_obj);
Chris Wilson8c45cec2017-06-15 09:14:35 +01001384 list_add_tail(&vma->exec_link, &eb->vmas);
Brad Volkin71745372014-12-11 12:13:12 -08001385
Chris Wilson058d88c2016-08-15 10:49:06 +01001386out:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001387 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson058d88c2016-08-15 10:49:06 +01001388 return vma;
Brad Volkin71745372014-12-11 12:13:12 -08001389}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001390
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001391static void
1392add_to_client(struct drm_i915_gem_request *req,
1393 struct drm_file *file)
1394{
1395 req->file_priv = file->driver_priv;
1396 list_add_tail(&req->client_link, &req->file_priv->mm.request_list);
1397}
1398
Chris Wilson5b043f42016-08-02 22:50:38 +01001399static int
Chris Wilson650bc632017-06-15 09:14:33 +01001400execbuf_submit(struct i915_execbuffer *eb)
Oscar Mateo78382592014-07-03 16:28:05 +01001401{
Chris Wilson2f5945b2015-10-06 11:39:55 +01001402 int ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001403
Chris Wilson650bc632017-06-15 09:14:33 +01001404 ret = eb_move_to_gpu(eb);
Oscar Mateo78382592014-07-03 16:28:05 +01001405 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001406 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001407
Chris Wilson650bc632017-06-15 09:14:33 +01001408 ret = i915_switch_context(eb->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001409 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001410 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001411
Chris Wilson650bc632017-06-15 09:14:33 +01001412 if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) {
1413 ret = i915_reset_gen7_sol_offsets(eb->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001414 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001415 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001416 }
1417
Chris Wilson650bc632017-06-15 09:14:33 +01001418 ret = eb->engine->emit_bb_start(eb->request,
1419 eb->batch->node.start +
1420 eb->batch_start_offset,
1421 eb->batch_len,
1422 eb->dispatch_flags);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001423 if (ret)
1424 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001425
Chris Wilson650bc632017-06-15 09:14:33 +01001426 eb_move_to_active(eb);
Oscar Mateo78382592014-07-03 16:28:05 +01001427
Chris Wilson2f5945b2015-10-06 11:39:55 +01001428 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001429}
1430
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001431/**
1432 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01001433 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001434 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001435static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01001436gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
1437 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001438{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001439 struct drm_i915_file_private *file_priv = file->driver_priv;
1440
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001441 /* Check whether the file_priv has already selected one ring. */
Joonas Lahtinen6f633402016-09-01 14:58:21 +03001442 if ((int)file_priv->bsd_engine < 0)
1443 file_priv->bsd_engine = atomic_fetch_xor(1,
1444 &dev_priv->mm.bsd_engine_dispatch_index);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001445
Chris Wilsonc80ff162016-07-27 09:07:27 +01001446 return file_priv->bsd_engine;
Chris Wilsond23db882014-05-23 08:48:08 +02001447}
1448
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001449#define I915_USER_RINGS (4)
1450
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00001451static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001452 [I915_EXEC_DEFAULT] = RCS,
1453 [I915_EXEC_RENDER] = RCS,
1454 [I915_EXEC_BLT] = BCS,
1455 [I915_EXEC_BSD] = VCS,
1456 [I915_EXEC_VEBOX] = VECS
1457};
1458
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001459static struct intel_engine_cs *
1460eb_select_engine(struct drm_i915_private *dev_priv,
1461 struct drm_file *file,
1462 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001463{
1464 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001465 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001466
1467 if (user_ring_id > I915_USER_RINGS) {
1468 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001469 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001470 }
1471
1472 if ((user_ring_id != I915_EXEC_BSD) &&
1473 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1474 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1475 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001476 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001477 }
1478
1479 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
1480 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
1481
1482 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01001483 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001484 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
1485 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00001486 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001487 bsd_idx--;
1488 } else {
1489 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
1490 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001491 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001492 }
1493
Akash Goel3b3f1652016-10-13 22:44:48 +05301494 engine = dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001495 } else {
Akash Goel3b3f1652016-10-13 22:44:48 +05301496 engine = dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001497 }
1498
Akash Goel3b3f1652016-10-13 22:44:48 +05301499 if (!engine) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001500 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001501 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001502 }
1503
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001504 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001505}
1506
Eric Anholtae662d32012-01-03 09:23:29 -08001507static int
Chris Wilson650bc632017-06-15 09:14:33 +01001508i915_gem_do_execbuffer(struct drm_device *dev,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001509 struct drm_file *file,
1510 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001511 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001512{
Chris Wilson650bc632017-06-15 09:14:33 +01001513 struct i915_execbuffer eb;
Chris Wilsonfec04452017-01-27 09:40:08 +00001514 struct dma_fence *in_fence = NULL;
1515 struct sync_file *out_fence = NULL;
1516 int out_fence_fd = -1;
Oscar Mateo78382592014-07-03 16:28:05 +01001517 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001518
Daniel Vettered5982e2013-01-17 22:23:36 +01001519 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001520 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001521
Chris Wilsonad19f102014-08-10 06:29:08 +01001522 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001523 if (ret)
1524 return ret;
1525
Chris Wilson650bc632017-06-15 09:14:33 +01001526 eb.i915 = to_i915(dev);
1527 eb.file = file;
1528 eb.args = args;
1529 eb.exec = exec;
1530 eb.need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
1531 reloc_cache_init(&eb.reloc_cache, eb.i915);
1532
1533 eb.batch_start_offset = args->batch_start_offset;
1534 eb.batch_len = args->batch_len;
1535
1536 eb.dispatch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001537 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02001538 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001539 return -EPERM;
1540
Chris Wilson650bc632017-06-15 09:14:33 +01001541 eb.dispatch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001542 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001543 if (args->flags & I915_EXEC_IS_PINNED)
Chris Wilson650bc632017-06-15 09:14:33 +01001544 eb.dispatch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001545
Chris Wilson650bc632017-06-15 09:14:33 +01001546 eb.engine = eb_select_engine(eb.i915, file, args);
1547 if (!eb.engine)
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001548 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001549
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001550 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
Chris Wilson650bc632017-06-15 09:14:33 +01001551 if (!HAS_RESOURCE_STREAMER(eb.i915)) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001552 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
1553 return -EINVAL;
1554 }
Chris Wilson650bc632017-06-15 09:14:33 +01001555 if (eb.engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001556 DRM_DEBUG("RS is not available on %s\n",
Chris Wilson650bc632017-06-15 09:14:33 +01001557 eb.engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001558 return -EINVAL;
1559 }
1560
Chris Wilson650bc632017-06-15 09:14:33 +01001561 eb.dispatch_flags |= I915_DISPATCH_RS;
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001562 }
1563
Chris Wilsonfec04452017-01-27 09:40:08 +00001564 if (args->flags & I915_EXEC_FENCE_IN) {
1565 in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08001566 if (!in_fence)
1567 return -EINVAL;
Chris Wilsonfec04452017-01-27 09:40:08 +00001568 }
1569
1570 if (args->flags & I915_EXEC_FENCE_OUT) {
1571 out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
1572 if (out_fence_fd < 0) {
1573 ret = out_fence_fd;
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08001574 goto err_in_fence;
Chris Wilsonfec04452017-01-27 09:40:08 +00001575 }
1576 }
1577
Chris Wilson67d97da2016-07-04 08:08:31 +01001578 /* Take a local wakeref for preparing to dispatch the execbuf as
1579 * we expect to access the hardware fairly frequently in the
1580 * process. Upon first dispatch, we acquire another prolonged
1581 * wakeref that we hold until the GPU has been idle for at least
1582 * 100ms.
1583 */
Chris Wilson650bc632017-06-15 09:14:33 +01001584 intel_runtime_pm_get(eb.i915);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001585
Chris Wilson54cf91d2010-11-25 18:00:26 +00001586 ret = i915_mutex_lock_interruptible(dev);
1587 if (ret)
1588 goto pre_mutex_err;
1589
Chris Wilson650bc632017-06-15 09:14:33 +01001590 ret = eb_select_context(&eb);
1591 if (ret) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001592 mutex_unlock(&dev->struct_mutex);
1593 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001594 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001595
Chris Wilson650bc632017-06-15 09:14:33 +01001596 if (eb_create(&eb)) {
1597 i915_gem_context_put(eb.ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001598 mutex_unlock(&dev->struct_mutex);
1599 ret = -ENOMEM;
1600 goto pre_mutex_err;
1601 }
1602
Chris Wilson54cf91d2010-11-25 18:00:26 +00001603 /* Look up object handles */
Chris Wilson650bc632017-06-15 09:14:33 +01001604 ret = eb_lookup_vmas(&eb);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001605 if (ret)
1606 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001607
Chris Wilson6fe4f142011-01-10 17:35:37 +00001608 /* take note of the batch buffer before we might reorder the lists */
Chris Wilson650bc632017-06-15 09:14:33 +01001609 eb.batch = eb_get_batch(&eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001610
Chris Wilson54cf91d2010-11-25 18:00:26 +00001611 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilson650bc632017-06-15 09:14:33 +01001612 ret = eb_reserve(&eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001613 if (ret)
1614 goto err;
1615
1616 /* The objects are in their final locations, apply the relocations. */
Chris Wilson650bc632017-06-15 09:14:33 +01001617 if (eb.need_relocs)
1618 ret = eb_relocate(&eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001619 if (ret) {
1620 if (ret == -EFAULT) {
Chris Wilson650bc632017-06-15 09:14:33 +01001621 ret = eb_relocate_slow(&eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001622 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1623 }
1624 if (ret)
1625 goto err;
1626 }
1627
1628 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson650bc632017-06-15 09:14:33 +01001629 if (eb.batch->obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001630 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001631 ret = -EINVAL;
1632 goto err;
1633 }
Chris Wilson650bc632017-06-15 09:14:33 +01001634 if (eb.batch_start_offset > eb.batch->size ||
1635 eb.batch_len > eb.batch->size - eb.batch_start_offset) {
Chris Wilson0b537272016-08-18 17:17:12 +01001636 DRM_DEBUG("Attempting to use out-of-bounds batch\n");
1637 ret = -EINVAL;
1638 goto err;
1639 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001640
Chris Wilson650bc632017-06-15 09:14:33 +01001641 if (eb.engine->needs_cmd_parser && eb.batch_len) {
Chris Wilson59bfa122016-08-04 16:32:31 +01001642 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001643
Chris Wilson650bc632017-06-15 09:14:33 +01001644 vma = eb_parse(&eb, drm_is_current_master(file));
Chris Wilson59bfa122016-08-04 16:32:31 +01001645 if (IS_ERR(vma)) {
1646 ret = PTR_ERR(vma);
Brad Volkin78a42372014-12-11 12:13:09 -08001647 goto err;
1648 }
Chris Wilson17cabf52015-01-14 11:20:57 +00001649
Chris Wilson59bfa122016-08-04 16:32:31 +01001650 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001651 /*
1652 * Batch parsed and accepted:
1653 *
1654 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1655 * bit from MI_BATCH_BUFFER_START commands issued in
1656 * the dispatch_execbuffer implementations. We
1657 * specifically don't want that set on batches the
1658 * command parser has accepted.
1659 */
Chris Wilson650bc632017-06-15 09:14:33 +01001660 eb.dispatch_flags |= I915_DISPATCH_SECURE;
1661 eb.batch_start_offset = 0;
1662 eb.batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001663 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001664 }
1665
Chris Wilson650bc632017-06-15 09:14:33 +01001666 eb.batch->obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1667 if (eb.batch_len == 0)
1668 eb.batch_len = eb.batch->size - eb.batch_start_offset;
Brad Volkin78a42372014-12-11 12:13:09 -08001669
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001670 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1671 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001672 * hsw should have this fixed, but bdw mucks it up again. */
Chris Wilson650bc632017-06-15 09:14:33 +01001673 if (eb.dispatch_flags & I915_DISPATCH_SECURE) {
1674 struct drm_i915_gem_object *obj = eb.batch->obj;
Chris Wilson058d88c2016-08-15 10:49:06 +01001675 struct i915_vma *vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01001676
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001677 /*
1678 * So on first glance it looks freaky that we pin the batch here
1679 * outside of the reservation loop. But:
1680 * - The batch is already pinned into the relevant ppgtt, so we
1681 * already have the backing storage fully allocated.
1682 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01001683 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001684 * fitting due to fragmentation.
1685 * So this is actually safe.
1686 */
Chris Wilson058d88c2016-08-15 10:49:06 +01001687 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
1688 if (IS_ERR(vma)) {
1689 ret = PTR_ERR(vma);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001690 goto err;
Chris Wilson058d88c2016-08-15 10:49:06 +01001691 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001692
Chris Wilson650bc632017-06-15 09:14:33 +01001693 eb.batch = vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01001694 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001695
John Harrison0c8dac82015-05-29 17:43:25 +01001696 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson650bc632017-06-15 09:14:33 +01001697 eb.request = i915_gem_request_alloc(eb.engine, eb.ctx);
1698 if (IS_ERR(eb.request)) {
1699 ret = PTR_ERR(eb.request);
John Harrison0c8dac82015-05-29 17:43:25 +01001700 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00001701 }
John Harrison0c8dac82015-05-29 17:43:25 +01001702
Chris Wilsonfec04452017-01-27 09:40:08 +00001703 if (in_fence) {
Chris Wilson650bc632017-06-15 09:14:33 +01001704 ret = i915_gem_request_await_dma_fence(eb.request, in_fence);
Chris Wilsonfec04452017-01-27 09:40:08 +00001705 if (ret < 0)
1706 goto err_request;
1707 }
1708
1709 if (out_fence_fd != -1) {
Chris Wilson650bc632017-06-15 09:14:33 +01001710 out_fence = sync_file_create(&eb.request->fence);
Chris Wilsonfec04452017-01-27 09:40:08 +00001711 if (!out_fence) {
1712 ret = -ENOMEM;
1713 goto err_request;
1714 }
1715 }
1716
Chris Wilson17f298cf2016-08-10 13:41:46 +01001717 /* Whilst this request exists, batch_obj will be on the
1718 * active_list, and so will hold the active reference. Only when this
1719 * request is retired will the the batch_obj be moved onto the
1720 * inactive_list and lose its active reference. Hence we do not need
1721 * to explicitly hold another reference here.
1722 */
Chris Wilson650bc632017-06-15 09:14:33 +01001723 eb.request->batch = eb.batch;
Chris Wilson17f298cf2016-08-10 13:41:46 +01001724
Chris Wilson650bc632017-06-15 09:14:33 +01001725 trace_i915_gem_request_queue(eb.request, eb.dispatch_flags);
1726 ret = execbuf_submit(&eb);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001727err_request:
Chris Wilson650bc632017-06-15 09:14:33 +01001728 __i915_add_request(eb.request, ret == 0);
1729 add_to_client(eb.request, file);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00001730
Chris Wilsonfec04452017-01-27 09:40:08 +00001731 if (out_fence) {
1732 if (ret == 0) {
1733 fd_install(out_fence_fd, out_fence->file);
1734 args->rsvd2 &= GENMASK_ULL(0, 31); /* keep in-fence */
1735 args->rsvd2 |= (u64)out_fence_fd << 32;
1736 out_fence_fd = -1;
1737 } else {
1738 fput(out_fence->file);
1739 }
1740 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001741
John Harrison0c8dac82015-05-29 17:43:25 +01001742err_batch_unpin:
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001743 /*
1744 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1745 * batch vma for correctness. For less ugly and less fragility this
1746 * needs to be adjusted to also track the ggtt batch vma properly as
1747 * active.
1748 */
Chris Wilson650bc632017-06-15 09:14:33 +01001749 if (eb.dispatch_flags & I915_DISPATCH_SECURE)
1750 i915_vma_unpin(eb.batch);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001751err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001752 /* the request owns the ref now */
Chris Wilson650bc632017-06-15 09:14:33 +01001753 eb_destroy(&eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001754 mutex_unlock(&dev->struct_mutex);
1755
1756pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001757 /* intel_gpu_busy should also get a ref, so it will free when the device
1758 * is really idle. */
Chris Wilson650bc632017-06-15 09:14:33 +01001759 intel_runtime_pm_put(eb.i915);
Chris Wilsonfec04452017-01-27 09:40:08 +00001760 if (out_fence_fd != -1)
1761 put_unused_fd(out_fence_fd);
Daniele Ceraolo Spurio4a04e372017-02-03 14:45:29 -08001762err_in_fence:
Chris Wilsonfec04452017-01-27 09:40:08 +00001763 dma_fence_put(in_fence);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001764 return ret;
1765}
1766
1767/*
1768 * Legacy execbuffer just creates an exec2 list from the original exec object
1769 * list array and passes it to the real function.
1770 */
1771int
1772i915_gem_execbuffer(struct drm_device *dev, void *data,
1773 struct drm_file *file)
1774{
1775 struct drm_i915_gem_execbuffer *args = data;
1776 struct drm_i915_gem_execbuffer2 exec2;
1777 struct drm_i915_gem_exec_object *exec_list = NULL;
1778 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1779 int ret, i;
1780
Chris Wilson54cf91d2010-11-25 18:00:26 +00001781 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001782 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001783 return -EINVAL;
1784 }
1785
1786 /* Copy in the exec list from userland */
Michal Hocko20981052017-05-17 14:23:12 +02001787 exec_list = kvmalloc_array(sizeof(*exec_list), args->buffer_count, GFP_KERNEL);
1788 exec2_list = kvmalloc_array(sizeof(*exec2_list), args->buffer_count, GFP_KERNEL);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001789 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001790 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001791 args->buffer_count);
Michal Hocko20981052017-05-17 14:23:12 +02001792 kvfree(exec_list);
1793 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001794 return -ENOMEM;
1795 }
1796 ret = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001797 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001798 sizeof(*exec_list) * args->buffer_count);
1799 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001800 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001801 args->buffer_count, ret);
Michal Hocko20981052017-05-17 14:23:12 +02001802 kvfree(exec_list);
1803 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001804 return -EFAULT;
1805 }
1806
1807 for (i = 0; i < args->buffer_count; i++) {
1808 exec2_list[i].handle = exec_list[i].handle;
1809 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1810 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1811 exec2_list[i].alignment = exec_list[i].alignment;
1812 exec2_list[i].offset = exec_list[i].offset;
Tvrtko Ursulinf0836b72016-11-16 08:55:32 +00001813 if (INTEL_GEN(to_i915(dev)) < 4)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001814 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1815 else
1816 exec2_list[i].flags = 0;
1817 }
1818
1819 exec2.buffers_ptr = args->buffers_ptr;
1820 exec2.buffer_count = args->buffer_count;
1821 exec2.batch_start_offset = args->batch_start_offset;
1822 exec2.batch_len = args->batch_len;
1823 exec2.DR1 = args->DR1;
1824 exec2.DR4 = args->DR4;
1825 exec2.num_cliprects = args->num_cliprects;
1826 exec2.cliprects_ptr = args->cliprects_ptr;
1827 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001828 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001829
Chris Wilson650bc632017-06-15 09:14:33 +01001830 ret = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001831 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001832 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001833 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001834
Chris Wilson54cf91d2010-11-25 18:00:26 +00001835 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001836 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001837 exec2_list[i].offset =
1838 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001839 ret = __copy_to_user(&user_exec_list[i].offset,
1840 &exec2_list[i].offset,
1841 sizeof(user_exec_list[i].offset));
1842 if (ret) {
1843 ret = -EFAULT;
1844 DRM_DEBUG("failed to copy %d exec entries "
1845 "back to user (%d)\n",
1846 args->buffer_count, ret);
1847 break;
1848 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001849 }
1850 }
1851
Michal Hocko20981052017-05-17 14:23:12 +02001852 kvfree(exec_list);
1853 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001854 return ret;
1855}
1856
1857int
1858i915_gem_execbuffer2(struct drm_device *dev, void *data,
1859 struct drm_file *file)
1860{
1861 struct drm_i915_gem_execbuffer2 *args = data;
1862 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1863 int ret;
1864
Xi Wanged8cd3b2012-04-23 04:06:41 -04001865 if (args->buffer_count < 1 ||
1866 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001867 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001868 return -EINVAL;
1869 }
1870
Michal Hocko20981052017-05-17 14:23:12 +02001871 exec2_list = kvmalloc_array(args->buffer_count,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01001872 sizeof(*exec2_list),
1873 GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001874 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001875 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001876 args->buffer_count);
1877 return -ENOMEM;
1878 }
1879 ret = copy_from_user(exec2_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001880 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001881 sizeof(*exec2_list) * args->buffer_count);
1882 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001883 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001884 args->buffer_count, ret);
Michal Hocko20981052017-05-17 14:23:12 +02001885 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001886 return -EFAULT;
1887 }
1888
Chris Wilson650bc632017-06-15 09:14:33 +01001889 ret = i915_gem_do_execbuffer(dev, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001890 if (!ret) {
1891 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001892 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001893 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001894 int i;
1895
1896 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001897 exec2_list[i].offset =
1898 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001899 ret = __copy_to_user(&user_exec_list[i].offset,
1900 &exec2_list[i].offset,
1901 sizeof(user_exec_list[i].offset));
1902 if (ret) {
1903 ret = -EFAULT;
1904 DRM_DEBUG("failed to copy %d exec entries "
1905 "back to user\n",
1906 args->buffer_count);
1907 break;
1908 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001909 }
1910 }
1911
Michal Hocko20981052017-05-17 14:23:12 +02001912 kvfree(exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001913 return ret;
1914}