blob: 70946c551e5dff2e9dc408564b9f1093f7730f16 [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 Widawsky6f65e292013-12-06 14:10:56 -080098 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +020099 struct drm_i915_gem_object *obj;
100 struct list_head objects;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000101 int i, ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000102
Ben Widawsky27173f12013-08-14 11:38:36 +0200103 INIT_LIST_HEAD(&objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000104 spin_lock(&file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +0200105 /* Grab a reference to the object and release the lock so we can lookup
106 * or create the VMA without using GFP_ATOMIC */
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000107 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000108 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
109 if (obj == NULL) {
110 spin_unlock(&file->table_lock);
111 DRM_DEBUG("Invalid object handle %d at index %d\n",
112 exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200113 ret = -ENOENT;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000114 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000115 }
116
Ben Widawsky27173f12013-08-14 11:38:36 +0200117 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000118 spin_unlock(&file->table_lock);
119 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
120 obj, exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200121 ret = -EINVAL;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000122 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000123 }
124
125 drm_gem_object_reference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200126 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000127 }
128 spin_unlock(&file->table_lock);
129
Ben Widawsky27173f12013-08-14 11:38:36 +0200130 i = 0;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000131 while (!list_empty(&objects)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200132 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800133 struct i915_address_space *bind_vm = vm;
134
Daniel Vetter2c9f8d52013-12-18 17:38:53 +0100135 if (exec[i].flags & EXEC_OBJECT_NEEDS_GTT &&
136 USES_FULL_PPGTT(vm->dev)) {
137 ret = -EINVAL;
Rodrigo Vivia25eebb2014-01-14 16:21:49 -0200138 goto err;
Daniel Vetter2c9f8d52013-12-18 17:38:53 +0100139 }
140
Ben Widawsky6f65e292013-12-06 14:10:56 -0800141 /* If we have secure dispatch, or the userspace assures us that
142 * they know what they're doing, use the GGTT VM.
143 */
Daniel Vettera7c1d4262013-12-18 17:46:18 +0100144 if (((args->flags & I915_EXEC_SECURE) &&
Ben Widawsky6f65e292013-12-06 14:10:56 -0800145 (i == (args->buffer_count - 1))))
146 bind_vm = &dev_priv->gtt.base;
Ben Widawsky27173f12013-08-14 11:38:36 +0200147
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000148 obj = list_first_entry(&objects,
149 struct drm_i915_gem_object,
150 obj_exec_link);
151
Daniel Vettere656a6c2013-08-14 14:14:04 +0200152 /*
153 * NOTE: We can leak any vmas created here when something fails
154 * later on. But that's no issue since vma_unbind can deal with
155 * vmas which are not actually bound. And since only
156 * lookup_or_create exists as an interface to get at the vma
157 * from the (obj, vm) we don't run the risk of creating
158 * duplicated vmas for the same vm.
159 */
Ben Widawsky6f65e292013-12-06 14:10:56 -0800160 vma = i915_gem_obj_lookup_or_create_vma(obj, bind_vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200161 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200162 DRM_DEBUG("Failed to lookup VMA\n");
163 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000164 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200165 }
166
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000167 /* Transfer ownership from the objects list to the vmas list. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200168 list_add_tail(&vma->exec_list, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000169 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200170
171 vma->exec_entry = &exec[i];
172 if (eb->and < 0) {
173 eb->lut[i] = vma;
174 } else {
175 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
176 vma->exec_handle = handle;
177 hlist_add_head(&vma->exec_node,
178 &eb->buckets[handle & eb->and]);
179 }
180 ++i;
181 }
182
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000183 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200184
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000185
186err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200187 while (!list_empty(&objects)) {
188 obj = list_first_entry(&objects,
189 struct drm_i915_gem_object,
190 obj_exec_link);
191 list_del_init(&obj->obj_exec_link);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000192 drm_gem_object_unreference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200193 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000194 /*
195 * Objects already transfered to the vmas list will be unreferenced by
196 * eb_destroy.
197 */
198
Ben Widawsky27173f12013-08-14 11:38:36 +0200199 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000200}
201
Ben Widawsky27173f12013-08-14 11:38:36 +0200202static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000203{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000204 if (eb->and < 0) {
205 if (handle >= -eb->and)
206 return NULL;
207 return eb->lut[handle];
208 } else {
209 struct hlist_head *head;
210 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000211
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000212 head = &eb->buckets[handle & eb->and];
213 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200214 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000215
Ben Widawsky27173f12013-08-14 11:38:36 +0200216 vma = hlist_entry(node, struct i915_vma, exec_node);
217 if (vma->exec_handle == handle)
218 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000219 }
220 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000221 }
Chris Wilson67731b82010-12-08 10:38:14 +0000222}
223
Chris Wilsona415d352013-11-26 11:23:15 +0000224static void
225i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
226{
227 struct drm_i915_gem_exec_object2 *entry;
228 struct drm_i915_gem_object *obj = vma->obj;
229
230 if (!drm_mm_node_allocated(&vma->node))
231 return;
232
233 entry = vma->exec_entry;
234
235 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
236 i915_gem_object_unpin_fence(obj);
237
238 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Daniel Vetter3d7f0f92013-12-18 16:23:37 +0100239 vma->pin_count--;
Chris Wilsona415d352013-11-26 11:23:15 +0000240
241 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
242}
243
244static void eb_destroy(struct eb_vmas *eb)
245{
Ben Widawsky27173f12013-08-14 11:38:36 +0200246 while (!list_empty(&eb->vmas)) {
247 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000248
Ben Widawsky27173f12013-08-14 11:38:36 +0200249 vma = list_first_entry(&eb->vmas,
250 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000251 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200252 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000253 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200254 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000255 }
Chris Wilson67731b82010-12-08 10:38:14 +0000256 kfree(eb);
257}
258
Chris Wilsondabdfe02012-03-26 10:10:27 +0200259static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
260{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300261 return (HAS_LLC(obj->base.dev) ||
262 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100263 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200264 obj->cache_level != I915_CACHE_NONE);
265}
266
Chris Wilson54cf91d2010-11-25 18:00:26 +0000267static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100268relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700269 struct drm_i915_gem_relocation_entry *reloc,
270 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100271{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700272 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100273 uint32_t page_offset = offset_in_page(reloc->offset);
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700274 uint64_t delta = reloc->delta + target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100275 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800276 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100277
Chris Wilson2cc86b82013-08-26 19:51:00 -0300278 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100279 if (ret)
280 return ret;
281
282 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
283 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700284 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700285
286 if (INTEL_INFO(dev)->gen >= 8) {
287 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
288
289 if (page_offset == 0) {
290 kunmap_atomic(vaddr);
291 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
292 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
293 }
294
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700295 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700296 }
297
Rafael Barbalho5032d872013-08-21 17:10:51 +0100298 kunmap_atomic(vaddr);
299
300 return 0;
301}
302
303static int
304relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700305 struct drm_i915_gem_relocation_entry *reloc,
306 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100307{
308 struct drm_device *dev = obj->base.dev;
309 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700310 uint64_t delta = reloc->delta + target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100311 uint32_t __iomem *reloc_entry;
312 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800313 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100314
315 ret = i915_gem_object_set_to_gtt_domain(obj, true);
316 if (ret)
317 return ret;
318
319 ret = i915_gem_object_put_fence(obj);
320 if (ret)
321 return ret;
322
323 /* Map the page containing the relocation we're going to perform. */
324 reloc->offset += i915_gem_obj_ggtt_offset(obj);
325 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
326 reloc->offset & PAGE_MASK);
327 reloc_entry = (uint32_t __iomem *)
328 (reloc_page + offset_in_page(reloc->offset));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700329 iowrite32(lower_32_bits(delta), reloc_entry);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700330
331 if (INTEL_INFO(dev)->gen >= 8) {
332 reloc_entry += 1;
333
334 if (offset_in_page(reloc->offset + sizeof(uint32_t)) == 0) {
335 io_mapping_unmap_atomic(reloc_page);
336 reloc_page = io_mapping_map_atomic_wc(
337 dev_priv->gtt.mappable,
338 reloc->offset + sizeof(uint32_t));
339 reloc_entry = reloc_page;
340 }
341
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700342 iowrite32(upper_32_bits(delta), reloc_entry);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700343 }
344
Rafael Barbalho5032d872013-08-21 17:10:51 +0100345 io_mapping_unmap_atomic(reloc_page);
346
347 return 0;
348}
349
350static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000351i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200352 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800353 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000354{
355 struct drm_device *dev = obj->base.dev;
356 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100357 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200358 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700359 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800360 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000361
Chris Wilson67731b82010-12-08 10:38:14 +0000362 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200363 target_vma = eb_get_vma(eb, reloc->target_handle);
364 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000365 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200366 target_i915_obj = target_vma->obj;
367 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000368
Ben Widawsky5ce09722013-11-25 09:54:40 -0800369 target_offset = target_vma->node.start;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000370
Eric Anholte844b992012-07-31 15:35:01 -0700371 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
372 * pipe_control writes because the gpu doesn't properly redirect them
373 * through the ppgtt for non_secure batchbuffers. */
374 if (unlikely(IS_GEN6(dev) &&
375 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
376 !target_i915_obj->has_global_gtt_mapping)) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800377 struct i915_vma *vma =
378 list_first_entry(&target_i915_obj->vma_list,
379 typeof(*vma), vma_link);
Ben Widawsky6f65e292013-12-06 14:10:56 -0800380 vma->bind_vma(vma, target_i915_obj->cache_level, GLOBAL_BIND);
Eric Anholte844b992012-07-31 15:35:01 -0700381 }
382
Chris Wilson54cf91d2010-11-25 18:00:26 +0000383 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000384 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100385 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000386 "obj %p target %d offset %d "
387 "read %08x write %08x",
388 obj, reloc->target_handle,
389 (int) reloc->offset,
390 reloc->read_domains,
391 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800392 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000393 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100394 if (unlikely((reloc->write_domain | reloc->read_domains)
395 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100396 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000397 "obj %p target %d offset %d "
398 "read %08x write %08x",
399 obj, reloc->target_handle,
400 (int) reloc->offset,
401 reloc->read_domains,
402 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800403 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000404 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000405
406 target_obj->pending_read_domains |= reloc->read_domains;
407 target_obj->pending_write_domain |= reloc->write_domain;
408
409 /* If the relocation already has the right value in it, no
410 * more work needs to be done.
411 */
412 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000413 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000414
415 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700416 if (unlikely(reloc->offset >
417 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100418 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000419 "obj %p target %d offset %d size %d.\n",
420 obj, reloc->target_handle,
421 (int) reloc->offset,
422 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800423 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000424 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000425 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100426 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000427 "obj %p target %d offset %d.\n",
428 obj, reloc->target_handle,
429 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800430 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000431 }
432
Chris Wilsondabdfe02012-03-26 10:10:27 +0200433 /* We can't wait for rendering with pagefaults disabled */
434 if (obj->active && in_atomic())
435 return -EFAULT;
436
Rafael Barbalho5032d872013-08-21 17:10:51 +0100437 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700438 ret = relocate_entry_cpu(obj, reloc, target_offset);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100439 else
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700440 ret = relocate_entry_gtt(obj, reloc, target_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000441
Daniel Vetterd4d36012013-09-02 20:56:23 +0200442 if (ret)
443 return ret;
444
Chris Wilson54cf91d2010-11-25 18:00:26 +0000445 /* and update the user's relocation entry */
446 reloc->presumed_offset = target_offset;
447
Chris Wilson67731b82010-12-08 10:38:14 +0000448 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000449}
450
451static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200452i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
453 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000454{
Chris Wilson1d83f442012-03-24 20:12:53 +0000455#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
456 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000457 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200458 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000459 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000460
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200461 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000462
Chris Wilson1d83f442012-03-24 20:12:53 +0000463 remain = entry->relocation_count;
464 while (remain) {
465 struct drm_i915_gem_relocation_entry *r = stack_reloc;
466 int count = remain;
467 if (count > ARRAY_SIZE(stack_reloc))
468 count = ARRAY_SIZE(stack_reloc);
469 remain -= count;
470
471 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000472 return -EFAULT;
473
Chris Wilson1d83f442012-03-24 20:12:53 +0000474 do {
475 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000476
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800477 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000478 if (ret)
479 return ret;
480
481 if (r->presumed_offset != offset &&
482 __copy_to_user_inatomic(&user_relocs->presumed_offset,
483 &r->presumed_offset,
484 sizeof(r->presumed_offset))) {
485 return -EFAULT;
486 }
487
488 user_relocs++;
489 r++;
490 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000491 }
492
493 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000494#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000495}
496
497static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200498i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
499 struct eb_vmas *eb,
500 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000501{
Ben Widawsky27173f12013-08-14 11:38:36 +0200502 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000503 int i, ret;
504
505 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800506 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000507 if (ret)
508 return ret;
509 }
510
511 return 0;
512}
513
514static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800515i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000516{
Ben Widawsky27173f12013-08-14 11:38:36 +0200517 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000518 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000519
Chris Wilsond4aeee72011-03-14 15:11:24 +0000520 /* This is the fast path and we cannot handle a pagefault whilst
521 * holding the struct mutex lest the user pass in the relocations
522 * contained within a mmaped bo. For in such a case we, the page
523 * fault handler would call i915_gem_fault() and we would try to
524 * acquire the struct mutex again. Obviously this is bad and so
525 * lockdep complains vehemently.
526 */
527 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200528 list_for_each_entry(vma, &eb->vmas, exec_list) {
529 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000530 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000531 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000532 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000533 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000534
Chris Wilsond4aeee72011-03-14 15:11:24 +0000535 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000536}
537
Chris Wilson1690e1e2011-12-14 13:57:08 +0100538static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200539i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100540 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200541 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100542{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800543 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200544 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200545 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100546 int ret;
547
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100548 flags = 0;
Chris Wilsone6a84462014-08-11 12:00:12 +0200549 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100550 flags |= PIN_MAPPABLE;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100551 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
Daniel Vetterbf3d1492014-02-14 14:01:12 +0100552 flags |= PIN_GLOBAL;
Chris Wilsond23db882014-05-23 08:48:08 +0200553 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
554 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100555
556 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100557 if (ret)
558 return ret;
559
Chris Wilson7788a762012-08-24 19:18:18 +0100560 entry->flags |= __EXEC_OBJECT_HAS_PIN;
561
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100562 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
563 ret = i915_gem_object_get_fence(obj);
564 if (ret)
565 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100566
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100567 if (i915_gem_object_pin_fence(obj))
568 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100569 }
570
Ben Widawsky27173f12013-08-14 11:38:36 +0200571 if (entry->offset != vma->node.start) {
572 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100573 *need_reloc = true;
574 }
575
576 if (entry->flags & EXEC_OBJECT_WRITE) {
577 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
578 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
579 }
580
Chris Wilson1690e1e2011-12-14 13:57:08 +0100581 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100582}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100583
Chris Wilsond23db882014-05-23 08:48:08 +0200584static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200585need_reloc_mappable(struct i915_vma *vma)
586{
587 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
588
589 if (entry->relocation_count == 0)
590 return false;
591
592 if (!i915_is_ggtt(vma->vm))
593 return false;
594
595 /* See also use_cpu_reloc() */
596 if (HAS_LLC(vma->obj->base.dev))
597 return false;
598
599 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
600 return false;
601
602 return true;
603}
604
605static bool
606eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200607{
608 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
609 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond23db882014-05-23 08:48:08 +0200610
Chris Wilsone6a84462014-08-11 12:00:12 +0200611 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
Chris Wilsond23db882014-05-23 08:48:08 +0200612 !i915_is_ggtt(vma->vm));
613
614 if (entry->alignment &&
615 vma->node.start & (entry->alignment - 1))
616 return true;
617
Chris Wilsone6a84462014-08-11 12:00:12 +0200618 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
Chris Wilsond23db882014-05-23 08:48:08 +0200619 return true;
620
621 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
622 vma->node.start < BATCH_OFFSET_BIAS)
623 return true;
624
625 return false;
626}
627
Chris Wilson54cf91d2010-11-25 18:00:26 +0000628static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100629i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200630 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100631 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000632{
Chris Wilson432e58e2010-11-25 19:32:06 +0000633 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200634 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700635 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200636 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100637 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
638 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000639
Ben Widawsky68c8c172013-09-11 14:57:50 -0700640 if (list_empty(vmas))
641 return 0;
642
Chris Wilson227f7822014-05-15 10:41:42 +0100643 i915_gem_retire_requests_ring(ring);
644
Ben Widawsky68c8c172013-09-11 14:57:50 -0700645 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
646
Ben Widawsky27173f12013-08-14 11:38:36 +0200647 INIT_LIST_HEAD(&ordered_vmas);
648 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000649 struct drm_i915_gem_exec_object2 *entry;
650 bool need_fence, need_mappable;
651
Ben Widawsky27173f12013-08-14 11:38:36 +0200652 vma = list_first_entry(vmas, struct i915_vma, exec_list);
653 obj = vma->obj;
654 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000655
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100656 if (!has_fenced_gpu_access)
657 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000658 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000659 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
660 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200661 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000662
Chris Wilsone6a84462014-08-11 12:00:12 +0200663 if (need_mappable) {
664 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200665 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200666 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200667 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000668
Daniel Vettered5982e2013-01-17 22:23:36 +0100669 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000670 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000671 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200672 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000673
674 /* Attempt to pin all of the buffers into the GTT.
675 * This is done in 3 phases:
676 *
677 * 1a. Unbind all objects that do not match the GTT constraints for
678 * the execbuffer (fenceable, mappable, alignment etc).
679 * 1b. Increment pin count for already bound objects.
680 * 2. Bind new objects.
681 * 3. Decrement pin count.
682 *
Chris Wilson7788a762012-08-24 19:18:18 +0100683 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000684 * room for the earlier objects *unless* we need to defragment.
685 */
686 retry = 0;
687 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100688 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000689
690 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200691 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200692 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000693 continue;
694
Chris Wilsone6a84462014-08-11 12:00:12 +0200695 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200696 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000697 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200698 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000699 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000700 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000701 }
702
703 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200704 list_for_each_entry(vma, vmas, exec_list) {
705 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100706 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000707
Ben Widawsky27173f12013-08-14 11:38:36 +0200708 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100709 if (ret)
710 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000711 }
712
Chris Wilsona415d352013-11-26 11:23:15 +0000713err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200714 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000715 return ret;
716
Chris Wilsona415d352013-11-26 11:23:15 +0000717 /* Decrement pin count for bound objects */
718 list_for_each_entry(vma, vmas, exec_list)
719 i915_gem_execbuffer_unreserve_vma(vma);
720
Ben Widawsky68c8c172013-09-11 14:57:50 -0700721 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000722 if (ret)
723 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000724 } while (1);
725}
726
727static int
728i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100729 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000730 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100731 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200732 struct eb_vmas *eb,
733 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000734{
735 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200736 struct i915_address_space *vm;
737 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100738 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000739 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000740 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200741 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000742
Ben Widawsky27173f12013-08-14 11:38:36 +0200743 if (WARN_ON(list_empty(&eb->vmas)))
744 return 0;
745
746 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
747
Chris Wilson67731b82010-12-08 10:38:14 +0000748 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200749 while (!list_empty(&eb->vmas)) {
750 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
751 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000752 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200753 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000754 }
755
Chris Wilson54cf91d2010-11-25 18:00:26 +0000756 mutex_unlock(&dev->struct_mutex);
757
758 total = 0;
759 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000760 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000761
Chris Wilsondd6864a2011-01-12 23:49:13 +0000762 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000763 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000764 if (reloc == NULL || reloc_offset == NULL) {
765 drm_free_large(reloc);
766 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000767 mutex_lock(&dev->struct_mutex);
768 return -ENOMEM;
769 }
770
771 total = 0;
772 for (i = 0; i < count; i++) {
773 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000774 u64 invalid_offset = (u64)-1;
775 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000776
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200777 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000778
779 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000780 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000781 ret = -EFAULT;
782 mutex_lock(&dev->struct_mutex);
783 goto err;
784 }
785
Chris Wilson262b6d32013-01-15 16:17:54 +0000786 /* As we do not update the known relocation offsets after
787 * relocating (due to the complexities in lock handling),
788 * we need to mark them as invalid now so that we force the
789 * relocation processing next time. Just in case the target
790 * object is evicted and then rebound into its old
791 * presumed_offset before the next execbuffer - if that
792 * happened we would make the mistake of assuming that the
793 * relocations were valid.
794 */
795 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100796 if (__copy_to_user(&user_relocs[j].presumed_offset,
797 &invalid_offset,
798 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000799 ret = -EFAULT;
800 mutex_lock(&dev->struct_mutex);
801 goto err;
802 }
803 }
804
Chris Wilsondd6864a2011-01-12 23:49:13 +0000805 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000806 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000807 }
808
809 ret = i915_mutex_lock_interruptible(dev);
810 if (ret) {
811 mutex_lock(&dev->struct_mutex);
812 goto err;
813 }
814
Chris Wilson67731b82010-12-08 10:38:14 +0000815 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000816 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200817 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000818 if (ret)
819 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000820
Daniel Vettered5982e2013-01-17 22:23:36 +0100821 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200822 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000823 if (ret)
824 goto err;
825
Ben Widawsky27173f12013-08-14 11:38:36 +0200826 list_for_each_entry(vma, &eb->vmas, exec_list) {
827 int offset = vma->exec_entry - exec;
828 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
829 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000830 if (ret)
831 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000832 }
833
834 /* Leave the user relocations as are, this is the painfully slow path,
835 * and we want to avoid the complication of dropping the lock whilst
836 * having buffers reserved in the aperture and so causing spurious
837 * ENOSPC for random operations.
838 */
839
840err:
841 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000842 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000843 return ret;
844}
845
Chris Wilson54cf91d2010-11-25 18:00:26 +0000846static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100847i915_gem_execbuffer_move_to_gpu(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200848 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000849{
Ben Widawsky27173f12013-08-14 11:38:36 +0200850 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200851 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100852 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000853 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000854
Ben Widawsky27173f12013-08-14 11:38:36 +0200855 list_for_each_entry(vma, vmas, exec_list) {
856 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700857 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000858 if (ret)
859 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200860
861 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100862 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200863
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200864 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000865 }
866
Chris Wilson000433b2013-08-08 14:41:09 +0100867 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800868 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200869
870 if (flush_domains & I915_GEM_DOMAIN_GTT)
871 wmb();
872
Chris Wilson09cf7c92012-07-13 14:14:08 +0100873 /* Unconditionally invalidate gpu caches and ensure that we do flush
874 * any residual writes from the previous batch.
875 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100876 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000877}
878
Chris Wilson432e58e2010-11-25 19:32:06 +0000879static bool
880i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000881{
Daniel Vettered5982e2013-01-17 22:23:36 +0100882 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
883 return false;
884
Chris Wilson432e58e2010-11-25 19:32:06 +0000885 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000886}
887
888static int
889validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
890 int count)
891{
892 int i;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200893 unsigned relocs_total = 0;
894 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000895
896 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200897 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000898 int length; /* limited by fault_in_pages_readable() */
899
Daniel Vettered5982e2013-01-17 22:23:36 +0100900 if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
901 return -EINVAL;
902
Kees Cook3118a4f2013-03-11 17:31:45 -0700903 /* First check for malicious input causing overflow in
904 * the worst case where we need to allocate the entire
905 * relocation tree as a single array.
906 */
907 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000908 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700909 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000910
911 length = exec[i].relocation_count *
912 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700913 /*
914 * We must check that the entire relocation array is safe
915 * to read, but since we may need to update the presumed
916 * offsets during execution, check for full write access.
917 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000918 if (!access_ok(VERIFY_WRITE, ptr, length))
919 return -EFAULT;
920
Jani Nikulad330a952014-01-21 11:24:25 +0200921 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +0800922 if (fault_in_multipages_readable(ptr, length))
923 return -EFAULT;
924 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000925 }
926
927 return 0;
928}
929
Oscar Mateo273497e2014-05-22 14:13:37 +0100930static struct intel_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200931i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100932 struct intel_engine_cs *ring, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200933{
Oscar Mateo273497e2014-05-22 14:13:37 +0100934 struct intel_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200935 struct i915_ctx_hang_stats *hs;
936
Oscar Mateo821d66d2014-07-03 16:28:00 +0100937 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +0100938 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200939
Ben Widawsky41bde552013-12-06 14:11:21 -0800940 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -1000941 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -0800942 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200943
Ben Widawsky41bde552013-12-06 14:11:21 -0800944 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200945 if (hs->banned) {
946 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -0800947 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200948 }
949
Ben Widawsky41bde552013-12-06 14:11:21 -0800950 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200951}
952
Chris Wilson432e58e2010-11-25 19:32:06 +0000953static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200954i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100955 struct intel_engine_cs *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000956{
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100957 u32 seqno = intel_ring_get_seqno(ring);
Ben Widawsky27173f12013-08-14 11:38:36 +0200958 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000959
Ben Widawsky27173f12013-08-14 11:38:36 +0200960 list_for_each_entry(vma, vmas, exec_list) {
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100961 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Ben Widawsky27173f12013-08-14 11:38:36 +0200962 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100963 u32 old_read = obj->base.read_domains;
964 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000965
Chris Wilson432e58e2010-11-25 19:32:06 +0000966 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100967 if (obj->base.write_domain == 0)
968 obj->base.pending_read_domains |= obj->base.read_domains;
969 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000970
Ben Widawskye2d05a82013-09-24 09:57:58 -0700971 i915_vma_move_to_active(vma, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000972 if (obj->base.write_domain) {
973 obj->dirty = 1;
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100974 obj->last_write_seqno = seqno;
Daniel Vetterf99d7062014-06-19 16:01:59 +0200975
976 intel_fb_obj_invalidate(obj, ring);
Chris Wilsonc8725f32014-03-17 12:21:55 +0000977
978 /* update for the implicit flush after a batch */
979 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson432e58e2010-11-25 19:32:06 +0000980 }
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100981 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
982 obj->last_fenced_seqno = seqno;
983 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
984 struct drm_i915_private *dev_priv = to_i915(ring->dev);
985 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
986 &dev_priv->mm.fence_list);
987 }
988 }
Chris Wilson432e58e2010-11-25 19:32:06 +0000989
Chris Wilsondb53a302011-02-03 11:57:46 +0000990 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000991 }
992}
993
Chris Wilson54cf91d2010-11-25 18:00:26 +0000994static void
995i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000996 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100997 struct intel_engine_cs *ring,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300998 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000999{
Daniel Vettercc889e02012-06-13 20:45:19 +02001000 /* Unconditionally force add_request to emit a full flush. */
1001 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001002
Chris Wilson432e58e2010-11-25 19:32:06 +00001003 /* Add a breadcrumb for the completion of the batch buffer */
Mika Kuoppala7d736f42013-06-12 15:01:39 +03001004 (void)__i915_add_request(ring, file, obj, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +00001005}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001006
1007static int
Eric Anholtae662d32012-01-03 09:23:29 -08001008i915_reset_gen7_sol_offsets(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001009 struct intel_engine_cs *ring)
Eric Anholtae662d32012-01-03 09:23:29 -08001010{
Jani Nikula50227e12014-03-31 14:27:21 +03001011 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtae662d32012-01-03 09:23:29 -08001012 int ret, i;
1013
Daniel Vetter9d662da2014-04-24 08:09:09 +02001014 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
1015 DRM_DEBUG("sol reset is gen7/rcs only\n");
1016 return -EINVAL;
1017 }
Eric Anholtae662d32012-01-03 09:23:29 -08001018
1019 ret = intel_ring_begin(ring, 4 * 3);
1020 if (ret)
1021 return ret;
1022
1023 for (i = 0; i < 4; i++) {
1024 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1025 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
1026 intel_ring_emit(ring, 0);
1027 }
1028
1029 intel_ring_advance(ring);
1030
1031 return 0;
1032}
1033
Oscar Mateo78382592014-07-03 16:28:05 +01001034static int
1035legacy_ringbuffer_submission(struct drm_device *dev, struct drm_file *file,
1036 struct intel_engine_cs *ring,
1037 struct intel_context *ctx,
1038 struct drm_i915_gem_execbuffer2 *args,
1039 struct list_head *vmas,
1040 struct drm_i915_gem_object *batch_obj,
1041 u64 exec_start, u32 flags)
1042{
1043 struct drm_clip_rect *cliprects = NULL;
1044 struct drm_i915_private *dev_priv = dev->dev_private;
1045 u64 exec_len;
1046 int instp_mode;
1047 u32 instp_mask;
1048 int i, ret = 0;
1049
1050 if (args->num_cliprects != 0) {
1051 if (ring != &dev_priv->ring[RCS]) {
1052 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
1053 return -EINVAL;
1054 }
1055
1056 if (INTEL_INFO(dev)->gen >= 5) {
1057 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1058 return -EINVAL;
1059 }
1060
1061 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1062 DRM_DEBUG("execbuf with %u cliprects\n",
1063 args->num_cliprects);
1064 return -EINVAL;
1065 }
1066
1067 cliprects = kcalloc(args->num_cliprects,
1068 sizeof(*cliprects),
1069 GFP_KERNEL);
1070 if (cliprects == NULL) {
1071 ret = -ENOMEM;
1072 goto error;
1073 }
1074
1075 if (copy_from_user(cliprects,
1076 to_user_ptr(args->cliprects_ptr),
1077 sizeof(*cliprects)*args->num_cliprects)) {
1078 ret = -EFAULT;
1079 goto error;
1080 }
1081 } else {
1082 if (args->DR4 == 0xffffffff) {
1083 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1084 args->DR4 = 0;
1085 }
1086
1087 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
1088 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
1089 return -EINVAL;
1090 }
1091 }
1092
1093 ret = i915_gem_execbuffer_move_to_gpu(ring, vmas);
1094 if (ret)
1095 goto error;
1096
1097 ret = i915_switch_context(ring, ctx);
1098 if (ret)
1099 goto error;
1100
1101 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1102 instp_mask = I915_EXEC_CONSTANTS_MASK;
1103 switch (instp_mode) {
1104 case I915_EXEC_CONSTANTS_REL_GENERAL:
1105 case I915_EXEC_CONSTANTS_ABSOLUTE:
1106 case I915_EXEC_CONSTANTS_REL_SURFACE:
1107 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
1108 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
1109 ret = -EINVAL;
1110 goto error;
1111 }
1112
1113 if (instp_mode != dev_priv->relative_constants_mode) {
1114 if (INTEL_INFO(dev)->gen < 4) {
1115 DRM_DEBUG("no rel constants on pre-gen4\n");
1116 ret = -EINVAL;
1117 goto error;
1118 }
1119
1120 if (INTEL_INFO(dev)->gen > 5 &&
1121 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1122 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
1123 ret = -EINVAL;
1124 goto error;
1125 }
1126
1127 /* The HW changed the meaning on this bit on gen6 */
1128 if (INTEL_INFO(dev)->gen >= 6)
1129 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1130 }
1131 break;
1132 default:
1133 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
1134 ret = -EINVAL;
1135 goto error;
1136 }
1137
1138 if (ring == &dev_priv->ring[RCS] &&
1139 instp_mode != dev_priv->relative_constants_mode) {
1140 ret = intel_ring_begin(ring, 4);
1141 if (ret)
1142 goto error;
1143
1144 intel_ring_emit(ring, MI_NOOP);
1145 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1146 intel_ring_emit(ring, INSTPM);
1147 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1148 intel_ring_advance(ring);
1149
1150 dev_priv->relative_constants_mode = instp_mode;
1151 }
1152
1153 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1154 ret = i915_reset_gen7_sol_offsets(dev, ring);
1155 if (ret)
1156 goto error;
1157 }
1158
1159 exec_len = args->batch_len;
1160 if (cliprects) {
1161 for (i = 0; i < args->num_cliprects; i++) {
1162 ret = i915_emit_box(dev, &cliprects[i],
1163 args->DR1, args->DR4);
1164 if (ret)
1165 goto error;
1166
1167 ret = ring->dispatch_execbuffer(ring,
1168 exec_start, exec_len,
1169 flags);
1170 if (ret)
1171 goto error;
1172 }
1173 } else {
1174 ret = ring->dispatch_execbuffer(ring,
1175 exec_start, exec_len,
1176 flags);
1177 if (ret)
1178 return ret;
1179 }
1180
1181 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1182
1183 i915_gem_execbuffer_move_to_active(vmas, ring);
1184 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
1185
1186error:
1187 kfree(cliprects);
1188 return ret;
1189}
1190
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001191/**
1192 * Find one BSD ring to dispatch the corresponding BSD command.
1193 * The Ring ID is returned.
1194 */
1195static int gen8_dispatch_bsd_ring(struct drm_device *dev,
1196 struct drm_file *file)
1197{
1198 struct drm_i915_private *dev_priv = dev->dev_private;
1199 struct drm_i915_file_private *file_priv = file->driver_priv;
1200
1201 /* Check whether the file_priv is using one ring */
1202 if (file_priv->bsd_ring)
1203 return file_priv->bsd_ring->id;
1204 else {
1205 /* If no, use the ping-pong mechanism to select one ring */
1206 int ring_id;
1207
1208 mutex_lock(&dev->struct_mutex);
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001209 if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001210 ring_id = VCS;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001211 dev_priv->mm.bsd_ring_dispatch_index = 1;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001212 } else {
1213 ring_id = VCS2;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001214 dev_priv->mm.bsd_ring_dispatch_index = 0;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001215 }
1216 file_priv->bsd_ring = &dev_priv->ring[ring_id];
1217 mutex_unlock(&dev->struct_mutex);
1218 return ring_id;
1219 }
1220}
1221
Chris Wilsond23db882014-05-23 08:48:08 +02001222static struct drm_i915_gem_object *
1223eb_get_batch(struct eb_vmas *eb)
1224{
1225 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
1226
1227 /*
1228 * SNA is doing fancy tricks with compressing batch buffers, which leads
1229 * to negative relocation deltas. Usually that works out ok since the
1230 * relocate address is still positive, except when the batch is placed
1231 * very low in the GTT. Ensure this doesn't happen.
1232 *
1233 * Note that actual hangs have only been observed on gen7, but for
1234 * paranoia do it everywhere.
1235 */
1236 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
1237
1238 return vma->obj;
1239}
1240
Eric Anholtae662d32012-01-03 09:23:29 -08001241static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001242i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1243 struct drm_file *file,
1244 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001245 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001246{
Jani Nikula50227e12014-03-31 14:27:21 +03001247 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +02001248 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001249 struct drm_i915_gem_object *batch_obj;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001250 struct intel_engine_cs *ring;
Oscar Mateo273497e2014-05-22 14:13:37 +01001251 struct intel_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001252 struct i915_address_space *vm;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001253 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Oscar Mateo78382592014-07-03 16:28:05 +01001254 u64 exec_start = args->batch_start_offset;
1255 u32 flags;
1256 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001257 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001258
Daniel Vettered5982e2013-01-17 22:23:36 +01001259 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001260 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001261
1262 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001263 if (ret)
1264 return ret;
1265
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001266 flags = 0;
1267 if (args->flags & I915_EXEC_SECURE) {
1268 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1269 return -EPERM;
1270
1271 flags |= I915_DISPATCH_SECURE;
1272 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001273 if (args->flags & I915_EXEC_IS_PINNED)
1274 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001275
Zhao Yakuib1a93302014-04-17 10:37:36 +08001276 if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
Daniel Vetterff240192012-01-31 21:08:14 +01001277 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001278 (int)(args->flags & I915_EXEC_RING_MASK));
1279 return -EINVAL;
1280 }
Ben Widawskyca01b122013-12-06 14:11:00 -08001281
1282 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1283 ring = &dev_priv->ring[RCS];
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001284 else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
1285 if (HAS_BSD2(dev)) {
1286 int ring_id;
1287 ring_id = gen8_dispatch_bsd_ring(dev, file);
1288 ring = &dev_priv->ring[ring_id];
1289 } else
1290 ring = &dev_priv->ring[VCS];
1291 } else
Ben Widawskyca01b122013-12-06 14:11:00 -08001292 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1293
Chris Wilsona15817c2012-05-11 14:29:31 +01001294 if (!intel_ring_initialized(ring)) {
1295 DRM_DEBUG("execbuf with invalid ring: %d\n",
1296 (int)(args->flags & I915_EXEC_RING_MASK));
1297 return -EINVAL;
1298 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001299
1300 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001301 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001302 return -EINVAL;
1303 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001304
Paulo Zanonif65c9162013-11-27 18:20:34 -02001305 intel_runtime_pm_get(dev_priv);
1306
Chris Wilson54cf91d2010-11-25 18:00:26 +00001307 ret = i915_mutex_lock_interruptible(dev);
1308 if (ret)
1309 goto pre_mutex_err;
1310
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001311 if (dev_priv->ums.mm_suspended) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001312 mutex_unlock(&dev->struct_mutex);
1313 ret = -EBUSY;
1314 goto pre_mutex_err;
1315 }
1316
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001317 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001318 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001319 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001320 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001321 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001322 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001323
1324 i915_gem_context_reference(ctx);
1325
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001326 vm = ctx->vm;
1327 if (!USES_FULL_PPGTT(dev))
1328 vm = &dev_priv->gtt.base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001329
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001330 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001331 if (eb == NULL) {
Ben Widawsky935f38d2014-04-04 22:41:07 -07001332 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001333 mutex_unlock(&dev->struct_mutex);
1334 ret = -ENOMEM;
1335 goto pre_mutex_err;
1336 }
1337
Chris Wilson54cf91d2010-11-25 18:00:26 +00001338 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001339 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001340 if (ret)
1341 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001342
Chris Wilson6fe4f142011-01-10 17:35:37 +00001343 /* take note of the batch buffer before we might reorder the lists */
Chris Wilsond23db882014-05-23 08:48:08 +02001344 batch_obj = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001345
Chris Wilson54cf91d2010-11-25 18:00:26 +00001346 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001347 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001348 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001349 if (ret)
1350 goto err;
1351
1352 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001353 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001354 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001355 if (ret) {
1356 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001357 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001358 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001359 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1360 }
1361 if (ret)
1362 goto err;
1363 }
1364
1365 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001366 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001367 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001368 ret = -EINVAL;
1369 goto err;
1370 }
1371 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1372
Brad Volkin351e3db2014-02-18 10:15:46 -08001373 if (i915_needs_cmd_parser(ring)) {
1374 ret = i915_parse_cmds(ring,
1375 batch_obj,
1376 args->batch_start_offset,
1377 file->is_master);
1378 if (ret)
1379 goto err;
1380
1381 /*
1382 * XXX: Actually do this when enabling batch copy...
1383 *
1384 * Set the DISPATCH_SECURE bit to remove the NON_SECURE bit
1385 * from MI_BATCH_BUFFER_START commands issued in the
1386 * dispatch_execbuffer implementations. We specifically don't
1387 * want that set when the command parser is enabled.
1388 */
1389 }
1390
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001391 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1392 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001393 * hsw should have this fixed, but bdw mucks it up again. */
Ben Widawsky6f65e292013-12-06 14:10:56 -08001394 if (flags & I915_DISPATCH_SECURE &&
1395 !batch_obj->has_global_gtt_mapping) {
1396 /* When we have multiple VMs, we'll need to make sure that we
1397 * allocate space first */
1398 struct i915_vma *vma = i915_gem_obj_to_ggtt(batch_obj);
1399 BUG_ON(!vma);
1400 vma->bind_vma(vma, batch_obj->cache_level, GLOBAL_BIND);
1401 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001402
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001403 if (flags & I915_DISPATCH_SECURE)
1404 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
1405 else
1406 exec_start += i915_gem_obj_offset(batch_obj, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001407
Oscar Mateo78382592014-07-03 16:28:05 +01001408 ret = legacy_ringbuffer_submission(dev, file, ring, ctx,
1409 args, &eb->vmas, batch_obj, exec_start, flags);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001410 if (ret)
1411 goto err;
1412
Chris Wilson54cf91d2010-11-25 18:00:26 +00001413err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001414 /* the request owns the ref now */
1415 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001416 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001417
1418 mutex_unlock(&dev->struct_mutex);
1419
1420pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001421 /* intel_gpu_busy should also get a ref, so it will free when the device
1422 * is really idle. */
1423 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001424 return ret;
1425}
1426
1427/*
1428 * Legacy execbuffer just creates an exec2 list from the original exec object
1429 * list array and passes it to the real function.
1430 */
1431int
1432i915_gem_execbuffer(struct drm_device *dev, void *data,
1433 struct drm_file *file)
1434{
1435 struct drm_i915_gem_execbuffer *args = data;
1436 struct drm_i915_gem_execbuffer2 exec2;
1437 struct drm_i915_gem_exec_object *exec_list = NULL;
1438 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1439 int ret, i;
1440
Chris Wilson54cf91d2010-11-25 18:00:26 +00001441 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001442 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001443 return -EINVAL;
1444 }
1445
1446 /* Copy in the exec list from userland */
1447 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1448 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1449 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001450 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001451 args->buffer_count);
1452 drm_free_large(exec_list);
1453 drm_free_large(exec2_list);
1454 return -ENOMEM;
1455 }
1456 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001457 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001458 sizeof(*exec_list) * args->buffer_count);
1459 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001460 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001461 args->buffer_count, ret);
1462 drm_free_large(exec_list);
1463 drm_free_large(exec2_list);
1464 return -EFAULT;
1465 }
1466
1467 for (i = 0; i < args->buffer_count; i++) {
1468 exec2_list[i].handle = exec_list[i].handle;
1469 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1470 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1471 exec2_list[i].alignment = exec_list[i].alignment;
1472 exec2_list[i].offset = exec_list[i].offset;
1473 if (INTEL_INFO(dev)->gen < 4)
1474 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1475 else
1476 exec2_list[i].flags = 0;
1477 }
1478
1479 exec2.buffers_ptr = args->buffers_ptr;
1480 exec2.buffer_count = args->buffer_count;
1481 exec2.batch_start_offset = args->batch_start_offset;
1482 exec2.batch_len = args->batch_len;
1483 exec2.DR1 = args->DR1;
1484 exec2.DR4 = args->DR4;
1485 exec2.num_cliprects = args->num_cliprects;
1486 exec2.cliprects_ptr = args->cliprects_ptr;
1487 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001488 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001489
Ben Widawsky41bde552013-12-06 14:11:21 -08001490 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001491 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001492 struct drm_i915_gem_exec_object __user *user_exec_list =
1493 to_user_ptr(args->buffers_ptr);
1494
Chris Wilson54cf91d2010-11-25 18:00:26 +00001495 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001496 for (i = 0; i < args->buffer_count; i++) {
1497 ret = __copy_to_user(&user_exec_list[i].offset,
1498 &exec2_list[i].offset,
1499 sizeof(user_exec_list[i].offset));
1500 if (ret) {
1501 ret = -EFAULT;
1502 DRM_DEBUG("failed to copy %d exec entries "
1503 "back to user (%d)\n",
1504 args->buffer_count, ret);
1505 break;
1506 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001507 }
1508 }
1509
1510 drm_free_large(exec_list);
1511 drm_free_large(exec2_list);
1512 return ret;
1513}
1514
1515int
1516i915_gem_execbuffer2(struct drm_device *dev, void *data,
1517 struct drm_file *file)
1518{
1519 struct drm_i915_gem_execbuffer2 *args = data;
1520 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1521 int ret;
1522
Xi Wanged8cd3b2012-04-23 04:06:41 -04001523 if (args->buffer_count < 1 ||
1524 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001525 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001526 return -EINVAL;
1527 }
1528
Daniel Vetter9cb34662014-04-24 08:09:11 +02001529 if (args->rsvd2 != 0) {
1530 DRM_DEBUG("dirty rvsd2 field\n");
1531 return -EINVAL;
1532 }
1533
Chris Wilson8408c282011-02-21 12:54:48 +00001534 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001535 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001536 if (exec2_list == NULL)
1537 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1538 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001539 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001540 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001541 args->buffer_count);
1542 return -ENOMEM;
1543 }
1544 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001545 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001546 sizeof(*exec2_list) * args->buffer_count);
1547 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001548 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001549 args->buffer_count, ret);
1550 drm_free_large(exec2_list);
1551 return -EFAULT;
1552 }
1553
Ben Widawsky41bde552013-12-06 14:11:21 -08001554 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001555 if (!ret) {
1556 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001557 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001558 to_user_ptr(args->buffers_ptr);
1559 int i;
1560
1561 for (i = 0; i < args->buffer_count; i++) {
1562 ret = __copy_to_user(&user_exec_list[i].offset,
1563 &exec2_list[i].offset,
1564 sizeof(user_exec_list[i].offset));
1565 if (ret) {
1566 ret = -EFAULT;
1567 DRM_DEBUG("failed to copy %d exec entries "
1568 "back to user\n",
1569 args->buffer_count);
1570 break;
1571 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001572 }
1573 }
1574
1575 drm_free_large(exec2_list);
1576 return ret;
1577}