blob: 1bb1f251eafcf1522d6e77b90cc2630d94cbb9ce [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
126 drm_gem_object_reference(&obj->base);
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 Wilson9ae9ab52013-12-04 09:52:58 +0000179 drm_gem_object_unreference(&obj->base);
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
Ben Widawsky27173f12013-08-14 11:38:36 +0200189static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000190{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000191 if (eb->and < 0) {
192 if (handle >= -eb->and)
193 return NULL;
194 return eb->lut[handle];
195 } else {
196 struct hlist_head *head;
Geliang Tangaa459502016-01-18 23:54:20 +0800197 struct i915_vma *vma;
Chris Wilson67731b82010-12-08 10:38:14 +0000198
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000199 head = &eb->buckets[handle & eb->and];
Geliang Tangaa459502016-01-18 23:54:20 +0800200 hlist_for_each_entry(vma, head, exec_node) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200201 if (vma->exec_handle == handle)
202 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000203 }
204 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000205 }
Chris Wilson67731b82010-12-08 10:38:14 +0000206}
207
Chris Wilsona415d352013-11-26 11:23:15 +0000208static void
209i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
210{
211 struct drm_i915_gem_exec_object2 *entry;
212 struct drm_i915_gem_object *obj = vma->obj;
213
214 if (!drm_mm_node_allocated(&vma->node))
215 return;
216
217 entry = vma->exec_entry;
218
219 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
220 i915_gem_object_unpin_fence(obj);
221
222 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Daniel Vetter3d7f0f92013-12-18 16:23:37 +0100223 vma->pin_count--;
Chris Wilsona415d352013-11-26 11:23:15 +0000224
Chris Wilsonde4e7832015-04-07 16:20:35 +0100225 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilsona415d352013-11-26 11:23:15 +0000226}
227
228static void eb_destroy(struct eb_vmas *eb)
229{
Ben Widawsky27173f12013-08-14 11:38:36 +0200230 while (!list_empty(&eb->vmas)) {
231 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000232
Ben Widawsky27173f12013-08-14 11:38:36 +0200233 vma = list_first_entry(&eb->vmas,
234 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000235 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200236 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000237 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200238 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000239 }
Chris Wilson67731b82010-12-08 10:38:14 +0000240 kfree(eb);
241}
242
Chris Wilsondabdfe02012-03-26 10:10:27 +0200243static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
244{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300245 return (HAS_LLC(obj->base.dev) ||
246 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200247 obj->cache_level != I915_CACHE_NONE);
248}
249
Michał Winiarski934acce2015-12-29 18:24:52 +0100250/* Used to convert any address to canonical form.
251 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
252 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
253 * addresses to be in a canonical form:
254 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
255 * canonical form [63:48] == [47]."
256 */
257#define GEN8_HIGH_ADDRESS_BIT 47
258static inline uint64_t gen8_canonical_addr(uint64_t address)
259{
260 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
261}
262
263static inline uint64_t gen8_noncanonical_addr(uint64_t address)
264{
265 return address & ((1ULL << (GEN8_HIGH_ADDRESS_BIT + 1)) - 1);
266}
267
268static inline uint64_t
269relocation_target(struct drm_i915_gem_relocation_entry *reloc,
270 uint64_t target_offset)
271{
272 return gen8_canonical_addr((int)reloc->delta + target_offset);
273}
274
Chris Wilson54cf91d2010-11-25 18:00:26 +0000275static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100276relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700277 struct drm_i915_gem_relocation_entry *reloc,
278 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100279{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700280 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100281 uint32_t page_offset = offset_in_page(reloc->offset);
Michał Winiarski934acce2015-12-29 18:24:52 +0100282 uint64_t delta = relocation_target(reloc, target_offset);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100283 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800284 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100285
Chris Wilson2cc86b82013-08-26 19:51:00 -0300286 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100287 if (ret)
288 return ret;
289
Dave Gordon033908a2015-12-10 18:51:23 +0000290 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Rafael Barbalho5032d872013-08-21 17:10:51 +0100291 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700292 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700293
294 if (INTEL_INFO(dev)->gen >= 8) {
295 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
296
297 if (page_offset == 0) {
298 kunmap_atomic(vaddr);
Dave Gordon033908a2015-12-10 18:51:23 +0000299 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700300 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
301 }
302
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700303 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700304 }
305
Rafael Barbalho5032d872013-08-21 17:10:51 +0100306 kunmap_atomic(vaddr);
307
308 return 0;
309}
310
311static int
312relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700313 struct drm_i915_gem_relocation_entry *reloc,
314 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100315{
316 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300317 struct drm_i915_private *dev_priv = to_i915(dev);
318 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michał Winiarski934acce2015-12-29 18:24:52 +0100319 uint64_t delta = relocation_target(reloc, target_offset);
Chris Wilson906843c2014-08-10 06:29:11 +0100320 uint64_t offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100321 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800322 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100323
324 ret = i915_gem_object_set_to_gtt_domain(obj, true);
325 if (ret)
326 return ret;
327
328 ret = i915_gem_object_put_fence(obj);
329 if (ret)
330 return ret;
331
332 /* Map the page containing the relocation we're going to perform. */
Chris Wilson906843c2014-08-10 06:29:11 +0100333 offset = i915_gem_obj_ggtt_offset(obj);
334 offset += reloc->offset;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300335 reloc_page = io_mapping_map_atomic_wc(ggtt->mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100336 offset & PAGE_MASK);
337 iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700338
339 if (INTEL_INFO(dev)->gen >= 8) {
Chris Wilson906843c2014-08-10 06:29:11 +0100340 offset += sizeof(uint32_t);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700341
Chris Wilson906843c2014-08-10 06:29:11 +0100342 if (offset_in_page(offset) == 0) {
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700343 io_mapping_unmap_atomic(reloc_page);
Chris Wilson906843c2014-08-10 06:29:11 +0100344 reloc_page =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300345 io_mapping_map_atomic_wc(ggtt->mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100346 offset);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700347 }
348
Chris Wilson906843c2014-08-10 06:29:11 +0100349 iowrite32(upper_32_bits(delta),
350 reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700351 }
352
Rafael Barbalho5032d872013-08-21 17:10:51 +0100353 io_mapping_unmap_atomic(reloc_page);
354
355 return 0;
356}
357
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000358static void
359clflush_write32(void *addr, uint32_t value)
360{
361 /* This is not a fast path, so KISS. */
362 drm_clflush_virt_range(addr, sizeof(uint32_t));
363 *(uint32_t *)addr = value;
364 drm_clflush_virt_range(addr, sizeof(uint32_t));
365}
366
367static int
368relocate_entry_clflush(struct drm_i915_gem_object *obj,
369 struct drm_i915_gem_relocation_entry *reloc,
370 uint64_t target_offset)
371{
372 struct drm_device *dev = obj->base.dev;
373 uint32_t page_offset = offset_in_page(reloc->offset);
Michał Winiarski934acce2015-12-29 18:24:52 +0100374 uint64_t delta = relocation_target(reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000375 char *vaddr;
376 int ret;
377
378 ret = i915_gem_object_set_to_gtt_domain(obj, true);
379 if (ret)
380 return ret;
381
Dave Gordon033908a2015-12-10 18:51:23 +0000382 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000383 reloc->offset >> PAGE_SHIFT));
384 clflush_write32(vaddr + page_offset, lower_32_bits(delta));
385
386 if (INTEL_INFO(dev)->gen >= 8) {
387 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
388
389 if (page_offset == 0) {
390 kunmap_atomic(vaddr);
Dave Gordon033908a2015-12-10 18:51:23 +0000391 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000392 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
393 }
394
395 clflush_write32(vaddr + page_offset, upper_32_bits(delta));
396 }
397
398 kunmap_atomic(vaddr);
399
400 return 0;
401}
402
Rafael Barbalho5032d872013-08-21 17:10:51 +0100403static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000404i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200405 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800406 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000407{
408 struct drm_device *dev = obj->base.dev;
409 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100410 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200411 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700412 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800413 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000414
Chris Wilson67731b82010-12-08 10:38:14 +0000415 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200416 target_vma = eb_get_vma(eb, reloc->target_handle);
417 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000418 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200419 target_i915_obj = target_vma->obj;
420 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000421
Michał Winiarski934acce2015-12-29 18:24:52 +0100422 target_offset = gen8_canonical_addr(target_vma->node.start);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000423
Eric Anholte844b992012-07-31 15:35:01 -0700424 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
425 * pipe_control writes because the gpu doesn't properly redirect them
426 * through the ppgtt for non_secure batchbuffers. */
427 if (unlikely(IS_GEN6(dev) &&
Daniel Vetter08755462015-04-20 09:04:05 -0700428 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000429 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
Daniel Vetter08755462015-04-20 09:04:05 -0700430 PIN_GLOBAL);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000431 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
432 return ret;
433 }
Eric Anholte844b992012-07-31 15:35:01 -0700434
Chris Wilson54cf91d2010-11-25 18:00:26 +0000435 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000436 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100437 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000438 "obj %p target %d offset %d "
439 "read %08x write %08x",
440 obj, reloc->target_handle,
441 (int) reloc->offset,
442 reloc->read_domains,
443 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800444 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000445 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100446 if (unlikely((reloc->write_domain | reloc->read_domains)
447 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100448 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000449 "obj %p target %d offset %d "
450 "read %08x write %08x",
451 obj, reloc->target_handle,
452 (int) reloc->offset,
453 reloc->read_domains,
454 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800455 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000456 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000457
458 target_obj->pending_read_domains |= reloc->read_domains;
459 target_obj->pending_write_domain |= reloc->write_domain;
460
461 /* If the relocation already has the right value in it, no
462 * more work needs to be done.
463 */
464 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000465 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000466
467 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700468 if (unlikely(reloc->offset >
469 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100470 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000471 "obj %p target %d offset %d size %d.\n",
472 obj, reloc->target_handle,
473 (int) reloc->offset,
474 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800475 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000476 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000477 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100478 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000479 "obj %p target %d offset %d.\n",
480 obj, reloc->target_handle,
481 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800482 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000483 }
484
Chris Wilsondabdfe02012-03-26 10:10:27 +0200485 /* We can't wait for rendering with pagefaults disabled */
David Hildenbrand32d82062015-05-11 17:52:12 +0200486 if (obj->active && pagefault_disabled())
Chris Wilsondabdfe02012-03-26 10:10:27 +0200487 return -EFAULT;
488
Rafael Barbalho5032d872013-08-21 17:10:51 +0100489 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700490 ret = relocate_entry_cpu(obj, reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000491 else if (obj->map_and_fenceable)
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700492 ret = relocate_entry_gtt(obj, reloc, target_offset);
Borislav Petkov906bf7f2016-03-29 17:41:59 +0200493 else if (static_cpu_has(X86_FEATURE_CLFLUSH))
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000494 ret = relocate_entry_clflush(obj, reloc, target_offset);
495 else {
496 WARN_ONCE(1, "Impossible case in relocation handling\n");
497 ret = -ENODEV;
498 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000499
Daniel Vetterd4d36012013-09-02 20:56:23 +0200500 if (ret)
501 return ret;
502
Chris Wilson54cf91d2010-11-25 18:00:26 +0000503 /* and update the user's relocation entry */
504 reloc->presumed_offset = target_offset;
505
Chris Wilson67731b82010-12-08 10:38:14 +0000506 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000507}
508
509static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200510i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
511 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000512{
Chris Wilson1d83f442012-03-24 20:12:53 +0000513#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
514 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000515 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200516 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000517 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000518
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300519 user_relocs = u64_to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000520
Chris Wilson1d83f442012-03-24 20:12:53 +0000521 remain = entry->relocation_count;
522 while (remain) {
523 struct drm_i915_gem_relocation_entry *r = stack_reloc;
524 int count = remain;
525 if (count > ARRAY_SIZE(stack_reloc))
526 count = ARRAY_SIZE(stack_reloc);
527 remain -= count;
528
529 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000530 return -EFAULT;
531
Chris Wilson1d83f442012-03-24 20:12:53 +0000532 do {
533 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000534
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800535 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000536 if (ret)
537 return ret;
538
539 if (r->presumed_offset != offset &&
Linus Torvalds5b09c3e2016-05-22 14:19:37 -0700540 __put_user(r->presumed_offset, &user_relocs->presumed_offset)) {
Chris Wilson1d83f442012-03-24 20:12:53 +0000541 return -EFAULT;
542 }
543
544 user_relocs++;
545 r++;
546 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000547 }
548
549 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000550#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000551}
552
553static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200554i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
555 struct eb_vmas *eb,
556 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000557{
Ben Widawsky27173f12013-08-14 11:38:36 +0200558 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000559 int i, ret;
560
561 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800562 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000563 if (ret)
564 return ret;
565 }
566
567 return 0;
568}
569
570static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800571i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000572{
Ben Widawsky27173f12013-08-14 11:38:36 +0200573 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000574 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000575
Chris Wilsond4aeee72011-03-14 15:11:24 +0000576 /* This is the fast path and we cannot handle a pagefault whilst
577 * holding the struct mutex lest the user pass in the relocations
578 * contained within a mmaped bo. For in such a case we, the page
579 * fault handler would call i915_gem_fault() and we would try to
580 * acquire the struct mutex again. Obviously this is bad and so
581 * lockdep complains vehemently.
582 */
583 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200584 list_for_each_entry(vma, &eb->vmas, exec_list) {
585 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000586 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000587 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000588 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000589 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000590
Chris Wilsond4aeee72011-03-14 15:11:24 +0000591 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000592}
593
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000594static bool only_mappable_for_reloc(unsigned int flags)
595{
596 return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
597 __EXEC_OBJECT_NEEDS_MAP;
598}
599
Chris Wilson1690e1e2011-12-14 13:57:08 +0100600static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200601i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000602 struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200603 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100604{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800605 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200606 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200607 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100608 int ret;
609
Daniel Vetter08755462015-04-20 09:04:05 -0700610 flags = PIN_USER;
Daniel Vetter0229da32015-04-14 19:01:54 +0200611 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
612 flags |= PIN_GLOBAL;
613
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000614 if (!drm_mm_node_allocated(&vma->node)) {
Michel Thierry101b5062015-10-01 13:33:57 +0100615 /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
616 * limit address to the first 4GBs for unflagged objects.
617 */
618 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
619 flags |= PIN_ZONE_4G;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000620 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
621 flags |= PIN_GLOBAL | PIN_MAPPABLE;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000622 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
623 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson506a8e82015-12-08 11:55:07 +0000624 if (entry->flags & EXEC_OBJECT_PINNED)
625 flags |= entry->offset | PIN_OFFSET_FIXED;
Michel Thierry101b5062015-10-01 13:33:57 +0100626 if ((flags & PIN_MAPPABLE) == 0)
627 flags |= PIN_HIGH;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000628 }
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100629
630 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000631 if ((ret == -ENOSPC || ret == -E2BIG) &&
632 only_mappable_for_reloc(entry->flags))
633 ret = i915_gem_object_pin(obj, vma->vm,
634 entry->alignment,
Daniel Vetter0229da32015-04-14 19:01:54 +0200635 flags & ~PIN_MAPPABLE);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100636 if (ret)
637 return ret;
638
Chris Wilson7788a762012-08-24 19:18:18 +0100639 entry->flags |= __EXEC_OBJECT_HAS_PIN;
640
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100641 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
642 ret = i915_gem_object_get_fence(obj);
643 if (ret)
644 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100645
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100646 if (i915_gem_object_pin_fence(obj))
647 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100648 }
649
Ben Widawsky27173f12013-08-14 11:38:36 +0200650 if (entry->offset != vma->node.start) {
651 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100652 *need_reloc = true;
653 }
654
655 if (entry->flags & EXEC_OBJECT_WRITE) {
656 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
657 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
658 }
659
Chris Wilson1690e1e2011-12-14 13:57:08 +0100660 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100661}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100662
Chris Wilsond23db882014-05-23 08:48:08 +0200663static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200664need_reloc_mappable(struct i915_vma *vma)
665{
666 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
667
668 if (entry->relocation_count == 0)
669 return false;
670
Chris Wilson596c5922016-02-26 11:03:20 +0000671 if (!vma->is_ggtt)
Chris Wilsone6a84462014-08-11 12:00:12 +0200672 return false;
673
674 /* See also use_cpu_reloc() */
675 if (HAS_LLC(vma->obj->base.dev))
676 return false;
677
678 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
679 return false;
680
681 return true;
682}
683
684static bool
685eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200686{
687 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
688 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond23db882014-05-23 08:48:08 +0200689
Chris Wilson596c5922016-02-26 11:03:20 +0000690 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP && !vma->is_ggtt);
Chris Wilsond23db882014-05-23 08:48:08 +0200691
692 if (entry->alignment &&
693 vma->node.start & (entry->alignment - 1))
694 return true;
695
Chris Wilson506a8e82015-12-08 11:55:07 +0000696 if (entry->flags & EXEC_OBJECT_PINNED &&
697 vma->node.start != entry->offset)
698 return true;
699
Chris Wilsond23db882014-05-23 08:48:08 +0200700 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
701 vma->node.start < BATCH_OFFSET_BIAS)
702 return true;
703
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000704 /* avoid costly ping-pong once a batch bo ended up non-mappable */
705 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
706 return !only_mappable_for_reloc(entry->flags);
707
Michel Thierry101b5062015-10-01 13:33:57 +0100708 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
709 (vma->node.start + vma->node.size - 1) >> 32)
710 return true;
711
Chris Wilsond23db882014-05-23 08:48:08 +0200712 return false;
713}
714
Chris Wilson54cf91d2010-11-25 18:00:26 +0000715static int
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000716i915_gem_execbuffer_reserve(struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200717 struct list_head *vmas,
Chris Wilsone2efd132016-05-24 14:53:34 +0100718 struct i915_gem_context *ctx,
Daniel Vettered5982e2013-01-17 22:23:36 +0100719 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000720{
Chris Wilson432e58e2010-11-25 19:32:06 +0000721 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200722 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700723 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200724 struct list_head ordered_vmas;
Chris Wilson506a8e82015-12-08 11:55:07 +0000725 struct list_head pinned_vmas;
Chris Wilsonc0336662016-05-06 15:40:21 +0100726 bool has_fenced_gpu_access = INTEL_GEN(engine->i915) < 4;
Chris Wilson7788a762012-08-24 19:18:18 +0100727 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000728
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000729 i915_gem_retire_requests_ring(engine);
Chris Wilson227f7822014-05-15 10:41:42 +0100730
Ben Widawsky68c8c172013-09-11 14:57:50 -0700731 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
732
Ben Widawsky27173f12013-08-14 11:38:36 +0200733 INIT_LIST_HEAD(&ordered_vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000734 INIT_LIST_HEAD(&pinned_vmas);
Ben Widawsky27173f12013-08-14 11:38:36 +0200735 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000736 struct drm_i915_gem_exec_object2 *entry;
737 bool need_fence, need_mappable;
738
Ben Widawsky27173f12013-08-14 11:38:36 +0200739 vma = list_first_entry(vmas, struct i915_vma, exec_list);
740 obj = vma->obj;
741 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000742
David Weinehallb1b38272015-05-20 17:00:13 +0300743 if (ctx->flags & CONTEXT_NO_ZEROMAP)
744 entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
745
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100746 if (!has_fenced_gpu_access)
747 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000748 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000749 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
750 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200751 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000752
Chris Wilson506a8e82015-12-08 11:55:07 +0000753 if (entry->flags & EXEC_OBJECT_PINNED)
754 list_move_tail(&vma->exec_list, &pinned_vmas);
755 else if (need_mappable) {
Chris Wilsone6a84462014-08-11 12:00:12 +0200756 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200757 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200758 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200759 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000760
Daniel Vettered5982e2013-01-17 22:23:36 +0100761 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000762 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000763 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200764 list_splice(&ordered_vmas, vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000765 list_splice(&pinned_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000766
767 /* Attempt to pin all of the buffers into the GTT.
768 * This is done in 3 phases:
769 *
770 * 1a. Unbind all objects that do not match the GTT constraints for
771 * the execbuffer (fenceable, mappable, alignment etc).
772 * 1b. Increment pin count for already bound objects.
773 * 2. Bind new objects.
774 * 3. Decrement pin count.
775 *
Chris Wilson7788a762012-08-24 19:18:18 +0100776 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000777 * room for the earlier objects *unless* we need to defragment.
778 */
779 retry = 0;
780 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100781 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000782
783 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200784 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200785 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000786 continue;
787
Chris Wilsone6a84462014-08-11 12:00:12 +0200788 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200789 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000790 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000791 ret = i915_gem_execbuffer_reserve_vma(vma,
792 engine,
793 need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000794 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000795 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000796 }
797
798 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200799 list_for_each_entry(vma, vmas, exec_list) {
800 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100801 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000802
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000803 ret = i915_gem_execbuffer_reserve_vma(vma, engine,
804 need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100805 if (ret)
806 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000807 }
808
Chris Wilsona415d352013-11-26 11:23:15 +0000809err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200810 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000811 return ret;
812
Chris Wilsona415d352013-11-26 11:23:15 +0000813 /* Decrement pin count for bound objects */
814 list_for_each_entry(vma, vmas, exec_list)
815 i915_gem_execbuffer_unreserve_vma(vma);
816
Ben Widawsky68c8c172013-09-11 14:57:50 -0700817 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000818 if (ret)
819 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000820 } while (1);
821}
822
823static int
824i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100825 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000826 struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000827 struct intel_engine_cs *engine,
Ben Widawsky27173f12013-08-14 11:38:36 +0200828 struct eb_vmas *eb,
David Weinehallb1b38272015-05-20 17:00:13 +0300829 struct drm_i915_gem_exec_object2 *exec,
Chris Wilsone2efd132016-05-24 14:53:34 +0100830 struct i915_gem_context *ctx)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000831{
832 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200833 struct i915_address_space *vm;
834 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100835 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000836 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000837 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200838 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000839
Ben Widawsky27173f12013-08-14 11:38:36 +0200840 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
841
Chris Wilson67731b82010-12-08 10:38:14 +0000842 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200843 while (!list_empty(&eb->vmas)) {
844 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
845 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000846 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200847 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000848 }
849
Chris Wilson54cf91d2010-11-25 18:00:26 +0000850 mutex_unlock(&dev->struct_mutex);
851
852 total = 0;
853 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000854 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000855
Chris Wilsondd6864a2011-01-12 23:49:13 +0000856 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000857 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000858 if (reloc == NULL || reloc_offset == NULL) {
859 drm_free_large(reloc);
860 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000861 mutex_lock(&dev->struct_mutex);
862 return -ENOMEM;
863 }
864
865 total = 0;
866 for (i = 0; i < count; i++) {
867 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000868 u64 invalid_offset = (u64)-1;
869 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000870
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300871 user_relocs = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000872
873 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000874 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000875 ret = -EFAULT;
876 mutex_lock(&dev->struct_mutex);
877 goto err;
878 }
879
Chris Wilson262b6d32013-01-15 16:17:54 +0000880 /* As we do not update the known relocation offsets after
881 * relocating (due to the complexities in lock handling),
882 * we need to mark them as invalid now so that we force the
883 * relocation processing next time. Just in case the target
884 * object is evicted and then rebound into its old
885 * presumed_offset before the next execbuffer - if that
886 * happened we would make the mistake of assuming that the
887 * relocations were valid.
888 */
889 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100890 if (__copy_to_user(&user_relocs[j].presumed_offset,
891 &invalid_offset,
892 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000893 ret = -EFAULT;
894 mutex_lock(&dev->struct_mutex);
895 goto err;
896 }
897 }
898
Chris Wilsondd6864a2011-01-12 23:49:13 +0000899 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000900 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000901 }
902
903 ret = i915_mutex_lock_interruptible(dev);
904 if (ret) {
905 mutex_lock(&dev->struct_mutex);
906 goto err;
907 }
908
Chris Wilson67731b82010-12-08 10:38:14 +0000909 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000910 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200911 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000912 if (ret)
913 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000914
Daniel Vettered5982e2013-01-17 22:23:36 +0100915 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000916 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
917 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000918 if (ret)
919 goto err;
920
Ben Widawsky27173f12013-08-14 11:38:36 +0200921 list_for_each_entry(vma, &eb->vmas, exec_list) {
922 int offset = vma->exec_entry - exec;
923 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
924 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000925 if (ret)
926 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000927 }
928
929 /* Leave the user relocations as are, this is the painfully slow path,
930 * and we want to avoid the complication of dropping the lock whilst
931 * having buffers reserved in the aperture and so causing spurious
932 * ENOSPC for random operations.
933 */
934
935err:
936 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000937 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000938 return ret;
939}
940
Chris Wilson54cf91d2010-11-25 18:00:26 +0000941static int
John Harrison535fbe82015-05-29 17:43:32 +0100942i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
Ben Widawsky27173f12013-08-14 11:38:36 +0200943 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000944{
Tvrtko Ursulin666796d2016-03-16 11:00:39 +0000945 const unsigned other_rings = ~intel_engine_flag(req->engine);
Ben Widawsky27173f12013-08-14 11:38:36 +0200946 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200947 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100948 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000949 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000950
Ben Widawsky27173f12013-08-14 11:38:36 +0200951 list_for_each_entry(vma, vmas, exec_list) {
952 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +0100953
954 if (obj->active & other_rings) {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000955 ret = i915_gem_object_sync(obj, req->engine, &req);
Chris Wilson03ade512015-04-27 13:41:18 +0100956 if (ret)
957 return ret;
958 }
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200959
960 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100961 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200962
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200963 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000964 }
965
Chris Wilson000433b2013-08-08 14:41:09 +0100966 if (flush_chipset)
Chris Wilsonc0336662016-05-06 15:40:21 +0100967 i915_gem_chipset_flush(req->engine->i915);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200968
969 if (flush_domains & I915_GEM_DOMAIN_GTT)
970 wmb();
971
Chris Wilson09cf7c92012-07-13 14:14:08 +0100972 /* Unconditionally invalidate gpu caches and ensure that we do flush
973 * any residual writes from the previous batch.
974 */
John Harrison2f200552015-05-29 17:43:53 +0100975 return intel_ring_invalidate_all_caches(req);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000976}
977
Chris Wilson432e58e2010-11-25 19:32:06 +0000978static bool
979i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000980{
Daniel Vettered5982e2013-01-17 22:23:36 +0100981 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
982 return false;
983
Chris Wilson2f5945b2015-10-06 11:39:55 +0100984 /* Kernel clipping was a DRI1 misfeature */
985 if (exec->num_cliprects || exec->cliprects_ptr)
986 return false;
987
988 if (exec->DR4 == 0xffffffff) {
989 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
990 exec->DR4 = 0;
991 }
992 if (exec->DR1 || exec->DR4)
993 return false;
994
995 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
996 return false;
997
998 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000999}
1000
1001static int
Chris Wilsonad19f102014-08-10 06:29:08 +01001002validate_exec_list(struct drm_device *dev,
1003 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001004 int count)
1005{
Daniel Vetterb205ca52013-09-19 14:00:11 +02001006 unsigned relocs_total = 0;
1007 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +01001008 unsigned invalid_flags;
1009 int i;
1010
Dave Gordon9e2793f62016-07-14 14:52:03 +01001011 /* INTERNAL flags must not overlap with external ones */
1012 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
1013
Chris Wilsonad19f102014-08-10 06:29:08 +01001014 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
1015 if (USES_FULL_PPGTT(dev))
1016 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001017
1018 for (i = 0; i < count; i++) {
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001019 char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001020 int length; /* limited by fault_in_pages_readable() */
1021
Chris Wilsonad19f102014-08-10 06:29:08 +01001022 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +01001023 return -EINVAL;
1024
Michał Winiarski934acce2015-12-29 18:24:52 +01001025 /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
1026 * any non-page-aligned or non-canonical addresses.
1027 */
1028 if (exec[i].flags & EXEC_OBJECT_PINNED) {
1029 if (exec[i].offset !=
1030 gen8_canonical_addr(exec[i].offset & PAGE_MASK))
1031 return -EINVAL;
1032
1033 /* From drm_mm perspective address space is continuous,
1034 * so from this point we're always using non-canonical
1035 * form internally.
1036 */
1037 exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
1038 }
1039
Chris Wilson55a97852015-06-19 13:59:46 +01001040 if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
1041 return -EINVAL;
1042
Kees Cook3118a4f2013-03-11 17:31:45 -07001043 /* First check for malicious input causing overflow in
1044 * the worst case where we need to allocate the entire
1045 * relocation tree as a single array.
1046 */
1047 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001048 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -07001049 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001050
1051 length = exec[i].relocation_count *
1052 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -07001053 /*
1054 * We must check that the entire relocation array is safe
1055 * to read, but since we may need to update the presumed
1056 * offsets during execution, check for full write access.
1057 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001058 if (!access_ok(VERIFY_WRITE, ptr, length))
1059 return -EFAULT;
1060
Jani Nikulad330a952014-01-21 11:24:25 +02001061 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +08001062 if (fault_in_multipages_readable(ptr, length))
1063 return -EFAULT;
1064 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001065 }
1066
1067 return 0;
1068}
1069
Chris Wilsone2efd132016-05-24 14:53:34 +01001070static struct i915_gem_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001071i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001072 struct intel_engine_cs *engine, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001073{
Chris Wilsone2efd132016-05-24 14:53:34 +01001074 struct i915_gem_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001075 struct i915_ctx_hang_stats *hs;
1076
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001077 if (engine->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001078 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001079
Chris Wilsonca585b52016-05-24 14:53:36 +01001080 ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001081 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -08001082 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001083
Ben Widawsky41bde552013-12-06 14:11:21 -08001084 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001085 if (hs->banned) {
1086 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -08001087 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001088 }
1089
Ben Widawsky41bde552013-12-06 14:11:21 -08001090 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001091}
1092
Oscar Mateoba8b7cc2014-07-24 17:04:33 +01001093void
Ben Widawsky27173f12013-08-14 11:38:36 +02001094i915_gem_execbuffer_move_to_active(struct list_head *vmas,
John Harrison8a8edb52015-05-29 17:43:33 +01001095 struct drm_i915_gem_request *req)
Chris Wilson432e58e2010-11-25 19:32:06 +00001096{
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001097 struct intel_engine_cs *engine = i915_gem_request_get_engine(req);
Ben Widawsky27173f12013-08-14 11:38:36 +02001098 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +00001099
Ben Widawsky27173f12013-08-14 11:38:36 +02001100 list_for_each_entry(vma, vmas, exec_list) {
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001101 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Ben Widawsky27173f12013-08-14 11:38:36 +02001102 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +01001103 u32 old_read = obj->base.read_domains;
1104 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +00001105
Chris Wilson51bc1402015-08-31 15:10:39 +01001106 obj->dirty = 1; /* be paranoid */
Chris Wilson432e58e2010-11-25 19:32:06 +00001107 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +01001108 if (obj->base.write_domain == 0)
1109 obj->base.pending_read_domains |= obj->base.read_domains;
1110 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +00001111
John Harrisonb2af0372015-05-29 17:43:50 +01001112 i915_vma_move_to_active(vma, req);
Chris Wilson432e58e2010-11-25 19:32:06 +00001113 if (obj->base.write_domain) {
John Harrison97b2a6a2014-11-24 18:49:26 +00001114 i915_gem_request_assign(&obj->last_write_req, req);
Daniel Vetterf99d7062014-06-19 16:01:59 +02001115
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -07001116 intel_fb_obj_invalidate(obj, ORIGIN_CS);
Chris Wilsonc8725f32014-03-17 12:21:55 +00001117
1118 /* update for the implicit flush after a batch */
1119 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson432e58e2010-11-25 19:32:06 +00001120 }
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001121 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
John Harrison97b2a6a2014-11-24 18:49:26 +00001122 i915_gem_request_assign(&obj->last_fenced_req, req);
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001123 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
Chris Wilsonc0336662016-05-06 15:40:21 +01001124 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001125 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
1126 &dev_priv->mm.fence_list);
1127 }
1128 }
Chris Wilson432e58e2010-11-25 19:32:06 +00001129
Chris Wilsondb53a302011-02-03 11:57:46 +00001130 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +00001131 }
1132}
1133
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001134static void
John Harrisonadeca762015-05-29 17:43:28 +01001135i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params *params)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001136{
Daniel Vettercc889e02012-06-13 20:45:19 +02001137 /* Unconditionally force add_request to emit a full flush. */
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001138 params->engine->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001139
Chris Wilson432e58e2010-11-25 19:32:06 +00001140 /* Add a breadcrumb for the completion of the batch buffer */
John Harrisonfcfa423c2015-05-29 17:44:12 +01001141 __i915_add_request(params->request, params->batch_obj, true);
Chris Wilson432e58e2010-11-25 19:32:06 +00001142}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001143
1144static int
Eric Anholtae662d32012-01-03 09:23:29 -08001145i915_reset_gen7_sol_offsets(struct drm_device *dev,
John Harrison2f200552015-05-29 17:43:53 +01001146 struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001147{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001148 struct intel_engine_cs *engine = req->engine;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001149 struct drm_i915_private *dev_priv = to_i915(dev);
Eric Anholtae662d32012-01-03 09:23:29 -08001150 int ret, i;
1151
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001152 if (!IS_GEN7(dev) || engine != &dev_priv->engine[RCS]) {
Daniel Vetter9d662da2014-04-24 08:09:09 +02001153 DRM_DEBUG("sol reset is gen7/rcs only\n");
1154 return -EINVAL;
1155 }
Eric Anholtae662d32012-01-03 09:23:29 -08001156
John Harrison5fb9de12015-05-29 17:44:07 +01001157 ret = intel_ring_begin(req, 4 * 3);
Eric Anholtae662d32012-01-03 09:23:29 -08001158 if (ret)
1159 return ret;
1160
1161 for (i = 0; i < 4; i++) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001162 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
1163 intel_ring_emit_reg(engine, GEN7_SO_WRITE_OFFSET(i));
1164 intel_ring_emit(engine, 0);
Eric Anholtae662d32012-01-03 09:23:29 -08001165 }
1166
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001167 intel_ring_advance(engine);
Eric Anholtae662d32012-01-03 09:23:29 -08001168
1169 return 0;
1170}
1171
Brad Volkin71745372014-12-11 12:13:12 -08001172static struct drm_i915_gem_object*
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001173i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
Brad Volkin71745372014-12-11 12:13:12 -08001174 struct drm_i915_gem_exec_object2 *shadow_exec_entry,
1175 struct eb_vmas *eb,
1176 struct drm_i915_gem_object *batch_obj,
1177 u32 batch_start_offset,
1178 u32 batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001179 bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001180{
Brad Volkin71745372014-12-11 12:13:12 -08001181 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001182 struct i915_vma *vma;
Brad Volkin71745372014-12-11 12:13:12 -08001183 int ret;
1184
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001185 shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
Chris Wilson17cabf52015-01-14 11:20:57 +00001186 PAGE_ALIGN(batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001187 if (IS_ERR(shadow_batch_obj))
1188 return shadow_batch_obj;
1189
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001190 ret = i915_parse_cmds(engine,
Brad Volkin71745372014-12-11 12:13:12 -08001191 batch_obj,
1192 shadow_batch_obj,
1193 batch_start_offset,
1194 batch_len,
1195 is_master);
Chris Wilson17cabf52015-01-14 11:20:57 +00001196 if (ret)
1197 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001198
Chris Wilson17cabf52015-01-14 11:20:57 +00001199 ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
1200 if (ret)
1201 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001202
Chris Wilsonde4e7832015-04-07 16:20:35 +01001203 i915_gem_object_unpin_pages(shadow_batch_obj);
1204
Chris Wilson17cabf52015-01-14 11:20:57 +00001205 memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
Brad Volkin71745372014-12-11 12:13:12 -08001206
Chris Wilson17cabf52015-01-14 11:20:57 +00001207 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1208 vma->exec_entry = shadow_exec_entry;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001209 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
Chris Wilson17cabf52015-01-14 11:20:57 +00001210 drm_gem_object_reference(&shadow_batch_obj->base);
1211 list_add_tail(&vma->exec_list, &eb->vmas);
Brad Volkin71745372014-12-11 12:13:12 -08001212
Chris Wilson17cabf52015-01-14 11:20:57 +00001213 shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
Brad Volkin71745372014-12-11 12:13:12 -08001214
Chris Wilson17cabf52015-01-14 11:20:57 +00001215 return shadow_batch_obj;
1216
1217err:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001218 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001219 if (ret == -EACCES) /* unhandled chained batch */
1220 return batch_obj;
1221 else
1222 return ERR_PTR(ret);
Brad Volkin71745372014-12-11 12:13:12 -08001223}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001224
Oscar Mateoa83014d2014-07-24 17:04:21 +01001225int
John Harrison5f19e2b2015-05-29 17:43:27 +01001226i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
Oscar Mateoa83014d2014-07-24 17:04:21 +01001227 struct drm_i915_gem_execbuffer2 *args,
John Harrison5f19e2b2015-05-29 17:43:27 +01001228 struct list_head *vmas)
Oscar Mateo78382592014-07-03 16:28:05 +01001229{
John Harrison5f19e2b2015-05-29 17:43:27 +01001230 struct drm_device *dev = params->dev;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001231 struct intel_engine_cs *engine = params->engine;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001232 struct drm_i915_private *dev_priv = to_i915(dev);
John Harrison5f19e2b2015-05-29 17:43:27 +01001233 u64 exec_start, exec_len;
Oscar Mateo78382592014-07-03 16:28:05 +01001234 int instp_mode;
1235 u32 instp_mask;
Chris Wilson2f5945b2015-10-06 11:39:55 +01001236 int ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001237
John Harrison535fbe82015-05-29 17:43:32 +01001238 ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
Oscar Mateo78382592014-07-03 16:28:05 +01001239 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001240 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001241
John Harrisonba01cc92015-05-29 17:43:41 +01001242 ret = i915_switch_context(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001243 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001244 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001245
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001246 WARN(params->ctx->ppgtt && params->ctx->ppgtt->pd_dirty_rings & (1<<engine->id),
1247 "%s didn't clear reload\n", engine->name);
Ben Widawsky563222a2015-03-19 12:53:28 +00001248
Oscar Mateo78382592014-07-03 16:28:05 +01001249 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1250 instp_mask = I915_EXEC_CONSTANTS_MASK;
1251 switch (instp_mode) {
1252 case I915_EXEC_CONSTANTS_REL_GENERAL:
1253 case I915_EXEC_CONSTANTS_ABSOLUTE:
1254 case I915_EXEC_CONSTANTS_REL_SURFACE:
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001255 if (instp_mode != 0 && engine != &dev_priv->engine[RCS]) {
Oscar Mateo78382592014-07-03 16:28:05 +01001256 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001257 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001258 }
1259
1260 if (instp_mode != dev_priv->relative_constants_mode) {
1261 if (INTEL_INFO(dev)->gen < 4) {
1262 DRM_DEBUG("no rel constants on pre-gen4\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001263 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001264 }
1265
1266 if (INTEL_INFO(dev)->gen > 5 &&
1267 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1268 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001269 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001270 }
1271
1272 /* The HW changed the meaning on this bit on gen6 */
1273 if (INTEL_INFO(dev)->gen >= 6)
1274 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1275 }
1276 break;
1277 default:
1278 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001279 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001280 }
1281
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001282 if (engine == &dev_priv->engine[RCS] &&
Chris Wilson2f5945b2015-10-06 11:39:55 +01001283 instp_mode != dev_priv->relative_constants_mode) {
John Harrison5fb9de12015-05-29 17:44:07 +01001284 ret = intel_ring_begin(params->request, 4);
Oscar Mateo78382592014-07-03 16:28:05 +01001285 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001286 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001287
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001288 intel_ring_emit(engine, MI_NOOP);
1289 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
1290 intel_ring_emit_reg(engine, INSTPM);
1291 intel_ring_emit(engine, instp_mask << 16 | instp_mode);
1292 intel_ring_advance(engine);
Oscar Mateo78382592014-07-03 16:28:05 +01001293
1294 dev_priv->relative_constants_mode = instp_mode;
1295 }
1296
1297 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
John Harrison2f200552015-05-29 17:43:53 +01001298 ret = i915_reset_gen7_sol_offsets(dev, params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001299 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001300 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001301 }
1302
John Harrison5f19e2b2015-05-29 17:43:27 +01001303 exec_len = args->batch_len;
1304 exec_start = params->batch_obj_vm_offset +
1305 params->args_batch_start_offset;
1306
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001307 if (exec_len == 0)
1308 exec_len = params->batch_obj->base.size;
1309
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001310 ret = engine->dispatch_execbuffer(params->request,
Chris Wilson2f5945b2015-10-06 11:39:55 +01001311 exec_start, exec_len,
1312 params->dispatch_flags);
1313 if (ret)
1314 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001315
John Harrison95c24162015-05-29 17:43:31 +01001316 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001317
John Harrison8a8edb52015-05-29 17:43:33 +01001318 i915_gem_execbuffer_move_to_active(vmas, params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001319
Chris Wilson2f5945b2015-10-06 11:39:55 +01001320 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001321}
1322
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001323/**
1324 * Find one BSD ring to dispatch the corresponding BSD command.
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001325 * The ring index is returned.
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001326 */
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001327static unsigned int
1328gen8_dispatch_bsd_ring(struct drm_i915_private *dev_priv, struct drm_file *file)
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001329{
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001330 struct drm_i915_file_private *file_priv = file->driver_priv;
1331
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001332 /* Check whether the file_priv has already selected one ring. */
1333 if ((int)file_priv->bsd_ring < 0) {
1334 /* If not, use the ping-pong mechanism to select one. */
Chris Wilson91c8a322016-07-05 10:40:23 +01001335 mutex_lock(&dev_priv->drm.struct_mutex);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001336 file_priv->bsd_ring = dev_priv->mm.bsd_ring_dispatch_index;
1337 dev_priv->mm.bsd_ring_dispatch_index ^= 1;
Chris Wilson91c8a322016-07-05 10:40:23 +01001338 mutex_unlock(&dev_priv->drm.struct_mutex);
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001339 }
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001340
1341 return file_priv->bsd_ring;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001342}
1343
Chris Wilsond23db882014-05-23 08:48:08 +02001344static struct drm_i915_gem_object *
1345eb_get_batch(struct eb_vmas *eb)
1346{
1347 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
1348
1349 /*
1350 * SNA is doing fancy tricks with compressing batch buffers, which leads
1351 * to negative relocation deltas. Usually that works out ok since the
1352 * relocate address is still positive, except when the batch is placed
1353 * very low in the GTT. Ensure this doesn't happen.
1354 *
1355 * Note that actual hangs have only been observed on gen7, but for
1356 * paranoia do it everywhere.
1357 */
Chris Wilson506a8e82015-12-08 11:55:07 +00001358 if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
1359 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
Chris Wilsond23db882014-05-23 08:48:08 +02001360
1361 return vma->obj;
1362}
1363
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001364#define I915_USER_RINGS (4)
1365
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00001366static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001367 [I915_EXEC_DEFAULT] = RCS,
1368 [I915_EXEC_RENDER] = RCS,
1369 [I915_EXEC_BLT] = BCS,
1370 [I915_EXEC_BSD] = VCS,
1371 [I915_EXEC_VEBOX] = VECS
1372};
1373
1374static int
1375eb_select_ring(struct drm_i915_private *dev_priv,
1376 struct drm_file *file,
1377 struct drm_i915_gem_execbuffer2 *args,
1378 struct intel_engine_cs **ring)
1379{
1380 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
1381
1382 if (user_ring_id > I915_USER_RINGS) {
1383 DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
1384 return -EINVAL;
1385 }
1386
1387 if ((user_ring_id != I915_EXEC_BSD) &&
1388 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1389 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1390 "bsd dispatch flags: %d\n", (int)(args->flags));
1391 return -EINVAL;
1392 }
1393
1394 if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
1395 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
1396
1397 if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
1398 bsd_idx = gen8_dispatch_bsd_ring(dev_priv, file);
1399 } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
1400 bsd_idx <= I915_EXEC_BSD_RING2) {
Tvrtko Ursulind9da6aa2016-01-27 13:41:09 +00001401 bsd_idx >>= I915_EXEC_BSD_SHIFT;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001402 bsd_idx--;
1403 } else {
1404 DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
1405 bsd_idx);
1406 return -EINVAL;
1407 }
1408
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001409 *ring = &dev_priv->engine[_VCS(bsd_idx)];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001410 } else {
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001411 *ring = &dev_priv->engine[user_ring_map[user_ring_id]];
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001412 }
1413
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00001414 if (!intel_engine_initialized(*ring)) {
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001415 DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
1416 return -EINVAL;
1417 }
1418
1419 return 0;
1420}
1421
Eric Anholtae662d32012-01-03 09:23:29 -08001422static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001423i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1424 struct drm_file *file,
1425 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001426 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001427{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001428 struct drm_i915_private *dev_priv = to_i915(dev);
1429 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Dave Gordon26827082016-01-19 19:02:53 +00001430 struct drm_i915_gem_request *req = NULL;
Ben Widawsky27173f12013-08-14 11:38:36 +02001431 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001432 struct drm_i915_gem_object *batch_obj;
Brad Volkin78a42372014-12-11 12:13:09 -08001433 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001434 struct intel_engine_cs *engine;
Chris Wilsone2efd132016-05-24 14:53:34 +01001435 struct i915_gem_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001436 struct i915_address_space *vm;
John Harrison5f19e2b2015-05-29 17:43:27 +01001437 struct i915_execbuffer_params params_master; /* XXX: will be removed later */
1438 struct i915_execbuffer_params *params = &params_master;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001439 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
John Harrison8e004ef2015-02-13 11:48:10 +00001440 u32 dispatch_flags;
Oscar Mateo78382592014-07-03 16:28:05 +01001441 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001442 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001443
Daniel Vettered5982e2013-01-17 22:23:36 +01001444 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001445 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001446
Chris Wilsonad19f102014-08-10 06:29:08 +01001447 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001448 if (ret)
1449 return ret;
1450
John Harrison8e004ef2015-02-13 11:48:10 +00001451 dispatch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001452 if (args->flags & I915_EXEC_SECURE) {
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02001453 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001454 return -EPERM;
1455
John Harrison8e004ef2015-02-13 11:48:10 +00001456 dispatch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001457 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001458 if (args->flags & I915_EXEC_IS_PINNED)
John Harrison8e004ef2015-02-13 11:48:10 +00001459 dispatch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001460
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001461 ret = eb_select_ring(dev_priv, file, args, &engine);
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00001462 if (ret)
1463 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001464
1465 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001466 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001467 return -EINVAL;
1468 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001469
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001470 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
1471 if (!HAS_RESOURCE_STREAMER(dev)) {
1472 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
1473 return -EINVAL;
1474 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001475 if (engine->id != RCS) {
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001476 DRM_DEBUG("RS is not available on %s\n",
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001477 engine->name);
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001478 return -EINVAL;
1479 }
1480
1481 dispatch_flags |= I915_DISPATCH_RS;
1482 }
1483
Chris Wilson67d97da2016-07-04 08:08:31 +01001484 /* Take a local wakeref for preparing to dispatch the execbuf as
1485 * we expect to access the hardware fairly frequently in the
1486 * process. Upon first dispatch, we acquire another prolonged
1487 * wakeref that we hold until the GPU has been idle for at least
1488 * 100ms.
1489 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001490 intel_runtime_pm_get(dev_priv);
1491
Chris Wilson54cf91d2010-11-25 18:00:26 +00001492 ret = i915_mutex_lock_interruptible(dev);
1493 if (ret)
1494 goto pre_mutex_err;
1495
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001496 ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001497 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001498 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001499 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001500 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001501 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001502
1503 i915_gem_context_reference(ctx);
1504
Daniel Vetterae6c4802014-08-06 15:04:53 +02001505 if (ctx->ppgtt)
1506 vm = &ctx->ppgtt->base;
1507 else
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001508 vm = &ggtt->base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001509
John Harrison5f19e2b2015-05-29 17:43:27 +01001510 memset(&params_master, 0x00, sizeof(params_master));
1511
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001512 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001513 if (eb == NULL) {
Ben Widawsky935f38d2014-04-04 22:41:07 -07001514 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001515 mutex_unlock(&dev->struct_mutex);
1516 ret = -ENOMEM;
1517 goto pre_mutex_err;
1518 }
1519
Chris Wilson54cf91d2010-11-25 18:00:26 +00001520 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001521 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001522 if (ret)
1523 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001524
Chris Wilson6fe4f142011-01-10 17:35:37 +00001525 /* take note of the batch buffer before we might reorder the lists */
Chris Wilsond23db882014-05-23 08:48:08 +02001526 batch_obj = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001527
Chris Wilson54cf91d2010-11-25 18:00:26 +00001528 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001529 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001530 ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
1531 &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001532 if (ret)
1533 goto err;
1534
1535 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001536 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001537 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001538 if (ret) {
1539 if (ret == -EFAULT) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001540 ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
1541 engine,
David Weinehallb1b38272015-05-20 17:00:13 +03001542 eb, exec, ctx);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001543 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1544 }
1545 if (ret)
1546 goto err;
1547 }
1548
1549 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001550 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001551 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001552 ret = -EINVAL;
1553 goto err;
1554 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001555
John Harrison5f19e2b2015-05-29 17:43:27 +01001556 params->args_batch_start_offset = args->batch_start_offset;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001557 if (i915_needs_cmd_parser(engine) && args->batch_len) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001558 struct drm_i915_gem_object *parsed_batch_obj;
1559
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001560 parsed_batch_obj = i915_gem_execbuffer_parse(engine,
1561 &shadow_exec_entry,
1562 eb,
1563 batch_obj,
1564 args->batch_start_offset,
1565 args->batch_len,
Daniel Vetterb3ac9f22016-06-21 10:54:20 +02001566 drm_is_current_master(file));
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001567 if (IS_ERR(parsed_batch_obj)) {
1568 ret = PTR_ERR(parsed_batch_obj);
Brad Volkin78a42372014-12-11 12:13:09 -08001569 goto err;
1570 }
Chris Wilson17cabf52015-01-14 11:20:57 +00001571
1572 /*
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001573 * parsed_batch_obj == batch_obj means batch not fully parsed:
1574 * Accept, but don't promote to secure.
Chris Wilson17cabf52015-01-14 11:20:57 +00001575 */
Chris Wilson17cabf52015-01-14 11:20:57 +00001576
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001577 if (parsed_batch_obj != batch_obj) {
1578 /*
1579 * Batch parsed and accepted:
1580 *
1581 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1582 * bit from MI_BATCH_BUFFER_START commands issued in
1583 * the dispatch_execbuffer implementations. We
1584 * specifically don't want that set on batches the
1585 * command parser has accepted.
1586 */
1587 dispatch_flags |= I915_DISPATCH_SECURE;
John Harrison5f19e2b2015-05-29 17:43:27 +01001588 params->args_batch_start_offset = 0;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001589 batch_obj = parsed_batch_obj;
1590 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001591 }
1592
Brad Volkin78a42372014-12-11 12:13:09 -08001593 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1594
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001595 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1596 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001597 * hsw should have this fixed, but bdw mucks it up again. */
John Harrison8e004ef2015-02-13 11:48:10 +00001598 if (dispatch_flags & I915_DISPATCH_SECURE) {
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001599 /*
1600 * So on first glance it looks freaky that we pin the batch here
1601 * outside of the reservation loop. But:
1602 * - The batch is already pinned into the relevant ppgtt, so we
1603 * already have the backing storage fully allocated.
1604 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01001605 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001606 * fitting due to fragmentation.
1607 * So this is actually safe.
1608 */
1609 ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
1610 if (ret)
1611 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001612
John Harrison5f19e2b2015-05-29 17:43:27 +01001613 params->batch_obj_vm_offset = i915_gem_obj_ggtt_offset(batch_obj);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001614 } else
John Harrison5f19e2b2015-05-29 17:43:27 +01001615 params->batch_obj_vm_offset = i915_gem_obj_offset(batch_obj, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001616
John Harrison0c8dac82015-05-29 17:43:25 +01001617 /* Allocate a request for this batch buffer nice and early. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001618 req = i915_gem_request_alloc(engine, ctx);
Dave Gordon26827082016-01-19 19:02:53 +00001619 if (IS_ERR(req)) {
1620 ret = PTR_ERR(req);
John Harrison0c8dac82015-05-29 17:43:25 +01001621 goto err_batch_unpin;
Dave Gordon26827082016-01-19 19:02:53 +00001622 }
John Harrison0c8dac82015-05-29 17:43:25 +01001623
Dave Gordon26827082016-01-19 19:02:53 +00001624 ret = i915_gem_request_add_to_client(req, file);
John Harrisonfcfa423c2015-05-29 17:44:12 +01001625 if (ret)
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001626 goto err_request;
John Harrisonfcfa423c2015-05-29 17:44:12 +01001627
John Harrison5f19e2b2015-05-29 17:43:27 +01001628 /*
1629 * Save assorted stuff away to pass through to *_submission().
1630 * NB: This data should be 'persistent' and not local as it will
1631 * kept around beyond the duration of the IOCTL once the GPU
1632 * scheduler arrives.
1633 */
1634 params->dev = dev;
1635 params->file = file;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001636 params->engine = engine;
John Harrison5f19e2b2015-05-29 17:43:27 +01001637 params->dispatch_flags = dispatch_flags;
1638 params->batch_obj = batch_obj;
1639 params->ctx = ctx;
Dave Gordon26827082016-01-19 19:02:53 +00001640 params->request = req;
John Harrison5f19e2b2015-05-29 17:43:27 +01001641
1642 ret = dev_priv->gt.execbuf_submit(params, args, &eb->vmas);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01001643err_request:
1644 i915_gem_execbuffer_retire_commands(params);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001645
John Harrison0c8dac82015-05-29 17:43:25 +01001646err_batch_unpin:
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001647 /*
1648 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1649 * batch vma for correctness. For less ugly and less fragility this
1650 * needs to be adjusted to also track the ggtt batch vma properly as
1651 * active.
1652 */
John Harrison8e004ef2015-02-13 11:48:10 +00001653 if (dispatch_flags & I915_DISPATCH_SECURE)
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001654 i915_gem_object_ggtt_unpin(batch_obj);
John Harrison0c8dac82015-05-29 17:43:25 +01001655
Chris Wilson54cf91d2010-11-25 18:00:26 +00001656err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001657 /* the request owns the ref now */
1658 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001659 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001660
1661 mutex_unlock(&dev->struct_mutex);
1662
1663pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001664 /* intel_gpu_busy should also get a ref, so it will free when the device
1665 * is really idle. */
1666 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001667 return ret;
1668}
1669
1670/*
1671 * Legacy execbuffer just creates an exec2 list from the original exec object
1672 * list array and passes it to the real function.
1673 */
1674int
1675i915_gem_execbuffer(struct drm_device *dev, void *data,
1676 struct drm_file *file)
1677{
1678 struct drm_i915_gem_execbuffer *args = data;
1679 struct drm_i915_gem_execbuffer2 exec2;
1680 struct drm_i915_gem_exec_object *exec_list = NULL;
1681 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1682 int ret, i;
1683
Chris Wilson54cf91d2010-11-25 18:00:26 +00001684 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001685 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001686 return -EINVAL;
1687 }
1688
1689 /* Copy in the exec list from userland */
1690 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1691 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1692 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001693 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001694 args->buffer_count);
1695 drm_free_large(exec_list);
1696 drm_free_large(exec2_list);
1697 return -ENOMEM;
1698 }
1699 ret = copy_from_user(exec_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001700 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001701 sizeof(*exec_list) * args->buffer_count);
1702 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001703 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001704 args->buffer_count, ret);
1705 drm_free_large(exec_list);
1706 drm_free_large(exec2_list);
1707 return -EFAULT;
1708 }
1709
1710 for (i = 0; i < args->buffer_count; i++) {
1711 exec2_list[i].handle = exec_list[i].handle;
1712 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1713 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1714 exec2_list[i].alignment = exec_list[i].alignment;
1715 exec2_list[i].offset = exec_list[i].offset;
1716 if (INTEL_INFO(dev)->gen < 4)
1717 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1718 else
1719 exec2_list[i].flags = 0;
1720 }
1721
1722 exec2.buffers_ptr = args->buffers_ptr;
1723 exec2.buffer_count = args->buffer_count;
1724 exec2.batch_start_offset = args->batch_start_offset;
1725 exec2.batch_len = args->batch_len;
1726 exec2.DR1 = args->DR1;
1727 exec2.DR4 = args->DR4;
1728 exec2.num_cliprects = args->num_cliprects;
1729 exec2.cliprects_ptr = args->cliprects_ptr;
1730 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001731 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001732
Ben Widawsky41bde552013-12-06 14:11:21 -08001733 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001734 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001735 struct drm_i915_gem_exec_object __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001736 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001737
Chris Wilson54cf91d2010-11-25 18:00:26 +00001738 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001739 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001740 exec2_list[i].offset =
1741 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001742 ret = __copy_to_user(&user_exec_list[i].offset,
1743 &exec2_list[i].offset,
1744 sizeof(user_exec_list[i].offset));
1745 if (ret) {
1746 ret = -EFAULT;
1747 DRM_DEBUG("failed to copy %d exec entries "
1748 "back to user (%d)\n",
1749 args->buffer_count, ret);
1750 break;
1751 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001752 }
1753 }
1754
1755 drm_free_large(exec_list);
1756 drm_free_large(exec2_list);
1757 return ret;
1758}
1759
1760int
1761i915_gem_execbuffer2(struct drm_device *dev, void *data,
1762 struct drm_file *file)
1763{
1764 struct drm_i915_gem_execbuffer2 *args = data;
1765 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1766 int ret;
1767
Xi Wanged8cd3b2012-04-23 04:06:41 -04001768 if (args->buffer_count < 1 ||
1769 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001770 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001771 return -EINVAL;
1772 }
1773
Daniel Vetter9cb34662014-04-24 08:09:11 +02001774 if (args->rsvd2 != 0) {
1775 DRM_DEBUG("dirty rvsd2 field\n");
1776 return -EINVAL;
1777 }
1778
Chris Wilsonf2a85e12016-04-08 12:11:13 +01001779 exec2_list = drm_malloc_gfp(args->buffer_count,
1780 sizeof(*exec2_list),
1781 GFP_TEMPORARY);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001782 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001783 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001784 args->buffer_count);
1785 return -ENOMEM;
1786 }
1787 ret = copy_from_user(exec2_list,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001788 u64_to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001789 sizeof(*exec2_list) * args->buffer_count);
1790 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001791 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001792 args->buffer_count, ret);
1793 drm_free_large(exec2_list);
1794 return -EFAULT;
1795 }
1796
Ben Widawsky41bde552013-12-06 14:11:21 -08001797 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001798 if (!ret) {
1799 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001800 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001801 u64_to_user_ptr(args->buffers_ptr);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001802 int i;
1803
1804 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarski934acce2015-12-29 18:24:52 +01001805 exec2_list[i].offset =
1806 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001807 ret = __copy_to_user(&user_exec_list[i].offset,
1808 &exec2_list[i].offset,
1809 sizeof(user_exec_list[i].offset));
1810 if (ret) {
1811 ret = -EFAULT;
1812 DRM_DEBUG("failed to copy %d exec entries "
1813 "back to user\n",
1814 args->buffer_count);
1815 break;
1816 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001817 }
1818 }
1819
1820 drm_free_large(exec2_list);
1821 return ret;
1822}