blob: 7f69aa820458bb2c94e6979c966820edfc0bce46 [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
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
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000133 obj = list_first_entry(&objects,
134 struct drm_i915_gem_object,
135 obj_exec_link);
136
Daniel Vettere656a6c2013-08-14 14:14:04 +0200137 /*
138 * NOTE: We can leak any vmas created here when something fails
139 * later on. But that's no issue since vma_unbind can deal with
140 * vmas which are not actually bound. And since only
141 * lookup_or_create exists as an interface to get at the vma
142 * from the (obj, vm) we don't run the risk of creating
143 * duplicated vmas for the same vm.
144 */
Daniel Vetterda51a1e2014-08-11 12:08:58 +0200145 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200146 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200147 DRM_DEBUG("Failed to lookup VMA\n");
148 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000149 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200150 }
151
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000152 /* Transfer ownership from the objects list to the vmas list. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200153 list_add_tail(&vma->exec_list, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000154 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200155
156 vma->exec_entry = &exec[i];
157 if (eb->and < 0) {
158 eb->lut[i] = vma;
159 } else {
160 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
161 vma->exec_handle = handle;
162 hlist_add_head(&vma->exec_node,
163 &eb->buckets[handle & eb->and]);
164 }
165 ++i;
166 }
167
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000168 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200169
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000170
171err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200172 while (!list_empty(&objects)) {
173 obj = list_first_entry(&objects,
174 struct drm_i915_gem_object,
175 obj_exec_link);
176 list_del_init(&obj->obj_exec_link);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000177 drm_gem_object_unreference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200178 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000179 /*
180 * Objects already transfered to the vmas list will be unreferenced by
181 * eb_destroy.
182 */
183
Ben Widawsky27173f12013-08-14 11:38:36 +0200184 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000185}
186
Ben Widawsky27173f12013-08-14 11:38:36 +0200187static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000188{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000189 if (eb->and < 0) {
190 if (handle >= -eb->and)
191 return NULL;
192 return eb->lut[handle];
193 } else {
194 struct hlist_head *head;
195 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000196
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000197 head = &eb->buckets[handle & eb->and];
198 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200199 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000200
Ben Widawsky27173f12013-08-14 11:38:36 +0200201 vma = hlist_entry(node, struct i915_vma, exec_node);
202 if (vma->exec_handle == handle)
203 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000204 }
205 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000206 }
Chris Wilson67731b82010-12-08 10:38:14 +0000207}
208
Chris Wilsona415d352013-11-26 11:23:15 +0000209static void
210i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
211{
212 struct drm_i915_gem_exec_object2 *entry;
213 struct drm_i915_gem_object *obj = vma->obj;
214
215 if (!drm_mm_node_allocated(&vma->node))
216 return;
217
218 entry = vma->exec_entry;
219
220 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
221 i915_gem_object_unpin_fence(obj);
222
223 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Daniel Vetter3d7f0f92013-12-18 16:23:37 +0100224 vma->pin_count--;
Chris Wilsona415d352013-11-26 11:23:15 +0000225
Chris Wilsonde4e7832015-04-07 16:20:35 +0100226 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilsona415d352013-11-26 11:23:15 +0000227}
228
229static void eb_destroy(struct eb_vmas *eb)
230{
Ben Widawsky27173f12013-08-14 11:38:36 +0200231 while (!list_empty(&eb->vmas)) {
232 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000233
Ben Widawsky27173f12013-08-14 11:38:36 +0200234 vma = list_first_entry(&eb->vmas,
235 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000236 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200237 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000238 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200239 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000240 }
Chris Wilson67731b82010-12-08 10:38:14 +0000241 kfree(eb);
242}
243
Chris Wilsondabdfe02012-03-26 10:10:27 +0200244static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
245{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300246 return (HAS_LLC(obj->base.dev) ||
247 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200248 obj->cache_level != I915_CACHE_NONE);
249}
250
Chris Wilson54cf91d2010-11-25 18:00:26 +0000251static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100252relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700253 struct drm_i915_gem_relocation_entry *reloc,
254 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100255{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700256 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100257 uint32_t page_offset = offset_in_page(reloc->offset);
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700258 uint64_t delta = reloc->delta + target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100259 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800260 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100261
Chris Wilson2cc86b82013-08-26 19:51:00 -0300262 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100263 if (ret)
264 return ret;
265
266 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
267 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700268 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700269
270 if (INTEL_INFO(dev)->gen >= 8) {
271 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
272
273 if (page_offset == 0) {
274 kunmap_atomic(vaddr);
275 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
276 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
277 }
278
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700279 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700280 }
281
Rafael Barbalho5032d872013-08-21 17:10:51 +0100282 kunmap_atomic(vaddr);
283
284 return 0;
285}
286
287static int
288relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700289 struct drm_i915_gem_relocation_entry *reloc,
290 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100291{
292 struct drm_device *dev = obj->base.dev;
293 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700294 uint64_t delta = reloc->delta + target_offset;
Chris Wilson906843c2014-08-10 06:29:11 +0100295 uint64_t offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100296 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800297 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100298
299 ret = i915_gem_object_set_to_gtt_domain(obj, true);
300 if (ret)
301 return ret;
302
303 ret = i915_gem_object_put_fence(obj);
304 if (ret)
305 return ret;
306
307 /* Map the page containing the relocation we're going to perform. */
Chris Wilson906843c2014-08-10 06:29:11 +0100308 offset = i915_gem_obj_ggtt_offset(obj);
309 offset += reloc->offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100310 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100311 offset & PAGE_MASK);
312 iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700313
314 if (INTEL_INFO(dev)->gen >= 8) {
Chris Wilson906843c2014-08-10 06:29:11 +0100315 offset += sizeof(uint32_t);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700316
Chris Wilson906843c2014-08-10 06:29:11 +0100317 if (offset_in_page(offset) == 0) {
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700318 io_mapping_unmap_atomic(reloc_page);
Chris Wilson906843c2014-08-10 06:29:11 +0100319 reloc_page =
320 io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
321 offset);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700322 }
323
Chris Wilson906843c2014-08-10 06:29:11 +0100324 iowrite32(upper_32_bits(delta),
325 reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700326 }
327
Rafael Barbalho5032d872013-08-21 17:10:51 +0100328 io_mapping_unmap_atomic(reloc_page);
329
330 return 0;
331}
332
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000333static void
334clflush_write32(void *addr, uint32_t value)
335{
336 /* This is not a fast path, so KISS. */
337 drm_clflush_virt_range(addr, sizeof(uint32_t));
338 *(uint32_t *)addr = value;
339 drm_clflush_virt_range(addr, sizeof(uint32_t));
340}
341
342static int
343relocate_entry_clflush(struct drm_i915_gem_object *obj,
344 struct drm_i915_gem_relocation_entry *reloc,
345 uint64_t target_offset)
346{
347 struct drm_device *dev = obj->base.dev;
348 uint32_t page_offset = offset_in_page(reloc->offset);
349 uint64_t delta = (int)reloc->delta + target_offset;
350 char *vaddr;
351 int ret;
352
353 ret = i915_gem_object_set_to_gtt_domain(obj, true);
354 if (ret)
355 return ret;
356
357 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
358 reloc->offset >> PAGE_SHIFT));
359 clflush_write32(vaddr + page_offset, lower_32_bits(delta));
360
361 if (INTEL_INFO(dev)->gen >= 8) {
362 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
363
364 if (page_offset == 0) {
365 kunmap_atomic(vaddr);
366 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
367 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
368 }
369
370 clflush_write32(vaddr + page_offset, upper_32_bits(delta));
371 }
372
373 kunmap_atomic(vaddr);
374
375 return 0;
376}
377
Rafael Barbalho5032d872013-08-21 17:10:51 +0100378static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000379i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200380 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800381 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000382{
383 struct drm_device *dev = obj->base.dev;
384 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100385 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200386 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700387 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800388 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000389
Chris Wilson67731b82010-12-08 10:38:14 +0000390 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200391 target_vma = eb_get_vma(eb, reloc->target_handle);
392 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000393 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200394 target_i915_obj = target_vma->obj;
395 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000396
Ben Widawsky5ce09722013-11-25 09:54:40 -0800397 target_offset = target_vma->node.start;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000398
Eric Anholte844b992012-07-31 15:35:01 -0700399 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
400 * pipe_control writes because the gpu doesn't properly redirect them
401 * through the ppgtt for non_secure batchbuffers. */
402 if (unlikely(IS_GEN6(dev) &&
403 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000404 !(target_vma->bound & GLOBAL_BIND))) {
405 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
406 GLOBAL_BIND);
407 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
408 return ret;
409 }
Eric Anholte844b992012-07-31 15:35:01 -0700410
Chris Wilson54cf91d2010-11-25 18:00:26 +0000411 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000412 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100413 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000414 "obj %p target %d offset %d "
415 "read %08x write %08x",
416 obj, reloc->target_handle,
417 (int) reloc->offset,
418 reloc->read_domains,
419 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800420 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000421 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100422 if (unlikely((reloc->write_domain | reloc->read_domains)
423 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100424 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000425 "obj %p target %d offset %d "
426 "read %08x write %08x",
427 obj, reloc->target_handle,
428 (int) reloc->offset,
429 reloc->read_domains,
430 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800431 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000432 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000433
434 target_obj->pending_read_domains |= reloc->read_domains;
435 target_obj->pending_write_domain |= reloc->write_domain;
436
437 /* If the relocation already has the right value in it, no
438 * more work needs to be done.
439 */
440 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000441 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000442
443 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700444 if (unlikely(reloc->offset >
445 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100446 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000447 "obj %p target %d offset %d size %d.\n",
448 obj, reloc->target_handle,
449 (int) reloc->offset,
450 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800451 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000452 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000453 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100454 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000455 "obj %p target %d offset %d.\n",
456 obj, reloc->target_handle,
457 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800458 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000459 }
460
Chris Wilsondabdfe02012-03-26 10:10:27 +0200461 /* We can't wait for rendering with pagefaults disabled */
462 if (obj->active && in_atomic())
463 return -EFAULT;
464
Rafael Barbalho5032d872013-08-21 17:10:51 +0100465 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700466 ret = relocate_entry_cpu(obj, reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000467 else if (obj->map_and_fenceable)
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700468 ret = relocate_entry_gtt(obj, reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000469 else if (cpu_has_clflush)
470 ret = relocate_entry_clflush(obj, reloc, target_offset);
471 else {
472 WARN_ONCE(1, "Impossible case in relocation handling\n");
473 ret = -ENODEV;
474 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000475
Daniel Vetterd4d36012013-09-02 20:56:23 +0200476 if (ret)
477 return ret;
478
Chris Wilson54cf91d2010-11-25 18:00:26 +0000479 /* and update the user's relocation entry */
480 reloc->presumed_offset = target_offset;
481
Chris Wilson67731b82010-12-08 10:38:14 +0000482 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000483}
484
485static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200486i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
487 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000488{
Chris Wilson1d83f442012-03-24 20:12:53 +0000489#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
490 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000491 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200492 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000493 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000494
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200495 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000496
Chris Wilson1d83f442012-03-24 20:12:53 +0000497 remain = entry->relocation_count;
498 while (remain) {
499 struct drm_i915_gem_relocation_entry *r = stack_reloc;
500 int count = remain;
501 if (count > ARRAY_SIZE(stack_reloc))
502 count = ARRAY_SIZE(stack_reloc);
503 remain -= count;
504
505 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000506 return -EFAULT;
507
Chris Wilson1d83f442012-03-24 20:12:53 +0000508 do {
509 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000510
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800511 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000512 if (ret)
513 return ret;
514
515 if (r->presumed_offset != offset &&
516 __copy_to_user_inatomic(&user_relocs->presumed_offset,
517 &r->presumed_offset,
518 sizeof(r->presumed_offset))) {
519 return -EFAULT;
520 }
521
522 user_relocs++;
523 r++;
524 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000525 }
526
527 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000528#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000529}
530
531static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200532i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
533 struct eb_vmas *eb,
534 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000535{
Ben Widawsky27173f12013-08-14 11:38:36 +0200536 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000537 int i, ret;
538
539 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800540 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000541 if (ret)
542 return ret;
543 }
544
545 return 0;
546}
547
548static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800549i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000550{
Ben Widawsky27173f12013-08-14 11:38:36 +0200551 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000552 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000553
Chris Wilsond4aeee72011-03-14 15:11:24 +0000554 /* This is the fast path and we cannot handle a pagefault whilst
555 * holding the struct mutex lest the user pass in the relocations
556 * contained within a mmaped bo. For in such a case we, the page
557 * fault handler would call i915_gem_fault() and we would try to
558 * acquire the struct mutex again. Obviously this is bad and so
559 * lockdep complains vehemently.
560 */
561 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200562 list_for_each_entry(vma, &eb->vmas, exec_list) {
563 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000564 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000565 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000566 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000567 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000568
Chris Wilsond4aeee72011-03-14 15:11:24 +0000569 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000570}
571
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000572static bool only_mappable_for_reloc(unsigned int flags)
573{
574 return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
575 __EXEC_OBJECT_NEEDS_MAP;
576}
577
Chris Wilson1690e1e2011-12-14 13:57:08 +0100578static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200579i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100580 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200581 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100582{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800583 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200584 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200585 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100586 int ret;
587
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100588 flags = 0;
Daniel Vetter0229da32015-04-14 19:01:54 +0200589 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
590 flags |= PIN_GLOBAL;
591
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000592 if (!drm_mm_node_allocated(&vma->node)) {
593 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
594 flags |= PIN_GLOBAL | PIN_MAPPABLE;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000595 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
596 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
597 }
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100598
599 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000600 if ((ret == -ENOSPC || ret == -E2BIG) &&
601 only_mappable_for_reloc(entry->flags))
602 ret = i915_gem_object_pin(obj, vma->vm,
603 entry->alignment,
Daniel Vetter0229da32015-04-14 19:01:54 +0200604 flags & ~PIN_MAPPABLE);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100605 if (ret)
606 return ret;
607
Chris Wilson7788a762012-08-24 19:18:18 +0100608 entry->flags |= __EXEC_OBJECT_HAS_PIN;
609
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100610 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
611 ret = i915_gem_object_get_fence(obj);
612 if (ret)
613 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100614
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100615 if (i915_gem_object_pin_fence(obj))
616 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100617 }
618
Ben Widawsky27173f12013-08-14 11:38:36 +0200619 if (entry->offset != vma->node.start) {
620 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100621 *need_reloc = true;
622 }
623
624 if (entry->flags & EXEC_OBJECT_WRITE) {
625 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
626 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
627 }
628
Chris Wilson1690e1e2011-12-14 13:57:08 +0100629 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100630}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100631
Chris Wilsond23db882014-05-23 08:48:08 +0200632static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200633need_reloc_mappable(struct i915_vma *vma)
634{
635 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
636
637 if (entry->relocation_count == 0)
638 return false;
639
640 if (!i915_is_ggtt(vma->vm))
641 return false;
642
643 /* See also use_cpu_reloc() */
644 if (HAS_LLC(vma->obj->base.dev))
645 return false;
646
647 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
648 return false;
649
650 return true;
651}
652
653static bool
654eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200655{
656 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
657 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond23db882014-05-23 08:48:08 +0200658
Chris Wilsone6a84462014-08-11 12:00:12 +0200659 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
Chris Wilsond23db882014-05-23 08:48:08 +0200660 !i915_is_ggtt(vma->vm));
661
662 if (entry->alignment &&
663 vma->node.start & (entry->alignment - 1))
664 return true;
665
Chris Wilsond23db882014-05-23 08:48:08 +0200666 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
667 vma->node.start < BATCH_OFFSET_BIAS)
668 return true;
669
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000670 /* avoid costly ping-pong once a batch bo ended up non-mappable */
671 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
672 return !only_mappable_for_reloc(entry->flags);
673
Chris Wilsond23db882014-05-23 08:48:08 +0200674 return false;
675}
676
Chris Wilson54cf91d2010-11-25 18:00:26 +0000677static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100678i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200679 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100680 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000681{
Chris Wilson432e58e2010-11-25 19:32:06 +0000682 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200683 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700684 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200685 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100686 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
687 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000688
Chris Wilson227f7822014-05-15 10:41:42 +0100689 i915_gem_retire_requests_ring(ring);
690
Ben Widawsky68c8c172013-09-11 14:57:50 -0700691 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
692
Ben Widawsky27173f12013-08-14 11:38:36 +0200693 INIT_LIST_HEAD(&ordered_vmas);
694 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000695 struct drm_i915_gem_exec_object2 *entry;
696 bool need_fence, need_mappable;
697
Ben Widawsky27173f12013-08-14 11:38:36 +0200698 vma = list_first_entry(vmas, struct i915_vma, exec_list);
699 obj = vma->obj;
700 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000701
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100702 if (!has_fenced_gpu_access)
703 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000704 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000705 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
706 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200707 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000708
Chris Wilsone6a84462014-08-11 12:00:12 +0200709 if (need_mappable) {
710 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200711 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200712 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200713 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000714
Daniel Vettered5982e2013-01-17 22:23:36 +0100715 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000716 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000717 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200718 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000719
720 /* Attempt to pin all of the buffers into the GTT.
721 * This is done in 3 phases:
722 *
723 * 1a. Unbind all objects that do not match the GTT constraints for
724 * the execbuffer (fenceable, mappable, alignment etc).
725 * 1b. Increment pin count for already bound objects.
726 * 2. Bind new objects.
727 * 3. Decrement pin count.
728 *
Chris Wilson7788a762012-08-24 19:18:18 +0100729 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000730 * room for the earlier objects *unless* we need to defragment.
731 */
732 retry = 0;
733 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100734 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000735
736 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200737 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200738 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000739 continue;
740
Chris Wilsone6a84462014-08-11 12:00:12 +0200741 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200742 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000743 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200744 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000745 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000746 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000747 }
748
749 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200750 list_for_each_entry(vma, vmas, exec_list) {
751 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100752 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000753
Ben Widawsky27173f12013-08-14 11:38:36 +0200754 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100755 if (ret)
756 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000757 }
758
Chris Wilsona415d352013-11-26 11:23:15 +0000759err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200760 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000761 return ret;
762
Chris Wilsona415d352013-11-26 11:23:15 +0000763 /* Decrement pin count for bound objects */
764 list_for_each_entry(vma, vmas, exec_list)
765 i915_gem_execbuffer_unreserve_vma(vma);
766
Ben Widawsky68c8c172013-09-11 14:57:50 -0700767 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000768 if (ret)
769 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000770 } while (1);
771}
772
773static int
774i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100775 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000776 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100777 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200778 struct eb_vmas *eb,
779 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000780{
781 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200782 struct i915_address_space *vm;
783 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100784 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000785 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000786 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200787 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000788
Ben Widawsky27173f12013-08-14 11:38:36 +0200789 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
790
Chris Wilson67731b82010-12-08 10:38:14 +0000791 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200792 while (!list_empty(&eb->vmas)) {
793 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
794 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000795 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200796 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000797 }
798
Chris Wilson54cf91d2010-11-25 18:00:26 +0000799 mutex_unlock(&dev->struct_mutex);
800
801 total = 0;
802 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000803 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000804
Chris Wilsondd6864a2011-01-12 23:49:13 +0000805 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000806 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000807 if (reloc == NULL || reloc_offset == NULL) {
808 drm_free_large(reloc);
809 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000810 mutex_lock(&dev->struct_mutex);
811 return -ENOMEM;
812 }
813
814 total = 0;
815 for (i = 0; i < count; i++) {
816 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000817 u64 invalid_offset = (u64)-1;
818 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000819
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200820 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000821
822 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000823 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000824 ret = -EFAULT;
825 mutex_lock(&dev->struct_mutex);
826 goto err;
827 }
828
Chris Wilson262b6d32013-01-15 16:17:54 +0000829 /* As we do not update the known relocation offsets after
830 * relocating (due to the complexities in lock handling),
831 * we need to mark them as invalid now so that we force the
832 * relocation processing next time. Just in case the target
833 * object is evicted and then rebound into its old
834 * presumed_offset before the next execbuffer - if that
835 * happened we would make the mistake of assuming that the
836 * relocations were valid.
837 */
838 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100839 if (__copy_to_user(&user_relocs[j].presumed_offset,
840 &invalid_offset,
841 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000842 ret = -EFAULT;
843 mutex_lock(&dev->struct_mutex);
844 goto err;
845 }
846 }
847
Chris Wilsondd6864a2011-01-12 23:49:13 +0000848 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000849 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000850 }
851
852 ret = i915_mutex_lock_interruptible(dev);
853 if (ret) {
854 mutex_lock(&dev->struct_mutex);
855 goto err;
856 }
857
Chris Wilson67731b82010-12-08 10:38:14 +0000858 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000859 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200860 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000861 if (ret)
862 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000863
Daniel Vettered5982e2013-01-17 22:23:36 +0100864 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200865 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000866 if (ret)
867 goto err;
868
Ben Widawsky27173f12013-08-14 11:38:36 +0200869 list_for_each_entry(vma, &eb->vmas, exec_list) {
870 int offset = vma->exec_entry - exec;
871 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
872 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000873 if (ret)
874 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000875 }
876
877 /* Leave the user relocations as are, this is the painfully slow path,
878 * and we want to avoid the complication of dropping the lock whilst
879 * having buffers reserved in the aperture and so causing spurious
880 * ENOSPC for random operations.
881 */
882
883err:
884 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000885 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000886 return ret;
887}
888
Chris Wilson54cf91d2010-11-25 18:00:26 +0000889static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100890i915_gem_execbuffer_move_to_gpu(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200891 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000892{
Ben Widawsky27173f12013-08-14 11:38:36 +0200893 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200894 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100895 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000896 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000897
Ben Widawsky27173f12013-08-14 11:38:36 +0200898 list_for_each_entry(vma, vmas, exec_list) {
899 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700900 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000901 if (ret)
902 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200903
904 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100905 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200906
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200907 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000908 }
909
Chris Wilson000433b2013-08-08 14:41:09 +0100910 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800911 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200912
913 if (flush_domains & I915_GEM_DOMAIN_GTT)
914 wmb();
915
Chris Wilson09cf7c92012-07-13 14:14:08 +0100916 /* Unconditionally invalidate gpu caches and ensure that we do flush
917 * any residual writes from the previous batch.
918 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100919 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000920}
921
Chris Wilson432e58e2010-11-25 19:32:06 +0000922static bool
923i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000924{
Daniel Vettered5982e2013-01-17 22:23:36 +0100925 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
926 return false;
927
Chris Wilson432e58e2010-11-25 19:32:06 +0000928 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000929}
930
931static int
Chris Wilsonad19f102014-08-10 06:29:08 +0100932validate_exec_list(struct drm_device *dev,
933 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000934 int count)
935{
Daniel Vetterb205ca52013-09-19 14:00:11 +0200936 unsigned relocs_total = 0;
937 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +0100938 unsigned invalid_flags;
939 int i;
940
941 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
942 if (USES_FULL_PPGTT(dev))
943 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000944
945 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200946 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000947 int length; /* limited by fault_in_pages_readable() */
948
Chris Wilsonad19f102014-08-10 06:29:08 +0100949 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +0100950 return -EINVAL;
951
Kees Cook3118a4f2013-03-11 17:31:45 -0700952 /* First check for malicious input causing overflow in
953 * the worst case where we need to allocate the entire
954 * relocation tree as a single array.
955 */
956 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000957 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700958 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000959
960 length = exec[i].relocation_count *
961 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700962 /*
963 * We must check that the entire relocation array is safe
964 * to read, but since we may need to update the presumed
965 * offsets during execution, check for full write access.
966 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000967 if (!access_ok(VERIFY_WRITE, ptr, length))
968 return -EFAULT;
969
Jani Nikulad330a952014-01-21 11:24:25 +0200970 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +0800971 if (fault_in_multipages_readable(ptr, length))
972 return -EFAULT;
973 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000974 }
975
976 return 0;
977}
978
Oscar Mateo273497e2014-05-22 14:13:37 +0100979static struct intel_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200980i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100981 struct intel_engine_cs *ring, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200982{
Oscar Mateo273497e2014-05-22 14:13:37 +0100983 struct intel_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200984 struct i915_ctx_hang_stats *hs;
985
Oscar Mateo821d66d2014-07-03 16:28:00 +0100986 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +0100987 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200988
Ben Widawsky41bde552013-12-06 14:11:21 -0800989 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -1000990 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -0800991 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200992
Ben Widawsky41bde552013-12-06 14:11:21 -0800993 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200994 if (hs->banned) {
995 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -0800996 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200997 }
998
Oscar Mateoec3e9962014-07-24 17:04:18 +0100999 if (i915.enable_execlists && !ctx->engine[ring->id].state) {
1000 int ret = intel_lr_context_deferred_create(ctx, ring);
1001 if (ret) {
1002 DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
1003 return ERR_PTR(ret);
1004 }
1005 }
1006
Ben Widawsky41bde552013-12-06 14:11:21 -08001007 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001008}
1009
Oscar Mateoba8b7cc2014-07-24 17:04:33 +01001010void
Ben Widawsky27173f12013-08-14 11:38:36 +02001011i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001012 struct intel_engine_cs *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +00001013{
John Harrison97b2a6a2014-11-24 18:49:26 +00001014 struct drm_i915_gem_request *req = intel_ring_get_request(ring);
Ben Widawsky27173f12013-08-14 11:38:36 +02001015 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +00001016
Ben Widawsky27173f12013-08-14 11:38:36 +02001017 list_for_each_entry(vma, vmas, exec_list) {
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001018 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Ben Widawsky27173f12013-08-14 11:38:36 +02001019 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +01001020 u32 old_read = obj->base.read_domains;
1021 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +00001022
Chris Wilson432e58e2010-11-25 19:32:06 +00001023 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +01001024 if (obj->base.write_domain == 0)
1025 obj->base.pending_read_domains |= obj->base.read_domains;
1026 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +00001027
Ben Widawskye2d05a82013-09-24 09:57:58 -07001028 i915_vma_move_to_active(vma, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +00001029 if (obj->base.write_domain) {
1030 obj->dirty = 1;
John Harrison97b2a6a2014-11-24 18:49:26 +00001031 i915_gem_request_assign(&obj->last_write_req, req);
Daniel Vetterf99d7062014-06-19 16:01:59 +02001032
Paulo Zanonia4001f12015-02-13 17:23:44 -02001033 intel_fb_obj_invalidate(obj, ring, ORIGIN_CS);
Chris Wilsonc8725f32014-03-17 12:21:55 +00001034
1035 /* update for the implicit flush after a batch */
1036 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson432e58e2010-11-25 19:32:06 +00001037 }
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001038 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
John Harrison97b2a6a2014-11-24 18:49:26 +00001039 i915_gem_request_assign(&obj->last_fenced_req, req);
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001040 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
1041 struct drm_i915_private *dev_priv = to_i915(ring->dev);
1042 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
1043 &dev_priv->mm.fence_list);
1044 }
1045 }
Chris Wilson432e58e2010-11-25 19:32:06 +00001046
Chris Wilsondb53a302011-02-03 11:57:46 +00001047 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +00001048 }
1049}
1050
Oscar Mateoba8b7cc2014-07-24 17:04:33 +01001051void
Chris Wilson54cf91d2010-11-25 18:00:26 +00001052i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +00001053 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001054 struct intel_engine_cs *ring,
Mika Kuoppala7d736f42013-06-12 15:01:39 +03001055 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001056{
Daniel Vettercc889e02012-06-13 20:45:19 +02001057 /* Unconditionally force add_request to emit a full flush. */
1058 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001059
Chris Wilson432e58e2010-11-25 19:32:06 +00001060 /* Add a breadcrumb for the completion of the batch buffer */
John Harrison9400ae52014-11-24 18:49:36 +00001061 (void)__i915_add_request(ring, file, obj);
Chris Wilson432e58e2010-11-25 19:32:06 +00001062}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001063
1064static int
Eric Anholtae662d32012-01-03 09:23:29 -08001065i915_reset_gen7_sol_offsets(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001066 struct intel_engine_cs *ring)
Eric Anholtae662d32012-01-03 09:23:29 -08001067{
Jani Nikula50227e12014-03-31 14:27:21 +03001068 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtae662d32012-01-03 09:23:29 -08001069 int ret, i;
1070
Daniel Vetter9d662da2014-04-24 08:09:09 +02001071 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
1072 DRM_DEBUG("sol reset is gen7/rcs only\n");
1073 return -EINVAL;
1074 }
Eric Anholtae662d32012-01-03 09:23:29 -08001075
1076 ret = intel_ring_begin(ring, 4 * 3);
1077 if (ret)
1078 return ret;
1079
1080 for (i = 0; i < 4; i++) {
1081 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1082 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
1083 intel_ring_emit(ring, 0);
1084 }
1085
1086 intel_ring_advance(ring);
1087
1088 return 0;
1089}
1090
Chris Wilson5c6c6002014-09-06 10:28:27 +01001091static int
1092i915_emit_box(struct intel_engine_cs *ring,
1093 struct drm_clip_rect *box,
1094 int DR1, int DR4)
1095{
1096 int ret;
1097
1098 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
1099 box->y2 <= 0 || box->x2 <= 0) {
1100 DRM_ERROR("Bad box %d,%d..%d,%d\n",
1101 box->x1, box->y1, box->x2, box->y2);
1102 return -EINVAL;
1103 }
1104
1105 if (INTEL_INFO(ring->dev)->gen >= 4) {
1106 ret = intel_ring_begin(ring, 4);
1107 if (ret)
1108 return ret;
1109
1110 intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO_I965);
1111 intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
1112 intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
1113 intel_ring_emit(ring, DR4);
1114 } else {
1115 ret = intel_ring_begin(ring, 6);
1116 if (ret)
1117 return ret;
1118
1119 intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO);
1120 intel_ring_emit(ring, DR1);
1121 intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
1122 intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
1123 intel_ring_emit(ring, DR4);
1124 intel_ring_emit(ring, 0);
1125 }
1126 intel_ring_advance(ring);
1127
1128 return 0;
1129}
1130
Brad Volkin71745372014-12-11 12:13:12 -08001131static struct drm_i915_gem_object*
1132i915_gem_execbuffer_parse(struct intel_engine_cs *ring,
1133 struct drm_i915_gem_exec_object2 *shadow_exec_entry,
1134 struct eb_vmas *eb,
1135 struct drm_i915_gem_object *batch_obj,
1136 u32 batch_start_offset,
1137 u32 batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001138 bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001139{
Brad Volkin71745372014-12-11 12:13:12 -08001140 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001141 struct i915_vma *vma;
Brad Volkin71745372014-12-11 12:13:12 -08001142 int ret;
1143
Chris Wilson06fbca72015-04-07 16:20:36 +01001144 shadow_batch_obj = i915_gem_batch_pool_get(&ring->batch_pool,
Chris Wilson17cabf52015-01-14 11:20:57 +00001145 PAGE_ALIGN(batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001146 if (IS_ERR(shadow_batch_obj))
1147 return shadow_batch_obj;
1148
1149 ret = i915_parse_cmds(ring,
1150 batch_obj,
1151 shadow_batch_obj,
1152 batch_start_offset,
1153 batch_len,
1154 is_master);
Chris Wilson17cabf52015-01-14 11:20:57 +00001155 if (ret)
1156 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001157
Chris Wilson17cabf52015-01-14 11:20:57 +00001158 ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
1159 if (ret)
1160 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001161
Chris Wilsonde4e7832015-04-07 16:20:35 +01001162 i915_gem_object_unpin_pages(shadow_batch_obj);
1163
Chris Wilson17cabf52015-01-14 11:20:57 +00001164 memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
Brad Volkin71745372014-12-11 12:13:12 -08001165
Chris Wilson17cabf52015-01-14 11:20:57 +00001166 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1167 vma->exec_entry = shadow_exec_entry;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001168 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
Chris Wilson17cabf52015-01-14 11:20:57 +00001169 drm_gem_object_reference(&shadow_batch_obj->base);
1170 list_add_tail(&vma->exec_list, &eb->vmas);
Brad Volkin71745372014-12-11 12:13:12 -08001171
Chris Wilson17cabf52015-01-14 11:20:57 +00001172 shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
Brad Volkin71745372014-12-11 12:13:12 -08001173
Chris Wilson17cabf52015-01-14 11:20:57 +00001174 return shadow_batch_obj;
1175
1176err:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001177 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001178 if (ret == -EACCES) /* unhandled chained batch */
1179 return batch_obj;
1180 else
1181 return ERR_PTR(ret);
Brad Volkin71745372014-12-11 12:13:12 -08001182}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001183
Oscar Mateoa83014d2014-07-24 17:04:21 +01001184int
1185i915_gem_ringbuffer_submission(struct drm_device *dev, struct drm_file *file,
1186 struct intel_engine_cs *ring,
1187 struct intel_context *ctx,
1188 struct drm_i915_gem_execbuffer2 *args,
1189 struct list_head *vmas,
1190 struct drm_i915_gem_object *batch_obj,
John Harrison8e004ef2015-02-13 11:48:10 +00001191 u64 exec_start, u32 dispatch_flags)
Oscar Mateo78382592014-07-03 16:28:05 +01001192{
1193 struct drm_clip_rect *cliprects = NULL;
1194 struct drm_i915_private *dev_priv = dev->dev_private;
1195 u64 exec_len;
1196 int instp_mode;
1197 u32 instp_mask;
1198 int i, ret = 0;
1199
1200 if (args->num_cliprects != 0) {
1201 if (ring != &dev_priv->ring[RCS]) {
1202 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
1203 return -EINVAL;
1204 }
1205
1206 if (INTEL_INFO(dev)->gen >= 5) {
1207 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1208 return -EINVAL;
1209 }
1210
1211 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1212 DRM_DEBUG("execbuf with %u cliprects\n",
1213 args->num_cliprects);
1214 return -EINVAL;
1215 }
1216
1217 cliprects = kcalloc(args->num_cliprects,
1218 sizeof(*cliprects),
1219 GFP_KERNEL);
1220 if (cliprects == NULL) {
1221 ret = -ENOMEM;
1222 goto error;
1223 }
1224
1225 if (copy_from_user(cliprects,
1226 to_user_ptr(args->cliprects_ptr),
1227 sizeof(*cliprects)*args->num_cliprects)) {
1228 ret = -EFAULT;
1229 goto error;
1230 }
1231 } else {
1232 if (args->DR4 == 0xffffffff) {
1233 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1234 args->DR4 = 0;
1235 }
1236
1237 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
1238 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
1239 return -EINVAL;
1240 }
1241 }
1242
1243 ret = i915_gem_execbuffer_move_to_gpu(ring, vmas);
1244 if (ret)
1245 goto error;
1246
1247 ret = i915_switch_context(ring, ctx);
1248 if (ret)
1249 goto error;
1250
Daniel Vetter92588112015-04-14 17:35:19 +02001251 WARN(ctx->ppgtt && ctx->ppgtt->pd_dirty_rings & (1<<ring->id),
1252 "%s didn't clear reload\n", ring->name);
Ben Widawsky563222a2015-03-19 12:53:28 +00001253
Oscar Mateo78382592014-07-03 16:28:05 +01001254 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1255 instp_mask = I915_EXEC_CONSTANTS_MASK;
1256 switch (instp_mode) {
1257 case I915_EXEC_CONSTANTS_REL_GENERAL:
1258 case I915_EXEC_CONSTANTS_ABSOLUTE:
1259 case I915_EXEC_CONSTANTS_REL_SURFACE:
1260 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
1261 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
1262 ret = -EINVAL;
1263 goto error;
1264 }
1265
1266 if (instp_mode != dev_priv->relative_constants_mode) {
1267 if (INTEL_INFO(dev)->gen < 4) {
1268 DRM_DEBUG("no rel constants on pre-gen4\n");
1269 ret = -EINVAL;
1270 goto error;
1271 }
1272
1273 if (INTEL_INFO(dev)->gen > 5 &&
1274 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1275 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
1276 ret = -EINVAL;
1277 goto error;
1278 }
1279
1280 /* The HW changed the meaning on this bit on gen6 */
1281 if (INTEL_INFO(dev)->gen >= 6)
1282 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1283 }
1284 break;
1285 default:
1286 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
1287 ret = -EINVAL;
1288 goto error;
1289 }
1290
1291 if (ring == &dev_priv->ring[RCS] &&
1292 instp_mode != dev_priv->relative_constants_mode) {
1293 ret = intel_ring_begin(ring, 4);
1294 if (ret)
1295 goto error;
1296
1297 intel_ring_emit(ring, MI_NOOP);
1298 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1299 intel_ring_emit(ring, INSTPM);
1300 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1301 intel_ring_advance(ring);
1302
1303 dev_priv->relative_constants_mode = instp_mode;
1304 }
1305
1306 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1307 ret = i915_reset_gen7_sol_offsets(dev, ring);
1308 if (ret)
1309 goto error;
1310 }
1311
1312 exec_len = args->batch_len;
1313 if (cliprects) {
1314 for (i = 0; i < args->num_cliprects; i++) {
Chris Wilson5c6c6002014-09-06 10:28:27 +01001315 ret = i915_emit_box(ring, &cliprects[i],
Oscar Mateo78382592014-07-03 16:28:05 +01001316 args->DR1, args->DR4);
1317 if (ret)
1318 goto error;
1319
1320 ret = ring->dispatch_execbuffer(ring,
1321 exec_start, exec_len,
John Harrison8e004ef2015-02-13 11:48:10 +00001322 dispatch_flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001323 if (ret)
1324 goto error;
1325 }
1326 } else {
1327 ret = ring->dispatch_execbuffer(ring,
1328 exec_start, exec_len,
John Harrison8e004ef2015-02-13 11:48:10 +00001329 dispatch_flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001330 if (ret)
1331 return ret;
1332 }
1333
John Harrison8e004ef2015-02-13 11:48:10 +00001334 trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), dispatch_flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001335
1336 i915_gem_execbuffer_move_to_active(vmas, ring);
1337 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
1338
1339error:
1340 kfree(cliprects);
1341 return ret;
1342}
1343
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001344/**
1345 * Find one BSD ring to dispatch the corresponding BSD command.
1346 * The Ring ID is returned.
1347 */
1348static int gen8_dispatch_bsd_ring(struct drm_device *dev,
1349 struct drm_file *file)
1350{
1351 struct drm_i915_private *dev_priv = dev->dev_private;
1352 struct drm_i915_file_private *file_priv = file->driver_priv;
1353
1354 /* Check whether the file_priv is using one ring */
1355 if (file_priv->bsd_ring)
1356 return file_priv->bsd_ring->id;
1357 else {
1358 /* If no, use the ping-pong mechanism to select one ring */
1359 int ring_id;
1360
1361 mutex_lock(&dev->struct_mutex);
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001362 if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001363 ring_id = VCS;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001364 dev_priv->mm.bsd_ring_dispatch_index = 1;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001365 } else {
1366 ring_id = VCS2;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001367 dev_priv->mm.bsd_ring_dispatch_index = 0;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001368 }
1369 file_priv->bsd_ring = &dev_priv->ring[ring_id];
1370 mutex_unlock(&dev->struct_mutex);
1371 return ring_id;
1372 }
1373}
1374
Chris Wilsond23db882014-05-23 08:48:08 +02001375static struct drm_i915_gem_object *
1376eb_get_batch(struct eb_vmas *eb)
1377{
1378 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
1379
1380 /*
1381 * SNA is doing fancy tricks with compressing batch buffers, which leads
1382 * to negative relocation deltas. Usually that works out ok since the
1383 * relocate address is still positive, except when the batch is placed
1384 * very low in the GTT. Ensure this doesn't happen.
1385 *
1386 * Note that actual hangs have only been observed on gen7, but for
1387 * paranoia do it everywhere.
1388 */
1389 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
1390
1391 return vma->obj;
1392}
1393
Eric Anholtae662d32012-01-03 09:23:29 -08001394static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001395i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1396 struct drm_file *file,
1397 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001398 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001399{
Jani Nikula50227e12014-03-31 14:27:21 +03001400 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +02001401 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001402 struct drm_i915_gem_object *batch_obj;
Brad Volkin78a42372014-12-11 12:13:09 -08001403 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001404 struct intel_engine_cs *ring;
Oscar Mateo273497e2014-05-22 14:13:37 +01001405 struct intel_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001406 struct i915_address_space *vm;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001407 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Oscar Mateo78382592014-07-03 16:28:05 +01001408 u64 exec_start = args->batch_start_offset;
John Harrison8e004ef2015-02-13 11:48:10 +00001409 u32 dispatch_flags;
Oscar Mateo78382592014-07-03 16:28:05 +01001410 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001411 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001412
Daniel Vettered5982e2013-01-17 22:23:36 +01001413 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001414 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001415
Chris Wilsonad19f102014-08-10 06:29:08 +01001416 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001417 if (ret)
1418 return ret;
1419
John Harrison8e004ef2015-02-13 11:48:10 +00001420 dispatch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001421 if (args->flags & I915_EXEC_SECURE) {
1422 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1423 return -EPERM;
1424
John Harrison8e004ef2015-02-13 11:48:10 +00001425 dispatch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001426 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001427 if (args->flags & I915_EXEC_IS_PINNED)
John Harrison8e004ef2015-02-13 11:48:10 +00001428 dispatch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001429
Zhao Yakuib1a93302014-04-17 10:37:36 +08001430 if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
Daniel Vetterff240192012-01-31 21:08:14 +01001431 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001432 (int)(args->flags & I915_EXEC_RING_MASK));
1433 return -EINVAL;
1434 }
Ben Widawskyca01b122013-12-06 14:11:00 -08001435
Zhipeng Gong8d360df2015-01-13 08:48:24 +08001436 if (((args->flags & I915_EXEC_RING_MASK) != I915_EXEC_BSD) &&
1437 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1438 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1439 "bsd dispatch flags: %d\n", (int)(args->flags));
1440 return -EINVAL;
1441 }
1442
Ben Widawskyca01b122013-12-06 14:11:00 -08001443 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1444 ring = &dev_priv->ring[RCS];
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001445 else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
1446 if (HAS_BSD2(dev)) {
1447 int ring_id;
Zhipeng Gong8d360df2015-01-13 08:48:24 +08001448
1449 switch (args->flags & I915_EXEC_BSD_MASK) {
1450 case I915_EXEC_BSD_DEFAULT:
1451 ring_id = gen8_dispatch_bsd_ring(dev, file);
1452 ring = &dev_priv->ring[ring_id];
1453 break;
1454 case I915_EXEC_BSD_RING1:
1455 ring = &dev_priv->ring[VCS];
1456 break;
1457 case I915_EXEC_BSD_RING2:
1458 ring = &dev_priv->ring[VCS2];
1459 break;
1460 default:
1461 DRM_DEBUG("execbuf with unknown bsd ring: %d\n",
1462 (int)(args->flags & I915_EXEC_BSD_MASK));
1463 return -EINVAL;
1464 }
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001465 } else
1466 ring = &dev_priv->ring[VCS];
1467 } else
Ben Widawskyca01b122013-12-06 14:11:00 -08001468 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1469
Chris Wilsona15817c2012-05-11 14:29:31 +01001470 if (!intel_ring_initialized(ring)) {
1471 DRM_DEBUG("execbuf with invalid ring: %d\n",
1472 (int)(args->flags & I915_EXEC_RING_MASK));
1473 return -EINVAL;
1474 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001475
1476 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001477 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001478 return -EINVAL;
1479 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001480
Paulo Zanonif65c9162013-11-27 18:20:34 -02001481 intel_runtime_pm_get(dev_priv);
1482
Chris Wilson54cf91d2010-11-25 18:00:26 +00001483 ret = i915_mutex_lock_interruptible(dev);
1484 if (ret)
1485 goto pre_mutex_err;
1486
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001487 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001488 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001489 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001490 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001491 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001492 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001493
1494 i915_gem_context_reference(ctx);
1495
Daniel Vetterae6c4802014-08-06 15:04:53 +02001496 if (ctx->ppgtt)
1497 vm = &ctx->ppgtt->base;
1498 else
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001499 vm = &dev_priv->gtt.base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001500
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001501 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001502 if (eb == NULL) {
Ben Widawsky935f38d2014-04-04 22:41:07 -07001503 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001504 mutex_unlock(&dev->struct_mutex);
1505 ret = -ENOMEM;
1506 goto pre_mutex_err;
1507 }
1508
Chris Wilson54cf91d2010-11-25 18:00:26 +00001509 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001510 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001511 if (ret)
1512 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001513
Chris Wilson6fe4f142011-01-10 17:35:37 +00001514 /* take note of the batch buffer before we might reorder the lists */
Chris Wilsond23db882014-05-23 08:48:08 +02001515 batch_obj = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001516
Chris Wilson54cf91d2010-11-25 18:00:26 +00001517 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001518 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001519 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001520 if (ret)
1521 goto err;
1522
1523 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001524 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001525 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001526 if (ret) {
1527 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001528 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001529 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001530 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1531 }
1532 if (ret)
1533 goto err;
1534 }
1535
1536 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001537 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001538 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001539 ret = -EINVAL;
1540 goto err;
1541 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001542
Chris Wilson743e78c2015-03-27 11:02:10 +00001543 if (i915_needs_cmd_parser(ring) && args->batch_len) {
Brad Volkin71745372014-12-11 12:13:12 -08001544 batch_obj = i915_gem_execbuffer_parse(ring,
1545 &shadow_exec_entry,
1546 eb,
1547 batch_obj,
1548 args->batch_start_offset,
1549 args->batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001550 file->is_master);
Brad Volkin71745372014-12-11 12:13:12 -08001551 if (IS_ERR(batch_obj)) {
1552 ret = PTR_ERR(batch_obj);
Brad Volkin78a42372014-12-11 12:13:09 -08001553 goto err;
1554 }
Chris Wilson17cabf52015-01-14 11:20:57 +00001555
1556 /*
1557 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1558 * bit from MI_BATCH_BUFFER_START commands issued in the
1559 * dispatch_execbuffer implementations. We specifically
1560 * don't want that set when the command parser is
1561 * enabled.
1562 *
1563 * FIXME: with aliasing ppgtt, buffers that should only
1564 * be in ggtt still end up in the aliasing ppgtt. remove
1565 * this check when that is fixed.
1566 */
1567 if (USES_FULL_PPGTT(dev))
John Harrison8e004ef2015-02-13 11:48:10 +00001568 dispatch_flags |= I915_DISPATCH_SECURE;
Chris Wilson17cabf52015-01-14 11:20:57 +00001569
1570 exec_start = 0;
Brad Volkin351e3db2014-02-18 10:15:46 -08001571 }
1572
Brad Volkin78a42372014-12-11 12:13:09 -08001573 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1574
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001575 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1576 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001577 * hsw should have this fixed, but bdw mucks it up again. */
John Harrison8e004ef2015-02-13 11:48:10 +00001578 if (dispatch_flags & I915_DISPATCH_SECURE) {
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001579 /*
1580 * So on first glance it looks freaky that we pin the batch here
1581 * outside of the reservation loop. But:
1582 * - The batch is already pinned into the relevant ppgtt, so we
1583 * already have the backing storage fully allocated.
1584 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01001585 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001586 * fitting due to fragmentation.
1587 * So this is actually safe.
1588 */
1589 ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
1590 if (ret)
1591 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001592
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001593 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001594 } else
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001595 exec_start += i915_gem_obj_offset(batch_obj, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001596
John Harrisonf3dc74c2015-03-19 12:30:06 +00001597 ret = dev_priv->gt.execbuf_submit(dev, file, ring, ctx, args,
1598 &eb->vmas, batch_obj, exec_start,
1599 dispatch_flags);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001600
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001601 /*
1602 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1603 * batch vma for correctness. For less ugly and less fragility this
1604 * needs to be adjusted to also track the ggtt batch vma properly as
1605 * active.
1606 */
John Harrison8e004ef2015-02-13 11:48:10 +00001607 if (dispatch_flags & I915_DISPATCH_SECURE)
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001608 i915_gem_object_ggtt_unpin(batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001609err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001610 /* the request owns the ref now */
1611 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001612 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001613
1614 mutex_unlock(&dev->struct_mutex);
1615
1616pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001617 /* intel_gpu_busy should also get a ref, so it will free when the device
1618 * is really idle. */
1619 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001620 return ret;
1621}
1622
1623/*
1624 * Legacy execbuffer just creates an exec2 list from the original exec object
1625 * list array and passes it to the real function.
1626 */
1627int
1628i915_gem_execbuffer(struct drm_device *dev, void *data,
1629 struct drm_file *file)
1630{
1631 struct drm_i915_gem_execbuffer *args = data;
1632 struct drm_i915_gem_execbuffer2 exec2;
1633 struct drm_i915_gem_exec_object *exec_list = NULL;
1634 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1635 int ret, i;
1636
Chris Wilson54cf91d2010-11-25 18:00:26 +00001637 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001638 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001639 return -EINVAL;
1640 }
1641
1642 /* Copy in the exec list from userland */
1643 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1644 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1645 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001646 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001647 args->buffer_count);
1648 drm_free_large(exec_list);
1649 drm_free_large(exec2_list);
1650 return -ENOMEM;
1651 }
1652 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001653 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001654 sizeof(*exec_list) * args->buffer_count);
1655 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001656 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001657 args->buffer_count, ret);
1658 drm_free_large(exec_list);
1659 drm_free_large(exec2_list);
1660 return -EFAULT;
1661 }
1662
1663 for (i = 0; i < args->buffer_count; i++) {
1664 exec2_list[i].handle = exec_list[i].handle;
1665 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1666 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1667 exec2_list[i].alignment = exec_list[i].alignment;
1668 exec2_list[i].offset = exec_list[i].offset;
1669 if (INTEL_INFO(dev)->gen < 4)
1670 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1671 else
1672 exec2_list[i].flags = 0;
1673 }
1674
1675 exec2.buffers_ptr = args->buffers_ptr;
1676 exec2.buffer_count = args->buffer_count;
1677 exec2.batch_start_offset = args->batch_start_offset;
1678 exec2.batch_len = args->batch_len;
1679 exec2.DR1 = args->DR1;
1680 exec2.DR4 = args->DR4;
1681 exec2.num_cliprects = args->num_cliprects;
1682 exec2.cliprects_ptr = args->cliprects_ptr;
1683 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001684 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001685
Ben Widawsky41bde552013-12-06 14:11:21 -08001686 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001687 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001688 struct drm_i915_gem_exec_object __user *user_exec_list =
1689 to_user_ptr(args->buffers_ptr);
1690
Chris Wilson54cf91d2010-11-25 18:00:26 +00001691 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001692 for (i = 0; i < args->buffer_count; i++) {
1693 ret = __copy_to_user(&user_exec_list[i].offset,
1694 &exec2_list[i].offset,
1695 sizeof(user_exec_list[i].offset));
1696 if (ret) {
1697 ret = -EFAULT;
1698 DRM_DEBUG("failed to copy %d exec entries "
1699 "back to user (%d)\n",
1700 args->buffer_count, ret);
1701 break;
1702 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001703 }
1704 }
1705
1706 drm_free_large(exec_list);
1707 drm_free_large(exec2_list);
1708 return ret;
1709}
1710
1711int
1712i915_gem_execbuffer2(struct drm_device *dev, void *data,
1713 struct drm_file *file)
1714{
1715 struct drm_i915_gem_execbuffer2 *args = data;
1716 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1717 int ret;
1718
Xi Wanged8cd3b2012-04-23 04:06:41 -04001719 if (args->buffer_count < 1 ||
1720 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001721 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001722 return -EINVAL;
1723 }
1724
Daniel Vetter9cb34662014-04-24 08:09:11 +02001725 if (args->rsvd2 != 0) {
1726 DRM_DEBUG("dirty rvsd2 field\n");
1727 return -EINVAL;
1728 }
1729
Chris Wilson8408c282011-02-21 12:54:48 +00001730 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001731 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001732 if (exec2_list == NULL)
1733 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1734 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001735 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001736 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001737 args->buffer_count);
1738 return -ENOMEM;
1739 }
1740 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001741 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001742 sizeof(*exec2_list) * args->buffer_count);
1743 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001744 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001745 args->buffer_count, ret);
1746 drm_free_large(exec2_list);
1747 return -EFAULT;
1748 }
1749
Ben Widawsky41bde552013-12-06 14:11:21 -08001750 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001751 if (!ret) {
1752 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001753 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001754 to_user_ptr(args->buffers_ptr);
1755 int i;
1756
1757 for (i = 0; i < args->buffer_count; i++) {
1758 ret = __copy_to_user(&user_exec_list[i].offset,
1759 &exec2_list[i].offset,
1760 sizeof(user_exec_list[i].offset));
1761 if (ret) {
1762 ret = -EFAULT;
1763 DRM_DEBUG("failed to copy %d exec entries "
1764 "back to user\n",
1765 args->buffer_count);
1766 break;
1767 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001768 }
1769 }
1770
1771 drm_free_large(exec2_list);
1772 return ret;
1773}