blob: d95a0432678f4460f90beefa73f85a45f79d6040 [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 Wilson596c5922016-02-26 11:03:20 +0000720 if (!vma->is_ggtt)
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 Wilson596c5922016-02-26 11:03:20 +0000739 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP && !vma->is_ggtt);
Chris Wilsond23db882014-05-23 08:48:08 +0200740
741 if (entry->alignment &&
742 vma->node.start & (entry->alignment - 1))
743 return true;
744
Chris Wilson91b2db62016-08-04 16:32:23 +0100745 if (vma->node.size < entry->pad_to_size)
746 return true;
747
Chris Wilson506a8e82015-12-08 11:55:07 +0000748 if (entry->flags & EXEC_OBJECT_PINNED &&
749 vma->node.start != entry->offset)
750 return true;
751
Chris Wilsond23db882014-05-23 08:48:08 +0200752 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
753 vma->node.start < BATCH_OFFSET_BIAS)
754 return true;
755
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000756 /* avoid costly ping-pong once a batch bo ended up non-mappable */
757 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
758 return !only_mappable_for_reloc(entry->flags);
759
Michel Thierry101b5062015-10-01 13:33:57 +0100760 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
761 (vma->node.start + vma->node.size - 1) >> 32)
762 return true;
763
Chris Wilsond23db882014-05-23 08:48:08 +0200764 return false;
765}
766
Chris Wilson54cf91d2010-11-25 18:00:26 +0000767static int
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000768i915_gem_execbuffer_reserve(struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200769 struct list_head *vmas,
Chris Wilsone2efd132016-05-24 14:53:34 +0100770 struct i915_gem_context *ctx,
Daniel Vettered5982e2013-01-17 22:23:36 +0100771 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000772{
Chris Wilson432e58e2010-11-25 19:32:06 +0000773 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200774 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700775 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200776 struct list_head ordered_vmas;
Chris Wilson506a8e82015-12-08 11:55:07 +0000777 struct list_head pinned_vmas;
Chris Wilsonc0336662016-05-06 15:40:21 +0100778 bool has_fenced_gpu_access = INTEL_GEN(engine->i915) < 4;
Chris Wilson7788a762012-08-24 19:18:18 +0100779 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000780
Ben Widawsky68c8c172013-09-11 14:57:50 -0700781 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
782
Ben Widawsky27173f12013-08-14 11:38:36 +0200783 INIT_LIST_HEAD(&ordered_vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000784 INIT_LIST_HEAD(&pinned_vmas);
Ben Widawsky27173f12013-08-14 11:38:36 +0200785 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000786 struct drm_i915_gem_exec_object2 *entry;
787 bool need_fence, need_mappable;
788
Ben Widawsky27173f12013-08-14 11:38:36 +0200789 vma = list_first_entry(vmas, struct i915_vma, exec_list);
790 obj = vma->obj;
791 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000792
David Weinehallb1b38272015-05-20 17:00:13 +0300793 if (ctx->flags & CONTEXT_NO_ZEROMAP)
794 entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
795
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100796 if (!has_fenced_gpu_access)
797 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000798 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000799 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
800 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200801 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000802
Chris Wilson506a8e82015-12-08 11:55:07 +0000803 if (entry->flags & EXEC_OBJECT_PINNED)
804 list_move_tail(&vma->exec_list, &pinned_vmas);
805 else if (need_mappable) {
Chris Wilsone6a84462014-08-11 12:00:12 +0200806 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200807 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200808 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200809 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000810
Daniel Vettered5982e2013-01-17 22:23:36 +0100811 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000812 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000813 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200814 list_splice(&ordered_vmas, vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000815 list_splice(&pinned_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000816
817 /* Attempt to pin all of the buffers into the GTT.
818 * This is done in 3 phases:
819 *
820 * 1a. Unbind all objects that do not match the GTT constraints for
821 * the execbuffer (fenceable, mappable, alignment etc).
822 * 1b. Increment pin count for already bound objects.
823 * 2. Bind new objects.
824 * 3. Decrement pin count.
825 *
Chris Wilson7788a762012-08-24 19:18:18 +0100826 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000827 * room for the earlier objects *unless* we need to defragment.
828 */
829 retry = 0;
830 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100831 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000832
833 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200834 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200835 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000836 continue;
837
Chris Wilsone6a84462014-08-11 12:00:12 +0200838 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200839 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000840 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000841 ret = i915_gem_execbuffer_reserve_vma(vma,
842 engine,
843 need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000844 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000845 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000846 }
847
848 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200849 list_for_each_entry(vma, vmas, exec_list) {
850 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100851 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000852
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000853 ret = i915_gem_execbuffer_reserve_vma(vma, engine,
854 need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100855 if (ret)
856 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000857 }
858
Chris Wilsona415d352013-11-26 11:23:15 +0000859err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200860 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000861 return ret;
862
Chris Wilsona415d352013-11-26 11:23:15 +0000863 /* Decrement pin count for bound objects */
864 list_for_each_entry(vma, vmas, exec_list)
865 i915_gem_execbuffer_unreserve_vma(vma);
866
Ben Widawsky68c8c172013-09-11 14:57:50 -0700867 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000868 if (ret)
869 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000870 } while (1);
871}
872
873static int
874i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100875 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000876 struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000877 struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200878 struct eb_vmas *eb,
David Weinehallb1b38272015-05-20 17:00:13 +0300879 struct drm_i915_gem_exec_object2 *exec,
Chris Wilsone2efd132016-05-24 14:53:34 +0100880 struct i915_gem_context *ctx)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000881{
882 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200883 struct i915_address_space *vm;
884 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100885 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000886 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000887 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200888 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000889
Ben Widawsky27173f12013-08-14 11:38:36 +0200890 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
891
Chris Wilson67731b82010-12-08 10:38:14 +0000892 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200893 while (!list_empty(&eb->vmas)) {
894 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
895 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000896 i915_gem_execbuffer_unreserve_vma(vma);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100897 i915_gem_object_put(vma->obj);
Chris Wilson67731b82010-12-08 10:38:14 +0000898 }
899
Chris Wilson54cf91d2010-11-25 18:00:26 +0000900 mutex_unlock(&dev->struct_mutex);
901
902 total = 0;
903 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000904 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000905
Chris Wilsondd6864a2011-01-12 23:49:13 +0000906 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000907 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000908 if (reloc == NULL || reloc_offset == NULL) {
909 drm_free_large(reloc);
910 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000911 mutex_lock(&dev->struct_mutex);
912 return -ENOMEM;
913 }
914
915 total = 0;
916 for (i = 0; i < count; i++) {
917 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000918 u64 invalid_offset = (u64)-1;
919 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000920
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300921 user_relocs = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000922
923 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000924 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000925 ret = -EFAULT;
926 mutex_lock(&dev->struct_mutex);
927 goto err;
928 }
929
Chris Wilson262b6d32013-01-15 16:17:54 +0000930 /* As we do not update the known relocation offsets after
931 * relocating (due to the complexities in lock handling),
932 * we need to mark them as invalid now so that we force the
933 * relocation processing next time. Just in case the target
934 * object is evicted and then rebound into its old
935 * presumed_offset before the next execbuffer - if that
936 * happened we would make the mistake of assuming that the
937 * relocations were valid.
938 */
939 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100940 if (__copy_to_user(&user_relocs[j].presumed_offset,
941 &invalid_offset,
942 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000943 ret = -EFAULT;
944 mutex_lock(&dev->struct_mutex);
945 goto err;
946 }
947 }
948
Chris Wilsondd6864a2011-01-12 23:49:13 +0000949 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000950 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000951 }
952
953 ret = i915_mutex_lock_interruptible(dev);
954 if (ret) {
955 mutex_lock(&dev->struct_mutex);
956 goto err;
957 }
958
Chris Wilson67731b82010-12-08 10:38:14 +0000959 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000960 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200961 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000962 if (ret)
963 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000964
Daniel Vettered5982e2013-01-17 22:23:36 +0100965 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000966 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
967 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000968 if (ret)
969 goto err;
970
Ben Widawsky27173f12013-08-14 11:38:36 +0200971 list_for_each_entry(vma, &eb->vmas, exec_list) {
972 int offset = vma->exec_entry - exec;
973 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
974 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000975 if (ret)
976 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000977 }
978
979 /* Leave the user relocations as are, this is the painfully slow path,
980 * and we want to avoid the complication of dropping the lock whilst
981 * having buffers reserved in the aperture and so causing spurious
982 * ENOSPC for random operations.
983 */
984
985err:
986 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000987 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000988 return ret;
989}
990
Chris Wilson54cf91d2010-11-25 18:00:26 +0000991static int
John Harrison535fbe82015-05-29 17:43:32 +0100992i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
Ben Widawsky27173f12013-08-14 11:38:36 +0200993 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000994{
Tvrtko Ursulin666796d2016-03-16 11:00:39 +0000995 const unsigned other_rings = ~intel_engine_flag(req->engine);
Ben Widawsky27173f12013-08-14 11:38:36 +0200996 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200997 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100998 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000999 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001000
Ben Widawsky27173f12013-08-14 11:38:36 +02001001 list_for_each_entry(vma, vmas, exec_list) {
1002 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +01001003
1004 if (obj->active & other_rings) {
Chris Wilson8e637172016-08-02 22:50:26 +01001005 ret = i915_gem_object_sync(obj, req);
Chris Wilson03ade512015-04-27 13:41:18 +01001006 if (ret)
1007 return ret;
1008 }
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001009
1010 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +01001011 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001012
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001013 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001014 }
1015
Chris Wilson000433b2013-08-08 14:41:09 +01001016 if (flush_chipset)
Chris Wilsonc0336662016-05-06 15:40:21 +01001017 i915_gem_chipset_flush(req->engine->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +02001018
1019 if (flush_domains & I915_GEM_DOMAIN_GTT)
1020 wmb();
1021
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001022 /* Unconditionally invalidate GPU caches and TLBs. */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001023 return req->engine->emit_flush(req, EMIT_INVALIDATE);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001024}
1025
Chris Wilson432e58e2010-11-25 19:32:06 +00001026static bool
1027i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001028{
Daniel Vettered5982e2013-01-17 22:23:36 +01001029 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
1030 return false;
1031
Chris Wilson2f5945b2015-10-06 11:39:55 +01001032 /* Kernel clipping was a DRI1 misfeature */
1033 if (exec->num_cliprects || exec->cliprects_ptr)
1034 return false;
1035
1036 if (exec->DR4 == 0xffffffff) {
1037 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1038 exec->DR4 = 0;
1039 }
1040 if (exec->DR1 || exec->DR4)
1041 return false;
1042
1043 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1044 return false;
1045
1046 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001047}
1048
1049static int
Chris Wilsonad19f102014-08-10 06:29:08 +01001050validate_exec_list(struct drm_device *dev,
1051 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001052 int count)
1053{
Daniel Vetterb205ca52013-09-19 14:00:11 +02001054 unsigned relocs_total = 0;
1055 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +01001056 unsigned invalid_flags;
1057 int i;
1058
Dave Gordon9e2793f62016-07-14 14:52:03 +01001059 /* INTERNAL flags must not overlap with external ones */
1060 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
1061
Chris Wilsonad19f102014-08-10 06:29:08 +01001062 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
1063 if (USES_FULL_PPGTT(dev))
1064 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001065
1066 for (i = 0; i < count; i++) {
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001067 char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001068 int length; /* limited by fault_in_pages_readable() */
1069
Chris Wilsonad19f102014-08-10 06:29:08 +01001070 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +01001071 return -EINVAL;
1072
Michał Winiarski934acce2015-12-29 18:24:52 +01001073 /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
1074 * any non-page-aligned or non-canonical addresses.
1075 */
1076 if (exec[i].flags & EXEC_OBJECT_PINNED) {
1077 if (exec[i].offset !=
1078 gen8_canonical_addr(exec[i].offset & PAGE_MASK))
1079 return -EINVAL;
1080
1081 /* From drm_mm perspective address space is continuous,
1082 * so from this point we're always using non-canonical
1083 * form internally.
1084 */
1085 exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
1086 }
1087
Chris Wilson55a97852015-06-19 13:59:46 +01001088 if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
1089 return -EINVAL;
1090
Chris Wilson91b2db62016-08-04 16:32:23 +01001091 /* pad_to_size was once a reserved field, so sanitize it */
1092 if (exec[i].flags & EXEC_OBJECT_PAD_TO_SIZE) {
1093 if (offset_in_page(exec[i].pad_to_size))
1094 return -EINVAL;
1095 } else {
1096 exec[i].pad_to_size = 0;
1097 }
1098
Kees Cook3118a4f2013-03-11 17:31:45 -07001099 /* First check for malicious input causing overflow in
1100 * the worst case where we need to allocate the entire
1101 * relocation tree as a single array.
1102 */
1103 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001104 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -07001105 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001106
1107 length = exec[i].relocation_count *
1108 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -07001109 /*
1110 * We must check that the entire relocation array is safe
1111 * to read, but since we may need to update the presumed
1112 * offsets during execution, check for full write access.
1113 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001114 if (!access_ok(VERIFY_WRITE, ptr, length))
1115 return -EFAULT;
1116
Jani Nikulad330a952014-01-21 11:24:25 +02001117 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +08001118 if (fault_in_multipages_readable(ptr, length))
1119 return -EFAULT;
1120 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001121 }
1122
1123 return 0;
1124}
1125
Chris Wilsone2efd132016-05-24 14:53:34 +01001126static struct i915_gem_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001127i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001128 struct intel_engine_cs *engine, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001129{
Chris Wilsone2efd132016-05-24 14:53:34 +01001130 struct i915_gem_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001131 struct i915_ctx_hang_stats *hs;
1132
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001133 if (engine->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001134 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001135
Chris Wilsonca585b52016-05-24 14:53:36 +01001136 ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001137 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -08001138 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001139
Ben Widawsky41bde552013-12-06 14:11:21 -08001140 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001141 if (hs->banned) {
1142 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -08001143 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001144 }
1145
Ben Widawsky41bde552013-12-06 14:11:21 -08001146 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001147}
1148
Chris Wilson5cf3d282016-08-04 07:52:43 +01001149void i915_vma_move_to_active(struct i915_vma *vma,
1150 struct drm_i915_gem_request *req,
1151 unsigned int flags)
1152{
1153 struct drm_i915_gem_object *obj = vma->obj;
1154 const unsigned int idx = req->engine->id;
1155
1156 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
1157
1158 obj->dirty = 1; /* be paranoid */
1159
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001160 /* Add a reference if we're newly entering the active list.
1161 * The order in which we add operations to the retirement queue is
1162 * vital here: mark_active adds to the start of the callback list,
1163 * such that subsequent callbacks are called first. Therefore we
1164 * add the active reference first and queue for it to be dropped
1165 * *last*.
1166 */
Chris Wilson5cf3d282016-08-04 07:52:43 +01001167 if (obj->active == 0)
1168 i915_gem_object_get(obj);
1169 obj->active |= 1 << idx;
1170 i915_gem_active_set(&obj->last_read[idx], req);
1171
1172 if (flags & EXEC_OBJECT_WRITE) {
1173 i915_gem_active_set(&obj->last_write, req);
1174
1175 intel_fb_obj_invalidate(obj, ORIGIN_CS);
1176
1177 /* update for the implicit flush after a batch */
1178 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1179 }
1180
1181 if (flags & EXEC_OBJECT_NEEDS_FENCE) {
1182 i915_gem_active_set(&obj->last_fence, req);
1183 if (flags & __EXEC_OBJECT_HAS_FENCE) {
1184 struct drm_i915_private *dev_priv = req->i915;
1185
1186 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
1187 &dev_priv->mm.fence_list);
1188 }
1189 }
1190
Chris Wilsonb0decaf2016-08-04 07:52:44 +01001191 i915_vma_set_active(vma, idx);
1192 i915_gem_active_set(&vma->last_read[idx], req);
Chris Wilson5cf3d282016-08-04 07:52:43 +01001193 list_move_tail(&vma->vm_link, &vma->vm->active_list);
1194}
1195
Chris Wilson5b043f42016-08-02 22:50:38 +01001196static void
Ben Widawsky27173f12013-08-14 11:38:36 +02001197i915_gem_execbuffer_move_to_active(struct list_head *vmas,
John Harrison8a8edb52015-05-29 17:43:33 +01001198 struct drm_i915_gem_request *req)
Chris Wilson432e58e2010-11-25 19:32:06 +00001199{
Ben Widawsky27173f12013-08-14 11:38:36 +02001200 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +00001201
Ben Widawsky27173f12013-08-14 11:38:36 +02001202 list_for_each_entry(vma, vmas, exec_list) {
1203 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +01001204 u32 old_read = obj->base.read_domains;
1205 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +00001206
Chris Wilson432e58e2010-11-25 19:32:06 +00001207 obj->base.write_domain = obj->base.pending_write_domain;
Chris Wilson5cf3d282016-08-04 07:52:43 +01001208 if (obj->base.write_domain)
1209 vma->exec_entry->flags |= EXEC_OBJECT_WRITE;
1210 else
Daniel Vettered5982e2013-01-17 22:23:36 +01001211 obj->base.pending_read_domains |= obj->base.read_domains;
1212 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +00001213
Chris Wilson5cf3d282016-08-04 07:52:43 +01001214 i915_vma_move_to_active(vma, req, vma->exec_entry->flags);
Chris Wilsondb53a302011-02-03 11:57:46 +00001215 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +00001216 }
1217}
1218
Chris Wilson54cf91d2010-11-25 18:00:26 +00001219static int
Chris Wilsonb5321f32016-08-02 22:50:18 +01001220i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001221{
Chris Wilson7e37f882016-08-02 22:50:21 +01001222 struct intel_ring *ring = req->ring;
Eric Anholtae662d32012-01-03 09:23:29 -08001223 int ret, i;
1224
Chris Wilsonb5321f32016-08-02 22:50:18 +01001225 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001226 DRM_DEBUG("sol reset is gen7/rcs only\n");
1227 return -EINVAL;
1228 }
Eric Anholtae662d32012-01-03 09:23:29 -08001229
John Harrison5fb9de12015-05-29 17:44:07 +01001230 ret = intel_ring_begin(req, 4 * 3);
Eric Anholtae662d32012-01-03 09:23:29 -08001231 if (ret)
1232 return ret;
1233
1234 for (i = 0; i < 4; i++) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001235 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1236 intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
1237 intel_ring_emit(ring, 0);
Eric Anholtae662d32012-01-03 09:23:29 -08001238 }
1239
Chris Wilsonb5321f32016-08-02 22:50:18 +01001240 intel_ring_advance(ring);
Eric Anholtae662d32012-01-03 09:23:29 -08001241
1242 return 0;
1243}
1244
Chris Wilson59bfa122016-08-04 16:32:31 +01001245static struct i915_vma*
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001246i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
Brad Volkin71745372014-12-11 12:13:12 -08001247 struct drm_i915_gem_exec_object2 *shadow_exec_entry,
Brad Volkin71745372014-12-11 12:13:12 -08001248 struct drm_i915_gem_object *batch_obj,
Chris Wilson59bfa122016-08-04 16:32:31 +01001249 struct eb_vmas *eb,
Brad Volkin71745372014-12-11 12:13:12 -08001250 u32 batch_start_offset,
1251 u32 batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001252 bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001253{
Brad Volkin71745372014-12-11 12:13:12 -08001254 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001255 struct i915_vma *vma;
Brad Volkin71745372014-12-11 12:13:12 -08001256 int ret;
1257
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001258 shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
Chris Wilson17cabf52015-01-14 11:20:57 +00001259 PAGE_ALIGN(batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001260 if (IS_ERR(shadow_batch_obj))
Chris Wilson59bfa122016-08-04 16:32:31 +01001261 return ERR_CAST(shadow_batch_obj);
Brad Volkin71745372014-12-11 12:13:12 -08001262
Chris Wilson33a051a2016-07-27 09:07:26 +01001263 ret = intel_engine_cmd_parser(engine,
1264 batch_obj,
1265 shadow_batch_obj,
1266 batch_start_offset,
1267 batch_len,
1268 is_master);
Chris Wilson17cabf52015-01-14 11:20:57 +00001269 if (ret)
1270 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001271
Chris Wilson17cabf52015-01-14 11:20:57 +00001272 ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
1273 if (ret)
1274 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001275
Chris Wilsonde4e7832015-04-07 16:20:35 +01001276 i915_gem_object_unpin_pages(shadow_batch_obj);
1277
Chris Wilson17cabf52015-01-14 11:20:57 +00001278 memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
Brad Volkin71745372014-12-11 12:13:12 -08001279
Chris Wilson17cabf52015-01-14 11:20:57 +00001280 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1281 vma->exec_entry = shadow_exec_entry;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001282 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
Chris Wilson25dc5562016-07-20 13:31:52 +01001283 i915_gem_object_get(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001284 list_add_tail(&vma->exec_list, &eb->vmas);
Brad Volkin71745372014-12-11 12:13:12 -08001285
Chris Wilson59bfa122016-08-04 16:32:31 +01001286 return vma;
Chris Wilson17cabf52015-01-14 11:20:57 +00001287
1288err:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001289 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001290 if (ret == -EACCES) /* unhandled chained batch */
Chris Wilson59bfa122016-08-04 16:32:31 +01001291 return NULL;
Chris Wilson17cabf52015-01-14 11:20:57 +00001292 else
1293 return ERR_PTR(ret);
Brad Volkin71745372014-12-11 12:13:12 -08001294}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001295
Chris Wilson5b043f42016-08-02 22:50:38 +01001296static int
1297execbuf_submit(struct i915_execbuffer_params *params,
1298 struct drm_i915_gem_execbuffer2 *args,
1299 struct list_head *vmas)
Oscar Mateo78382592014-07-03 16:28:05 +01001300{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001301 struct drm_i915_private *dev_priv = params->request->i915;
John Harrison5f19e2b2015-05-29 17:43:27 +01001302 u64 exec_start, exec_len;
Oscar Mateo78382592014-07-03 16:28:05 +01001303 int instp_mode;
1304 u32 instp_mask;
Chris Wilson2f5945b2015-10-06 11:39:55 +01001305 int ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001306
John Harrison535fbe82015-05-29 17:43:32 +01001307 ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
Oscar Mateo78382592014-07-03 16:28:05 +01001308 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001309 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001310
John Harrisonba01cc92015-05-29 17:43:41 +01001311 ret = i915_switch_context(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001312 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001313 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001314
1315 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1316 instp_mask = I915_EXEC_CONSTANTS_MASK;
1317 switch (instp_mode) {
1318 case I915_EXEC_CONSTANTS_REL_GENERAL:
1319 case I915_EXEC_CONSTANTS_ABSOLUTE:
1320 case I915_EXEC_CONSTANTS_REL_SURFACE:
Chris Wilsonb5321f32016-08-02 22:50:18 +01001321 if (instp_mode != 0 && params->engine->id != RCS) {
Oscar Mateo78382592014-07-03 16:28:05 +01001322 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001323 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001324 }
1325
1326 if (instp_mode != dev_priv->relative_constants_mode) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001327 if (INTEL_INFO(dev_priv)->gen < 4) {
Oscar Mateo78382592014-07-03 16:28:05 +01001328 DRM_DEBUG("no rel constants on pre-gen4\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001329 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001330 }
1331
Chris Wilsonb5321f32016-08-02 22:50:18 +01001332 if (INTEL_INFO(dev_priv)->gen > 5 &&
Oscar Mateo78382592014-07-03 16:28:05 +01001333 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1334 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001335 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001336 }
1337
1338 /* The HW changed the meaning on this bit on gen6 */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001339 if (INTEL_INFO(dev_priv)->gen >= 6)
Oscar Mateo78382592014-07-03 16:28:05 +01001340 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1341 }
1342 break;
1343 default:
1344 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001345 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001346 }
1347
Chris Wilsonb5321f32016-08-02 22:50:18 +01001348 if (params->engine->id == RCS &&
Chris Wilson2f5945b2015-10-06 11:39:55 +01001349 instp_mode != dev_priv->relative_constants_mode) {
Chris Wilson7e37f882016-08-02 22:50:21 +01001350 struct intel_ring *ring = params->request->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01001351
John Harrison5fb9de12015-05-29 17:44:07 +01001352 ret = intel_ring_begin(params->request, 4);
Oscar Mateo78382592014-07-03 16:28:05 +01001353 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001354 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001355
Chris Wilsonb5321f32016-08-02 22:50:18 +01001356 intel_ring_emit(ring, MI_NOOP);
1357 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1358 intel_ring_emit_reg(ring, INSTPM);
1359 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1360 intel_ring_advance(ring);
Oscar Mateo78382592014-07-03 16:28:05 +01001361
1362 dev_priv->relative_constants_mode = instp_mode;
1363 }
1364
1365 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001366 ret = i915_reset_gen7_sol_offsets(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001367 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001368 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001369 }
1370
John Harrison5f19e2b2015-05-29 17:43:27 +01001371 exec_len = args->batch_len;
Chris Wilson59bfa122016-08-04 16:32:31 +01001372 exec_start = params->batch->node.start +
John Harrison5f19e2b2015-05-29 17:43:27 +01001373 params->args_batch_start_offset;
1374
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001375 if (exec_len == 0)
Chris Wilson59bfa122016-08-04 16:32:31 +01001376 exec_len = params->batch->size;
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001377
Chris Wilson803688b2016-08-02 22:50:27 +01001378 ret = params->engine->emit_bb_start(params->request,
1379 exec_start, exec_len,
1380 params->dispatch_flags);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001381 if (ret)
1382 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001383
John Harrison95c24162015-05-29 17:43:31 +01001384 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001385
John Harrison8a8edb52015-05-29 17:43:33 +01001386 i915_gem_execbuffer_move_to_active(vmas, params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001387
Chris Wilson2f5945b2015-10-06 11:39:55 +01001388 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001389}
1390
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001391/**
1392 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01001393 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001394 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001395static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01001396gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
1397 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001398{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001399 struct drm_i915_file_private *file_priv = file->driver_priv;
1400
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001401 /* Check whether the file_priv has already selected one ring. */
Chris Wilsonc80ff162016-07-27 09:07:27 +01001402 if ((int)file_priv->bsd_engine < 0) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001403 /* If not, use the ping-pong mechanism to select one. */
Chris Wilson91c8a322016-07-05 10:40:23 +01001404 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonc80ff162016-07-27 09:07:27 +01001405 file_priv->bsd_engine = dev_priv->mm.bsd_engine_dispatch_index;
1406 dev_priv->mm.bsd_engine_dispatch_index ^= 1;
Chris Wilson91c8a322016-07-05 10:40:23 +01001407 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001408 }
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001409
Chris Wilsonc80ff162016-07-27 09:07:27 +01001410 return file_priv->bsd_engine;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001411}
1412
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001413#define I915_USER_RINGS (4)
1414
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00001415static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001416 [I915_EXEC_DEFAULT] = RCS,
1417 [I915_EXEC_RENDER] = RCS,
1418 [I915_EXEC_BLT] = BCS,
1419 [I915_EXEC_BSD] = VCS,
1420 [I915_EXEC_VEBOX] = VECS
1421};
1422
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001423static struct intel_engine_cs *
1424eb_select_engine(struct drm_i915_private *dev_priv,
1425 struct drm_file *file,
1426 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001427{
1428 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001429 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001430
1431 if (user_ring_id > I915_USER_RINGS) {
1432 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001433 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001434 }
1435
1436 if ((user_ring_id != I915_EXEC_BSD) &&
1437 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1438 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1439 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001440 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001441 }
1442
1443 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
1444 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
1445
1446 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01001447 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001448 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
1449 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00001450 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001451 bsd_idx--;
1452 } else {
1453 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
1454 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001455 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001456 }
1457
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001458 engine = &dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001459 } else {
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001460 engine = &dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001461 }
1462
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001463 if (!intel_engine_initialized(engine)) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001464 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001465 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001466 }
1467
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001468 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001469}
1470
Eric Anholtae662d32012-01-03 09:23:29 -08001471static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001472i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1473 struct drm_file *file,
1474 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001475 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001476{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001477 struct drm_i915_private *dev_priv = to_i915(dev);
1478 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky27173f12013-08-14 11:38:36 +02001479 struct eb_vmas *eb;
Brad Volkin78a42372014-12-11 12:13:09 -08001480 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001481 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +01001482 struct i915_gem_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001483 struct i915_address_space *vm;
John Harrison5f19e2b2015-05-29 17:43:27 +01001484 struct i915_execbuffer_params params_master; /* XXX: will be removed later */
1485 struct i915_execbuffer_params *params = &params_master;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001486 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
John Harrison8e004ef2015-02-13 11:48:10 +00001487 u32 dispatch_flags;
Oscar Mateo78382592014-07-03 16:28:05 +01001488 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001489 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001490
Daniel Vettered5982e2013-01-17 22:23:36 +01001491 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001492 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001493
Chris Wilsonad19f102014-08-10 06:29:08 +01001494 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001495 if (ret)
1496 return ret;
1497
John Harrison8e004ef2015-02-13 11:48:10 +00001498 dispatch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001499 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02001500 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001501 return -EPERM;
1502
John Harrison8e004ef2015-02-13 11:48:10 +00001503 dispatch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001504 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001505 if (args->flags & I915_EXEC_IS_PINNED)
John Harrison8e004ef2015-02-13 11:48:10 +00001506 dispatch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001507
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001508 engine = eb_select_engine(dev_priv, file, args);
1509 if (!engine)
1510 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001511
1512 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001513 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001514 return -EINVAL;
1515 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001516
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001517 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
1518 if (!HAS_RESOURCE_STREAMER(dev)) {
1519 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
1520 return -EINVAL;
1521 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001522 if (engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001523 DRM_DEBUG("RS is not available on %s\n",
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001524 engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001525 return -EINVAL;
1526 }
1527
1528 dispatch_flags |= I915_DISPATCH_RS;
1529 }
1530
Chris Wilson67d97da2016-07-04 08:08:31 +01001531 /* Take a local wakeref for preparing to dispatch the execbuf as
1532 * we expect to access the hardware fairly frequently in the
1533 * process. Upon first dispatch, we acquire another prolonged
1534 * wakeref that we hold until the GPU has been idle for at least
1535 * 100ms.
1536 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001537 intel_runtime_pm_get(dev_priv);
1538
Chris Wilson54cf91d2010-11-25 18:00:26 +00001539 ret = i915_mutex_lock_interruptible(dev);
1540 if (ret)
1541 goto pre_mutex_err;
1542
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001543 ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001544 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001545 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001546 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001547 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001548 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001549
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001550 i915_gem_context_get(ctx);
Ben Widawsky41bde552013-12-06 14:11:21 -08001551
Daniel Vetterae6c4802014-08-06 15:04:53 +02001552 if (ctx->ppgtt)
1553 vm = &ctx->ppgtt->base;
1554 else
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001555 vm = &ggtt->base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001556
John Harrison5f19e2b2015-05-29 17:43:27 +01001557 memset(&params_master, 0x00, sizeof(params_master));
1558
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001559 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001560 if (eb == NULL) {
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001561 i915_gem_context_put(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001562 mutex_unlock(&dev->struct_mutex);
1563 ret = -ENOMEM;
1564 goto pre_mutex_err;
1565 }
1566
Chris Wilson54cf91d2010-11-25 18:00:26 +00001567 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001568 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001569 if (ret)
1570 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001571
Chris Wilson6fe4f142011-01-10 17:35:37 +00001572 /* take note of the batch buffer before we might reorder the lists */
Chris Wilson59bfa122016-08-04 16:32:31 +01001573 params->batch = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001574
Chris Wilson54cf91d2010-11-25 18:00:26 +00001575 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001576 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001577 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
1578 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001579 if (ret)
1580 goto err;
1581
1582 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001583 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001584 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001585 if (ret) {
1586 if (ret == -EFAULT) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001587 ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
1588 engine,
David Weinehallb1b38272015-05-20 17:00:13 +03001589 eb, exec, ctx);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001590 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1591 }
1592 if (ret)
1593 goto err;
1594 }
1595
1596 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson59bfa122016-08-04 16:32:31 +01001597 if (params->batch->obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001598 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001599 ret = -EINVAL;
1600 goto err;
1601 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001602
John Harrison5f19e2b2015-05-29 17:43:27 +01001603 params->args_batch_start_offset = args->batch_start_offset;
Chris Wilson33a051a2016-07-27 09:07:26 +01001604 if (intel_engine_needs_cmd_parser(engine) && args->batch_len) {
Chris Wilson59bfa122016-08-04 16:32:31 +01001605 struct i915_vma *vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001606
Chris Wilson59bfa122016-08-04 16:32:31 +01001607 vma = i915_gem_execbuffer_parse(engine, &shadow_exec_entry,
1608 params->batch->obj,
1609 eb,
1610 args->batch_start_offset,
1611 args->batch_len,
1612 drm_is_current_master(file));
1613 if (IS_ERR(vma)) {
1614 ret = PTR_ERR(vma);
Brad Volkin78a42372014-12-11 12:13:09 -08001615 goto err;
1616 }
Chris Wilson17cabf52015-01-14 11:20:57 +00001617
Chris Wilson59bfa122016-08-04 16:32:31 +01001618 if (vma) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001619 /*
1620 * Batch parsed and accepted:
1621 *
1622 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1623 * bit from MI_BATCH_BUFFER_START commands issued in
1624 * the dispatch_execbuffer implementations. We
1625 * specifically don't want that set on batches the
1626 * command parser has accepted.
1627 */
1628 dispatch_flags |= I915_DISPATCH_SECURE;
John Harrison5f19e2b2015-05-29 17:43:27 +01001629 params->args_batch_start_offset = 0;
Chris Wilson59bfa122016-08-04 16:32:31 +01001630 params->batch = vma;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001631 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001632 }
1633
Chris Wilson59bfa122016-08-04 16:32:31 +01001634 params->batch->obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
Brad Volkin78a42372014-12-11 12:13:09 -08001635
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001636 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1637 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001638 * hsw should have this fixed, but bdw mucks it up again. */
John Harrison8e004ef2015-02-13 11:48:10 +00001639 if (dispatch_flags & I915_DISPATCH_SECURE) {
Chris Wilson59bfa122016-08-04 16:32:31 +01001640 struct drm_i915_gem_object *obj = params->batch->obj;
1641
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001642 /*
1643 * So on first glance it looks freaky that we pin the batch here
1644 * outside of the reservation loop. But:
1645 * - The batch is already pinned into the relevant ppgtt, so we
1646 * already have the backing storage fully allocated.
1647 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01001648 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001649 * fitting due to fragmentation.
1650 * So this is actually safe.
1651 */
Chris Wilson59bfa122016-08-04 16:32:31 +01001652 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001653 if (ret)
1654 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001655
Chris Wilson59bfa122016-08-04 16:32:31 +01001656 params->batch = i915_gem_obj_to_ggtt(obj);
1657 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001658
John Harrison0c8dac82015-05-29 17:43:25 +01001659 /* Allocate a request for this batch buffer nice and early. */
Chris Wilson8e637172016-08-02 22:50:26 +01001660 params->request = i915_gem_request_alloc(engine, ctx);
1661 if (IS_ERR(params->request)) {
1662 ret = PTR_ERR(params->request);
John Harrison0c8dac82015-05-29 17:43:25 +01001663 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00001664 }
John Harrison0c8dac82015-05-29 17:43:25 +01001665
Chris Wilson8e637172016-08-02 22:50:26 +01001666 ret = i915_gem_request_add_to_client(params->request, file);
John Harrisonfcfa423c2015-05-29 17:44:12 +01001667 if (ret)
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001668 goto err_request;
John Harrisonfcfa423c2015-05-29 17:44:12 +01001669
John Harrison5f19e2b2015-05-29 17:43:27 +01001670 /*
1671 * Save assorted stuff away to pass through to *_submission().
1672 * NB: This data should be 'persistent' and not local as it will
1673 * kept around beyond the duration of the IOCTL once the GPU
1674 * scheduler arrives.
1675 */
1676 params->dev = dev;
1677 params->file = file;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001678 params->engine = engine;
John Harrison5f19e2b2015-05-29 17:43:27 +01001679 params->dispatch_flags = dispatch_flags;
John Harrison5f19e2b2015-05-29 17:43:27 +01001680 params->ctx = ctx;
1681
Chris Wilson5b043f42016-08-02 22:50:38 +01001682 ret = execbuf_submit(params, args, &eb->vmas);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001683err_request:
Chris Wilson59bfa122016-08-04 16:32:31 +01001684 __i915_add_request(params->request, params->batch->obj, ret == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001685
John Harrison0c8dac82015-05-29 17:43:25 +01001686err_batch_unpin:
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001687 /*
1688 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1689 * batch vma for correctness. For less ugly and less fragility this
1690 * needs to be adjusted to also track the ggtt batch vma properly as
1691 * active.
1692 */
John Harrison8e004ef2015-02-13 11:48:10 +00001693 if (dispatch_flags & I915_DISPATCH_SECURE)
Chris Wilson59bfa122016-08-04 16:32:31 +01001694 i915_vma_unpin(params->batch);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001695err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001696 /* the request owns the ref now */
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001697 i915_gem_context_put(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001698 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001699
1700 mutex_unlock(&dev->struct_mutex);
1701
1702pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001703 /* intel_gpu_busy should also get a ref, so it will free when the device
1704 * is really idle. */
1705 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001706 return ret;
1707}
1708
1709/*
1710 * Legacy execbuffer just creates an exec2 list from the original exec object
1711 * list array and passes it to the real function.
1712 */
1713int
1714i915_gem_execbuffer(struct drm_device *dev, void *data,
1715 struct drm_file *file)
1716{
1717 struct drm_i915_gem_execbuffer *args = data;
1718 struct drm_i915_gem_execbuffer2 exec2;
1719 struct drm_i915_gem_exec_object *exec_list = NULL;
1720 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1721 int ret, i;
1722
Chris Wilson54cf91d2010-11-25 18:00:26 +00001723 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001724 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001725 return -EINVAL;
1726 }
1727
1728 /* Copy in the exec list from userland */
1729 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1730 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1731 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001732 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001733 args->buffer_count);
1734 drm_free_large(exec_list);
1735 drm_free_large(exec2_list);
1736 return -ENOMEM;
1737 }
1738 ret = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001739 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001740 sizeof(*exec_list) * args->buffer_count);
1741 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001742 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001743 args->buffer_count, ret);
1744 drm_free_large(exec_list);
1745 drm_free_large(exec2_list);
1746 return -EFAULT;
1747 }
1748
1749 for (i = 0; i < args->buffer_count; i++) {
1750 exec2_list[i].handle = exec_list[i].handle;
1751 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1752 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1753 exec2_list[i].alignment = exec_list[i].alignment;
1754 exec2_list[i].offset = exec_list[i].offset;
1755 if (INTEL_INFO(dev)->gen < 4)
1756 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1757 else
1758 exec2_list[i].flags = 0;
1759 }
1760
1761 exec2.buffers_ptr = args->buffers_ptr;
1762 exec2.buffer_count = args->buffer_count;
1763 exec2.batch_start_offset = args->batch_start_offset;
1764 exec2.batch_len = args->batch_len;
1765 exec2.DR1 = args->DR1;
1766 exec2.DR4 = args->DR4;
1767 exec2.num_cliprects = args->num_cliprects;
1768 exec2.cliprects_ptr = args->cliprects_ptr;
1769 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001770 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001771
Ben Widawsky41bde552013-12-06 14:11:21 -08001772 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001773 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001774 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001775 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001776
Chris Wilson54cf91d2010-11-25 18:00:26 +00001777 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001778 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001779 exec2_list[i].offset =
1780 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001781 ret = __copy_to_user(&user_exec_list[i].offset,
1782 &exec2_list[i].offset,
1783 sizeof(user_exec_list[i].offset));
1784 if (ret) {
1785 ret = -EFAULT;
1786 DRM_DEBUG("failed to copy %d exec entries "
1787 "back to user (%d)\n",
1788 args->buffer_count, ret);
1789 break;
1790 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001791 }
1792 }
1793
1794 drm_free_large(exec_list);
1795 drm_free_large(exec2_list);
1796 return ret;
1797}
1798
1799int
1800i915_gem_execbuffer2(struct drm_device *dev, void *data,
1801 struct drm_file *file)
1802{
1803 struct drm_i915_gem_execbuffer2 *args = data;
1804 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1805 int ret;
1806
Xi Wanged8cd3b2012-04-23 04:06:41 -04001807 if (args->buffer_count < 1 ||
1808 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001809 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001810 return -EINVAL;
1811 }
1812
Daniel Vetter9cb34662014-04-24 08:09:11 +02001813 if (args->rsvd2 != 0) {
1814 DRM_DEBUG("dirty rvsd2 field\n");
1815 return -EINVAL;
1816 }
1817
Chris Wilsonf2a85e12016-04-08 12:11:13 +01001818 exec2_list = drm_malloc_gfp(args->buffer_count,
1819 sizeof(*exec2_list),
1820 GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001821 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001822 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001823 args->buffer_count);
1824 return -ENOMEM;
1825 }
1826 ret = copy_from_user(exec2_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001827 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001828 sizeof(*exec2_list) * args->buffer_count);
1829 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001830 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001831 args->buffer_count, ret);
1832 drm_free_large(exec2_list);
1833 return -EFAULT;
1834 }
1835
Ben Widawsky41bde552013-12-06 14:11:21 -08001836 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001837 if (!ret) {
1838 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001839 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001840 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001841 int i;
1842
1843 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001844 exec2_list[i].offset =
1845 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001846 ret = __copy_to_user(&user_exec_list[i].offset,
1847 &exec2_list[i].offset,
1848 sizeof(user_exec_list[i].offset));
1849 if (ret) {
1850 ret = -EFAULT;
1851 DRM_DEBUG("failed to copy %d exec entries "
1852 "back to user\n",
1853 args->buffer_count);
1854 break;
1855 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001856 }
1857 }
1858
1859 drm_free_large(exec2_list);
1860 return ret;
1861}