blob: d0ef675fb1695c55d209f7ceb31d643029e56a88 [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
Ben Widawsky27173f12013-08-14 11:38:36 +020045struct eb_vmas {
46 struct list_head vmas;
Chris Wilson67731b82010-12-08 10:38:14 +000047 int and;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000048 union {
Ben Widawsky27173f12013-08-14 11:38:36 +020049 struct i915_vma *lut[0];
Chris Wilsoneef90cc2013-01-08 10:53:17 +000050 struct hlist_head buckets[0];
51 };
Chris Wilson67731b82010-12-08 10:38:14 +000052};
53
Ben Widawsky27173f12013-08-14 11:38:36 +020054static struct eb_vmas *
Ben Widawsky17601cbc2013-11-25 09:54:38 -080055eb_create(struct drm_i915_gem_execbuffer2 *args)
Chris Wilson67731b82010-12-08 10:38:14 +000056{
Ben Widawsky27173f12013-08-14 11:38:36 +020057 struct eb_vmas *eb = NULL;
Chris Wilson67731b82010-12-08 10:38:14 +000058
Chris Wilsoneef90cc2013-01-08 10:53:17 +000059 if (args->flags & I915_EXEC_HANDLE_LUT) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020060 unsigned size = args->buffer_count;
Ben Widawsky27173f12013-08-14 11:38:36 +020061 size *= sizeof(struct i915_vma *);
62 size += sizeof(struct eb_vmas);
Chris Wilsoneef90cc2013-01-08 10:53:17 +000063 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
64 }
65
66 if (eb == NULL) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020067 unsigned size = args->buffer_count;
68 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Lauri Kasanen27b7c632013-03-27 15:04:55 +020069 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
Chris Wilsoneef90cc2013-01-08 10:53:17 +000070 while (count > 2*size)
71 count >>= 1;
72 eb = kzalloc(count*sizeof(struct hlist_head) +
Ben Widawsky27173f12013-08-14 11:38:36 +020073 sizeof(struct eb_vmas),
Chris Wilsoneef90cc2013-01-08 10:53:17 +000074 GFP_TEMPORARY);
75 if (eb == NULL)
76 return eb;
77
78 eb->and = count - 1;
79 } else
80 eb->and = -args->buffer_count;
81
Ben Widawsky27173f12013-08-14 11:38:36 +020082 INIT_LIST_HEAD(&eb->vmas);
Chris Wilson67731b82010-12-08 10:38:14 +000083 return eb;
84}
85
86static void
Ben Widawsky27173f12013-08-14 11:38:36 +020087eb_reset(struct eb_vmas *eb)
Chris Wilson67731b82010-12-08 10:38:14 +000088{
Chris Wilsoneef90cc2013-01-08 10:53:17 +000089 if (eb->and >= 0)
90 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
Chris Wilson67731b82010-12-08 10:38:14 +000091}
92
Chris Wilson3b96eff2013-01-08 10:53:14 +000093static int
Ben Widawsky27173f12013-08-14 11:38:36 +020094eb_lookup_vmas(struct eb_vmas *eb,
95 struct drm_i915_gem_exec_object2 *exec,
96 const struct drm_i915_gem_execbuffer2 *args,
97 struct i915_address_space *vm,
98 struct drm_file *file)
Chris Wilson3b96eff2013-01-08 10:53:14 +000099{
Ben Widawsky27173f12013-08-14 11:38:36 +0200100 struct drm_i915_gem_object *obj;
101 struct list_head objects;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000102 int i, ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000103
Ben Widawsky27173f12013-08-14 11:38:36 +0200104 INIT_LIST_HEAD(&objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000105 spin_lock(&file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +0200106 /* Grab a reference to the object and release the lock so we can lookup
107 * or create the VMA without using GFP_ATOMIC */
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000108 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000109 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
110 if (obj == NULL) {
111 spin_unlock(&file->table_lock);
112 DRM_DEBUG("Invalid object handle %d at index %d\n",
113 exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200114 ret = -ENOENT;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000115 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000116 }
117
Ben Widawsky27173f12013-08-14 11:38:36 +0200118 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000119 spin_unlock(&file->table_lock);
120 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
121 obj, exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200122 ret = -EINVAL;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000123 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000124 }
125
Chris Wilson25dc5562016-07-20 13:31:52 +0100126 i915_gem_object_get(obj);
Ben Widawsky27173f12013-08-14 11:38:36 +0200127 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000128 }
129 spin_unlock(&file->table_lock);
130
Ben Widawsky27173f12013-08-14 11:38:36 +0200131 i = 0;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000132 while (!list_empty(&objects)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200133 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800134
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000135 obj = list_first_entry(&objects,
136 struct drm_i915_gem_object,
137 obj_exec_link);
138
Daniel Vettere656a6c2013-08-14 14:14:04 +0200139 /*
140 * NOTE: We can leak any vmas created here when something fails
141 * later on. But that's no issue since vma_unbind can deal with
142 * vmas which are not actually bound. And since only
143 * lookup_or_create exists as an interface to get at the vma
144 * from the (obj, vm) we don't run the risk of creating
145 * duplicated vmas for the same vm.
146 */
Daniel Vetterda51a1e2014-08-11 12:08:58 +0200147 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200148 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200149 DRM_DEBUG("Failed to lookup VMA\n");
150 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000151 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200152 }
153
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000154 /* Transfer ownership from the objects list to the vmas list. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200155 list_add_tail(&vma->exec_list, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000156 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200157
158 vma->exec_entry = &exec[i];
159 if (eb->and < 0) {
160 eb->lut[i] = vma;
161 } else {
162 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
163 vma->exec_handle = handle;
164 hlist_add_head(&vma->exec_node,
165 &eb->buckets[handle & eb->and]);
166 }
167 ++i;
168 }
169
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000170 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200171
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000172
173err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200174 while (!list_empty(&objects)) {
175 obj = list_first_entry(&objects,
176 struct drm_i915_gem_object,
177 obj_exec_link);
178 list_del_init(&obj->obj_exec_link);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100179 i915_gem_object_put(obj);
Ben Widawsky27173f12013-08-14 11:38:36 +0200180 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000181 /*
182 * Objects already transfered to the vmas list will be unreferenced by
183 * eb_destroy.
184 */
185
Ben Widawsky27173f12013-08-14 11:38:36 +0200186 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000187}
188
Dave Gordon4bfa3392016-07-14 14:52:04 +0100189static inline struct i915_vma *
190eb_get_batch_vma(struct eb_vmas *eb)
191{
192 /* The batch is always the LAST item in the VMA list */
193 struct i915_vma *vma = list_last_entry(&eb->vmas, typeof(*vma), exec_list);
194
195 return vma;
196}
197
198static struct drm_i915_gem_object *
199eb_get_batch(struct eb_vmas *eb)
200{
201 struct i915_vma *vma = eb_get_batch_vma(eb);
202
203 /*
204 * SNA is doing fancy tricks with compressing batch buffers, which leads
205 * to negative relocation deltas. Usually that works out ok since the
206 * relocate address is still positive, except when the batch is placed
207 * very low in the GTT. Ensure this doesn't happen.
208 *
209 * Note that actual hangs have only been observed on gen7, but for
210 * paranoia do it everywhere.
211 */
212 if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
213 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
214
215 return vma->obj;
216}
217
Ben Widawsky27173f12013-08-14 11:38:36 +0200218static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000219{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000220 if (eb->and < 0) {
221 if (handle >= -eb->and)
222 return NULL;
223 return eb->lut[handle];
224 } else {
225 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800226 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000227
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000228 head = &eb->buckets[handle & eb->and];
Geliang Tangaa459502016-01-18 23:54:20 +0800229 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200230 if (vma->exec_handle == handle)
231 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000232 }
233 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000234 }
Chris Wilson67731b82010-12-08 10:38:14 +0000235}
236
Chris Wilsona415d352013-11-26 11:23:15 +0000237static void
238i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
239{
240 struct drm_i915_gem_exec_object2 *entry;
241 struct drm_i915_gem_object *obj = vma->obj;
242
243 if (!drm_mm_node_allocated(&vma->node))
244 return;
245
246 entry = vma->exec_entry;
247
248 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
249 i915_gem_object_unpin_fence(obj);
250
251 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Daniel Vetter3d7f0f92013-12-18 16:23:37 +0100252 vma->pin_count--;
Chris Wilsona415d352013-11-26 11:23:15 +0000253
Chris Wilsonde4e7832015-04-07 16:20:35 +0100254 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilsona415d352013-11-26 11:23:15 +0000255}
256
257static void eb_destroy(struct eb_vmas *eb)
258{
Ben Widawsky27173f12013-08-14 11:38:36 +0200259 while (!list_empty(&eb->vmas)) {
260 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000261
Ben Widawsky27173f12013-08-14 11:38:36 +0200262 vma = list_first_entry(&eb->vmas,
263 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000264 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200265 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000266 i915_gem_execbuffer_unreserve_vma(vma);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100267 i915_gem_object_put(vma->obj);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000268 }
Chris Wilson67731b82010-12-08 10:38:14 +0000269 kfree(eb);
270}
271
Chris Wilsondabdfe02012-03-26 10:10:27 +0200272static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
273{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300274 return (HAS_LLC(obj->base.dev) ||
275 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200276 obj->cache_level != I915_CACHE_NONE);
277}
278
Michał Winiarski934acce2015-12-29 18:24:52 +0100279/* Used to convert any address to canonical form.
280 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
281 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
282 * addresses to be in a canonical form:
283 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
284 * canonical form [63:48] == [47]."
285 */
286#define GEN8_HIGH_ADDRESS_BIT 47
287static inline uint64_t gen8_canonical_addr(uint64_t address)
288{
289 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
290}
291
292static inline uint64_t gen8_noncanonical_addr(uint64_t address)
293{
294 return address & ((1ULL << (GEN8_HIGH_ADDRESS_BIT + 1)) - 1);
295}
296
297static inline uint64_t
298relocation_target(struct drm_i915_gem_relocation_entry *reloc,
299 uint64_t target_offset)
300{
301 return gen8_canonical_addr((int)reloc->delta + target_offset);
302}
303
Chris Wilson54cf91d2010-11-25 18:00:26 +0000304static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100305relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700306 struct drm_i915_gem_relocation_entry *reloc,
307 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100308{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700309 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100310 uint32_t page_offset = offset_in_page(reloc->offset);
Michał Winiarski934acce2015-12-29 18:24:52 +0100311 uint64_t delta = relocation_target(reloc, target_offset);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100312 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800313 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100314
Chris Wilson2cc86b82013-08-26 19:51:00 -0300315 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100316 if (ret)
317 return ret;
318
Dave Gordon033908a2015-12-10 18:51:23 +0000319 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Rafael Barbalho5032d872013-08-21 17:10:51 +0100320 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700321 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700322
323 if (INTEL_INFO(dev)->gen >= 8) {
324 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
325
326 if (page_offset == 0) {
327 kunmap_atomic(vaddr);
Dave Gordon033908a2015-12-10 18:51:23 +0000328 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700329 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
330 }
331
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700332 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700333 }
334
Rafael Barbalho5032d872013-08-21 17:10:51 +0100335 kunmap_atomic(vaddr);
336
337 return 0;
338}
339
340static int
341relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700342 struct drm_i915_gem_relocation_entry *reloc,
343 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100344{
345 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300346 struct drm_i915_private *dev_priv = to_i915(dev);
347 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michał Winiarski934acce2015-12-29 18:24:52 +0100348 uint64_t delta = relocation_target(reloc, target_offset);
Chris Wilson906843c2014-08-10 06:29:11 +0100349 uint64_t offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100350 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800351 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100352
353 ret = i915_gem_object_set_to_gtt_domain(obj, true);
354 if (ret)
355 return ret;
356
357 ret = i915_gem_object_put_fence(obj);
358 if (ret)
359 return ret;
360
361 /* Map the page containing the relocation we're going to perform. */
Chris Wilson906843c2014-08-10 06:29:11 +0100362 offset = i915_gem_obj_ggtt_offset(obj);
363 offset += reloc->offset;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300364 reloc_page = io_mapping_map_atomic_wc(ggtt->mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100365 offset & PAGE_MASK);
366 iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700367
368 if (INTEL_INFO(dev)->gen >= 8) {
Chris Wilson906843c2014-08-10 06:29:11 +0100369 offset += sizeof(uint32_t);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700370
Chris Wilson906843c2014-08-10 06:29:11 +0100371 if (offset_in_page(offset) == 0) {
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700372 io_mapping_unmap_atomic(reloc_page);
Chris Wilson906843c2014-08-10 06:29:11 +0100373 reloc_page =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300374 io_mapping_map_atomic_wc(ggtt->mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100375 offset);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700376 }
377
Chris Wilson906843c2014-08-10 06:29:11 +0100378 iowrite32(upper_32_bits(delta),
379 reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700380 }
381
Rafael Barbalho5032d872013-08-21 17:10:51 +0100382 io_mapping_unmap_atomic(reloc_page);
383
384 return 0;
385}
386
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000387static void
388clflush_write32(void *addr, uint32_t value)
389{
390 /* This is not a fast path, so KISS. */
391 drm_clflush_virt_range(addr, sizeof(uint32_t));
392 *(uint32_t *)addr = value;
393 drm_clflush_virt_range(addr, sizeof(uint32_t));
394}
395
396static int
397relocate_entry_clflush(struct drm_i915_gem_object *obj,
398 struct drm_i915_gem_relocation_entry *reloc,
399 uint64_t target_offset)
400{
401 struct drm_device *dev = obj->base.dev;
402 uint32_t page_offset = offset_in_page(reloc->offset);
Michał Winiarski934acce2015-12-29 18:24:52 +0100403 uint64_t delta = relocation_target(reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000404 char *vaddr;
405 int ret;
406
407 ret = i915_gem_object_set_to_gtt_domain(obj, true);
408 if (ret)
409 return ret;
410
Dave Gordon033908a2015-12-10 18:51:23 +0000411 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000412 reloc->offset >> PAGE_SHIFT));
413 clflush_write32(vaddr + page_offset, lower_32_bits(delta));
414
415 if (INTEL_INFO(dev)->gen >= 8) {
416 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
417
418 if (page_offset == 0) {
419 kunmap_atomic(vaddr);
Dave Gordon033908a2015-12-10 18:51:23 +0000420 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000421 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
422 }
423
424 clflush_write32(vaddr + page_offset, upper_32_bits(delta));
425 }
426
427 kunmap_atomic(vaddr);
428
429 return 0;
430}
431
Rafael Barbalho5032d872013-08-21 17:10:51 +0100432static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000433i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200434 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800435 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000436{
437 struct drm_device *dev = obj->base.dev;
438 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100439 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200440 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700441 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800442 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000443
Chris Wilson67731b82010-12-08 10:38:14 +0000444 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200445 target_vma = eb_get_vma(eb, reloc->target_handle);
446 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000447 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200448 target_i915_obj = target_vma->obj;
449 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000450
Michał Winiarski934acce2015-12-29 18:24:52 +0100451 target_offset = gen8_canonical_addr(target_vma->node.start);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000452
Eric Anholte844b992012-07-31 15:35:01 -0700453 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
454 * pipe_control writes because the gpu doesn't properly redirect them
455 * through the ppgtt for non_secure batchbuffers. */
456 if (unlikely(IS_GEN6(dev) &&
Daniel Vetter08755462015-04-20 09:04:05 -0700457 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000458 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
Daniel Vetter08755462015-04-20 09:04:05 -0700459 PIN_GLOBAL);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000460 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
461 return ret;
462 }
Eric Anholte844b992012-07-31 15:35:01 -0700463
Chris Wilson54cf91d2010-11-25 18:00:26 +0000464 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000465 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100466 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000467 "obj %p target %d offset %d "
468 "read %08x write %08x",
469 obj, reloc->target_handle,
470 (int) reloc->offset,
471 reloc->read_domains,
472 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800473 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000474 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100475 if (unlikely((reloc->write_domain | reloc->read_domains)
476 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100477 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000478 "obj %p target %d offset %d "
479 "read %08x write %08x",
480 obj, reloc->target_handle,
481 (int) reloc->offset,
482 reloc->read_domains,
483 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800484 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000485 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000486
487 target_obj->pending_read_domains |= reloc->read_domains;
488 target_obj->pending_write_domain |= reloc->write_domain;
489
490 /* If the relocation already has the right value in it, no
491 * more work needs to be done.
492 */
493 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000494 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000495
496 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700497 if (unlikely(reloc->offset >
498 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100499 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000500 "obj %p target %d offset %d size %d.\n",
501 obj, reloc->target_handle,
502 (int) reloc->offset,
503 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800504 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000505 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000506 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100507 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000508 "obj %p target %d offset %d.\n",
509 obj, reloc->target_handle,
510 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800511 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000512 }
513
Chris Wilsondabdfe02012-03-26 10:10:27 +0200514 /* We can't wait for rendering with pagefaults disabled */
David Hildenbrand32d82062015-05-11 17:52:12 +0200515 if (obj->active && pagefault_disabled())
Chris Wilsondabdfe02012-03-26 10:10:27 +0200516 return -EFAULT;
517
Rafael Barbalho5032d872013-08-21 17:10:51 +0100518 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700519 ret = relocate_entry_cpu(obj, reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000520 else if (obj->map_and_fenceable)
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700521 ret = relocate_entry_gtt(obj, reloc, target_offset);
Borislav Petkov906bf7f2016-03-29 17:41:59 +0200522 else if (static_cpu_has(X86_FEATURE_CLFLUSH))
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000523 ret = relocate_entry_clflush(obj, reloc, target_offset);
524 else {
525 WARN_ONCE(1, "Impossible case in relocation handling\n");
526 ret = -ENODEV;
527 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000528
Daniel Vetterd4d36012013-09-02 20:56:23 +0200529 if (ret)
530 return ret;
531
Chris Wilson54cf91d2010-11-25 18:00:26 +0000532 /* and update the user's relocation entry */
533 reloc->presumed_offset = target_offset;
534
Chris Wilson67731b82010-12-08 10:38:14 +0000535 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000536}
537
538static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200539i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
540 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000541{
Chris Wilson1d83f442012-03-24 20:12:53 +0000542#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
543 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000544 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200545 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000546 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000547
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300548 user_relocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000549
Chris Wilson1d83f442012-03-24 20:12:53 +0000550 remain = entry->relocation_count;
551 while (remain) {
552 struct drm_i915_gem_relocation_entry *r = stack_reloc;
553 int count = remain;
554 if (count > ARRAY_SIZE(stack_reloc))
555 count = ARRAY_SIZE(stack_reloc);
556 remain -= count;
557
558 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000559 return -EFAULT;
560
Chris Wilson1d83f442012-03-24 20:12:53 +0000561 do {
562 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000563
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800564 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000565 if (ret)
566 return ret;
567
568 if (r->presumed_offset != offset &&
Linus Torvalds5b09c3e2016-05-22 14:19:37 -0700569 __put_user(r->presumed_offset, &user_relocs->presumed_offset)) {
Chris Wilson1d83f442012-03-24 20:12:53 +0000570 return -EFAULT;
571 }
572
573 user_relocs++;
574 r++;
575 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000576 }
577
578 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000579#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000580}
581
582static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200583i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
584 struct eb_vmas *eb,
585 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000586{
Ben Widawsky27173f12013-08-14 11:38:36 +0200587 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000588 int i, ret;
589
590 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800591 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000592 if (ret)
593 return ret;
594 }
595
596 return 0;
597}
598
599static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800600i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000601{
Ben Widawsky27173f12013-08-14 11:38:36 +0200602 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000603 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000604
Chris Wilsond4aeee72011-03-14 15:11:24 +0000605 /* This is the fast path and we cannot handle a pagefault whilst
606 * holding the struct mutex lest the user pass in the relocations
607 * contained within a mmaped bo. For in such a case we, the page
608 * fault handler would call i915_gem_fault() and we would try to
609 * acquire the struct mutex again. Obviously this is bad and so
610 * lockdep complains vehemently.
611 */
612 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200613 list_for_each_entry(vma, &eb->vmas, exec_list) {
614 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000615 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000616 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000617 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000618 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000619
Chris Wilsond4aeee72011-03-14 15:11:24 +0000620 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000621}
622
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000623static bool only_mappable_for_reloc(unsigned int flags)
624{
625 return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
626 __EXEC_OBJECT_NEEDS_MAP;
627}
628
Chris Wilson1690e1e2011-12-14 13:57:08 +0100629static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200630i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000631 struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200632 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100633{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800634 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200635 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200636 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100637 int ret;
638
Daniel Vetter08755462015-04-20 09:04:05 -0700639 flags = PIN_USER;
Daniel Vetter0229da32015-04-14 19:01:54 +0200640 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
641 flags |= PIN_GLOBAL;
642
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000643 if (!drm_mm_node_allocated(&vma->node)) {
Michel Thierry101b5062015-10-01 13:33:57 +0100644 /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
645 * limit address to the first 4GBs for unflagged objects.
646 */
647 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
648 flags |= PIN_ZONE_4G;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000649 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
650 flags |= PIN_GLOBAL | PIN_MAPPABLE;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000651 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
652 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson506a8e82015-12-08 11:55:07 +0000653 if (entry->flags & EXEC_OBJECT_PINNED)
654 flags |= entry->offset | PIN_OFFSET_FIXED;
Michel Thierry101b5062015-10-01 13:33:57 +0100655 if ((flags & PIN_MAPPABLE) == 0)
656 flags |= PIN_HIGH;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000657 }
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100658
659 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000660 if ((ret == -ENOSPC || ret == -E2BIG) &&
661 only_mappable_for_reloc(entry->flags))
662 ret = i915_gem_object_pin(obj, vma->vm,
663 entry->alignment,
Daniel Vetter0229da32015-04-14 19:01:54 +0200664 flags & ~PIN_MAPPABLE);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100665 if (ret)
666 return ret;
667
Chris Wilson7788a762012-08-24 19:18:18 +0100668 entry->flags |= __EXEC_OBJECT_HAS_PIN;
669
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100670 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
671 ret = i915_gem_object_get_fence(obj);
672 if (ret)
673 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100674
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100675 if (i915_gem_object_pin_fence(obj))
676 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100677 }
678
Ben Widawsky27173f12013-08-14 11:38:36 +0200679 if (entry->offset != vma->node.start) {
680 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100681 *need_reloc = true;
682 }
683
684 if (entry->flags & EXEC_OBJECT_WRITE) {
685 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
686 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
687 }
688
Chris Wilson1690e1e2011-12-14 13:57:08 +0100689 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100690}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100691
Chris Wilsond23db882014-05-23 08:48:08 +0200692static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200693need_reloc_mappable(struct i915_vma *vma)
694{
695 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
696
697 if (entry->relocation_count == 0)
698 return false;
699
Chris Wilson596c5922016-02-26 11:03:20 +0000700 if (!vma->is_ggtt)
Chris Wilsone6a84462014-08-11 12:00:12 +0200701 return false;
702
703 /* See also use_cpu_reloc() */
704 if (HAS_LLC(vma->obj->base.dev))
705 return false;
706
707 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
708 return false;
709
710 return true;
711}
712
713static bool
714eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200715{
716 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
717 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond23db882014-05-23 08:48:08 +0200718
Chris Wilson596c5922016-02-26 11:03:20 +0000719 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP && !vma->is_ggtt);
Chris Wilsond23db882014-05-23 08:48:08 +0200720
721 if (entry->alignment &&
722 vma->node.start & (entry->alignment - 1))
723 return true;
724
Chris Wilson506a8e82015-12-08 11:55:07 +0000725 if (entry->flags & EXEC_OBJECT_PINNED &&
726 vma->node.start != entry->offset)
727 return true;
728
Chris Wilsond23db882014-05-23 08:48:08 +0200729 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
730 vma->node.start < BATCH_OFFSET_BIAS)
731 return true;
732
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000733 /* avoid costly ping-pong once a batch bo ended up non-mappable */
734 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
735 return !only_mappable_for_reloc(entry->flags);
736
Michel Thierry101b5062015-10-01 13:33:57 +0100737 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
738 (vma->node.start + vma->node.size - 1) >> 32)
739 return true;
740
Chris Wilsond23db882014-05-23 08:48:08 +0200741 return false;
742}
743
Chris Wilson54cf91d2010-11-25 18:00:26 +0000744static int
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000745i915_gem_execbuffer_reserve(struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200746 struct list_head *vmas,
Chris Wilsone2efd132016-05-24 14:53:34 +0100747 struct i915_gem_context *ctx,
Daniel Vettered5982e2013-01-17 22:23:36 +0100748 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000749{
Chris Wilson432e58e2010-11-25 19:32:06 +0000750 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200751 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700752 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200753 struct list_head ordered_vmas;
Chris Wilson506a8e82015-12-08 11:55:07 +0000754 struct list_head pinned_vmas;
Chris Wilsonc0336662016-05-06 15:40:21 +0100755 bool has_fenced_gpu_access = INTEL_GEN(engine->i915) < 4;
Chris Wilson7788a762012-08-24 19:18:18 +0100756 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000757
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000758 i915_gem_retire_requests_ring(engine);
Chris Wilson227f7822014-05-15 10:41:42 +0100759
Ben Widawsky68c8c172013-09-11 14:57:50 -0700760 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
761
Ben Widawsky27173f12013-08-14 11:38:36 +0200762 INIT_LIST_HEAD(&ordered_vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000763 INIT_LIST_HEAD(&pinned_vmas);
Ben Widawsky27173f12013-08-14 11:38:36 +0200764 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000765 struct drm_i915_gem_exec_object2 *entry;
766 bool need_fence, need_mappable;
767
Ben Widawsky27173f12013-08-14 11:38:36 +0200768 vma = list_first_entry(vmas, struct i915_vma, exec_list);
769 obj = vma->obj;
770 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000771
David Weinehallb1b38272015-05-20 17:00:13 +0300772 if (ctx->flags & CONTEXT_NO_ZEROMAP)
773 entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
774
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100775 if (!has_fenced_gpu_access)
776 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000777 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000778 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
779 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200780 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000781
Chris Wilson506a8e82015-12-08 11:55:07 +0000782 if (entry->flags & EXEC_OBJECT_PINNED)
783 list_move_tail(&vma->exec_list, &pinned_vmas);
784 else if (need_mappable) {
Chris Wilsone6a84462014-08-11 12:00:12 +0200785 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200786 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200787 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200788 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000789
Daniel Vettered5982e2013-01-17 22:23:36 +0100790 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000791 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000792 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200793 list_splice(&ordered_vmas, vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000794 list_splice(&pinned_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000795
796 /* Attempt to pin all of the buffers into the GTT.
797 * This is done in 3 phases:
798 *
799 * 1a. Unbind all objects that do not match the GTT constraints for
800 * the execbuffer (fenceable, mappable, alignment etc).
801 * 1b. Increment pin count for already bound objects.
802 * 2. Bind new objects.
803 * 3. Decrement pin count.
804 *
Chris Wilson7788a762012-08-24 19:18:18 +0100805 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000806 * room for the earlier objects *unless* we need to defragment.
807 */
808 retry = 0;
809 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100810 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000811
812 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200813 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200814 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000815 continue;
816
Chris Wilsone6a84462014-08-11 12:00:12 +0200817 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200818 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000819 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000820 ret = i915_gem_execbuffer_reserve_vma(vma,
821 engine,
822 need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000823 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000824 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000825 }
826
827 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200828 list_for_each_entry(vma, vmas, exec_list) {
829 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100830 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000831
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000832 ret = i915_gem_execbuffer_reserve_vma(vma, engine,
833 need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100834 if (ret)
835 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000836 }
837
Chris Wilsona415d352013-11-26 11:23:15 +0000838err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200839 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000840 return ret;
841
Chris Wilsona415d352013-11-26 11:23:15 +0000842 /* Decrement pin count for bound objects */
843 list_for_each_entry(vma, vmas, exec_list)
844 i915_gem_execbuffer_unreserve_vma(vma);
845
Ben Widawsky68c8c172013-09-11 14:57:50 -0700846 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000847 if (ret)
848 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000849 } while (1);
850}
851
852static int
853i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100854 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000855 struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000856 struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200857 struct eb_vmas *eb,
David Weinehallb1b38272015-05-20 17:00:13 +0300858 struct drm_i915_gem_exec_object2 *exec,
Chris Wilsone2efd132016-05-24 14:53:34 +0100859 struct i915_gem_context *ctx)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000860{
861 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200862 struct i915_address_space *vm;
863 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100864 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000865 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000866 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200867 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000868
Ben Widawsky27173f12013-08-14 11:38:36 +0200869 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
870
Chris Wilson67731b82010-12-08 10:38:14 +0000871 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200872 while (!list_empty(&eb->vmas)) {
873 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
874 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000875 i915_gem_execbuffer_unreserve_vma(vma);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100876 i915_gem_object_put(vma->obj);
Chris Wilson67731b82010-12-08 10:38:14 +0000877 }
878
Chris Wilson54cf91d2010-11-25 18:00:26 +0000879 mutex_unlock(&dev->struct_mutex);
880
881 total = 0;
882 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000883 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000884
Chris Wilsondd6864a2011-01-12 23:49:13 +0000885 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000886 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000887 if (reloc == NULL || reloc_offset == NULL) {
888 drm_free_large(reloc);
889 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000890 mutex_lock(&dev->struct_mutex);
891 return -ENOMEM;
892 }
893
894 total = 0;
895 for (i = 0; i < count; i++) {
896 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000897 u64 invalid_offset = (u64)-1;
898 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000899
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300900 user_relocs = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000901
902 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000903 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000904 ret = -EFAULT;
905 mutex_lock(&dev->struct_mutex);
906 goto err;
907 }
908
Chris Wilson262b6d32013-01-15 16:17:54 +0000909 /* As we do not update the known relocation offsets after
910 * relocating (due to the complexities in lock handling),
911 * we need to mark them as invalid now so that we force the
912 * relocation processing next time. Just in case the target
913 * object is evicted and then rebound into its old
914 * presumed_offset before the next execbuffer - if that
915 * happened we would make the mistake of assuming that the
916 * relocations were valid.
917 */
918 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100919 if (__copy_to_user(&user_relocs[j].presumed_offset,
920 &invalid_offset,
921 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000922 ret = -EFAULT;
923 mutex_lock(&dev->struct_mutex);
924 goto err;
925 }
926 }
927
Chris Wilsondd6864a2011-01-12 23:49:13 +0000928 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000929 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000930 }
931
932 ret = i915_mutex_lock_interruptible(dev);
933 if (ret) {
934 mutex_lock(&dev->struct_mutex);
935 goto err;
936 }
937
Chris Wilson67731b82010-12-08 10:38:14 +0000938 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000939 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200940 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000941 if (ret)
942 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000943
Daniel Vettered5982e2013-01-17 22:23:36 +0100944 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000945 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
946 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000947 if (ret)
948 goto err;
949
Ben Widawsky27173f12013-08-14 11:38:36 +0200950 list_for_each_entry(vma, &eb->vmas, exec_list) {
951 int offset = vma->exec_entry - exec;
952 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
953 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000954 if (ret)
955 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000956 }
957
958 /* Leave the user relocations as are, this is the painfully slow path,
959 * and we want to avoid the complication of dropping the lock whilst
960 * having buffers reserved in the aperture and so causing spurious
961 * ENOSPC for random operations.
962 */
963
964err:
965 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000966 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000967 return ret;
968}
969
Chris Wilson54cf91d2010-11-25 18:00:26 +0000970static int
John Harrison535fbe82015-05-29 17:43:32 +0100971i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
Ben Widawsky27173f12013-08-14 11:38:36 +0200972 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000973{
Tvrtko Ursulin666796d2016-03-16 11:00:39 +0000974 const unsigned other_rings = ~intel_engine_flag(req->engine);
Ben Widawsky27173f12013-08-14 11:38:36 +0200975 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200976 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100977 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000978 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000979
Ben Widawsky27173f12013-08-14 11:38:36 +0200980 list_for_each_entry(vma, vmas, exec_list) {
981 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +0100982
983 if (obj->active & other_rings) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000984 ret = i915_gem_object_sync(obj, req->engine, &req);
Chris Wilson03ade512015-04-27 13:41:18 +0100985 if (ret)
986 return ret;
987 }
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200988
989 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100990 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200991
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200992 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000993 }
994
Chris Wilson000433b2013-08-08 14:41:09 +0100995 if (flush_chipset)
Chris Wilsonc0336662016-05-06 15:40:21 +0100996 i915_gem_chipset_flush(req->engine->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200997
998 if (flush_domains & I915_GEM_DOMAIN_GTT)
999 wmb();
1000
Chris Wilson09cf7c92012-07-13 14:14:08 +01001001 /* Unconditionally invalidate gpu caches and ensure that we do flush
1002 * any residual writes from the previous batch.
1003 */
Chris Wilson7e37f882016-08-02 22:50:21 +01001004 return intel_engine_invalidate_all_caches(req);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001005}
1006
Chris Wilson432e58e2010-11-25 19:32:06 +00001007static bool
1008i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001009{
Daniel Vettered5982e2013-01-17 22:23:36 +01001010 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
1011 return false;
1012
Chris Wilson2f5945b2015-10-06 11:39:55 +01001013 /* Kernel clipping was a DRI1 misfeature */
1014 if (exec->num_cliprects || exec->cliprects_ptr)
1015 return false;
1016
1017 if (exec->DR4 == 0xffffffff) {
1018 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1019 exec->DR4 = 0;
1020 }
1021 if (exec->DR1 || exec->DR4)
1022 return false;
1023
1024 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
1025 return false;
1026
1027 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001028}
1029
1030static int
Chris Wilsonad19f102014-08-10 06:29:08 +01001031validate_exec_list(struct drm_device *dev,
1032 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001033 int count)
1034{
Daniel Vetterb205ca52013-09-19 14:00:11 +02001035 unsigned relocs_total = 0;
1036 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +01001037 unsigned invalid_flags;
1038 int i;
1039
Dave Gordon9e2793f62016-07-14 14:52:03 +01001040 /* INTERNAL flags must not overlap with external ones */
1041 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
1042
Chris Wilsonad19f102014-08-10 06:29:08 +01001043 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
1044 if (USES_FULL_PPGTT(dev))
1045 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001046
1047 for (i = 0; i < count; i++) {
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001048 char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001049 int length; /* limited by fault_in_pages_readable() */
1050
Chris Wilsonad19f102014-08-10 06:29:08 +01001051 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +01001052 return -EINVAL;
1053
Michał Winiarski934acce2015-12-29 18:24:52 +01001054 /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
1055 * any non-page-aligned or non-canonical addresses.
1056 */
1057 if (exec[i].flags & EXEC_OBJECT_PINNED) {
1058 if (exec[i].offset !=
1059 gen8_canonical_addr(exec[i].offset & PAGE_MASK))
1060 return -EINVAL;
1061
1062 /* From drm_mm perspective address space is continuous,
1063 * so from this point we're always using non-canonical
1064 * form internally.
1065 */
1066 exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
1067 }
1068
Chris Wilson55a97852015-06-19 13:59:46 +01001069 if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
1070 return -EINVAL;
1071
Kees Cook3118a4f2013-03-11 17:31:45 -07001072 /* First check for malicious input causing overflow in
1073 * the worst case where we need to allocate the entire
1074 * relocation tree as a single array.
1075 */
1076 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001077 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -07001078 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001079
1080 length = exec[i].relocation_count *
1081 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -07001082 /*
1083 * We must check that the entire relocation array is safe
1084 * to read, but since we may need to update the presumed
1085 * offsets during execution, check for full write access.
1086 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001087 if (!access_ok(VERIFY_WRITE, ptr, length))
1088 return -EFAULT;
1089
Jani Nikulad330a952014-01-21 11:24:25 +02001090 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +08001091 if (fault_in_multipages_readable(ptr, length))
1092 return -EFAULT;
1093 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001094 }
1095
1096 return 0;
1097}
1098
Chris Wilsone2efd132016-05-24 14:53:34 +01001099static struct i915_gem_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001100i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001101 struct intel_engine_cs *engine, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001102{
Chris Wilsone2efd132016-05-24 14:53:34 +01001103 struct i915_gem_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001104 struct i915_ctx_hang_stats *hs;
1105
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001106 if (engine->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001107 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001108
Chris Wilsonca585b52016-05-24 14:53:36 +01001109 ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001110 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -08001111 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001112
Ben Widawsky41bde552013-12-06 14:11:21 -08001113 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001114 if (hs->banned) {
1115 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -08001116 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001117 }
1118
Ben Widawsky41bde552013-12-06 14:11:21 -08001119 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001120}
1121
Oscar Mateoba8b7cc2014-07-24 17:04:33 +01001122void
Ben Widawsky27173f12013-08-14 11:38:36 +02001123i915_gem_execbuffer_move_to_active(struct list_head *vmas,
John Harrison8a8edb52015-05-29 17:43:33 +01001124 struct drm_i915_gem_request *req)
Chris Wilson432e58e2010-11-25 19:32:06 +00001125{
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001126 struct intel_engine_cs *engine = i915_gem_request_get_engine(req);
Ben Widawsky27173f12013-08-14 11:38:36 +02001127 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +00001128
Ben Widawsky27173f12013-08-14 11:38:36 +02001129 list_for_each_entry(vma, vmas, exec_list) {
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001130 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Ben Widawsky27173f12013-08-14 11:38:36 +02001131 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +01001132 u32 old_read = obj->base.read_domains;
1133 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +00001134
Chris Wilson51bc1402015-08-31 15:10:39 +01001135 obj->dirty = 1; /* be paranoid */
Chris Wilson432e58e2010-11-25 19:32:06 +00001136 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +01001137 if (obj->base.write_domain == 0)
1138 obj->base.pending_read_domains |= obj->base.read_domains;
1139 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +00001140
John Harrisonb2af0372015-05-29 17:43:50 +01001141 i915_vma_move_to_active(vma, req);
Chris Wilson432e58e2010-11-25 19:32:06 +00001142 if (obj->base.write_domain) {
John Harrison97b2a6a2014-11-24 18:49:26 +00001143 i915_gem_request_assign(&obj->last_write_req, req);
Daniel Vetterf99d7062014-06-19 16:01:59 +02001144
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -07001145 intel_fb_obj_invalidate(obj, ORIGIN_CS);
Chris Wilsonc8725f32014-03-17 12:21:55 +00001146
1147 /* update for the implicit flush after a batch */
1148 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson432e58e2010-11-25 19:32:06 +00001149 }
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001150 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
John Harrison97b2a6a2014-11-24 18:49:26 +00001151 i915_gem_request_assign(&obj->last_fenced_req, req);
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001152 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
Chris Wilsonc0336662016-05-06 15:40:21 +01001153 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001154 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
1155 &dev_priv->mm.fence_list);
1156 }
1157 }
Chris Wilson432e58e2010-11-25 19:32:06 +00001158
Chris Wilsondb53a302011-02-03 11:57:46 +00001159 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +00001160 }
1161}
1162
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001163static void
John Harrisonadeca762015-05-29 17:43:28 +01001164i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params *params)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001165{
Daniel Vettercc889e02012-06-13 20:45:19 +02001166 /* Unconditionally force add_request to emit a full flush. */
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001167 params->engine->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001168
Chris Wilson432e58e2010-11-25 19:32:06 +00001169 /* Add a breadcrumb for the completion of the batch buffer */
John Harrisonfcfa423c2015-05-29 17:44:12 +01001170 __i915_add_request(params->request, params->batch_obj, true);
Chris Wilson432e58e2010-11-25 19:32:06 +00001171}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001172
1173static int
Chris Wilsonb5321f32016-08-02 22:50:18 +01001174i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001175{
Chris Wilson7e37f882016-08-02 22:50:21 +01001176 struct intel_ring *ring = req->ring;
Eric Anholtae662d32012-01-03 09:23:29 -08001177 int ret, i;
1178
Chris Wilsonb5321f32016-08-02 22:50:18 +01001179 if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001180 DRM_DEBUG("sol reset is gen7/rcs only\n");
1181 return -EINVAL;
1182 }
Eric Anholtae662d32012-01-03 09:23:29 -08001183
John Harrison5fb9de12015-05-29 17:44:07 +01001184 ret = intel_ring_begin(req, 4 * 3);
Eric Anholtae662d32012-01-03 09:23:29 -08001185 if (ret)
1186 return ret;
1187
1188 for (i = 0; i < 4; i++) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001189 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1190 intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
1191 intel_ring_emit(ring, 0);
Eric Anholtae662d32012-01-03 09:23:29 -08001192 }
1193
Chris Wilsonb5321f32016-08-02 22:50:18 +01001194 intel_ring_advance(ring);
Eric Anholtae662d32012-01-03 09:23:29 -08001195
1196 return 0;
1197}
1198
Brad Volkin71745372014-12-11 12:13:12 -08001199static struct drm_i915_gem_object*
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001200i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
Brad Volkin71745372014-12-11 12:13:12 -08001201 struct drm_i915_gem_exec_object2 *shadow_exec_entry,
1202 struct eb_vmas *eb,
1203 struct drm_i915_gem_object *batch_obj,
1204 u32 batch_start_offset,
1205 u32 batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001206 bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001207{
Brad Volkin71745372014-12-11 12:13:12 -08001208 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001209 struct i915_vma *vma;
Brad Volkin71745372014-12-11 12:13:12 -08001210 int ret;
1211
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001212 shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
Chris Wilson17cabf52015-01-14 11:20:57 +00001213 PAGE_ALIGN(batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001214 if (IS_ERR(shadow_batch_obj))
1215 return shadow_batch_obj;
1216
Chris Wilson33a051a2016-07-27 09:07:26 +01001217 ret = intel_engine_cmd_parser(engine,
1218 batch_obj,
1219 shadow_batch_obj,
1220 batch_start_offset,
1221 batch_len,
1222 is_master);
Chris Wilson17cabf52015-01-14 11:20:57 +00001223 if (ret)
1224 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001225
Chris Wilson17cabf52015-01-14 11:20:57 +00001226 ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
1227 if (ret)
1228 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001229
Chris Wilsonde4e7832015-04-07 16:20:35 +01001230 i915_gem_object_unpin_pages(shadow_batch_obj);
1231
Chris Wilson17cabf52015-01-14 11:20:57 +00001232 memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
Brad Volkin71745372014-12-11 12:13:12 -08001233
Chris Wilson17cabf52015-01-14 11:20:57 +00001234 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1235 vma->exec_entry = shadow_exec_entry;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001236 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
Chris Wilson25dc5562016-07-20 13:31:52 +01001237 i915_gem_object_get(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001238 list_add_tail(&vma->exec_list, &eb->vmas);
Brad Volkin71745372014-12-11 12:13:12 -08001239
Chris Wilson17cabf52015-01-14 11:20:57 +00001240 shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
Brad Volkin71745372014-12-11 12:13:12 -08001241
Chris Wilson17cabf52015-01-14 11:20:57 +00001242 return shadow_batch_obj;
1243
1244err:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001245 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001246 if (ret == -EACCES) /* unhandled chained batch */
1247 return batch_obj;
1248 else
1249 return ERR_PTR(ret);
Brad Volkin71745372014-12-11 12:13:12 -08001250}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001251
Oscar Mateoa83014d2014-07-24 17:04:21 +01001252int
John Harrison5f19e2b2015-05-29 17:43:27 +01001253i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
Oscar Mateoa83014d2014-07-24 17:04:21 +01001254 struct drm_i915_gem_execbuffer2 *args,
John Harrison5f19e2b2015-05-29 17:43:27 +01001255 struct list_head *vmas)
Oscar Mateo78382592014-07-03 16:28:05 +01001256{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001257 struct drm_i915_private *dev_priv = params->request->i915;
John Harrison5f19e2b2015-05-29 17:43:27 +01001258 u64 exec_start, exec_len;
Oscar Mateo78382592014-07-03 16:28:05 +01001259 int instp_mode;
1260 u32 instp_mask;
Chris Wilson2f5945b2015-10-06 11:39:55 +01001261 int ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001262
John Harrison535fbe82015-05-29 17:43:32 +01001263 ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
Oscar Mateo78382592014-07-03 16:28:05 +01001264 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001265 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001266
John Harrisonba01cc92015-05-29 17:43:41 +01001267 ret = i915_switch_context(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001268 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001269 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001270
1271 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1272 instp_mask = I915_EXEC_CONSTANTS_MASK;
1273 switch (instp_mode) {
1274 case I915_EXEC_CONSTANTS_REL_GENERAL:
1275 case I915_EXEC_CONSTANTS_ABSOLUTE:
1276 case I915_EXEC_CONSTANTS_REL_SURFACE:
Chris Wilsonb5321f32016-08-02 22:50:18 +01001277 if (instp_mode != 0 && params->engine->id != RCS) {
Oscar Mateo78382592014-07-03 16:28:05 +01001278 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001279 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001280 }
1281
1282 if (instp_mode != dev_priv->relative_constants_mode) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001283 if (INTEL_INFO(dev_priv)->gen < 4) {
Oscar Mateo78382592014-07-03 16:28:05 +01001284 DRM_DEBUG("no rel constants on pre-gen4\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001285 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001286 }
1287
Chris Wilsonb5321f32016-08-02 22:50:18 +01001288 if (INTEL_INFO(dev_priv)->gen > 5 &&
Oscar Mateo78382592014-07-03 16:28:05 +01001289 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1290 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001291 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001292 }
1293
1294 /* The HW changed the meaning on this bit on gen6 */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001295 if (INTEL_INFO(dev_priv)->gen >= 6)
Oscar Mateo78382592014-07-03 16:28:05 +01001296 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1297 }
1298 break;
1299 default:
1300 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001301 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001302 }
1303
Chris Wilsonb5321f32016-08-02 22:50:18 +01001304 if (params->engine->id == RCS &&
Chris Wilson2f5945b2015-10-06 11:39:55 +01001305 instp_mode != dev_priv->relative_constants_mode) {
Chris Wilson7e37f882016-08-02 22:50:21 +01001306 struct intel_ring *ring = params->request->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01001307
John Harrison5fb9de12015-05-29 17:44:07 +01001308 ret = intel_ring_begin(params->request, 4);
Oscar Mateo78382592014-07-03 16:28:05 +01001309 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001310 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001311
Chris Wilsonb5321f32016-08-02 22:50:18 +01001312 intel_ring_emit(ring, MI_NOOP);
1313 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1314 intel_ring_emit_reg(ring, INSTPM);
1315 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1316 intel_ring_advance(ring);
Oscar Mateo78382592014-07-03 16:28:05 +01001317
1318 dev_priv->relative_constants_mode = instp_mode;
1319 }
1320
1321 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001322 ret = i915_reset_gen7_sol_offsets(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001323 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001324 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001325 }
1326
John Harrison5f19e2b2015-05-29 17:43:27 +01001327 exec_len = args->batch_len;
1328 exec_start = params->batch_obj_vm_offset +
1329 params->args_batch_start_offset;
1330
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001331 if (exec_len == 0)
1332 exec_len = params->batch_obj->base.size;
1333
Chris Wilsonb5321f32016-08-02 22:50:18 +01001334 ret = params->engine->dispatch_execbuffer(params->request,
1335 exec_start, exec_len,
1336 params->dispatch_flags);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001337 if (ret)
1338 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001339
John Harrison95c24162015-05-29 17:43:31 +01001340 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001341
John Harrison8a8edb52015-05-29 17:43:33 +01001342 i915_gem_execbuffer_move_to_active(vmas, params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001343
Chris Wilson2f5945b2015-10-06 11:39:55 +01001344 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001345}
1346
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001347/**
1348 * Find one BSD ring to dispatch the corresponding BSD command.
Chris Wilsonc80ff162016-07-27 09:07:27 +01001349 * The engine index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001350 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001351static unsigned int
Chris Wilsonc80ff162016-07-27 09:07:27 +01001352gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
1353 struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001354{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001355 struct drm_i915_file_private *file_priv = file->driver_priv;
1356
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001357 /* Check whether the file_priv has already selected one ring. */
Chris Wilsonc80ff162016-07-27 09:07:27 +01001358 if ((int)file_priv->bsd_engine < 0) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001359 /* If not, use the ping-pong mechanism to select one. */
Chris Wilson91c8a322016-07-05 10:40:23 +01001360 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonc80ff162016-07-27 09:07:27 +01001361 file_priv->bsd_engine = dev_priv->mm.bsd_engine_dispatch_index;
1362 dev_priv->mm.bsd_engine_dispatch_index ^= 1;
Chris Wilson91c8a322016-07-05 10:40:23 +01001363 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001364 }
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001365
Chris Wilsonc80ff162016-07-27 09:07:27 +01001366 return file_priv->bsd_engine;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001367}
1368
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001369#define I915_USER_RINGS (4)
1370
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00001371static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001372 [I915_EXEC_DEFAULT] = RCS,
1373 [I915_EXEC_RENDER] = RCS,
1374 [I915_EXEC_BLT] = BCS,
1375 [I915_EXEC_BSD] = VCS,
1376 [I915_EXEC_VEBOX] = VECS
1377};
1378
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001379static struct intel_engine_cs *
1380eb_select_engine(struct drm_i915_private *dev_priv,
1381 struct drm_file *file,
1382 struct drm_i915_gem_execbuffer2 *args)
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001383{
1384 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001385 struct intel_engine_cs *engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001386
1387 if (user_ring_id > I915_USER_RINGS) {
1388 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001389 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001390 }
1391
1392 if ((user_ring_id != I915_EXEC_BSD) &&
1393 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1394 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1395 "bsd dispatch flags: %d\n", (int)(args->flags));
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001396 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001397 }
1398
1399 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
1400 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
1401
1402 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
Chris Wilsonc80ff162016-07-27 09:07:27 +01001403 bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001404 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
1405 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00001406 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001407 bsd_idx--;
1408 } else {
1409 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
1410 bsd_idx);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001411 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001412 }
1413
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001414 engine = &dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001415 } else {
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001416 engine = &dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001417 }
1418
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001419 if (!intel_engine_initialized(engine)) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001420 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001421 return NULL;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001422 }
1423
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001424 return engine;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001425}
1426
Eric Anholtae662d32012-01-03 09:23:29 -08001427static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001428i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1429 struct drm_file *file,
1430 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001431 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001432{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001433 struct drm_i915_private *dev_priv = to_i915(dev);
1434 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Dave Gordon26827082016-01-19 19:02:53 +00001435 struct drm_i915_gem_request *req = NULL;
Ben Widawsky27173f12013-08-14 11:38:36 +02001436 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001437 struct drm_i915_gem_object *batch_obj;
Brad Volkin78a42372014-12-11 12:13:09 -08001438 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001439 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +01001440 struct i915_gem_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001441 struct i915_address_space *vm;
John Harrison5f19e2b2015-05-29 17:43:27 +01001442 struct i915_execbuffer_params params_master; /* XXX: will be removed later */
1443 struct i915_execbuffer_params *params = &params_master;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001444 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
John Harrison8e004ef2015-02-13 11:48:10 +00001445 u32 dispatch_flags;
Oscar Mateo78382592014-07-03 16:28:05 +01001446 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001447 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001448
Daniel Vettered5982e2013-01-17 22:23:36 +01001449 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001450 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001451
Chris Wilsonad19f102014-08-10 06:29:08 +01001452 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001453 if (ret)
1454 return ret;
1455
John Harrison8e004ef2015-02-13 11:48:10 +00001456 dispatch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001457 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02001458 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001459 return -EPERM;
1460
John Harrison8e004ef2015-02-13 11:48:10 +00001461 dispatch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001462 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001463 if (args->flags & I915_EXEC_IS_PINNED)
John Harrison8e004ef2015-02-13 11:48:10 +00001464 dispatch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001465
Dave Gordonf8ca0c02016-07-20 18:16:07 +01001466 engine = eb_select_engine(dev_priv, file, args);
1467 if (!engine)
1468 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001469
1470 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001471 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001472 return -EINVAL;
1473 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001474
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001475 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
1476 if (!HAS_RESOURCE_STREAMER(dev)) {
1477 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
1478 return -EINVAL;
1479 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001480 if (engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001481 DRM_DEBUG("RS is not available on %s\n",
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001482 engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001483 return -EINVAL;
1484 }
1485
1486 dispatch_flags |= I915_DISPATCH_RS;
1487 }
1488
Chris Wilson67d97da2016-07-04 08:08:31 +01001489 /* Take a local wakeref for preparing to dispatch the execbuf as
1490 * we expect to access the hardware fairly frequently in the
1491 * process. Upon first dispatch, we acquire another prolonged
1492 * wakeref that we hold until the GPU has been idle for at least
1493 * 100ms.
1494 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001495 intel_runtime_pm_get(dev_priv);
1496
Chris Wilson54cf91d2010-11-25 18:00:26 +00001497 ret = i915_mutex_lock_interruptible(dev);
1498 if (ret)
1499 goto pre_mutex_err;
1500
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001501 ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001502 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001503 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001504 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001505 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001506 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001507
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001508 i915_gem_context_get(ctx);
Ben Widawsky41bde552013-12-06 14:11:21 -08001509
Daniel Vetterae6c4802014-08-06 15:04:53 +02001510 if (ctx->ppgtt)
1511 vm = &ctx->ppgtt->base;
1512 else
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001513 vm = &ggtt->base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001514
John Harrison5f19e2b2015-05-29 17:43:27 +01001515 memset(&params_master, 0x00, sizeof(params_master));
1516
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001517 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001518 if (eb == NULL) {
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001519 i915_gem_context_put(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001520 mutex_unlock(&dev->struct_mutex);
1521 ret = -ENOMEM;
1522 goto pre_mutex_err;
1523 }
1524
Chris Wilson54cf91d2010-11-25 18:00:26 +00001525 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001526 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001527 if (ret)
1528 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001529
Chris Wilson6fe4f142011-01-10 17:35:37 +00001530 /* take note of the batch buffer before we might reorder the lists */
Chris Wilsond23db882014-05-23 08:48:08 +02001531 batch_obj = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001532
Chris Wilson54cf91d2010-11-25 18:00:26 +00001533 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001534 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001535 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
1536 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001537 if (ret)
1538 goto err;
1539
1540 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001541 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001542 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001543 if (ret) {
1544 if (ret == -EFAULT) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001545 ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
1546 engine,
David Weinehallb1b38272015-05-20 17:00:13 +03001547 eb, exec, ctx);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001548 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1549 }
1550 if (ret)
1551 goto err;
1552 }
1553
1554 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001555 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001556 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001557 ret = -EINVAL;
1558 goto err;
1559 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001560
John Harrison5f19e2b2015-05-29 17:43:27 +01001561 params->args_batch_start_offset = args->batch_start_offset;
Chris Wilson33a051a2016-07-27 09:07:26 +01001562 if (intel_engine_needs_cmd_parser(engine) && args->batch_len) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001563 struct drm_i915_gem_object *parsed_batch_obj;
1564
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001565 parsed_batch_obj = i915_gem_execbuffer_parse(engine,
1566 &shadow_exec_entry,
1567 eb,
1568 batch_obj,
1569 args->batch_start_offset,
1570 args->batch_len,
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02001571 drm_is_current_master(file));
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001572 if (IS_ERR(parsed_batch_obj)) {
1573 ret = PTR_ERR(parsed_batch_obj);
Brad Volkin78a42372014-12-11 12:13:09 -08001574 goto err;
1575 }
Chris Wilson17cabf52015-01-14 11:20:57 +00001576
1577 /*
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001578 * parsed_batch_obj == batch_obj means batch not fully parsed:
1579 * Accept, but don't promote to secure.
Chris Wilson17cabf52015-01-14 11:20:57 +00001580 */
Chris Wilson17cabf52015-01-14 11:20:57 +00001581
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001582 if (parsed_batch_obj != batch_obj) {
1583 /*
1584 * Batch parsed and accepted:
1585 *
1586 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1587 * bit from MI_BATCH_BUFFER_START commands issued in
1588 * the dispatch_execbuffer implementations. We
1589 * specifically don't want that set on batches the
1590 * command parser has accepted.
1591 */
1592 dispatch_flags |= I915_DISPATCH_SECURE;
John Harrison5f19e2b2015-05-29 17:43:27 +01001593 params->args_batch_start_offset = 0;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001594 batch_obj = parsed_batch_obj;
1595 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001596 }
1597
Brad Volkin78a42372014-12-11 12:13:09 -08001598 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1599
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001600 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1601 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001602 * hsw should have this fixed, but bdw mucks it up again. */
John Harrison8e004ef2015-02-13 11:48:10 +00001603 if (dispatch_flags & I915_DISPATCH_SECURE) {
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001604 /*
1605 * So on first glance it looks freaky that we pin the batch here
1606 * outside of the reservation loop. But:
1607 * - The batch is already pinned into the relevant ppgtt, so we
1608 * already have the backing storage fully allocated.
1609 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01001610 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001611 * fitting due to fragmentation.
1612 * So this is actually safe.
1613 */
1614 ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
1615 if (ret)
1616 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001617
John Harrison5f19e2b2015-05-29 17:43:27 +01001618 params->batch_obj_vm_offset = i915_gem_obj_ggtt_offset(batch_obj);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001619 } else
John Harrison5f19e2b2015-05-29 17:43:27 +01001620 params->batch_obj_vm_offset = i915_gem_obj_offset(batch_obj, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001621
John Harrison0c8dac82015-05-29 17:43:25 +01001622 /* Allocate a request for this batch buffer nice and early. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001623 req = i915_gem_request_alloc(engine, ctx);
Dave Gordon26827082016-01-19 19:02:53 +00001624 if (IS_ERR(req)) {
1625 ret = PTR_ERR(req);
John Harrison0c8dac82015-05-29 17:43:25 +01001626 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00001627 }
John Harrison0c8dac82015-05-29 17:43:25 +01001628
Dave Gordon26827082016-01-19 19:02:53 +00001629 ret = i915_gem_request_add_to_client(req, file);
John Harrisonfcfa423c2015-05-29 17:44:12 +01001630 if (ret)
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001631 goto err_request;
John Harrisonfcfa423c2015-05-29 17:44:12 +01001632
John Harrison5f19e2b2015-05-29 17:43:27 +01001633 /*
1634 * Save assorted stuff away to pass through to *_submission().
1635 * NB: This data should be 'persistent' and not local as it will
1636 * kept around beyond the duration of the IOCTL once the GPU
1637 * scheduler arrives.
1638 */
1639 params->dev = dev;
1640 params->file = file;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001641 params->engine = engine;
John Harrison5f19e2b2015-05-29 17:43:27 +01001642 params->dispatch_flags = dispatch_flags;
1643 params->batch_obj = batch_obj;
1644 params->ctx = ctx;
Dave Gordon26827082016-01-19 19:02:53 +00001645 params->request = req;
John Harrison5f19e2b2015-05-29 17:43:27 +01001646
1647 ret = dev_priv->gt.execbuf_submit(params, args, &eb->vmas);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001648err_request:
1649 i915_gem_execbuffer_retire_commands(params);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001650
John Harrison0c8dac82015-05-29 17:43:25 +01001651err_batch_unpin:
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001652 /*
1653 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1654 * batch vma for correctness. For less ugly and less fragility this
1655 * needs to be adjusted to also track the ggtt batch vma properly as
1656 * active.
1657 */
John Harrison8e004ef2015-02-13 11:48:10 +00001658 if (dispatch_flags & I915_DISPATCH_SECURE)
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001659 i915_gem_object_ggtt_unpin(batch_obj);
John Harrison0c8dac82015-05-29 17:43:25 +01001660
Chris Wilson54cf91d2010-11-25 18:00:26 +00001661err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001662 /* the request owns the ref now */
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001663 i915_gem_context_put(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001664 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001665
1666 mutex_unlock(&dev->struct_mutex);
1667
1668pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001669 /* intel_gpu_busy should also get a ref, so it will free when the device
1670 * is really idle. */
1671 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001672 return ret;
1673}
1674
1675/*
1676 * Legacy execbuffer just creates an exec2 list from the original exec object
1677 * list array and passes it to the real function.
1678 */
1679int
1680i915_gem_execbuffer(struct drm_device *dev, void *data,
1681 struct drm_file *file)
1682{
1683 struct drm_i915_gem_execbuffer *args = data;
1684 struct drm_i915_gem_execbuffer2 exec2;
1685 struct drm_i915_gem_exec_object *exec_list = NULL;
1686 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1687 int ret, i;
1688
Chris Wilson54cf91d2010-11-25 18:00:26 +00001689 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001690 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001691 return -EINVAL;
1692 }
1693
1694 /* Copy in the exec list from userland */
1695 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1696 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1697 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001698 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001699 args->buffer_count);
1700 drm_free_large(exec_list);
1701 drm_free_large(exec2_list);
1702 return -ENOMEM;
1703 }
1704 ret = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001705 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001706 sizeof(*exec_list) * args->buffer_count);
1707 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001708 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001709 args->buffer_count, ret);
1710 drm_free_large(exec_list);
1711 drm_free_large(exec2_list);
1712 return -EFAULT;
1713 }
1714
1715 for (i = 0; i < args->buffer_count; i++) {
1716 exec2_list[i].handle = exec_list[i].handle;
1717 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1718 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1719 exec2_list[i].alignment = exec_list[i].alignment;
1720 exec2_list[i].offset = exec_list[i].offset;
1721 if (INTEL_INFO(dev)->gen < 4)
1722 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1723 else
1724 exec2_list[i].flags = 0;
1725 }
1726
1727 exec2.buffers_ptr = args->buffers_ptr;
1728 exec2.buffer_count = args->buffer_count;
1729 exec2.batch_start_offset = args->batch_start_offset;
1730 exec2.batch_len = args->batch_len;
1731 exec2.DR1 = args->DR1;
1732 exec2.DR4 = args->DR4;
1733 exec2.num_cliprects = args->num_cliprects;
1734 exec2.cliprects_ptr = args->cliprects_ptr;
1735 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001736 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001737
Ben Widawsky41bde552013-12-06 14:11:21 -08001738 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001739 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001740 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001741 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001742
Chris Wilson54cf91d2010-11-25 18:00:26 +00001743 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001744 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001745 exec2_list[i].offset =
1746 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001747 ret = __copy_to_user(&user_exec_list[i].offset,
1748 &exec2_list[i].offset,
1749 sizeof(user_exec_list[i].offset));
1750 if (ret) {
1751 ret = -EFAULT;
1752 DRM_DEBUG("failed to copy %d exec entries "
1753 "back to user (%d)\n",
1754 args->buffer_count, ret);
1755 break;
1756 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001757 }
1758 }
1759
1760 drm_free_large(exec_list);
1761 drm_free_large(exec2_list);
1762 return ret;
1763}
1764
1765int
1766i915_gem_execbuffer2(struct drm_device *dev, void *data,
1767 struct drm_file *file)
1768{
1769 struct drm_i915_gem_execbuffer2 *args = data;
1770 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1771 int ret;
1772
Xi Wanged8cd3b2012-04-23 04:06:41 -04001773 if (args->buffer_count < 1 ||
1774 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001775 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001776 return -EINVAL;
1777 }
1778
Daniel Vetter9cb34662014-04-24 08:09:11 +02001779 if (args->rsvd2 != 0) {
1780 DRM_DEBUG("dirty rvsd2 field\n");
1781 return -EINVAL;
1782 }
1783
Chris Wilsonf2a85e12016-04-08 12:11:13 +01001784 exec2_list = drm_malloc_gfp(args->buffer_count,
1785 sizeof(*exec2_list),
1786 GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001787 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001788 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001789 args->buffer_count);
1790 return -ENOMEM;
1791 }
1792 ret = copy_from_user(exec2_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001793 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001794 sizeof(*exec2_list) * args->buffer_count);
1795 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001796 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001797 args->buffer_count, ret);
1798 drm_free_large(exec2_list);
1799 return -EFAULT;
1800 }
1801
Ben Widawsky41bde552013-12-06 14:11:21 -08001802 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001803 if (!ret) {
1804 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001805 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001806 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001807 int i;
1808
1809 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001810 exec2_list[i].offset =
1811 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001812 ret = __copy_to_user(&user_exec_list[i].offset,
1813 &exec2_list[i].offset,
1814 sizeof(user_exec_list[i].offset));
1815 if (ret) {
1816 ret = -EFAULT;
1817 DRM_DEBUG("failed to copy %d exec entries "
1818 "back to user\n",
1819 args->buffer_count);
1820 break;
1821 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001822 }
1823 }
1824
1825 drm_free_large(exec2_list);
1826 return ret;
1827}