blob: 3a30133f93e858a449366727c266a4093485e085 [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>
Chris Wilson54cf91d2010-11-25 18:00:26 +000035
Chris Wilsona415d352013-11-26 11:23:15 +000036#define __EXEC_OBJECT_HAS_PIN (1<<31)
37#define __EXEC_OBJECT_HAS_FENCE (1<<30)
Chris Wilsond23db882014-05-23 08:48:08 +020038#define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
39
40#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000041
Ben Widawsky27173f12013-08-14 11:38:36 +020042struct eb_vmas {
43 struct list_head vmas;
Chris Wilson67731b82010-12-08 10:38:14 +000044 int and;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000045 union {
Ben Widawsky27173f12013-08-14 11:38:36 +020046 struct i915_vma *lut[0];
Chris Wilsoneef90cc2013-01-08 10:53:17 +000047 struct hlist_head buckets[0];
48 };
Chris Wilson67731b82010-12-08 10:38:14 +000049};
50
Ben Widawsky27173f12013-08-14 11:38:36 +020051static struct eb_vmas *
Ben Widawsky17601cbc2013-11-25 09:54:38 -080052eb_create(struct drm_i915_gem_execbuffer2 *args)
Chris Wilson67731b82010-12-08 10:38:14 +000053{
Ben Widawsky27173f12013-08-14 11:38:36 +020054 struct eb_vmas *eb = NULL;
Chris Wilson67731b82010-12-08 10:38:14 +000055
Chris Wilsoneef90cc2013-01-08 10:53:17 +000056 if (args->flags & I915_EXEC_HANDLE_LUT) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020057 unsigned size = args->buffer_count;
Ben Widawsky27173f12013-08-14 11:38:36 +020058 size *= sizeof(struct i915_vma *);
59 size += sizeof(struct eb_vmas);
Chris Wilsoneef90cc2013-01-08 10:53:17 +000060 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
61 }
62
63 if (eb == NULL) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020064 unsigned size = args->buffer_count;
65 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Lauri Kasanen27b7c632013-03-27 15:04:55 +020066 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
Chris Wilsoneef90cc2013-01-08 10:53:17 +000067 while (count > 2*size)
68 count >>= 1;
69 eb = kzalloc(count*sizeof(struct hlist_head) +
Ben Widawsky27173f12013-08-14 11:38:36 +020070 sizeof(struct eb_vmas),
Chris Wilsoneef90cc2013-01-08 10:53:17 +000071 GFP_TEMPORARY);
72 if (eb == NULL)
73 return eb;
74
75 eb->and = count - 1;
76 } else
77 eb->and = -args->buffer_count;
78
Ben Widawsky27173f12013-08-14 11:38:36 +020079 INIT_LIST_HEAD(&eb->vmas);
Chris Wilson67731b82010-12-08 10:38:14 +000080 return eb;
81}
82
83static void
Ben Widawsky27173f12013-08-14 11:38:36 +020084eb_reset(struct eb_vmas *eb)
Chris Wilson67731b82010-12-08 10:38:14 +000085{
Chris Wilsoneef90cc2013-01-08 10:53:17 +000086 if (eb->and >= 0)
87 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
Chris Wilson67731b82010-12-08 10:38:14 +000088}
89
Chris Wilson3b96eff2013-01-08 10:53:14 +000090static int
Ben Widawsky27173f12013-08-14 11:38:36 +020091eb_lookup_vmas(struct eb_vmas *eb,
92 struct drm_i915_gem_exec_object2 *exec,
93 const struct drm_i915_gem_execbuffer2 *args,
94 struct i915_address_space *vm,
95 struct drm_file *file)
Chris Wilson3b96eff2013-01-08 10:53:14 +000096{
Ben Widawsky6f65e292013-12-06 14:10:56 -080097 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +020098 struct drm_i915_gem_object *obj;
99 struct list_head objects;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000100 int i, ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000101
Ben Widawsky27173f12013-08-14 11:38:36 +0200102 INIT_LIST_HEAD(&objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000103 spin_lock(&file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +0200104 /* Grab a reference to the object and release the lock so we can lookup
105 * or create the VMA without using GFP_ATOMIC */
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000106 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000107 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
108 if (obj == NULL) {
109 spin_unlock(&file->table_lock);
110 DRM_DEBUG("Invalid object handle %d at index %d\n",
111 exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200112 ret = -ENOENT;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000113 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000114 }
115
Ben Widawsky27173f12013-08-14 11:38:36 +0200116 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000117 spin_unlock(&file->table_lock);
118 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
119 obj, exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200120 ret = -EINVAL;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000121 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000122 }
123
124 drm_gem_object_reference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200125 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000126 }
127 spin_unlock(&file->table_lock);
128
Ben Widawsky27173f12013-08-14 11:38:36 +0200129 i = 0;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000130 while (!list_empty(&objects)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200131 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800132 struct i915_address_space *bind_vm = vm;
133
Daniel Vetter2c9f8d52013-12-18 17:38:53 +0100134 if (exec[i].flags & EXEC_OBJECT_NEEDS_GTT &&
135 USES_FULL_PPGTT(vm->dev)) {
136 ret = -EINVAL;
Rodrigo Vivia25eebb2014-01-14 16:21:49 -0200137 goto err;
Daniel Vetter2c9f8d52013-12-18 17:38:53 +0100138 }
139
Ben Widawsky6f65e292013-12-06 14:10:56 -0800140 /* If we have secure dispatch, or the userspace assures us that
141 * they know what they're doing, use the GGTT VM.
142 */
Daniel Vettera7c1d4262013-12-18 17:46:18 +0100143 if (((args->flags & I915_EXEC_SECURE) &&
Ben Widawsky6f65e292013-12-06 14:10:56 -0800144 (i == (args->buffer_count - 1))))
145 bind_vm = &dev_priv->gtt.base;
Ben Widawsky27173f12013-08-14 11:38:36 +0200146
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000147 obj = list_first_entry(&objects,
148 struct drm_i915_gem_object,
149 obj_exec_link);
150
Daniel Vettere656a6c2013-08-14 14:14:04 +0200151 /*
152 * NOTE: We can leak any vmas created here when something fails
153 * later on. But that's no issue since vma_unbind can deal with
154 * vmas which are not actually bound. And since only
155 * lookup_or_create exists as an interface to get at the vma
156 * from the (obj, vm) we don't run the risk of creating
157 * duplicated vmas for the same vm.
158 */
Ben Widawsky6f65e292013-12-06 14:10:56 -0800159 vma = i915_gem_obj_lookup_or_create_vma(obj, bind_vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200160 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200161 DRM_DEBUG("Failed to lookup VMA\n");
162 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000163 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200164 }
165
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000166 /* Transfer ownership from the objects list to the vmas list. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200167 list_add_tail(&vma->exec_list, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000168 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200169
170 vma->exec_entry = &exec[i];
171 if (eb->and < 0) {
172 eb->lut[i] = vma;
173 } else {
174 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
175 vma->exec_handle = handle;
176 hlist_add_head(&vma->exec_node,
177 &eb->buckets[handle & eb->and]);
178 }
179 ++i;
180 }
181
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000182 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200183
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000184
185err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200186 while (!list_empty(&objects)) {
187 obj = list_first_entry(&objects,
188 struct drm_i915_gem_object,
189 obj_exec_link);
190 list_del_init(&obj->obj_exec_link);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000191 drm_gem_object_unreference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200192 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000193 /*
194 * Objects already transfered to the vmas list will be unreferenced by
195 * eb_destroy.
196 */
197
Ben Widawsky27173f12013-08-14 11:38:36 +0200198 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000199}
200
Ben Widawsky27173f12013-08-14 11:38:36 +0200201static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000202{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000203 if (eb->and < 0) {
204 if (handle >= -eb->and)
205 return NULL;
206 return eb->lut[handle];
207 } else {
208 struct hlist_head *head;
209 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000210
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000211 head = &eb->buckets[handle & eb->and];
212 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200213 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000214
Ben Widawsky27173f12013-08-14 11:38:36 +0200215 vma = hlist_entry(node, struct i915_vma, exec_node);
216 if (vma->exec_handle == handle)
217 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000218 }
219 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000220 }
Chris Wilson67731b82010-12-08 10:38:14 +0000221}
222
Chris Wilsona415d352013-11-26 11:23:15 +0000223static void
224i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
225{
226 struct drm_i915_gem_exec_object2 *entry;
227 struct drm_i915_gem_object *obj = vma->obj;
228
229 if (!drm_mm_node_allocated(&vma->node))
230 return;
231
232 entry = vma->exec_entry;
233
234 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
235 i915_gem_object_unpin_fence(obj);
236
237 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Daniel Vetter3d7f0f92013-12-18 16:23:37 +0100238 vma->pin_count--;
Chris Wilsona415d352013-11-26 11:23:15 +0000239
240 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
241}
242
243static void eb_destroy(struct eb_vmas *eb)
244{
Ben Widawsky27173f12013-08-14 11:38:36 +0200245 while (!list_empty(&eb->vmas)) {
246 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000247
Ben Widawsky27173f12013-08-14 11:38:36 +0200248 vma = list_first_entry(&eb->vmas,
249 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000250 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200251 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000252 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200253 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000254 }
Chris Wilson67731b82010-12-08 10:38:14 +0000255 kfree(eb);
256}
257
Chris Wilsondabdfe02012-03-26 10:10:27 +0200258static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
259{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300260 return (HAS_LLC(obj->base.dev) ||
261 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100262 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200263 obj->cache_level != I915_CACHE_NONE);
264}
265
Chris Wilson54cf91d2010-11-25 18:00:26 +0000266static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100267relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700268 struct drm_i915_gem_relocation_entry *reloc,
269 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100270{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700271 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100272 uint32_t page_offset = offset_in_page(reloc->offset);
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700273 uint64_t delta = reloc->delta + target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100274 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800275 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100276
Chris Wilson2cc86b82013-08-26 19:51:00 -0300277 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100278 if (ret)
279 return ret;
280
281 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
282 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700283 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700284
285 if (INTEL_INFO(dev)->gen >= 8) {
286 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
287
288 if (page_offset == 0) {
289 kunmap_atomic(vaddr);
290 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
291 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
292 }
293
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700294 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700295 }
296
Rafael Barbalho5032d872013-08-21 17:10:51 +0100297 kunmap_atomic(vaddr);
298
299 return 0;
300}
301
302static int
303relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700304 struct drm_i915_gem_relocation_entry *reloc,
305 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100306{
307 struct drm_device *dev = obj->base.dev;
308 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700309 uint64_t delta = reloc->delta + target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100310 uint32_t __iomem *reloc_entry;
311 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800312 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100313
314 ret = i915_gem_object_set_to_gtt_domain(obj, true);
315 if (ret)
316 return ret;
317
318 ret = i915_gem_object_put_fence(obj);
319 if (ret)
320 return ret;
321
322 /* Map the page containing the relocation we're going to perform. */
323 reloc->offset += i915_gem_obj_ggtt_offset(obj);
324 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
325 reloc->offset & PAGE_MASK);
326 reloc_entry = (uint32_t __iomem *)
327 (reloc_page + offset_in_page(reloc->offset));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700328 iowrite32(lower_32_bits(delta), reloc_entry);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700329
330 if (INTEL_INFO(dev)->gen >= 8) {
331 reloc_entry += 1;
332
333 if (offset_in_page(reloc->offset + sizeof(uint32_t)) == 0) {
334 io_mapping_unmap_atomic(reloc_page);
335 reloc_page = io_mapping_map_atomic_wc(
336 dev_priv->gtt.mappable,
337 reloc->offset + sizeof(uint32_t));
338 reloc_entry = reloc_page;
339 }
340
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700341 iowrite32(upper_32_bits(delta), reloc_entry);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700342 }
343
Rafael Barbalho5032d872013-08-21 17:10:51 +0100344 io_mapping_unmap_atomic(reloc_page);
345
346 return 0;
347}
348
349static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000350i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200351 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800352 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000353{
354 struct drm_device *dev = obj->base.dev;
355 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100356 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200357 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700358 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800359 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000360
Chris Wilson67731b82010-12-08 10:38:14 +0000361 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200362 target_vma = eb_get_vma(eb, reloc->target_handle);
363 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000364 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200365 target_i915_obj = target_vma->obj;
366 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000367
Ben Widawsky5ce09722013-11-25 09:54:40 -0800368 target_offset = target_vma->node.start;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000369
Eric Anholte844b992012-07-31 15:35:01 -0700370 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
371 * pipe_control writes because the gpu doesn't properly redirect them
372 * through the ppgtt for non_secure batchbuffers. */
373 if (unlikely(IS_GEN6(dev) &&
374 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
375 !target_i915_obj->has_global_gtt_mapping)) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800376 struct i915_vma *vma =
377 list_first_entry(&target_i915_obj->vma_list,
378 typeof(*vma), vma_link);
Ben Widawsky6f65e292013-12-06 14:10:56 -0800379 vma->bind_vma(vma, target_i915_obj->cache_level, GLOBAL_BIND);
Eric Anholte844b992012-07-31 15:35:01 -0700380 }
381
Chris Wilson54cf91d2010-11-25 18:00:26 +0000382 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000383 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100384 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000385 "obj %p target %d offset %d "
386 "read %08x write %08x",
387 obj, reloc->target_handle,
388 (int) reloc->offset,
389 reloc->read_domains,
390 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800391 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000392 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100393 if (unlikely((reloc->write_domain | reloc->read_domains)
394 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100395 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000396 "obj %p target %d offset %d "
397 "read %08x write %08x",
398 obj, reloc->target_handle,
399 (int) reloc->offset,
400 reloc->read_domains,
401 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800402 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000403 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000404
405 target_obj->pending_read_domains |= reloc->read_domains;
406 target_obj->pending_write_domain |= reloc->write_domain;
407
408 /* If the relocation already has the right value in it, no
409 * more work needs to be done.
410 */
411 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000412 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000413
414 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700415 if (unlikely(reloc->offset >
416 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100417 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000418 "obj %p target %d offset %d size %d.\n",
419 obj, reloc->target_handle,
420 (int) reloc->offset,
421 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800422 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000423 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000424 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100425 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000426 "obj %p target %d offset %d.\n",
427 obj, reloc->target_handle,
428 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800429 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000430 }
431
Chris Wilsondabdfe02012-03-26 10:10:27 +0200432 /* We can't wait for rendering with pagefaults disabled */
433 if (obj->active && in_atomic())
434 return -EFAULT;
435
Rafael Barbalho5032d872013-08-21 17:10:51 +0100436 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700437 ret = relocate_entry_cpu(obj, reloc, target_offset);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100438 else
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700439 ret = relocate_entry_gtt(obj, reloc, target_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000440
Daniel Vetterd4d36012013-09-02 20:56:23 +0200441 if (ret)
442 return ret;
443
Chris Wilson54cf91d2010-11-25 18:00:26 +0000444 /* and update the user's relocation entry */
445 reloc->presumed_offset = target_offset;
446
Chris Wilson67731b82010-12-08 10:38:14 +0000447 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000448}
449
450static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200451i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
452 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000453{
Chris Wilson1d83f442012-03-24 20:12:53 +0000454#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
455 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000456 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200457 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000458 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000459
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200460 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000461
Chris Wilson1d83f442012-03-24 20:12:53 +0000462 remain = entry->relocation_count;
463 while (remain) {
464 struct drm_i915_gem_relocation_entry *r = stack_reloc;
465 int count = remain;
466 if (count > ARRAY_SIZE(stack_reloc))
467 count = ARRAY_SIZE(stack_reloc);
468 remain -= count;
469
470 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000471 return -EFAULT;
472
Chris Wilson1d83f442012-03-24 20:12:53 +0000473 do {
474 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000475
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800476 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000477 if (ret)
478 return ret;
479
480 if (r->presumed_offset != offset &&
481 __copy_to_user_inatomic(&user_relocs->presumed_offset,
482 &r->presumed_offset,
483 sizeof(r->presumed_offset))) {
484 return -EFAULT;
485 }
486
487 user_relocs++;
488 r++;
489 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000490 }
491
492 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000493#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000494}
495
496static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200497i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
498 struct eb_vmas *eb,
499 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000500{
Ben Widawsky27173f12013-08-14 11:38:36 +0200501 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000502 int i, ret;
503
504 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800505 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000506 if (ret)
507 return ret;
508 }
509
510 return 0;
511}
512
513static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800514i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000515{
Ben Widawsky27173f12013-08-14 11:38:36 +0200516 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000517 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000518
Chris Wilsond4aeee72011-03-14 15:11:24 +0000519 /* This is the fast path and we cannot handle a pagefault whilst
520 * holding the struct mutex lest the user pass in the relocations
521 * contained within a mmaped bo. For in such a case we, the page
522 * fault handler would call i915_gem_fault() and we would try to
523 * acquire the struct mutex again. Obviously this is bad and so
524 * lockdep complains vehemently.
525 */
526 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200527 list_for_each_entry(vma, &eb->vmas, exec_list) {
528 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000529 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000530 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000531 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000532 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000533
Chris Wilsond4aeee72011-03-14 15:11:24 +0000534 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000535}
536
Chris Wilson1690e1e2011-12-14 13:57:08 +0100537static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200538need_reloc_mappable(struct i915_vma *vma)
Chris Wilsondabdfe02012-03-26 10:10:27 +0200539{
Ben Widawsky27173f12013-08-14 11:38:36 +0200540 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
541 return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
542 i915_is_ggtt(vma->vm);
Chris Wilsondabdfe02012-03-26 10:10:27 +0200543}
544
545static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200546i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100547 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200548 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100549{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800550 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200551 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100552 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100553 bool need_fence;
Chris Wilsond23db882014-05-23 08:48:08 +0200554 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100555 int ret;
556
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100557 flags = 0;
558
Chris Wilson1690e1e2011-12-14 13:57:08 +0100559 need_fence =
560 has_fenced_gpu_access &&
561 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
562 obj->tiling_mode != I915_TILING_NONE;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100563 if (need_fence || need_reloc_mappable(vma))
564 flags |= PIN_MAPPABLE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100565
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100566 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
Daniel Vetterbf3d1492014-02-14 14:01:12 +0100567 flags |= PIN_GLOBAL;
Chris Wilsond23db882014-05-23 08:48:08 +0200568 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
569 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100570
571 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100572 if (ret)
573 return ret;
574
Chris Wilson7788a762012-08-24 19:18:18 +0100575 entry->flags |= __EXEC_OBJECT_HAS_PIN;
576
Chris Wilson1690e1e2011-12-14 13:57:08 +0100577 if (has_fenced_gpu_access) {
578 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100579 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000580 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100581 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100582
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000583 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100584 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000585
Chris Wilson7dd49062012-03-21 10:48:18 +0000586 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100587 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100588 }
589
Ben Widawsky27173f12013-08-14 11:38:36 +0200590 if (entry->offset != vma->node.start) {
591 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100592 *need_reloc = true;
593 }
594
595 if (entry->flags & EXEC_OBJECT_WRITE) {
596 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
597 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
598 }
599
Chris Wilson1690e1e2011-12-14 13:57:08 +0100600 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100601}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100602
Chris Wilsond23db882014-05-23 08:48:08 +0200603static bool
604eb_vma_misplaced(struct i915_vma *vma, bool has_fenced_gpu_access)
605{
606 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
607 struct drm_i915_gem_object *obj = vma->obj;
608 bool need_fence, need_mappable;
609
610 need_fence =
611 has_fenced_gpu_access &&
612 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
613 obj->tiling_mode != I915_TILING_NONE;
614 need_mappable = need_fence || need_reloc_mappable(vma);
615
616 WARN_ON((need_mappable || need_fence) &&
617 !i915_is_ggtt(vma->vm));
618
619 if (entry->alignment &&
620 vma->node.start & (entry->alignment - 1))
621 return true;
622
623 if (need_mappable && !obj->map_and_fenceable)
624 return true;
625
626 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
627 vma->node.start < BATCH_OFFSET_BIAS)
628 return true;
629
630 return false;
631}
632
Chris Wilson54cf91d2010-11-25 18:00:26 +0000633static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100634i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200635 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100636 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000637{
Chris Wilson432e58e2010-11-25 19:32:06 +0000638 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200639 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700640 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200641 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100642 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
643 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000644
Ben Widawsky68c8c172013-09-11 14:57:50 -0700645 if (list_empty(vmas))
646 return 0;
647
Chris Wilson227f7822014-05-15 10:41:42 +0100648 i915_gem_retire_requests_ring(ring);
649
Ben Widawsky68c8c172013-09-11 14:57:50 -0700650 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
651
Ben Widawsky27173f12013-08-14 11:38:36 +0200652 INIT_LIST_HEAD(&ordered_vmas);
653 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000654 struct drm_i915_gem_exec_object2 *entry;
655 bool need_fence, need_mappable;
656
Ben Widawsky27173f12013-08-14 11:38:36 +0200657 vma = list_first_entry(vmas, struct i915_vma, exec_list);
658 obj = vma->obj;
659 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000660
661 need_fence =
662 has_fenced_gpu_access &&
663 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
664 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200665 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000666
667 if (need_mappable)
Ben Widawsky27173f12013-08-14 11:38:36 +0200668 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000669 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200670 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000671
Daniel Vettered5982e2013-01-17 22:23:36 +0100672 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000673 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100674 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000675 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200676 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000677
678 /* Attempt to pin all of the buffers into the GTT.
679 * This is done in 3 phases:
680 *
681 * 1a. Unbind all objects that do not match the GTT constraints for
682 * the execbuffer (fenceable, mappable, alignment etc).
683 * 1b. Increment pin count for already bound objects.
684 * 2. Bind new objects.
685 * 3. Decrement pin count.
686 *
Chris Wilson7788a762012-08-24 19:18:18 +0100687 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000688 * room for the earlier objects *unless* we need to defragment.
689 */
690 retry = 0;
691 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100692 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000693
694 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200695 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200696 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000697 continue;
698
Chris Wilsond23db882014-05-23 08:48:08 +0200699 if (eb_vma_misplaced(vma, has_fenced_gpu_access))
Ben Widawsky27173f12013-08-14 11:38:36 +0200700 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000701 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200702 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000703 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000704 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000705 }
706
707 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200708 list_for_each_entry(vma, vmas, exec_list) {
709 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100710 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000711
Ben Widawsky27173f12013-08-14 11:38:36 +0200712 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100713 if (ret)
714 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000715 }
716
Chris Wilsona415d352013-11-26 11:23:15 +0000717err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200718 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000719 return ret;
720
Chris Wilsona415d352013-11-26 11:23:15 +0000721 /* Decrement pin count for bound objects */
722 list_for_each_entry(vma, vmas, exec_list)
723 i915_gem_execbuffer_unreserve_vma(vma);
724
Ben Widawsky68c8c172013-09-11 14:57:50 -0700725 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000726 if (ret)
727 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000728 } while (1);
729}
730
731static int
732i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100733 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000734 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100735 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200736 struct eb_vmas *eb,
737 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000738{
739 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200740 struct i915_address_space *vm;
741 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100742 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000743 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000744 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200745 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000746
Ben Widawsky27173f12013-08-14 11:38:36 +0200747 if (WARN_ON(list_empty(&eb->vmas)))
748 return 0;
749
750 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
751
Chris Wilson67731b82010-12-08 10:38:14 +0000752 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200753 while (!list_empty(&eb->vmas)) {
754 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
755 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000756 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200757 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000758 }
759
Chris Wilson54cf91d2010-11-25 18:00:26 +0000760 mutex_unlock(&dev->struct_mutex);
761
762 total = 0;
763 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000764 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000765
Chris Wilsondd6864a2011-01-12 23:49:13 +0000766 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000767 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000768 if (reloc == NULL || reloc_offset == NULL) {
769 drm_free_large(reloc);
770 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000771 mutex_lock(&dev->struct_mutex);
772 return -ENOMEM;
773 }
774
775 total = 0;
776 for (i = 0; i < count; i++) {
777 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000778 u64 invalid_offset = (u64)-1;
779 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000780
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200781 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000782
783 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000784 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000785 ret = -EFAULT;
786 mutex_lock(&dev->struct_mutex);
787 goto err;
788 }
789
Chris Wilson262b6d32013-01-15 16:17:54 +0000790 /* As we do not update the known relocation offsets after
791 * relocating (due to the complexities in lock handling),
792 * we need to mark them as invalid now so that we force the
793 * relocation processing next time. Just in case the target
794 * object is evicted and then rebound into its old
795 * presumed_offset before the next execbuffer - if that
796 * happened we would make the mistake of assuming that the
797 * relocations were valid.
798 */
799 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100800 if (__copy_to_user(&user_relocs[j].presumed_offset,
801 &invalid_offset,
802 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000803 ret = -EFAULT;
804 mutex_lock(&dev->struct_mutex);
805 goto err;
806 }
807 }
808
Chris Wilsondd6864a2011-01-12 23:49:13 +0000809 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000810 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000811 }
812
813 ret = i915_mutex_lock_interruptible(dev);
814 if (ret) {
815 mutex_lock(&dev->struct_mutex);
816 goto err;
817 }
818
Chris Wilson67731b82010-12-08 10:38:14 +0000819 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000820 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200821 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000822 if (ret)
823 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000824
Daniel Vettered5982e2013-01-17 22:23:36 +0100825 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200826 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000827 if (ret)
828 goto err;
829
Ben Widawsky27173f12013-08-14 11:38:36 +0200830 list_for_each_entry(vma, &eb->vmas, exec_list) {
831 int offset = vma->exec_entry - exec;
832 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
833 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000834 if (ret)
835 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000836 }
837
838 /* Leave the user relocations as are, this is the painfully slow path,
839 * and we want to avoid the complication of dropping the lock whilst
840 * having buffers reserved in the aperture and so causing spurious
841 * ENOSPC for random operations.
842 */
843
844err:
845 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000846 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000847 return ret;
848}
849
Chris Wilson54cf91d2010-11-25 18:00:26 +0000850static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100851i915_gem_execbuffer_move_to_gpu(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200852 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000853{
Ben Widawsky27173f12013-08-14 11:38:36 +0200854 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200855 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100856 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000857 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000858
Ben Widawsky27173f12013-08-14 11:38:36 +0200859 list_for_each_entry(vma, vmas, exec_list) {
860 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700861 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000862 if (ret)
863 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200864
865 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100866 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200867
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200868 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000869 }
870
Chris Wilson000433b2013-08-08 14:41:09 +0100871 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800872 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200873
874 if (flush_domains & I915_GEM_DOMAIN_GTT)
875 wmb();
876
Chris Wilson09cf7c92012-07-13 14:14:08 +0100877 /* Unconditionally invalidate gpu caches and ensure that we do flush
878 * any residual writes from the previous batch.
879 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100880 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000881}
882
Chris Wilson432e58e2010-11-25 19:32:06 +0000883static bool
884i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000885{
Daniel Vettered5982e2013-01-17 22:23:36 +0100886 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
887 return false;
888
Chris Wilson432e58e2010-11-25 19:32:06 +0000889 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000890}
891
892static int
893validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
894 int count)
895{
896 int i;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200897 unsigned relocs_total = 0;
898 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000899
900 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200901 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000902 int length; /* limited by fault_in_pages_readable() */
903
Daniel Vettered5982e2013-01-17 22:23:36 +0100904 if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
905 return -EINVAL;
906
Kees Cook3118a4f2013-03-11 17:31:45 -0700907 /* First check for malicious input causing overflow in
908 * the worst case where we need to allocate the entire
909 * relocation tree as a single array.
910 */
911 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000912 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700913 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000914
915 length = exec[i].relocation_count *
916 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700917 /*
918 * We must check that the entire relocation array is safe
919 * to read, but since we may need to update the presumed
920 * offsets during execution, check for full write access.
921 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000922 if (!access_ok(VERIFY_WRITE, ptr, length))
923 return -EFAULT;
924
Jani Nikulad330a952014-01-21 11:24:25 +0200925 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +0800926 if (fault_in_multipages_readable(ptr, length))
927 return -EFAULT;
928 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000929 }
930
931 return 0;
932}
933
Oscar Mateo273497e2014-05-22 14:13:37 +0100934static struct intel_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200935i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100936 struct intel_engine_cs *ring, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200937{
Oscar Mateo273497e2014-05-22 14:13:37 +0100938 struct intel_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200939 struct i915_ctx_hang_stats *hs;
940
Daniel Vetter7c9c4b82013-12-18 16:37:49 +0100941 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_ID)
942 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200943
Ben Widawsky41bde552013-12-06 14:11:21 -0800944 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -1000945 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -0800946 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200947
Ben Widawsky41bde552013-12-06 14:11:21 -0800948 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200949 if (hs->banned) {
950 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -0800951 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200952 }
953
Ben Widawsky41bde552013-12-06 14:11:21 -0800954 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200955}
956
Chris Wilson432e58e2010-11-25 19:32:06 +0000957static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200958i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100959 struct intel_engine_cs *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000960{
Ben Widawsky27173f12013-08-14 11:38:36 +0200961 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000962
Ben Widawsky27173f12013-08-14 11:38:36 +0200963 list_for_each_entry(vma, vmas, exec_list) {
964 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100965 u32 old_read = obj->base.read_domains;
966 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000967
Chris Wilson432e58e2010-11-25 19:32:06 +0000968 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100969 if (obj->base.write_domain == 0)
970 obj->base.pending_read_domains |= obj->base.read_domains;
971 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000972 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
973
Ben Widawskye2d05a82013-09-24 09:57:58 -0700974 i915_vma_move_to_active(vma, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000975 if (obj->base.write_domain) {
976 obj->dirty = 1;
Chris Wilson9d7730912012-11-27 16:22:52 +0000977 obj->last_write_seqno = intel_ring_get_seqno(ring);
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800978 /* check for potential scanout */
979 if (i915_gem_obj_ggtt_bound(obj) &&
980 i915_gem_obj_to_ggtt(obj)->pin_count)
Chris Wilsonc65355b2013-06-06 16:53:41 -0300981 intel_mark_fb_busy(obj, ring);
Chris Wilsonc8725f32014-03-17 12:21:55 +0000982
983 /* update for the implicit flush after a batch */
984 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson432e58e2010-11-25 19:32:06 +0000985 }
986
Chris Wilsondb53a302011-02-03 11:57:46 +0000987 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000988 }
989}
990
Chris Wilson54cf91d2010-11-25 18:00:26 +0000991static void
992i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000993 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100994 struct intel_engine_cs *ring,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300995 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000996{
Daniel Vettercc889e02012-06-13 20:45:19 +0200997 /* Unconditionally force add_request to emit a full flush. */
998 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000999
Chris Wilson432e58e2010-11-25 19:32:06 +00001000 /* Add a breadcrumb for the completion of the batch buffer */
Mika Kuoppala7d736f42013-06-12 15:01:39 +03001001 (void)__i915_add_request(ring, file, obj, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +00001002}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001003
1004static int
Eric Anholtae662d32012-01-03 09:23:29 -08001005i915_reset_gen7_sol_offsets(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001006 struct intel_engine_cs *ring)
Eric Anholtae662d32012-01-03 09:23:29 -08001007{
Jani Nikula50227e12014-03-31 14:27:21 +03001008 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtae662d32012-01-03 09:23:29 -08001009 int ret, i;
1010
Daniel Vetter9d662da2014-04-24 08:09:09 +02001011 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
1012 DRM_DEBUG("sol reset is gen7/rcs only\n");
1013 return -EINVAL;
1014 }
Eric Anholtae662d32012-01-03 09:23:29 -08001015
1016 ret = intel_ring_begin(ring, 4 * 3);
1017 if (ret)
1018 return ret;
1019
1020 for (i = 0; i < 4; i++) {
1021 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1022 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
1023 intel_ring_emit(ring, 0);
1024 }
1025
1026 intel_ring_advance(ring);
1027
1028 return 0;
1029}
1030
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001031/**
1032 * Find one BSD ring to dispatch the corresponding BSD command.
1033 * The Ring ID is returned.
1034 */
1035static int gen8_dispatch_bsd_ring(struct drm_device *dev,
1036 struct drm_file *file)
1037{
1038 struct drm_i915_private *dev_priv = dev->dev_private;
1039 struct drm_i915_file_private *file_priv = file->driver_priv;
1040
1041 /* Check whether the file_priv is using one ring */
1042 if (file_priv->bsd_ring)
1043 return file_priv->bsd_ring->id;
1044 else {
1045 /* If no, use the ping-pong mechanism to select one ring */
1046 int ring_id;
1047
1048 mutex_lock(&dev->struct_mutex);
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001049 if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001050 ring_id = VCS;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001051 dev_priv->mm.bsd_ring_dispatch_index = 1;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001052 } else {
1053 ring_id = VCS2;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001054 dev_priv->mm.bsd_ring_dispatch_index = 0;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001055 }
1056 file_priv->bsd_ring = &dev_priv->ring[ring_id];
1057 mutex_unlock(&dev->struct_mutex);
1058 return ring_id;
1059 }
1060}
1061
Chris Wilsond23db882014-05-23 08:48:08 +02001062static struct drm_i915_gem_object *
1063eb_get_batch(struct eb_vmas *eb)
1064{
1065 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
1066
1067 /*
1068 * SNA is doing fancy tricks with compressing batch buffers, which leads
1069 * to negative relocation deltas. Usually that works out ok since the
1070 * relocate address is still positive, except when the batch is placed
1071 * very low in the GTT. Ensure this doesn't happen.
1072 *
1073 * Note that actual hangs have only been observed on gen7, but for
1074 * paranoia do it everywhere.
1075 */
1076 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
1077
1078 return vma->obj;
1079}
1080
Eric Anholtae662d32012-01-03 09:23:29 -08001081static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001082i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1083 struct drm_file *file,
1084 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001085 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001086{
Jani Nikula50227e12014-03-31 14:27:21 +03001087 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +02001088 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001089 struct drm_i915_gem_object *batch_obj;
1090 struct drm_clip_rect *cliprects = NULL;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001091 struct intel_engine_cs *ring;
Oscar Mateo273497e2014-05-22 14:13:37 +01001092 struct intel_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001093 struct i915_address_space *vm;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001094 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001095 u64 exec_start = args->batch_start_offset, exec_len;
Daniel Vettered5982e2013-01-17 22:23:36 +01001096 u32 mask, flags;
Chris Wilson72bfa192010-12-19 11:42:05 +00001097 int ret, mode, i;
Daniel Vettered5982e2013-01-17 22:23:36 +01001098 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001099
Daniel Vettered5982e2013-01-17 22:23:36 +01001100 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001101 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001102
1103 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001104 if (ret)
1105 return ret;
1106
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001107 flags = 0;
1108 if (args->flags & I915_EXEC_SECURE) {
1109 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1110 return -EPERM;
1111
1112 flags |= I915_DISPATCH_SECURE;
1113 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001114 if (args->flags & I915_EXEC_IS_PINNED)
1115 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001116
Zhao Yakuib1a93302014-04-17 10:37:36 +08001117 if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
Daniel Vetterff240192012-01-31 21:08:14 +01001118 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001119 (int)(args->flags & I915_EXEC_RING_MASK));
1120 return -EINVAL;
1121 }
Ben Widawskyca01b122013-12-06 14:11:00 -08001122
1123 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1124 ring = &dev_priv->ring[RCS];
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001125 else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
1126 if (HAS_BSD2(dev)) {
1127 int ring_id;
1128 ring_id = gen8_dispatch_bsd_ring(dev, file);
1129 ring = &dev_priv->ring[ring_id];
1130 } else
1131 ring = &dev_priv->ring[VCS];
1132 } else
Ben Widawskyca01b122013-12-06 14:11:00 -08001133 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1134
Chris Wilsona15817c2012-05-11 14:29:31 +01001135 if (!intel_ring_initialized(ring)) {
1136 DRM_DEBUG("execbuf with invalid ring: %d\n",
1137 (int)(args->flags & I915_EXEC_RING_MASK));
1138 return -EINVAL;
1139 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001140
Chris Wilson72bfa192010-12-19 11:42:05 +00001141 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001142 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +00001143 switch (mode) {
1144 case I915_EXEC_CONSTANTS_REL_GENERAL:
1145 case I915_EXEC_CONSTANTS_ABSOLUTE:
1146 case I915_EXEC_CONSTANTS_REL_SURFACE:
Daniel Vetterc0f5b822014-04-24 08:09:10 +02001147 if (mode != 0 && ring != &dev_priv->ring[RCS]) {
1148 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
1149 return -EINVAL;
1150 }
1151
1152 if (mode != dev_priv->relative_constants_mode) {
1153 if (INTEL_INFO(dev)->gen < 4) {
1154 DRM_DEBUG("no rel constants on pre-gen4\n");
Chris Wilson72bfa192010-12-19 11:42:05 +00001155 return -EINVAL;
Daniel Vetterc0f5b822014-04-24 08:09:10 +02001156 }
Chris Wilson72bfa192010-12-19 11:42:05 +00001157
1158 if (INTEL_INFO(dev)->gen > 5 &&
Daniel Vetterc0f5b822014-04-24 08:09:10 +02001159 mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1160 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
Chris Wilson72bfa192010-12-19 11:42:05 +00001161 return -EINVAL;
Daniel Vetterc0f5b822014-04-24 08:09:10 +02001162 }
Ben Widawsky84f9f932011-12-12 19:21:58 -08001163
1164 /* The HW changed the meaning on this bit on gen6 */
1165 if (INTEL_INFO(dev)->gen >= 6)
1166 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +00001167 }
1168 break;
1169 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001170 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +00001171 return -EINVAL;
1172 }
1173
Chris Wilson54cf91d2010-11-25 18:00:26 +00001174 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001175 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001176 return -EINVAL;
1177 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001178
1179 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001180 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +01001181 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001182 return -EINVAL;
1183 }
1184
Daniel Vetter6ebebc92012-04-26 23:28:11 +02001185 if (INTEL_INFO(dev)->gen >= 5) {
1186 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1187 return -EINVAL;
1188 }
1189
Xi Wang44afb3a2012-04-23 04:06:42 -04001190 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1191 DRM_DEBUG("execbuf with %u cliprects\n",
1192 args->num_cliprects);
1193 return -EINVAL;
1194 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001195
Daniel Vettera1e22652013-09-21 00:35:38 +02001196 cliprects = kcalloc(args->num_cliprects,
1197 sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001198 GFP_KERNEL);
1199 if (cliprects == NULL) {
1200 ret = -ENOMEM;
1201 goto pre_mutex_err;
1202 }
1203
Chris Wilson432e58e2010-11-25 19:32:06 +00001204 if (copy_from_user(cliprects,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001205 to_user_ptr(args->cliprects_ptr),
1206 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001207 ret = -EFAULT;
1208 goto pre_mutex_err;
1209 }
Daniel Vetter9cb34662014-04-24 08:09:11 +02001210 } else {
Daniel Vetterffd93f22014-05-13 13:37:37 +02001211 if (args->DR4 == 0xffffffff) {
1212 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1213 args->DR4 = 0;
1214 }
1215
Daniel Vetter9cb34662014-04-24 08:09:11 +02001216 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
1217 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
1218 return -EINVAL;
1219 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001220 }
1221
Paulo Zanonif65c9162013-11-27 18:20:34 -02001222 intel_runtime_pm_get(dev_priv);
1223
Chris Wilson54cf91d2010-11-25 18:00:26 +00001224 ret = i915_mutex_lock_interruptible(dev);
1225 if (ret)
1226 goto pre_mutex_err;
1227
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001228 if (dev_priv->ums.mm_suspended) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001229 mutex_unlock(&dev->struct_mutex);
1230 ret = -EBUSY;
1231 goto pre_mutex_err;
1232 }
1233
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001234 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001235 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001236 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001237 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001238 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001239 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001240
1241 i915_gem_context_reference(ctx);
1242
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001243 vm = ctx->vm;
1244 if (!USES_FULL_PPGTT(dev))
1245 vm = &dev_priv->gtt.base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001246
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001247 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001248 if (eb == NULL) {
Ben Widawsky935f38d2014-04-04 22:41:07 -07001249 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001250 mutex_unlock(&dev->struct_mutex);
1251 ret = -ENOMEM;
1252 goto pre_mutex_err;
1253 }
1254
Chris Wilson54cf91d2010-11-25 18:00:26 +00001255 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001256 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001257 if (ret)
1258 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001259
Chris Wilson6fe4f142011-01-10 17:35:37 +00001260 /* take note of the batch buffer before we might reorder the lists */
Chris Wilsond23db882014-05-23 08:48:08 +02001261 batch_obj = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001262
Chris Wilson54cf91d2010-11-25 18:00:26 +00001263 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001264 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001265 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001266 if (ret)
1267 goto err;
1268
1269 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001270 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001271 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001272 if (ret) {
1273 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001274 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001275 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001276 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1277 }
1278 if (ret)
1279 goto err;
1280 }
1281
1282 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001283 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001284 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001285 ret = -EINVAL;
1286 goto err;
1287 }
1288 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1289
Brad Volkin351e3db2014-02-18 10:15:46 -08001290 if (i915_needs_cmd_parser(ring)) {
1291 ret = i915_parse_cmds(ring,
1292 batch_obj,
1293 args->batch_start_offset,
1294 file->is_master);
1295 if (ret)
1296 goto err;
1297
1298 /*
1299 * XXX: Actually do this when enabling batch copy...
1300 *
1301 * Set the DISPATCH_SECURE bit to remove the NON_SECURE bit
1302 * from MI_BATCH_BUFFER_START commands issued in the
1303 * dispatch_execbuffer implementations. We specifically don't
1304 * want that set when the command parser is enabled.
1305 */
1306 }
1307
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001308 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1309 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001310 * hsw should have this fixed, but bdw mucks it up again. */
Ben Widawsky6f65e292013-12-06 14:10:56 -08001311 if (flags & I915_DISPATCH_SECURE &&
1312 !batch_obj->has_global_gtt_mapping) {
1313 /* When we have multiple VMs, we'll need to make sure that we
1314 * allocate space first */
1315 struct i915_vma *vma = i915_gem_obj_to_ggtt(batch_obj);
1316 BUG_ON(!vma);
1317 vma->bind_vma(vma, batch_obj->cache_level, GLOBAL_BIND);
1318 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001319
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001320 if (flags & I915_DISPATCH_SECURE)
1321 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
1322 else
1323 exec_start += i915_gem_obj_offset(batch_obj, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001324
Ben Widawsky27173f12013-08-14 11:38:36 +02001325 ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001326 if (ret)
1327 goto err;
1328
Chris Wilson691e6412014-04-09 09:07:36 +01001329 ret = i915_switch_context(ring, ctx);
Eric Anholt0da5cec2012-07-23 12:33:55 -07001330 if (ret)
1331 goto err;
1332
Ben Widawskye2971bd2011-12-12 19:21:57 -08001333 if (ring == &dev_priv->ring[RCS] &&
1334 mode != dev_priv->relative_constants_mode) {
1335 ret = intel_ring_begin(ring, 4);
1336 if (ret)
1337 goto err;
1338
1339 intel_ring_emit(ring, MI_NOOP);
1340 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1341 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001342 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001343 intel_ring_advance(ring);
1344
1345 dev_priv->relative_constants_mode = mode;
1346 }
1347
Eric Anholtae662d32012-01-03 09:23:29 -08001348 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1349 ret = i915_reset_gen7_sol_offsets(dev, ring);
1350 if (ret)
1351 goto err;
1352 }
1353
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001354
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001355 exec_len = args->batch_len;
1356 if (cliprects) {
1357 for (i = 0; i < args->num_cliprects; i++) {
1358 ret = i915_emit_box(dev, &cliprects[i],
1359 args->DR1, args->DR4);
1360 if (ret)
1361 goto err;
1362
1363 ret = ring->dispatch_execbuffer(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001364 exec_start, exec_len,
1365 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001366 if (ret)
1367 goto err;
1368 }
1369 } else {
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001370 ret = ring->dispatch_execbuffer(ring,
1371 exec_start, exec_len,
1372 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001373 if (ret)
1374 goto err;
1375 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001376
Chris Wilson9d7730912012-11-27 16:22:52 +00001377 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1378
Ben Widawsky27173f12013-08-14 11:38:36 +02001379 i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
Mika Kuoppala7d736f42013-06-12 15:01:39 +03001380 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001381
1382err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001383 /* the request owns the ref now */
1384 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001385 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001386
1387 mutex_unlock(&dev->struct_mutex);
1388
1389pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001390 kfree(cliprects);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001391
1392 /* intel_gpu_busy should also get a ref, so it will free when the device
1393 * is really idle. */
1394 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001395 return ret;
1396}
1397
1398/*
1399 * Legacy execbuffer just creates an exec2 list from the original exec object
1400 * list array and passes it to the real function.
1401 */
1402int
1403i915_gem_execbuffer(struct drm_device *dev, void *data,
1404 struct drm_file *file)
1405{
1406 struct drm_i915_gem_execbuffer *args = data;
1407 struct drm_i915_gem_execbuffer2 exec2;
1408 struct drm_i915_gem_exec_object *exec_list = NULL;
1409 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1410 int ret, i;
1411
Chris Wilson54cf91d2010-11-25 18:00:26 +00001412 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001413 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001414 return -EINVAL;
1415 }
1416
1417 /* Copy in the exec list from userland */
1418 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1419 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1420 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001421 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001422 args->buffer_count);
1423 drm_free_large(exec_list);
1424 drm_free_large(exec2_list);
1425 return -ENOMEM;
1426 }
1427 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001428 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001429 sizeof(*exec_list) * args->buffer_count);
1430 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001431 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001432 args->buffer_count, ret);
1433 drm_free_large(exec_list);
1434 drm_free_large(exec2_list);
1435 return -EFAULT;
1436 }
1437
1438 for (i = 0; i < args->buffer_count; i++) {
1439 exec2_list[i].handle = exec_list[i].handle;
1440 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1441 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1442 exec2_list[i].alignment = exec_list[i].alignment;
1443 exec2_list[i].offset = exec_list[i].offset;
1444 if (INTEL_INFO(dev)->gen < 4)
1445 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1446 else
1447 exec2_list[i].flags = 0;
1448 }
1449
1450 exec2.buffers_ptr = args->buffers_ptr;
1451 exec2.buffer_count = args->buffer_count;
1452 exec2.batch_start_offset = args->batch_start_offset;
1453 exec2.batch_len = args->batch_len;
1454 exec2.DR1 = args->DR1;
1455 exec2.DR4 = args->DR4;
1456 exec2.num_cliprects = args->num_cliprects;
1457 exec2.cliprects_ptr = args->cliprects_ptr;
1458 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001459 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001460
Ben Widawsky41bde552013-12-06 14:11:21 -08001461 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001462 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001463 struct drm_i915_gem_exec_object __user *user_exec_list =
1464 to_user_ptr(args->buffers_ptr);
1465
Chris Wilson54cf91d2010-11-25 18:00:26 +00001466 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001467 for (i = 0; i < args->buffer_count; i++) {
1468 ret = __copy_to_user(&user_exec_list[i].offset,
1469 &exec2_list[i].offset,
1470 sizeof(user_exec_list[i].offset));
1471 if (ret) {
1472 ret = -EFAULT;
1473 DRM_DEBUG("failed to copy %d exec entries "
1474 "back to user (%d)\n",
1475 args->buffer_count, ret);
1476 break;
1477 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001478 }
1479 }
1480
1481 drm_free_large(exec_list);
1482 drm_free_large(exec2_list);
1483 return ret;
1484}
1485
1486int
1487i915_gem_execbuffer2(struct drm_device *dev, void *data,
1488 struct drm_file *file)
1489{
1490 struct drm_i915_gem_execbuffer2 *args = data;
1491 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1492 int ret;
1493
Xi Wanged8cd3b2012-04-23 04:06:41 -04001494 if (args->buffer_count < 1 ||
1495 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001496 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001497 return -EINVAL;
1498 }
1499
Daniel Vetter9cb34662014-04-24 08:09:11 +02001500 if (args->rsvd2 != 0) {
1501 DRM_DEBUG("dirty rvsd2 field\n");
1502 return -EINVAL;
1503 }
1504
Chris Wilson8408c282011-02-21 12:54:48 +00001505 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001506 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001507 if (exec2_list == NULL)
1508 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1509 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001510 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001511 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001512 args->buffer_count);
1513 return -ENOMEM;
1514 }
1515 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001516 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001517 sizeof(*exec2_list) * args->buffer_count);
1518 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001519 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001520 args->buffer_count, ret);
1521 drm_free_large(exec2_list);
1522 return -EFAULT;
1523 }
1524
Ben Widawsky41bde552013-12-06 14:11:21 -08001525 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001526 if (!ret) {
1527 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001528 struct drm_i915_gem_exec_object2 *user_exec_list =
1529 to_user_ptr(args->buffers_ptr);
1530 int i;
1531
1532 for (i = 0; i < args->buffer_count; i++) {
1533 ret = __copy_to_user(&user_exec_list[i].offset,
1534 &exec2_list[i].offset,
1535 sizeof(user_exec_list[i].offset));
1536 if (ret) {
1537 ret = -EFAULT;
1538 DRM_DEBUG("failed to copy %d exec entries "
1539 "back to user\n",
1540 args->buffer_count);
1541 break;
1542 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001543 }
1544 }
1545
1546 drm_free_large(exec2_list);
1547 return ret;
1548}