blob: a0c95ab15d574d30579d73db745e8fd774354245 [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"
Eugeni Dodonovf45b5552011-12-09 17:16:37 -080034#include <linux/dma_remapping.h>
David Hildenbrand32d82062015-05-11 17:52:12 +020035#include <linux/uaccess.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000036
Dave Gordon9e2793f62016-07-14 14:52:03 +010037#define __EXEC_OBJECT_HAS_PIN (1<<31)
38#define __EXEC_OBJECT_HAS_FENCE (1<<30)
39#define __EXEC_OBJECT_NEEDS_MAP (1<<29)
40#define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
41#define __EXEC_OBJECT_INTERNAL_FLAGS (0xf<<28) /* all of the above */
Chris Wilsond23db882014-05-23 08:48:08 +020042
43#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000044
Chris Wilson5b043f42016-08-02 22:50:38 +010045struct i915_execbuffer_params {
46 struct drm_device *dev;
47 struct drm_file *file;
Chris Wilson59bfa122016-08-04 16:32:31 +010048 struct i915_vma *batch;
49 u32 dispatch_flags;
50 u32 args_batch_start_offset;
Chris Wilson5b043f42016-08-02 22:50:38 +010051 struct intel_engine_cs *engine;
Chris Wilson5b043f42016-08-02 22:50:38 +010052 struct i915_gem_context *ctx;
53 struct drm_i915_gem_request *request;
54};
55
Ben Widawsky27173f12013-08-14 11:38:36 +020056struct eb_vmas {
57 struct list_head vmas;
Chris Wilson67731b82010-12-08 10:38:14 +000058 int and;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000059 union {
Ben Widawsky27173f12013-08-14 11:38:36 +020060 struct i915_vma *lut[0];
Chris Wilsoneef90cc2013-01-08 10:53:17 +000061 struct hlist_head buckets[0];
62 };
Chris Wilson67731b82010-12-08 10:38:14 +000063};
64
Ben Widawsky27173f12013-08-14 11:38:36 +020065static struct eb_vmas *
Ben Widawsky17601cbc2013-11-25 09:54:38 -080066eb_create(struct drm_i915_gem_execbuffer2 *args)
Chris Wilson67731b82010-12-08 10:38:14 +000067{
Ben Widawsky27173f12013-08-14 11:38:36 +020068 struct eb_vmas *eb = NULL;
Chris Wilson67731b82010-12-08 10:38:14 +000069
Chris Wilsoneef90cc2013-01-08 10:53:17 +000070 if (args->flags & I915_EXEC_HANDLE_LUT) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020071 unsigned size = args->buffer_count;
Ben Widawsky27173f12013-08-14 11:38:36 +020072 size *= sizeof(struct i915_vma *);
73 size += sizeof(struct eb_vmas);
Chris Wilsoneef90cc2013-01-08 10:53:17 +000074 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
75 }
76
77 if (eb == NULL) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020078 unsigned size = args->buffer_count;
79 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Lauri Kasanen27b7c632013-03-27 15:04:55 +020080 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
Chris Wilsoneef90cc2013-01-08 10:53:17 +000081 while (count > 2*size)
82 count >>= 1;
83 eb = kzalloc(count*sizeof(struct hlist_head) +
Ben Widawsky27173f12013-08-14 11:38:36 +020084 sizeof(struct eb_vmas),
Chris Wilsoneef90cc2013-01-08 10:53:17 +000085 GFP_TEMPORARY);
86 if (eb == NULL)
87 return eb;
88
89 eb->and = count - 1;
90 } else
91 eb->and = -args->buffer_count;
92
Ben Widawsky27173f12013-08-14 11:38:36 +020093 INIT_LIST_HEAD(&eb->vmas);
Chris Wilson67731b82010-12-08 10:38:14 +000094 return eb;
95}
96
97static void
Ben Widawsky27173f12013-08-14 11:38:36 +020098eb_reset(struct eb_vmas *eb)
Chris Wilson67731b82010-12-08 10:38:14 +000099{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000100 if (eb->and >= 0)
101 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
Chris Wilson67731b82010-12-08 10:38:14 +0000102}
103
Chris Wilson59bfa122016-08-04 16:32:31 +0100104static struct i915_vma *
105eb_get_batch(struct eb_vmas *eb)
106{
107 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
108
109 /*
110 * SNA is doing fancy tricks with compressing batch buffers, which leads
111 * to negative relocation deltas. Usually that works out ok since the
112 * relocate address is still positive, except when the batch is placed
113 * very low in the GTT. Ensure this doesn't happen.
114 *
115 * Note that actual hangs have only been observed on gen7, but for
116 * paranoia do it everywhere.
117 */
118 if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
119 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
120
121 return vma;
122}
123
Chris Wilson3b96eff2013-01-08 10:53:14 +0000124static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200125eb_lookup_vmas(struct eb_vmas *eb,
126 struct drm_i915_gem_exec_object2 *exec,
127 const struct drm_i915_gem_execbuffer2 *args,
128 struct i915_address_space *vm,
129 struct drm_file *file)
Chris Wilson3b96eff2013-01-08 10:53:14 +0000130{
Ben Widawsky27173f12013-08-14 11:38:36 +0200131 struct drm_i915_gem_object *obj;
132 struct list_head objects;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000133 int i, ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000134
Ben Widawsky27173f12013-08-14 11:38:36 +0200135 INIT_LIST_HEAD(&objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000136 spin_lock(&file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +0200137 /* Grab a reference to the object and release the lock so we can lookup
138 * or create the VMA without using GFP_ATOMIC */
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000139 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000140 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
141 if (obj == NULL) {
142 spin_unlock(&file->table_lock);
143 DRM_DEBUG("Invalid object handle %d at index %d\n",
144 exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200145 ret = -ENOENT;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000146 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000147 }
148
Ben Widawsky27173f12013-08-14 11:38:36 +0200149 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000150 spin_unlock(&file->table_lock);
151 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
152 obj, exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200153 ret = -EINVAL;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000154 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000155 }
156
Chris Wilson25dc5562016-07-20 13:31:52 +0100157 i915_gem_object_get(obj);
Ben Widawsky27173f12013-08-14 11:38:36 +0200158 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000159 }
160 spin_unlock(&file->table_lock);
161
Ben Widawsky27173f12013-08-14 11:38:36 +0200162 i = 0;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000163 while (!list_empty(&objects)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200164 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800165
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000166 obj = list_first_entry(&objects,
167 struct drm_i915_gem_object,
168 obj_exec_link);
169
Daniel Vettere656a6c2013-08-14 14:14:04 +0200170 /*
171 * NOTE: We can leak any vmas created here when something fails
172 * later on. But that's no issue since vma_unbind can deal with
173 * vmas which are not actually bound. And since only
174 * lookup_or_create exists as an interface to get at the vma
175 * from the (obj, vm) we don't run the risk of creating
176 * duplicated vmas for the same vm.
177 */
Daniel Vetterda51a1e2014-08-11 12:08:58 +0200178 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200179 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200180 DRM_DEBUG("Failed to lookup VMA\n");
181 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000182 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200183 }
184
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000185 /* Transfer ownership from the objects list to the vmas list. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200186 list_add_tail(&vma->exec_list, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000187 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200188
189 vma->exec_entry = &exec[i];
190 if (eb->and < 0) {
191 eb->lut[i] = vma;
192 } else {
193 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
194 vma->exec_handle = handle;
195 hlist_add_head(&vma->exec_node,
196 &eb->buckets[handle & eb->and]);
197 }
198 ++i;
199 }
200
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000201 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200202
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000203
204err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200205 while (!list_empty(&objects)) {
206 obj = list_first_entry(&objects,
207 struct drm_i915_gem_object,
208 obj_exec_link);
209 list_del_init(&obj->obj_exec_link);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100210 i915_gem_object_put(obj);
Ben Widawsky27173f12013-08-14 11:38:36 +0200211 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000212 /*
213 * Objects already transfered to the vmas list will be unreferenced by
214 * eb_destroy.
215 */
216
Ben Widawsky27173f12013-08-14 11:38:36 +0200217 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000218}
219
Ben Widawsky27173f12013-08-14 11:38:36 +0200220static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000221{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000222 if (eb->and < 0) {
223 if (handle >= -eb->and)
224 return NULL;
225 return eb->lut[handle];
226 } else {
227 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800228 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000229
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000230 head = &eb->buckets[handle & eb->and];
Geliang Tangaa459502016-01-18 23:54:20 +0800231 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200232 if (vma->exec_handle == handle)
233 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000234 }
235 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000236 }
Chris Wilson67731b82010-12-08 10:38:14 +0000237}
238
Chris Wilsona415d352013-11-26 11:23:15 +0000239static void
240i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
241{
242 struct drm_i915_gem_exec_object2 *entry;
243 struct drm_i915_gem_object *obj = vma->obj;
244
245 if (!drm_mm_node_allocated(&vma->node))
246 return;
247
248 entry = vma->exec_entry;
249
250 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
251 i915_gem_object_unpin_fence(obj);
252
253 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100254 __i915_vma_unpin(vma);
Chris Wilsona415d352013-11-26 11:23:15 +0000255
Chris Wilsonde4e7832015-04-07 16:20:35 +0100256 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilsona415d352013-11-26 11:23:15 +0000257}
258
259static void eb_destroy(struct eb_vmas *eb)
260{
Ben Widawsky27173f12013-08-14 11:38:36 +0200261 while (!list_empty(&eb->vmas)) {
262 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000263
Ben Widawsky27173f12013-08-14 11:38:36 +0200264 vma = list_first_entry(&eb->vmas,
265 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000266 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200267 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000268 i915_gem_execbuffer_unreserve_vma(vma);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100269 i915_gem_object_put(vma->obj);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000270 }
Chris Wilson67731b82010-12-08 10:38:14 +0000271 kfree(eb);
272}
273
Chris Wilsondabdfe02012-03-26 10:10:27 +0200274static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
275{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300276 return (HAS_LLC(obj->base.dev) ||
277 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200278 obj->cache_level != I915_CACHE_NONE);
279}
280
Michał Winiarski934acce2015-12-29 18:24:52 +0100281/* Used to convert any address to canonical form.
282 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
283 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
284 * addresses to be in a canonical form:
285 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
286 * canonical form [63:48] == [47]."
287 */
288#define GEN8_HIGH_ADDRESS_BIT 47
289static inline uint64_t gen8_canonical_addr(uint64_t address)
290{
291 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
292}
293
294static inline uint64_t gen8_noncanonical_addr(uint64_t address)
295{
296 return address & ((1ULL << (GEN8_HIGH_ADDRESS_BIT + 1)) - 1);
297}
298
299static inline uint64_t
300relocation_target(struct drm_i915_gem_relocation_entry *reloc,
301 uint64_t target_offset)
302{
303 return gen8_canonical_addr((int)reloc->delta + target_offset);
304}
305
Chris Wilson54cf91d2010-11-25 18:00:26 +0000306static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100307relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700308 struct drm_i915_gem_relocation_entry *reloc,
309 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100310{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700311 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100312 uint32_t page_offset = offset_in_page(reloc->offset);
Michał Winiarski934acce2015-12-29 18:24:52 +0100313 uint64_t delta = relocation_target(reloc, target_offset);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100314 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800315 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100316
Chris Wilson2cc86b82013-08-26 19:51:00 -0300317 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100318 if (ret)
319 return ret;
320
Dave Gordon033908a2015-12-10 18:51:23 +0000321 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Rafael Barbalho5032d872013-08-21 17:10:51 +0100322 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700323 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700324
325 if (INTEL_INFO(dev)->gen >= 8) {
326 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
327
328 if (page_offset == 0) {
329 kunmap_atomic(vaddr);
Dave Gordon033908a2015-12-10 18:51:23 +0000330 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700331 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
332 }
333
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700334 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700335 }
336
Rafael Barbalho5032d872013-08-21 17:10:51 +0100337 kunmap_atomic(vaddr);
338
339 return 0;
340}
341
342static int
343relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700344 struct drm_i915_gem_relocation_entry *reloc,
345 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100346{
347 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300348 struct drm_i915_private *dev_priv = to_i915(dev);
349 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michał Winiarski934acce2015-12-29 18:24:52 +0100350 uint64_t delta = relocation_target(reloc, target_offset);
Chris Wilson906843c2014-08-10 06:29:11 +0100351 uint64_t offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100352 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800353 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100354
355 ret = i915_gem_object_set_to_gtt_domain(obj, true);
356 if (ret)
357 return ret;
358
359 ret = i915_gem_object_put_fence(obj);
360 if (ret)
361 return ret;
362
363 /* Map the page containing the relocation we're going to perform. */
Chris Wilson906843c2014-08-10 06:29:11 +0100364 offset = i915_gem_obj_ggtt_offset(obj);
365 offset += reloc->offset;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300366 reloc_page = io_mapping_map_atomic_wc(ggtt->mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100367 offset & PAGE_MASK);
368 iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700369
370 if (INTEL_INFO(dev)->gen >= 8) {
Chris Wilson906843c2014-08-10 06:29:11 +0100371 offset += sizeof(uint32_t);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700372
Chris Wilson906843c2014-08-10 06:29:11 +0100373 if (offset_in_page(offset) == 0) {
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700374 io_mapping_unmap_atomic(reloc_page);
Chris Wilson906843c2014-08-10 06:29:11 +0100375 reloc_page =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300376 io_mapping_map_atomic_wc(ggtt->mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100377 offset);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700378 }
379
Chris Wilson906843c2014-08-10 06:29:11 +0100380 iowrite32(upper_32_bits(delta),
381 reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700382 }
383
Rafael Barbalho5032d872013-08-21 17:10:51 +0100384 io_mapping_unmap_atomic(reloc_page);
385
386 return 0;
387}
388
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000389static void
390clflush_write32(void *addr, uint32_t value)
391{
392 /* This is not a fast path, so KISS. */
393 drm_clflush_virt_range(addr, sizeof(uint32_t));
394 *(uint32_t *)addr = value;
395 drm_clflush_virt_range(addr, sizeof(uint32_t));
396}
397
398static int
399relocate_entry_clflush(struct drm_i915_gem_object *obj,
400 struct drm_i915_gem_relocation_entry *reloc,
401 uint64_t target_offset)
402{
403 struct drm_device *dev = obj->base.dev;
404 uint32_t page_offset = offset_in_page(reloc->offset);
Michał Winiarski934acce2015-12-29 18:24:52 +0100405 uint64_t delta = relocation_target(reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000406 char *vaddr;
407 int ret;
408
409 ret = i915_gem_object_set_to_gtt_domain(obj, true);
410 if (ret)
411 return ret;
412
Dave Gordon033908a2015-12-10 18:51:23 +0000413 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000414 reloc->offset >> PAGE_SHIFT));
415 clflush_write32(vaddr + page_offset, lower_32_bits(delta));
416
417 if (INTEL_INFO(dev)->gen >= 8) {
418 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
419
420 if (page_offset == 0) {
421 kunmap_atomic(vaddr);
Dave Gordon033908a2015-12-10 18:51:23 +0000422 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000423 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
424 }
425
426 clflush_write32(vaddr + page_offset, upper_32_bits(delta));
427 }
428
429 kunmap_atomic(vaddr);
430
431 return 0;
432}
433
Chris Wilson909d0742016-08-04 07:52:41 +0100434static bool object_is_idle(struct drm_i915_gem_object *obj)
435{
436 unsigned long active = obj->active;
437 int idx;
438
439 for_each_active(active, idx) {
440 if (!i915_gem_active_is_idle(&obj->last_read[idx],
441 &obj->base.dev->struct_mutex))
442 return false;
443 }
444
445 return true;
446}
447
Rafael Barbalho5032d872013-08-21 17:10:51 +0100448static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000449i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200450 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800451 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000452{
453 struct drm_device *dev = obj->base.dev;
454 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100455 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200456 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700457 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800458 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000459
Chris Wilson67731b82010-12-08 10:38:14 +0000460 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200461 target_vma = eb_get_vma(eb, reloc->target_handle);
462 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000463 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200464 target_i915_obj = target_vma->obj;
465 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000466
Michał Winiarski934acce2015-12-29 18:24:52 +0100467 target_offset = gen8_canonical_addr(target_vma->node.start);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000468
Eric Anholte844b992012-07-31 15:35:01 -0700469 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
470 * pipe_control writes because the gpu doesn't properly redirect them
471 * through the ppgtt for non_secure batchbuffers. */
472 if (unlikely(IS_GEN6(dev) &&
Daniel Vetter08755462015-04-20 09:04:05 -0700473 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000474 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
Daniel Vetter08755462015-04-20 09:04:05 -0700475 PIN_GLOBAL);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000476 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
477 return ret;
478 }
Eric Anholte844b992012-07-31 15:35:01 -0700479
Chris Wilson54cf91d2010-11-25 18:00:26 +0000480 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000481 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100482 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000483 "obj %p target %d offset %d "
484 "read %08x write %08x",
485 obj, reloc->target_handle,
486 (int) reloc->offset,
487 reloc->read_domains,
488 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800489 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000490 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100491 if (unlikely((reloc->write_domain | reloc->read_domains)
492 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100493 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000494 "obj %p target %d offset %d "
495 "read %08x write %08x",
496 obj, reloc->target_handle,
497 (int) reloc->offset,
498 reloc->read_domains,
499 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800500 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000501 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000502
503 target_obj->pending_read_domains |= reloc->read_domains;
504 target_obj->pending_write_domain |= reloc->write_domain;
505
506 /* If the relocation already has the right value in it, no
507 * more work needs to be done.
508 */
509 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000510 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000511
512 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700513 if (unlikely(reloc->offset >
514 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100515 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000516 "obj %p target %d offset %d size %d.\n",
517 obj, reloc->target_handle,
518 (int) reloc->offset,
519 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800520 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000521 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000522 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100523 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000524 "obj %p target %d offset %d.\n",
525 obj, reloc->target_handle,
526 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800527 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000528 }
529
Chris Wilsondabdfe02012-03-26 10:10:27 +0200530 /* We can't wait for rendering with pagefaults disabled */
Chris Wilson909d0742016-08-04 07:52:41 +0100531 if (pagefault_disabled() && !object_is_idle(obj))
Chris Wilsondabdfe02012-03-26 10:10:27 +0200532 return -EFAULT;
533
Rafael Barbalho5032d872013-08-21 17:10:51 +0100534 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700535 ret = relocate_entry_cpu(obj, reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000536 else if (obj->map_and_fenceable)
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700537 ret = relocate_entry_gtt(obj, reloc, target_offset);
Borislav Petkov906bf7f2016-03-29 17:41:59 +0200538 else if (static_cpu_has(X86_FEATURE_CLFLUSH))
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000539 ret = relocate_entry_clflush(obj, reloc, target_offset);
540 else {
541 WARN_ONCE(1, "Impossible case in relocation handling\n");
542 ret = -ENODEV;
543 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000544
Daniel Vetterd4d36012013-09-02 20:56:23 +0200545 if (ret)
546 return ret;
547
Chris Wilson54cf91d2010-11-25 18:00:26 +0000548 /* and update the user's relocation entry */
549 reloc->presumed_offset = target_offset;
550
Chris Wilson67731b82010-12-08 10:38:14 +0000551 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000552}
553
554static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200555i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
556 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000557{
Chris Wilson1d83f442012-03-24 20:12:53 +0000558#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
559 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000560 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200561 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000562 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000563
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300564 user_relocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000565
Chris Wilson1d83f442012-03-24 20:12:53 +0000566 remain = entry->relocation_count;
567 while (remain) {
568 struct drm_i915_gem_relocation_entry *r = stack_reloc;
569 int count = remain;
570 if (count > ARRAY_SIZE(stack_reloc))
571 count = ARRAY_SIZE(stack_reloc);
572 remain -= count;
573
574 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000575 return -EFAULT;
576
Chris Wilson1d83f442012-03-24 20:12:53 +0000577 do {
578 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000579
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800580 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000581 if (ret)
582 return ret;
583
584 if (r->presumed_offset != offset &&
Linus Torvalds5b09c3e2016-05-22 14:19:37 -0700585 __put_user(r->presumed_offset, &user_relocs->presumed_offset)) {
Chris Wilson1d83f442012-03-24 20:12:53 +0000586 return -EFAULT;
587 }
588
589 user_relocs++;
590 r++;
591 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000592 }
593
594 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000595#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000596}
597
598static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200599i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
600 struct eb_vmas *eb,
601 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000602{
Ben Widawsky27173f12013-08-14 11:38:36 +0200603 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000604 int i, ret;
605
606 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800607 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000608 if (ret)
609 return ret;
610 }
611
612 return 0;
613}
614
615static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800616i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000617{
Ben Widawsky27173f12013-08-14 11:38:36 +0200618 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000619 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000620
Chris Wilsond4aeee72011-03-14 15:11:24 +0000621 /* This is the fast path and we cannot handle a pagefault whilst
622 * holding the struct mutex lest the user pass in the relocations
623 * contained within a mmaped bo. For in such a case we, the page
624 * fault handler would call i915_gem_fault() and we would try to
625 * acquire the struct mutex again. Obviously this is bad and so
626 * lockdep complains vehemently.
627 */
628 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200629 list_for_each_entry(vma, &eb->vmas, exec_list) {
630 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000631 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000632 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000633 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000634 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000635
Chris Wilsond4aeee72011-03-14 15:11:24 +0000636 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000637}
638
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000639static bool only_mappable_for_reloc(unsigned int flags)
640{
641 return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
642 __EXEC_OBJECT_NEEDS_MAP;
643}
644
Chris Wilson1690e1e2011-12-14 13:57:08 +0100645static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200646i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000647 struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200648 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100649{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800650 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200651 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200652 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100653 int ret;
654
Daniel Vetter08755462015-04-20 09:04:05 -0700655 flags = PIN_USER;
Daniel Vetter0229da32015-04-14 19:01:54 +0200656 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
657 flags |= PIN_GLOBAL;
658
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000659 if (!drm_mm_node_allocated(&vma->node)) {
Michel Thierry101b5062015-10-01 13:33:57 +0100660 /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
661 * limit address to the first 4GBs for unflagged objects.
662 */
663 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
664 flags |= PIN_ZONE_4G;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000665 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
666 flags |= PIN_GLOBAL | PIN_MAPPABLE;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000667 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
668 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson506a8e82015-12-08 11:55:07 +0000669 if (entry->flags & EXEC_OBJECT_PINNED)
670 flags |= entry->offset | PIN_OFFSET_FIXED;
Michel Thierry101b5062015-10-01 13:33:57 +0100671 if ((flags & PIN_MAPPABLE) == 0)
672 flags |= PIN_HIGH;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000673 }
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100674
Chris Wilson59bfa122016-08-04 16:32:31 +0100675 ret = i915_vma_pin(vma,
676 entry->pad_to_size,
677 entry->alignment,
678 flags);
679 if ((ret == -ENOSPC || ret == -E2BIG) &&
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000680 only_mappable_for_reloc(entry->flags))
Chris Wilson59bfa122016-08-04 16:32:31 +0100681 ret = i915_vma_pin(vma,
682 entry->pad_to_size,
683 entry->alignment,
684 flags & ~PIN_MAPPABLE);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100685 if (ret)
686 return ret;
687
Chris Wilson7788a762012-08-24 19:18:18 +0100688 entry->flags |= __EXEC_OBJECT_HAS_PIN;
689
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100690 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
691 ret = i915_gem_object_get_fence(obj);
692 if (ret)
693 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100694
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100695 if (i915_gem_object_pin_fence(obj))
696 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100697 }
698
Ben Widawsky27173f12013-08-14 11:38:36 +0200699 if (entry->offset != vma->node.start) {
700 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100701 *need_reloc = true;
702 }
703
704 if (entry->flags & EXEC_OBJECT_WRITE) {
705 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
706 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
707 }
708
Chris Wilson1690e1e2011-12-14 13:57:08 +0100709 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100710}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100711
Chris Wilsond23db882014-05-23 08:48:08 +0200712static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200713need_reloc_mappable(struct i915_vma *vma)
714{
715 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
716
717 if (entry->relocation_count == 0)
718 return false;
719
Chris Wilson3272db52016-08-04 16:32:32 +0100720 if (!i915_vma_is_ggtt(vma))
Chris Wilsone6a84462014-08-11 12:00:12 +0200721 return false;
722
723 /* See also use_cpu_reloc() */
724 if (HAS_LLC(vma->obj->base.dev))
725 return false;
726
727 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
728 return false;
729
730 return true;
731}
732
733static bool
734eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200735{
736 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
737 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond23db882014-05-23 08:48:08 +0200738
Chris Wilson3272db52016-08-04 16:32:32 +0100739 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
740 !i915_vma_is_ggtt(vma));
Chris Wilsond23db882014-05-23 08:48:08 +0200741
742 if (entry->alignment &&
743 vma->node.start & (entry->alignment - 1))
744 return true;
745
Chris Wilson91b2db62016-08-04 16:32:23 +0100746 if (vma->node.size < entry->pad_to_size)
747 return true;
748
Chris Wilson506a8e82015-12-08 11:55:07 +0000749 if (entry->flags & EXEC_OBJECT_PINNED &&
750 vma->node.start != entry->offset)
751 return true;
752
Chris Wilsond23db882014-05-23 08:48:08 +0200753 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
754 vma->node.start < BATCH_OFFSET_BIAS)
755 return true;
756
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000757 /* avoid costly ping-pong once a batch bo ended up non-mappable */
758 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
759 return !only_mappable_for_reloc(entry->flags);
760
Michel Thierry101b5062015-10-01 13:33:57 +0100761 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
762 (vma->node.start + vma->node.size - 1) >> 32)
763 return true;
764
Chris Wilsond23db882014-05-23 08:48:08 +0200765 return false;
766}
767
Chris Wilson54cf91d2010-11-25 18:00:26 +0000768static int
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000769i915_gem_execbuffer_reserve(struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200770 struct list_head *vmas,
Chris Wilsone2efd132016-05-24 14:53:34 +0100771 struct i915_gem_context *ctx,
Daniel Vettered5982e2013-01-17 22:23:36 +0100772 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000773{
Chris Wilson432e58e2010-11-25 19:32:06 +0000774 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200775 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700776 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200777 struct list_head ordered_vmas;
Chris Wilson506a8e82015-12-08 11:55:07 +0000778 struct list_head pinned_vmas;
Chris Wilsonc0336662016-05-06 15:40:21 +0100779 bool has_fenced_gpu_access = INTEL_GEN(engine->i915) < 4;
Chris Wilson7788a762012-08-24 19:18:18 +0100780 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000781
Ben Widawsky68c8c172013-09-11 14:57:50 -0700782 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
783
Ben Widawsky27173f12013-08-14 11:38:36 +0200784 INIT_LIST_HEAD(&ordered_vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000785 INIT_LIST_HEAD(&pinned_vmas);
Ben Widawsky27173f12013-08-14 11:38:36 +0200786 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000787 struct drm_i915_gem_exec_object2 *entry;
788 bool need_fence, need_mappable;
789
Ben Widawsky27173f12013-08-14 11:38:36 +0200790 vma = list_first_entry(vmas, struct i915_vma, exec_list);
791 obj = vma->obj;
792 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000793
David Weinehallb1b38272015-05-20 17:00:13 +0300794 if (ctx->flags & CONTEXT_NO_ZEROMAP)
795 entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
796
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100797 if (!has_fenced_gpu_access)
798 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000799 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000800 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
801 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200802 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000803
Chris Wilson506a8e82015-12-08 11:55:07 +0000804 if (entry->flags & EXEC_OBJECT_PINNED)
805 list_move_tail(&vma->exec_list, &pinned_vmas);
806 else if (need_mappable) {
Chris Wilsone6a84462014-08-11 12:00:12 +0200807 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200808 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200809 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200810 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000811
Daniel Vettered5982e2013-01-17 22:23:36 +0100812 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000813 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000814 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200815 list_splice(&ordered_vmas, vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000816 list_splice(&pinned_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000817
818 /* Attempt to pin all of the buffers into the GTT.
819 * This is done in 3 phases:
820 *
821 * 1a. Unbind all objects that do not match the GTT constraints for
822 * the execbuffer (fenceable, mappable, alignment etc).
823 * 1b. Increment pin count for already bound objects.
824 * 2. Bind new objects.
825 * 3. Decrement pin count.
826 *
Chris Wilson7788a762012-08-24 19:18:18 +0100827 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000828 * room for the earlier objects *unless* we need to defragment.
829 */
830 retry = 0;
831 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100832 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000833
834 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200835 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200836 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000837 continue;
838
Chris Wilsone6a84462014-08-11 12:00:12 +0200839 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200840 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000841 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000842 ret = i915_gem_execbuffer_reserve_vma(vma,
843 engine,
844 need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000845 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000846 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000847 }
848
849 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200850 list_for_each_entry(vma, vmas, exec_list) {
851 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100852 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000853
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000854 ret = i915_gem_execbuffer_reserve_vma(vma, engine,
855 need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100856 if (ret)
857 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000858 }
859
Chris Wilsona415d352013-11-26 11:23:15 +0000860err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200861 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000862 return ret;
863
Chris Wilsona415d352013-11-26 11:23:15 +0000864 /* Decrement pin count for bound objects */
865 list_for_each_entry(vma, vmas, exec_list)
866 i915_gem_execbuffer_unreserve_vma(vma);
867
Ben Widawsky68c8c172013-09-11 14:57:50 -0700868 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000869 if (ret)
870 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000871 } while (1);
872}
873
874static int
875i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100876 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000877 struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000878 struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200879 struct eb_vmas *eb,
David Weinehallb1b38272015-05-20 17:00:13 +0300880 struct drm_i915_gem_exec_object2 *exec,
Chris Wilsone2efd132016-05-24 14:53:34 +0100881 struct i915_gem_context *ctx)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000882{
883 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200884 struct i915_address_space *vm;
885 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100886 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000887 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000888 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200889 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000890
Ben Widawsky27173f12013-08-14 11:38:36 +0200891 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
892
Chris Wilson67731b82010-12-08 10:38:14 +0000893 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200894 while (!list_empty(&eb->vmas)) {
895 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
896 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000897 i915_gem_execbuffer_unreserve_vma(vma);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100898 i915_gem_object_put(vma->obj);
Chris Wilson67731b82010-12-08 10:38:14 +0000899 }
900
Chris Wilson54cf91d2010-11-25 18:00:26 +0000901 mutex_unlock(&dev->struct_mutex);
902
903 total = 0;
904 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000905 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000906
Chris Wilsondd6864a2011-01-12 23:49:13 +0000907 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000908 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000909 if (reloc == NULL || reloc_offset == NULL) {
910 drm_free_large(reloc);
911 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000912 mutex_lock(&dev->struct_mutex);
913 return -ENOMEM;
914 }
915
916 total = 0;
917 for (i = 0; i < count; i++) {
918 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000919 u64 invalid_offset = (u64)-1;
920 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000921
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300922 user_relocs = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000923
924 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000925 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000926 ret = -EFAULT;
927 mutex_lock(&dev->struct_mutex);
928 goto err;
929 }
930
Chris Wilson262b6d32013-01-15 16:17:54 +0000931 /* As we do not update the known relocation offsets after
932 * relocating (due to the complexities in lock handling),
933 * we need to mark them as invalid now so that we force the
934 * relocation processing next time. Just in case the target
935 * object is evicted and then rebound into its old
936 * presumed_offset before the next execbuffer - if that
937 * happened we would make the mistake of assuming that the
938 * relocations were valid.
939 */
940 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100941 if (__copy_to_user(&user_relocs[j].presumed_offset,
942 &invalid_offset,
943 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000944 ret = -EFAULT;
945 mutex_lock(&dev->struct_mutex);
946 goto err;
947 }
948 }
949
Chris Wilsondd6864a2011-01-12 23:49:13 +0000950 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000951 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000952 }
953
954 ret = i915_mutex_lock_interruptible(dev);
955 if (ret) {
956 mutex_lock(&dev->struct_mutex);
957 goto err;
958 }
959
Chris Wilson67731b82010-12-08 10:38:14 +0000960 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000961 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200962 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000963 if (ret)
964 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000965
Daniel Vettered5982e2013-01-17 22:23:36 +0100966 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000967 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
968 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000969 if (ret)
970 goto err;
971
Ben Widawsky27173f12013-08-14 11:38:36 +0200972 list_for_each_entry(vma, &eb->vmas, exec_list) {
973 int offset = vma->exec_entry - exec;
974 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
975 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000976 if (ret)
977 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000978 }
979
980 /* Leave the user relocations as are, this is the painfully slow path,
981 * and we want to avoid the complication of dropping the lock whilst
982 * having buffers reserved in the aperture and so causing spurious
983 * ENOSPC for random operations.
984 */
985
986err:
987 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000988 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000989 return ret;
990}
991
Chris Wilson54cf91d2010-11-25 18:00:26 +0000992static int
John Harrison535fbe82015-05-29 17:43:32 +0100993i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
Ben Widawsky27173f12013-08-14 11:38:36 +0200994 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000995{
Tvrtko Ursulin666796d2016-03-16 11:00:39 +0000996 const unsigned other_rings = ~intel_engine_flag(req->engine);
Ben Widawsky27173f12013-08-14 11:38:36 +0200997 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200998 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100999 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +00001000 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001001
Ben Widawsky27173f12013-08-14 11:38:36 +02001002 list_for_each_entry(vma, vmas, exec_list) {
1003 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001004
1005 if (obj->active & other_rings) {
Chris Wilson8e637172016-08-02 22:50:26 +01001006 ret = i915_gem_object_sync(obj, req);
Chris Wilson03ade512015-04-27 13:41:18 +01001007 if (ret)
1008 return ret;
1009 }
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001010
1011 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +01001012 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001013
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001014 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001015 }
1016
Chris Wilson000433b2013-08-08 14:41:09 +01001017 if (flush_chipset)
Chris Wilsonc0336662016-05-06 15:40:21 +01001018 i915_gem_chipset_flush(req->engine->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001019
1020 if (flush_domains & I915_GEM_DOMAIN_GTT)
1021 wmb();
1022
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001023 /* Unconditionally invalidate GPU caches and TLBs. */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001024 return req->engine->emit_flush(req, EMIT_INVALIDATE);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001025}
1026
Chris Wilson432e58e2010-11-25 19:32:06 +00001027static bool
1028i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001029{
Daniel Vettered5982e2013-01-17 22:23:36 +01001030 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
1031 return false;
1032
Chris Wilson2f5945b2015-10-06 11:39:55 +01001033 /* Kernel clipping was a DRI1 misfeature */
1034 if (exec->num_cliprects || exec->cliprects_ptr)
1035 return false;
1036
1037 if (exec->DR4 == 0xffffffff) {
1038 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1039 exec->DR4 = 0;
1040 }
1041 if (exec->DR1 || exec->DR4)
1042 return false;
1043
1044 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1045 return false;
1046
1047 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001048}
1049
1050static int
Chris Wilsonad19f102014-08-10 06:29:08 +01001051validate_exec_list(struct drm_device *dev,
1052 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001053 int count)
1054{
Daniel Vetterb205ca52013-09-19 14:00:11 +02001055 unsigned relocs_total = 0;
1056 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +01001057 unsigned invalid_flags;
1058 int i;
1059
Dave Gordon9e2793f62016-07-14 14:52:03 +01001060 /* INTERNAL flags must not overlap with external ones */
1061 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
1062
Chris Wilsonad19f102014-08-10 06:29:08 +01001063 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
1064 if (USES_FULL_PPGTT(dev))
1065 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001066
1067 for (i = 0; i < count; i++) {
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001068 char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001069 int length; /* limited by fault_in_pages_readable() */
1070
Chris Wilsonad19f102014-08-10 06:29:08 +01001071 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +01001072 return -EINVAL;
1073
Michał Winiarski934acce2015-12-29 18:24:52 +01001074 /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
1075 * any non-page-aligned or non-canonical addresses.
1076 */
1077 if (exec[i].flags & EXEC_OBJECT_PINNED) {
1078 if (exec[i].offset !=
1079 gen8_canonical_addr(exec[i].offset & PAGE_MASK))
1080 return -EINVAL;
1081
1082 /* From drm_mm perspective address space is continuous,
1083 * so from this point we're always using non-canonical
1084 * form internally.
1085 */
1086 exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
1087 }
1088
Chris Wilson55a97852015-06-19 13:59:46 +01001089 if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
1090 return -EINVAL;
1091
Chris Wilson91b2db62016-08-04 16:32:23 +01001092 /* pad_to_size was once a reserved field, so sanitize it */
1093 if (exec[i].flags & EXEC_OBJECT_PAD_TO_SIZE) {
1094 if (offset_in_page(exec[i].pad_to_size))
1095 return -EINVAL;
1096 } else {
1097 exec[i].pad_to_size = 0;
1098 }
1099
Kees Cook3118a4f2013-03-11 17:31:45 -07001100 /* First check for malicious input causing overflow in
1101 * the worst case where we need to allocate the entire
1102 * relocation tree as a single array.
1103 */
1104 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001105 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -07001106 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001107
1108 length = exec[i].relocation_count *
1109 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -07001110 /*
1111 * We must check that the entire relocation array is safe
1112 * to read, but since we may need to update the presumed
1113 * offsets during execution, check for full write access.
1114 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001115 if (!access_ok(VERIFY_WRITE, ptr, length))
1116 return -EFAULT;
1117
Jani Nikulad330a952014-01-21 11:24:25 +02001118 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +08001119 if (fault_in_multipages_readable(ptr, length))
1120 return -EFAULT;
1121 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001122 }
1123
1124 return 0;
1125}
1126
Chris Wilsone2efd132016-05-24 14:53:34 +01001127static struct i915_gem_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001128i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001129 struct intel_engine_cs *engine, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001130{
Chris Wilsone2efd132016-05-24 14:53:34 +01001131 struct i915_gem_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001132 struct i915_ctx_hang_stats *hs;
1133
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001134 if (engine->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001135 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001136
Chris Wilsonca585b52016-05-24 14:53:36 +01001137 ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001138 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -08001139 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001140
Ben Widawsky41bde552013-12-06 14:11:21 -08001141 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001142 if (hs->banned) {
1143 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -08001144 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001145 }
1146
Ben Widawsky41bde552013-12-06 14:11:21 -08001147 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001148}
1149
Chris Wilson5cf3d282016-08-04 07:52:43 +01001150void i915_vma_move_to_active(struct i915_vma *vma,
1151 struct drm_i915_gem_request *req,
1152 unsigned int flags)
1153{
1154 struct drm_i915_gem_object *obj = vma->obj;
1155 const unsigned int idx = req->engine->id;
1156
1157 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1158
1159 obj->dirty = 1; /* be paranoid */
1160
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001161 /* Add a reference if we're newly entering the active list.
1162 * The order in which we add operations to the retirement queue is
1163 * vital here: mark_active adds to the start of the callback list,
1164 * such that subsequent callbacks are called first. Therefore we
1165 * add the active reference first and queue for it to be dropped
1166 * *last*.
1167 */
Chris Wilson5cf3d282016-08-04 07:52:43 +01001168 if (obj->active == 0)
1169 i915_gem_object_get(obj);
1170 obj->active |= 1 << idx;
1171 i915_gem_active_set(&obj->last_read[idx], req);
1172
1173 if (flags & EXEC_OBJECT_WRITE) {
1174 i915_gem_active_set(&obj->last_write, req);
1175
1176 intel_fb_obj_invalidate(obj, ORIGIN_CS);
1177
1178 /* update for the implicit flush after a batch */
1179 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1180 }
1181
1182 if (flags & EXEC_OBJECT_NEEDS_FENCE) {
1183 i915_gem_active_set(&obj->last_fence, req);
1184 if (flags & __EXEC_OBJECT_HAS_FENCE) {
1185 struct drm_i915_private *dev_priv = req->i915;
1186
1187 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
1188 &dev_priv->mm.fence_list);
1189 }
1190 }
1191
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001192 i915_vma_set_active(vma, idx);
1193 i915_gem_active_set(&vma->last_read[idx], req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001194 list_move_tail(&vma->vm_link, &vma->vm->active_list);
1195}
1196
Chris Wilson5b043f42016-08-02 22:50:38 +01001197static void
Ben Widawsky27173f12013-08-14 11:38:36 +02001198i915_gem_execbuffer_move_to_active(struct list_head *vmas,
John Harrison8a8edb52015-05-29 17:43:33 +01001199 struct drm_i915_gem_request *req)
Chris Wilson432e58e2010-11-25 19:32:06 +00001200{
Ben Widawsky27173f12013-08-14 11:38:36 +02001201 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +00001202
Ben Widawsky27173f12013-08-14 11:38:36 +02001203 list_for_each_entry(vma, vmas, exec_list) {
1204 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +01001205 u32 old_read = obj->base.read_domains;
1206 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +00001207
Chris Wilson432e58e2010-11-25 19:32:06 +00001208 obj->base.write_domain = obj->base.pending_write_domain;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001209 if (obj->base.write_domain)
1210 vma->exec_entry->flags |= EXEC_OBJECT_WRITE;
1211 else
Daniel Vettered5982e2013-01-17 22:23:36 +01001212 obj->base.pending_read_domains |= obj->base.read_domains;
1213 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +00001214
Chris Wilson5cf3d282016-08-04 07:52:43 +01001215 i915_vma_move_to_active(vma, req, vma->exec_entry->flags);
Chris Wilsondb53a302011-02-03 11:57:46 +00001216 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +00001217 }
1218}
1219
Chris Wilson54cf91d2010-11-25 18:00:26 +00001220static int
Chris Wilsonb5321f32016-08-02 22:50:18 +01001221i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001222{
Chris Wilson7e37f882016-08-02 22:50:21 +01001223 struct intel_ring *ring = req->ring;
Eric Anholtae662d32012-01-03 09:23:29 -08001224 int ret, i;
1225
Chris Wilsonb5321f32016-08-02 22:50:18 +01001226 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001227 DRM_DEBUG("sol reset is gen7/rcs only\n");
1228 return -EINVAL;
1229 }
Eric Anholtae662d32012-01-03 09:23:29 -08001230
John Harrison5fb9de12015-05-29 17:44:07 +01001231 ret = intel_ring_begin(req, 4 * 3);
Eric Anholtae662d32012-01-03 09:23:29 -08001232 if (ret)
1233 return ret;
1234
1235 for (i = 0; i < 4; i++) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001236 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1237 intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
1238 intel_ring_emit(ring, 0);
Eric Anholtae662d32012-01-03 09:23:29 -08001239 }
1240
Chris Wilsonb5321f32016-08-02 22:50:18 +01001241 intel_ring_advance(ring);
Eric Anholtae662d32012-01-03 09:23:29 -08001242
1243 return 0;
1244}
1245
Chris Wilson59bfa122016-08-04 16:32:31 +01001246static struct i915_vma*
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001247i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
Brad Volkin71745372014-12-11 12:13:12 -08001248 struct drm_i915_gem_exec_object2 *shadow_exec_entry,
Brad Volkin71745372014-12-11 12:13:12 -08001249 struct drm_i915_gem_object *batch_obj,
Chris Wilson59bfa122016-08-04 16:32:31 +01001250 struct eb_vmas *eb,
Brad Volkin71745372014-12-11 12:13:12 -08001251 u32 batch_start_offset,
1252 u32 batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001253 bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001254{
Brad Volkin71745372014-12-11 12:13:12 -08001255 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001256 struct i915_vma *vma;
Brad Volkin71745372014-12-11 12:13:12 -08001257 int ret;
1258
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001259 shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
Chris Wilson17cabf52015-01-14 11:20:57 +00001260 PAGE_ALIGN(batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001261 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001262 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001263
Chris Wilson33a051a2016-07-27 09:07:26 +01001264 ret = intel_engine_cmd_parser(engine,
1265 batch_obj,
1266 shadow_batch_obj,
1267 batch_start_offset,
1268 batch_len,
1269 is_master);
Chris Wilson17cabf52015-01-14 11:20:57 +00001270 if (ret)
1271 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001272
Chris Wilson17cabf52015-01-14 11:20:57 +00001273 ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
1274 if (ret)
1275 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001276
Chris Wilsonde4e7832015-04-07 16:20:35 +01001277 i915_gem_object_unpin_pages(shadow_batch_obj);
1278
Chris Wilson17cabf52015-01-14 11:20:57 +00001279 memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
Brad Volkin71745372014-12-11 12:13:12 -08001280
Chris Wilson17cabf52015-01-14 11:20:57 +00001281 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1282 vma->exec_entry = shadow_exec_entry;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001283 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
Chris Wilson25dc5562016-07-20 13:31:52 +01001284 i915_gem_object_get(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001285 list_add_tail(&vma->exec_list, &eb->vmas);
Brad Volkin71745372014-12-11 12:13:12 -08001286
Chris Wilson59bfa122016-08-04 16:32:31 +01001287 return vma;
Chris Wilson17cabf52015-01-14 11:20:57 +00001288
1289err:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001290 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001291 if (ret == -EACCES) /* unhandled chained batch */
Chris Wilson59bfa122016-08-04 16:32:31 +01001292 return NULL;
Chris Wilson17cabf52015-01-14 11:20:57 +00001293 else
1294 return ERR_PTR(ret);
Brad Volkin71745372014-12-11 12:13:12 -08001295}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001296
Chris Wilson5b043f42016-08-02 22:50:38 +01001297static int
1298execbuf_submit(struct i915_execbuffer_params *params,
1299 struct drm_i915_gem_execbuffer2 *args,
1300 struct list_head *vmas)
Oscar Mateo78382592014-07-03 16:28:05 +01001301{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001302 struct drm_i915_private *dev_priv = params->request->i915;
John Harrison5f19e2b2015-05-29 17:43:27 +01001303 u64 exec_start, exec_len;
Oscar Mateo78382592014-07-03 16:28:05 +01001304 int instp_mode;
1305 u32 instp_mask;
Chris Wilson2f5945b2015-10-06 11:39:55 +01001306 int ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001307
John Harrison535fbe82015-05-29 17:43:32 +01001308 ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
Oscar Mateo78382592014-07-03 16:28:05 +01001309 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001310 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001311
John Harrisonba01cc92015-05-29 17:43:41 +01001312 ret = i915_switch_context(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001313 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001314 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001315
1316 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1317 instp_mask = I915_EXEC_CONSTANTS_MASK;
1318 switch (instp_mode) {
1319 case I915_EXEC_CONSTANTS_REL_GENERAL:
1320 case I915_EXEC_CONSTANTS_ABSOLUTE:
1321 case I915_EXEC_CONSTANTS_REL_SURFACE:
Chris Wilsonb5321f32016-08-02 22:50:18 +01001322 if (instp_mode != 0 && params->engine->id != RCS) {
Oscar Mateo78382592014-07-03 16:28:05 +01001323 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001324 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001325 }
1326
1327 if (instp_mode != dev_priv->relative_constants_mode) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001328 if (INTEL_INFO(dev_priv)->gen < 4) {
Oscar Mateo78382592014-07-03 16:28:05 +01001329 DRM_DEBUG("no rel constants on pre-gen4\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001330 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001331 }
1332
Chris Wilsonb5321f32016-08-02 22:50:18 +01001333 if (INTEL_INFO(dev_priv)->gen > 5 &&
Oscar Mateo78382592014-07-03 16:28:05 +01001334 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1335 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001336 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001337 }
1338
1339 /* The HW changed the meaning on this bit on gen6 */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001340 if (INTEL_INFO(dev_priv)->gen >= 6)
Oscar Mateo78382592014-07-03 16:28:05 +01001341 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1342 }
1343 break;
1344 default:
1345 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001346 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001347 }
1348
Chris Wilsonb5321f32016-08-02 22:50:18 +01001349 if (params->engine->id == RCS &&
Chris Wilson2f5945b2015-10-06 11:39:55 +01001350 instp_mode != dev_priv->relative_constants_mode) {
Chris Wilson7e37f882016-08-02 22:50:21 +01001351 struct intel_ring *ring = params->request->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01001352
John Harrison5fb9de12015-05-29 17:44:07 +01001353 ret = intel_ring_begin(params->request, 4);
Oscar Mateo78382592014-07-03 16:28:05 +01001354 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001355 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001356
Chris Wilsonb5321f32016-08-02 22:50:18 +01001357 intel_ring_emit(ring, MI_NOOP);
1358 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1359 intel_ring_emit_reg(ring, INSTPM);
1360 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1361 intel_ring_advance(ring);
Oscar Mateo78382592014-07-03 16:28:05 +01001362
1363 dev_priv->relative_constants_mode = instp_mode;
1364 }
1365
1366 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001367 ret = i915_reset_gen7_sol_offsets(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001368 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001369 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001370 }
1371
John Harrison5f19e2b2015-05-29 17:43:27 +01001372 exec_len = args->batch_len;
Chris Wilson59bfa122016-08-04 16:32:31 +01001373 exec_start = params->batch->node.start +
John Harrison5f19e2b2015-05-29 17:43:27 +01001374 params->args_batch_start_offset;
1375
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001376 if (exec_len == 0)
Chris Wilson59bfa122016-08-04 16:32:31 +01001377 exec_len = params->batch->size;
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001378
Chris Wilson803688b2016-08-02 22:50:27 +01001379 ret = params->engine->emit_bb_start(params->request,
1380 exec_start, exec_len,
1381 params->dispatch_flags);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001382 if (ret)
1383 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001384
John Harrison95c24162015-05-29 17:43:31 +01001385 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001386
John Harrison8a8edb52015-05-29 17:43:33 +01001387 i915_gem_execbuffer_move_to_active(vmas, params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001388
Chris Wilson2f5945b2015-10-06 11:39:55 +01001389 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001390}
1391
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001392/**
1393 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01001394 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001395 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001396static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01001397gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
1398 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001399{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001400 struct drm_i915_file_private *file_priv = file->driver_priv;
1401
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001402 /* Check whether the file_priv has already selected one ring. */
Chris Wilsonc80ff162016-07-27 09:07:27 +01001403 if ((int)file_priv->bsd_engine < 0) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001404 /* If not, use the ping-pong mechanism to select one. */
Chris Wilson91c8a322016-07-05 10:40:23 +01001405 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonc80ff162016-07-27 09:07:27 +01001406 file_priv->bsd_engine = dev_priv->mm.bsd_engine_dispatch_index;
1407 dev_priv->mm.bsd_engine_dispatch_index ^= 1;
Chris Wilson91c8a322016-07-05 10:40:23 +01001408 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001409 }
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001410
Chris Wilsonc80ff162016-07-27 09:07:27 +01001411 return file_priv->bsd_engine;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001412}
1413
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001414#define I915_USER_RINGS (4)
1415
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00001416static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001417 [I915_EXEC_DEFAULT] = RCS,
1418 [I915_EXEC_RENDER] = RCS,
1419 [I915_EXEC_BLT] = BCS,
1420 [I915_EXEC_BSD] = VCS,
1421 [I915_EXEC_VEBOX] = VECS
1422};
1423
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001424static struct intel_engine_cs *
1425eb_select_engine(struct drm_i915_private *dev_priv,
1426 struct drm_file *file,
1427 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001428{
1429 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001430 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001431
1432 if (user_ring_id > I915_USER_RINGS) {
1433 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001434 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001435 }
1436
1437 if ((user_ring_id != I915_EXEC_BSD) &&
1438 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1439 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1440 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001441 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001442 }
1443
1444 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
1445 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
1446
1447 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01001448 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001449 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
1450 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00001451 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001452 bsd_idx--;
1453 } else {
1454 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
1455 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001456 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001457 }
1458
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001459 engine = &dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001460 } else {
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001461 engine = &dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001462 }
1463
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001464 if (!intel_engine_initialized(engine)) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001465 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001466 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001467 }
1468
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001469 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001470}
1471
Eric Anholtae662d32012-01-03 09:23:29 -08001472static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001473i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1474 struct drm_file *file,
1475 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001476 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001477{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001478 struct drm_i915_private *dev_priv = to_i915(dev);
1479 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky27173f12013-08-14 11:38:36 +02001480 struct eb_vmas *eb;
Brad Volkin78a42372014-12-11 12:13:09 -08001481 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001482 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +01001483 struct i915_gem_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001484 struct i915_address_space *vm;
John Harrison5f19e2b2015-05-29 17:43:27 +01001485 struct i915_execbuffer_params params_master; /* XXX: will be removed later */
1486 struct i915_execbuffer_params *params = &params_master;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001487 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
John Harrison8e004ef2015-02-13 11:48:10 +00001488 u32 dispatch_flags;
Oscar Mateo78382592014-07-03 16:28:05 +01001489 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001490 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001491
Daniel Vettered5982e2013-01-17 22:23:36 +01001492 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001493 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001494
Chris Wilsonad19f102014-08-10 06:29:08 +01001495 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001496 if (ret)
1497 return ret;
1498
John Harrison8e004ef2015-02-13 11:48:10 +00001499 dispatch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001500 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02001501 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001502 return -EPERM;
1503
John Harrison8e004ef2015-02-13 11:48:10 +00001504 dispatch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001505 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001506 if (args->flags & I915_EXEC_IS_PINNED)
John Harrison8e004ef2015-02-13 11:48:10 +00001507 dispatch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001508
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001509 engine = eb_select_engine(dev_priv, file, args);
1510 if (!engine)
1511 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001512
1513 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001514 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001515 return -EINVAL;
1516 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001517
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001518 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
1519 if (!HAS_RESOURCE_STREAMER(dev)) {
1520 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
1521 return -EINVAL;
1522 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001523 if (engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001524 DRM_DEBUG("RS is not available on %s\n",
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001525 engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001526 return -EINVAL;
1527 }
1528
1529 dispatch_flags |= I915_DISPATCH_RS;
1530 }
1531
Chris Wilson67d97da2016-07-04 08:08:31 +01001532 /* Take a local wakeref for preparing to dispatch the execbuf as
1533 * we expect to access the hardware fairly frequently in the
1534 * process. Upon first dispatch, we acquire another prolonged
1535 * wakeref that we hold until the GPU has been idle for at least
1536 * 100ms.
1537 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001538 intel_runtime_pm_get(dev_priv);
1539
Chris Wilson54cf91d2010-11-25 18:00:26 +00001540 ret = i915_mutex_lock_interruptible(dev);
1541 if (ret)
1542 goto pre_mutex_err;
1543
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001544 ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001545 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001546 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001547 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001548 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001549 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001550
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001551 i915_gem_context_get(ctx);
Ben Widawsky41bde552013-12-06 14:11:21 -08001552
Daniel Vetterae6c4802014-08-06 15:04:53 +02001553 if (ctx->ppgtt)
1554 vm = &ctx->ppgtt->base;
1555 else
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001556 vm = &ggtt->base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001557
John Harrison5f19e2b2015-05-29 17:43:27 +01001558 memset(&params_master, 0x00, sizeof(params_master));
1559
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001560 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001561 if (eb == NULL) {
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001562 i915_gem_context_put(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001563 mutex_unlock(&dev->struct_mutex);
1564 ret = -ENOMEM;
1565 goto pre_mutex_err;
1566 }
1567
Chris Wilson54cf91d2010-11-25 18:00:26 +00001568 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001569 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001570 if (ret)
1571 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001572
Chris Wilson6fe4f142011-01-10 17:35:37 +00001573 /* take note of the batch buffer before we might reorder the lists */
Chris Wilson59bfa122016-08-04 16:32:31 +01001574 params->batch = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001575
Chris Wilson54cf91d2010-11-25 18:00:26 +00001576 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001577 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001578 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
1579 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001580 if (ret)
1581 goto err;
1582
1583 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001584 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001585 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001586 if (ret) {
1587 if (ret == -EFAULT) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001588 ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
1589 engine,
David Weinehallb1b38272015-05-20 17:00:13 +03001590 eb, exec, ctx);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001591 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1592 }
1593 if (ret)
1594 goto err;
1595 }
1596
1597 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson59bfa122016-08-04 16:32:31 +01001598 if (params->batch->obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001599 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001600 ret = -EINVAL;
1601 goto err;
1602 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001603
John Harrison5f19e2b2015-05-29 17:43:27 +01001604 params->args_batch_start_offset = args->batch_start_offset;
Chris Wilson33a051a2016-07-27 09:07:26 +01001605 if (intel_engine_needs_cmd_parser(engine) && args->batch_len) {
Chris Wilson59bfa122016-08-04 16:32:31 +01001606 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001607
Chris Wilson59bfa122016-08-04 16:32:31 +01001608 vma = i915_gem_execbuffer_parse(engine, &shadow_exec_entry,
1609 params->batch->obj,
1610 eb,
1611 args->batch_start_offset,
1612 args->batch_len,
1613 drm_is_current_master(file));
1614 if (IS_ERR(vma)) {
1615 ret = PTR_ERR(vma);
Brad Volkin78a42372014-12-11 12:13:09 -08001616 goto err;
1617 }
Chris Wilson17cabf52015-01-14 11:20:57 +00001618
Chris Wilson59bfa122016-08-04 16:32:31 +01001619 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001620 /*
1621 * Batch parsed and accepted:
1622 *
1623 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1624 * bit from MI_BATCH_BUFFER_START commands issued in
1625 * the dispatch_execbuffer implementations. We
1626 * specifically don't want that set on batches the
1627 * command parser has accepted.
1628 */
1629 dispatch_flags |= I915_DISPATCH_SECURE;
John Harrison5f19e2b2015-05-29 17:43:27 +01001630 params->args_batch_start_offset = 0;
Chris Wilson59bfa122016-08-04 16:32:31 +01001631 params->batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001632 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001633 }
1634
Chris Wilson59bfa122016-08-04 16:32:31 +01001635 params->batch->obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Brad Volkin78a42372014-12-11 12:13:09 -08001636
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001637 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1638 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001639 * hsw should have this fixed, but bdw mucks it up again. */
John Harrison8e004ef2015-02-13 11:48:10 +00001640 if (dispatch_flags & I915_DISPATCH_SECURE) {
Chris Wilson59bfa122016-08-04 16:32:31 +01001641 struct drm_i915_gem_object *obj = params->batch->obj;
1642
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001643 /*
1644 * So on first glance it looks freaky that we pin the batch here
1645 * outside of the reservation loop. But:
1646 * - The batch is already pinned into the relevant ppgtt, so we
1647 * already have the backing storage fully allocated.
1648 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01001649 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001650 * fitting due to fragmentation.
1651 * So this is actually safe.
1652 */
Chris Wilson59bfa122016-08-04 16:32:31 +01001653 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001654 if (ret)
1655 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001656
Chris Wilson59bfa122016-08-04 16:32:31 +01001657 params->batch = i915_gem_obj_to_ggtt(obj);
1658 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001659
John Harrison0c8dac82015-05-29 17:43:25 +01001660 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson8e637172016-08-02 22:50:26 +01001661 params->request = i915_gem_request_alloc(engine, ctx);
1662 if (IS_ERR(params->request)) {
1663 ret = PTR_ERR(params->request);
John Harrison0c8dac82015-05-29 17:43:25 +01001664 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00001665 }
John Harrison0c8dac82015-05-29 17:43:25 +01001666
Chris Wilson8e637172016-08-02 22:50:26 +01001667 ret = i915_gem_request_add_to_client(params->request, file);
John Harrisonfcfa423c2015-05-29 17:44:12 +01001668 if (ret)
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001669 goto err_request;
John Harrisonfcfa423c2015-05-29 17:44:12 +01001670
John Harrison5f19e2b2015-05-29 17:43:27 +01001671 /*
1672 * Save assorted stuff away to pass through to *_submission().
1673 * NB: This data should be 'persistent' and not local as it will
1674 * kept around beyond the duration of the IOCTL once the GPU
1675 * scheduler arrives.
1676 */
1677 params->dev = dev;
1678 params->file = file;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001679 params->engine = engine;
John Harrison5f19e2b2015-05-29 17:43:27 +01001680 params->dispatch_flags = dispatch_flags;
John Harrison5f19e2b2015-05-29 17:43:27 +01001681 params->ctx = ctx;
1682
Chris Wilson5b043f42016-08-02 22:50:38 +01001683 ret = execbuf_submit(params, args, &eb->vmas);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001684err_request:
Chris Wilson59bfa122016-08-04 16:32:31 +01001685 __i915_add_request(params->request, params->batch->obj, ret == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001686
John Harrison0c8dac82015-05-29 17:43:25 +01001687err_batch_unpin:
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001688 /*
1689 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1690 * batch vma for correctness. For less ugly and less fragility this
1691 * needs to be adjusted to also track the ggtt batch vma properly as
1692 * active.
1693 */
John Harrison8e004ef2015-02-13 11:48:10 +00001694 if (dispatch_flags & I915_DISPATCH_SECURE)
Chris Wilson59bfa122016-08-04 16:32:31 +01001695 i915_vma_unpin(params->batch);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001696err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001697 /* the request owns the ref now */
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001698 i915_gem_context_put(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001699 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001700
1701 mutex_unlock(&dev->struct_mutex);
1702
1703pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001704 /* intel_gpu_busy should also get a ref, so it will free when the device
1705 * is really idle. */
1706 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001707 return ret;
1708}
1709
1710/*
1711 * Legacy execbuffer just creates an exec2 list from the original exec object
1712 * list array and passes it to the real function.
1713 */
1714int
1715i915_gem_execbuffer(struct drm_device *dev, void *data,
1716 struct drm_file *file)
1717{
1718 struct drm_i915_gem_execbuffer *args = data;
1719 struct drm_i915_gem_execbuffer2 exec2;
1720 struct drm_i915_gem_exec_object *exec_list = NULL;
1721 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1722 int ret, i;
1723
Chris Wilson54cf91d2010-11-25 18:00:26 +00001724 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001725 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001726 return -EINVAL;
1727 }
1728
1729 /* Copy in the exec list from userland */
1730 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1731 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1732 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001733 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001734 args->buffer_count);
1735 drm_free_large(exec_list);
1736 drm_free_large(exec2_list);
1737 return -ENOMEM;
1738 }
1739 ret = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001740 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001741 sizeof(*exec_list) * args->buffer_count);
1742 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001743 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001744 args->buffer_count, ret);
1745 drm_free_large(exec_list);
1746 drm_free_large(exec2_list);
1747 return -EFAULT;
1748 }
1749
1750 for (i = 0; i < args->buffer_count; i++) {
1751 exec2_list[i].handle = exec_list[i].handle;
1752 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1753 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1754 exec2_list[i].alignment = exec_list[i].alignment;
1755 exec2_list[i].offset = exec_list[i].offset;
1756 if (INTEL_INFO(dev)->gen < 4)
1757 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1758 else
1759 exec2_list[i].flags = 0;
1760 }
1761
1762 exec2.buffers_ptr = args->buffers_ptr;
1763 exec2.buffer_count = args->buffer_count;
1764 exec2.batch_start_offset = args->batch_start_offset;
1765 exec2.batch_len = args->batch_len;
1766 exec2.DR1 = args->DR1;
1767 exec2.DR4 = args->DR4;
1768 exec2.num_cliprects = args->num_cliprects;
1769 exec2.cliprects_ptr = args->cliprects_ptr;
1770 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001771 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001772
Ben Widawsky41bde552013-12-06 14:11:21 -08001773 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001774 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001775 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001776 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001777
Chris Wilson54cf91d2010-11-25 18:00:26 +00001778 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001779 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001780 exec2_list[i].offset =
1781 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001782 ret = __copy_to_user(&user_exec_list[i].offset,
1783 &exec2_list[i].offset,
1784 sizeof(user_exec_list[i].offset));
1785 if (ret) {
1786 ret = -EFAULT;
1787 DRM_DEBUG("failed to copy %d exec entries "
1788 "back to user (%d)\n",
1789 args->buffer_count, ret);
1790 break;
1791 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001792 }
1793 }
1794
1795 drm_free_large(exec_list);
1796 drm_free_large(exec2_list);
1797 return ret;
1798}
1799
1800int
1801i915_gem_execbuffer2(struct drm_device *dev, void *data,
1802 struct drm_file *file)
1803{
1804 struct drm_i915_gem_execbuffer2 *args = data;
1805 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1806 int ret;
1807
Xi Wanged8cd3b2012-04-23 04:06:41 -04001808 if (args->buffer_count < 1 ||
1809 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001810 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001811 return -EINVAL;
1812 }
1813
Daniel Vetter9cb34662014-04-24 08:09:11 +02001814 if (args->rsvd2 != 0) {
1815 DRM_DEBUG("dirty rvsd2 field\n");
1816 return -EINVAL;
1817 }
1818
Chris Wilsonf2a85e12016-04-08 12:11:13 +01001819 exec2_list = drm_malloc_gfp(args->buffer_count,
1820 sizeof(*exec2_list),
1821 GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001822 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001823 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001824 args->buffer_count);
1825 return -ENOMEM;
1826 }
1827 ret = copy_from_user(exec2_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001828 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001829 sizeof(*exec2_list) * args->buffer_count);
1830 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001831 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001832 args->buffer_count, ret);
1833 drm_free_large(exec2_list);
1834 return -EFAULT;
1835 }
1836
Ben Widawsky41bde552013-12-06 14:11:21 -08001837 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001838 if (!ret) {
1839 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001840 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001841 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001842 int i;
1843
1844 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001845 exec2_list[i].offset =
1846 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001847 ret = __copy_to_user(&user_exec_list[i].offset,
1848 &exec2_list[i].offset,
1849 sizeof(user_exec_list[i].offset));
1850 if (ret) {
1851 ret = -EFAULT;
1852 DRM_DEBUG("failed to copy %d exec entries "
1853 "back to user\n",
1854 args->buffer_count);
1855 break;
1856 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001857 }
1858 }
1859
1860 drm_free_large(exec2_list);
1861 return ret;
1862}