blob: e8e194fa2e6593e8f9fc2fc8d5ead40f247cfbb4 [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
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
30#include <drm/i915_drm.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000031#include "i915_drv.h"
32#include "i915_trace.h"
33#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010034#include "intel_frontbuffer.h"
Eugeni Dodonovf45b5552011-12-09 17:16:37 -080035#include <linux/dma_remapping.h>
David Hildenbrand32d82062015-05-11 17:52:12 +020036#include <linux/uaccess.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000037
Dave Gordon9e2793f62016-07-14 14:52:03 +010038#define __EXEC_OBJECT_HAS_PIN (1<<31)
39#define __EXEC_OBJECT_HAS_FENCE (1<<30)
40#define __EXEC_OBJECT_NEEDS_MAP (1<<29)
41#define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
42#define __EXEC_OBJECT_INTERNAL_FLAGS (0xf<<28) /* all of the above */
Chris Wilsond23db882014-05-23 08:48:08 +020043
44#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000045
Chris Wilson5b043f42016-08-02 22:50:38 +010046struct i915_execbuffer_params {
47 struct drm_device *dev;
48 struct drm_file *file;
Chris Wilson59bfa122016-08-04 16:32:31 +010049 struct i915_vma *batch;
50 u32 dispatch_flags;
51 u32 args_batch_start_offset;
Chris Wilson5b043f42016-08-02 22:50:38 +010052 struct intel_engine_cs *engine;
Chris Wilson5b043f42016-08-02 22:50:38 +010053 struct i915_gem_context *ctx;
54 struct drm_i915_gem_request *request;
55};
56
Ben Widawsky27173f12013-08-14 11:38:36 +020057struct eb_vmas {
58 struct list_head vmas;
Chris Wilson67731b82010-12-08 10:38:14 +000059 int and;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000060 union {
Ben Widawsky27173f12013-08-14 11:38:36 +020061 struct i915_vma *lut[0];
Chris Wilsoneef90cc2013-01-08 10:53:17 +000062 struct hlist_head buckets[0];
63 };
Chris Wilson67731b82010-12-08 10:38:14 +000064};
65
Ben Widawsky27173f12013-08-14 11:38:36 +020066static struct eb_vmas *
Ben Widawsky17601cbc2013-11-25 09:54:38 -080067eb_create(struct drm_i915_gem_execbuffer2 *args)
Chris Wilson67731b82010-12-08 10:38:14 +000068{
Ben Widawsky27173f12013-08-14 11:38:36 +020069 struct eb_vmas *eb = NULL;
Chris Wilson67731b82010-12-08 10:38:14 +000070
Chris Wilsoneef90cc2013-01-08 10:53:17 +000071 if (args->flags & I915_EXEC_HANDLE_LUT) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020072 unsigned size = args->buffer_count;
Ben Widawsky27173f12013-08-14 11:38:36 +020073 size *= sizeof(struct i915_vma *);
74 size += sizeof(struct eb_vmas);
Chris Wilsoneef90cc2013-01-08 10:53:17 +000075 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
76 }
77
78 if (eb == NULL) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020079 unsigned size = args->buffer_count;
80 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Lauri Kasanen27b7c632013-03-27 15:04:55 +020081 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
Chris Wilsoneef90cc2013-01-08 10:53:17 +000082 while (count > 2*size)
83 count >>= 1;
84 eb = kzalloc(count*sizeof(struct hlist_head) +
Ben Widawsky27173f12013-08-14 11:38:36 +020085 sizeof(struct eb_vmas),
Chris Wilsoneef90cc2013-01-08 10:53:17 +000086 GFP_TEMPORARY);
87 if (eb == NULL)
88 return eb;
89
90 eb->and = count - 1;
91 } else
92 eb->and = -args->buffer_count;
93
Ben Widawsky27173f12013-08-14 11:38:36 +020094 INIT_LIST_HEAD(&eb->vmas);
Chris Wilson67731b82010-12-08 10:38:14 +000095 return eb;
96}
97
98static void
Ben Widawsky27173f12013-08-14 11:38:36 +020099eb_reset(struct eb_vmas *eb)
Chris Wilson67731b82010-12-08 10:38:14 +0000100{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000101 if (eb->and >= 0)
102 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
Chris Wilson67731b82010-12-08 10:38:14 +0000103}
104
Chris Wilson59bfa122016-08-04 16:32:31 +0100105static struct i915_vma *
106eb_get_batch(struct eb_vmas *eb)
107{
108 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
109
110 /*
111 * SNA is doing fancy tricks with compressing batch buffers, which leads
112 * to negative relocation deltas. Usually that works out ok since the
113 * relocate address is still positive, except when the batch is placed
114 * very low in the GTT. Ensure this doesn't happen.
115 *
116 * Note that actual hangs have only been observed on gen7, but for
117 * paranoia do it everywhere.
118 */
119 if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
120 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
121
122 return vma;
123}
124
Chris Wilson3b96eff2013-01-08 10:53:14 +0000125static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200126eb_lookup_vmas(struct eb_vmas *eb,
127 struct drm_i915_gem_exec_object2 *exec,
128 const struct drm_i915_gem_execbuffer2 *args,
129 struct i915_address_space *vm,
130 struct drm_file *file)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000131{
Ben Widawsky27173f12013-08-14 11:38:36 +0200132 struct drm_i915_gem_object *obj;
133 struct list_head objects;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000134 int i, ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000135
Ben Widawsky27173f12013-08-14 11:38:36 +0200136 INIT_LIST_HEAD(&objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000137 spin_lock(&file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +0200138 /* Grab a reference to the object and release the lock so we can lookup
139 * or create the VMA without using GFP_ATOMIC */
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000140 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000141 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
142 if (obj == NULL) {
143 spin_unlock(&file->table_lock);
144 DRM_DEBUG("Invalid object handle %d at index %d\n",
145 exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200146 ret = -ENOENT;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000147 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000148 }
149
Ben Widawsky27173f12013-08-14 11:38:36 +0200150 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000151 spin_unlock(&file->table_lock);
152 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
153 obj, exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200154 ret = -EINVAL;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000155 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000156 }
157
Chris Wilson25dc5562016-07-20 13:31:52 +0100158 i915_gem_object_get(obj);
Ben Widawsky27173f12013-08-14 11:38:36 +0200159 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000160 }
161 spin_unlock(&file->table_lock);
162
Ben Widawsky27173f12013-08-14 11:38:36 +0200163 i = 0;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000164 while (!list_empty(&objects)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200165 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800166
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000167 obj = list_first_entry(&objects,
168 struct drm_i915_gem_object,
169 obj_exec_link);
170
Daniel Vettere656a6c2013-08-14 14:14:04 +0200171 /*
172 * NOTE: We can leak any vmas created here when something fails
173 * later on. But that's no issue since vma_unbind can deal with
174 * vmas which are not actually bound. And since only
175 * lookup_or_create exists as an interface to get at the vma
176 * from the (obj, vm) we don't run the risk of creating
177 * duplicated vmas for the same vm.
178 */
Daniel Vetterda51a1e2014-08-11 12:08:58 +0200179 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200180 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200181 DRM_DEBUG("Failed to lookup VMA\n");
182 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000183 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200184 }
185
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000186 /* Transfer ownership from the objects list to the vmas list. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200187 list_add_tail(&vma->exec_list, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000188 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200189
190 vma->exec_entry = &exec[i];
191 if (eb->and < 0) {
192 eb->lut[i] = vma;
193 } else {
194 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
195 vma->exec_handle = handle;
196 hlist_add_head(&vma->exec_node,
197 &eb->buckets[handle & eb->and]);
198 }
199 ++i;
200 }
201
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000202 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200203
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000204
205err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200206 while (!list_empty(&objects)) {
207 obj = list_first_entry(&objects,
208 struct drm_i915_gem_object,
209 obj_exec_link);
210 list_del_init(&obj->obj_exec_link);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100211 i915_gem_object_put(obj);
Ben Widawsky27173f12013-08-14 11:38:36 +0200212 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000213 /*
214 * Objects already transfered to the vmas list will be unreferenced by
215 * eb_destroy.
216 */
217
Ben Widawsky27173f12013-08-14 11:38:36 +0200218 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000219}
220
Ben Widawsky27173f12013-08-14 11:38:36 +0200221static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000222{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000223 if (eb->and < 0) {
224 if (handle >= -eb->and)
225 return NULL;
226 return eb->lut[handle];
227 } else {
228 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800229 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000230
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000231 head = &eb->buckets[handle & eb->and];
Geliang Tangaa459502016-01-18 23:54:20 +0800232 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200233 if (vma->exec_handle == handle)
234 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000235 }
236 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000237 }
Chris Wilson67731b82010-12-08 10:38:14 +0000238}
239
Chris Wilsona415d352013-11-26 11:23:15 +0000240static void
241i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
242{
243 struct drm_i915_gem_exec_object2 *entry;
244 struct drm_i915_gem_object *obj = vma->obj;
245
246 if (!drm_mm_node_allocated(&vma->node))
247 return;
248
249 entry = vma->exec_entry;
250
251 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
252 i915_gem_object_unpin_fence(obj);
253
254 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100255 __i915_vma_unpin(vma);
Chris Wilsona415d352013-11-26 11:23:15 +0000256
Chris Wilsonde4e7832015-04-07 16:20:35 +0100257 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilsona415d352013-11-26 11:23:15 +0000258}
259
260static void eb_destroy(struct eb_vmas *eb)
261{
Ben Widawsky27173f12013-08-14 11:38:36 +0200262 while (!list_empty(&eb->vmas)) {
263 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000264
Ben Widawsky27173f12013-08-14 11:38:36 +0200265 vma = list_first_entry(&eb->vmas,
266 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000267 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200268 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000269 i915_gem_execbuffer_unreserve_vma(vma);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100270 i915_gem_object_put(vma->obj);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000271 }
Chris Wilson67731b82010-12-08 10:38:14 +0000272 kfree(eb);
273}
274
Chris Wilsondabdfe02012-03-26 10:10:27 +0200275static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
276{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300277 return (HAS_LLC(obj->base.dev) ||
278 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200279 obj->cache_level != I915_CACHE_NONE);
280}
281
Michał Winiarski934acce2015-12-29 18:24:52 +0100282/* Used to convert any address to canonical form.
283 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
284 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
285 * addresses to be in a canonical form:
286 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
287 * canonical form [63:48] == [47]."
288 */
289#define GEN8_HIGH_ADDRESS_BIT 47
290static inline uint64_t gen8_canonical_addr(uint64_t address)
291{
292 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
293}
294
295static inline uint64_t gen8_noncanonical_addr(uint64_t address)
296{
297 return address & ((1ULL << (GEN8_HIGH_ADDRESS_BIT + 1)) - 1);
298}
299
300static inline uint64_t
301relocation_target(struct drm_i915_gem_relocation_entry *reloc,
302 uint64_t target_offset)
303{
304 return gen8_canonical_addr((int)reloc->delta + target_offset);
305}
306
Chris Wilson54cf91d2010-11-25 18:00:26 +0000307static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100308relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700309 struct drm_i915_gem_relocation_entry *reloc,
310 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100311{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700312 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100313 uint32_t page_offset = offset_in_page(reloc->offset);
Michał Winiarski934acce2015-12-29 18:24:52 +0100314 uint64_t delta = relocation_target(reloc, target_offset);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100315 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800316 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100317
Chris Wilson2cc86b82013-08-26 19:51:00 -0300318 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100319 if (ret)
320 return ret;
321
Dave Gordon033908a2015-12-10 18:51:23 +0000322 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Rafael Barbalho5032d872013-08-21 17:10:51 +0100323 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700324 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700325
326 if (INTEL_INFO(dev)->gen >= 8) {
327 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
328
329 if (page_offset == 0) {
330 kunmap_atomic(vaddr);
Dave Gordon033908a2015-12-10 18:51:23 +0000331 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700332 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
333 }
334
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700335 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700336 }
337
Rafael Barbalho5032d872013-08-21 17:10:51 +0100338 kunmap_atomic(vaddr);
339
340 return 0;
341}
342
343static int
344relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700345 struct drm_i915_gem_relocation_entry *reloc,
346 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100347{
348 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300349 struct drm_i915_private *dev_priv = to_i915(dev);
350 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michał Winiarski934acce2015-12-29 18:24:52 +0100351 uint64_t delta = relocation_target(reloc, target_offset);
Chris Wilson906843c2014-08-10 06:29:11 +0100352 uint64_t offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100353 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800354 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100355
356 ret = i915_gem_object_set_to_gtt_domain(obj, true);
357 if (ret)
358 return ret;
359
360 ret = i915_gem_object_put_fence(obj);
361 if (ret)
362 return ret;
363
364 /* Map the page containing the relocation we're going to perform. */
Chris Wilson906843c2014-08-10 06:29:11 +0100365 offset = i915_gem_obj_ggtt_offset(obj);
366 offset += reloc->offset;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300367 reloc_page = io_mapping_map_atomic_wc(ggtt->mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100368 offset & PAGE_MASK);
369 iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700370
371 if (INTEL_INFO(dev)->gen >= 8) {
Chris Wilson906843c2014-08-10 06:29:11 +0100372 offset += sizeof(uint32_t);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700373
Chris Wilson906843c2014-08-10 06:29:11 +0100374 if (offset_in_page(offset) == 0) {
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700375 io_mapping_unmap_atomic(reloc_page);
Chris Wilson906843c2014-08-10 06:29:11 +0100376 reloc_page =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300377 io_mapping_map_atomic_wc(ggtt->mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100378 offset);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700379 }
380
Chris Wilson906843c2014-08-10 06:29:11 +0100381 iowrite32(upper_32_bits(delta),
382 reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700383 }
384
Rafael Barbalho5032d872013-08-21 17:10:51 +0100385 io_mapping_unmap_atomic(reloc_page);
386
387 return 0;
388}
389
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000390static void
391clflush_write32(void *addr, uint32_t value)
392{
393 /* This is not a fast path, so KISS. */
394 drm_clflush_virt_range(addr, sizeof(uint32_t));
395 *(uint32_t *)addr = value;
396 drm_clflush_virt_range(addr, sizeof(uint32_t));
397}
398
399static int
400relocate_entry_clflush(struct drm_i915_gem_object *obj,
401 struct drm_i915_gem_relocation_entry *reloc,
402 uint64_t target_offset)
403{
404 struct drm_device *dev = obj->base.dev;
405 uint32_t page_offset = offset_in_page(reloc->offset);
Michał Winiarski934acce2015-12-29 18:24:52 +0100406 uint64_t delta = relocation_target(reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000407 char *vaddr;
408 int ret;
409
410 ret = i915_gem_object_set_to_gtt_domain(obj, true);
411 if (ret)
412 return ret;
413
Dave Gordon033908a2015-12-10 18:51:23 +0000414 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000415 reloc->offset >> PAGE_SHIFT));
416 clflush_write32(vaddr + page_offset, lower_32_bits(delta));
417
418 if (INTEL_INFO(dev)->gen >= 8) {
419 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
420
421 if (page_offset == 0) {
422 kunmap_atomic(vaddr);
Dave Gordon033908a2015-12-10 18:51:23 +0000423 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000424 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
425 }
426
427 clflush_write32(vaddr + page_offset, upper_32_bits(delta));
428 }
429
430 kunmap_atomic(vaddr);
431
432 return 0;
433}
434
Chris Wilson909d0742016-08-04 07:52:41 +0100435static bool object_is_idle(struct drm_i915_gem_object *obj)
436{
437 unsigned long active = obj->active;
438 int idx;
439
440 for_each_active(active, idx) {
441 if (!i915_gem_active_is_idle(&obj->last_read[idx],
442 &obj->base.dev->struct_mutex))
443 return false;
444 }
445
446 return true;
447}
448
Rafael Barbalho5032d872013-08-21 17:10:51 +0100449static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000450i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200451 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800452 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000453{
454 struct drm_device *dev = obj->base.dev;
455 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100456 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200457 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700458 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800459 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000460
Chris Wilson67731b82010-12-08 10:38:14 +0000461 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200462 target_vma = eb_get_vma(eb, reloc->target_handle);
463 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000464 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200465 target_i915_obj = target_vma->obj;
466 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000467
Michał Winiarski934acce2015-12-29 18:24:52 +0100468 target_offset = gen8_canonical_addr(target_vma->node.start);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000469
Eric Anholte844b992012-07-31 15:35:01 -0700470 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
471 * pipe_control writes because the gpu doesn't properly redirect them
472 * through the ppgtt for non_secure batchbuffers. */
473 if (unlikely(IS_GEN6(dev) &&
Daniel Vetter08755462015-04-20 09:04:05 -0700474 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000475 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
Daniel Vetter08755462015-04-20 09:04:05 -0700476 PIN_GLOBAL);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000477 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
478 return ret;
479 }
Eric Anholte844b992012-07-31 15:35:01 -0700480
Chris Wilson54cf91d2010-11-25 18:00:26 +0000481 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000482 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100483 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000484 "obj %p target %d offset %d "
485 "read %08x write %08x",
486 obj, reloc->target_handle,
487 (int) reloc->offset,
488 reloc->read_domains,
489 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800490 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000491 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100492 if (unlikely((reloc->write_domain | reloc->read_domains)
493 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100494 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000495 "obj %p target %d offset %d "
496 "read %08x write %08x",
497 obj, reloc->target_handle,
498 (int) reloc->offset,
499 reloc->read_domains,
500 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800501 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000502 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000503
504 target_obj->pending_read_domains |= reloc->read_domains;
505 target_obj->pending_write_domain |= reloc->write_domain;
506
507 /* If the relocation already has the right value in it, no
508 * more work needs to be done.
509 */
510 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000511 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000512
513 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700514 if (unlikely(reloc->offset >
515 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100516 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000517 "obj %p target %d offset %d size %d.\n",
518 obj, reloc->target_handle,
519 (int) reloc->offset,
520 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800521 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000522 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000523 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100524 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000525 "obj %p target %d offset %d.\n",
526 obj, reloc->target_handle,
527 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800528 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000529 }
530
Chris Wilsondabdfe02012-03-26 10:10:27 +0200531 /* We can't wait for rendering with pagefaults disabled */
Chris Wilson909d0742016-08-04 07:52:41 +0100532 if (pagefault_disabled() && !object_is_idle(obj))
Chris Wilsondabdfe02012-03-26 10:10:27 +0200533 return -EFAULT;
534
Rafael Barbalho5032d872013-08-21 17:10:51 +0100535 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700536 ret = relocate_entry_cpu(obj, reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000537 else if (obj->map_and_fenceable)
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700538 ret = relocate_entry_gtt(obj, reloc, target_offset);
Borislav Petkov906bf7f2016-03-29 17:41:59 +0200539 else if (static_cpu_has(X86_FEATURE_CLFLUSH))
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000540 ret = relocate_entry_clflush(obj, reloc, target_offset);
541 else {
542 WARN_ONCE(1, "Impossible case in relocation handling\n");
543 ret = -ENODEV;
544 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000545
Daniel Vetterd4d36012013-09-02 20:56:23 +0200546 if (ret)
547 return ret;
548
Chris Wilson54cf91d2010-11-25 18:00:26 +0000549 /* and update the user's relocation entry */
550 reloc->presumed_offset = target_offset;
551
Chris Wilson67731b82010-12-08 10:38:14 +0000552 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000553}
554
555static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200556i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
557 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000558{
Chris Wilson1d83f442012-03-24 20:12:53 +0000559#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
560 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000561 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200562 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000563 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000564
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300565 user_relocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000566
Chris Wilson1d83f442012-03-24 20:12:53 +0000567 remain = entry->relocation_count;
568 while (remain) {
569 struct drm_i915_gem_relocation_entry *r = stack_reloc;
570 int count = remain;
571 if (count > ARRAY_SIZE(stack_reloc))
572 count = ARRAY_SIZE(stack_reloc);
573 remain -= count;
574
575 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000576 return -EFAULT;
577
Chris Wilson1d83f442012-03-24 20:12:53 +0000578 do {
579 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000580
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800581 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000582 if (ret)
583 return ret;
584
585 if (r->presumed_offset != offset &&
Linus Torvalds5b09c3e2016-05-22 14:19:37 -0700586 __put_user(r->presumed_offset, &user_relocs->presumed_offset)) {
Chris Wilson1d83f442012-03-24 20:12:53 +0000587 return -EFAULT;
588 }
589
590 user_relocs++;
591 r++;
592 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000593 }
594
595 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000596#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000597}
598
599static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200600i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
601 struct eb_vmas *eb,
602 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000603{
Ben Widawsky27173f12013-08-14 11:38:36 +0200604 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000605 int i, ret;
606
607 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800608 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000609 if (ret)
610 return ret;
611 }
612
613 return 0;
614}
615
616static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800617i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000618{
Ben Widawsky27173f12013-08-14 11:38:36 +0200619 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000620 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000621
Chris Wilsond4aeee72011-03-14 15:11:24 +0000622 /* This is the fast path and we cannot handle a pagefault whilst
623 * holding the struct mutex lest the user pass in the relocations
624 * contained within a mmaped bo. For in such a case we, the page
625 * fault handler would call i915_gem_fault() and we would try to
626 * acquire the struct mutex again. Obviously this is bad and so
627 * lockdep complains vehemently.
628 */
629 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200630 list_for_each_entry(vma, &eb->vmas, exec_list) {
631 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000632 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000633 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000634 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000635 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000636
Chris Wilsond4aeee72011-03-14 15:11:24 +0000637 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000638}
639
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000640static bool only_mappable_for_reloc(unsigned int flags)
641{
642 return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
643 __EXEC_OBJECT_NEEDS_MAP;
644}
645
Chris Wilson1690e1e2011-12-14 13:57:08 +0100646static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200647i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000648 struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200649 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100650{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800651 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200652 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200653 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100654 int ret;
655
Daniel Vetter08755462015-04-20 09:04:05 -0700656 flags = PIN_USER;
Daniel Vetter0229da32015-04-14 19:01:54 +0200657 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
658 flags |= PIN_GLOBAL;
659
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000660 if (!drm_mm_node_allocated(&vma->node)) {
Michel Thierry101b5062015-10-01 13:33:57 +0100661 /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
662 * limit address to the first 4GBs for unflagged objects.
663 */
664 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
665 flags |= PIN_ZONE_4G;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000666 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
667 flags |= PIN_GLOBAL | PIN_MAPPABLE;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000668 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
669 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson506a8e82015-12-08 11:55:07 +0000670 if (entry->flags & EXEC_OBJECT_PINNED)
671 flags |= entry->offset | PIN_OFFSET_FIXED;
Michel Thierry101b5062015-10-01 13:33:57 +0100672 if ((flags & PIN_MAPPABLE) == 0)
673 flags |= PIN_HIGH;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000674 }
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100675
Chris Wilson59bfa122016-08-04 16:32:31 +0100676 ret = i915_vma_pin(vma,
677 entry->pad_to_size,
678 entry->alignment,
679 flags);
680 if ((ret == -ENOSPC || ret == -E2BIG) &&
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000681 only_mappable_for_reloc(entry->flags))
Chris Wilson59bfa122016-08-04 16:32:31 +0100682 ret = i915_vma_pin(vma,
683 entry->pad_to_size,
684 entry->alignment,
685 flags & ~PIN_MAPPABLE);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100686 if (ret)
687 return ret;
688
Chris Wilson7788a762012-08-24 19:18:18 +0100689 entry->flags |= __EXEC_OBJECT_HAS_PIN;
690
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100691 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
692 ret = i915_gem_object_get_fence(obj);
693 if (ret)
694 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100695
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100696 if (i915_gem_object_pin_fence(obj))
697 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100698 }
699
Ben Widawsky27173f12013-08-14 11:38:36 +0200700 if (entry->offset != vma->node.start) {
701 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100702 *need_reloc = true;
703 }
704
705 if (entry->flags & EXEC_OBJECT_WRITE) {
706 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
707 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
708 }
709
Chris Wilson1690e1e2011-12-14 13:57:08 +0100710 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100711}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100712
Chris Wilsond23db882014-05-23 08:48:08 +0200713static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200714need_reloc_mappable(struct i915_vma *vma)
715{
716 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
717
718 if (entry->relocation_count == 0)
719 return false;
720
Chris Wilson3272db52016-08-04 16:32:32 +0100721 if (!i915_vma_is_ggtt(vma))
Chris Wilsone6a84462014-08-11 12:00:12 +0200722 return false;
723
724 /* See also use_cpu_reloc() */
725 if (HAS_LLC(vma->obj->base.dev))
726 return false;
727
728 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
729 return false;
730
731 return true;
732}
733
734static bool
735eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200736{
737 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
738 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond23db882014-05-23 08:48:08 +0200739
Chris Wilson3272db52016-08-04 16:32:32 +0100740 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
741 !i915_vma_is_ggtt(vma));
Chris Wilsond23db882014-05-23 08:48:08 +0200742
743 if (entry->alignment &&
744 vma->node.start & (entry->alignment - 1))
745 return true;
746
Chris Wilson91b2db62016-08-04 16:32:23 +0100747 if (vma->node.size < entry->pad_to_size)
748 return true;
749
Chris Wilson506a8e82015-12-08 11:55:07 +0000750 if (entry->flags & EXEC_OBJECT_PINNED &&
751 vma->node.start != entry->offset)
752 return true;
753
Chris Wilsond23db882014-05-23 08:48:08 +0200754 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
755 vma->node.start < BATCH_OFFSET_BIAS)
756 return true;
757
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000758 /* avoid costly ping-pong once a batch bo ended up non-mappable */
759 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
760 return !only_mappable_for_reloc(entry->flags);
761
Michel Thierry101b5062015-10-01 13:33:57 +0100762 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
763 (vma->node.start + vma->node.size - 1) >> 32)
764 return true;
765
Chris Wilsond23db882014-05-23 08:48:08 +0200766 return false;
767}
768
Chris Wilson54cf91d2010-11-25 18:00:26 +0000769static int
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000770i915_gem_execbuffer_reserve(struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200771 struct list_head *vmas,
Chris Wilsone2efd132016-05-24 14:53:34 +0100772 struct i915_gem_context *ctx,
Daniel Vettered5982e2013-01-17 22:23:36 +0100773 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000774{
Chris Wilson432e58e2010-11-25 19:32:06 +0000775 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200776 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700777 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200778 struct list_head ordered_vmas;
Chris Wilson506a8e82015-12-08 11:55:07 +0000779 struct list_head pinned_vmas;
Chris Wilsonc0336662016-05-06 15:40:21 +0100780 bool has_fenced_gpu_access = INTEL_GEN(engine->i915) < 4;
Chris Wilson7788a762012-08-24 19:18:18 +0100781 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000782
Ben Widawsky68c8c172013-09-11 14:57:50 -0700783 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
784
Ben Widawsky27173f12013-08-14 11:38:36 +0200785 INIT_LIST_HEAD(&ordered_vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000786 INIT_LIST_HEAD(&pinned_vmas);
Ben Widawsky27173f12013-08-14 11:38:36 +0200787 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000788 struct drm_i915_gem_exec_object2 *entry;
789 bool need_fence, need_mappable;
790
Ben Widawsky27173f12013-08-14 11:38:36 +0200791 vma = list_first_entry(vmas, struct i915_vma, exec_list);
792 obj = vma->obj;
793 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000794
David Weinehallb1b38272015-05-20 17:00:13 +0300795 if (ctx->flags & CONTEXT_NO_ZEROMAP)
796 entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
797
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100798 if (!has_fenced_gpu_access)
799 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000800 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000801 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
802 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200803 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000804
Chris Wilson506a8e82015-12-08 11:55:07 +0000805 if (entry->flags & EXEC_OBJECT_PINNED)
806 list_move_tail(&vma->exec_list, &pinned_vmas);
807 else if (need_mappable) {
Chris Wilsone6a84462014-08-11 12:00:12 +0200808 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200809 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200810 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200811 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000812
Daniel Vettered5982e2013-01-17 22:23:36 +0100813 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000814 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000815 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200816 list_splice(&ordered_vmas, vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000817 list_splice(&pinned_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000818
819 /* Attempt to pin all of the buffers into the GTT.
820 * This is done in 3 phases:
821 *
822 * 1a. Unbind all objects that do not match the GTT constraints for
823 * the execbuffer (fenceable, mappable, alignment etc).
824 * 1b. Increment pin count for already bound objects.
825 * 2. Bind new objects.
826 * 3. Decrement pin count.
827 *
Chris Wilson7788a762012-08-24 19:18:18 +0100828 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000829 * room for the earlier objects *unless* we need to defragment.
830 */
831 retry = 0;
832 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100833 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000834
835 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200836 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200837 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000838 continue;
839
Chris Wilsone6a84462014-08-11 12:00:12 +0200840 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200841 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000842 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000843 ret = i915_gem_execbuffer_reserve_vma(vma,
844 engine,
845 need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000846 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000847 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000848 }
849
850 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200851 list_for_each_entry(vma, vmas, exec_list) {
852 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100853 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000854
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000855 ret = i915_gem_execbuffer_reserve_vma(vma, engine,
856 need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100857 if (ret)
858 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000859 }
860
Chris Wilsona415d352013-11-26 11:23:15 +0000861err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200862 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000863 return ret;
864
Chris Wilsona415d352013-11-26 11:23:15 +0000865 /* Decrement pin count for bound objects */
866 list_for_each_entry(vma, vmas, exec_list)
867 i915_gem_execbuffer_unreserve_vma(vma);
868
Ben Widawsky68c8c172013-09-11 14:57:50 -0700869 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000870 if (ret)
871 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000872 } while (1);
873}
874
875static int
876i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100877 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000878 struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000879 struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200880 struct eb_vmas *eb,
David Weinehallb1b38272015-05-20 17:00:13 +0300881 struct drm_i915_gem_exec_object2 *exec,
Chris Wilsone2efd132016-05-24 14:53:34 +0100882 struct i915_gem_context *ctx)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000883{
884 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200885 struct i915_address_space *vm;
886 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100887 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000888 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000889 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200890 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000891
Ben Widawsky27173f12013-08-14 11:38:36 +0200892 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
893
Chris Wilson67731b82010-12-08 10:38:14 +0000894 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200895 while (!list_empty(&eb->vmas)) {
896 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
897 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000898 i915_gem_execbuffer_unreserve_vma(vma);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100899 i915_gem_object_put(vma->obj);
Chris Wilson67731b82010-12-08 10:38:14 +0000900 }
901
Chris Wilson54cf91d2010-11-25 18:00:26 +0000902 mutex_unlock(&dev->struct_mutex);
903
904 total = 0;
905 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000906 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000907
Chris Wilsondd6864a2011-01-12 23:49:13 +0000908 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000909 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000910 if (reloc == NULL || reloc_offset == NULL) {
911 drm_free_large(reloc);
912 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000913 mutex_lock(&dev->struct_mutex);
914 return -ENOMEM;
915 }
916
917 total = 0;
918 for (i = 0; i < count; i++) {
919 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000920 u64 invalid_offset = (u64)-1;
921 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000922
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300923 user_relocs = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000924
925 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000926 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000927 ret = -EFAULT;
928 mutex_lock(&dev->struct_mutex);
929 goto err;
930 }
931
Chris Wilson262b6d32013-01-15 16:17:54 +0000932 /* As we do not update the known relocation offsets after
933 * relocating (due to the complexities in lock handling),
934 * we need to mark them as invalid now so that we force the
935 * relocation processing next time. Just in case the target
936 * object is evicted and then rebound into its old
937 * presumed_offset before the next execbuffer - if that
938 * happened we would make the mistake of assuming that the
939 * relocations were valid.
940 */
941 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100942 if (__copy_to_user(&user_relocs[j].presumed_offset,
943 &invalid_offset,
944 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000945 ret = -EFAULT;
946 mutex_lock(&dev->struct_mutex);
947 goto err;
948 }
949 }
950
Chris Wilsondd6864a2011-01-12 23:49:13 +0000951 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000952 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000953 }
954
955 ret = i915_mutex_lock_interruptible(dev);
956 if (ret) {
957 mutex_lock(&dev->struct_mutex);
958 goto err;
959 }
960
Chris Wilson67731b82010-12-08 10:38:14 +0000961 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000962 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200963 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000964 if (ret)
965 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000966
Daniel Vettered5982e2013-01-17 22:23:36 +0100967 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000968 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
969 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000970 if (ret)
971 goto err;
972
Ben Widawsky27173f12013-08-14 11:38:36 +0200973 list_for_each_entry(vma, &eb->vmas, exec_list) {
974 int offset = vma->exec_entry - exec;
975 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
976 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000977 if (ret)
978 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000979 }
980
981 /* Leave the user relocations as are, this is the painfully slow path,
982 * and we want to avoid the complication of dropping the lock whilst
983 * having buffers reserved in the aperture and so causing spurious
984 * ENOSPC for random operations.
985 */
986
987err:
988 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000989 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000990 return ret;
991}
992
Chris Wilson54cf91d2010-11-25 18:00:26 +0000993static int
John Harrison535fbe82015-05-29 17:43:32 +0100994i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
Ben Widawsky27173f12013-08-14 11:38:36 +0200995 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000996{
Tvrtko Ursulin666796d2016-03-16 11:00:39 +0000997 const unsigned other_rings = ~intel_engine_flag(req->engine);
Ben Widawsky27173f12013-08-14 11:38:36 +0200998 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200999 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +01001000 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +00001001 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001002
Ben Widawsky27173f12013-08-14 11:38:36 +02001003 list_for_each_entry(vma, vmas, exec_list) {
1004 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001005
1006 if (obj->active & other_rings) {
Chris Wilson8e637172016-08-02 22:50:26 +01001007 ret = i915_gem_object_sync(obj, req);
Chris Wilson03ade512015-04-27 13:41:18 +01001008 if (ret)
1009 return ret;
1010 }
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001011
1012 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +01001013 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001014
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001015 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001016 }
1017
Chris Wilson000433b2013-08-08 14:41:09 +01001018 if (flush_chipset)
Chris Wilsonc0336662016-05-06 15:40:21 +01001019 i915_gem_chipset_flush(req->engine->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001020
1021 if (flush_domains & I915_GEM_DOMAIN_GTT)
1022 wmb();
1023
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001024 /* Unconditionally invalidate GPU caches and TLBs. */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001025 return req->engine->emit_flush(req, EMIT_INVALIDATE);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001026}
1027
Chris Wilson432e58e2010-11-25 19:32:06 +00001028static bool
1029i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001030{
Daniel Vettered5982e2013-01-17 22:23:36 +01001031 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
1032 return false;
1033
Chris Wilson2f5945b2015-10-06 11:39:55 +01001034 /* Kernel clipping was a DRI1 misfeature */
1035 if (exec->num_cliprects || exec->cliprects_ptr)
1036 return false;
1037
1038 if (exec->DR4 == 0xffffffff) {
1039 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1040 exec->DR4 = 0;
1041 }
1042 if (exec->DR1 || exec->DR4)
1043 return false;
1044
1045 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1046 return false;
1047
1048 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001049}
1050
1051static int
Chris Wilsonad19f102014-08-10 06:29:08 +01001052validate_exec_list(struct drm_device *dev,
1053 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001054 int count)
1055{
Daniel Vetterb205ca52013-09-19 14:00:11 +02001056 unsigned relocs_total = 0;
1057 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +01001058 unsigned invalid_flags;
1059 int i;
1060
Dave Gordon9e2793f62016-07-14 14:52:03 +01001061 /* INTERNAL flags must not overlap with external ones */
1062 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
1063
Chris Wilsonad19f102014-08-10 06:29:08 +01001064 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
1065 if (USES_FULL_PPGTT(dev))
1066 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001067
1068 for (i = 0; i < count; i++) {
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001069 char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001070 int length; /* limited by fault_in_pages_readable() */
1071
Chris Wilsonad19f102014-08-10 06:29:08 +01001072 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +01001073 return -EINVAL;
1074
Michał Winiarski934acce2015-12-29 18:24:52 +01001075 /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
1076 * any non-page-aligned or non-canonical addresses.
1077 */
1078 if (exec[i].flags & EXEC_OBJECT_PINNED) {
1079 if (exec[i].offset !=
1080 gen8_canonical_addr(exec[i].offset & PAGE_MASK))
1081 return -EINVAL;
1082
1083 /* From drm_mm perspective address space is continuous,
1084 * so from this point we're always using non-canonical
1085 * form internally.
1086 */
1087 exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
1088 }
1089
Chris Wilson55a97852015-06-19 13:59:46 +01001090 if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
1091 return -EINVAL;
1092
Chris Wilson91b2db62016-08-04 16:32:23 +01001093 /* pad_to_size was once a reserved field, so sanitize it */
1094 if (exec[i].flags & EXEC_OBJECT_PAD_TO_SIZE) {
1095 if (offset_in_page(exec[i].pad_to_size))
1096 return -EINVAL;
1097 } else {
1098 exec[i].pad_to_size = 0;
1099 }
1100
Kees Cook3118a4f2013-03-11 17:31:45 -07001101 /* First check for malicious input causing overflow in
1102 * the worst case where we need to allocate the entire
1103 * relocation tree as a single array.
1104 */
1105 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001106 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -07001107 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001108
1109 length = exec[i].relocation_count *
1110 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -07001111 /*
1112 * We must check that the entire relocation array is safe
1113 * to read, but since we may need to update the presumed
1114 * offsets during execution, check for full write access.
1115 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001116 if (!access_ok(VERIFY_WRITE, ptr, length))
1117 return -EFAULT;
1118
Jani Nikulad330a952014-01-21 11:24:25 +02001119 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +08001120 if (fault_in_multipages_readable(ptr, length))
1121 return -EFAULT;
1122 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001123 }
1124
1125 return 0;
1126}
1127
Chris Wilsone2efd132016-05-24 14:53:34 +01001128static struct i915_gem_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001129i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001130 struct intel_engine_cs *engine, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001131{
Chris Wilsone2efd132016-05-24 14:53:34 +01001132 struct i915_gem_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001133 struct i915_ctx_hang_stats *hs;
1134
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001135 if (engine->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001136 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001137
Chris Wilsonca585b52016-05-24 14:53:36 +01001138 ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001139 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -08001140 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001141
Ben Widawsky41bde552013-12-06 14:11:21 -08001142 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001143 if (hs->banned) {
1144 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -08001145 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001146 }
1147
Ben Widawsky41bde552013-12-06 14:11:21 -08001148 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001149}
1150
Chris Wilson5cf3d282016-08-04 07:52:43 +01001151void i915_vma_move_to_active(struct i915_vma *vma,
1152 struct drm_i915_gem_request *req,
1153 unsigned int flags)
1154{
1155 struct drm_i915_gem_object *obj = vma->obj;
1156 const unsigned int idx = req->engine->id;
1157
1158 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1159
1160 obj->dirty = 1; /* be paranoid */
1161
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001162 /* Add a reference if we're newly entering the active list.
1163 * The order in which we add operations to the retirement queue is
1164 * vital here: mark_active adds to the start of the callback list,
1165 * such that subsequent callbacks are called first. Therefore we
1166 * add the active reference first and queue for it to be dropped
1167 * *last*.
1168 */
Chris Wilson5cf3d282016-08-04 07:52:43 +01001169 if (obj->active == 0)
1170 i915_gem_object_get(obj);
1171 obj->active |= 1 << idx;
1172 i915_gem_active_set(&obj->last_read[idx], req);
1173
1174 if (flags & EXEC_OBJECT_WRITE) {
1175 i915_gem_active_set(&obj->last_write, req);
1176
1177 intel_fb_obj_invalidate(obj, ORIGIN_CS);
1178
1179 /* update for the implicit flush after a batch */
1180 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1181 }
1182
1183 if (flags & EXEC_OBJECT_NEEDS_FENCE) {
1184 i915_gem_active_set(&obj->last_fence, req);
1185 if (flags & __EXEC_OBJECT_HAS_FENCE) {
1186 struct drm_i915_private *dev_priv = req->i915;
1187
1188 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
1189 &dev_priv->mm.fence_list);
1190 }
1191 }
1192
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001193 i915_vma_set_active(vma, idx);
1194 i915_gem_active_set(&vma->last_read[idx], req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001195 list_move_tail(&vma->vm_link, &vma->vm->active_list);
1196}
1197
Chris Wilson5b043f42016-08-02 22:50:38 +01001198static void
Ben Widawsky27173f12013-08-14 11:38:36 +02001199i915_gem_execbuffer_move_to_active(struct list_head *vmas,
John Harrison8a8edb52015-05-29 17:43:33 +01001200 struct drm_i915_gem_request *req)
Chris Wilson432e58e2010-11-25 19:32:06 +00001201{
Ben Widawsky27173f12013-08-14 11:38:36 +02001202 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +00001203
Ben Widawsky27173f12013-08-14 11:38:36 +02001204 list_for_each_entry(vma, vmas, exec_list) {
1205 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +01001206 u32 old_read = obj->base.read_domains;
1207 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +00001208
Chris Wilson432e58e2010-11-25 19:32:06 +00001209 obj->base.write_domain = obj->base.pending_write_domain;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001210 if (obj->base.write_domain)
1211 vma->exec_entry->flags |= EXEC_OBJECT_WRITE;
1212 else
Daniel Vettered5982e2013-01-17 22:23:36 +01001213 obj->base.pending_read_domains |= obj->base.read_domains;
1214 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +00001215
Chris Wilson5cf3d282016-08-04 07:52:43 +01001216 i915_vma_move_to_active(vma, req, vma->exec_entry->flags);
Chris Wilsondb53a302011-02-03 11:57:46 +00001217 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +00001218 }
1219}
1220
Chris Wilson54cf91d2010-11-25 18:00:26 +00001221static int
Chris Wilsonb5321f32016-08-02 22:50:18 +01001222i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001223{
Chris Wilson7e37f882016-08-02 22:50:21 +01001224 struct intel_ring *ring = req->ring;
Eric Anholtae662d32012-01-03 09:23:29 -08001225 int ret, i;
1226
Chris Wilsonb5321f32016-08-02 22:50:18 +01001227 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001228 DRM_DEBUG("sol reset is gen7/rcs only\n");
1229 return -EINVAL;
1230 }
Eric Anholtae662d32012-01-03 09:23:29 -08001231
John Harrison5fb9de12015-05-29 17:44:07 +01001232 ret = intel_ring_begin(req, 4 * 3);
Eric Anholtae662d32012-01-03 09:23:29 -08001233 if (ret)
1234 return ret;
1235
1236 for (i = 0; i < 4; i++) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001237 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1238 intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
1239 intel_ring_emit(ring, 0);
Eric Anholtae662d32012-01-03 09:23:29 -08001240 }
1241
Chris Wilsonb5321f32016-08-02 22:50:18 +01001242 intel_ring_advance(ring);
Eric Anholtae662d32012-01-03 09:23:29 -08001243
1244 return 0;
1245}
1246
Chris Wilson59bfa122016-08-04 16:32:31 +01001247static struct i915_vma*
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001248i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
Brad Volkin71745372014-12-11 12:13:12 -08001249 struct drm_i915_gem_exec_object2 *shadow_exec_entry,
Brad Volkin71745372014-12-11 12:13:12 -08001250 struct drm_i915_gem_object *batch_obj,
Chris Wilson59bfa122016-08-04 16:32:31 +01001251 struct eb_vmas *eb,
Brad Volkin71745372014-12-11 12:13:12 -08001252 u32 batch_start_offset,
1253 u32 batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001254 bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001255{
Brad Volkin71745372014-12-11 12:13:12 -08001256 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001257 struct i915_vma *vma;
Brad Volkin71745372014-12-11 12:13:12 -08001258 int ret;
1259
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001260 shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
Chris Wilson17cabf52015-01-14 11:20:57 +00001261 PAGE_ALIGN(batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001262 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001263 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001264
Chris Wilson33a051a2016-07-27 09:07:26 +01001265 ret = intel_engine_cmd_parser(engine,
1266 batch_obj,
1267 shadow_batch_obj,
1268 batch_start_offset,
1269 batch_len,
1270 is_master);
Chris Wilson17cabf52015-01-14 11:20:57 +00001271 if (ret)
1272 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001273
Chris Wilsonde895082016-08-04 16:32:34 +01001274 ret = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
Chris Wilson17cabf52015-01-14 11:20:57 +00001275 if (ret)
1276 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001277
Chris Wilsonde4e7832015-04-07 16:20:35 +01001278 i915_gem_object_unpin_pages(shadow_batch_obj);
1279
Chris Wilson17cabf52015-01-14 11:20:57 +00001280 memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
Brad Volkin71745372014-12-11 12:13:12 -08001281
Chris Wilson17cabf52015-01-14 11:20:57 +00001282 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1283 vma->exec_entry = shadow_exec_entry;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001284 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
Chris Wilson25dc5562016-07-20 13:31:52 +01001285 i915_gem_object_get(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001286 list_add_tail(&vma->exec_list, &eb->vmas);
Brad Volkin71745372014-12-11 12:13:12 -08001287
Chris Wilson59bfa122016-08-04 16:32:31 +01001288 return vma;
Chris Wilson17cabf52015-01-14 11:20:57 +00001289
1290err:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001291 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001292 if (ret == -EACCES) /* unhandled chained batch */
Chris Wilson59bfa122016-08-04 16:32:31 +01001293 return NULL;
Chris Wilson17cabf52015-01-14 11:20:57 +00001294 else
1295 return ERR_PTR(ret);
Brad Volkin71745372014-12-11 12:13:12 -08001296}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001297
Chris Wilson5b043f42016-08-02 22:50:38 +01001298static int
1299execbuf_submit(struct i915_execbuffer_params *params,
1300 struct drm_i915_gem_execbuffer2 *args,
1301 struct list_head *vmas)
Oscar Mateo78382592014-07-03 16:28:05 +01001302{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001303 struct drm_i915_private *dev_priv = params->request->i915;
John Harrison5f19e2b2015-05-29 17:43:27 +01001304 u64 exec_start, exec_len;
Oscar Mateo78382592014-07-03 16:28:05 +01001305 int instp_mode;
1306 u32 instp_mask;
Chris Wilson2f5945b2015-10-06 11:39:55 +01001307 int ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001308
John Harrison535fbe82015-05-29 17:43:32 +01001309 ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
Oscar Mateo78382592014-07-03 16:28:05 +01001310 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001311 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001312
John Harrisonba01cc92015-05-29 17:43:41 +01001313 ret = i915_switch_context(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001314 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001315 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001316
1317 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1318 instp_mask = I915_EXEC_CONSTANTS_MASK;
1319 switch (instp_mode) {
1320 case I915_EXEC_CONSTANTS_REL_GENERAL:
1321 case I915_EXEC_CONSTANTS_ABSOLUTE:
1322 case I915_EXEC_CONSTANTS_REL_SURFACE:
Chris Wilsonb5321f32016-08-02 22:50:18 +01001323 if (instp_mode != 0 && params->engine->id != RCS) {
Oscar Mateo78382592014-07-03 16:28:05 +01001324 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001325 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001326 }
1327
1328 if (instp_mode != dev_priv->relative_constants_mode) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001329 if (INTEL_INFO(dev_priv)->gen < 4) {
Oscar Mateo78382592014-07-03 16:28:05 +01001330 DRM_DEBUG("no rel constants on pre-gen4\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001331 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001332 }
1333
Chris Wilsonb5321f32016-08-02 22:50:18 +01001334 if (INTEL_INFO(dev_priv)->gen > 5 &&
Oscar Mateo78382592014-07-03 16:28:05 +01001335 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1336 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001337 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001338 }
1339
1340 /* The HW changed the meaning on this bit on gen6 */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001341 if (INTEL_INFO(dev_priv)->gen >= 6)
Oscar Mateo78382592014-07-03 16:28:05 +01001342 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1343 }
1344 break;
1345 default:
1346 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001347 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001348 }
1349
Chris Wilsonb5321f32016-08-02 22:50:18 +01001350 if (params->engine->id == RCS &&
Chris Wilson2f5945b2015-10-06 11:39:55 +01001351 instp_mode != dev_priv->relative_constants_mode) {
Chris Wilson7e37f882016-08-02 22:50:21 +01001352 struct intel_ring *ring = params->request->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01001353
John Harrison5fb9de12015-05-29 17:44:07 +01001354 ret = intel_ring_begin(params->request, 4);
Oscar Mateo78382592014-07-03 16:28:05 +01001355 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001356 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001357
Chris Wilsonb5321f32016-08-02 22:50:18 +01001358 intel_ring_emit(ring, MI_NOOP);
1359 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1360 intel_ring_emit_reg(ring, INSTPM);
1361 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1362 intel_ring_advance(ring);
Oscar Mateo78382592014-07-03 16:28:05 +01001363
1364 dev_priv->relative_constants_mode = instp_mode;
1365 }
1366
1367 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001368 ret = i915_reset_gen7_sol_offsets(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001369 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001370 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001371 }
1372
John Harrison5f19e2b2015-05-29 17:43:27 +01001373 exec_len = args->batch_len;
Chris Wilson59bfa122016-08-04 16:32:31 +01001374 exec_start = params->batch->node.start +
John Harrison5f19e2b2015-05-29 17:43:27 +01001375 params->args_batch_start_offset;
1376
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001377 if (exec_len == 0)
Chris Wilson59bfa122016-08-04 16:32:31 +01001378 exec_len = params->batch->size;
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001379
Chris Wilson803688b2016-08-02 22:50:27 +01001380 ret = params->engine->emit_bb_start(params->request,
1381 exec_start, exec_len,
1382 params->dispatch_flags);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001383 if (ret)
1384 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001385
John Harrison95c24162015-05-29 17:43:31 +01001386 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001387
John Harrison8a8edb52015-05-29 17:43:33 +01001388 i915_gem_execbuffer_move_to_active(vmas, params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001389
Chris Wilson2f5945b2015-10-06 11:39:55 +01001390 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001391}
1392
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001393/**
1394 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01001395 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001396 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001397static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01001398gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
1399 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001400{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001401 struct drm_i915_file_private *file_priv = file->driver_priv;
1402
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001403 /* Check whether the file_priv has already selected one ring. */
Chris Wilsonc80ff162016-07-27 09:07:27 +01001404 if ((int)file_priv->bsd_engine < 0) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001405 /* If not, use the ping-pong mechanism to select one. */
Chris Wilson91c8a322016-07-05 10:40:23 +01001406 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonc80ff162016-07-27 09:07:27 +01001407 file_priv->bsd_engine = dev_priv->mm.bsd_engine_dispatch_index;
1408 dev_priv->mm.bsd_engine_dispatch_index ^= 1;
Chris Wilson91c8a322016-07-05 10:40:23 +01001409 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001410 }
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001411
Chris Wilsonc80ff162016-07-27 09:07:27 +01001412 return file_priv->bsd_engine;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001413}
1414
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001415#define I915_USER_RINGS (4)
1416
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00001417static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001418 [I915_EXEC_DEFAULT] = RCS,
1419 [I915_EXEC_RENDER] = RCS,
1420 [I915_EXEC_BLT] = BCS,
1421 [I915_EXEC_BSD] = VCS,
1422 [I915_EXEC_VEBOX] = VECS
1423};
1424
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001425static struct intel_engine_cs *
1426eb_select_engine(struct drm_i915_private *dev_priv,
1427 struct drm_file *file,
1428 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001429{
1430 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001431 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001432
1433 if (user_ring_id > I915_USER_RINGS) {
1434 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001435 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001436 }
1437
1438 if ((user_ring_id != I915_EXEC_BSD) &&
1439 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1440 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1441 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001442 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001443 }
1444
1445 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
1446 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
1447
1448 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01001449 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001450 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
1451 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00001452 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001453 bsd_idx--;
1454 } else {
1455 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
1456 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001457 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001458 }
1459
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001460 engine = &dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001461 } else {
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001462 engine = &dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001463 }
1464
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001465 if (!intel_engine_initialized(engine)) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001466 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001467 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001468 }
1469
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001470 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001471}
1472
Eric Anholtae662d32012-01-03 09:23:29 -08001473static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001474i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1475 struct drm_file *file,
1476 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001477 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001478{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001479 struct drm_i915_private *dev_priv = to_i915(dev);
1480 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky27173f12013-08-14 11:38:36 +02001481 struct eb_vmas *eb;
Brad Volkin78a42372014-12-11 12:13:09 -08001482 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001483 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +01001484 struct i915_gem_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001485 struct i915_address_space *vm;
John Harrison5f19e2b2015-05-29 17:43:27 +01001486 struct i915_execbuffer_params params_master; /* XXX: will be removed later */
1487 struct i915_execbuffer_params *params = &params_master;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001488 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
John Harrison8e004ef2015-02-13 11:48:10 +00001489 u32 dispatch_flags;
Oscar Mateo78382592014-07-03 16:28:05 +01001490 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001491 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001492
Daniel Vettered5982e2013-01-17 22:23:36 +01001493 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001494 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001495
Chris Wilsonad19f102014-08-10 06:29:08 +01001496 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001497 if (ret)
1498 return ret;
1499
John Harrison8e004ef2015-02-13 11:48:10 +00001500 dispatch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001501 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02001502 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001503 return -EPERM;
1504
John Harrison8e004ef2015-02-13 11:48:10 +00001505 dispatch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001506 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001507 if (args->flags & I915_EXEC_IS_PINNED)
John Harrison8e004ef2015-02-13 11:48:10 +00001508 dispatch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001509
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001510 engine = eb_select_engine(dev_priv, file, args);
1511 if (!engine)
1512 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001513
1514 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001515 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001516 return -EINVAL;
1517 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001518
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001519 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
1520 if (!HAS_RESOURCE_STREAMER(dev)) {
1521 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
1522 return -EINVAL;
1523 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001524 if (engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001525 DRM_DEBUG("RS is not available on %s\n",
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001526 engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001527 return -EINVAL;
1528 }
1529
1530 dispatch_flags |= I915_DISPATCH_RS;
1531 }
1532
Chris Wilson67d97da2016-07-04 08:08:31 +01001533 /* Take a local wakeref for preparing to dispatch the execbuf as
1534 * we expect to access the hardware fairly frequently in the
1535 * process. Upon first dispatch, we acquire another prolonged
1536 * wakeref that we hold until the GPU has been idle for at least
1537 * 100ms.
1538 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001539 intel_runtime_pm_get(dev_priv);
1540
Chris Wilson54cf91d2010-11-25 18:00:26 +00001541 ret = i915_mutex_lock_interruptible(dev);
1542 if (ret)
1543 goto pre_mutex_err;
1544
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001545 ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001546 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001547 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001548 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001549 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001550 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001551
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001552 i915_gem_context_get(ctx);
Ben Widawsky41bde552013-12-06 14:11:21 -08001553
Daniel Vetterae6c4802014-08-06 15:04:53 +02001554 if (ctx->ppgtt)
1555 vm = &ctx->ppgtt->base;
1556 else
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001557 vm = &ggtt->base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001558
John Harrison5f19e2b2015-05-29 17:43:27 +01001559 memset(&params_master, 0x00, sizeof(params_master));
1560
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001561 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001562 if (eb == NULL) {
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001563 i915_gem_context_put(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001564 mutex_unlock(&dev->struct_mutex);
1565 ret = -ENOMEM;
1566 goto pre_mutex_err;
1567 }
1568
Chris Wilson54cf91d2010-11-25 18:00:26 +00001569 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001570 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001571 if (ret)
1572 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001573
Chris Wilson6fe4f142011-01-10 17:35:37 +00001574 /* take note of the batch buffer before we might reorder the lists */
Chris Wilson59bfa122016-08-04 16:32:31 +01001575 params->batch = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001576
Chris Wilson54cf91d2010-11-25 18:00:26 +00001577 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001578 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001579 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
1580 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001581 if (ret)
1582 goto err;
1583
1584 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001585 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001586 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001587 if (ret) {
1588 if (ret == -EFAULT) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001589 ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
1590 engine,
David Weinehallb1b38272015-05-20 17:00:13 +03001591 eb, exec, ctx);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001592 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1593 }
1594 if (ret)
1595 goto err;
1596 }
1597
1598 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson59bfa122016-08-04 16:32:31 +01001599 if (params->batch->obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001600 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001601 ret = -EINVAL;
1602 goto err;
1603 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001604
John Harrison5f19e2b2015-05-29 17:43:27 +01001605 params->args_batch_start_offset = args->batch_start_offset;
Chris Wilson33a051a2016-07-27 09:07:26 +01001606 if (intel_engine_needs_cmd_parser(engine) && args->batch_len) {
Chris Wilson59bfa122016-08-04 16:32:31 +01001607 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001608
Chris Wilson59bfa122016-08-04 16:32:31 +01001609 vma = i915_gem_execbuffer_parse(engine, &shadow_exec_entry,
1610 params->batch->obj,
1611 eb,
1612 args->batch_start_offset,
1613 args->batch_len,
1614 drm_is_current_master(file));
1615 if (IS_ERR(vma)) {
1616 ret = PTR_ERR(vma);
Brad Volkin78a42372014-12-11 12:13:09 -08001617 goto err;
1618 }
Chris Wilson17cabf52015-01-14 11:20:57 +00001619
Chris Wilson59bfa122016-08-04 16:32:31 +01001620 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001621 /*
1622 * Batch parsed and accepted:
1623 *
1624 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1625 * bit from MI_BATCH_BUFFER_START commands issued in
1626 * the dispatch_execbuffer implementations. We
1627 * specifically don't want that set on batches the
1628 * command parser has accepted.
1629 */
1630 dispatch_flags |= I915_DISPATCH_SECURE;
John Harrison5f19e2b2015-05-29 17:43:27 +01001631 params->args_batch_start_offset = 0;
Chris Wilson59bfa122016-08-04 16:32:31 +01001632 params->batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001633 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001634 }
1635
Chris Wilson59bfa122016-08-04 16:32:31 +01001636 params->batch->obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Brad Volkin78a42372014-12-11 12:13:09 -08001637
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001638 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1639 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001640 * hsw should have this fixed, but bdw mucks it up again. */
John Harrison8e004ef2015-02-13 11:48:10 +00001641 if (dispatch_flags & I915_DISPATCH_SECURE) {
Chris Wilson59bfa122016-08-04 16:32:31 +01001642 struct drm_i915_gem_object *obj = params->batch->obj;
1643
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001644 /*
1645 * So on first glance it looks freaky that we pin the batch here
1646 * outside of the reservation loop. But:
1647 * - The batch is already pinned into the relevant ppgtt, so we
1648 * already have the backing storage fully allocated.
1649 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01001650 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001651 * fitting due to fragmentation.
1652 * So this is actually safe.
1653 */
Chris Wilsonde895082016-08-04 16:32:34 +01001654 ret = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001655 if (ret)
1656 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001657
Chris Wilson59bfa122016-08-04 16:32:31 +01001658 params->batch = i915_gem_obj_to_ggtt(obj);
1659 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001660
John Harrison0c8dac82015-05-29 17:43:25 +01001661 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson8e637172016-08-02 22:50:26 +01001662 params->request = i915_gem_request_alloc(engine, ctx);
1663 if (IS_ERR(params->request)) {
1664 ret = PTR_ERR(params->request);
John Harrison0c8dac82015-05-29 17:43:25 +01001665 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00001666 }
John Harrison0c8dac82015-05-29 17:43:25 +01001667
Chris Wilson8e637172016-08-02 22:50:26 +01001668 ret = i915_gem_request_add_to_client(params->request, file);
John Harrisonfcfa423c2015-05-29 17:44:12 +01001669 if (ret)
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001670 goto err_request;
John Harrisonfcfa423c2015-05-29 17:44:12 +01001671
John Harrison5f19e2b2015-05-29 17:43:27 +01001672 /*
1673 * Save assorted stuff away to pass through to *_submission().
1674 * NB: This data should be 'persistent' and not local as it will
1675 * kept around beyond the duration of the IOCTL once the GPU
1676 * scheduler arrives.
1677 */
1678 params->dev = dev;
1679 params->file = file;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001680 params->engine = engine;
John Harrison5f19e2b2015-05-29 17:43:27 +01001681 params->dispatch_flags = dispatch_flags;
John Harrison5f19e2b2015-05-29 17:43:27 +01001682 params->ctx = ctx;
1683
Chris Wilson5b043f42016-08-02 22:50:38 +01001684 ret = execbuf_submit(params, args, &eb->vmas);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001685err_request:
Chris Wilson59bfa122016-08-04 16:32:31 +01001686 __i915_add_request(params->request, params->batch->obj, ret == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001687
John Harrison0c8dac82015-05-29 17:43:25 +01001688err_batch_unpin:
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001689 /*
1690 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1691 * batch vma for correctness. For less ugly and less fragility this
1692 * needs to be adjusted to also track the ggtt batch vma properly as
1693 * active.
1694 */
John Harrison8e004ef2015-02-13 11:48:10 +00001695 if (dispatch_flags & I915_DISPATCH_SECURE)
Chris Wilson59bfa122016-08-04 16:32:31 +01001696 i915_vma_unpin(params->batch);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001697err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001698 /* the request owns the ref now */
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001699 i915_gem_context_put(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001700 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001701
1702 mutex_unlock(&dev->struct_mutex);
1703
1704pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001705 /* intel_gpu_busy should also get a ref, so it will free when the device
1706 * is really idle. */
1707 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001708 return ret;
1709}
1710
1711/*
1712 * Legacy execbuffer just creates an exec2 list from the original exec object
1713 * list array and passes it to the real function.
1714 */
1715int
1716i915_gem_execbuffer(struct drm_device *dev, void *data,
1717 struct drm_file *file)
1718{
1719 struct drm_i915_gem_execbuffer *args = data;
1720 struct drm_i915_gem_execbuffer2 exec2;
1721 struct drm_i915_gem_exec_object *exec_list = NULL;
1722 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1723 int ret, i;
1724
Chris Wilson54cf91d2010-11-25 18:00:26 +00001725 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001726 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001727 return -EINVAL;
1728 }
1729
1730 /* Copy in the exec list from userland */
1731 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1732 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1733 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001734 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001735 args->buffer_count);
1736 drm_free_large(exec_list);
1737 drm_free_large(exec2_list);
1738 return -ENOMEM;
1739 }
1740 ret = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001741 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001742 sizeof(*exec_list) * args->buffer_count);
1743 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001744 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001745 args->buffer_count, ret);
1746 drm_free_large(exec_list);
1747 drm_free_large(exec2_list);
1748 return -EFAULT;
1749 }
1750
1751 for (i = 0; i < args->buffer_count; i++) {
1752 exec2_list[i].handle = exec_list[i].handle;
1753 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1754 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1755 exec2_list[i].alignment = exec_list[i].alignment;
1756 exec2_list[i].offset = exec_list[i].offset;
1757 if (INTEL_INFO(dev)->gen < 4)
1758 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1759 else
1760 exec2_list[i].flags = 0;
1761 }
1762
1763 exec2.buffers_ptr = args->buffers_ptr;
1764 exec2.buffer_count = args->buffer_count;
1765 exec2.batch_start_offset = args->batch_start_offset;
1766 exec2.batch_len = args->batch_len;
1767 exec2.DR1 = args->DR1;
1768 exec2.DR4 = args->DR4;
1769 exec2.num_cliprects = args->num_cliprects;
1770 exec2.cliprects_ptr = args->cliprects_ptr;
1771 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001772 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001773
Ben Widawsky41bde552013-12-06 14:11:21 -08001774 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001775 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001776 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001777 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001778
Chris Wilson54cf91d2010-11-25 18:00:26 +00001779 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001780 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001781 exec2_list[i].offset =
1782 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001783 ret = __copy_to_user(&user_exec_list[i].offset,
1784 &exec2_list[i].offset,
1785 sizeof(user_exec_list[i].offset));
1786 if (ret) {
1787 ret = -EFAULT;
1788 DRM_DEBUG("failed to copy %d exec entries "
1789 "back to user (%d)\n",
1790 args->buffer_count, ret);
1791 break;
1792 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001793 }
1794 }
1795
1796 drm_free_large(exec_list);
1797 drm_free_large(exec2_list);
1798 return ret;
1799}
1800
1801int
1802i915_gem_execbuffer2(struct drm_device *dev, void *data,
1803 struct drm_file *file)
1804{
1805 struct drm_i915_gem_execbuffer2 *args = data;
1806 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1807 int ret;
1808
Xi Wanged8cd3b2012-04-23 04:06:41 -04001809 if (args->buffer_count < 1 ||
1810 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001811 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001812 return -EINVAL;
1813 }
1814
Daniel Vetter9cb34662014-04-24 08:09:11 +02001815 if (args->rsvd2 != 0) {
1816 DRM_DEBUG("dirty rvsd2 field\n");
1817 return -EINVAL;
1818 }
1819
Chris Wilsonf2a85e12016-04-08 12:11:13 +01001820 exec2_list = drm_malloc_gfp(args->buffer_count,
1821 sizeof(*exec2_list),
1822 GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001823 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001824 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001825 args->buffer_count);
1826 return -ENOMEM;
1827 }
1828 ret = copy_from_user(exec2_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001829 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001830 sizeof(*exec2_list) * args->buffer_count);
1831 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001832 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001833 args->buffer_count, ret);
1834 drm_free_large(exec2_list);
1835 return -EFAULT;
1836 }
1837
Ben Widawsky41bde552013-12-06 14:11:21 -08001838 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001839 if (!ret) {
1840 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001841 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001842 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001843 int i;
1844
1845 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001846 exec2_list[i].offset =
1847 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001848 ret = __copy_to_user(&user_exec_list[i].offset,
1849 &exec2_list[i].offset,
1850 sizeof(user_exec_list[i].offset));
1851 if (ret) {
1852 ret = -EFAULT;
1853 DRM_DEBUG("failed to copy %d exec entries "
1854 "back to user\n",
1855 args->buffer_count);
1856 break;
1857 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001858 }
1859 }
1860
1861 drm_free_large(exec2_list);
1862 return ret;
1863}