blob: a1da3028a949a26533fc2c5874b9452a896ee533 [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{
Chris Wilson573adb32016-08-04 16:32:39 +0100437 unsigned long active = i915_gem_object_get_active(obj);
Chris Wilson909d0742016-08-04 07:52:41 +0100438 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 Wilson573adb32016-08-04 16:32:39 +0100993static unsigned int eb_other_engines(struct drm_i915_gem_request *req)
994{
995 unsigned int mask;
996
997 mask = ~intel_engine_flag(req->engine) & I915_BO_ACTIVE_MASK;
998 mask <<= I915_BO_ACTIVE_SHIFT;
999
1000 return mask;
1001}
1002
Chris Wilson54cf91d2010-11-25 18:00:26 +00001003static int
John Harrison535fbe82015-05-29 17:43:32 +01001004i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
Ben Widawsky27173f12013-08-14 11:38:36 +02001005 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001006{
Chris Wilson573adb32016-08-04 16:32:39 +01001007 const unsigned int other_rings = eb_other_engines(req);
Ben Widawsky27173f12013-08-14 11:38:36 +02001008 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001009 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +01001010 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +00001011 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001012
Ben Widawsky27173f12013-08-14 11:38:36 +02001013 list_for_each_entry(vma, vmas, exec_list) {
1014 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001015
Chris Wilson573adb32016-08-04 16:32:39 +01001016 if (obj->flags & other_rings) {
Chris Wilson8e637172016-08-02 22:50:26 +01001017 ret = i915_gem_object_sync(obj, req);
Chris Wilson03ade512015-04-27 13:41:18 +01001018 if (ret)
1019 return ret;
1020 }
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001021
1022 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +01001023 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001024
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001025 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001026 }
1027
Chris Wilson000433b2013-08-08 14:41:09 +01001028 if (flush_chipset)
Chris Wilsonc0336662016-05-06 15:40:21 +01001029 i915_gem_chipset_flush(req->engine->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001030
1031 if (flush_domains & I915_GEM_DOMAIN_GTT)
1032 wmb();
1033
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001034 /* Unconditionally invalidate GPU caches and TLBs. */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001035 return req->engine->emit_flush(req, EMIT_INVALIDATE);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001036}
1037
Chris Wilson432e58e2010-11-25 19:32:06 +00001038static bool
1039i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001040{
Daniel Vettered5982e2013-01-17 22:23:36 +01001041 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
1042 return false;
1043
Chris Wilson2f5945b2015-10-06 11:39:55 +01001044 /* Kernel clipping was a DRI1 misfeature */
1045 if (exec->num_cliprects || exec->cliprects_ptr)
1046 return false;
1047
1048 if (exec->DR4 == 0xffffffff) {
1049 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1050 exec->DR4 = 0;
1051 }
1052 if (exec->DR1 || exec->DR4)
1053 return false;
1054
1055 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1056 return false;
1057
1058 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001059}
1060
1061static int
Chris Wilsonad19f102014-08-10 06:29:08 +01001062validate_exec_list(struct drm_device *dev,
1063 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001064 int count)
1065{
Daniel Vetterb205ca52013-09-19 14:00:11 +02001066 unsigned relocs_total = 0;
1067 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +01001068 unsigned invalid_flags;
1069 int i;
1070
Dave Gordon9e2793f62016-07-14 14:52:03 +01001071 /* INTERNAL flags must not overlap with external ones */
1072 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
1073
Chris Wilsonad19f102014-08-10 06:29:08 +01001074 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
1075 if (USES_FULL_PPGTT(dev))
1076 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001077
1078 for (i = 0; i < count; i++) {
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001079 char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001080 int length; /* limited by fault_in_pages_readable() */
1081
Chris Wilsonad19f102014-08-10 06:29:08 +01001082 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +01001083 return -EINVAL;
1084
Michał Winiarski934acce2015-12-29 18:24:52 +01001085 /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
1086 * any non-page-aligned or non-canonical addresses.
1087 */
1088 if (exec[i].flags & EXEC_OBJECT_PINNED) {
1089 if (exec[i].offset !=
1090 gen8_canonical_addr(exec[i].offset & PAGE_MASK))
1091 return -EINVAL;
1092
1093 /* From drm_mm perspective address space is continuous,
1094 * so from this point we're always using non-canonical
1095 * form internally.
1096 */
1097 exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
1098 }
1099
Chris Wilson55a97852015-06-19 13:59:46 +01001100 if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
1101 return -EINVAL;
1102
Chris Wilson91b2db62016-08-04 16:32:23 +01001103 /* pad_to_size was once a reserved field, so sanitize it */
1104 if (exec[i].flags & EXEC_OBJECT_PAD_TO_SIZE) {
1105 if (offset_in_page(exec[i].pad_to_size))
1106 return -EINVAL;
1107 } else {
1108 exec[i].pad_to_size = 0;
1109 }
1110
Kees Cook3118a4f2013-03-11 17:31:45 -07001111 /* First check for malicious input causing overflow in
1112 * the worst case where we need to allocate the entire
1113 * relocation tree as a single array.
1114 */
1115 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001116 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -07001117 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001118
1119 length = exec[i].relocation_count *
1120 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -07001121 /*
1122 * We must check that the entire relocation array is safe
1123 * to read, but since we may need to update the presumed
1124 * offsets during execution, check for full write access.
1125 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001126 if (!access_ok(VERIFY_WRITE, ptr, length))
1127 return -EFAULT;
1128
Jani Nikulad330a952014-01-21 11:24:25 +02001129 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +08001130 if (fault_in_multipages_readable(ptr, length))
1131 return -EFAULT;
1132 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001133 }
1134
1135 return 0;
1136}
1137
Chris Wilsone2efd132016-05-24 14:53:34 +01001138static struct i915_gem_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001139i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001140 struct intel_engine_cs *engine, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001141{
Chris Wilsone2efd132016-05-24 14:53:34 +01001142 struct i915_gem_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001143 struct i915_ctx_hang_stats *hs;
1144
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001145 if (engine->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001146 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001147
Chris Wilsonca585b52016-05-24 14:53:36 +01001148 ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001149 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -08001150 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001151
Ben Widawsky41bde552013-12-06 14:11:21 -08001152 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001153 if (hs->banned) {
1154 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -08001155 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001156 }
1157
Ben Widawsky41bde552013-12-06 14:11:21 -08001158 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001159}
1160
Chris Wilson5cf3d282016-08-04 07:52:43 +01001161void i915_vma_move_to_active(struct i915_vma *vma,
1162 struct drm_i915_gem_request *req,
1163 unsigned int flags)
1164{
1165 struct drm_i915_gem_object *obj = vma->obj;
1166 const unsigned int idx = req->engine->id;
1167
1168 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1169
1170 obj->dirty = 1; /* be paranoid */
1171
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001172 /* Add a reference if we're newly entering the active list.
1173 * The order in which we add operations to the retirement queue is
1174 * vital here: mark_active adds to the start of the callback list,
1175 * such that subsequent callbacks are called first. Therefore we
1176 * add the active reference first and queue for it to be dropped
1177 * *last*.
1178 */
Chris Wilson573adb32016-08-04 16:32:39 +01001179 if (!i915_gem_object_is_active(obj))
Chris Wilson5cf3d282016-08-04 07:52:43 +01001180 i915_gem_object_get(obj);
Chris Wilson573adb32016-08-04 16:32:39 +01001181 i915_gem_object_set_active(obj, idx);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001182 i915_gem_active_set(&obj->last_read[idx], req);
1183
1184 if (flags & EXEC_OBJECT_WRITE) {
1185 i915_gem_active_set(&obj->last_write, req);
1186
1187 intel_fb_obj_invalidate(obj, ORIGIN_CS);
1188
1189 /* update for the implicit flush after a batch */
1190 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1191 }
1192
1193 if (flags & EXEC_OBJECT_NEEDS_FENCE) {
1194 i915_gem_active_set(&obj->last_fence, req);
1195 if (flags & __EXEC_OBJECT_HAS_FENCE) {
1196 struct drm_i915_private *dev_priv = req->i915;
1197
1198 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
1199 &dev_priv->mm.fence_list);
1200 }
1201 }
1202
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001203 i915_vma_set_active(vma, idx);
1204 i915_gem_active_set(&vma->last_read[idx], req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001205 list_move_tail(&vma->vm_link, &vma->vm->active_list);
1206}
1207
Chris Wilson5b043f42016-08-02 22:50:38 +01001208static void
Ben Widawsky27173f12013-08-14 11:38:36 +02001209i915_gem_execbuffer_move_to_active(struct list_head *vmas,
John Harrison8a8edb52015-05-29 17:43:33 +01001210 struct drm_i915_gem_request *req)
Chris Wilson432e58e2010-11-25 19:32:06 +00001211{
Ben Widawsky27173f12013-08-14 11:38:36 +02001212 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +00001213
Ben Widawsky27173f12013-08-14 11:38:36 +02001214 list_for_each_entry(vma, vmas, exec_list) {
1215 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +01001216 u32 old_read = obj->base.read_domains;
1217 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +00001218
Chris Wilson432e58e2010-11-25 19:32:06 +00001219 obj->base.write_domain = obj->base.pending_write_domain;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001220 if (obj->base.write_domain)
1221 vma->exec_entry->flags |= EXEC_OBJECT_WRITE;
1222 else
Daniel Vettered5982e2013-01-17 22:23:36 +01001223 obj->base.pending_read_domains |= obj->base.read_domains;
1224 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +00001225
Chris Wilson5cf3d282016-08-04 07:52:43 +01001226 i915_vma_move_to_active(vma, req, vma->exec_entry->flags);
Chris Wilsondb53a302011-02-03 11:57:46 +00001227 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +00001228 }
1229}
1230
Chris Wilson54cf91d2010-11-25 18:00:26 +00001231static int
Chris Wilsonb5321f32016-08-02 22:50:18 +01001232i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001233{
Chris Wilson7e37f882016-08-02 22:50:21 +01001234 struct intel_ring *ring = req->ring;
Eric Anholtae662d32012-01-03 09:23:29 -08001235 int ret, i;
1236
Chris Wilsonb5321f32016-08-02 22:50:18 +01001237 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001238 DRM_DEBUG("sol reset is gen7/rcs only\n");
1239 return -EINVAL;
1240 }
Eric Anholtae662d32012-01-03 09:23:29 -08001241
John Harrison5fb9de12015-05-29 17:44:07 +01001242 ret = intel_ring_begin(req, 4 * 3);
Eric Anholtae662d32012-01-03 09:23:29 -08001243 if (ret)
1244 return ret;
1245
1246 for (i = 0; i < 4; i++) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001247 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1248 intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
1249 intel_ring_emit(ring, 0);
Eric Anholtae662d32012-01-03 09:23:29 -08001250 }
1251
Chris Wilsonb5321f32016-08-02 22:50:18 +01001252 intel_ring_advance(ring);
Eric Anholtae662d32012-01-03 09:23:29 -08001253
1254 return 0;
1255}
1256
Chris Wilson59bfa122016-08-04 16:32:31 +01001257static struct i915_vma*
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001258i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
Brad Volkin71745372014-12-11 12:13:12 -08001259 struct drm_i915_gem_exec_object2 *shadow_exec_entry,
Brad Volkin71745372014-12-11 12:13:12 -08001260 struct drm_i915_gem_object *batch_obj,
Chris Wilson59bfa122016-08-04 16:32:31 +01001261 struct eb_vmas *eb,
Brad Volkin71745372014-12-11 12:13:12 -08001262 u32 batch_start_offset,
1263 u32 batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001264 bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001265{
Brad Volkin71745372014-12-11 12:13:12 -08001266 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001267 struct i915_vma *vma;
Brad Volkin71745372014-12-11 12:13:12 -08001268 int ret;
1269
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001270 shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
Chris Wilson17cabf52015-01-14 11:20:57 +00001271 PAGE_ALIGN(batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001272 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001273 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001274
Chris Wilson33a051a2016-07-27 09:07:26 +01001275 ret = intel_engine_cmd_parser(engine,
1276 batch_obj,
1277 shadow_batch_obj,
1278 batch_start_offset,
1279 batch_len,
1280 is_master);
Chris Wilson17cabf52015-01-14 11:20:57 +00001281 if (ret)
1282 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001283
Chris Wilsonde895082016-08-04 16:32:34 +01001284 ret = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
Chris Wilson17cabf52015-01-14 11:20:57 +00001285 if (ret)
1286 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001287
Chris Wilsonde4e7832015-04-07 16:20:35 +01001288 i915_gem_object_unpin_pages(shadow_batch_obj);
1289
Chris Wilson17cabf52015-01-14 11:20:57 +00001290 memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
Brad Volkin71745372014-12-11 12:13:12 -08001291
Chris Wilson17cabf52015-01-14 11:20:57 +00001292 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1293 vma->exec_entry = shadow_exec_entry;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001294 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
Chris Wilson25dc5562016-07-20 13:31:52 +01001295 i915_gem_object_get(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001296 list_add_tail(&vma->exec_list, &eb->vmas);
Brad Volkin71745372014-12-11 12:13:12 -08001297
Chris Wilson59bfa122016-08-04 16:32:31 +01001298 return vma;
Chris Wilson17cabf52015-01-14 11:20:57 +00001299
1300err:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001301 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001302 if (ret == -EACCES) /* unhandled chained batch */
Chris Wilson59bfa122016-08-04 16:32:31 +01001303 return NULL;
Chris Wilson17cabf52015-01-14 11:20:57 +00001304 else
1305 return ERR_PTR(ret);
Brad Volkin71745372014-12-11 12:13:12 -08001306}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001307
Chris Wilson5b043f42016-08-02 22:50:38 +01001308static int
1309execbuf_submit(struct i915_execbuffer_params *params,
1310 struct drm_i915_gem_execbuffer2 *args,
1311 struct list_head *vmas)
Oscar Mateo78382592014-07-03 16:28:05 +01001312{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001313 struct drm_i915_private *dev_priv = params->request->i915;
John Harrison5f19e2b2015-05-29 17:43:27 +01001314 u64 exec_start, exec_len;
Oscar Mateo78382592014-07-03 16:28:05 +01001315 int instp_mode;
1316 u32 instp_mask;
Chris Wilson2f5945b2015-10-06 11:39:55 +01001317 int ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001318
John Harrison535fbe82015-05-29 17:43:32 +01001319 ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
Oscar Mateo78382592014-07-03 16:28:05 +01001320 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001321 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001322
John Harrisonba01cc92015-05-29 17:43:41 +01001323 ret = i915_switch_context(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001324 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001325 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001326
1327 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1328 instp_mask = I915_EXEC_CONSTANTS_MASK;
1329 switch (instp_mode) {
1330 case I915_EXEC_CONSTANTS_REL_GENERAL:
1331 case I915_EXEC_CONSTANTS_ABSOLUTE:
1332 case I915_EXEC_CONSTANTS_REL_SURFACE:
Chris Wilsonb5321f32016-08-02 22:50:18 +01001333 if (instp_mode != 0 && params->engine->id != RCS) {
Oscar Mateo78382592014-07-03 16:28:05 +01001334 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001335 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001336 }
1337
1338 if (instp_mode != dev_priv->relative_constants_mode) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001339 if (INTEL_INFO(dev_priv)->gen < 4) {
Oscar Mateo78382592014-07-03 16:28:05 +01001340 DRM_DEBUG("no rel constants on pre-gen4\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001341 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001342 }
1343
Chris Wilsonb5321f32016-08-02 22:50:18 +01001344 if (INTEL_INFO(dev_priv)->gen > 5 &&
Oscar Mateo78382592014-07-03 16:28:05 +01001345 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1346 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001347 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001348 }
1349
1350 /* The HW changed the meaning on this bit on gen6 */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001351 if (INTEL_INFO(dev_priv)->gen >= 6)
Oscar Mateo78382592014-07-03 16:28:05 +01001352 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1353 }
1354 break;
1355 default:
1356 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001357 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001358 }
1359
Chris Wilsonb5321f32016-08-02 22:50:18 +01001360 if (params->engine->id == RCS &&
Chris Wilson2f5945b2015-10-06 11:39:55 +01001361 instp_mode != dev_priv->relative_constants_mode) {
Chris Wilson7e37f882016-08-02 22:50:21 +01001362 struct intel_ring *ring = params->request->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01001363
John Harrison5fb9de12015-05-29 17:44:07 +01001364 ret = intel_ring_begin(params->request, 4);
Oscar Mateo78382592014-07-03 16:28:05 +01001365 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001366 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001367
Chris Wilsonb5321f32016-08-02 22:50:18 +01001368 intel_ring_emit(ring, MI_NOOP);
1369 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1370 intel_ring_emit_reg(ring, INSTPM);
1371 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1372 intel_ring_advance(ring);
Oscar Mateo78382592014-07-03 16:28:05 +01001373
1374 dev_priv->relative_constants_mode = instp_mode;
1375 }
1376
1377 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001378 ret = i915_reset_gen7_sol_offsets(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001379 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001380 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001381 }
1382
John Harrison5f19e2b2015-05-29 17:43:27 +01001383 exec_len = args->batch_len;
Chris Wilson59bfa122016-08-04 16:32:31 +01001384 exec_start = params->batch->node.start +
John Harrison5f19e2b2015-05-29 17:43:27 +01001385 params->args_batch_start_offset;
1386
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001387 if (exec_len == 0)
Chris Wilson59bfa122016-08-04 16:32:31 +01001388 exec_len = params->batch->size;
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001389
Chris Wilson803688b2016-08-02 22:50:27 +01001390 ret = params->engine->emit_bb_start(params->request,
1391 exec_start, exec_len,
1392 params->dispatch_flags);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001393 if (ret)
1394 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001395
John Harrison95c24162015-05-29 17:43:31 +01001396 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001397
John Harrison8a8edb52015-05-29 17:43:33 +01001398 i915_gem_execbuffer_move_to_active(vmas, params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001399
Chris Wilson2f5945b2015-10-06 11:39:55 +01001400 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001401}
1402
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001403/**
1404 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01001405 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001406 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001407static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01001408gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
1409 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001410{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001411 struct drm_i915_file_private *file_priv = file->driver_priv;
1412
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001413 /* Check whether the file_priv has already selected one ring. */
Chris Wilsonc80ff162016-07-27 09:07:27 +01001414 if ((int)file_priv->bsd_engine < 0) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001415 /* If not, use the ping-pong mechanism to select one. */
Chris Wilson91c8a322016-07-05 10:40:23 +01001416 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonc80ff162016-07-27 09:07:27 +01001417 file_priv->bsd_engine = dev_priv->mm.bsd_engine_dispatch_index;
1418 dev_priv->mm.bsd_engine_dispatch_index ^= 1;
Chris Wilson91c8a322016-07-05 10:40:23 +01001419 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001420 }
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001421
Chris Wilsonc80ff162016-07-27 09:07:27 +01001422 return file_priv->bsd_engine;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001423}
1424
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001425#define I915_USER_RINGS (4)
1426
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00001427static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001428 [I915_EXEC_DEFAULT] = RCS,
1429 [I915_EXEC_RENDER] = RCS,
1430 [I915_EXEC_BLT] = BCS,
1431 [I915_EXEC_BSD] = VCS,
1432 [I915_EXEC_VEBOX] = VECS
1433};
1434
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001435static struct intel_engine_cs *
1436eb_select_engine(struct drm_i915_private *dev_priv,
1437 struct drm_file *file,
1438 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001439{
1440 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001441 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001442
1443 if (user_ring_id > I915_USER_RINGS) {
1444 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001445 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001446 }
1447
1448 if ((user_ring_id != I915_EXEC_BSD) &&
1449 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1450 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1451 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001452 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001453 }
1454
1455 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
1456 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
1457
1458 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01001459 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001460 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
1461 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00001462 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001463 bsd_idx--;
1464 } else {
1465 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
1466 bsd_idx);
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 engine = &dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001471 } else {
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001472 engine = &dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001473 }
1474
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001475 if (!intel_engine_initialized(engine)) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001476 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001477 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001478 }
1479
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001480 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001481}
1482
Eric Anholtae662d32012-01-03 09:23:29 -08001483static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001484i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1485 struct drm_file *file,
1486 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001487 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001488{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001489 struct drm_i915_private *dev_priv = to_i915(dev);
1490 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky27173f12013-08-14 11:38:36 +02001491 struct eb_vmas *eb;
Brad Volkin78a42372014-12-11 12:13:09 -08001492 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001493 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +01001494 struct i915_gem_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001495 struct i915_address_space *vm;
John Harrison5f19e2b2015-05-29 17:43:27 +01001496 struct i915_execbuffer_params params_master; /* XXX: will be removed later */
1497 struct i915_execbuffer_params *params = &params_master;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001498 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
John Harrison8e004ef2015-02-13 11:48:10 +00001499 u32 dispatch_flags;
Oscar Mateo78382592014-07-03 16:28:05 +01001500 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001501 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001502
Daniel Vettered5982e2013-01-17 22:23:36 +01001503 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001504 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001505
Chris Wilsonad19f102014-08-10 06:29:08 +01001506 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001507 if (ret)
1508 return ret;
1509
John Harrison8e004ef2015-02-13 11:48:10 +00001510 dispatch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001511 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02001512 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001513 return -EPERM;
1514
John Harrison8e004ef2015-02-13 11:48:10 +00001515 dispatch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001516 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001517 if (args->flags & I915_EXEC_IS_PINNED)
John Harrison8e004ef2015-02-13 11:48:10 +00001518 dispatch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001519
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001520 engine = eb_select_engine(dev_priv, file, args);
1521 if (!engine)
1522 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001523
1524 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001525 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001526 return -EINVAL;
1527 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001528
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001529 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
1530 if (!HAS_RESOURCE_STREAMER(dev)) {
1531 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
1532 return -EINVAL;
1533 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001534 if (engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001535 DRM_DEBUG("RS is not available on %s\n",
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001536 engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001537 return -EINVAL;
1538 }
1539
1540 dispatch_flags |= I915_DISPATCH_RS;
1541 }
1542
Chris Wilson67d97da2016-07-04 08:08:31 +01001543 /* Take a local wakeref for preparing to dispatch the execbuf as
1544 * we expect to access the hardware fairly frequently in the
1545 * process. Upon first dispatch, we acquire another prolonged
1546 * wakeref that we hold until the GPU has been idle for at least
1547 * 100ms.
1548 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001549 intel_runtime_pm_get(dev_priv);
1550
Chris Wilson54cf91d2010-11-25 18:00:26 +00001551 ret = i915_mutex_lock_interruptible(dev);
1552 if (ret)
1553 goto pre_mutex_err;
1554
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001555 ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001556 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001557 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001558 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001559 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001560 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001561
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001562 i915_gem_context_get(ctx);
Ben Widawsky41bde552013-12-06 14:11:21 -08001563
Daniel Vetterae6c4802014-08-06 15:04:53 +02001564 if (ctx->ppgtt)
1565 vm = &ctx->ppgtt->base;
1566 else
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001567 vm = &ggtt->base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001568
John Harrison5f19e2b2015-05-29 17:43:27 +01001569 memset(&params_master, 0x00, sizeof(params_master));
1570
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001571 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001572 if (eb == NULL) {
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001573 i915_gem_context_put(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001574 mutex_unlock(&dev->struct_mutex);
1575 ret = -ENOMEM;
1576 goto pre_mutex_err;
1577 }
1578
Chris Wilson54cf91d2010-11-25 18:00:26 +00001579 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001580 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001581 if (ret)
1582 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001583
Chris Wilson6fe4f142011-01-10 17:35:37 +00001584 /* take note of the batch buffer before we might reorder the lists */
Chris Wilson59bfa122016-08-04 16:32:31 +01001585 params->batch = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001586
Chris Wilson54cf91d2010-11-25 18:00:26 +00001587 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001588 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001589 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
1590 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001591 if (ret)
1592 goto err;
1593
1594 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001595 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001596 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001597 if (ret) {
1598 if (ret == -EFAULT) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001599 ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
1600 engine,
David Weinehallb1b38272015-05-20 17:00:13 +03001601 eb, exec, ctx);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001602 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1603 }
1604 if (ret)
1605 goto err;
1606 }
1607
1608 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson59bfa122016-08-04 16:32:31 +01001609 if (params->batch->obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001610 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001611 ret = -EINVAL;
1612 goto err;
1613 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001614
John Harrison5f19e2b2015-05-29 17:43:27 +01001615 params->args_batch_start_offset = args->batch_start_offset;
Chris Wilson33a051a2016-07-27 09:07:26 +01001616 if (intel_engine_needs_cmd_parser(engine) && args->batch_len) {
Chris Wilson59bfa122016-08-04 16:32:31 +01001617 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001618
Chris Wilson59bfa122016-08-04 16:32:31 +01001619 vma = i915_gem_execbuffer_parse(engine, &shadow_exec_entry,
1620 params->batch->obj,
1621 eb,
1622 args->batch_start_offset,
1623 args->batch_len,
1624 drm_is_current_master(file));
1625 if (IS_ERR(vma)) {
1626 ret = PTR_ERR(vma);
Brad Volkin78a42372014-12-11 12:13:09 -08001627 goto err;
1628 }
Chris Wilson17cabf52015-01-14 11:20:57 +00001629
Chris Wilson59bfa122016-08-04 16:32:31 +01001630 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001631 /*
1632 * Batch parsed and accepted:
1633 *
1634 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1635 * bit from MI_BATCH_BUFFER_START commands issued in
1636 * the dispatch_execbuffer implementations. We
1637 * specifically don't want that set on batches the
1638 * command parser has accepted.
1639 */
1640 dispatch_flags |= I915_DISPATCH_SECURE;
John Harrison5f19e2b2015-05-29 17:43:27 +01001641 params->args_batch_start_offset = 0;
Chris Wilson59bfa122016-08-04 16:32:31 +01001642 params->batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001643 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001644 }
1645
Chris Wilson59bfa122016-08-04 16:32:31 +01001646 params->batch->obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Brad Volkin78a42372014-12-11 12:13:09 -08001647
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001648 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1649 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001650 * hsw should have this fixed, but bdw mucks it up again. */
John Harrison8e004ef2015-02-13 11:48:10 +00001651 if (dispatch_flags & I915_DISPATCH_SECURE) {
Chris Wilson59bfa122016-08-04 16:32:31 +01001652 struct drm_i915_gem_object *obj = params->batch->obj;
1653
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001654 /*
1655 * So on first glance it looks freaky that we pin the batch here
1656 * outside of the reservation loop. But:
1657 * - The batch is already pinned into the relevant ppgtt, so we
1658 * already have the backing storage fully allocated.
1659 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01001660 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001661 * fitting due to fragmentation.
1662 * So this is actually safe.
1663 */
Chris Wilsonde895082016-08-04 16:32:34 +01001664 ret = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001665 if (ret)
1666 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001667
Chris Wilson59bfa122016-08-04 16:32:31 +01001668 params->batch = i915_gem_obj_to_ggtt(obj);
1669 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001670
John Harrison0c8dac82015-05-29 17:43:25 +01001671 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson8e637172016-08-02 22:50:26 +01001672 params->request = i915_gem_request_alloc(engine, ctx);
1673 if (IS_ERR(params->request)) {
1674 ret = PTR_ERR(params->request);
John Harrison0c8dac82015-05-29 17:43:25 +01001675 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00001676 }
John Harrison0c8dac82015-05-29 17:43:25 +01001677
Chris Wilson8e637172016-08-02 22:50:26 +01001678 ret = i915_gem_request_add_to_client(params->request, file);
John Harrisonfcfa423c2015-05-29 17:44:12 +01001679 if (ret)
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001680 goto err_request;
John Harrisonfcfa423c2015-05-29 17:44:12 +01001681
John Harrison5f19e2b2015-05-29 17:43:27 +01001682 /*
1683 * Save assorted stuff away to pass through to *_submission().
1684 * NB: This data should be 'persistent' and not local as it will
1685 * kept around beyond the duration of the IOCTL once the GPU
1686 * scheduler arrives.
1687 */
1688 params->dev = dev;
1689 params->file = file;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001690 params->engine = engine;
John Harrison5f19e2b2015-05-29 17:43:27 +01001691 params->dispatch_flags = dispatch_flags;
John Harrison5f19e2b2015-05-29 17:43:27 +01001692 params->ctx = ctx;
1693
Chris Wilson5b043f42016-08-02 22:50:38 +01001694 ret = execbuf_submit(params, args, &eb->vmas);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001695err_request:
Chris Wilson59bfa122016-08-04 16:32:31 +01001696 __i915_add_request(params->request, params->batch->obj, ret == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001697
John Harrison0c8dac82015-05-29 17:43:25 +01001698err_batch_unpin:
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001699 /*
1700 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1701 * batch vma for correctness. For less ugly and less fragility this
1702 * needs to be adjusted to also track the ggtt batch vma properly as
1703 * active.
1704 */
John Harrison8e004ef2015-02-13 11:48:10 +00001705 if (dispatch_flags & I915_DISPATCH_SECURE)
Chris Wilson59bfa122016-08-04 16:32:31 +01001706 i915_vma_unpin(params->batch);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001707err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001708 /* the request owns the ref now */
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001709 i915_gem_context_put(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001710 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001711
1712 mutex_unlock(&dev->struct_mutex);
1713
1714pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001715 /* intel_gpu_busy should also get a ref, so it will free when the device
1716 * is really idle. */
1717 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001718 return ret;
1719}
1720
1721/*
1722 * Legacy execbuffer just creates an exec2 list from the original exec object
1723 * list array and passes it to the real function.
1724 */
1725int
1726i915_gem_execbuffer(struct drm_device *dev, void *data,
1727 struct drm_file *file)
1728{
1729 struct drm_i915_gem_execbuffer *args = data;
1730 struct drm_i915_gem_execbuffer2 exec2;
1731 struct drm_i915_gem_exec_object *exec_list = NULL;
1732 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1733 int ret, i;
1734
Chris Wilson54cf91d2010-11-25 18:00:26 +00001735 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001736 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001737 return -EINVAL;
1738 }
1739
1740 /* Copy in the exec list from userland */
1741 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1742 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1743 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001744 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001745 args->buffer_count);
1746 drm_free_large(exec_list);
1747 drm_free_large(exec2_list);
1748 return -ENOMEM;
1749 }
1750 ret = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001751 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001752 sizeof(*exec_list) * args->buffer_count);
1753 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001754 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001755 args->buffer_count, ret);
1756 drm_free_large(exec_list);
1757 drm_free_large(exec2_list);
1758 return -EFAULT;
1759 }
1760
1761 for (i = 0; i < args->buffer_count; i++) {
1762 exec2_list[i].handle = exec_list[i].handle;
1763 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1764 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1765 exec2_list[i].alignment = exec_list[i].alignment;
1766 exec2_list[i].offset = exec_list[i].offset;
1767 if (INTEL_INFO(dev)->gen < 4)
1768 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1769 else
1770 exec2_list[i].flags = 0;
1771 }
1772
1773 exec2.buffers_ptr = args->buffers_ptr;
1774 exec2.buffer_count = args->buffer_count;
1775 exec2.batch_start_offset = args->batch_start_offset;
1776 exec2.batch_len = args->batch_len;
1777 exec2.DR1 = args->DR1;
1778 exec2.DR4 = args->DR4;
1779 exec2.num_cliprects = args->num_cliprects;
1780 exec2.cliprects_ptr = args->cliprects_ptr;
1781 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001782 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001783
Ben Widawsky41bde552013-12-06 14:11:21 -08001784 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001785 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001786 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001787 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001788
Chris Wilson54cf91d2010-11-25 18:00:26 +00001789 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001790 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001791 exec2_list[i].offset =
1792 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001793 ret = __copy_to_user(&user_exec_list[i].offset,
1794 &exec2_list[i].offset,
1795 sizeof(user_exec_list[i].offset));
1796 if (ret) {
1797 ret = -EFAULT;
1798 DRM_DEBUG("failed to copy %d exec entries "
1799 "back to user (%d)\n",
1800 args->buffer_count, ret);
1801 break;
1802 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001803 }
1804 }
1805
1806 drm_free_large(exec_list);
1807 drm_free_large(exec2_list);
1808 return ret;
1809}
1810
1811int
1812i915_gem_execbuffer2(struct drm_device *dev, void *data,
1813 struct drm_file *file)
1814{
1815 struct drm_i915_gem_execbuffer2 *args = data;
1816 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1817 int ret;
1818
Xi Wanged8cd3b2012-04-23 04:06:41 -04001819 if (args->buffer_count < 1 ||
1820 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001821 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001822 return -EINVAL;
1823 }
1824
Daniel Vetter9cb34662014-04-24 08:09:11 +02001825 if (args->rsvd2 != 0) {
1826 DRM_DEBUG("dirty rvsd2 field\n");
1827 return -EINVAL;
1828 }
1829
Chris Wilsonf2a85e12016-04-08 12:11:13 +01001830 exec2_list = drm_malloc_gfp(args->buffer_count,
1831 sizeof(*exec2_list),
1832 GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001833 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001834 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001835 args->buffer_count);
1836 return -ENOMEM;
1837 }
1838 ret = copy_from_user(exec2_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001839 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001840 sizeof(*exec2_list) * args->buffer_count);
1841 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001842 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001843 args->buffer_count, ret);
1844 drm_free_large(exec2_list);
1845 return -EFAULT;
1846 }
1847
Ben Widawsky41bde552013-12-06 14:11:21 -08001848 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001849 if (!ret) {
1850 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001851 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001852 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001853 int i;
1854
1855 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001856 exec2_list[i].offset =
1857 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001858 ret = __copy_to_user(&user_exec_list[i].offset,
1859 &exec2_list[i].offset,
1860 sizeof(user_exec_list[i].offset));
1861 if (ret) {
1862 ret = -EFAULT;
1863 DRM_DEBUG("failed to copy %d exec entries "
1864 "back to user\n",
1865 args->buffer_count);
1866 break;
1867 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001868 }
1869 }
1870
1871 drm_free_large(exec2_list);
1872 return ret;
1873}