blob: 099998156e6cfff512e5555c8162020e64677484 [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
Ben Widawsky27173f12013-08-14 11:38:36 +020036struct eb_vmas {
37 struct list_head vmas;
Chris Wilson67731b82010-12-08 10:38:14 +000038 int and;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000039 union {
Ben Widawsky27173f12013-08-14 11:38:36 +020040 struct i915_vma *lut[0];
Chris Wilsoneef90cc2013-01-08 10:53:17 +000041 struct hlist_head buckets[0];
42 };
Chris Wilson67731b82010-12-08 10:38:14 +000043};
44
Ben Widawsky27173f12013-08-14 11:38:36 +020045static struct eb_vmas *
Ben Widawsky17601cbc2013-11-25 09:54:38 -080046eb_create(struct drm_i915_gem_execbuffer2 *args)
Chris Wilson67731b82010-12-08 10:38:14 +000047{
Ben Widawsky27173f12013-08-14 11:38:36 +020048 struct eb_vmas *eb = NULL;
Chris Wilson67731b82010-12-08 10:38:14 +000049
Chris Wilsoneef90cc2013-01-08 10:53:17 +000050 if (args->flags & I915_EXEC_HANDLE_LUT) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020051 unsigned size = args->buffer_count;
Ben Widawsky27173f12013-08-14 11:38:36 +020052 size *= sizeof(struct i915_vma *);
53 size += sizeof(struct eb_vmas);
Chris Wilsoneef90cc2013-01-08 10:53:17 +000054 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
55 }
56
57 if (eb == NULL) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020058 unsigned size = args->buffer_count;
59 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Lauri Kasanen27b7c632013-03-27 15:04:55 +020060 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
Chris Wilsoneef90cc2013-01-08 10:53:17 +000061 while (count > 2*size)
62 count >>= 1;
63 eb = kzalloc(count*sizeof(struct hlist_head) +
Ben Widawsky27173f12013-08-14 11:38:36 +020064 sizeof(struct eb_vmas),
Chris Wilsoneef90cc2013-01-08 10:53:17 +000065 GFP_TEMPORARY);
66 if (eb == NULL)
67 return eb;
68
69 eb->and = count - 1;
70 } else
71 eb->and = -args->buffer_count;
72
Ben Widawsky27173f12013-08-14 11:38:36 +020073 INIT_LIST_HEAD(&eb->vmas);
Chris Wilson67731b82010-12-08 10:38:14 +000074 return eb;
75}
76
77static void
Ben Widawsky27173f12013-08-14 11:38:36 +020078eb_reset(struct eb_vmas *eb)
Chris Wilson67731b82010-12-08 10:38:14 +000079{
Chris Wilsoneef90cc2013-01-08 10:53:17 +000080 if (eb->and >= 0)
81 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
Chris Wilson67731b82010-12-08 10:38:14 +000082}
83
Chris Wilson3b96eff2013-01-08 10:53:14 +000084static int
Ben Widawsky27173f12013-08-14 11:38:36 +020085eb_lookup_vmas(struct eb_vmas *eb,
86 struct drm_i915_gem_exec_object2 *exec,
87 const struct drm_i915_gem_execbuffer2 *args,
88 struct i915_address_space *vm,
89 struct drm_file *file)
Chris Wilson3b96eff2013-01-08 10:53:14 +000090{
Ben Widawsky6f65e292013-12-06 14:10:56 -080091 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +020092 struct drm_i915_gem_object *obj;
93 struct list_head objects;
94 int i, ret = 0;
Chris Wilson3b96eff2013-01-08 10:53:14 +000095
Ben Widawsky27173f12013-08-14 11:38:36 +020096 INIT_LIST_HEAD(&objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +000097 spin_lock(&file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +020098 /* Grab a reference to the object and release the lock so we can lookup
99 * or create the VMA without using GFP_ATOMIC */
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000100 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000101 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
102 if (obj == NULL) {
103 spin_unlock(&file->table_lock);
104 DRM_DEBUG("Invalid object handle %d at index %d\n",
105 exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200106 ret = -ENOENT;
107 goto out;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000108 }
109
Ben Widawsky27173f12013-08-14 11:38:36 +0200110 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000111 spin_unlock(&file->table_lock);
112 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
113 obj, exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200114 ret = -EINVAL;
115 goto out;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000116 }
117
118 drm_gem_object_reference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200119 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000120 }
121 spin_unlock(&file->table_lock);
122
Ben Widawsky27173f12013-08-14 11:38:36 +0200123 i = 0;
124 list_for_each_entry(obj, &objects, obj_exec_link) {
125 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800126 struct i915_address_space *bind_vm = vm;
127
128 /* If we have secure dispatch, or the userspace assures us that
129 * they know what they're doing, use the GGTT VM.
130 */
131 if (exec[i].flags & EXEC_OBJECT_NEEDS_GTT ||
132 ((args->flags & I915_EXEC_SECURE) &&
133 (i == (args->buffer_count - 1))))
134 bind_vm = &dev_priv->gtt.base;
Ben Widawsky27173f12013-08-14 11:38:36 +0200135
Daniel Vettere656a6c2013-08-14 14:14:04 +0200136 /*
137 * NOTE: We can leak any vmas created here when something fails
138 * later on. But that's no issue since vma_unbind can deal with
139 * vmas which are not actually bound. And since only
140 * lookup_or_create exists as an interface to get at the vma
141 * from the (obj, vm) we don't run the risk of creating
142 * duplicated vmas for the same vm.
143 */
Ben Widawsky6f65e292013-12-06 14:10:56 -0800144 vma = i915_gem_obj_lookup_or_create_vma(obj, bind_vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200145 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200146 DRM_DEBUG("Failed to lookup VMA\n");
147 ret = PTR_ERR(vma);
148 goto out;
149 }
150
151 list_add_tail(&vma->exec_list, &eb->vmas);
152
153 vma->exec_entry = &exec[i];
154 if (eb->and < 0) {
155 eb->lut[i] = vma;
156 } else {
157 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
158 vma->exec_handle = handle;
159 hlist_add_head(&vma->exec_node,
160 &eb->buckets[handle & eb->and]);
161 }
162 ++i;
163 }
164
165
166out:
167 while (!list_empty(&objects)) {
168 obj = list_first_entry(&objects,
169 struct drm_i915_gem_object,
170 obj_exec_link);
171 list_del_init(&obj->obj_exec_link);
172 if (ret)
173 drm_gem_object_unreference(&obj->base);
174 }
175 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000176}
177
Ben Widawsky27173f12013-08-14 11:38:36 +0200178static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000179{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000180 if (eb->and < 0) {
181 if (handle >= -eb->and)
182 return NULL;
183 return eb->lut[handle];
184 } else {
185 struct hlist_head *head;
186 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000187
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000188 head = &eb->buckets[handle & eb->and];
189 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200190 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000191
Ben Widawsky27173f12013-08-14 11:38:36 +0200192 vma = hlist_entry(node, struct i915_vma, exec_node);
193 if (vma->exec_handle == handle)
194 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000195 }
196 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000197 }
Chris Wilson67731b82010-12-08 10:38:14 +0000198}
199
Ben Widawsky27173f12013-08-14 11:38:36 +0200200static void eb_destroy(struct eb_vmas *eb) {
201 while (!list_empty(&eb->vmas)) {
202 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000203
Ben Widawsky27173f12013-08-14 11:38:36 +0200204 vma = list_first_entry(&eb->vmas,
205 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000206 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200207 list_del_init(&vma->exec_list);
208 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000209 }
Chris Wilson67731b82010-12-08 10:38:14 +0000210 kfree(eb);
211}
212
Chris Wilsondabdfe02012-03-26 10:10:27 +0200213static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
214{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300215 return (HAS_LLC(obj->base.dev) ||
216 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100217 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200218 obj->cache_level != I915_CACHE_NONE);
219}
220
Chris Wilson54cf91d2010-11-25 18:00:26 +0000221static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100222relocate_entry_cpu(struct drm_i915_gem_object *obj,
223 struct drm_i915_gem_relocation_entry *reloc)
224{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700225 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100226 uint32_t page_offset = offset_in_page(reloc->offset);
227 char *vaddr;
228 int ret = -EINVAL;
229
Chris Wilson2cc86b82013-08-26 19:51:00 -0300230 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100231 if (ret)
232 return ret;
233
234 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
235 reloc->offset >> PAGE_SHIFT));
236 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700237
238 if (INTEL_INFO(dev)->gen >= 8) {
239 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
240
241 if (page_offset == 0) {
242 kunmap_atomic(vaddr);
243 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
244 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
245 }
246
247 *(uint32_t *)(vaddr + page_offset) = 0;
248 }
249
Rafael Barbalho5032d872013-08-21 17:10:51 +0100250 kunmap_atomic(vaddr);
251
252 return 0;
253}
254
255static int
256relocate_entry_gtt(struct drm_i915_gem_object *obj,
257 struct drm_i915_gem_relocation_entry *reloc)
258{
259 struct drm_device *dev = obj->base.dev;
260 struct drm_i915_private *dev_priv = dev->dev_private;
261 uint32_t __iomem *reloc_entry;
262 void __iomem *reloc_page;
263 int ret = -EINVAL;
264
265 ret = i915_gem_object_set_to_gtt_domain(obj, true);
266 if (ret)
267 return ret;
268
269 ret = i915_gem_object_put_fence(obj);
270 if (ret)
271 return ret;
272
273 /* Map the page containing the relocation we're going to perform. */
274 reloc->offset += i915_gem_obj_ggtt_offset(obj);
275 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
276 reloc->offset & PAGE_MASK);
277 reloc_entry = (uint32_t __iomem *)
278 (reloc_page + offset_in_page(reloc->offset));
279 iowrite32(reloc->delta, reloc_entry);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700280
281 if (INTEL_INFO(dev)->gen >= 8) {
282 reloc_entry += 1;
283
284 if (offset_in_page(reloc->offset + sizeof(uint32_t)) == 0) {
285 io_mapping_unmap_atomic(reloc_page);
286 reloc_page = io_mapping_map_atomic_wc(
287 dev_priv->gtt.mappable,
288 reloc->offset + sizeof(uint32_t));
289 reloc_entry = reloc_page;
290 }
291
292 iowrite32(0, reloc_entry);
293 }
294
Rafael Barbalho5032d872013-08-21 17:10:51 +0100295 io_mapping_unmap_atomic(reloc_page);
296
297 return 0;
298}
299
300static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000301i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200302 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800303 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000304{
305 struct drm_device *dev = obj->base.dev;
306 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100307 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200308 struct i915_vma *target_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000309 uint32_t target_offset;
310 int ret = -EINVAL;
311
Chris Wilson67731b82010-12-08 10:38:14 +0000312 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200313 target_vma = eb_get_vma(eb, reloc->target_handle);
314 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000315 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200316 target_i915_obj = target_vma->obj;
317 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000318
Ben Widawsky5ce09722013-11-25 09:54:40 -0800319 target_offset = target_vma->node.start;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000320
Eric Anholte844b992012-07-31 15:35:01 -0700321 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
322 * pipe_control writes because the gpu doesn't properly redirect them
323 * through the ppgtt for non_secure batchbuffers. */
324 if (unlikely(IS_GEN6(dev) &&
325 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
326 !target_i915_obj->has_global_gtt_mapping)) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800327 struct i915_vma *vma =
328 list_first_entry(&target_i915_obj->vma_list,
329 typeof(*vma), vma_link);
Ben Widawsky6f65e292013-12-06 14:10:56 -0800330 vma->bind_vma(vma, target_i915_obj->cache_level, GLOBAL_BIND);
Eric Anholte844b992012-07-31 15:35:01 -0700331 }
332
Chris Wilson54cf91d2010-11-25 18:00:26 +0000333 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000334 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100335 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000336 "obj %p target %d offset %d "
337 "read %08x write %08x",
338 obj, reloc->target_handle,
339 (int) reloc->offset,
340 reloc->read_domains,
341 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000342 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000343 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100344 if (unlikely((reloc->write_domain | reloc->read_domains)
345 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100346 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000347 "obj %p target %d offset %d "
348 "read %08x write %08x",
349 obj, reloc->target_handle,
350 (int) reloc->offset,
351 reloc->read_domains,
352 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000353 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000354 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000355
356 target_obj->pending_read_domains |= reloc->read_domains;
357 target_obj->pending_write_domain |= reloc->write_domain;
358
359 /* If the relocation already has the right value in it, no
360 * more work needs to be done.
361 */
362 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000363 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000364
365 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700366 if (unlikely(reloc->offset >
367 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100368 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000369 "obj %p target %d offset %d size %d.\n",
370 obj, reloc->target_handle,
371 (int) reloc->offset,
372 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000373 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000374 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000375 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100376 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000377 "obj %p target %d offset %d.\n",
378 obj, reloc->target_handle,
379 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000380 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000381 }
382
Chris Wilsondabdfe02012-03-26 10:10:27 +0200383 /* We can't wait for rendering with pagefaults disabled */
384 if (obj->active && in_atomic())
385 return -EFAULT;
386
Chris Wilson54cf91d2010-11-25 18:00:26 +0000387 reloc->delta += target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100388 if (use_cpu_reloc(obj))
389 ret = relocate_entry_cpu(obj, reloc);
390 else
391 ret = relocate_entry_gtt(obj, reloc);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000392
Daniel Vetterd4d36012013-09-02 20:56:23 +0200393 if (ret)
394 return ret;
395
Chris Wilson54cf91d2010-11-25 18:00:26 +0000396 /* and update the user's relocation entry */
397 reloc->presumed_offset = target_offset;
398
Chris Wilson67731b82010-12-08 10:38:14 +0000399 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000400}
401
402static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200403i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
404 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000405{
Chris Wilson1d83f442012-03-24 20:12:53 +0000406#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
407 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000408 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200409 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000410 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000411
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200412 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000413
Chris Wilson1d83f442012-03-24 20:12:53 +0000414 remain = entry->relocation_count;
415 while (remain) {
416 struct drm_i915_gem_relocation_entry *r = stack_reloc;
417 int count = remain;
418 if (count > ARRAY_SIZE(stack_reloc))
419 count = ARRAY_SIZE(stack_reloc);
420 remain -= count;
421
422 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000423 return -EFAULT;
424
Chris Wilson1d83f442012-03-24 20:12:53 +0000425 do {
426 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000427
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800428 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000429 if (ret)
430 return ret;
431
432 if (r->presumed_offset != offset &&
433 __copy_to_user_inatomic(&user_relocs->presumed_offset,
434 &r->presumed_offset,
435 sizeof(r->presumed_offset))) {
436 return -EFAULT;
437 }
438
439 user_relocs++;
440 r++;
441 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000442 }
443
444 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000445#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000446}
447
448static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200449i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
450 struct eb_vmas *eb,
451 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000452{
Ben Widawsky27173f12013-08-14 11:38:36 +0200453 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000454 int i, ret;
455
456 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800457 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000458 if (ret)
459 return ret;
460 }
461
462 return 0;
463}
464
465static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800466i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000467{
Ben Widawsky27173f12013-08-14 11:38:36 +0200468 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000469 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000470
Chris Wilsond4aeee72011-03-14 15:11:24 +0000471 /* This is the fast path and we cannot handle a pagefault whilst
472 * holding the struct mutex lest the user pass in the relocations
473 * contained within a mmaped bo. For in such a case we, the page
474 * fault handler would call i915_gem_fault() and we would try to
475 * acquire the struct mutex again. Obviously this is bad and so
476 * lockdep complains vehemently.
477 */
478 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200479 list_for_each_entry(vma, &eb->vmas, exec_list) {
480 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000481 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000482 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000483 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000484 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000485
Chris Wilsond4aeee72011-03-14 15:11:24 +0000486 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000487}
488
Chris Wilson7788a762012-08-24 19:18:18 +0100489#define __EXEC_OBJECT_HAS_PIN (1<<31)
490#define __EXEC_OBJECT_HAS_FENCE (1<<30)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100491
492static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200493need_reloc_mappable(struct i915_vma *vma)
Chris Wilsondabdfe02012-03-26 10:10:27 +0200494{
Ben Widawsky27173f12013-08-14 11:38:36 +0200495 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
496 return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
497 i915_is_ggtt(vma->vm);
Chris Wilsondabdfe02012-03-26 10:10:27 +0200498}
499
500static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200501i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
502 struct intel_ring_buffer *ring,
503 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100504{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800505 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200506 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100507 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
508 bool need_fence, need_mappable;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800509 u32 flags = (entry->flags & EXEC_OBJECT_NEEDS_GTT) &&
510 !vma->obj->has_global_gtt_mapping ? GLOBAL_BIND : 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100511 int ret;
512
513 need_fence =
514 has_fenced_gpu_access &&
515 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
516 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200517 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100518
Ben Widawsky27173f12013-08-14 11:38:36 +0200519 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, need_mappable,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700520 false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100521 if (ret)
522 return ret;
523
Chris Wilson7788a762012-08-24 19:18:18 +0100524 entry->flags |= __EXEC_OBJECT_HAS_PIN;
525
Chris Wilson1690e1e2011-12-14 13:57:08 +0100526 if (has_fenced_gpu_access) {
527 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100528 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000529 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100530 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100531
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000532 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100533 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000534
Chris Wilson7dd49062012-03-21 10:48:18 +0000535 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100536 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100537 }
538
Ben Widawsky27173f12013-08-14 11:38:36 +0200539 if (entry->offset != vma->node.start) {
540 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100541 *need_reloc = true;
542 }
543
544 if (entry->flags & EXEC_OBJECT_WRITE) {
545 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
546 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
547 }
548
Ben Widawsky6f65e292013-12-06 14:10:56 -0800549 vma->bind_vma(vma, obj->cache_level, flags);
Daniel Vettered5982e2013-01-17 22:23:36 +0100550
Chris Wilson1690e1e2011-12-14 13:57:08 +0100551 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100552}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100553
Chris Wilson7788a762012-08-24 19:18:18 +0100554static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200555i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
Chris Wilson7788a762012-08-24 19:18:18 +0100556{
557 struct drm_i915_gem_exec_object2 *entry;
Ben Widawsky27173f12013-08-14 11:38:36 +0200558 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson7788a762012-08-24 19:18:18 +0100559
Ben Widawsky27173f12013-08-14 11:38:36 +0200560 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson7788a762012-08-24 19:18:18 +0100561 return;
562
Ben Widawsky27173f12013-08-14 11:38:36 +0200563 entry = vma->exec_entry;
Chris Wilson7788a762012-08-24 19:18:18 +0100564
565 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
566 i915_gem_object_unpin_fence(obj);
567
568 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800569 vma->pin_count--;
Chris Wilson7788a762012-08-24 19:18:18 +0100570
571 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100572}
573
Chris Wilson54cf91d2010-11-25 18:00:26 +0000574static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000575i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200576 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100577 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000578{
Chris Wilson432e58e2010-11-25 19:32:06 +0000579 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200580 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700581 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200582 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100583 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
584 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000585
Ben Widawsky68c8c172013-09-11 14:57:50 -0700586 if (list_empty(vmas))
587 return 0;
588
589 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
590
Ben Widawsky27173f12013-08-14 11:38:36 +0200591 INIT_LIST_HEAD(&ordered_vmas);
592 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000593 struct drm_i915_gem_exec_object2 *entry;
594 bool need_fence, need_mappable;
595
Ben Widawsky27173f12013-08-14 11:38:36 +0200596 vma = list_first_entry(vmas, struct i915_vma, exec_list);
597 obj = vma->obj;
598 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000599
600 need_fence =
601 has_fenced_gpu_access &&
602 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
603 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200604 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000605
606 if (need_mappable)
Ben Widawsky27173f12013-08-14 11:38:36 +0200607 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000608 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200609 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000610
Daniel Vettered5982e2013-01-17 22:23:36 +0100611 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000612 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100613 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000614 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200615 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000616
617 /* Attempt to pin all of the buffers into the GTT.
618 * This is done in 3 phases:
619 *
620 * 1a. Unbind all objects that do not match the GTT constraints for
621 * the execbuffer (fenceable, mappable, alignment etc).
622 * 1b. Increment pin count for already bound objects.
623 * 2. Bind new objects.
624 * 3. Decrement pin count.
625 *
Chris Wilson7788a762012-08-24 19:18:18 +0100626 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000627 * room for the earlier objects *unless* we need to defragment.
628 */
629 retry = 0;
630 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100631 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000632
633 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200634 list_for_each_entry(vma, vmas, exec_list) {
635 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000636 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100637
Ben Widawsky27173f12013-08-14 11:38:36 +0200638 obj = vma->obj;
639
640 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000641 continue;
642
643 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000644 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000645 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
646 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200647 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000648
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700649 WARN_ON((need_mappable || need_fence) &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200650 !i915_is_ggtt(vma->vm));
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700651
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700652 if ((entry->alignment &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200653 vma->node.start & (entry->alignment - 1)) ||
Chris Wilson54cf91d2010-11-25 18:00:26 +0000654 (need_mappable && !obj->map_and_fenceable))
Ben Widawsky27173f12013-08-14 11:38:36 +0200655 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000656 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200657 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000658 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000659 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000660 }
661
662 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200663 list_for_each_entry(vma, vmas, exec_list) {
664 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100665 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000666
Ben Widawsky27173f12013-08-14 11:38:36 +0200667 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100668 if (ret)
669 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000670 }
671
Chris Wilson7788a762012-08-24 19:18:18 +0100672err: /* Decrement pin count for bound objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200673 list_for_each_entry(vma, vmas, exec_list)
674 i915_gem_execbuffer_unreserve_vma(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000675
Chris Wilson6c085a72012-08-20 11:40:46 +0200676 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000677 return ret;
678
Ben Widawsky68c8c172013-09-11 14:57:50 -0700679 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000680 if (ret)
681 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000682 } while (1);
683}
684
685static int
686i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100687 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000688 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000689 struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200690 struct eb_vmas *eb,
691 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000692{
693 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200694 struct i915_address_space *vm;
695 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100696 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000697 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000698 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200699 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000700
Ben Widawsky27173f12013-08-14 11:38:36 +0200701 if (WARN_ON(list_empty(&eb->vmas)))
702 return 0;
703
704 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
705
Chris Wilson67731b82010-12-08 10:38:14 +0000706 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200707 while (!list_empty(&eb->vmas)) {
708 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
709 list_del_init(&vma->exec_list);
710 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000711 }
712
Chris Wilson54cf91d2010-11-25 18:00:26 +0000713 mutex_unlock(&dev->struct_mutex);
714
715 total = 0;
716 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000717 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000718
Chris Wilsondd6864a2011-01-12 23:49:13 +0000719 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000720 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000721 if (reloc == NULL || reloc_offset == NULL) {
722 drm_free_large(reloc);
723 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000724 mutex_lock(&dev->struct_mutex);
725 return -ENOMEM;
726 }
727
728 total = 0;
729 for (i = 0; i < count; i++) {
730 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000731 u64 invalid_offset = (u64)-1;
732 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000733
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200734 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000735
736 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000737 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000738 ret = -EFAULT;
739 mutex_lock(&dev->struct_mutex);
740 goto err;
741 }
742
Chris Wilson262b6d32013-01-15 16:17:54 +0000743 /* As we do not update the known relocation offsets after
744 * relocating (due to the complexities in lock handling),
745 * we need to mark them as invalid now so that we force the
746 * relocation processing next time. Just in case the target
747 * object is evicted and then rebound into its old
748 * presumed_offset before the next execbuffer - if that
749 * happened we would make the mistake of assuming that the
750 * relocations were valid.
751 */
752 for (j = 0; j < exec[i].relocation_count; j++) {
753 if (copy_to_user(&user_relocs[j].presumed_offset,
754 &invalid_offset,
755 sizeof(invalid_offset))) {
756 ret = -EFAULT;
757 mutex_lock(&dev->struct_mutex);
758 goto err;
759 }
760 }
761
Chris Wilsondd6864a2011-01-12 23:49:13 +0000762 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000763 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000764 }
765
766 ret = i915_mutex_lock_interruptible(dev);
767 if (ret) {
768 mutex_lock(&dev->struct_mutex);
769 goto err;
770 }
771
Chris Wilson67731b82010-12-08 10:38:14 +0000772 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000773 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200774 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000775 if (ret)
776 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000777
Daniel Vettered5982e2013-01-17 22:23:36 +0100778 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200779 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000780 if (ret)
781 goto err;
782
Ben Widawsky27173f12013-08-14 11:38:36 +0200783 list_for_each_entry(vma, &eb->vmas, exec_list) {
784 int offset = vma->exec_entry - exec;
785 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
786 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000787 if (ret)
788 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000789 }
790
791 /* Leave the user relocations as are, this is the painfully slow path,
792 * and we want to avoid the complication of dropping the lock whilst
793 * having buffers reserved in the aperture and so causing spurious
794 * ENOSPC for random operations.
795 */
796
797err:
798 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000799 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000800 return ret;
801}
802
Chris Wilson54cf91d2010-11-25 18:00:26 +0000803static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000804i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200805 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000806{
Ben Widawsky27173f12013-08-14 11:38:36 +0200807 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200808 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100809 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000810 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000811
Ben Widawsky27173f12013-08-14 11:38:36 +0200812 list_for_each_entry(vma, vmas, exec_list) {
813 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700814 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000815 if (ret)
816 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200817
818 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100819 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200820
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200821 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000822 }
823
Chris Wilson000433b2013-08-08 14:41:09 +0100824 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800825 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200826
827 if (flush_domains & I915_GEM_DOMAIN_GTT)
828 wmb();
829
Chris Wilson09cf7c92012-07-13 14:14:08 +0100830 /* Unconditionally invalidate gpu caches and ensure that we do flush
831 * any residual writes from the previous batch.
832 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100833 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000834}
835
Chris Wilson432e58e2010-11-25 19:32:06 +0000836static bool
837i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000838{
Daniel Vettered5982e2013-01-17 22:23:36 +0100839 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
840 return false;
841
Chris Wilson432e58e2010-11-25 19:32:06 +0000842 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000843}
844
845static int
846validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
847 int count)
848{
849 int i;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200850 unsigned relocs_total = 0;
851 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000852
853 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200854 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000855 int length; /* limited by fault_in_pages_readable() */
856
Daniel Vettered5982e2013-01-17 22:23:36 +0100857 if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
858 return -EINVAL;
859
Kees Cook3118a4f2013-03-11 17:31:45 -0700860 /* First check for malicious input causing overflow in
861 * the worst case where we need to allocate the entire
862 * relocation tree as a single array.
863 */
864 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000865 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700866 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000867
868 length = exec[i].relocation_count *
869 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700870 /*
871 * We must check that the entire relocation array is safe
872 * to read, but since we may need to update the presumed
873 * offsets during execution, check for full write access.
874 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000875 if (!access_ok(VERIFY_WRITE, ptr, length))
876 return -EFAULT;
877
Xiong Zhang0b74b502013-07-19 13:51:24 +0800878 if (likely(!i915_prefault_disable)) {
879 if (fault_in_multipages_readable(ptr, length))
880 return -EFAULT;
881 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000882 }
883
884 return 0;
885}
886
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200887static int
888i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
889 const u32 ctx_id)
890{
891 struct i915_ctx_hang_stats *hs;
892
893 hs = i915_gem_context_get_hang_stats(dev, file, ctx_id);
894 if (IS_ERR(hs))
895 return PTR_ERR(hs);
896
897 if (hs->banned) {
898 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
899 return -EIO;
900 }
901
902 return 0;
903}
904
Chris Wilson432e58e2010-11-25 19:32:06 +0000905static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200906i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Chris Wilson9d7730912012-11-27 16:22:52 +0000907 struct intel_ring_buffer *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000908{
Ben Widawsky27173f12013-08-14 11:38:36 +0200909 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000910
Ben Widawsky27173f12013-08-14 11:38:36 +0200911 list_for_each_entry(vma, vmas, exec_list) {
912 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100913 u32 old_read = obj->base.read_domains;
914 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000915
Chris Wilson432e58e2010-11-25 19:32:06 +0000916 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100917 if (obj->base.write_domain == 0)
918 obj->base.pending_read_domains |= obj->base.read_domains;
919 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000920 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
921
Ben Widawskye2d05a82013-09-24 09:57:58 -0700922 i915_vma_move_to_active(vma, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000923 if (obj->base.write_domain) {
924 obj->dirty = 1;
Chris Wilson9d7730912012-11-27 16:22:52 +0000925 obj->last_write_seqno = intel_ring_get_seqno(ring);
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800926 /* check for potential scanout */
927 if (i915_gem_obj_ggtt_bound(obj) &&
928 i915_gem_obj_to_ggtt(obj)->pin_count)
Chris Wilsonc65355b2013-06-06 16:53:41 -0300929 intel_mark_fb_busy(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000930 }
931
Chris Wilsondb53a302011-02-03 11:57:46 +0000932 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000933 }
934}
935
Chris Wilson54cf91d2010-11-25 18:00:26 +0000936static void
937i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000938 struct drm_file *file,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300939 struct intel_ring_buffer *ring,
940 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000941{
Daniel Vettercc889e02012-06-13 20:45:19 +0200942 /* Unconditionally force add_request to emit a full flush. */
943 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000944
Chris Wilson432e58e2010-11-25 19:32:06 +0000945 /* Add a breadcrumb for the completion of the batch buffer */
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300946 (void)__i915_add_request(ring, file, obj, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000947}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000948
949static int
Eric Anholtae662d32012-01-03 09:23:29 -0800950i915_reset_gen7_sol_offsets(struct drm_device *dev,
951 struct intel_ring_buffer *ring)
952{
953 drm_i915_private_t *dev_priv = dev->dev_private;
954 int ret, i;
955
956 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
957 return 0;
958
959 ret = intel_ring_begin(ring, 4 * 3);
960 if (ret)
961 return ret;
962
963 for (i = 0; i < 4; i++) {
964 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
965 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
966 intel_ring_emit(ring, 0);
967 }
968
969 intel_ring_advance(ring);
970
971 return 0;
972}
973
974static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000975i915_gem_do_execbuffer(struct drm_device *dev, void *data,
976 struct drm_file *file,
977 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700978 struct drm_i915_gem_exec_object2 *exec,
979 struct i915_address_space *vm)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000980{
981 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +0200982 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000983 struct drm_i915_gem_object *batch_obj;
984 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000985 struct intel_ring_buffer *ring;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200986 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000987 u32 exec_start, exec_len;
Daniel Vettered5982e2013-01-17 22:23:36 +0100988 u32 mask, flags;
Chris Wilson72bfa192010-12-19 11:42:05 +0000989 int ret, mode, i;
Daniel Vettered5982e2013-01-17 22:23:36 +0100990 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000991
Daniel Vettered5982e2013-01-17 22:23:36 +0100992 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +0000993 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +0000994
995 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000996 if (ret)
997 return ret;
998
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100999 flags = 0;
1000 if (args->flags & I915_EXEC_SECURE) {
1001 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1002 return -EPERM;
1003
1004 flags |= I915_DISPATCH_SECURE;
1005 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001006 if (args->flags & I915_EXEC_IS_PINNED)
1007 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001008
Chris Wilson54cf91d2010-11-25 18:00:26 +00001009 switch (args->flags & I915_EXEC_RING_MASK) {
1010 case I915_EXEC_DEFAULT:
1011 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001012 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +00001013 break;
1014 case I915_EXEC_BSD:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001015 ring = &dev_priv->ring[VCS];
Chris Wilsone8520962013-07-03 17:22:07 +03001016 if (ctx_id != DEFAULT_CONTEXT_ID) {
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001017 DRM_DEBUG("Ring %s doesn't support contexts\n",
1018 ring->name);
1019 return -EPERM;
1020 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001021 break;
1022 case I915_EXEC_BLT:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001023 ring = &dev_priv->ring[BCS];
Chris Wilsone8520962013-07-03 17:22:07 +03001024 if (ctx_id != DEFAULT_CONTEXT_ID) {
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001025 DRM_DEBUG("Ring %s doesn't support contexts\n",
1026 ring->name);
1027 return -EPERM;
1028 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001029 break;
Xiang, Haihao82f91b62013-05-28 19:22:33 -07001030 case I915_EXEC_VEBOX:
1031 ring = &dev_priv->ring[VECS];
Chris Wilsone8520962013-07-03 17:22:07 +03001032 if (ctx_id != DEFAULT_CONTEXT_ID) {
Xiang, Haihao82f91b62013-05-28 19:22:33 -07001033 DRM_DEBUG("Ring %s doesn't support contexts\n",
1034 ring->name);
1035 return -EPERM;
1036 }
1037 break;
1038
Chris Wilson54cf91d2010-11-25 18:00:26 +00001039 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001040 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001041 (int)(args->flags & I915_EXEC_RING_MASK));
1042 return -EINVAL;
1043 }
Chris Wilsona15817c2012-05-11 14:29:31 +01001044 if (!intel_ring_initialized(ring)) {
1045 DRM_DEBUG("execbuf with invalid ring: %d\n",
1046 (int)(args->flags & I915_EXEC_RING_MASK));
1047 return -EINVAL;
1048 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001049
Chris Wilson72bfa192010-12-19 11:42:05 +00001050 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001051 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +00001052 switch (mode) {
1053 case I915_EXEC_CONSTANTS_REL_GENERAL:
1054 case I915_EXEC_CONSTANTS_ABSOLUTE:
1055 case I915_EXEC_CONSTANTS_REL_SURFACE:
1056 if (ring == &dev_priv->ring[RCS] &&
1057 mode != dev_priv->relative_constants_mode) {
1058 if (INTEL_INFO(dev)->gen < 4)
1059 return -EINVAL;
1060
1061 if (INTEL_INFO(dev)->gen > 5 &&
1062 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1063 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001064
1065 /* The HW changed the meaning on this bit on gen6 */
1066 if (INTEL_INFO(dev)->gen >= 6)
1067 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +00001068 }
1069 break;
1070 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001071 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +00001072 return -EINVAL;
1073 }
1074
Chris Wilson54cf91d2010-11-25 18:00:26 +00001075 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001076 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001077 return -EINVAL;
1078 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001079
1080 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001081 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +01001082 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001083 return -EINVAL;
1084 }
1085
Daniel Vetter6ebebc92012-04-26 23:28:11 +02001086 if (INTEL_INFO(dev)->gen >= 5) {
1087 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1088 return -EINVAL;
1089 }
1090
Xi Wang44afb3a2012-04-23 04:06:42 -04001091 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1092 DRM_DEBUG("execbuf with %u cliprects\n",
1093 args->num_cliprects);
1094 return -EINVAL;
1095 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001096
Daniel Vettera1e22652013-09-21 00:35:38 +02001097 cliprects = kcalloc(args->num_cliprects,
1098 sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001099 GFP_KERNEL);
1100 if (cliprects == NULL) {
1101 ret = -ENOMEM;
1102 goto pre_mutex_err;
1103 }
1104
Chris Wilson432e58e2010-11-25 19:32:06 +00001105 if (copy_from_user(cliprects,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001106 to_user_ptr(args->cliprects_ptr),
1107 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001108 ret = -EFAULT;
1109 goto pre_mutex_err;
1110 }
1111 }
1112
Chris Wilson54cf91d2010-11-25 18:00:26 +00001113 ret = i915_mutex_lock_interruptible(dev);
1114 if (ret)
1115 goto pre_mutex_err;
1116
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001117 if (dev_priv->ums.mm_suspended) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001118 mutex_unlock(&dev->struct_mutex);
1119 ret = -EBUSY;
1120 goto pre_mutex_err;
1121 }
1122
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001123 ret = i915_gem_validate_context(dev, file, ctx_id);
1124 if (ret) {
1125 mutex_unlock(&dev->struct_mutex);
1126 goto pre_mutex_err;
1127 }
1128
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001129 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001130 if (eb == NULL) {
1131 mutex_unlock(&dev->struct_mutex);
1132 ret = -ENOMEM;
1133 goto pre_mutex_err;
1134 }
1135
Chris Wilson54cf91d2010-11-25 18:00:26 +00001136 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001137 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001138 if (ret)
1139 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001140
Chris Wilson6fe4f142011-01-10 17:35:37 +00001141 /* take note of the batch buffer before we might reorder the lists */
Ben Widawsky27173f12013-08-14 11:38:36 +02001142 batch_obj = list_entry(eb->vmas.prev, struct i915_vma, exec_list)->obj;
Chris Wilson6fe4f142011-01-10 17:35:37 +00001143
Chris Wilson54cf91d2010-11-25 18:00:26 +00001144 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001145 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001146 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001147 if (ret)
1148 goto err;
1149
1150 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001151 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001152 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001153 if (ret) {
1154 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001155 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001156 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001157 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1158 }
1159 if (ret)
1160 goto err;
1161 }
1162
1163 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001164 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001165 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001166 ret = -EINVAL;
1167 goto err;
1168 }
1169 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1170
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001171 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1172 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001173 * hsw should have this fixed, but bdw mucks it up again. */
Ben Widawsky6f65e292013-12-06 14:10:56 -08001174 if (flags & I915_DISPATCH_SECURE &&
1175 !batch_obj->has_global_gtt_mapping) {
1176 /* When we have multiple VMs, we'll need to make sure that we
1177 * allocate space first */
1178 struct i915_vma *vma = i915_gem_obj_to_ggtt(batch_obj);
1179 BUG_ON(!vma);
1180 vma->bind_vma(vma, batch_obj->cache_level, GLOBAL_BIND);
1181 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001182
Ben Widawsky27173f12013-08-14 11:38:36 +02001183 ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001184 if (ret)
1185 goto err;
1186
Eric Anholt0da5cec2012-07-23 12:33:55 -07001187 ret = i915_switch_context(ring, file, ctx_id);
1188 if (ret)
1189 goto err;
1190
Ben Widawskye2971bd2011-12-12 19:21:57 -08001191 if (ring == &dev_priv->ring[RCS] &&
1192 mode != dev_priv->relative_constants_mode) {
1193 ret = intel_ring_begin(ring, 4);
1194 if (ret)
1195 goto err;
1196
1197 intel_ring_emit(ring, MI_NOOP);
1198 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1199 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001200 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001201 intel_ring_advance(ring);
1202
1203 dev_priv->relative_constants_mode = mode;
1204 }
1205
Eric Anholtae662d32012-01-03 09:23:29 -08001206 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1207 ret = i915_reset_gen7_sol_offsets(dev, ring);
1208 if (ret)
1209 goto err;
1210 }
1211
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001212 exec_start = i915_gem_obj_offset(batch_obj, vm) +
1213 args->batch_start_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001214 exec_len = args->batch_len;
1215 if (cliprects) {
1216 for (i = 0; i < args->num_cliprects; i++) {
1217 ret = i915_emit_box(dev, &cliprects[i],
1218 args->DR1, args->DR4);
1219 if (ret)
1220 goto err;
1221
1222 ret = ring->dispatch_execbuffer(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001223 exec_start, exec_len,
1224 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001225 if (ret)
1226 goto err;
1227 }
1228 } else {
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001229 ret = ring->dispatch_execbuffer(ring,
1230 exec_start, exec_len,
1231 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001232 if (ret)
1233 goto err;
1234 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001235
Chris Wilson9d7730912012-11-27 16:22:52 +00001236 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1237
Ben Widawsky27173f12013-08-14 11:38:36 +02001238 i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
Mika Kuoppala7d736f42013-06-12 15:01:39 +03001239 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001240
1241err:
Chris Wilson67731b82010-12-08 10:38:14 +00001242 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001243
1244 mutex_unlock(&dev->struct_mutex);
1245
1246pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001247 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001248 return ret;
1249}
1250
1251/*
1252 * Legacy execbuffer just creates an exec2 list from the original exec object
1253 * list array and passes it to the real function.
1254 */
1255int
1256i915_gem_execbuffer(struct drm_device *dev, void *data,
1257 struct drm_file *file)
1258{
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001259 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001260 struct drm_i915_gem_execbuffer *args = data;
1261 struct drm_i915_gem_execbuffer2 exec2;
1262 struct drm_i915_gem_exec_object *exec_list = NULL;
1263 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1264 int ret, i;
1265
Chris Wilson54cf91d2010-11-25 18:00:26 +00001266 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001267 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001268 return -EINVAL;
1269 }
1270
1271 /* Copy in the exec list from userland */
1272 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1273 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1274 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001275 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001276 args->buffer_count);
1277 drm_free_large(exec_list);
1278 drm_free_large(exec2_list);
1279 return -ENOMEM;
1280 }
1281 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001282 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001283 sizeof(*exec_list) * args->buffer_count);
1284 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001285 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001286 args->buffer_count, ret);
1287 drm_free_large(exec_list);
1288 drm_free_large(exec2_list);
1289 return -EFAULT;
1290 }
1291
1292 for (i = 0; i < args->buffer_count; i++) {
1293 exec2_list[i].handle = exec_list[i].handle;
1294 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1295 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1296 exec2_list[i].alignment = exec_list[i].alignment;
1297 exec2_list[i].offset = exec_list[i].offset;
1298 if (INTEL_INFO(dev)->gen < 4)
1299 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1300 else
1301 exec2_list[i].flags = 0;
1302 }
1303
1304 exec2.buffers_ptr = args->buffers_ptr;
1305 exec2.buffer_count = args->buffer_count;
1306 exec2.batch_start_offset = args->batch_start_offset;
1307 exec2.batch_len = args->batch_len;
1308 exec2.DR1 = args->DR1;
1309 exec2.DR4 = args->DR4;
1310 exec2.num_cliprects = args->num_cliprects;
1311 exec2.cliprects_ptr = args->cliprects_ptr;
1312 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001313 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001314
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001315 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list,
1316 &dev_priv->gtt.base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001317 if (!ret) {
1318 /* Copy the new buffer offsets back to the user's exec list. */
1319 for (i = 0; i < args->buffer_count; i++)
1320 exec_list[i].offset = exec2_list[i].offset;
1321 /* ... and back out to userspace */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001322 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001323 exec_list,
1324 sizeof(*exec_list) * args->buffer_count);
1325 if (ret) {
1326 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001327 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001328 "back to user (%d)\n",
1329 args->buffer_count, ret);
1330 }
1331 }
1332
1333 drm_free_large(exec_list);
1334 drm_free_large(exec2_list);
1335 return ret;
1336}
1337
1338int
1339i915_gem_execbuffer2(struct drm_device *dev, void *data,
1340 struct drm_file *file)
1341{
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001342 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001343 struct drm_i915_gem_execbuffer2 *args = data;
1344 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1345 int ret;
1346
Xi Wanged8cd3b2012-04-23 04:06:41 -04001347 if (args->buffer_count < 1 ||
1348 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001349 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001350 return -EINVAL;
1351 }
1352
Chris Wilson8408c282011-02-21 12:54:48 +00001353 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001354 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001355 if (exec2_list == NULL)
1356 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1357 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001358 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001359 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001360 args->buffer_count);
1361 return -ENOMEM;
1362 }
1363 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001364 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001365 sizeof(*exec2_list) * args->buffer_count);
1366 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001367 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001368 args->buffer_count, ret);
1369 drm_free_large(exec2_list);
1370 return -EFAULT;
1371 }
1372
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001373 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list,
1374 &dev_priv->gtt.base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001375 if (!ret) {
1376 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001377 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001378 exec2_list,
1379 sizeof(*exec2_list) * args->buffer_count);
1380 if (ret) {
1381 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001382 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001383 "back to user (%d)\n",
1384 args->buffer_count, ret);
1385 }
1386 }
1387
1388 drm_free_large(exec2_list);
1389 return ret;
1390}