blob: c8d13fea4b250a3e84194a873e8491f52a30c16e [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
Chris Wilsonad778f82016-08-04 16:32:42 +010029#include <linux/dma_remapping.h>
30#include <linux/reservation.h>
31#include <linux/uaccess.h>
32
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/drmP.h>
34#include <drm/i915_drm.h>
Chris Wilsonad778f82016-08-04 16:32:42 +010035
Chris Wilson54cf91d2010-11-25 18:00:26 +000036#include "i915_drv.h"
Chris Wilsonad778f82016-08-04 16:32:42 +010037#include "i915_gem_dmabuf.h"
Chris Wilson54cf91d2010-11-25 18:00:26 +000038#include "i915_trace.h"
39#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010040#include "intel_frontbuffer.h"
Chris Wilson54cf91d2010-11-25 18:00:26 +000041
Dave Gordon9e2793f62016-07-14 14:52:03 +010042#define __EXEC_OBJECT_HAS_PIN (1<<31)
43#define __EXEC_OBJECT_HAS_FENCE (1<<30)
44#define __EXEC_OBJECT_NEEDS_MAP (1<<29)
45#define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
46#define __EXEC_OBJECT_INTERNAL_FLAGS (0xf<<28) /* all of the above */
Chris Wilsond23db882014-05-23 08:48:08 +020047
48#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000049
Chris Wilson5b043f42016-08-02 22:50:38 +010050struct i915_execbuffer_params {
51 struct drm_device *dev;
52 struct drm_file *file;
Chris Wilson59bfa122016-08-04 16:32:31 +010053 struct i915_vma *batch;
54 u32 dispatch_flags;
55 u32 args_batch_start_offset;
Chris Wilson5b043f42016-08-02 22:50:38 +010056 struct intel_engine_cs *engine;
Chris Wilson5b043f42016-08-02 22:50:38 +010057 struct i915_gem_context *ctx;
58 struct drm_i915_gem_request *request;
59};
60
Ben Widawsky27173f12013-08-14 11:38:36 +020061struct eb_vmas {
62 struct list_head vmas;
Chris Wilson67731b82010-12-08 10:38:14 +000063 int and;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000064 union {
Ben Widawsky27173f12013-08-14 11:38:36 +020065 struct i915_vma *lut[0];
Chris Wilsoneef90cc2013-01-08 10:53:17 +000066 struct hlist_head buckets[0];
67 };
Chris Wilson67731b82010-12-08 10:38:14 +000068};
69
Ben Widawsky27173f12013-08-14 11:38:36 +020070static struct eb_vmas *
Ben Widawsky17601cbc2013-11-25 09:54:38 -080071eb_create(struct drm_i915_gem_execbuffer2 *args)
Chris Wilson67731b82010-12-08 10:38:14 +000072{
Ben Widawsky27173f12013-08-14 11:38:36 +020073 struct eb_vmas *eb = NULL;
Chris Wilson67731b82010-12-08 10:38:14 +000074
Chris Wilsoneef90cc2013-01-08 10:53:17 +000075 if (args->flags & I915_EXEC_HANDLE_LUT) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020076 unsigned size = args->buffer_count;
Ben Widawsky27173f12013-08-14 11:38:36 +020077 size *= sizeof(struct i915_vma *);
78 size += sizeof(struct eb_vmas);
Chris Wilsoneef90cc2013-01-08 10:53:17 +000079 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
80 }
81
82 if (eb == NULL) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020083 unsigned size = args->buffer_count;
84 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Lauri Kasanen27b7c632013-03-27 15:04:55 +020085 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
Chris Wilsoneef90cc2013-01-08 10:53:17 +000086 while (count > 2*size)
87 count >>= 1;
88 eb = kzalloc(count*sizeof(struct hlist_head) +
Ben Widawsky27173f12013-08-14 11:38:36 +020089 sizeof(struct eb_vmas),
Chris Wilsoneef90cc2013-01-08 10:53:17 +000090 GFP_TEMPORARY);
91 if (eb == NULL)
92 return eb;
93
94 eb->and = count - 1;
95 } else
96 eb->and = -args->buffer_count;
97
Ben Widawsky27173f12013-08-14 11:38:36 +020098 INIT_LIST_HEAD(&eb->vmas);
Chris Wilson67731b82010-12-08 10:38:14 +000099 return eb;
100}
101
102static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200103eb_reset(struct eb_vmas *eb)
Chris Wilson67731b82010-12-08 10:38:14 +0000104{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000105 if (eb->and >= 0)
106 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
Chris Wilson67731b82010-12-08 10:38:14 +0000107}
108
Chris Wilson59bfa122016-08-04 16:32:31 +0100109static struct i915_vma *
110eb_get_batch(struct eb_vmas *eb)
111{
112 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
113
114 /*
115 * SNA is doing fancy tricks with compressing batch buffers, which leads
116 * to negative relocation deltas. Usually that works out ok since the
117 * relocate address is still positive, except when the batch is placed
118 * very low in the GTT. Ensure this doesn't happen.
119 *
120 * Note that actual hangs have only been observed on gen7, but for
121 * paranoia do it everywhere.
122 */
123 if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
124 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
125
126 return vma;
127}
128
Chris Wilson3b96eff2013-01-08 10:53:14 +0000129static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200130eb_lookup_vmas(struct eb_vmas *eb,
131 struct drm_i915_gem_exec_object2 *exec,
132 const struct drm_i915_gem_execbuffer2 *args,
133 struct i915_address_space *vm,
134 struct drm_file *file)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000135{
Ben Widawsky27173f12013-08-14 11:38:36 +0200136 struct drm_i915_gem_object *obj;
137 struct list_head objects;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000138 int i, ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000139
Ben Widawsky27173f12013-08-14 11:38:36 +0200140 INIT_LIST_HEAD(&objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000141 spin_lock(&file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +0200142 /* Grab a reference to the object and release the lock so we can lookup
143 * or create the VMA without using GFP_ATOMIC */
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000144 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000145 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
146 if (obj == NULL) {
147 spin_unlock(&file->table_lock);
148 DRM_DEBUG("Invalid object handle %d at index %d\n",
149 exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200150 ret = -ENOENT;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000151 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000152 }
153
Ben Widawsky27173f12013-08-14 11:38:36 +0200154 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000155 spin_unlock(&file->table_lock);
156 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
157 obj, exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200158 ret = -EINVAL;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000159 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000160 }
161
Chris Wilson25dc5562016-07-20 13:31:52 +0100162 i915_gem_object_get(obj);
Ben Widawsky27173f12013-08-14 11:38:36 +0200163 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000164 }
165 spin_unlock(&file->table_lock);
166
Ben Widawsky27173f12013-08-14 11:38:36 +0200167 i = 0;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000168 while (!list_empty(&objects)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200169 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800170
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000171 obj = list_first_entry(&objects,
172 struct drm_i915_gem_object,
173 obj_exec_link);
174
Daniel Vettere656a6c2013-08-14 14:14:04 +0200175 /*
176 * NOTE: We can leak any vmas created here when something fails
177 * later on. But that's no issue since vma_unbind can deal with
178 * vmas which are not actually bound. And since only
179 * lookup_or_create exists as an interface to get at the vma
180 * from the (obj, vm) we don't run the risk of creating
181 * duplicated vmas for the same vm.
182 */
Daniel Vetterda51a1e2014-08-11 12:08:58 +0200183 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200184 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200185 DRM_DEBUG("Failed to lookup VMA\n");
186 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000187 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200188 }
189
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000190 /* Transfer ownership from the objects list to the vmas list. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200191 list_add_tail(&vma->exec_list, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000192 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200193
194 vma->exec_entry = &exec[i];
195 if (eb->and < 0) {
196 eb->lut[i] = vma;
197 } else {
198 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
199 vma->exec_handle = handle;
200 hlist_add_head(&vma->exec_node,
201 &eb->buckets[handle & eb->and]);
202 }
203 ++i;
204 }
205
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000206 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200207
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000208
209err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200210 while (!list_empty(&objects)) {
211 obj = list_first_entry(&objects,
212 struct drm_i915_gem_object,
213 obj_exec_link);
214 list_del_init(&obj->obj_exec_link);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100215 i915_gem_object_put(obj);
Ben Widawsky27173f12013-08-14 11:38:36 +0200216 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000217 /*
218 * Objects already transfered to the vmas list will be unreferenced by
219 * eb_destroy.
220 */
221
Ben Widawsky27173f12013-08-14 11:38:36 +0200222 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000223}
224
Ben Widawsky27173f12013-08-14 11:38:36 +0200225static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000226{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000227 if (eb->and < 0) {
228 if (handle >= -eb->and)
229 return NULL;
230 return eb->lut[handle];
231 } else {
232 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800233 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000234
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000235 head = &eb->buckets[handle & eb->and];
Geliang Tangaa459502016-01-18 23:54:20 +0800236 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200237 if (vma->exec_handle == handle)
238 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000239 }
240 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000241 }
Chris Wilson67731b82010-12-08 10:38:14 +0000242}
243
Chris Wilsona415d352013-11-26 11:23:15 +0000244static void
245i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
246{
247 struct drm_i915_gem_exec_object2 *entry;
248 struct drm_i915_gem_object *obj = vma->obj;
249
250 if (!drm_mm_node_allocated(&vma->node))
251 return;
252
253 entry = vma->exec_entry;
254
255 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
256 i915_gem_object_unpin_fence(obj);
257
258 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100259 __i915_vma_unpin(vma);
Chris Wilsona415d352013-11-26 11:23:15 +0000260
Chris Wilsonde4e7832015-04-07 16:20:35 +0100261 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilsona415d352013-11-26 11:23:15 +0000262}
263
264static void eb_destroy(struct eb_vmas *eb)
265{
Ben Widawsky27173f12013-08-14 11:38:36 +0200266 while (!list_empty(&eb->vmas)) {
267 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000268
Ben Widawsky27173f12013-08-14 11:38:36 +0200269 vma = list_first_entry(&eb->vmas,
270 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000271 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200272 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000273 i915_gem_execbuffer_unreserve_vma(vma);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100274 i915_gem_object_put(vma->obj);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000275 }
Chris Wilson67731b82010-12-08 10:38:14 +0000276 kfree(eb);
277}
278
Chris Wilsondabdfe02012-03-26 10:10:27 +0200279static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
280{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300281 return (HAS_LLC(obj->base.dev) ||
282 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200283 obj->cache_level != I915_CACHE_NONE);
284}
285
Michał Winiarski934acce2015-12-29 18:24:52 +0100286/* Used to convert any address to canonical form.
287 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
288 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
289 * addresses to be in a canonical form:
290 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
291 * canonical form [63:48] == [47]."
292 */
293#define GEN8_HIGH_ADDRESS_BIT 47
294static inline uint64_t gen8_canonical_addr(uint64_t address)
295{
296 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
297}
298
299static inline uint64_t gen8_noncanonical_addr(uint64_t address)
300{
301 return address & ((1ULL << (GEN8_HIGH_ADDRESS_BIT + 1)) - 1);
302}
303
304static inline uint64_t
305relocation_target(struct drm_i915_gem_relocation_entry *reloc,
306 uint64_t target_offset)
307{
308 return gen8_canonical_addr((int)reloc->delta + target_offset);
309}
310
Chris Wilson54cf91d2010-11-25 18:00:26 +0000311static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100312relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700313 struct drm_i915_gem_relocation_entry *reloc,
314 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100315{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700316 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100317 uint32_t page_offset = offset_in_page(reloc->offset);
Michał Winiarski934acce2015-12-29 18:24:52 +0100318 uint64_t delta = relocation_target(reloc, target_offset);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100319 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800320 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100321
Chris Wilson2cc86b82013-08-26 19:51:00 -0300322 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100323 if (ret)
324 return ret;
325
Dave Gordon033908a2015-12-10 18:51:23 +0000326 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Rafael Barbalho5032d872013-08-21 17:10:51 +0100327 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700328 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700329
330 if (INTEL_INFO(dev)->gen >= 8) {
331 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
332
333 if (page_offset == 0) {
334 kunmap_atomic(vaddr);
Dave Gordon033908a2015-12-10 18:51:23 +0000335 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700336 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
337 }
338
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700339 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700340 }
341
Rafael Barbalho5032d872013-08-21 17:10:51 +0100342 kunmap_atomic(vaddr);
343
344 return 0;
345}
346
347static int
348relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700349 struct drm_i915_gem_relocation_entry *reloc,
350 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100351{
352 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300353 struct drm_i915_private *dev_priv = to_i915(dev);
354 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michał Winiarski934acce2015-12-29 18:24:52 +0100355 uint64_t delta = relocation_target(reloc, target_offset);
Chris Wilson906843c2014-08-10 06:29:11 +0100356 uint64_t offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100357 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800358 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100359
360 ret = i915_gem_object_set_to_gtt_domain(obj, true);
361 if (ret)
362 return ret;
363
364 ret = i915_gem_object_put_fence(obj);
365 if (ret)
366 return ret;
367
368 /* Map the page containing the relocation we're going to perform. */
Chris Wilson906843c2014-08-10 06:29:11 +0100369 offset = i915_gem_obj_ggtt_offset(obj);
370 offset += reloc->offset;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300371 reloc_page = io_mapping_map_atomic_wc(ggtt->mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100372 offset & PAGE_MASK);
373 iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700374
375 if (INTEL_INFO(dev)->gen >= 8) {
Chris Wilson906843c2014-08-10 06:29:11 +0100376 offset += sizeof(uint32_t);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700377
Chris Wilson906843c2014-08-10 06:29:11 +0100378 if (offset_in_page(offset) == 0) {
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700379 io_mapping_unmap_atomic(reloc_page);
Chris Wilson906843c2014-08-10 06:29:11 +0100380 reloc_page =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300381 io_mapping_map_atomic_wc(ggtt->mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100382 offset);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700383 }
384
Chris Wilson906843c2014-08-10 06:29:11 +0100385 iowrite32(upper_32_bits(delta),
386 reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700387 }
388
Rafael Barbalho5032d872013-08-21 17:10:51 +0100389 io_mapping_unmap_atomic(reloc_page);
390
391 return 0;
392}
393
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000394static void
395clflush_write32(void *addr, uint32_t value)
396{
397 /* This is not a fast path, so KISS. */
398 drm_clflush_virt_range(addr, sizeof(uint32_t));
399 *(uint32_t *)addr = value;
400 drm_clflush_virt_range(addr, sizeof(uint32_t));
401}
402
403static int
404relocate_entry_clflush(struct drm_i915_gem_object *obj,
405 struct drm_i915_gem_relocation_entry *reloc,
406 uint64_t target_offset)
407{
408 struct drm_device *dev = obj->base.dev;
409 uint32_t page_offset = offset_in_page(reloc->offset);
Michał Winiarski934acce2015-12-29 18:24:52 +0100410 uint64_t delta = relocation_target(reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000411 char *vaddr;
412 int ret;
413
414 ret = i915_gem_object_set_to_gtt_domain(obj, true);
415 if (ret)
416 return ret;
417
Dave Gordon033908a2015-12-10 18:51:23 +0000418 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000419 reloc->offset >> PAGE_SHIFT));
420 clflush_write32(vaddr + page_offset, lower_32_bits(delta));
421
422 if (INTEL_INFO(dev)->gen >= 8) {
423 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
424
425 if (page_offset == 0) {
426 kunmap_atomic(vaddr);
Dave Gordon033908a2015-12-10 18:51:23 +0000427 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000428 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
429 }
430
431 clflush_write32(vaddr + page_offset, upper_32_bits(delta));
432 }
433
434 kunmap_atomic(vaddr);
435
436 return 0;
437}
438
Chris Wilson909d0742016-08-04 07:52:41 +0100439static bool object_is_idle(struct drm_i915_gem_object *obj)
440{
Chris Wilson573adb32016-08-04 16:32:39 +0100441 unsigned long active = i915_gem_object_get_active(obj);
Chris Wilson909d0742016-08-04 07:52:41 +0100442 int idx;
443
444 for_each_active(active, idx) {
445 if (!i915_gem_active_is_idle(&obj->last_read[idx],
446 &obj->base.dev->struct_mutex))
447 return false;
448 }
449
450 return true;
451}
452
Rafael Barbalho5032d872013-08-21 17:10:51 +0100453static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000454i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200455 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800456 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000457{
458 struct drm_device *dev = obj->base.dev;
459 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100460 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200461 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700462 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800463 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000464
Chris Wilson67731b82010-12-08 10:38:14 +0000465 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200466 target_vma = eb_get_vma(eb, reloc->target_handle);
467 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000468 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200469 target_i915_obj = target_vma->obj;
470 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000471
Michał Winiarski934acce2015-12-29 18:24:52 +0100472 target_offset = gen8_canonical_addr(target_vma->node.start);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000473
Eric Anholte844b992012-07-31 15:35:01 -0700474 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
475 * pipe_control writes because the gpu doesn't properly redirect them
476 * through the ppgtt for non_secure batchbuffers. */
477 if (unlikely(IS_GEN6(dev) &&
Daniel Vetter08755462015-04-20 09:04:05 -0700478 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000479 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
Daniel Vetter08755462015-04-20 09:04:05 -0700480 PIN_GLOBAL);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000481 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
482 return ret;
483 }
Eric Anholte844b992012-07-31 15:35:01 -0700484
Chris Wilson54cf91d2010-11-25 18:00:26 +0000485 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000486 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100487 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000488 "obj %p target %d offset %d "
489 "read %08x write %08x",
490 obj, reloc->target_handle,
491 (int) reloc->offset,
492 reloc->read_domains,
493 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800494 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000495 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100496 if (unlikely((reloc->write_domain | reloc->read_domains)
497 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100498 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000499 "obj %p target %d offset %d "
500 "read %08x write %08x",
501 obj, reloc->target_handle,
502 (int) reloc->offset,
503 reloc->read_domains,
504 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800505 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000506 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000507
508 target_obj->pending_read_domains |= reloc->read_domains;
509 target_obj->pending_write_domain |= reloc->write_domain;
510
511 /* If the relocation already has the right value in it, no
512 * more work needs to be done.
513 */
514 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000515 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000516
517 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700518 if (unlikely(reloc->offset >
519 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100520 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000521 "obj %p target %d offset %d size %d.\n",
522 obj, reloc->target_handle,
523 (int) reloc->offset,
524 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800525 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000526 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000527 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100528 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000529 "obj %p target %d offset %d.\n",
530 obj, reloc->target_handle,
531 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800532 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000533 }
534
Chris Wilsondabdfe02012-03-26 10:10:27 +0200535 /* We can't wait for rendering with pagefaults disabled */
Chris Wilson909d0742016-08-04 07:52:41 +0100536 if (pagefault_disabled() && !object_is_idle(obj))
Chris Wilsondabdfe02012-03-26 10:10:27 +0200537 return -EFAULT;
538
Rafael Barbalho5032d872013-08-21 17:10:51 +0100539 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700540 ret = relocate_entry_cpu(obj, reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000541 else if (obj->map_and_fenceable)
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700542 ret = relocate_entry_gtt(obj, reloc, target_offset);
Borislav Petkov906bf7f2016-03-29 17:41:59 +0200543 else if (static_cpu_has(X86_FEATURE_CLFLUSH))
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000544 ret = relocate_entry_clflush(obj, reloc, target_offset);
545 else {
546 WARN_ONCE(1, "Impossible case in relocation handling\n");
547 ret = -ENODEV;
548 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000549
Daniel Vetterd4d36012013-09-02 20:56:23 +0200550 if (ret)
551 return ret;
552
Chris Wilson54cf91d2010-11-25 18:00:26 +0000553 /* and update the user's relocation entry */
554 reloc->presumed_offset = target_offset;
555
Chris Wilson67731b82010-12-08 10:38:14 +0000556 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000557}
558
559static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200560i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
561 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000562{
Chris Wilson1d83f442012-03-24 20:12:53 +0000563#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
564 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000565 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200566 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000567 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000568
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300569 user_relocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000570
Chris Wilson1d83f442012-03-24 20:12:53 +0000571 remain = entry->relocation_count;
572 while (remain) {
573 struct drm_i915_gem_relocation_entry *r = stack_reloc;
574 int count = remain;
575 if (count > ARRAY_SIZE(stack_reloc))
576 count = ARRAY_SIZE(stack_reloc);
577 remain -= count;
578
579 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000580 return -EFAULT;
581
Chris Wilson1d83f442012-03-24 20:12:53 +0000582 do {
583 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000584
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800585 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000586 if (ret)
587 return ret;
588
589 if (r->presumed_offset != offset &&
Linus Torvalds5b09c3e2016-05-22 14:19:37 -0700590 __put_user(r->presumed_offset, &user_relocs->presumed_offset)) {
Chris Wilson1d83f442012-03-24 20:12:53 +0000591 return -EFAULT;
592 }
593
594 user_relocs++;
595 r++;
596 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000597 }
598
599 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000600#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000601}
602
603static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200604i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
605 struct eb_vmas *eb,
606 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000607{
Ben Widawsky27173f12013-08-14 11:38:36 +0200608 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000609 int i, ret;
610
611 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800612 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000613 if (ret)
614 return ret;
615 }
616
617 return 0;
618}
619
620static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800621i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000622{
Ben Widawsky27173f12013-08-14 11:38:36 +0200623 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000624 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000625
Chris Wilsond4aeee72011-03-14 15:11:24 +0000626 /* This is the fast path and we cannot handle a pagefault whilst
627 * holding the struct mutex lest the user pass in the relocations
628 * contained within a mmaped bo. For in such a case we, the page
629 * fault handler would call i915_gem_fault() and we would try to
630 * acquire the struct mutex again. Obviously this is bad and so
631 * lockdep complains vehemently.
632 */
633 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200634 list_for_each_entry(vma, &eb->vmas, exec_list) {
635 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000636 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000637 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000638 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000639 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000640
Chris Wilsond4aeee72011-03-14 15:11:24 +0000641 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000642}
643
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000644static bool only_mappable_for_reloc(unsigned int flags)
645{
646 return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
647 __EXEC_OBJECT_NEEDS_MAP;
648}
649
Chris Wilson1690e1e2011-12-14 13:57:08 +0100650static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200651i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000652 struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200653 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100654{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800655 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200656 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200657 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100658 int ret;
659
Daniel Vetter08755462015-04-20 09:04:05 -0700660 flags = PIN_USER;
Daniel Vetter0229da32015-04-14 19:01:54 +0200661 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
662 flags |= PIN_GLOBAL;
663
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000664 if (!drm_mm_node_allocated(&vma->node)) {
Michel Thierry101b5062015-10-01 13:33:57 +0100665 /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
666 * limit address to the first 4GBs for unflagged objects.
667 */
668 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
669 flags |= PIN_ZONE_4G;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000670 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
671 flags |= PIN_GLOBAL | PIN_MAPPABLE;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000672 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
673 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson506a8e82015-12-08 11:55:07 +0000674 if (entry->flags & EXEC_OBJECT_PINNED)
675 flags |= entry->offset | PIN_OFFSET_FIXED;
Michel Thierry101b5062015-10-01 13:33:57 +0100676 if ((flags & PIN_MAPPABLE) == 0)
677 flags |= PIN_HIGH;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000678 }
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100679
Chris Wilson59bfa122016-08-04 16:32:31 +0100680 ret = i915_vma_pin(vma,
681 entry->pad_to_size,
682 entry->alignment,
683 flags);
684 if ((ret == -ENOSPC || ret == -E2BIG) &&
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000685 only_mappable_for_reloc(entry->flags))
Chris Wilson59bfa122016-08-04 16:32:31 +0100686 ret = i915_vma_pin(vma,
687 entry->pad_to_size,
688 entry->alignment,
689 flags & ~PIN_MAPPABLE);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100690 if (ret)
691 return ret;
692
Chris Wilson7788a762012-08-24 19:18:18 +0100693 entry->flags |= __EXEC_OBJECT_HAS_PIN;
694
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100695 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
696 ret = i915_gem_object_get_fence(obj);
697 if (ret)
698 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100699
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100700 if (i915_gem_object_pin_fence(obj))
701 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100702 }
703
Ben Widawsky27173f12013-08-14 11:38:36 +0200704 if (entry->offset != vma->node.start) {
705 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100706 *need_reloc = true;
707 }
708
709 if (entry->flags & EXEC_OBJECT_WRITE) {
710 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
711 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
712 }
713
Chris Wilson1690e1e2011-12-14 13:57:08 +0100714 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100715}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100716
Chris Wilsond23db882014-05-23 08:48:08 +0200717static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200718need_reloc_mappable(struct i915_vma *vma)
719{
720 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
721
722 if (entry->relocation_count == 0)
723 return false;
724
Chris Wilson3272db52016-08-04 16:32:32 +0100725 if (!i915_vma_is_ggtt(vma))
Chris Wilsone6a84462014-08-11 12:00:12 +0200726 return false;
727
728 /* See also use_cpu_reloc() */
729 if (HAS_LLC(vma->obj->base.dev))
730 return false;
731
732 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
733 return false;
734
735 return true;
736}
737
738static bool
739eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200740{
741 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
742 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond23db882014-05-23 08:48:08 +0200743
Chris Wilson3272db52016-08-04 16:32:32 +0100744 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
745 !i915_vma_is_ggtt(vma));
Chris Wilsond23db882014-05-23 08:48:08 +0200746
747 if (entry->alignment &&
748 vma->node.start & (entry->alignment - 1))
749 return true;
750
Chris Wilson91b2db62016-08-04 16:32:23 +0100751 if (vma->node.size < entry->pad_to_size)
752 return true;
753
Chris Wilson506a8e82015-12-08 11:55:07 +0000754 if (entry->flags & EXEC_OBJECT_PINNED &&
755 vma->node.start != entry->offset)
756 return true;
757
Chris Wilsond23db882014-05-23 08:48:08 +0200758 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
759 vma->node.start < BATCH_OFFSET_BIAS)
760 return true;
761
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000762 /* avoid costly ping-pong once a batch bo ended up non-mappable */
763 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
764 return !only_mappable_for_reloc(entry->flags);
765
Michel Thierry101b5062015-10-01 13:33:57 +0100766 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
767 (vma->node.start + vma->node.size - 1) >> 32)
768 return true;
769
Chris Wilsond23db882014-05-23 08:48:08 +0200770 return false;
771}
772
Chris Wilson54cf91d2010-11-25 18:00:26 +0000773static int
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000774i915_gem_execbuffer_reserve(struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200775 struct list_head *vmas,
Chris Wilsone2efd132016-05-24 14:53:34 +0100776 struct i915_gem_context *ctx,
Daniel Vettered5982e2013-01-17 22:23:36 +0100777 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000778{
Chris Wilson432e58e2010-11-25 19:32:06 +0000779 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200780 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700781 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200782 struct list_head ordered_vmas;
Chris Wilson506a8e82015-12-08 11:55:07 +0000783 struct list_head pinned_vmas;
Chris Wilsonc0336662016-05-06 15:40:21 +0100784 bool has_fenced_gpu_access = INTEL_GEN(engine->i915) < 4;
Chris Wilson7788a762012-08-24 19:18:18 +0100785 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000786
Ben Widawsky68c8c172013-09-11 14:57:50 -0700787 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
788
Ben Widawsky27173f12013-08-14 11:38:36 +0200789 INIT_LIST_HEAD(&ordered_vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000790 INIT_LIST_HEAD(&pinned_vmas);
Ben Widawsky27173f12013-08-14 11:38:36 +0200791 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000792 struct drm_i915_gem_exec_object2 *entry;
793 bool need_fence, need_mappable;
794
Ben Widawsky27173f12013-08-14 11:38:36 +0200795 vma = list_first_entry(vmas, struct i915_vma, exec_list);
796 obj = vma->obj;
797 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000798
David Weinehallb1b38272015-05-20 17:00:13 +0300799 if (ctx->flags & CONTEXT_NO_ZEROMAP)
800 entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
801
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100802 if (!has_fenced_gpu_access)
803 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000804 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000805 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
Chris Wilson3e510a82016-08-05 10:14:23 +0100806 i915_gem_object_is_tiled(obj);
Ben Widawsky27173f12013-08-14 11:38:36 +0200807 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000808
Chris Wilson506a8e82015-12-08 11:55:07 +0000809 if (entry->flags & EXEC_OBJECT_PINNED)
810 list_move_tail(&vma->exec_list, &pinned_vmas);
811 else if (need_mappable) {
Chris Wilsone6a84462014-08-11 12:00:12 +0200812 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200813 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200814 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200815 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000816
Daniel Vettered5982e2013-01-17 22:23:36 +0100817 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000818 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000819 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200820 list_splice(&ordered_vmas, vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000821 list_splice(&pinned_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000822
823 /* Attempt to pin all of the buffers into the GTT.
824 * This is done in 3 phases:
825 *
826 * 1a. Unbind all objects that do not match the GTT constraints for
827 * the execbuffer (fenceable, mappable, alignment etc).
828 * 1b. Increment pin count for already bound objects.
829 * 2. Bind new objects.
830 * 3. Decrement pin count.
831 *
Chris Wilson7788a762012-08-24 19:18:18 +0100832 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000833 * room for the earlier objects *unless* we need to defragment.
834 */
835 retry = 0;
836 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100837 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000838
839 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200840 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200841 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000842 continue;
843
Chris Wilsone6a84462014-08-11 12:00:12 +0200844 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200845 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000846 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000847 ret = i915_gem_execbuffer_reserve_vma(vma,
848 engine,
849 need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000850 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000851 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000852 }
853
854 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200855 list_for_each_entry(vma, vmas, exec_list) {
856 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100857 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000858
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000859 ret = i915_gem_execbuffer_reserve_vma(vma, engine,
860 need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100861 if (ret)
862 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000863 }
864
Chris Wilsona415d352013-11-26 11:23:15 +0000865err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200866 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000867 return ret;
868
Chris Wilsona415d352013-11-26 11:23:15 +0000869 /* Decrement pin count for bound objects */
870 list_for_each_entry(vma, vmas, exec_list)
871 i915_gem_execbuffer_unreserve_vma(vma);
872
Ben Widawsky68c8c172013-09-11 14:57:50 -0700873 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000874 if (ret)
875 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000876 } while (1);
877}
878
879static int
880i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100881 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000882 struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000883 struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200884 struct eb_vmas *eb,
David Weinehallb1b38272015-05-20 17:00:13 +0300885 struct drm_i915_gem_exec_object2 *exec,
Chris Wilsone2efd132016-05-24 14:53:34 +0100886 struct i915_gem_context *ctx)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000887{
888 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200889 struct i915_address_space *vm;
890 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100891 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000892 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000893 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200894 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000895
Ben Widawsky27173f12013-08-14 11:38:36 +0200896 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
897
Chris Wilson67731b82010-12-08 10:38:14 +0000898 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200899 while (!list_empty(&eb->vmas)) {
900 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
901 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000902 i915_gem_execbuffer_unreserve_vma(vma);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100903 i915_gem_object_put(vma->obj);
Chris Wilson67731b82010-12-08 10:38:14 +0000904 }
905
Chris Wilson54cf91d2010-11-25 18:00:26 +0000906 mutex_unlock(&dev->struct_mutex);
907
908 total = 0;
909 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000910 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000911
Chris Wilsondd6864a2011-01-12 23:49:13 +0000912 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000913 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000914 if (reloc == NULL || reloc_offset == NULL) {
915 drm_free_large(reloc);
916 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000917 mutex_lock(&dev->struct_mutex);
918 return -ENOMEM;
919 }
920
921 total = 0;
922 for (i = 0; i < count; i++) {
923 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000924 u64 invalid_offset = (u64)-1;
925 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000926
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300927 user_relocs = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000928
929 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000930 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000931 ret = -EFAULT;
932 mutex_lock(&dev->struct_mutex);
933 goto err;
934 }
935
Chris Wilson262b6d32013-01-15 16:17:54 +0000936 /* As we do not update the known relocation offsets after
937 * relocating (due to the complexities in lock handling),
938 * we need to mark them as invalid now so that we force the
939 * relocation processing next time. Just in case the target
940 * object is evicted and then rebound into its old
941 * presumed_offset before the next execbuffer - if that
942 * happened we would make the mistake of assuming that the
943 * relocations were valid.
944 */
945 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100946 if (__copy_to_user(&user_relocs[j].presumed_offset,
947 &invalid_offset,
948 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000949 ret = -EFAULT;
950 mutex_lock(&dev->struct_mutex);
951 goto err;
952 }
953 }
954
Chris Wilsondd6864a2011-01-12 23:49:13 +0000955 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000956 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000957 }
958
959 ret = i915_mutex_lock_interruptible(dev);
960 if (ret) {
961 mutex_lock(&dev->struct_mutex);
962 goto err;
963 }
964
Chris Wilson67731b82010-12-08 10:38:14 +0000965 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000966 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200967 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000968 if (ret)
969 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000970
Daniel Vettered5982e2013-01-17 22:23:36 +0100971 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000972 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
973 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000974 if (ret)
975 goto err;
976
Ben Widawsky27173f12013-08-14 11:38:36 +0200977 list_for_each_entry(vma, &eb->vmas, exec_list) {
978 int offset = vma->exec_entry - exec;
979 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
980 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000981 if (ret)
982 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000983 }
984
985 /* Leave the user relocations as are, this is the painfully slow path,
986 * and we want to avoid the complication of dropping the lock whilst
987 * having buffers reserved in the aperture and so causing spurious
988 * ENOSPC for random operations.
989 */
990
991err:
992 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000993 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000994 return ret;
995}
996
Chris Wilson573adb32016-08-04 16:32:39 +0100997static unsigned int eb_other_engines(struct drm_i915_gem_request *req)
998{
999 unsigned int mask;
1000
1001 mask = ~intel_engine_flag(req->engine) & I915_BO_ACTIVE_MASK;
1002 mask <<= I915_BO_ACTIVE_SHIFT;
1003
1004 return mask;
1005}
1006
Chris Wilson54cf91d2010-11-25 18:00:26 +00001007static int
John Harrison535fbe82015-05-29 17:43:32 +01001008i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
Ben Widawsky27173f12013-08-14 11:38:36 +02001009 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001010{
Chris Wilson573adb32016-08-04 16:32:39 +01001011 const unsigned int other_rings = eb_other_engines(req);
Ben Widawsky27173f12013-08-14 11:38:36 +02001012 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001013 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +01001014 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +00001015 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001016
Ben Widawsky27173f12013-08-14 11:38:36 +02001017 list_for_each_entry(vma, vmas, exec_list) {
1018 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001019
Chris Wilson573adb32016-08-04 16:32:39 +01001020 if (obj->flags & other_rings) {
Chris Wilson8e637172016-08-02 22:50:26 +01001021 ret = i915_gem_object_sync(obj, req);
Chris Wilson03ade512015-04-27 13:41:18 +01001022 if (ret)
1023 return ret;
1024 }
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001025
1026 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +01001027 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001028
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001029 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001030 }
1031
Chris Wilson000433b2013-08-08 14:41:09 +01001032 if (flush_chipset)
Chris Wilsonc0336662016-05-06 15:40:21 +01001033 i915_gem_chipset_flush(req->engine->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001034
1035 if (flush_domains & I915_GEM_DOMAIN_GTT)
1036 wmb();
1037
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001038 /* Unconditionally invalidate GPU caches and TLBs. */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001039 return req->engine->emit_flush(req, EMIT_INVALIDATE);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001040}
1041
Chris Wilson432e58e2010-11-25 19:32:06 +00001042static bool
1043i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001044{
Daniel Vettered5982e2013-01-17 22:23:36 +01001045 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
1046 return false;
1047
Chris Wilson2f5945b2015-10-06 11:39:55 +01001048 /* Kernel clipping was a DRI1 misfeature */
1049 if (exec->num_cliprects || exec->cliprects_ptr)
1050 return false;
1051
1052 if (exec->DR4 == 0xffffffff) {
1053 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1054 exec->DR4 = 0;
1055 }
1056 if (exec->DR1 || exec->DR4)
1057 return false;
1058
1059 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1060 return false;
1061
1062 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001063}
1064
1065static int
Chris Wilsonad19f102014-08-10 06:29:08 +01001066validate_exec_list(struct drm_device *dev,
1067 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001068 int count)
1069{
Daniel Vetterb205ca52013-09-19 14:00:11 +02001070 unsigned relocs_total = 0;
1071 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +01001072 unsigned invalid_flags;
1073 int i;
1074
Dave Gordon9e2793f62016-07-14 14:52:03 +01001075 /* INTERNAL flags must not overlap with external ones */
1076 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
1077
Chris Wilsonad19f102014-08-10 06:29:08 +01001078 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
1079 if (USES_FULL_PPGTT(dev))
1080 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001081
1082 for (i = 0; i < count; i++) {
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001083 char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001084 int length; /* limited by fault_in_pages_readable() */
1085
Chris Wilsonad19f102014-08-10 06:29:08 +01001086 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +01001087 return -EINVAL;
1088
Michał Winiarski934acce2015-12-29 18:24:52 +01001089 /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
1090 * any non-page-aligned or non-canonical addresses.
1091 */
1092 if (exec[i].flags & EXEC_OBJECT_PINNED) {
1093 if (exec[i].offset !=
1094 gen8_canonical_addr(exec[i].offset & PAGE_MASK))
1095 return -EINVAL;
1096
1097 /* From drm_mm perspective address space is continuous,
1098 * so from this point we're always using non-canonical
1099 * form internally.
1100 */
1101 exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
1102 }
1103
Chris Wilson55a97852015-06-19 13:59:46 +01001104 if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
1105 return -EINVAL;
1106
Chris Wilson91b2db62016-08-04 16:32:23 +01001107 /* pad_to_size was once a reserved field, so sanitize it */
1108 if (exec[i].flags & EXEC_OBJECT_PAD_TO_SIZE) {
1109 if (offset_in_page(exec[i].pad_to_size))
1110 return -EINVAL;
1111 } else {
1112 exec[i].pad_to_size = 0;
1113 }
1114
Kees Cook3118a4f2013-03-11 17:31:45 -07001115 /* First check for malicious input causing overflow in
1116 * the worst case where we need to allocate the entire
1117 * relocation tree as a single array.
1118 */
1119 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001120 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -07001121 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001122
1123 length = exec[i].relocation_count *
1124 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -07001125 /*
1126 * We must check that the entire relocation array is safe
1127 * to read, but since we may need to update the presumed
1128 * offsets during execution, check for full write access.
1129 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001130 if (!access_ok(VERIFY_WRITE, ptr, length))
1131 return -EFAULT;
1132
Jani Nikulad330a952014-01-21 11:24:25 +02001133 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +08001134 if (fault_in_multipages_readable(ptr, length))
1135 return -EFAULT;
1136 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001137 }
1138
1139 return 0;
1140}
1141
Chris Wilsone2efd132016-05-24 14:53:34 +01001142static struct i915_gem_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001143i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001144 struct intel_engine_cs *engine, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001145{
Chris Wilsone2efd132016-05-24 14:53:34 +01001146 struct i915_gem_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001147 struct i915_ctx_hang_stats *hs;
1148
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001149 if (engine->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001150 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001151
Chris Wilsonca585b52016-05-24 14:53:36 +01001152 ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001153 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -08001154 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001155
Ben Widawsky41bde552013-12-06 14:11:21 -08001156 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001157 if (hs->banned) {
1158 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -08001159 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001160 }
1161
Ben Widawsky41bde552013-12-06 14:11:21 -08001162 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001163}
1164
Chris Wilson5cf3d282016-08-04 07:52:43 +01001165void i915_vma_move_to_active(struct i915_vma *vma,
1166 struct drm_i915_gem_request *req,
1167 unsigned int flags)
1168{
1169 struct drm_i915_gem_object *obj = vma->obj;
1170 const unsigned int idx = req->engine->id;
1171
1172 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1173
1174 obj->dirty = 1; /* be paranoid */
1175
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001176 /* Add a reference if we're newly entering the active list.
1177 * The order in which we add operations to the retirement queue is
1178 * vital here: mark_active adds to the start of the callback list,
1179 * such that subsequent callbacks are called first. Therefore we
1180 * add the active reference first and queue for it to be dropped
1181 * *last*.
1182 */
Chris Wilson573adb32016-08-04 16:32:39 +01001183 if (!i915_gem_object_is_active(obj))
Chris Wilson5cf3d282016-08-04 07:52:43 +01001184 i915_gem_object_get(obj);
Chris Wilson573adb32016-08-04 16:32:39 +01001185 i915_gem_object_set_active(obj, idx);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001186 i915_gem_active_set(&obj->last_read[idx], req);
1187
1188 if (flags & EXEC_OBJECT_WRITE) {
1189 i915_gem_active_set(&obj->last_write, req);
1190
1191 intel_fb_obj_invalidate(obj, ORIGIN_CS);
1192
1193 /* update for the implicit flush after a batch */
1194 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1195 }
1196
1197 if (flags & EXEC_OBJECT_NEEDS_FENCE) {
1198 i915_gem_active_set(&obj->last_fence, req);
1199 if (flags & __EXEC_OBJECT_HAS_FENCE) {
1200 struct drm_i915_private *dev_priv = req->i915;
1201
1202 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
1203 &dev_priv->mm.fence_list);
1204 }
1205 }
1206
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001207 i915_vma_set_active(vma, idx);
1208 i915_gem_active_set(&vma->last_read[idx], req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001209 list_move_tail(&vma->vm_link, &vma->vm->active_list);
1210}
1211
Chris Wilsonad778f82016-08-04 16:32:42 +01001212static void eb_export_fence(struct drm_i915_gem_object *obj,
1213 struct drm_i915_gem_request *req,
1214 unsigned int flags)
1215{
1216 struct reservation_object *resv;
1217
1218 resv = i915_gem_object_get_dmabuf_resv(obj);
1219 if (!resv)
1220 return;
1221
1222 /* Ignore errors from failing to allocate the new fence, we can't
1223 * handle an error right now. Worst case should be missed
1224 * synchronisation leading to rendering corruption.
1225 */
1226 ww_mutex_lock(&resv->lock, NULL);
1227 if (flags & EXEC_OBJECT_WRITE)
1228 reservation_object_add_excl_fence(resv, &req->fence);
1229 else if (reservation_object_reserve_shared(resv) == 0)
1230 reservation_object_add_shared_fence(resv, &req->fence);
1231 ww_mutex_unlock(&resv->lock);
1232}
1233
Chris Wilson5b043f42016-08-02 22:50:38 +01001234static void
Ben Widawsky27173f12013-08-14 11:38:36 +02001235i915_gem_execbuffer_move_to_active(struct list_head *vmas,
John Harrison8a8edb52015-05-29 17:43:33 +01001236 struct drm_i915_gem_request *req)
Chris Wilson432e58e2010-11-25 19:32:06 +00001237{
Ben Widawsky27173f12013-08-14 11:38:36 +02001238 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +00001239
Ben Widawsky27173f12013-08-14 11:38:36 +02001240 list_for_each_entry(vma, vmas, exec_list) {
1241 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +01001242 u32 old_read = obj->base.read_domains;
1243 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +00001244
Chris Wilson432e58e2010-11-25 19:32:06 +00001245 obj->base.write_domain = obj->base.pending_write_domain;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001246 if (obj->base.write_domain)
1247 vma->exec_entry->flags |= EXEC_OBJECT_WRITE;
1248 else
Daniel Vettered5982e2013-01-17 22:23:36 +01001249 obj->base.pending_read_domains |= obj->base.read_domains;
1250 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +00001251
Chris Wilson5cf3d282016-08-04 07:52:43 +01001252 i915_vma_move_to_active(vma, req, vma->exec_entry->flags);
Chris Wilsonad778f82016-08-04 16:32:42 +01001253 eb_export_fence(obj, req, vma->exec_entry->flags);
Chris Wilsondb53a302011-02-03 11:57:46 +00001254 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +00001255 }
1256}
1257
Chris Wilson54cf91d2010-11-25 18:00:26 +00001258static int
Chris Wilsonb5321f32016-08-02 22:50:18 +01001259i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001260{
Chris Wilson7e37f882016-08-02 22:50:21 +01001261 struct intel_ring *ring = req->ring;
Eric Anholtae662d32012-01-03 09:23:29 -08001262 int ret, i;
1263
Chris Wilsonb5321f32016-08-02 22:50:18 +01001264 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001265 DRM_DEBUG("sol reset is gen7/rcs only\n");
1266 return -EINVAL;
1267 }
Eric Anholtae662d32012-01-03 09:23:29 -08001268
John Harrison5fb9de12015-05-29 17:44:07 +01001269 ret = intel_ring_begin(req, 4 * 3);
Eric Anholtae662d32012-01-03 09:23:29 -08001270 if (ret)
1271 return ret;
1272
1273 for (i = 0; i < 4; i++) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001274 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1275 intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
1276 intel_ring_emit(ring, 0);
Eric Anholtae662d32012-01-03 09:23:29 -08001277 }
1278
Chris Wilsonb5321f32016-08-02 22:50:18 +01001279 intel_ring_advance(ring);
Eric Anholtae662d32012-01-03 09:23:29 -08001280
1281 return 0;
1282}
1283
Chris Wilson59bfa122016-08-04 16:32:31 +01001284static struct i915_vma*
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001285i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
Brad Volkin71745372014-12-11 12:13:12 -08001286 struct drm_i915_gem_exec_object2 *shadow_exec_entry,
Brad Volkin71745372014-12-11 12:13:12 -08001287 struct drm_i915_gem_object *batch_obj,
Chris Wilson59bfa122016-08-04 16:32:31 +01001288 struct eb_vmas *eb,
Brad Volkin71745372014-12-11 12:13:12 -08001289 u32 batch_start_offset,
1290 u32 batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001291 bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001292{
Brad Volkin71745372014-12-11 12:13:12 -08001293 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001294 struct i915_vma *vma;
Brad Volkin71745372014-12-11 12:13:12 -08001295 int ret;
1296
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001297 shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
Chris Wilson17cabf52015-01-14 11:20:57 +00001298 PAGE_ALIGN(batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001299 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001300 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001301
Chris Wilson33a051a2016-07-27 09:07:26 +01001302 ret = intel_engine_cmd_parser(engine,
1303 batch_obj,
1304 shadow_batch_obj,
1305 batch_start_offset,
1306 batch_len,
1307 is_master);
Chris Wilson17cabf52015-01-14 11:20:57 +00001308 if (ret)
1309 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001310
Chris Wilsonde895082016-08-04 16:32:34 +01001311 ret = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
Chris Wilson17cabf52015-01-14 11:20:57 +00001312 if (ret)
1313 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001314
Chris Wilsonde4e7832015-04-07 16:20:35 +01001315 i915_gem_object_unpin_pages(shadow_batch_obj);
1316
Chris Wilson17cabf52015-01-14 11:20:57 +00001317 memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
Brad Volkin71745372014-12-11 12:13:12 -08001318
Chris Wilson17cabf52015-01-14 11:20:57 +00001319 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1320 vma->exec_entry = shadow_exec_entry;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001321 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
Chris Wilson25dc5562016-07-20 13:31:52 +01001322 i915_gem_object_get(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001323 list_add_tail(&vma->exec_list, &eb->vmas);
Brad Volkin71745372014-12-11 12:13:12 -08001324
Chris Wilson59bfa122016-08-04 16:32:31 +01001325 return vma;
Chris Wilson17cabf52015-01-14 11:20:57 +00001326
1327err:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001328 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001329 if (ret == -EACCES) /* unhandled chained batch */
Chris Wilson59bfa122016-08-04 16:32:31 +01001330 return NULL;
Chris Wilson17cabf52015-01-14 11:20:57 +00001331 else
1332 return ERR_PTR(ret);
Brad Volkin71745372014-12-11 12:13:12 -08001333}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001334
Chris Wilson5b043f42016-08-02 22:50:38 +01001335static int
1336execbuf_submit(struct i915_execbuffer_params *params,
1337 struct drm_i915_gem_execbuffer2 *args,
1338 struct list_head *vmas)
Oscar Mateo78382592014-07-03 16:28:05 +01001339{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001340 struct drm_i915_private *dev_priv = params->request->i915;
John Harrison5f19e2b2015-05-29 17:43:27 +01001341 u64 exec_start, exec_len;
Oscar Mateo78382592014-07-03 16:28:05 +01001342 int instp_mode;
1343 u32 instp_mask;
Chris Wilson2f5945b2015-10-06 11:39:55 +01001344 int ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001345
John Harrison535fbe82015-05-29 17:43:32 +01001346 ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
Oscar Mateo78382592014-07-03 16:28:05 +01001347 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001348 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001349
John Harrisonba01cc92015-05-29 17:43:41 +01001350 ret = i915_switch_context(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001351 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001352 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001353
1354 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1355 instp_mask = I915_EXEC_CONSTANTS_MASK;
1356 switch (instp_mode) {
1357 case I915_EXEC_CONSTANTS_REL_GENERAL:
1358 case I915_EXEC_CONSTANTS_ABSOLUTE:
1359 case I915_EXEC_CONSTANTS_REL_SURFACE:
Chris Wilsonb5321f32016-08-02 22:50:18 +01001360 if (instp_mode != 0 && params->engine->id != RCS) {
Oscar Mateo78382592014-07-03 16:28:05 +01001361 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001362 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001363 }
1364
1365 if (instp_mode != dev_priv->relative_constants_mode) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001366 if (INTEL_INFO(dev_priv)->gen < 4) {
Oscar Mateo78382592014-07-03 16:28:05 +01001367 DRM_DEBUG("no rel constants on pre-gen4\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001368 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001369 }
1370
Chris Wilsonb5321f32016-08-02 22:50:18 +01001371 if (INTEL_INFO(dev_priv)->gen > 5 &&
Oscar Mateo78382592014-07-03 16:28:05 +01001372 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1373 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001374 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001375 }
1376
1377 /* The HW changed the meaning on this bit on gen6 */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001378 if (INTEL_INFO(dev_priv)->gen >= 6)
Oscar Mateo78382592014-07-03 16:28:05 +01001379 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1380 }
1381 break;
1382 default:
1383 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001384 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001385 }
1386
Chris Wilsonb5321f32016-08-02 22:50:18 +01001387 if (params->engine->id == RCS &&
Chris Wilson2f5945b2015-10-06 11:39:55 +01001388 instp_mode != dev_priv->relative_constants_mode) {
Chris Wilson7e37f882016-08-02 22:50:21 +01001389 struct intel_ring *ring = params->request->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01001390
John Harrison5fb9de12015-05-29 17:44:07 +01001391 ret = intel_ring_begin(params->request, 4);
Oscar Mateo78382592014-07-03 16:28:05 +01001392 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001393 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001394
Chris Wilsonb5321f32016-08-02 22:50:18 +01001395 intel_ring_emit(ring, MI_NOOP);
1396 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1397 intel_ring_emit_reg(ring, INSTPM);
1398 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1399 intel_ring_advance(ring);
Oscar Mateo78382592014-07-03 16:28:05 +01001400
1401 dev_priv->relative_constants_mode = instp_mode;
1402 }
1403
1404 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001405 ret = i915_reset_gen7_sol_offsets(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001406 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001407 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001408 }
1409
John Harrison5f19e2b2015-05-29 17:43:27 +01001410 exec_len = args->batch_len;
Chris Wilson59bfa122016-08-04 16:32:31 +01001411 exec_start = params->batch->node.start +
John Harrison5f19e2b2015-05-29 17:43:27 +01001412 params->args_batch_start_offset;
1413
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001414 if (exec_len == 0)
Chris Wilson59bfa122016-08-04 16:32:31 +01001415 exec_len = params->batch->size;
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001416
Chris Wilson803688b2016-08-02 22:50:27 +01001417 ret = params->engine->emit_bb_start(params->request,
1418 exec_start, exec_len,
1419 params->dispatch_flags);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001420 if (ret)
1421 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001422
John Harrison95c24162015-05-29 17:43:31 +01001423 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001424
John Harrison8a8edb52015-05-29 17:43:33 +01001425 i915_gem_execbuffer_move_to_active(vmas, params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001426
Chris Wilson2f5945b2015-10-06 11:39:55 +01001427 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001428}
1429
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001430/**
1431 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01001432 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001433 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001434static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01001435gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
1436 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001437{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001438 struct drm_i915_file_private *file_priv = file->driver_priv;
1439
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001440 /* Check whether the file_priv has already selected one ring. */
Chris Wilsonc80ff162016-07-27 09:07:27 +01001441 if ((int)file_priv->bsd_engine < 0) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001442 /* If not, use the ping-pong mechanism to select one. */
Chris Wilson91c8a322016-07-05 10:40:23 +01001443 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonc80ff162016-07-27 09:07:27 +01001444 file_priv->bsd_engine = dev_priv->mm.bsd_engine_dispatch_index;
1445 dev_priv->mm.bsd_engine_dispatch_index ^= 1;
Chris Wilson91c8a322016-07-05 10:40:23 +01001446 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001447 }
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001448
Chris Wilsonc80ff162016-07-27 09:07:27 +01001449 return file_priv->bsd_engine;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001450}
1451
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001452#define I915_USER_RINGS (4)
1453
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00001454static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001455 [I915_EXEC_DEFAULT] = RCS,
1456 [I915_EXEC_RENDER] = RCS,
1457 [I915_EXEC_BLT] = BCS,
1458 [I915_EXEC_BSD] = VCS,
1459 [I915_EXEC_VEBOX] = VECS
1460};
1461
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001462static struct intel_engine_cs *
1463eb_select_engine(struct drm_i915_private *dev_priv,
1464 struct drm_file *file,
1465 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001466{
1467 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001468 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001469
1470 if (user_ring_id > I915_USER_RINGS) {
1471 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001472 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001473 }
1474
1475 if ((user_ring_id != I915_EXEC_BSD) &&
1476 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1477 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1478 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001479 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001480 }
1481
1482 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
1483 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
1484
1485 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01001486 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001487 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
1488 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00001489 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001490 bsd_idx--;
1491 } else {
1492 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
1493 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001494 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001495 }
1496
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001497 engine = &dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001498 } else {
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001499 engine = &dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001500 }
1501
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001502 if (!intel_engine_initialized(engine)) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001503 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001504 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001505 }
1506
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001507 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001508}
1509
Eric Anholtae662d32012-01-03 09:23:29 -08001510static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001511i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1512 struct drm_file *file,
1513 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001514 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001515{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001516 struct drm_i915_private *dev_priv = to_i915(dev);
1517 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky27173f12013-08-14 11:38:36 +02001518 struct eb_vmas *eb;
Brad Volkin78a42372014-12-11 12:13:09 -08001519 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001520 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +01001521 struct i915_gem_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001522 struct i915_address_space *vm;
John Harrison5f19e2b2015-05-29 17:43:27 +01001523 struct i915_execbuffer_params params_master; /* XXX: will be removed later */
1524 struct i915_execbuffer_params *params = &params_master;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001525 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
John Harrison8e004ef2015-02-13 11:48:10 +00001526 u32 dispatch_flags;
Oscar Mateo78382592014-07-03 16:28:05 +01001527 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001528 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001529
Daniel Vettered5982e2013-01-17 22:23:36 +01001530 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001531 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001532
Chris Wilsonad19f102014-08-10 06:29:08 +01001533 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001534 if (ret)
1535 return ret;
1536
John Harrison8e004ef2015-02-13 11:48:10 +00001537 dispatch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001538 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02001539 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001540 return -EPERM;
1541
John Harrison8e004ef2015-02-13 11:48:10 +00001542 dispatch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001543 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001544 if (args->flags & I915_EXEC_IS_PINNED)
John Harrison8e004ef2015-02-13 11:48:10 +00001545 dispatch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001546
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001547 engine = eb_select_engine(dev_priv, file, args);
1548 if (!engine)
1549 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001550
1551 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001552 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001553 return -EINVAL;
1554 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001555
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001556 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
1557 if (!HAS_RESOURCE_STREAMER(dev)) {
1558 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
1559 return -EINVAL;
1560 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001561 if (engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001562 DRM_DEBUG("RS is not available on %s\n",
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001563 engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001564 return -EINVAL;
1565 }
1566
1567 dispatch_flags |= I915_DISPATCH_RS;
1568 }
1569
Chris Wilson67d97da2016-07-04 08:08:31 +01001570 /* Take a local wakeref for preparing to dispatch the execbuf as
1571 * we expect to access the hardware fairly frequently in the
1572 * process. Upon first dispatch, we acquire another prolonged
1573 * wakeref that we hold until the GPU has been idle for at least
1574 * 100ms.
1575 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001576 intel_runtime_pm_get(dev_priv);
1577
Chris Wilson54cf91d2010-11-25 18:00:26 +00001578 ret = i915_mutex_lock_interruptible(dev);
1579 if (ret)
1580 goto pre_mutex_err;
1581
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001582 ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001583 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001584 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001585 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001586 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001587 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001588
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001589 i915_gem_context_get(ctx);
Ben Widawsky41bde552013-12-06 14:11:21 -08001590
Daniel Vetterae6c4802014-08-06 15:04:53 +02001591 if (ctx->ppgtt)
1592 vm = &ctx->ppgtt->base;
1593 else
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001594 vm = &ggtt->base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001595
John Harrison5f19e2b2015-05-29 17:43:27 +01001596 memset(&params_master, 0x00, sizeof(params_master));
1597
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001598 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001599 if (eb == NULL) {
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001600 i915_gem_context_put(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001601 mutex_unlock(&dev->struct_mutex);
1602 ret = -ENOMEM;
1603 goto pre_mutex_err;
1604 }
1605
Chris Wilson54cf91d2010-11-25 18:00:26 +00001606 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001607 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001608 if (ret)
1609 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001610
Chris Wilson6fe4f142011-01-10 17:35:37 +00001611 /* take note of the batch buffer before we might reorder the lists */
Chris Wilson59bfa122016-08-04 16:32:31 +01001612 params->batch = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001613
Chris Wilson54cf91d2010-11-25 18:00:26 +00001614 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001615 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001616 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
1617 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001618 if (ret)
1619 goto err;
1620
1621 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001622 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001623 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001624 if (ret) {
1625 if (ret == -EFAULT) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001626 ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
1627 engine,
David Weinehallb1b38272015-05-20 17:00:13 +03001628 eb, exec, ctx);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001629 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1630 }
1631 if (ret)
1632 goto err;
1633 }
1634
1635 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson59bfa122016-08-04 16:32:31 +01001636 if (params->batch->obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001637 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001638 ret = -EINVAL;
1639 goto err;
1640 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001641
John Harrison5f19e2b2015-05-29 17:43:27 +01001642 params->args_batch_start_offset = args->batch_start_offset;
Chris Wilson33a051a2016-07-27 09:07:26 +01001643 if (intel_engine_needs_cmd_parser(engine) && args->batch_len) {
Chris Wilson59bfa122016-08-04 16:32:31 +01001644 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001645
Chris Wilson59bfa122016-08-04 16:32:31 +01001646 vma = i915_gem_execbuffer_parse(engine, &shadow_exec_entry,
1647 params->batch->obj,
1648 eb,
1649 args->batch_start_offset,
1650 args->batch_len,
1651 drm_is_current_master(file));
1652 if (IS_ERR(vma)) {
1653 ret = PTR_ERR(vma);
Brad Volkin78a42372014-12-11 12:13:09 -08001654 goto err;
1655 }
Chris Wilson17cabf52015-01-14 11:20:57 +00001656
Chris Wilson59bfa122016-08-04 16:32:31 +01001657 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001658 /*
1659 * Batch parsed and accepted:
1660 *
1661 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1662 * bit from MI_BATCH_BUFFER_START commands issued in
1663 * the dispatch_execbuffer implementations. We
1664 * specifically don't want that set on batches the
1665 * command parser has accepted.
1666 */
1667 dispatch_flags |= I915_DISPATCH_SECURE;
John Harrison5f19e2b2015-05-29 17:43:27 +01001668 params->args_batch_start_offset = 0;
Chris Wilson59bfa122016-08-04 16:32:31 +01001669 params->batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001670 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001671 }
1672
Chris Wilson59bfa122016-08-04 16:32:31 +01001673 params->batch->obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Brad Volkin78a42372014-12-11 12:13:09 -08001674
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001675 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1676 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001677 * hsw should have this fixed, but bdw mucks it up again. */
John Harrison8e004ef2015-02-13 11:48:10 +00001678 if (dispatch_flags & I915_DISPATCH_SECURE) {
Chris Wilson59bfa122016-08-04 16:32:31 +01001679 struct drm_i915_gem_object *obj = params->batch->obj;
1680
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001681 /*
1682 * So on first glance it looks freaky that we pin the batch here
1683 * outside of the reservation loop. But:
1684 * - The batch is already pinned into the relevant ppgtt, so we
1685 * already have the backing storage fully allocated.
1686 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01001687 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001688 * fitting due to fragmentation.
1689 * So this is actually safe.
1690 */
Chris Wilsonde895082016-08-04 16:32:34 +01001691 ret = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001692 if (ret)
1693 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001694
Chris Wilson59bfa122016-08-04 16:32:31 +01001695 params->batch = i915_gem_obj_to_ggtt(obj);
1696 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001697
John Harrison0c8dac82015-05-29 17:43:25 +01001698 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson8e637172016-08-02 22:50:26 +01001699 params->request = i915_gem_request_alloc(engine, ctx);
1700 if (IS_ERR(params->request)) {
1701 ret = PTR_ERR(params->request);
John Harrison0c8dac82015-05-29 17:43:25 +01001702 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00001703 }
John Harrison0c8dac82015-05-29 17:43:25 +01001704
Chris Wilson17f298cf2016-08-10 13:41:46 +01001705 /* Whilst this request exists, batch_obj will be on the
1706 * active_list, and so will hold the active reference. Only when this
1707 * request is retired will the the batch_obj be moved onto the
1708 * inactive_list and lose its active reference. Hence we do not need
1709 * to explicitly hold another reference here.
1710 */
1711 params->request->batch_obj = params->batch->obj;
1712
Chris Wilson8e637172016-08-02 22:50:26 +01001713 ret = i915_gem_request_add_to_client(params->request, file);
John Harrisonfcfa423c2015-05-29 17:44:12 +01001714 if (ret)
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001715 goto err_request;
John Harrisonfcfa423c2015-05-29 17:44:12 +01001716
John Harrison5f19e2b2015-05-29 17:43:27 +01001717 /*
1718 * Save assorted stuff away to pass through to *_submission().
1719 * NB: This data should be 'persistent' and not local as it will
1720 * kept around beyond the duration of the IOCTL once the GPU
1721 * scheduler arrives.
1722 */
1723 params->dev = dev;
1724 params->file = file;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001725 params->engine = engine;
John Harrison5f19e2b2015-05-29 17:43:27 +01001726 params->dispatch_flags = dispatch_flags;
John Harrison5f19e2b2015-05-29 17:43:27 +01001727 params->ctx = ctx;
1728
Chris Wilson5b043f42016-08-02 22:50:38 +01001729 ret = execbuf_submit(params, args, &eb->vmas);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001730err_request:
Chris Wilson17f298cf2016-08-10 13:41:46 +01001731 __i915_add_request(params->request, ret == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001732
John Harrison0c8dac82015-05-29 17:43:25 +01001733err_batch_unpin:
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001734 /*
1735 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1736 * batch vma for correctness. For less ugly and less fragility this
1737 * needs to be adjusted to also track the ggtt batch vma properly as
1738 * active.
1739 */
John Harrison8e004ef2015-02-13 11:48:10 +00001740 if (dispatch_flags & I915_DISPATCH_SECURE)
Chris Wilson59bfa122016-08-04 16:32:31 +01001741 i915_vma_unpin(params->batch);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001742err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001743 /* the request owns the ref now */
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001744 i915_gem_context_put(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001745 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001746
1747 mutex_unlock(&dev->struct_mutex);
1748
1749pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001750 /* intel_gpu_busy should also get a ref, so it will free when the device
1751 * is really idle. */
1752 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001753 return ret;
1754}
1755
1756/*
1757 * Legacy execbuffer just creates an exec2 list from the original exec object
1758 * list array and passes it to the real function.
1759 */
1760int
1761i915_gem_execbuffer(struct drm_device *dev, void *data,
1762 struct drm_file *file)
1763{
1764 struct drm_i915_gem_execbuffer *args = data;
1765 struct drm_i915_gem_execbuffer2 exec2;
1766 struct drm_i915_gem_exec_object *exec_list = NULL;
1767 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1768 int ret, i;
1769
Chris Wilson54cf91d2010-11-25 18:00:26 +00001770 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001771 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001772 return -EINVAL;
1773 }
1774
1775 /* Copy in the exec list from userland */
1776 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1777 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1778 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001779 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001780 args->buffer_count);
1781 drm_free_large(exec_list);
1782 drm_free_large(exec2_list);
1783 return -ENOMEM;
1784 }
1785 ret = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001786 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001787 sizeof(*exec_list) * args->buffer_count);
1788 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001789 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001790 args->buffer_count, ret);
1791 drm_free_large(exec_list);
1792 drm_free_large(exec2_list);
1793 return -EFAULT;
1794 }
1795
1796 for (i = 0; i < args->buffer_count; i++) {
1797 exec2_list[i].handle = exec_list[i].handle;
1798 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1799 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1800 exec2_list[i].alignment = exec_list[i].alignment;
1801 exec2_list[i].offset = exec_list[i].offset;
1802 if (INTEL_INFO(dev)->gen < 4)
1803 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1804 else
1805 exec2_list[i].flags = 0;
1806 }
1807
1808 exec2.buffers_ptr = args->buffers_ptr;
1809 exec2.buffer_count = args->buffer_count;
1810 exec2.batch_start_offset = args->batch_start_offset;
1811 exec2.batch_len = args->batch_len;
1812 exec2.DR1 = args->DR1;
1813 exec2.DR4 = args->DR4;
1814 exec2.num_cliprects = args->num_cliprects;
1815 exec2.cliprects_ptr = args->cliprects_ptr;
1816 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001817 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001818
Ben Widawsky41bde552013-12-06 14:11:21 -08001819 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001820 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001821 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001822 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001823
Chris Wilson54cf91d2010-11-25 18:00:26 +00001824 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001825 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001826 exec2_list[i].offset =
1827 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001828 ret = __copy_to_user(&user_exec_list[i].offset,
1829 &exec2_list[i].offset,
1830 sizeof(user_exec_list[i].offset));
1831 if (ret) {
1832 ret = -EFAULT;
1833 DRM_DEBUG("failed to copy %d exec entries "
1834 "back to user (%d)\n",
1835 args->buffer_count, ret);
1836 break;
1837 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001838 }
1839 }
1840
1841 drm_free_large(exec_list);
1842 drm_free_large(exec2_list);
1843 return ret;
1844}
1845
1846int
1847i915_gem_execbuffer2(struct drm_device *dev, void *data,
1848 struct drm_file *file)
1849{
1850 struct drm_i915_gem_execbuffer2 *args = data;
1851 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1852 int ret;
1853
Xi Wanged8cd3b2012-04-23 04:06:41 -04001854 if (args->buffer_count < 1 ||
1855 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001856 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001857 return -EINVAL;
1858 }
1859
Daniel Vetter9cb34662014-04-24 08:09:11 +02001860 if (args->rsvd2 != 0) {
1861 DRM_DEBUG("dirty rvsd2 field\n");
1862 return -EINVAL;
1863 }
1864
Chris Wilsonf2a85e12016-04-08 12:11:13 +01001865 exec2_list = drm_malloc_gfp(args->buffer_count,
1866 sizeof(*exec2_list),
1867 GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001868 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001869 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001870 args->buffer_count);
1871 return -ENOMEM;
1872 }
1873 ret = copy_from_user(exec2_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001874 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001875 sizeof(*exec2_list) * args->buffer_count);
1876 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001877 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001878 args->buffer_count, ret);
1879 drm_free_large(exec2_list);
1880 return -EFAULT;
1881 }
1882
Ben Widawsky41bde552013-12-06 14:11:21 -08001883 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001884 if (!ret) {
1885 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001886 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001887 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001888 int i;
1889
1890 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001891 exec2_list[i].offset =
1892 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001893 ret = __copy_to_user(&user_exec_list[i].offset,
1894 &exec2_list[i].offset,
1895 sizeof(user_exec_list[i].offset));
1896 if (ret) {
1897 ret = -EFAULT;
1898 DRM_DEBUG("failed to copy %d exec entries "
1899 "back to user\n",
1900 args->buffer_count);
1901 break;
1902 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001903 }
1904 }
1905
1906 drm_free_large(exec2_list);
1907 return ret;
1908}