blob: 5973e2005e894ca1d2249f39fc75b4eee9093ddb [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 Wilsone6a84462014-08-11 12:00:12 +020038#define __EXEC_OBJECT_NEEDS_MAP (1<<29)
Chris Wilsond23db882014-05-23 08:48:08 +020039#define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
40
41#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000042
Ben Widawsky27173f12013-08-14 11:38:36 +020043struct eb_vmas {
44 struct list_head vmas;
Chris Wilson67731b82010-12-08 10:38:14 +000045 int and;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000046 union {
Ben Widawsky27173f12013-08-14 11:38:36 +020047 struct i915_vma *lut[0];
Chris Wilsoneef90cc2013-01-08 10:53:17 +000048 struct hlist_head buckets[0];
49 };
Chris Wilson67731b82010-12-08 10:38:14 +000050};
51
Ben Widawsky27173f12013-08-14 11:38:36 +020052static struct eb_vmas *
Ben Widawsky17601cbc2013-11-25 09:54:38 -080053eb_create(struct drm_i915_gem_execbuffer2 *args)
Chris Wilson67731b82010-12-08 10:38:14 +000054{
Ben Widawsky27173f12013-08-14 11:38:36 +020055 struct eb_vmas *eb = NULL;
Chris Wilson67731b82010-12-08 10:38:14 +000056
Chris Wilsoneef90cc2013-01-08 10:53:17 +000057 if (args->flags & I915_EXEC_HANDLE_LUT) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020058 unsigned size = args->buffer_count;
Ben Widawsky27173f12013-08-14 11:38:36 +020059 size *= sizeof(struct i915_vma *);
60 size += sizeof(struct eb_vmas);
Chris Wilsoneef90cc2013-01-08 10:53:17 +000061 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
62 }
63
64 if (eb == NULL) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020065 unsigned size = args->buffer_count;
66 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Lauri Kasanen27b7c632013-03-27 15:04:55 +020067 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
Chris Wilsoneef90cc2013-01-08 10:53:17 +000068 while (count > 2*size)
69 count >>= 1;
70 eb = kzalloc(count*sizeof(struct hlist_head) +
Ben Widawsky27173f12013-08-14 11:38:36 +020071 sizeof(struct eb_vmas),
Chris Wilsoneef90cc2013-01-08 10:53:17 +000072 GFP_TEMPORARY);
73 if (eb == NULL)
74 return eb;
75
76 eb->and = count - 1;
77 } else
78 eb->and = -args->buffer_count;
79
Ben Widawsky27173f12013-08-14 11:38:36 +020080 INIT_LIST_HEAD(&eb->vmas);
Chris Wilson67731b82010-12-08 10:38:14 +000081 return eb;
82}
83
84static void
Ben Widawsky27173f12013-08-14 11:38:36 +020085eb_reset(struct eb_vmas *eb)
Chris Wilson67731b82010-12-08 10:38:14 +000086{
Chris Wilsoneef90cc2013-01-08 10:53:17 +000087 if (eb->and >= 0)
88 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
Chris Wilson67731b82010-12-08 10:38:14 +000089}
90
Chris Wilson3b96eff2013-01-08 10:53:14 +000091static int
Ben Widawsky27173f12013-08-14 11:38:36 +020092eb_lookup_vmas(struct eb_vmas *eb,
93 struct drm_i915_gem_exec_object2 *exec,
94 const struct drm_i915_gem_execbuffer2 *args,
95 struct i915_address_space *vm,
96 struct drm_file *file)
Chris Wilson3b96eff2013-01-08 10:53:14 +000097{
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
Thomas Hellstrom355a7012014-11-20 09:56:25 +0100124 WARN_ONCE(obj->base.dumb,
125 "GPU use of dumb buffer is illegal.\n");
126
Chris Wilson3b96eff2013-01-08 10:53:14 +0000127 drm_gem_object_reference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200128 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000129 }
130 spin_unlock(&file->table_lock);
131
Ben Widawsky27173f12013-08-14 11:38:36 +0200132 i = 0;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000133 while (!list_empty(&objects)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200134 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800135
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000136 obj = list_first_entry(&objects,
137 struct drm_i915_gem_object,
138 obj_exec_link);
139
Daniel Vettere656a6c2013-08-14 14:14:04 +0200140 /*
141 * NOTE: We can leak any vmas created here when something fails
142 * later on. But that's no issue since vma_unbind can deal with
143 * vmas which are not actually bound. And since only
144 * lookup_or_create exists as an interface to get at the vma
145 * from the (obj, vm) we don't run the risk of creating
146 * duplicated vmas for the same vm.
147 */
Daniel Vetterda51a1e2014-08-11 12:08:58 +0200148 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200149 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200150 DRM_DEBUG("Failed to lookup VMA\n");
151 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000152 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200153 }
154
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000155 /* Transfer ownership from the objects list to the vmas list. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200156 list_add_tail(&vma->exec_list, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000157 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200158
159 vma->exec_entry = &exec[i];
160 if (eb->and < 0) {
161 eb->lut[i] = vma;
162 } else {
163 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
164 vma->exec_handle = handle;
165 hlist_add_head(&vma->exec_node,
166 &eb->buckets[handle & eb->and]);
167 }
168 ++i;
169 }
170
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000171 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200172
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000173
174err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200175 while (!list_empty(&objects)) {
176 obj = list_first_entry(&objects,
177 struct drm_i915_gem_object,
178 obj_exec_link);
179 list_del_init(&obj->obj_exec_link);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000180 drm_gem_object_unreference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200181 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000182 /*
183 * Objects already transfered to the vmas list will be unreferenced by
184 * eb_destroy.
185 */
186
Ben Widawsky27173f12013-08-14 11:38:36 +0200187 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000188}
189
Ben Widawsky27173f12013-08-14 11:38:36 +0200190static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000191{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000192 if (eb->and < 0) {
193 if (handle >= -eb->and)
194 return NULL;
195 return eb->lut[handle];
196 } else {
197 struct hlist_head *head;
198 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000199
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000200 head = &eb->buckets[handle & eb->and];
201 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200202 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000203
Ben Widawsky27173f12013-08-14 11:38:36 +0200204 vma = hlist_entry(node, struct i915_vma, exec_node);
205 if (vma->exec_handle == handle)
206 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000207 }
208 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000209 }
Chris Wilson67731b82010-12-08 10:38:14 +0000210}
211
Chris Wilsona415d352013-11-26 11:23:15 +0000212static void
213i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
214{
215 struct drm_i915_gem_exec_object2 *entry;
216 struct drm_i915_gem_object *obj = vma->obj;
217
218 if (!drm_mm_node_allocated(&vma->node))
219 return;
220
221 entry = vma->exec_entry;
222
223 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
224 i915_gem_object_unpin_fence(obj);
225
226 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Daniel Vetter3d7f0f92013-12-18 16:23:37 +0100227 vma->pin_count--;
Chris Wilsona415d352013-11-26 11:23:15 +0000228
229 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
230}
231
232static void eb_destroy(struct eb_vmas *eb)
233{
Ben Widawsky27173f12013-08-14 11:38:36 +0200234 while (!list_empty(&eb->vmas)) {
235 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000236
Ben Widawsky27173f12013-08-14 11:38:36 +0200237 vma = list_first_entry(&eb->vmas,
238 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000239 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200240 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000241 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200242 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000243 }
Chris Wilson67731b82010-12-08 10:38:14 +0000244 kfree(eb);
245}
246
Chris Wilsondabdfe02012-03-26 10:10:27 +0200247static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
248{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300249 return (HAS_LLC(obj->base.dev) ||
250 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100251 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200252 obj->cache_level != I915_CACHE_NONE);
253}
254
Chris Wilson54cf91d2010-11-25 18:00:26 +0000255static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100256relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700257 struct drm_i915_gem_relocation_entry *reloc,
258 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100259{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700260 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100261 uint32_t page_offset = offset_in_page(reloc->offset);
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700262 uint64_t delta = reloc->delta + target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100263 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800264 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100265
Chris Wilson2cc86b82013-08-26 19:51:00 -0300266 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100267 if (ret)
268 return ret;
269
270 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
271 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700272 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700273
274 if (INTEL_INFO(dev)->gen >= 8) {
275 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
276
277 if (page_offset == 0) {
278 kunmap_atomic(vaddr);
279 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
280 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
281 }
282
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700283 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700284 }
285
Rafael Barbalho5032d872013-08-21 17:10:51 +0100286 kunmap_atomic(vaddr);
287
288 return 0;
289}
290
291static int
292relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700293 struct drm_i915_gem_relocation_entry *reloc,
294 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100295{
296 struct drm_device *dev = obj->base.dev;
297 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700298 uint64_t delta = reloc->delta + target_offset;
Chris Wilson906843c2014-08-10 06:29:11 +0100299 uint64_t offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100300 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800301 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100302
303 ret = i915_gem_object_set_to_gtt_domain(obj, true);
304 if (ret)
305 return ret;
306
307 ret = i915_gem_object_put_fence(obj);
308 if (ret)
309 return ret;
310
311 /* Map the page containing the relocation we're going to perform. */
Chris Wilson906843c2014-08-10 06:29:11 +0100312 offset = i915_gem_obj_ggtt_offset(obj);
313 offset += reloc->offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100314 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100315 offset & PAGE_MASK);
316 iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700317
318 if (INTEL_INFO(dev)->gen >= 8) {
Chris Wilson906843c2014-08-10 06:29:11 +0100319 offset += sizeof(uint32_t);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700320
Chris Wilson906843c2014-08-10 06:29:11 +0100321 if (offset_in_page(offset) == 0) {
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700322 io_mapping_unmap_atomic(reloc_page);
Chris Wilson906843c2014-08-10 06:29:11 +0100323 reloc_page =
324 io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
325 offset);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700326 }
327
Chris Wilson906843c2014-08-10 06:29:11 +0100328 iowrite32(upper_32_bits(delta),
329 reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700330 }
331
Rafael Barbalho5032d872013-08-21 17:10:51 +0100332 io_mapping_unmap_atomic(reloc_page);
333
334 return 0;
335}
336
337static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000338i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200339 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800340 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000341{
342 struct drm_device *dev = obj->base.dev;
343 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100344 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200345 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700346 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800347 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000348
Chris Wilson67731b82010-12-08 10:38:14 +0000349 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200350 target_vma = eb_get_vma(eb, reloc->target_handle);
351 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000352 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200353 target_i915_obj = target_vma->obj;
354 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000355
Ben Widawsky5ce09722013-11-25 09:54:40 -0800356 target_offset = target_vma->node.start;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000357
Eric Anholte844b992012-07-31 15:35:01 -0700358 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
359 * pipe_control writes because the gpu doesn't properly redirect them
360 * through the ppgtt for non_secure batchbuffers. */
361 if (unlikely(IS_GEN6(dev) &&
362 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000363 !(target_vma->bound & GLOBAL_BIND))) {
364 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
365 GLOBAL_BIND);
366 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
367 return ret;
368 }
Eric Anholte844b992012-07-31 15:35:01 -0700369
Chris Wilson54cf91d2010-11-25 18:00:26 +0000370 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000371 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100372 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000373 "obj %p target %d offset %d "
374 "read %08x write %08x",
375 obj, reloc->target_handle,
376 (int) reloc->offset,
377 reloc->read_domains,
378 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800379 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000380 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100381 if (unlikely((reloc->write_domain | reloc->read_domains)
382 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100383 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000384 "obj %p target %d offset %d "
385 "read %08x write %08x",
386 obj, reloc->target_handle,
387 (int) reloc->offset,
388 reloc->read_domains,
389 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800390 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000391 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000392
393 target_obj->pending_read_domains |= reloc->read_domains;
394 target_obj->pending_write_domain |= reloc->write_domain;
395
396 /* If the relocation already has the right value in it, no
397 * more work needs to be done.
398 */
399 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000400 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000401
402 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700403 if (unlikely(reloc->offset >
404 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100405 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000406 "obj %p target %d offset %d size %d.\n",
407 obj, reloc->target_handle,
408 (int) reloc->offset,
409 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800410 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000411 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000412 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100413 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000414 "obj %p target %d offset %d.\n",
415 obj, reloc->target_handle,
416 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800417 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000418 }
419
Chris Wilsondabdfe02012-03-26 10:10:27 +0200420 /* We can't wait for rendering with pagefaults disabled */
421 if (obj->active && in_atomic())
422 return -EFAULT;
423
Rafael Barbalho5032d872013-08-21 17:10:51 +0100424 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700425 ret = relocate_entry_cpu(obj, reloc, target_offset);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100426 else
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700427 ret = relocate_entry_gtt(obj, reloc, target_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000428
Daniel Vetterd4d36012013-09-02 20:56:23 +0200429 if (ret)
430 return ret;
431
Chris Wilson54cf91d2010-11-25 18:00:26 +0000432 /* and update the user's relocation entry */
433 reloc->presumed_offset = target_offset;
434
Chris Wilson67731b82010-12-08 10:38:14 +0000435 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000436}
437
438static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200439i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
440 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000441{
Chris Wilson1d83f442012-03-24 20:12:53 +0000442#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
443 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000444 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200445 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000446 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000447
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200448 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000449
Chris Wilson1d83f442012-03-24 20:12:53 +0000450 remain = entry->relocation_count;
451 while (remain) {
452 struct drm_i915_gem_relocation_entry *r = stack_reloc;
453 int count = remain;
454 if (count > ARRAY_SIZE(stack_reloc))
455 count = ARRAY_SIZE(stack_reloc);
456 remain -= count;
457
458 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000459 return -EFAULT;
460
Chris Wilson1d83f442012-03-24 20:12:53 +0000461 do {
462 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000463
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800464 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000465 if (ret)
466 return ret;
467
468 if (r->presumed_offset != offset &&
469 __copy_to_user_inatomic(&user_relocs->presumed_offset,
470 &r->presumed_offset,
471 sizeof(r->presumed_offset))) {
472 return -EFAULT;
473 }
474
475 user_relocs++;
476 r++;
477 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000478 }
479
480 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000481#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000482}
483
484static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200485i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
486 struct eb_vmas *eb,
487 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000488{
Ben Widawsky27173f12013-08-14 11:38:36 +0200489 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000490 int i, ret;
491
492 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800493 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000494 if (ret)
495 return ret;
496 }
497
498 return 0;
499}
500
501static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800502i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000503{
Ben Widawsky27173f12013-08-14 11:38:36 +0200504 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000505 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000506
Chris Wilsond4aeee72011-03-14 15:11:24 +0000507 /* This is the fast path and we cannot handle a pagefault whilst
508 * holding the struct mutex lest the user pass in the relocations
509 * contained within a mmaped bo. For in such a case we, the page
510 * fault handler would call i915_gem_fault() and we would try to
511 * acquire the struct mutex again. Obviously this is bad and so
512 * lockdep complains vehemently.
513 */
514 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200515 list_for_each_entry(vma, &eb->vmas, exec_list) {
516 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000517 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000518 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000519 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000520 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000521
Chris Wilsond4aeee72011-03-14 15:11:24 +0000522 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000523}
524
Chris Wilson1690e1e2011-12-14 13:57:08 +0100525static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200526i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100527 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200528 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100529{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800530 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200531 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200532 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100533 int ret;
534
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100535 flags = 0;
Chris Wilsone6a84462014-08-11 12:00:12 +0200536 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
Chris Wilsonc826c442014-10-31 13:53:53 +0000537 flags |= PIN_GLOBAL | PIN_MAPPABLE;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100538 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
Daniel Vetterbf3d1492014-02-14 14:01:12 +0100539 flags |= PIN_GLOBAL;
Chris Wilsond23db882014-05-23 08:48:08 +0200540 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
541 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100542
543 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100544 if (ret)
545 return ret;
546
Chris Wilson7788a762012-08-24 19:18:18 +0100547 entry->flags |= __EXEC_OBJECT_HAS_PIN;
548
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100549 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
550 ret = i915_gem_object_get_fence(obj);
551 if (ret)
552 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100553
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100554 if (i915_gem_object_pin_fence(obj))
555 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100556 }
557
Ben Widawsky27173f12013-08-14 11:38:36 +0200558 if (entry->offset != vma->node.start) {
559 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100560 *need_reloc = true;
561 }
562
563 if (entry->flags & EXEC_OBJECT_WRITE) {
564 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
565 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
566 }
567
Chris Wilson1690e1e2011-12-14 13:57:08 +0100568 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100569}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100570
Chris Wilsond23db882014-05-23 08:48:08 +0200571static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200572need_reloc_mappable(struct i915_vma *vma)
573{
574 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
575
576 if (entry->relocation_count == 0)
577 return false;
578
579 if (!i915_is_ggtt(vma->vm))
580 return false;
581
582 /* See also use_cpu_reloc() */
583 if (HAS_LLC(vma->obj->base.dev))
584 return false;
585
586 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
587 return false;
588
589 return true;
590}
591
592static bool
593eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200594{
595 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
596 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond23db882014-05-23 08:48:08 +0200597
Chris Wilsone6a84462014-08-11 12:00:12 +0200598 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
Chris Wilsond23db882014-05-23 08:48:08 +0200599 !i915_is_ggtt(vma->vm));
600
601 if (entry->alignment &&
602 vma->node.start & (entry->alignment - 1))
603 return true;
604
Chris Wilsone6a84462014-08-11 12:00:12 +0200605 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
Chris Wilsond23db882014-05-23 08:48:08 +0200606 return true;
607
608 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
609 vma->node.start < BATCH_OFFSET_BIAS)
610 return true;
611
612 return false;
613}
614
Chris Wilson54cf91d2010-11-25 18:00:26 +0000615static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100616i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200617 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100618 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000619{
Chris Wilson432e58e2010-11-25 19:32:06 +0000620 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200621 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700622 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200623 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100624 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
625 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000626
Chris Wilson227f7822014-05-15 10:41:42 +0100627 i915_gem_retire_requests_ring(ring);
628
Ben Widawsky68c8c172013-09-11 14:57:50 -0700629 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
630
Ben Widawsky27173f12013-08-14 11:38:36 +0200631 INIT_LIST_HEAD(&ordered_vmas);
632 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000633 struct drm_i915_gem_exec_object2 *entry;
634 bool need_fence, need_mappable;
635
Ben Widawsky27173f12013-08-14 11:38:36 +0200636 vma = list_first_entry(vmas, struct i915_vma, exec_list);
637 obj = vma->obj;
638 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000639
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100640 if (!has_fenced_gpu_access)
641 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000642 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000643 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
644 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200645 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000646
Chris Wilsone6a84462014-08-11 12:00:12 +0200647 if (need_mappable) {
648 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200649 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200650 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200651 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000652
Daniel Vettered5982e2013-01-17 22:23:36 +0100653 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000654 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000655 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200656 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000657
658 /* Attempt to pin all of the buffers into the GTT.
659 * This is done in 3 phases:
660 *
661 * 1a. Unbind all objects that do not match the GTT constraints for
662 * the execbuffer (fenceable, mappable, alignment etc).
663 * 1b. Increment pin count for already bound objects.
664 * 2. Bind new objects.
665 * 3. Decrement pin count.
666 *
Chris Wilson7788a762012-08-24 19:18:18 +0100667 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000668 * room for the earlier objects *unless* we need to defragment.
669 */
670 retry = 0;
671 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100672 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000673
674 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200675 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200676 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000677 continue;
678
Chris Wilsone6a84462014-08-11 12:00:12 +0200679 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200680 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000681 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200682 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000683 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000684 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000685 }
686
687 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200688 list_for_each_entry(vma, vmas, exec_list) {
689 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100690 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000691
Ben Widawsky27173f12013-08-14 11:38:36 +0200692 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100693 if (ret)
694 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000695 }
696
Chris Wilsona415d352013-11-26 11:23:15 +0000697err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200698 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000699 return ret;
700
Chris Wilsona415d352013-11-26 11:23:15 +0000701 /* Decrement pin count for bound objects */
702 list_for_each_entry(vma, vmas, exec_list)
703 i915_gem_execbuffer_unreserve_vma(vma);
704
Ben Widawsky68c8c172013-09-11 14:57:50 -0700705 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000706 if (ret)
707 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000708 } while (1);
709}
710
711static int
712i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100713 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000714 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100715 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200716 struct eb_vmas *eb,
717 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000718{
719 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200720 struct i915_address_space *vm;
721 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100722 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000723 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000724 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200725 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000726
Ben Widawsky27173f12013-08-14 11:38:36 +0200727 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
728
Chris Wilson67731b82010-12-08 10:38:14 +0000729 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200730 while (!list_empty(&eb->vmas)) {
731 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
732 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000733 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200734 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000735 }
736
Chris Wilson54cf91d2010-11-25 18:00:26 +0000737 mutex_unlock(&dev->struct_mutex);
738
739 total = 0;
740 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000741 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000742
Chris Wilsondd6864a2011-01-12 23:49:13 +0000743 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000744 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000745 if (reloc == NULL || reloc_offset == NULL) {
746 drm_free_large(reloc);
747 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000748 mutex_lock(&dev->struct_mutex);
749 return -ENOMEM;
750 }
751
752 total = 0;
753 for (i = 0; i < count; i++) {
754 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000755 u64 invalid_offset = (u64)-1;
756 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000757
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200758 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000759
760 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000761 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000762 ret = -EFAULT;
763 mutex_lock(&dev->struct_mutex);
764 goto err;
765 }
766
Chris Wilson262b6d32013-01-15 16:17:54 +0000767 /* As we do not update the known relocation offsets after
768 * relocating (due to the complexities in lock handling),
769 * we need to mark them as invalid now so that we force the
770 * relocation processing next time. Just in case the target
771 * object is evicted and then rebound into its old
772 * presumed_offset before the next execbuffer - if that
773 * happened we would make the mistake of assuming that the
774 * relocations were valid.
775 */
776 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100777 if (__copy_to_user(&user_relocs[j].presumed_offset,
778 &invalid_offset,
779 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000780 ret = -EFAULT;
781 mutex_lock(&dev->struct_mutex);
782 goto err;
783 }
784 }
785
Chris Wilsondd6864a2011-01-12 23:49:13 +0000786 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000787 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000788 }
789
790 ret = i915_mutex_lock_interruptible(dev);
791 if (ret) {
792 mutex_lock(&dev->struct_mutex);
793 goto err;
794 }
795
Chris Wilson67731b82010-12-08 10:38:14 +0000796 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000797 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200798 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000799 if (ret)
800 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000801
Daniel Vettered5982e2013-01-17 22:23:36 +0100802 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200803 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000804 if (ret)
805 goto err;
806
Ben Widawsky27173f12013-08-14 11:38:36 +0200807 list_for_each_entry(vma, &eb->vmas, exec_list) {
808 int offset = vma->exec_entry - exec;
809 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
810 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000811 if (ret)
812 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000813 }
814
815 /* Leave the user relocations as are, this is the painfully slow path,
816 * and we want to avoid the complication of dropping the lock whilst
817 * having buffers reserved in the aperture and so causing spurious
818 * ENOSPC for random operations.
819 */
820
821err:
822 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000823 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000824 return ret;
825}
826
Chris Wilson54cf91d2010-11-25 18:00:26 +0000827static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100828i915_gem_execbuffer_move_to_gpu(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200829 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000830{
Ben Widawsky27173f12013-08-14 11:38:36 +0200831 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200832 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100833 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000834 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000835
Ben Widawsky27173f12013-08-14 11:38:36 +0200836 list_for_each_entry(vma, vmas, exec_list) {
837 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700838 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000839 if (ret)
840 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200841
842 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100843 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200844
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200845 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000846 }
847
Chris Wilson000433b2013-08-08 14:41:09 +0100848 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800849 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200850
851 if (flush_domains & I915_GEM_DOMAIN_GTT)
852 wmb();
853
Chris Wilson09cf7c92012-07-13 14:14:08 +0100854 /* Unconditionally invalidate gpu caches and ensure that we do flush
855 * any residual writes from the previous batch.
856 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100857 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000858}
859
Chris Wilson432e58e2010-11-25 19:32:06 +0000860static bool
861i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000862{
Daniel Vettered5982e2013-01-17 22:23:36 +0100863 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
864 return false;
865
Chris Wilson432e58e2010-11-25 19:32:06 +0000866 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000867}
868
869static int
Chris Wilsonad19f102014-08-10 06:29:08 +0100870validate_exec_list(struct drm_device *dev,
871 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000872 int count)
873{
Daniel Vetterb205ca52013-09-19 14:00:11 +0200874 unsigned relocs_total = 0;
875 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +0100876 unsigned invalid_flags;
877 int i;
878
879 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
880 if (USES_FULL_PPGTT(dev))
881 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000882
883 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200884 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000885 int length; /* limited by fault_in_pages_readable() */
886
Chris Wilsonad19f102014-08-10 06:29:08 +0100887 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +0100888 return -EINVAL;
889
Kees Cook3118a4f2013-03-11 17:31:45 -0700890 /* First check for malicious input causing overflow in
891 * the worst case where we need to allocate the entire
892 * relocation tree as a single array.
893 */
894 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000895 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700896 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000897
898 length = exec[i].relocation_count *
899 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700900 /*
901 * We must check that the entire relocation array is safe
902 * to read, but since we may need to update the presumed
903 * offsets during execution, check for full write access.
904 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000905 if (!access_ok(VERIFY_WRITE, ptr, length))
906 return -EFAULT;
907
Jani Nikulad330a952014-01-21 11:24:25 +0200908 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +0800909 if (fault_in_multipages_readable(ptr, length))
910 return -EFAULT;
911 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000912 }
913
914 return 0;
915}
916
Oscar Mateo273497e2014-05-22 14:13:37 +0100917static struct intel_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200918i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100919 struct intel_engine_cs *ring, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200920{
Oscar Mateo273497e2014-05-22 14:13:37 +0100921 struct intel_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200922 struct i915_ctx_hang_stats *hs;
923
Oscar Mateo821d66d2014-07-03 16:28:00 +0100924 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +0100925 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200926
Ben Widawsky41bde552013-12-06 14:11:21 -0800927 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -1000928 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -0800929 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200930
Ben Widawsky41bde552013-12-06 14:11:21 -0800931 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200932 if (hs->banned) {
933 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -0800934 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200935 }
936
Oscar Mateoec3e9962014-07-24 17:04:18 +0100937 if (i915.enable_execlists && !ctx->engine[ring->id].state) {
938 int ret = intel_lr_context_deferred_create(ctx, ring);
939 if (ret) {
940 DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
941 return ERR_PTR(ret);
942 }
943 }
944
Ben Widawsky41bde552013-12-06 14:11:21 -0800945 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200946}
947
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100948void
Ben Widawsky27173f12013-08-14 11:38:36 +0200949i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100950 struct intel_engine_cs *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000951{
John Harrison97b2a6a2014-11-24 18:49:26 +0000952 struct drm_i915_gem_request *req = intel_ring_get_request(ring);
Ben Widawsky27173f12013-08-14 11:38:36 +0200953 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000954
Ben Widawsky27173f12013-08-14 11:38:36 +0200955 list_for_each_entry(vma, vmas, exec_list) {
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100956 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Ben Widawsky27173f12013-08-14 11:38:36 +0200957 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100958 u32 old_read = obj->base.read_domains;
959 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000960
Chris Wilson432e58e2010-11-25 19:32:06 +0000961 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100962 if (obj->base.write_domain == 0)
963 obj->base.pending_read_domains |= obj->base.read_domains;
964 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000965
Ben Widawskye2d05a82013-09-24 09:57:58 -0700966 i915_vma_move_to_active(vma, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000967 if (obj->base.write_domain) {
968 obj->dirty = 1;
John Harrison97b2a6a2014-11-24 18:49:26 +0000969 i915_gem_request_assign(&obj->last_write_req, req);
Daniel Vetterf99d7062014-06-19 16:01:59 +0200970
971 intel_fb_obj_invalidate(obj, ring);
Chris Wilsonc8725f32014-03-17 12:21:55 +0000972
973 /* update for the implicit flush after a batch */
974 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson432e58e2010-11-25 19:32:06 +0000975 }
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100976 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
John Harrison97b2a6a2014-11-24 18:49:26 +0000977 i915_gem_request_assign(&obj->last_fenced_req, req);
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100978 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
979 struct drm_i915_private *dev_priv = to_i915(ring->dev);
980 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
981 &dev_priv->mm.fence_list);
982 }
983 }
Chris Wilson432e58e2010-11-25 19:32:06 +0000984
Chris Wilsondb53a302011-02-03 11:57:46 +0000985 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000986 }
987}
988
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100989void
Chris Wilson54cf91d2010-11-25 18:00:26 +0000990i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000991 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100992 struct intel_engine_cs *ring,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300993 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000994{
Daniel Vettercc889e02012-06-13 20:45:19 +0200995 /* Unconditionally force add_request to emit a full flush. */
996 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000997
Chris Wilson432e58e2010-11-25 19:32:06 +0000998 /* Add a breadcrumb for the completion of the batch buffer */
John Harrison9400ae52014-11-24 18:49:36 +0000999 (void)__i915_add_request(ring, file, obj);
Chris Wilson432e58e2010-11-25 19:32:06 +00001000}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001001
1002static int
Eric Anholtae662d32012-01-03 09:23:29 -08001003i915_reset_gen7_sol_offsets(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001004 struct intel_engine_cs *ring)
Eric Anholtae662d32012-01-03 09:23:29 -08001005{
Jani Nikula50227e12014-03-31 14:27:21 +03001006 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtae662d32012-01-03 09:23:29 -08001007 int ret, i;
1008
Daniel Vetter9d662da2014-04-24 08:09:09 +02001009 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
1010 DRM_DEBUG("sol reset is gen7/rcs only\n");
1011 return -EINVAL;
1012 }
Eric Anholtae662d32012-01-03 09:23:29 -08001013
1014 ret = intel_ring_begin(ring, 4 * 3);
1015 if (ret)
1016 return ret;
1017
1018 for (i = 0; i < 4; i++) {
1019 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1020 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
1021 intel_ring_emit(ring, 0);
1022 }
1023
1024 intel_ring_advance(ring);
1025
1026 return 0;
1027}
1028
Chris Wilson5c6c6002014-09-06 10:28:27 +01001029static int
1030i915_emit_box(struct intel_engine_cs *ring,
1031 struct drm_clip_rect *box,
1032 int DR1, int DR4)
1033{
1034 int ret;
1035
1036 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
1037 box->y2 <= 0 || box->x2 <= 0) {
1038 DRM_ERROR("Bad box %d,%d..%d,%d\n",
1039 box->x1, box->y1, box->x2, box->y2);
1040 return -EINVAL;
1041 }
1042
1043 if (INTEL_INFO(ring->dev)->gen >= 4) {
1044 ret = intel_ring_begin(ring, 4);
1045 if (ret)
1046 return ret;
1047
1048 intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO_I965);
1049 intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
1050 intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
1051 intel_ring_emit(ring, DR4);
1052 } else {
1053 ret = intel_ring_begin(ring, 6);
1054 if (ret)
1055 return ret;
1056
1057 intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO);
1058 intel_ring_emit(ring, DR1);
1059 intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
1060 intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
1061 intel_ring_emit(ring, DR4);
1062 intel_ring_emit(ring, 0);
1063 }
1064 intel_ring_advance(ring);
1065
1066 return 0;
1067}
1068
1069
Oscar Mateoa83014d2014-07-24 17:04:21 +01001070int
1071i915_gem_ringbuffer_submission(struct drm_device *dev, struct drm_file *file,
1072 struct intel_engine_cs *ring,
1073 struct intel_context *ctx,
1074 struct drm_i915_gem_execbuffer2 *args,
1075 struct list_head *vmas,
1076 struct drm_i915_gem_object *batch_obj,
1077 u64 exec_start, u32 flags)
Oscar Mateo78382592014-07-03 16:28:05 +01001078{
1079 struct drm_clip_rect *cliprects = NULL;
1080 struct drm_i915_private *dev_priv = dev->dev_private;
1081 u64 exec_len;
1082 int instp_mode;
1083 u32 instp_mask;
1084 int i, ret = 0;
1085
1086 if (args->num_cliprects != 0) {
1087 if (ring != &dev_priv->ring[RCS]) {
1088 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
1089 return -EINVAL;
1090 }
1091
1092 if (INTEL_INFO(dev)->gen >= 5) {
1093 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1094 return -EINVAL;
1095 }
1096
1097 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1098 DRM_DEBUG("execbuf with %u cliprects\n",
1099 args->num_cliprects);
1100 return -EINVAL;
1101 }
1102
1103 cliprects = kcalloc(args->num_cliprects,
1104 sizeof(*cliprects),
1105 GFP_KERNEL);
1106 if (cliprects == NULL) {
1107 ret = -ENOMEM;
1108 goto error;
1109 }
1110
1111 if (copy_from_user(cliprects,
1112 to_user_ptr(args->cliprects_ptr),
1113 sizeof(*cliprects)*args->num_cliprects)) {
1114 ret = -EFAULT;
1115 goto error;
1116 }
1117 } else {
1118 if (args->DR4 == 0xffffffff) {
1119 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1120 args->DR4 = 0;
1121 }
1122
1123 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
1124 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
1125 return -EINVAL;
1126 }
1127 }
1128
1129 ret = i915_gem_execbuffer_move_to_gpu(ring, vmas);
1130 if (ret)
1131 goto error;
1132
1133 ret = i915_switch_context(ring, ctx);
1134 if (ret)
1135 goto error;
1136
1137 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1138 instp_mask = I915_EXEC_CONSTANTS_MASK;
1139 switch (instp_mode) {
1140 case I915_EXEC_CONSTANTS_REL_GENERAL:
1141 case I915_EXEC_CONSTANTS_ABSOLUTE:
1142 case I915_EXEC_CONSTANTS_REL_SURFACE:
1143 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
1144 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
1145 ret = -EINVAL;
1146 goto error;
1147 }
1148
1149 if (instp_mode != dev_priv->relative_constants_mode) {
1150 if (INTEL_INFO(dev)->gen < 4) {
1151 DRM_DEBUG("no rel constants on pre-gen4\n");
1152 ret = -EINVAL;
1153 goto error;
1154 }
1155
1156 if (INTEL_INFO(dev)->gen > 5 &&
1157 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1158 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
1159 ret = -EINVAL;
1160 goto error;
1161 }
1162
1163 /* The HW changed the meaning on this bit on gen6 */
1164 if (INTEL_INFO(dev)->gen >= 6)
1165 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1166 }
1167 break;
1168 default:
1169 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
1170 ret = -EINVAL;
1171 goto error;
1172 }
1173
1174 if (ring == &dev_priv->ring[RCS] &&
1175 instp_mode != dev_priv->relative_constants_mode) {
1176 ret = intel_ring_begin(ring, 4);
1177 if (ret)
1178 goto error;
1179
1180 intel_ring_emit(ring, MI_NOOP);
1181 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1182 intel_ring_emit(ring, INSTPM);
1183 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1184 intel_ring_advance(ring);
1185
1186 dev_priv->relative_constants_mode = instp_mode;
1187 }
1188
1189 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1190 ret = i915_reset_gen7_sol_offsets(dev, ring);
1191 if (ret)
1192 goto error;
1193 }
1194
1195 exec_len = args->batch_len;
1196 if (cliprects) {
1197 for (i = 0; i < args->num_cliprects; i++) {
Chris Wilson5c6c6002014-09-06 10:28:27 +01001198 ret = i915_emit_box(ring, &cliprects[i],
Oscar Mateo78382592014-07-03 16:28:05 +01001199 args->DR1, args->DR4);
1200 if (ret)
1201 goto error;
1202
1203 ret = ring->dispatch_execbuffer(ring,
1204 exec_start, exec_len,
1205 flags);
1206 if (ret)
1207 goto error;
1208 }
1209 } else {
1210 ret = ring->dispatch_execbuffer(ring,
1211 exec_start, exec_len,
1212 flags);
1213 if (ret)
1214 return ret;
1215 }
1216
John Harrison74328ee2014-11-24 18:49:38 +00001217 trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001218
1219 i915_gem_execbuffer_move_to_active(vmas, ring);
1220 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
1221
1222error:
1223 kfree(cliprects);
1224 return ret;
1225}
1226
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001227/**
1228 * Find one BSD ring to dispatch the corresponding BSD command.
1229 * The Ring ID is returned.
1230 */
1231static int gen8_dispatch_bsd_ring(struct drm_device *dev,
1232 struct drm_file *file)
1233{
1234 struct drm_i915_private *dev_priv = dev->dev_private;
1235 struct drm_i915_file_private *file_priv = file->driver_priv;
1236
1237 /* Check whether the file_priv is using one ring */
1238 if (file_priv->bsd_ring)
1239 return file_priv->bsd_ring->id;
1240 else {
1241 /* If no, use the ping-pong mechanism to select one ring */
1242 int ring_id;
1243
1244 mutex_lock(&dev->struct_mutex);
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001245 if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001246 ring_id = VCS;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001247 dev_priv->mm.bsd_ring_dispatch_index = 1;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001248 } else {
1249 ring_id = VCS2;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001250 dev_priv->mm.bsd_ring_dispatch_index = 0;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001251 }
1252 file_priv->bsd_ring = &dev_priv->ring[ring_id];
1253 mutex_unlock(&dev->struct_mutex);
1254 return ring_id;
1255 }
1256}
1257
Chris Wilsond23db882014-05-23 08:48:08 +02001258static struct drm_i915_gem_object *
1259eb_get_batch(struct eb_vmas *eb)
1260{
1261 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
1262
1263 /*
1264 * SNA is doing fancy tricks with compressing batch buffers, which leads
1265 * to negative relocation deltas. Usually that works out ok since the
1266 * relocate address is still positive, except when the batch is placed
1267 * very low in the GTT. Ensure this doesn't happen.
1268 *
1269 * Note that actual hangs have only been observed on gen7, but for
1270 * paranoia do it everywhere.
1271 */
1272 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
1273
1274 return vma->obj;
1275}
1276
Eric Anholtae662d32012-01-03 09:23:29 -08001277static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001278i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1279 struct drm_file *file,
1280 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001281 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001282{
Jani Nikula50227e12014-03-31 14:27:21 +03001283 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +02001284 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001285 struct drm_i915_gem_object *batch_obj;
Brad Volkin78a42372014-12-11 12:13:09 -08001286 struct drm_i915_gem_object *shadow_batch_obj = NULL;
1287 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001288 struct intel_engine_cs *ring;
Oscar Mateo273497e2014-05-22 14:13:37 +01001289 struct intel_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001290 struct i915_address_space *vm;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001291 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Oscar Mateo78382592014-07-03 16:28:05 +01001292 u64 exec_start = args->batch_start_offset;
1293 u32 flags;
1294 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001295 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001296
Daniel Vettered5982e2013-01-17 22:23:36 +01001297 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001298 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001299
Chris Wilsonad19f102014-08-10 06:29:08 +01001300 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001301 if (ret)
1302 return ret;
1303
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001304 flags = 0;
1305 if (args->flags & I915_EXEC_SECURE) {
1306 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1307 return -EPERM;
1308
1309 flags |= I915_DISPATCH_SECURE;
1310 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001311 if (args->flags & I915_EXEC_IS_PINNED)
1312 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001313
Zhao Yakuib1a93302014-04-17 10:37:36 +08001314 if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
Daniel Vetterff240192012-01-31 21:08:14 +01001315 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001316 (int)(args->flags & I915_EXEC_RING_MASK));
1317 return -EINVAL;
1318 }
Ben Widawskyca01b122013-12-06 14:11:00 -08001319
1320 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1321 ring = &dev_priv->ring[RCS];
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001322 else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
1323 if (HAS_BSD2(dev)) {
1324 int ring_id;
1325 ring_id = gen8_dispatch_bsd_ring(dev, file);
1326 ring = &dev_priv->ring[ring_id];
1327 } else
1328 ring = &dev_priv->ring[VCS];
1329 } else
Ben Widawskyca01b122013-12-06 14:11:00 -08001330 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1331
Chris Wilsona15817c2012-05-11 14:29:31 +01001332 if (!intel_ring_initialized(ring)) {
1333 DRM_DEBUG("execbuf with invalid ring: %d\n",
1334 (int)(args->flags & I915_EXEC_RING_MASK));
1335 return -EINVAL;
1336 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001337
1338 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001339 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001340 return -EINVAL;
1341 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001342
Paulo Zanonif65c9162013-11-27 18:20:34 -02001343 intel_runtime_pm_get(dev_priv);
1344
Chris Wilson54cf91d2010-11-25 18:00:26 +00001345 ret = i915_mutex_lock_interruptible(dev);
1346 if (ret)
1347 goto pre_mutex_err;
1348
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001349 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001350 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001351 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001352 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001353 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001354 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001355
1356 i915_gem_context_reference(ctx);
1357
Daniel Vetterae6c4802014-08-06 15:04:53 +02001358 if (ctx->ppgtt)
1359 vm = &ctx->ppgtt->base;
1360 else
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001361 vm = &dev_priv->gtt.base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001362
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001363 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001364 if (eb == NULL) {
Ben Widawsky935f38d2014-04-04 22:41:07 -07001365 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001366 mutex_unlock(&dev->struct_mutex);
1367 ret = -ENOMEM;
1368 goto pre_mutex_err;
1369 }
1370
Chris Wilson54cf91d2010-11-25 18:00:26 +00001371 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001372 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001373 if (ret)
1374 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001375
Chris Wilson6fe4f142011-01-10 17:35:37 +00001376 /* take note of the batch buffer before we might reorder the lists */
Chris Wilsond23db882014-05-23 08:48:08 +02001377 batch_obj = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001378
Chris Wilson54cf91d2010-11-25 18:00:26 +00001379 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001380 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001381 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001382 if (ret)
1383 goto err;
1384
1385 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001386 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001387 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001388 if (ret) {
1389 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001390 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001391 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001392 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1393 }
1394 if (ret)
1395 goto err;
1396 }
1397
1398 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001399 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001400 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001401 ret = -EINVAL;
1402 goto err;
1403 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001404
Brad Volkin351e3db2014-02-18 10:15:46 -08001405 if (i915_needs_cmd_parser(ring)) {
Brad Volkin78a42372014-12-11 12:13:09 -08001406 shadow_batch_obj =
1407 i915_gem_batch_pool_get(&dev_priv->mm.batch_pool,
1408 batch_obj->base.size);
1409 if (IS_ERR(shadow_batch_obj)) {
1410 ret = PTR_ERR(shadow_batch_obj);
1411 /* Don't try to clean up the obj in the error path */
1412 shadow_batch_obj = NULL;
1413 goto err;
1414 }
1415
1416 ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 4096, 0);
1417 if (ret)
1418 goto err;
1419
Brad Volkin351e3db2014-02-18 10:15:46 -08001420 ret = i915_parse_cmds(ring,
1421 batch_obj,
Brad Volkin78a42372014-12-11 12:13:09 -08001422 shadow_batch_obj,
Brad Volkin351e3db2014-02-18 10:15:46 -08001423 args->batch_start_offset,
Brad Volkinb9ffd802014-12-11 12:13:10 -08001424 args->batch_len,
Brad Volkin351e3db2014-02-18 10:15:46 -08001425 file->is_master);
Brad Volkin78a42372014-12-11 12:13:09 -08001426 i915_gem_object_ggtt_unpin(shadow_batch_obj);
1427
Brad Volkin42c71562014-10-16 12:24:42 -07001428 if (ret) {
1429 if (ret != -EACCES)
1430 goto err;
1431 } else {
Brad Volkin78a42372014-12-11 12:13:09 -08001432 struct i915_vma *vma;
1433
1434 memset(&shadow_exec_entry, 0,
1435 sizeof(shadow_exec_entry));
1436
1437 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1438 vma->exec_entry = &shadow_exec_entry;
1439 drm_gem_object_reference(&shadow_batch_obj->base);
1440 list_add_tail(&vma->exec_list, &eb->vmas);
1441
1442 shadow_batch_obj->base.pending_read_domains =
1443 batch_obj->base.pending_read_domains;
1444
1445 batch_obj = shadow_batch_obj;
1446
Brad Volkin42c71562014-10-16 12:24:42 -07001447 /*
Brad Volkin78a42372014-12-11 12:13:09 -08001448 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1449 * bit from MI_BATCH_BUFFER_START commands issued in the
1450 * dispatch_execbuffer implementations. We specifically
1451 * don't want that set when the command parser is
1452 * enabled.
Brad Volkin42c71562014-10-16 12:24:42 -07001453 *
Brad Volkin78a42372014-12-11 12:13:09 -08001454 * FIXME: with aliasing ppgtt, buffers that should only
1455 * be in ggtt still end up in the aliasing ppgtt. remove
1456 * this check when that is fixed.
Brad Volkin42c71562014-10-16 12:24:42 -07001457 */
Brad Volkin78a42372014-12-11 12:13:09 -08001458 if (USES_FULL_PPGTT(dev))
1459 flags |= I915_DISPATCH_SECURE;
Brad Volkin42c71562014-10-16 12:24:42 -07001460 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001461 }
1462
Brad Volkin78a42372014-12-11 12:13:09 -08001463 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1464
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001465 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1466 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001467 * hsw should have this fixed, but bdw mucks it up again. */
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001468 if (flags & I915_DISPATCH_SECURE) {
1469 /*
1470 * So on first glance it looks freaky that we pin the batch here
1471 * outside of the reservation loop. But:
1472 * - The batch is already pinned into the relevant ppgtt, so we
1473 * already have the backing storage fully allocated.
1474 * - No other BO uses the global gtt (well contexts, but meh),
1475 * so we don't really have issues with mutliple objects not
1476 * fitting due to fragmentation.
1477 * So this is actually safe.
1478 */
1479 ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
1480 if (ret)
1481 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001482
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001483 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001484 } else
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001485 exec_start += i915_gem_obj_offset(batch_obj, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001486
Oscar Mateoa83014d2014-07-24 17:04:21 +01001487 ret = dev_priv->gt.do_execbuf(dev, file, ring, ctx, args,
1488 &eb->vmas, batch_obj, exec_start, flags);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001489
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001490 /*
1491 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1492 * batch vma for correctness. For less ugly and less fragility this
1493 * needs to be adjusted to also track the ggtt batch vma properly as
1494 * active.
1495 */
1496 if (flags & I915_DISPATCH_SECURE)
1497 i915_gem_object_ggtt_unpin(batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001498err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001499 /* the request owns the ref now */
1500 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001501 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001502
1503 mutex_unlock(&dev->struct_mutex);
1504
1505pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001506 /* intel_gpu_busy should also get a ref, so it will free when the device
1507 * is really idle. */
1508 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001509 return ret;
1510}
1511
1512/*
1513 * Legacy execbuffer just creates an exec2 list from the original exec object
1514 * list array and passes it to the real function.
1515 */
1516int
1517i915_gem_execbuffer(struct drm_device *dev, void *data,
1518 struct drm_file *file)
1519{
1520 struct drm_i915_gem_execbuffer *args = data;
1521 struct drm_i915_gem_execbuffer2 exec2;
1522 struct drm_i915_gem_exec_object *exec_list = NULL;
1523 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1524 int ret, i;
1525
Chris Wilson54cf91d2010-11-25 18:00:26 +00001526 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001527 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001528 return -EINVAL;
1529 }
1530
1531 /* Copy in the exec list from userland */
1532 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1533 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1534 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001535 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001536 args->buffer_count);
1537 drm_free_large(exec_list);
1538 drm_free_large(exec2_list);
1539 return -ENOMEM;
1540 }
1541 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001542 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001543 sizeof(*exec_list) * args->buffer_count);
1544 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001545 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001546 args->buffer_count, ret);
1547 drm_free_large(exec_list);
1548 drm_free_large(exec2_list);
1549 return -EFAULT;
1550 }
1551
1552 for (i = 0; i < args->buffer_count; i++) {
1553 exec2_list[i].handle = exec_list[i].handle;
1554 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1555 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1556 exec2_list[i].alignment = exec_list[i].alignment;
1557 exec2_list[i].offset = exec_list[i].offset;
1558 if (INTEL_INFO(dev)->gen < 4)
1559 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1560 else
1561 exec2_list[i].flags = 0;
1562 }
1563
1564 exec2.buffers_ptr = args->buffers_ptr;
1565 exec2.buffer_count = args->buffer_count;
1566 exec2.batch_start_offset = args->batch_start_offset;
1567 exec2.batch_len = args->batch_len;
1568 exec2.DR1 = args->DR1;
1569 exec2.DR4 = args->DR4;
1570 exec2.num_cliprects = args->num_cliprects;
1571 exec2.cliprects_ptr = args->cliprects_ptr;
1572 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001573 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001574
Ben Widawsky41bde552013-12-06 14:11:21 -08001575 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001576 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001577 struct drm_i915_gem_exec_object __user *user_exec_list =
1578 to_user_ptr(args->buffers_ptr);
1579
Chris Wilson54cf91d2010-11-25 18:00:26 +00001580 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001581 for (i = 0; i < args->buffer_count; i++) {
1582 ret = __copy_to_user(&user_exec_list[i].offset,
1583 &exec2_list[i].offset,
1584 sizeof(user_exec_list[i].offset));
1585 if (ret) {
1586 ret = -EFAULT;
1587 DRM_DEBUG("failed to copy %d exec entries "
1588 "back to user (%d)\n",
1589 args->buffer_count, ret);
1590 break;
1591 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001592 }
1593 }
1594
1595 drm_free_large(exec_list);
1596 drm_free_large(exec2_list);
1597 return ret;
1598}
1599
1600int
1601i915_gem_execbuffer2(struct drm_device *dev, void *data,
1602 struct drm_file *file)
1603{
1604 struct drm_i915_gem_execbuffer2 *args = data;
1605 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1606 int ret;
1607
Xi Wanged8cd3b2012-04-23 04:06:41 -04001608 if (args->buffer_count < 1 ||
1609 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001610 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001611 return -EINVAL;
1612 }
1613
Daniel Vetter9cb34662014-04-24 08:09:11 +02001614 if (args->rsvd2 != 0) {
1615 DRM_DEBUG("dirty rvsd2 field\n");
1616 return -EINVAL;
1617 }
1618
Chris Wilson8408c282011-02-21 12:54:48 +00001619 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001620 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001621 if (exec2_list == NULL)
1622 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1623 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001624 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001625 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001626 args->buffer_count);
1627 return -ENOMEM;
1628 }
1629 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001630 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001631 sizeof(*exec2_list) * args->buffer_count);
1632 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001633 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001634 args->buffer_count, ret);
1635 drm_free_large(exec2_list);
1636 return -EFAULT;
1637 }
1638
Ben Widawsky41bde552013-12-06 14:11:21 -08001639 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001640 if (!ret) {
1641 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001642 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001643 to_user_ptr(args->buffers_ptr);
1644 int i;
1645
1646 for (i = 0; i < args->buffer_count; i++) {
1647 ret = __copy_to_user(&user_exec_list[i].offset,
1648 &exec2_list[i].offset,
1649 sizeof(user_exec_list[i].offset));
1650 if (ret) {
1651 ret = -EFAULT;
1652 DRM_DEBUG("failed to copy %d exec entries "
1653 "back to user\n",
1654 args->buffer_count);
1655 break;
1656 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001657 }
1658 }
1659
1660 drm_free_large(exec2_list);
1661 return ret;
1662}