blob: 20fef6c502676b2c82bdd19cbe343f5f864b0ccc [file] [log] [blame]
Chris Wilson54cf91d2010-11-25 18:00:26 +00001/*
2 * Copyright © 2008,2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
26 *
27 */
28
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
30#include <drm/i915_drm.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000031#include "i915_drv.h"
32#include "i915_trace.h"
33#include "intel_drv.h"
Eugeni Dodonovf45b5552011-12-09 17:16:37 -080034#include <linux/dma_remapping.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000035
Chris Wilsona415d352013-11-26 11:23:15 +000036#define __EXEC_OBJECT_HAS_PIN (1<<31)
37#define __EXEC_OBJECT_HAS_FENCE (1<<30)
Chris Wilsond23db882014-05-23 08:48:08 +020038#define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
39
40#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000041
Ben Widawsky27173f12013-08-14 11:38:36 +020042struct eb_vmas {
43 struct list_head vmas;
Chris Wilson67731b82010-12-08 10:38:14 +000044 int and;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000045 union {
Ben Widawsky27173f12013-08-14 11:38:36 +020046 struct i915_vma *lut[0];
Chris Wilsoneef90cc2013-01-08 10:53:17 +000047 struct hlist_head buckets[0];
48 };
Chris Wilson67731b82010-12-08 10:38:14 +000049};
50
Ben Widawsky27173f12013-08-14 11:38:36 +020051static struct eb_vmas *
Ben Widawsky17601cbc2013-11-25 09:54:38 -080052eb_create(struct drm_i915_gem_execbuffer2 *args)
Chris Wilson67731b82010-12-08 10:38:14 +000053{
Ben Widawsky27173f12013-08-14 11:38:36 +020054 struct eb_vmas *eb = NULL;
Chris Wilson67731b82010-12-08 10:38:14 +000055
Chris Wilsoneef90cc2013-01-08 10:53:17 +000056 if (args->flags & I915_EXEC_HANDLE_LUT) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020057 unsigned size = args->buffer_count;
Ben Widawsky27173f12013-08-14 11:38:36 +020058 size *= sizeof(struct i915_vma *);
59 size += sizeof(struct eb_vmas);
Chris Wilsoneef90cc2013-01-08 10:53:17 +000060 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
61 }
62
63 if (eb == NULL) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020064 unsigned size = args->buffer_count;
65 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Lauri Kasanen27b7c632013-03-27 15:04:55 +020066 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
Chris Wilsoneef90cc2013-01-08 10:53:17 +000067 while (count > 2*size)
68 count >>= 1;
69 eb = kzalloc(count*sizeof(struct hlist_head) +
Ben Widawsky27173f12013-08-14 11:38:36 +020070 sizeof(struct eb_vmas),
Chris Wilsoneef90cc2013-01-08 10:53:17 +000071 GFP_TEMPORARY);
72 if (eb == NULL)
73 return eb;
74
75 eb->and = count - 1;
76 } else
77 eb->and = -args->buffer_count;
78
Ben Widawsky27173f12013-08-14 11:38:36 +020079 INIT_LIST_HEAD(&eb->vmas);
Chris Wilson67731b82010-12-08 10:38:14 +000080 return eb;
81}
82
83static void
Ben Widawsky27173f12013-08-14 11:38:36 +020084eb_reset(struct eb_vmas *eb)
Chris Wilson67731b82010-12-08 10:38:14 +000085{
Chris Wilsoneef90cc2013-01-08 10:53:17 +000086 if (eb->and >= 0)
87 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
Chris Wilson67731b82010-12-08 10:38:14 +000088}
89
Chris Wilson3b96eff2013-01-08 10:53:14 +000090static int
Ben Widawsky27173f12013-08-14 11:38:36 +020091eb_lookup_vmas(struct eb_vmas *eb,
92 struct drm_i915_gem_exec_object2 *exec,
93 const struct drm_i915_gem_execbuffer2 *args,
94 struct i915_address_space *vm,
95 struct drm_file *file)
Chris Wilson3b96eff2013-01-08 10:53:14 +000096{
Ben Widawsky6f65e292013-12-06 14:10:56 -080097 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +020098 struct drm_i915_gem_object *obj;
99 struct list_head objects;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000100 int i, ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000101
Ben Widawsky27173f12013-08-14 11:38:36 +0200102 INIT_LIST_HEAD(&objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000103 spin_lock(&file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +0200104 /* Grab a reference to the object and release the lock so we can lookup
105 * or create the VMA without using GFP_ATOMIC */
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000106 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000107 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
108 if (obj == NULL) {
109 spin_unlock(&file->table_lock);
110 DRM_DEBUG("Invalid object handle %d at index %d\n",
111 exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200112 ret = -ENOENT;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000113 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000114 }
115
Ben Widawsky27173f12013-08-14 11:38:36 +0200116 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000117 spin_unlock(&file->table_lock);
118 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
119 obj, exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200120 ret = -EINVAL;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000121 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000122 }
123
124 drm_gem_object_reference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200125 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000126 }
127 spin_unlock(&file->table_lock);
128
Ben Widawsky27173f12013-08-14 11:38:36 +0200129 i = 0;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000130 while (!list_empty(&objects)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200131 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800132 struct i915_address_space *bind_vm = vm;
133
Daniel Vetter2c9f8d52013-12-18 17:38:53 +0100134 if (exec[i].flags & EXEC_OBJECT_NEEDS_GTT &&
135 USES_FULL_PPGTT(vm->dev)) {
136 ret = -EINVAL;
Rodrigo Vivia25eebb2014-01-14 16:21:49 -0200137 goto err;
Daniel Vetter2c9f8d52013-12-18 17:38:53 +0100138 }
139
Ben Widawsky6f65e292013-12-06 14:10:56 -0800140 /* If we have secure dispatch, or the userspace assures us that
141 * they know what they're doing, use the GGTT VM.
142 */
Daniel Vettera7c1d4262013-12-18 17:46:18 +0100143 if (((args->flags & I915_EXEC_SECURE) &&
Ben Widawsky6f65e292013-12-06 14:10:56 -0800144 (i == (args->buffer_count - 1))))
145 bind_vm = &dev_priv->gtt.base;
Ben Widawsky27173f12013-08-14 11:38:36 +0200146
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000147 obj = list_first_entry(&objects,
148 struct drm_i915_gem_object,
149 obj_exec_link);
150
Daniel Vettere656a6c2013-08-14 14:14:04 +0200151 /*
152 * NOTE: We can leak any vmas created here when something fails
153 * later on. But that's no issue since vma_unbind can deal with
154 * vmas which are not actually bound. And since only
155 * lookup_or_create exists as an interface to get at the vma
156 * from the (obj, vm) we don't run the risk of creating
157 * duplicated vmas for the same vm.
158 */
Ben Widawsky6f65e292013-12-06 14:10:56 -0800159 vma = i915_gem_obj_lookup_or_create_vma(obj, bind_vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200160 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200161 DRM_DEBUG("Failed to lookup VMA\n");
162 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000163 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200164 }
165
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000166 /* Transfer ownership from the objects list to the vmas list. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200167 list_add_tail(&vma->exec_list, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000168 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200169
170 vma->exec_entry = &exec[i];
171 if (eb->and < 0) {
172 eb->lut[i] = vma;
173 } else {
174 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
175 vma->exec_handle = handle;
176 hlist_add_head(&vma->exec_node,
177 &eb->buckets[handle & eb->and]);
178 }
179 ++i;
180 }
181
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000182 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200183
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000184
185err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200186 while (!list_empty(&objects)) {
187 obj = list_first_entry(&objects,
188 struct drm_i915_gem_object,
189 obj_exec_link);
190 list_del_init(&obj->obj_exec_link);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000191 drm_gem_object_unreference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200192 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000193 /*
194 * Objects already transfered to the vmas list will be unreferenced by
195 * eb_destroy.
196 */
197
Ben Widawsky27173f12013-08-14 11:38:36 +0200198 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000199}
200
Ben Widawsky27173f12013-08-14 11:38:36 +0200201static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000202{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000203 if (eb->and < 0) {
204 if (handle >= -eb->and)
205 return NULL;
206 return eb->lut[handle];
207 } else {
208 struct hlist_head *head;
209 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000210
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000211 head = &eb->buckets[handle & eb->and];
212 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200213 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000214
Ben Widawsky27173f12013-08-14 11:38:36 +0200215 vma = hlist_entry(node, struct i915_vma, exec_node);
216 if (vma->exec_handle == handle)
217 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000218 }
219 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000220 }
Chris Wilson67731b82010-12-08 10:38:14 +0000221}
222
Chris Wilsona415d352013-11-26 11:23:15 +0000223static void
224i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
225{
226 struct drm_i915_gem_exec_object2 *entry;
227 struct drm_i915_gem_object *obj = vma->obj;
228
229 if (!drm_mm_node_allocated(&vma->node))
230 return;
231
232 entry = vma->exec_entry;
233
234 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
235 i915_gem_object_unpin_fence(obj);
236
237 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Daniel Vetter3d7f0f92013-12-18 16:23:37 +0100238 vma->pin_count--;
Chris Wilsona415d352013-11-26 11:23:15 +0000239
240 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
241}
242
243static void eb_destroy(struct eb_vmas *eb)
244{
Ben Widawsky27173f12013-08-14 11:38:36 +0200245 while (!list_empty(&eb->vmas)) {
246 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000247
Ben Widawsky27173f12013-08-14 11:38:36 +0200248 vma = list_first_entry(&eb->vmas,
249 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000250 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200251 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000252 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200253 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000254 }
Chris Wilson67731b82010-12-08 10:38:14 +0000255 kfree(eb);
256}
257
Chris Wilsondabdfe02012-03-26 10:10:27 +0200258static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
259{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300260 return (HAS_LLC(obj->base.dev) ||
261 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100262 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200263 obj->cache_level != I915_CACHE_NONE);
264}
265
Chris Wilson54cf91d2010-11-25 18:00:26 +0000266static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100267relocate_entry_cpu(struct drm_i915_gem_object *obj,
268 struct drm_i915_gem_relocation_entry *reloc)
269{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700270 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100271 uint32_t page_offset = offset_in_page(reloc->offset);
272 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800273 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100274
Chris Wilson2cc86b82013-08-26 19:51:00 -0300275 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100276 if (ret)
277 return ret;
278
279 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
280 reloc->offset >> PAGE_SHIFT));
281 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700282
283 if (INTEL_INFO(dev)->gen >= 8) {
284 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
285
286 if (page_offset == 0) {
287 kunmap_atomic(vaddr);
288 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
289 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
290 }
291
292 *(uint32_t *)(vaddr + page_offset) = 0;
293 }
294
Rafael Barbalho5032d872013-08-21 17:10:51 +0100295 kunmap_atomic(vaddr);
296
297 return 0;
298}
299
300static int
301relocate_entry_gtt(struct drm_i915_gem_object *obj,
302 struct drm_i915_gem_relocation_entry *reloc)
303{
304 struct drm_device *dev = obj->base.dev;
305 struct drm_i915_private *dev_priv = dev->dev_private;
306 uint32_t __iomem *reloc_entry;
307 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800308 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100309
310 ret = i915_gem_object_set_to_gtt_domain(obj, true);
311 if (ret)
312 return ret;
313
314 ret = i915_gem_object_put_fence(obj);
315 if (ret)
316 return ret;
317
318 /* Map the page containing the relocation we're going to perform. */
319 reloc->offset += i915_gem_obj_ggtt_offset(obj);
320 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
321 reloc->offset & PAGE_MASK);
322 reloc_entry = (uint32_t __iomem *)
323 (reloc_page + offset_in_page(reloc->offset));
324 iowrite32(reloc->delta, reloc_entry);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700325
326 if (INTEL_INFO(dev)->gen >= 8) {
327 reloc_entry += 1;
328
329 if (offset_in_page(reloc->offset + sizeof(uint32_t)) == 0) {
330 io_mapping_unmap_atomic(reloc_page);
331 reloc_page = io_mapping_map_atomic_wc(
332 dev_priv->gtt.mappable,
333 reloc->offset + sizeof(uint32_t));
334 reloc_entry = reloc_page;
335 }
336
337 iowrite32(0, reloc_entry);
338 }
339
Rafael Barbalho5032d872013-08-21 17:10:51 +0100340 io_mapping_unmap_atomic(reloc_page);
341
342 return 0;
343}
344
345static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000346i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200347 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800348 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000349{
350 struct drm_device *dev = obj->base.dev;
351 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100352 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200353 struct i915_vma *target_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000354 uint32_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800355 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000356
Chris Wilson67731b82010-12-08 10:38:14 +0000357 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200358 target_vma = eb_get_vma(eb, reloc->target_handle);
359 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000360 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200361 target_i915_obj = target_vma->obj;
362 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000363
Ben Widawsky5ce09722013-11-25 09:54:40 -0800364 target_offset = target_vma->node.start;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000365
Eric Anholte844b992012-07-31 15:35:01 -0700366 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
367 * pipe_control writes because the gpu doesn't properly redirect them
368 * through the ppgtt for non_secure batchbuffers. */
369 if (unlikely(IS_GEN6(dev) &&
370 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
371 !target_i915_obj->has_global_gtt_mapping)) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800372 struct i915_vma *vma =
373 list_first_entry(&target_i915_obj->vma_list,
374 typeof(*vma), vma_link);
Ben Widawsky6f65e292013-12-06 14:10:56 -0800375 vma->bind_vma(vma, target_i915_obj->cache_level, GLOBAL_BIND);
Eric Anholte844b992012-07-31 15:35:01 -0700376 }
377
Chris Wilson54cf91d2010-11-25 18:00:26 +0000378 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000379 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100380 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000381 "obj %p target %d offset %d "
382 "read %08x write %08x",
383 obj, reloc->target_handle,
384 (int) reloc->offset,
385 reloc->read_domains,
386 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800387 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000388 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100389 if (unlikely((reloc->write_domain | reloc->read_domains)
390 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100391 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000392 "obj %p target %d offset %d "
393 "read %08x write %08x",
394 obj, reloc->target_handle,
395 (int) reloc->offset,
396 reloc->read_domains,
397 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800398 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000399 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000400
401 target_obj->pending_read_domains |= reloc->read_domains;
402 target_obj->pending_write_domain |= reloc->write_domain;
403
404 /* If the relocation already has the right value in it, no
405 * more work needs to be done.
406 */
407 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000408 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000409
410 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700411 if (unlikely(reloc->offset >
412 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100413 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000414 "obj %p target %d offset %d size %d.\n",
415 obj, reloc->target_handle,
416 (int) reloc->offset,
417 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800418 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000419 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000420 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100421 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000422 "obj %p target %d offset %d.\n",
423 obj, reloc->target_handle,
424 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800425 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000426 }
427
Chris Wilsondabdfe02012-03-26 10:10:27 +0200428 /* We can't wait for rendering with pagefaults disabled */
429 if (obj->active && in_atomic())
430 return -EFAULT;
431
Chris Wilson54cf91d2010-11-25 18:00:26 +0000432 reloc->delta += target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100433 if (use_cpu_reloc(obj))
434 ret = relocate_entry_cpu(obj, reloc);
435 else
436 ret = relocate_entry_gtt(obj, reloc);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000437
Daniel Vetterd4d36012013-09-02 20:56:23 +0200438 if (ret)
439 return ret;
440
Chris Wilson54cf91d2010-11-25 18:00:26 +0000441 /* and update the user's relocation entry */
442 reloc->presumed_offset = target_offset;
443
Chris Wilson67731b82010-12-08 10:38:14 +0000444 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000445}
446
447static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200448i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
449 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000450{
Chris Wilson1d83f442012-03-24 20:12:53 +0000451#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
452 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000453 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200454 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000455 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000456
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200457 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000458
Chris Wilson1d83f442012-03-24 20:12:53 +0000459 remain = entry->relocation_count;
460 while (remain) {
461 struct drm_i915_gem_relocation_entry *r = stack_reloc;
462 int count = remain;
463 if (count > ARRAY_SIZE(stack_reloc))
464 count = ARRAY_SIZE(stack_reloc);
465 remain -= count;
466
467 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000468 return -EFAULT;
469
Chris Wilson1d83f442012-03-24 20:12:53 +0000470 do {
471 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000472
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800473 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000474 if (ret)
475 return ret;
476
477 if (r->presumed_offset != offset &&
478 __copy_to_user_inatomic(&user_relocs->presumed_offset,
479 &r->presumed_offset,
480 sizeof(r->presumed_offset))) {
481 return -EFAULT;
482 }
483
484 user_relocs++;
485 r++;
486 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000487 }
488
489 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000490#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000491}
492
493static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200494i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
495 struct eb_vmas *eb,
496 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000497{
Ben Widawsky27173f12013-08-14 11:38:36 +0200498 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000499 int i, ret;
500
501 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800502 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000503 if (ret)
504 return ret;
505 }
506
507 return 0;
508}
509
510static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800511i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000512{
Ben Widawsky27173f12013-08-14 11:38:36 +0200513 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000514 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000515
Chris Wilsond4aeee72011-03-14 15:11:24 +0000516 /* This is the fast path and we cannot handle a pagefault whilst
517 * holding the struct mutex lest the user pass in the relocations
518 * contained within a mmaped bo. For in such a case we, the page
519 * fault handler would call i915_gem_fault() and we would try to
520 * acquire the struct mutex again. Obviously this is bad and so
521 * lockdep complains vehemently.
522 */
523 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200524 list_for_each_entry(vma, &eb->vmas, exec_list) {
525 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000526 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000527 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000528 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000529 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000530
Chris Wilsond4aeee72011-03-14 15:11:24 +0000531 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000532}
533
Chris Wilson1690e1e2011-12-14 13:57:08 +0100534static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200535need_reloc_mappable(struct i915_vma *vma)
Chris Wilsondabdfe02012-03-26 10:10:27 +0200536{
Ben Widawsky27173f12013-08-14 11:38:36 +0200537 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
538 return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
539 i915_is_ggtt(vma->vm);
Chris Wilsondabdfe02012-03-26 10:10:27 +0200540}
541
542static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200543i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
544 struct intel_ring_buffer *ring,
545 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100546{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800547 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200548 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100549 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100550 bool need_fence;
Chris Wilsond23db882014-05-23 08:48:08 +0200551 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100552 int ret;
553
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100554 flags = 0;
555
Chris Wilson1690e1e2011-12-14 13:57:08 +0100556 need_fence =
557 has_fenced_gpu_access &&
558 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
559 obj->tiling_mode != I915_TILING_NONE;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100560 if (need_fence || need_reloc_mappable(vma))
561 flags |= PIN_MAPPABLE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100562
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100563 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
Daniel Vetterbf3d1492014-02-14 14:01:12 +0100564 flags |= PIN_GLOBAL;
Chris Wilsond23db882014-05-23 08:48:08 +0200565 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
566 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100567
568 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100569 if (ret)
570 return ret;
571
Chris Wilson7788a762012-08-24 19:18:18 +0100572 entry->flags |= __EXEC_OBJECT_HAS_PIN;
573
Chris Wilson1690e1e2011-12-14 13:57:08 +0100574 if (has_fenced_gpu_access) {
575 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100576 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000577 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100578 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100579
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000580 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100581 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000582
Chris Wilson7dd49062012-03-21 10:48:18 +0000583 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100584 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100585 }
586
Ben Widawsky27173f12013-08-14 11:38:36 +0200587 if (entry->offset != vma->node.start) {
588 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100589 *need_reloc = true;
590 }
591
592 if (entry->flags & EXEC_OBJECT_WRITE) {
593 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
594 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
595 }
596
Chris Wilson1690e1e2011-12-14 13:57:08 +0100597 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100598}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100599
Chris Wilsond23db882014-05-23 08:48:08 +0200600static bool
601eb_vma_misplaced(struct i915_vma *vma, bool has_fenced_gpu_access)
602{
603 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
604 struct drm_i915_gem_object *obj = vma->obj;
605 bool need_fence, need_mappable;
606
607 need_fence =
608 has_fenced_gpu_access &&
609 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
610 obj->tiling_mode != I915_TILING_NONE;
611 need_mappable = need_fence || need_reloc_mappable(vma);
612
613 WARN_ON((need_mappable || need_fence) &&
614 !i915_is_ggtt(vma->vm));
615
616 if (entry->alignment &&
617 vma->node.start & (entry->alignment - 1))
618 return true;
619
620 if (need_mappable && !obj->map_and_fenceable)
621 return true;
622
623 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
624 vma->node.start < BATCH_OFFSET_BIAS)
625 return true;
626
627 return false;
628}
629
Chris Wilson54cf91d2010-11-25 18:00:26 +0000630static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000631i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200632 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100633 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000634{
Chris Wilson432e58e2010-11-25 19:32:06 +0000635 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200636 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700637 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200638 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100639 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
640 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000641
Ben Widawsky68c8c172013-09-11 14:57:50 -0700642 if (list_empty(vmas))
643 return 0;
644
645 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
656 need_fence =
657 has_fenced_gpu_access &&
658 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
659 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200660 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000661
662 if (need_mappable)
Ben Widawsky27173f12013-08-14 11:38:36 +0200663 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000664 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200665 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000666
Daniel Vettered5982e2013-01-17 22:23:36 +0100667 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000668 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100669 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000670 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200671 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000672
673 /* Attempt to pin all of the buffers into the GTT.
674 * This is done in 3 phases:
675 *
676 * 1a. Unbind all objects that do not match the GTT constraints for
677 * the execbuffer (fenceable, mappable, alignment etc).
678 * 1b. Increment pin count for already bound objects.
679 * 2. Bind new objects.
680 * 3. Decrement pin count.
681 *
Chris Wilson7788a762012-08-24 19:18:18 +0100682 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000683 * room for the earlier objects *unless* we need to defragment.
684 */
685 retry = 0;
686 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100687 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000688
689 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200690 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200691 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000692 continue;
693
Chris Wilsond23db882014-05-23 08:48:08 +0200694 if (eb_vma_misplaced(vma, has_fenced_gpu_access))
Ben Widawsky27173f12013-08-14 11:38:36 +0200695 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000696 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200697 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000698 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000699 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000700 }
701
702 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200703 list_for_each_entry(vma, vmas, exec_list) {
704 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100705 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000706
Ben Widawsky27173f12013-08-14 11:38:36 +0200707 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100708 if (ret)
709 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000710 }
711
Chris Wilsona415d352013-11-26 11:23:15 +0000712err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200713 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000714 return ret;
715
Chris Wilsona415d352013-11-26 11:23:15 +0000716 /* Decrement pin count for bound objects */
717 list_for_each_entry(vma, vmas, exec_list)
718 i915_gem_execbuffer_unreserve_vma(vma);
719
Ben Widawsky68c8c172013-09-11 14:57:50 -0700720 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000721 if (ret)
722 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000723 } while (1);
724}
725
726static int
727i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100728 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000729 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000730 struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200731 struct eb_vmas *eb,
732 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000733{
734 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200735 struct i915_address_space *vm;
736 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100737 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000738 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000739 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200740 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000741
Ben Widawsky27173f12013-08-14 11:38:36 +0200742 if (WARN_ON(list_empty(&eb->vmas)))
743 return 0;
744
745 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
746
Chris Wilson67731b82010-12-08 10:38:14 +0000747 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200748 while (!list_empty(&eb->vmas)) {
749 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
750 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000751 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200752 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000753 }
754
Chris Wilson54cf91d2010-11-25 18:00:26 +0000755 mutex_unlock(&dev->struct_mutex);
756
757 total = 0;
758 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000759 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000760
Chris Wilsondd6864a2011-01-12 23:49:13 +0000761 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000762 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000763 if (reloc == NULL || reloc_offset == NULL) {
764 drm_free_large(reloc);
765 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000766 mutex_lock(&dev->struct_mutex);
767 return -ENOMEM;
768 }
769
770 total = 0;
771 for (i = 0; i < count; i++) {
772 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000773 u64 invalid_offset = (u64)-1;
774 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000775
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200776 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000777
778 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000779 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000780 ret = -EFAULT;
781 mutex_lock(&dev->struct_mutex);
782 goto err;
783 }
784
Chris Wilson262b6d32013-01-15 16:17:54 +0000785 /* As we do not update the known relocation offsets after
786 * relocating (due to the complexities in lock handling),
787 * we need to mark them as invalid now so that we force the
788 * relocation processing next time. Just in case the target
789 * object is evicted and then rebound into its old
790 * presumed_offset before the next execbuffer - if that
791 * happened we would make the mistake of assuming that the
792 * relocations were valid.
793 */
794 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100795 if (__copy_to_user(&user_relocs[j].presumed_offset,
796 &invalid_offset,
797 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000798 ret = -EFAULT;
799 mutex_lock(&dev->struct_mutex);
800 goto err;
801 }
802 }
803
Chris Wilsondd6864a2011-01-12 23:49:13 +0000804 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000805 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000806 }
807
808 ret = i915_mutex_lock_interruptible(dev);
809 if (ret) {
810 mutex_lock(&dev->struct_mutex);
811 goto err;
812 }
813
Chris Wilson67731b82010-12-08 10:38:14 +0000814 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000815 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200816 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000817 if (ret)
818 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000819
Daniel Vettered5982e2013-01-17 22:23:36 +0100820 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200821 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000822 if (ret)
823 goto err;
824
Ben Widawsky27173f12013-08-14 11:38:36 +0200825 list_for_each_entry(vma, &eb->vmas, exec_list) {
826 int offset = vma->exec_entry - exec;
827 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
828 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000829 if (ret)
830 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000831 }
832
833 /* Leave the user relocations as are, this is the painfully slow path,
834 * and we want to avoid the complication of dropping the lock whilst
835 * having buffers reserved in the aperture and so causing spurious
836 * ENOSPC for random operations.
837 */
838
839err:
840 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000841 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000842 return ret;
843}
844
Chris Wilson54cf91d2010-11-25 18:00:26 +0000845static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000846i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200847 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000848{
Ben Widawsky27173f12013-08-14 11:38:36 +0200849 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200850 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100851 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000852 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000853
Ben Widawsky27173f12013-08-14 11:38:36 +0200854 list_for_each_entry(vma, vmas, exec_list) {
855 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700856 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000857 if (ret)
858 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200859
860 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100861 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200862
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200863 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000864 }
865
Chris Wilson000433b2013-08-08 14:41:09 +0100866 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800867 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200868
869 if (flush_domains & I915_GEM_DOMAIN_GTT)
870 wmb();
871
Chris Wilson09cf7c92012-07-13 14:14:08 +0100872 /* Unconditionally invalidate gpu caches and ensure that we do flush
873 * any residual writes from the previous batch.
874 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100875 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000876}
877
Chris Wilson432e58e2010-11-25 19:32:06 +0000878static bool
879i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000880{
Daniel Vettered5982e2013-01-17 22:23:36 +0100881 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
882 return false;
883
Chris Wilson432e58e2010-11-25 19:32:06 +0000884 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000885}
886
887static int
888validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
889 int count)
890{
891 int i;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200892 unsigned relocs_total = 0;
893 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000894
895 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200896 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000897 int length; /* limited by fault_in_pages_readable() */
898
Daniel Vettered5982e2013-01-17 22:23:36 +0100899 if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
900 return -EINVAL;
901
Kees Cook3118a4f2013-03-11 17:31:45 -0700902 /* First check for malicious input causing overflow in
903 * the worst case where we need to allocate the entire
904 * relocation tree as a single array.
905 */
906 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000907 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700908 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000909
910 length = exec[i].relocation_count *
911 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700912 /*
913 * We must check that the entire relocation array is safe
914 * to read, but since we may need to update the presumed
915 * offsets during execution, check for full write access.
916 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000917 if (!access_ok(VERIFY_WRITE, ptr, length))
918 return -EFAULT;
919
Jani Nikulad330a952014-01-21 11:24:25 +0200920 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +0800921 if (fault_in_multipages_readable(ptr, length))
922 return -EFAULT;
923 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000924 }
925
926 return 0;
927}
928
Ben Widawsky41bde552013-12-06 14:11:21 -0800929static struct i915_hw_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200930i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Daniel Vetter7c9c4b82013-12-18 16:37:49 +0100931 struct intel_ring_buffer *ring, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200932{
Ben Widawsky41bde552013-12-06 14:11:21 -0800933 struct i915_hw_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200934 struct i915_ctx_hang_stats *hs;
935
Daniel Vetter7c9c4b82013-12-18 16:37:49 +0100936 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_ID)
937 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200938
Ben Widawsky41bde552013-12-06 14:11:21 -0800939 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -1000940 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -0800941 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200942
Ben Widawsky41bde552013-12-06 14:11:21 -0800943 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200944 if (hs->banned) {
945 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -0800946 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200947 }
948
Ben Widawsky41bde552013-12-06 14:11:21 -0800949 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200950}
951
Chris Wilson432e58e2010-11-25 19:32:06 +0000952static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200953i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Chris Wilson9d7730912012-11-27 16:22:52 +0000954 struct intel_ring_buffer *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000955{
Ben Widawsky27173f12013-08-14 11:38:36 +0200956 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000957
Ben Widawsky27173f12013-08-14 11:38:36 +0200958 list_for_each_entry(vma, vmas, exec_list) {
959 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100960 u32 old_read = obj->base.read_domains;
961 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000962
Chris Wilson432e58e2010-11-25 19:32:06 +0000963 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100964 if (obj->base.write_domain == 0)
965 obj->base.pending_read_domains |= obj->base.read_domains;
966 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000967 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
968
Ben Widawskye2d05a82013-09-24 09:57:58 -0700969 i915_vma_move_to_active(vma, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000970 if (obj->base.write_domain) {
971 obj->dirty = 1;
Chris Wilson9d7730912012-11-27 16:22:52 +0000972 obj->last_write_seqno = intel_ring_get_seqno(ring);
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800973 /* check for potential scanout */
974 if (i915_gem_obj_ggtt_bound(obj) &&
975 i915_gem_obj_to_ggtt(obj)->pin_count)
Chris Wilsonc65355b2013-06-06 16:53:41 -0300976 intel_mark_fb_busy(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000977 }
978
Chris Wilsondb53a302011-02-03 11:57:46 +0000979 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000980 }
981}
982
Chris Wilson54cf91d2010-11-25 18:00:26 +0000983static void
984i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000985 struct drm_file *file,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300986 struct intel_ring_buffer *ring,
987 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000988{
Daniel Vettercc889e02012-06-13 20:45:19 +0200989 /* Unconditionally force add_request to emit a full flush. */
990 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000991
Chris Wilson432e58e2010-11-25 19:32:06 +0000992 /* Add a breadcrumb for the completion of the batch buffer */
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300993 (void)__i915_add_request(ring, file, obj, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000994}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000995
996static int
Eric Anholtae662d32012-01-03 09:23:29 -0800997i915_reset_gen7_sol_offsets(struct drm_device *dev,
998 struct intel_ring_buffer *ring)
999{
Jani Nikula50227e12014-03-31 14:27:21 +03001000 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtae662d32012-01-03 09:23:29 -08001001 int ret, i;
1002
1003 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
1004 return 0;
1005
1006 ret = intel_ring_begin(ring, 4 * 3);
1007 if (ret)
1008 return ret;
1009
1010 for (i = 0; i < 4; i++) {
1011 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1012 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
1013 intel_ring_emit(ring, 0);
1014 }
1015
1016 intel_ring_advance(ring);
1017
1018 return 0;
1019}
1020
Chris Wilsond23db882014-05-23 08:48:08 +02001021static struct drm_i915_gem_object *
1022eb_get_batch(struct eb_vmas *eb)
1023{
1024 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
1025
1026 /*
1027 * SNA is doing fancy tricks with compressing batch buffers, which leads
1028 * to negative relocation deltas. Usually that works out ok since the
1029 * relocate address is still positive, except when the batch is placed
1030 * very low in the GTT. Ensure this doesn't happen.
1031 *
1032 * Note that actual hangs have only been observed on gen7, but for
1033 * paranoia do it everywhere.
1034 */
1035 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
1036
1037 return vma->obj;
1038}
1039
Eric Anholtae662d32012-01-03 09:23:29 -08001040static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001041i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1042 struct drm_file *file,
1043 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001044 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001045{
Jani Nikula50227e12014-03-31 14:27:21 +03001046 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +02001047 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001048 struct drm_i915_gem_object *batch_obj;
1049 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001050 struct intel_ring_buffer *ring;
Ben Widawsky41bde552013-12-06 14:11:21 -08001051 struct i915_hw_context *ctx;
1052 struct i915_address_space *vm;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001053 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001054 u32 exec_start = args->batch_start_offset, exec_len;
Daniel Vettered5982e2013-01-17 22:23:36 +01001055 u32 mask, flags;
Chris Wilson72bfa192010-12-19 11:42:05 +00001056 int ret, mode, i;
Daniel Vettered5982e2013-01-17 22:23:36 +01001057 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001058
Daniel Vettered5982e2013-01-17 22:23:36 +01001059 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001060 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001061
1062 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001063 if (ret)
1064 return ret;
1065
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001066 flags = 0;
1067 if (args->flags & I915_EXEC_SECURE) {
1068 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1069 return -EPERM;
1070
1071 flags |= I915_DISPATCH_SECURE;
1072 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001073 if (args->flags & I915_EXEC_IS_PINNED)
1074 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001075
Ben Widawskyca01b122013-12-06 14:11:00 -08001076 if ((args->flags & I915_EXEC_RING_MASK) > I915_NUM_RINGS) {
Daniel Vetterff240192012-01-31 21:08:14 +01001077 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001078 (int)(args->flags & I915_EXEC_RING_MASK));
1079 return -EINVAL;
1080 }
Ben Widawskyca01b122013-12-06 14:11:00 -08001081
1082 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1083 ring = &dev_priv->ring[RCS];
1084 else
1085 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1086
Chris Wilsona15817c2012-05-11 14:29:31 +01001087 if (!intel_ring_initialized(ring)) {
1088 DRM_DEBUG("execbuf with invalid ring: %d\n",
1089 (int)(args->flags & I915_EXEC_RING_MASK));
1090 return -EINVAL;
1091 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001092
Chris Wilson72bfa192010-12-19 11:42:05 +00001093 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001094 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +00001095 switch (mode) {
1096 case I915_EXEC_CONSTANTS_REL_GENERAL:
1097 case I915_EXEC_CONSTANTS_ABSOLUTE:
1098 case I915_EXEC_CONSTANTS_REL_SURFACE:
1099 if (ring == &dev_priv->ring[RCS] &&
1100 mode != dev_priv->relative_constants_mode) {
1101 if (INTEL_INFO(dev)->gen < 4)
1102 return -EINVAL;
1103
1104 if (INTEL_INFO(dev)->gen > 5 &&
1105 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1106 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001107
1108 /* The HW changed the meaning on this bit on gen6 */
1109 if (INTEL_INFO(dev)->gen >= 6)
1110 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +00001111 }
1112 break;
1113 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001114 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +00001115 return -EINVAL;
1116 }
1117
Chris Wilson54cf91d2010-11-25 18:00:26 +00001118 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001119 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001120 return -EINVAL;
1121 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001122
1123 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001124 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +01001125 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001126 return -EINVAL;
1127 }
1128
Daniel Vetter6ebebc92012-04-26 23:28:11 +02001129 if (INTEL_INFO(dev)->gen >= 5) {
1130 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1131 return -EINVAL;
1132 }
1133
Xi Wang44afb3a2012-04-23 04:06:42 -04001134 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1135 DRM_DEBUG("execbuf with %u cliprects\n",
1136 args->num_cliprects);
1137 return -EINVAL;
1138 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001139
Daniel Vettera1e22652013-09-21 00:35:38 +02001140 cliprects = kcalloc(args->num_cliprects,
1141 sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001142 GFP_KERNEL);
1143 if (cliprects == NULL) {
1144 ret = -ENOMEM;
1145 goto pre_mutex_err;
1146 }
1147
Chris Wilson432e58e2010-11-25 19:32:06 +00001148 if (copy_from_user(cliprects,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001149 to_user_ptr(args->cliprects_ptr),
1150 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001151 ret = -EFAULT;
1152 goto pre_mutex_err;
1153 }
1154 }
1155
Paulo Zanonif65c9162013-11-27 18:20:34 -02001156 intel_runtime_pm_get(dev_priv);
1157
Chris Wilson54cf91d2010-11-25 18:00:26 +00001158 ret = i915_mutex_lock_interruptible(dev);
1159 if (ret)
1160 goto pre_mutex_err;
1161
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001162 if (dev_priv->ums.mm_suspended) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001163 mutex_unlock(&dev->struct_mutex);
1164 ret = -EBUSY;
1165 goto pre_mutex_err;
1166 }
1167
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001168 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001169 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001170 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001171 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001172 goto pre_mutex_err;
Ben Widawsky41bde552013-12-06 14:11:21 -08001173 }
1174
1175 i915_gem_context_reference(ctx);
1176
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001177 vm = ctx->vm;
1178 if (!USES_FULL_PPGTT(dev))
1179 vm = &dev_priv->gtt.base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001180
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001181 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001182 if (eb == NULL) {
1183 mutex_unlock(&dev->struct_mutex);
1184 ret = -ENOMEM;
1185 goto pre_mutex_err;
1186 }
1187
Chris Wilson54cf91d2010-11-25 18:00:26 +00001188 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001189 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001190 if (ret)
1191 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001192
Chris Wilson6fe4f142011-01-10 17:35:37 +00001193 /* take note of the batch buffer before we might reorder the lists */
Chris Wilsond23db882014-05-23 08:48:08 +02001194 batch_obj = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001195
Chris Wilson54cf91d2010-11-25 18:00:26 +00001196 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001197 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001198 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001199 if (ret)
1200 goto err;
1201
1202 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001203 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001204 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001205 if (ret) {
1206 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001207 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001208 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001209 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1210 }
1211 if (ret)
1212 goto err;
1213 }
1214
1215 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001216 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001217 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001218 ret = -EINVAL;
1219 goto err;
1220 }
1221 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1222
Brad Volkin351e3db2014-02-18 10:15:46 -08001223 if (i915_needs_cmd_parser(ring)) {
1224 ret = i915_parse_cmds(ring,
1225 batch_obj,
1226 args->batch_start_offset,
1227 file->is_master);
1228 if (ret)
1229 goto err;
1230
1231 /*
1232 * XXX: Actually do this when enabling batch copy...
1233 *
1234 * Set the DISPATCH_SECURE bit to remove the NON_SECURE bit
1235 * from MI_BATCH_BUFFER_START commands issued in the
1236 * dispatch_execbuffer implementations. We specifically don't
1237 * want that set when the command parser is enabled.
1238 */
1239 }
1240
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001241 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1242 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001243 * hsw should have this fixed, but bdw mucks it up again. */
Ben Widawsky6f65e292013-12-06 14:10:56 -08001244 if (flags & I915_DISPATCH_SECURE &&
1245 !batch_obj->has_global_gtt_mapping) {
1246 /* When we have multiple VMs, we'll need to make sure that we
1247 * allocate space first */
1248 struct i915_vma *vma = i915_gem_obj_to_ggtt(batch_obj);
1249 BUG_ON(!vma);
1250 vma->bind_vma(vma, batch_obj->cache_level, GLOBAL_BIND);
1251 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001252
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001253 if (flags & I915_DISPATCH_SECURE)
1254 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
1255 else
1256 exec_start += i915_gem_obj_offset(batch_obj, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001257
Ben Widawsky27173f12013-08-14 11:38:36 +02001258 ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001259 if (ret)
1260 goto err;
1261
Chris Wilson691e6412014-04-09 09:07:36 +01001262 ret = i915_switch_context(ring, ctx);
Eric Anholt0da5cec2012-07-23 12:33:55 -07001263 if (ret)
1264 goto err;
1265
Ben Widawskye2971bd2011-12-12 19:21:57 -08001266 if (ring == &dev_priv->ring[RCS] &&
1267 mode != dev_priv->relative_constants_mode) {
1268 ret = intel_ring_begin(ring, 4);
1269 if (ret)
1270 goto err;
1271
1272 intel_ring_emit(ring, MI_NOOP);
1273 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1274 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001275 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001276 intel_ring_advance(ring);
1277
1278 dev_priv->relative_constants_mode = mode;
1279 }
1280
Eric Anholtae662d32012-01-03 09:23:29 -08001281 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1282 ret = i915_reset_gen7_sol_offsets(dev, ring);
1283 if (ret)
1284 goto err;
1285 }
1286
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001287
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001288 exec_len = args->batch_len;
1289 if (cliprects) {
1290 for (i = 0; i < args->num_cliprects; i++) {
1291 ret = i915_emit_box(dev, &cliprects[i],
1292 args->DR1, args->DR4);
1293 if (ret)
1294 goto err;
1295
1296 ret = ring->dispatch_execbuffer(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001297 exec_start, exec_len,
1298 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001299 if (ret)
1300 goto err;
1301 }
1302 } else {
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001303 ret = ring->dispatch_execbuffer(ring,
1304 exec_start, exec_len,
1305 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001306 if (ret)
1307 goto err;
1308 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001309
Chris Wilson9d7730912012-11-27 16:22:52 +00001310 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1311
Ben Widawsky27173f12013-08-14 11:38:36 +02001312 i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
Mika Kuoppala7d736f42013-06-12 15:01:39 +03001313 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001314
1315err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001316 /* the request owns the ref now */
1317 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001318 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001319
1320 mutex_unlock(&dev->struct_mutex);
1321
1322pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001323 kfree(cliprects);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001324
1325 /* intel_gpu_busy should also get a ref, so it will free when the device
1326 * is really idle. */
1327 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001328 return ret;
1329}
1330
1331/*
1332 * Legacy execbuffer just creates an exec2 list from the original exec object
1333 * list array and passes it to the real function.
1334 */
1335int
1336i915_gem_execbuffer(struct drm_device *dev, void *data,
1337 struct drm_file *file)
1338{
1339 struct drm_i915_gem_execbuffer *args = data;
1340 struct drm_i915_gem_execbuffer2 exec2;
1341 struct drm_i915_gem_exec_object *exec_list = NULL;
1342 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1343 int ret, i;
1344
Chris Wilson54cf91d2010-11-25 18:00:26 +00001345 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001346 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001347 return -EINVAL;
1348 }
1349
1350 /* Copy in the exec list from userland */
1351 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1352 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1353 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001354 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001355 args->buffer_count);
1356 drm_free_large(exec_list);
1357 drm_free_large(exec2_list);
1358 return -ENOMEM;
1359 }
1360 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001361 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001362 sizeof(*exec_list) * args->buffer_count);
1363 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001364 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001365 args->buffer_count, ret);
1366 drm_free_large(exec_list);
1367 drm_free_large(exec2_list);
1368 return -EFAULT;
1369 }
1370
1371 for (i = 0; i < args->buffer_count; i++) {
1372 exec2_list[i].handle = exec_list[i].handle;
1373 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1374 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1375 exec2_list[i].alignment = exec_list[i].alignment;
1376 exec2_list[i].offset = exec_list[i].offset;
1377 if (INTEL_INFO(dev)->gen < 4)
1378 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1379 else
1380 exec2_list[i].flags = 0;
1381 }
1382
1383 exec2.buffers_ptr = args->buffers_ptr;
1384 exec2.buffer_count = args->buffer_count;
1385 exec2.batch_start_offset = args->batch_start_offset;
1386 exec2.batch_len = args->batch_len;
1387 exec2.DR1 = args->DR1;
1388 exec2.DR4 = args->DR4;
1389 exec2.num_cliprects = args->num_cliprects;
1390 exec2.cliprects_ptr = args->cliprects_ptr;
1391 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001392 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001393
Ben Widawsky41bde552013-12-06 14:11:21 -08001394 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001395 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001396 struct drm_i915_gem_exec_object __user *user_exec_list =
1397 to_user_ptr(args->buffers_ptr);
1398
Chris Wilson54cf91d2010-11-25 18:00:26 +00001399 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001400 for (i = 0; i < args->buffer_count; i++) {
1401 ret = __copy_to_user(&user_exec_list[i].offset,
1402 &exec2_list[i].offset,
1403 sizeof(user_exec_list[i].offset));
1404 if (ret) {
1405 ret = -EFAULT;
1406 DRM_DEBUG("failed to copy %d exec entries "
1407 "back to user (%d)\n",
1408 args->buffer_count, ret);
1409 break;
1410 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001411 }
1412 }
1413
1414 drm_free_large(exec_list);
1415 drm_free_large(exec2_list);
1416 return ret;
1417}
1418
1419int
1420i915_gem_execbuffer2(struct drm_device *dev, void *data,
1421 struct drm_file *file)
1422{
1423 struct drm_i915_gem_execbuffer2 *args = data;
1424 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1425 int ret;
1426
Xi Wanged8cd3b2012-04-23 04:06:41 -04001427 if (args->buffer_count < 1 ||
1428 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001429 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001430 return -EINVAL;
1431 }
1432
Chris Wilson8408c282011-02-21 12:54:48 +00001433 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001434 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001435 if (exec2_list == NULL)
1436 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1437 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001438 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001439 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001440 args->buffer_count);
1441 return -ENOMEM;
1442 }
1443 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001444 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001445 sizeof(*exec2_list) * args->buffer_count);
1446 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001447 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001448 args->buffer_count, ret);
1449 drm_free_large(exec2_list);
1450 return -EFAULT;
1451 }
1452
Ben Widawsky41bde552013-12-06 14:11:21 -08001453 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001454 if (!ret) {
1455 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001456 struct drm_i915_gem_exec_object2 *user_exec_list =
1457 to_user_ptr(args->buffers_ptr);
1458 int i;
1459
1460 for (i = 0; i < args->buffer_count; i++) {
1461 ret = __copy_to_user(&user_exec_list[i].offset,
1462 &exec2_list[i].offset,
1463 sizeof(user_exec_list[i].offset));
1464 if (ret) {
1465 ret = -EFAULT;
1466 DRM_DEBUG("failed to copy %d exec entries "
1467 "back to user\n",
1468 args->buffer_count);
1469 break;
1470 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001471 }
1472 }
1473
1474 drm_free_large(exec2_list);
1475 return ret;
1476}