blob: a36511db14168060adc0aea4cd7fc3a865d3858b [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)
38
Ben Widawsky27173f12013-08-14 11:38:36 +020039struct eb_vmas {
40 struct list_head vmas;
Chris Wilson67731b82010-12-08 10:38:14 +000041 int and;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000042 union {
Ben Widawsky27173f12013-08-14 11:38:36 +020043 struct i915_vma *lut[0];
Chris Wilsoneef90cc2013-01-08 10:53:17 +000044 struct hlist_head buckets[0];
45 };
Chris Wilson67731b82010-12-08 10:38:14 +000046};
47
Ben Widawsky27173f12013-08-14 11:38:36 +020048static struct eb_vmas *
Ben Widawsky17601cbc2013-11-25 09:54:38 -080049eb_create(struct drm_i915_gem_execbuffer2 *args)
Chris Wilson67731b82010-12-08 10:38:14 +000050{
Ben Widawsky27173f12013-08-14 11:38:36 +020051 struct eb_vmas *eb = NULL;
Chris Wilson67731b82010-12-08 10:38:14 +000052
Chris Wilsoneef90cc2013-01-08 10:53:17 +000053 if (args->flags & I915_EXEC_HANDLE_LUT) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020054 unsigned size = args->buffer_count;
Ben Widawsky27173f12013-08-14 11:38:36 +020055 size *= sizeof(struct i915_vma *);
56 size += sizeof(struct eb_vmas);
Chris Wilsoneef90cc2013-01-08 10:53:17 +000057 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
58 }
59
60 if (eb == NULL) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020061 unsigned size = args->buffer_count;
62 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Lauri Kasanen27b7c632013-03-27 15:04:55 +020063 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
Chris Wilsoneef90cc2013-01-08 10:53:17 +000064 while (count > 2*size)
65 count >>= 1;
66 eb = kzalloc(count*sizeof(struct hlist_head) +
Ben Widawsky27173f12013-08-14 11:38:36 +020067 sizeof(struct eb_vmas),
Chris Wilsoneef90cc2013-01-08 10:53:17 +000068 GFP_TEMPORARY);
69 if (eb == NULL)
70 return eb;
71
72 eb->and = count - 1;
73 } else
74 eb->and = -args->buffer_count;
75
Ben Widawsky27173f12013-08-14 11:38:36 +020076 INIT_LIST_HEAD(&eb->vmas);
Chris Wilson67731b82010-12-08 10:38:14 +000077 return eb;
78}
79
80static void
Ben Widawsky27173f12013-08-14 11:38:36 +020081eb_reset(struct eb_vmas *eb)
Chris Wilson67731b82010-12-08 10:38:14 +000082{
Chris Wilsoneef90cc2013-01-08 10:53:17 +000083 if (eb->and >= 0)
84 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
Chris Wilson67731b82010-12-08 10:38:14 +000085}
86
Chris Wilson3b96eff2013-01-08 10:53:14 +000087static int
Ben Widawsky27173f12013-08-14 11:38:36 +020088eb_lookup_vmas(struct eb_vmas *eb,
89 struct drm_i915_gem_exec_object2 *exec,
90 const struct drm_i915_gem_execbuffer2 *args,
91 struct i915_address_space *vm,
92 struct drm_file *file)
Chris Wilson3b96eff2013-01-08 10:53:14 +000093{
Ben Widawsky6f65e292013-12-06 14:10:56 -080094 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +020095 struct drm_i915_gem_object *obj;
96 struct list_head objects;
97 int i, ret = 0;
Chris Wilson3b96eff2013-01-08 10:53:14 +000098
Ben Widawsky27173f12013-08-14 11:38:36 +020099 INIT_LIST_HEAD(&objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000100 spin_lock(&file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +0200101 /* Grab a reference to the object and release the lock so we can lookup
102 * or create the VMA without using GFP_ATOMIC */
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000103 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000104 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
105 if (obj == NULL) {
106 spin_unlock(&file->table_lock);
107 DRM_DEBUG("Invalid object handle %d at index %d\n",
108 exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200109 ret = -ENOENT;
110 goto out;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000111 }
112
Ben Widawsky27173f12013-08-14 11:38:36 +0200113 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000114 spin_unlock(&file->table_lock);
115 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
116 obj, exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200117 ret = -EINVAL;
118 goto out;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000119 }
120
121 drm_gem_object_reference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200122 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000123 }
124 spin_unlock(&file->table_lock);
125
Ben Widawsky27173f12013-08-14 11:38:36 +0200126 i = 0;
127 list_for_each_entry(obj, &objects, obj_exec_link) {
128 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800129 struct i915_address_space *bind_vm = vm;
130
Daniel Vetter2c9f8d52013-12-18 17:38:53 +0100131 if (exec[i].flags & EXEC_OBJECT_NEEDS_GTT &&
132 USES_FULL_PPGTT(vm->dev)) {
133 ret = -EINVAL;
134 goto out;
135 }
136
Ben Widawsky6f65e292013-12-06 14:10:56 -0800137 /* If we have secure dispatch, or the userspace assures us that
138 * they know what they're doing, use the GGTT VM.
139 */
Daniel Vettera7c1d4262013-12-18 17:46:18 +0100140 if (((args->flags & I915_EXEC_SECURE) &&
Ben Widawsky6f65e292013-12-06 14:10:56 -0800141 (i == (args->buffer_count - 1))))
142 bind_vm = &dev_priv->gtt.base;
Ben Widawsky27173f12013-08-14 11:38:36 +0200143
Daniel Vettere656a6c2013-08-14 14:14:04 +0200144 /*
145 * NOTE: We can leak any vmas created here when something fails
146 * later on. But that's no issue since vma_unbind can deal with
147 * vmas which are not actually bound. And since only
148 * lookup_or_create exists as an interface to get at the vma
149 * from the (obj, vm) we don't run the risk of creating
150 * duplicated vmas for the same vm.
151 */
Ben Widawsky6f65e292013-12-06 14:10:56 -0800152 vma = i915_gem_obj_lookup_or_create_vma(obj, bind_vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200153 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200154 DRM_DEBUG("Failed to lookup VMA\n");
155 ret = PTR_ERR(vma);
156 goto out;
157 }
158
159 list_add_tail(&vma->exec_list, &eb->vmas);
160
161 vma->exec_entry = &exec[i];
162 if (eb->and < 0) {
163 eb->lut[i] = vma;
164 } else {
165 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
166 vma->exec_handle = handle;
167 hlist_add_head(&vma->exec_node,
168 &eb->buckets[handle & eb->and]);
169 }
170 ++i;
171 }
172
173
174out:
175 while (!list_empty(&objects)) {
176 obj = list_first_entry(&objects,
177 struct drm_i915_gem_object,
178 obj_exec_link);
179 list_del_init(&obj->obj_exec_link);
180 if (ret)
181 drm_gem_object_unreference(&obj->base);
182 }
183 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000184}
185
Ben Widawsky27173f12013-08-14 11:38:36 +0200186static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000187{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000188 if (eb->and < 0) {
189 if (handle >= -eb->and)
190 return NULL;
191 return eb->lut[handle];
192 } else {
193 struct hlist_head *head;
194 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000195
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000196 head = &eb->buckets[handle & eb->and];
197 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200198 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000199
Ben Widawsky27173f12013-08-14 11:38:36 +0200200 vma = hlist_entry(node, struct i915_vma, exec_node);
201 if (vma->exec_handle == handle)
202 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000203 }
204 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000205 }
Chris Wilson67731b82010-12-08 10:38:14 +0000206}
207
Chris Wilsona415d352013-11-26 11:23:15 +0000208static void
209i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
210{
211 struct drm_i915_gem_exec_object2 *entry;
212 struct drm_i915_gem_object *obj = vma->obj;
213
214 if (!drm_mm_node_allocated(&vma->node))
215 return;
216
217 entry = vma->exec_entry;
218
219 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
220 i915_gem_object_unpin_fence(obj);
221
222 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Daniel Vetter3d7f0f92013-12-18 16:23:37 +0100223 vma->pin_count--;
Chris Wilsona415d352013-11-26 11:23:15 +0000224
225 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
226}
227
228static void eb_destroy(struct eb_vmas *eb)
229{
Ben Widawsky27173f12013-08-14 11:38:36 +0200230 while (!list_empty(&eb->vmas)) {
231 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000232
Ben Widawsky27173f12013-08-14 11:38:36 +0200233 vma = list_first_entry(&eb->vmas,
234 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000235 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200236 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000237 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200238 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000239 }
Chris Wilson67731b82010-12-08 10:38:14 +0000240 kfree(eb);
241}
242
Chris Wilsondabdfe02012-03-26 10:10:27 +0200243static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
244{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300245 return (HAS_LLC(obj->base.dev) ||
246 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100247 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200248 obj->cache_level != I915_CACHE_NONE);
249}
250
Chris Wilson54cf91d2010-11-25 18:00:26 +0000251static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100252relocate_entry_cpu(struct drm_i915_gem_object *obj,
253 struct drm_i915_gem_relocation_entry *reloc)
254{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700255 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100256 uint32_t page_offset = offset_in_page(reloc->offset);
257 char *vaddr;
258 int ret = -EINVAL;
259
Chris Wilson2cc86b82013-08-26 19:51:00 -0300260 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100261 if (ret)
262 return ret;
263
264 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
265 reloc->offset >> PAGE_SHIFT));
266 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700267
268 if (INTEL_INFO(dev)->gen >= 8) {
269 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
270
271 if (page_offset == 0) {
272 kunmap_atomic(vaddr);
273 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
274 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
275 }
276
277 *(uint32_t *)(vaddr + page_offset) = 0;
278 }
279
Rafael Barbalho5032d872013-08-21 17:10:51 +0100280 kunmap_atomic(vaddr);
281
282 return 0;
283}
284
285static int
286relocate_entry_gtt(struct drm_i915_gem_object *obj,
287 struct drm_i915_gem_relocation_entry *reloc)
288{
289 struct drm_device *dev = obj->base.dev;
290 struct drm_i915_private *dev_priv = dev->dev_private;
291 uint32_t __iomem *reloc_entry;
292 void __iomem *reloc_page;
293 int ret = -EINVAL;
294
295 ret = i915_gem_object_set_to_gtt_domain(obj, true);
296 if (ret)
297 return ret;
298
299 ret = i915_gem_object_put_fence(obj);
300 if (ret)
301 return ret;
302
303 /* Map the page containing the relocation we're going to perform. */
304 reloc->offset += i915_gem_obj_ggtt_offset(obj);
305 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
306 reloc->offset & PAGE_MASK);
307 reloc_entry = (uint32_t __iomem *)
308 (reloc_page + offset_in_page(reloc->offset));
309 iowrite32(reloc->delta, reloc_entry);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700310
311 if (INTEL_INFO(dev)->gen >= 8) {
312 reloc_entry += 1;
313
314 if (offset_in_page(reloc->offset + sizeof(uint32_t)) == 0) {
315 io_mapping_unmap_atomic(reloc_page);
316 reloc_page = io_mapping_map_atomic_wc(
317 dev_priv->gtt.mappable,
318 reloc->offset + sizeof(uint32_t));
319 reloc_entry = reloc_page;
320 }
321
322 iowrite32(0, reloc_entry);
323 }
324
Rafael Barbalho5032d872013-08-21 17:10:51 +0100325 io_mapping_unmap_atomic(reloc_page);
326
327 return 0;
328}
329
330static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000331i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200332 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800333 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000334{
335 struct drm_device *dev = obj->base.dev;
336 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100337 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200338 struct i915_vma *target_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000339 uint32_t target_offset;
340 int ret = -EINVAL;
341
Chris Wilson67731b82010-12-08 10:38:14 +0000342 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200343 target_vma = eb_get_vma(eb, reloc->target_handle);
344 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000345 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200346 target_i915_obj = target_vma->obj;
347 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000348
Ben Widawsky5ce09722013-11-25 09:54:40 -0800349 target_offset = target_vma->node.start;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000350
Eric Anholte844b992012-07-31 15:35:01 -0700351 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
352 * pipe_control writes because the gpu doesn't properly redirect them
353 * through the ppgtt for non_secure batchbuffers. */
354 if (unlikely(IS_GEN6(dev) &&
355 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
356 !target_i915_obj->has_global_gtt_mapping)) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800357 struct i915_vma *vma =
358 list_first_entry(&target_i915_obj->vma_list,
359 typeof(*vma), vma_link);
Ben Widawsky6f65e292013-12-06 14:10:56 -0800360 vma->bind_vma(vma, target_i915_obj->cache_level, GLOBAL_BIND);
Eric Anholte844b992012-07-31 15:35:01 -0700361 }
362
Chris Wilson54cf91d2010-11-25 18:00:26 +0000363 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000364 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100365 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000366 "obj %p target %d offset %d "
367 "read %08x write %08x",
368 obj, reloc->target_handle,
369 (int) reloc->offset,
370 reloc->read_domains,
371 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000372 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000373 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100374 if (unlikely((reloc->write_domain | reloc->read_domains)
375 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100376 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000377 "obj %p target %d offset %d "
378 "read %08x write %08x",
379 obj, reloc->target_handle,
380 (int) reloc->offset,
381 reloc->read_domains,
382 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000383 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000384 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000385
386 target_obj->pending_read_domains |= reloc->read_domains;
387 target_obj->pending_write_domain |= reloc->write_domain;
388
389 /* If the relocation already has the right value in it, no
390 * more work needs to be done.
391 */
392 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000393 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000394
395 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700396 if (unlikely(reloc->offset >
397 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100398 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000399 "obj %p target %d offset %d size %d.\n",
400 obj, reloc->target_handle,
401 (int) reloc->offset,
402 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000403 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000404 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000405 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100406 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000407 "obj %p target %d offset %d.\n",
408 obj, reloc->target_handle,
409 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000410 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000411 }
412
Chris Wilsondabdfe02012-03-26 10:10:27 +0200413 /* We can't wait for rendering with pagefaults disabled */
414 if (obj->active && in_atomic())
415 return -EFAULT;
416
Chris Wilson54cf91d2010-11-25 18:00:26 +0000417 reloc->delta += target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100418 if (use_cpu_reloc(obj))
419 ret = relocate_entry_cpu(obj, reloc);
420 else
421 ret = relocate_entry_gtt(obj, reloc);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000422
Daniel Vetterd4d36012013-09-02 20:56:23 +0200423 if (ret)
424 return ret;
425
Chris Wilson54cf91d2010-11-25 18:00:26 +0000426 /* and update the user's relocation entry */
427 reloc->presumed_offset = target_offset;
428
Chris Wilson67731b82010-12-08 10:38:14 +0000429 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000430}
431
432static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200433i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
434 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000435{
Chris Wilson1d83f442012-03-24 20:12:53 +0000436#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
437 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000438 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200439 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000440 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000441
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200442 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000443
Chris Wilson1d83f442012-03-24 20:12:53 +0000444 remain = entry->relocation_count;
445 while (remain) {
446 struct drm_i915_gem_relocation_entry *r = stack_reloc;
447 int count = remain;
448 if (count > ARRAY_SIZE(stack_reloc))
449 count = ARRAY_SIZE(stack_reloc);
450 remain -= count;
451
452 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000453 return -EFAULT;
454
Chris Wilson1d83f442012-03-24 20:12:53 +0000455 do {
456 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000457
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800458 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000459 if (ret)
460 return ret;
461
462 if (r->presumed_offset != offset &&
463 __copy_to_user_inatomic(&user_relocs->presumed_offset,
464 &r->presumed_offset,
465 sizeof(r->presumed_offset))) {
466 return -EFAULT;
467 }
468
469 user_relocs++;
470 r++;
471 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000472 }
473
474 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000475#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000476}
477
478static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200479i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
480 struct eb_vmas *eb,
481 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000482{
Ben Widawsky27173f12013-08-14 11:38:36 +0200483 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000484 int i, ret;
485
486 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800487 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000488 if (ret)
489 return ret;
490 }
491
492 return 0;
493}
494
495static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800496i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000497{
Ben Widawsky27173f12013-08-14 11:38:36 +0200498 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000499 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000500
Chris Wilsond4aeee72011-03-14 15:11:24 +0000501 /* This is the fast path and we cannot handle a pagefault whilst
502 * holding the struct mutex lest the user pass in the relocations
503 * contained within a mmaped bo. For in such a case we, the page
504 * fault handler would call i915_gem_fault() and we would try to
505 * acquire the struct mutex again. Obviously this is bad and so
506 * lockdep complains vehemently.
507 */
508 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200509 list_for_each_entry(vma, &eb->vmas, exec_list) {
510 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000511 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000512 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000513 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000514 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000515
Chris Wilsond4aeee72011-03-14 15:11:24 +0000516 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000517}
518
Chris Wilson1690e1e2011-12-14 13:57:08 +0100519static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200520need_reloc_mappable(struct i915_vma *vma)
Chris Wilsondabdfe02012-03-26 10:10:27 +0200521{
Ben Widawsky27173f12013-08-14 11:38:36 +0200522 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
523 return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
524 i915_is_ggtt(vma->vm);
Chris Wilsondabdfe02012-03-26 10:10:27 +0200525}
526
527static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200528i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
529 struct intel_ring_buffer *ring,
530 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100531{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800532 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200533 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100534 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
535 bool need_fence, need_mappable;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800536 u32 flags = (entry->flags & EXEC_OBJECT_NEEDS_GTT) &&
537 !vma->obj->has_global_gtt_mapping ? GLOBAL_BIND : 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100538 int ret;
539
540 need_fence =
541 has_fenced_gpu_access &&
542 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
543 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200544 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100545
Ben Widawsky27173f12013-08-14 11:38:36 +0200546 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, need_mappable,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700547 false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100548 if (ret)
549 return ret;
550
Chris Wilson7788a762012-08-24 19:18:18 +0100551 entry->flags |= __EXEC_OBJECT_HAS_PIN;
552
Chris Wilson1690e1e2011-12-14 13:57:08 +0100553 if (has_fenced_gpu_access) {
554 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100555 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000556 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100557 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100558
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000559 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100560 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000561
Chris Wilson7dd49062012-03-21 10:48:18 +0000562 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100563 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100564 }
565
Ben Widawsky27173f12013-08-14 11:38:36 +0200566 if (entry->offset != vma->node.start) {
567 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100568 *need_reloc = true;
569 }
570
571 if (entry->flags & EXEC_OBJECT_WRITE) {
572 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
573 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
574 }
575
Ben Widawsky6f65e292013-12-06 14:10:56 -0800576 vma->bind_vma(vma, obj->cache_level, flags);
Daniel Vettered5982e2013-01-17 22:23:36 +0100577
Chris Wilson1690e1e2011-12-14 13:57:08 +0100578 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100579}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100580
Chris Wilson54cf91d2010-11-25 18:00:26 +0000581static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000582i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200583 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100584 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000585{
Chris Wilson432e58e2010-11-25 19:32:06 +0000586 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200587 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700588 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200589 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100590 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
591 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000592
Ben Widawsky68c8c172013-09-11 14:57:50 -0700593 if (list_empty(vmas))
594 return 0;
595
596 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
597
Ben Widawsky27173f12013-08-14 11:38:36 +0200598 INIT_LIST_HEAD(&ordered_vmas);
599 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000600 struct drm_i915_gem_exec_object2 *entry;
601 bool need_fence, need_mappable;
602
Ben Widawsky27173f12013-08-14 11:38:36 +0200603 vma = list_first_entry(vmas, struct i915_vma, exec_list);
604 obj = vma->obj;
605 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000606
607 need_fence =
608 has_fenced_gpu_access &&
609 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
610 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200611 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000612
613 if (need_mappable)
Ben Widawsky27173f12013-08-14 11:38:36 +0200614 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000615 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200616 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000617
Daniel Vettered5982e2013-01-17 22:23:36 +0100618 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000619 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100620 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000621 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200622 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000623
624 /* Attempt to pin all of the buffers into the GTT.
625 * This is done in 3 phases:
626 *
627 * 1a. Unbind all objects that do not match the GTT constraints for
628 * the execbuffer (fenceable, mappable, alignment etc).
629 * 1b. Increment pin count for already bound objects.
630 * 2. Bind new objects.
631 * 3. Decrement pin count.
632 *
Chris Wilson7788a762012-08-24 19:18:18 +0100633 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000634 * room for the earlier objects *unless* we need to defragment.
635 */
636 retry = 0;
637 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100638 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000639
640 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200641 list_for_each_entry(vma, vmas, exec_list) {
642 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000643 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100644
Ben Widawsky27173f12013-08-14 11:38:36 +0200645 obj = vma->obj;
646
647 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000648 continue;
649
650 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000651 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000652 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
653 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200654 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000655
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700656 WARN_ON((need_mappable || need_fence) &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200657 !i915_is_ggtt(vma->vm));
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700658
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700659 if ((entry->alignment &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200660 vma->node.start & (entry->alignment - 1)) ||
Chris Wilson54cf91d2010-11-25 18:00:26 +0000661 (need_mappable && !obj->map_and_fenceable))
Ben Widawsky27173f12013-08-14 11:38:36 +0200662 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000663 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200664 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000665 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000666 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000667 }
668
669 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200670 list_for_each_entry(vma, vmas, exec_list) {
671 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100672 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000673
Ben Widawsky27173f12013-08-14 11:38:36 +0200674 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100675 if (ret)
676 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000677 }
678
Chris Wilsona415d352013-11-26 11:23:15 +0000679err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200680 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000681 return ret;
682
Chris Wilsona415d352013-11-26 11:23:15 +0000683 /* Decrement pin count for bound objects */
684 list_for_each_entry(vma, vmas, exec_list)
685 i915_gem_execbuffer_unreserve_vma(vma);
686
Ben Widawsky68c8c172013-09-11 14:57:50 -0700687 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000688 if (ret)
689 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000690 } while (1);
691}
692
693static int
694i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100695 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000696 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000697 struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200698 struct eb_vmas *eb,
699 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000700{
701 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200702 struct i915_address_space *vm;
703 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100704 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000705 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000706 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200707 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000708
Ben Widawsky27173f12013-08-14 11:38:36 +0200709 if (WARN_ON(list_empty(&eb->vmas)))
710 return 0;
711
712 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
713
Chris Wilson67731b82010-12-08 10:38:14 +0000714 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200715 while (!list_empty(&eb->vmas)) {
716 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
717 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000718 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200719 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000720 }
721
Chris Wilson54cf91d2010-11-25 18:00:26 +0000722 mutex_unlock(&dev->struct_mutex);
723
724 total = 0;
725 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000726 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000727
Chris Wilsondd6864a2011-01-12 23:49:13 +0000728 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000729 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000730 if (reloc == NULL || reloc_offset == NULL) {
731 drm_free_large(reloc);
732 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000733 mutex_lock(&dev->struct_mutex);
734 return -ENOMEM;
735 }
736
737 total = 0;
738 for (i = 0; i < count; i++) {
739 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000740 u64 invalid_offset = (u64)-1;
741 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000742
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200743 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000744
745 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000746 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000747 ret = -EFAULT;
748 mutex_lock(&dev->struct_mutex);
749 goto err;
750 }
751
Chris Wilson262b6d32013-01-15 16:17:54 +0000752 /* As we do not update the known relocation offsets after
753 * relocating (due to the complexities in lock handling),
754 * we need to mark them as invalid now so that we force the
755 * relocation processing next time. Just in case the target
756 * object is evicted and then rebound into its old
757 * presumed_offset before the next execbuffer - if that
758 * happened we would make the mistake of assuming that the
759 * relocations were valid.
760 */
761 for (j = 0; j < exec[i].relocation_count; j++) {
762 if (copy_to_user(&user_relocs[j].presumed_offset,
763 &invalid_offset,
764 sizeof(invalid_offset))) {
765 ret = -EFAULT;
766 mutex_lock(&dev->struct_mutex);
767 goto err;
768 }
769 }
770
Chris Wilsondd6864a2011-01-12 23:49:13 +0000771 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000772 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000773 }
774
775 ret = i915_mutex_lock_interruptible(dev);
776 if (ret) {
777 mutex_lock(&dev->struct_mutex);
778 goto err;
779 }
780
Chris Wilson67731b82010-12-08 10:38:14 +0000781 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000782 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200783 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000784 if (ret)
785 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000786
Daniel Vettered5982e2013-01-17 22:23:36 +0100787 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200788 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000789 if (ret)
790 goto err;
791
Ben Widawsky27173f12013-08-14 11:38:36 +0200792 list_for_each_entry(vma, &eb->vmas, exec_list) {
793 int offset = vma->exec_entry - exec;
794 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
795 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000796 if (ret)
797 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000798 }
799
800 /* Leave the user relocations as are, this is the painfully slow path,
801 * and we want to avoid the complication of dropping the lock whilst
802 * having buffers reserved in the aperture and so causing spurious
803 * ENOSPC for random operations.
804 */
805
806err:
807 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000808 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000809 return ret;
810}
811
Chris Wilson54cf91d2010-11-25 18:00:26 +0000812static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000813i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200814 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000815{
Ben Widawsky27173f12013-08-14 11:38:36 +0200816 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200817 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100818 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000819 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000820
Ben Widawsky27173f12013-08-14 11:38:36 +0200821 list_for_each_entry(vma, vmas, exec_list) {
822 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700823 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000824 if (ret)
825 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200826
827 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100828 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200829
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200830 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000831 }
832
Chris Wilson000433b2013-08-08 14:41:09 +0100833 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800834 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200835
836 if (flush_domains & I915_GEM_DOMAIN_GTT)
837 wmb();
838
Chris Wilson09cf7c92012-07-13 14:14:08 +0100839 /* Unconditionally invalidate gpu caches and ensure that we do flush
840 * any residual writes from the previous batch.
841 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100842 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000843}
844
Chris Wilson432e58e2010-11-25 19:32:06 +0000845static bool
846i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000847{
Daniel Vettered5982e2013-01-17 22:23:36 +0100848 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
849 return false;
850
Chris Wilson432e58e2010-11-25 19:32:06 +0000851 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000852}
853
854static int
855validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
856 int count)
857{
858 int i;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200859 unsigned relocs_total = 0;
860 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000861
862 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200863 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000864 int length; /* limited by fault_in_pages_readable() */
865
Daniel Vettered5982e2013-01-17 22:23:36 +0100866 if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
867 return -EINVAL;
868
Kees Cook3118a4f2013-03-11 17:31:45 -0700869 /* First check for malicious input causing overflow in
870 * the worst case where we need to allocate the entire
871 * relocation tree as a single array.
872 */
873 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000874 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700875 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000876
877 length = exec[i].relocation_count *
878 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700879 /*
880 * We must check that the entire relocation array is safe
881 * to read, but since we may need to update the presumed
882 * offsets during execution, check for full write access.
883 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000884 if (!access_ok(VERIFY_WRITE, ptr, length))
885 return -EFAULT;
886
Xiong Zhang0b74b502013-07-19 13:51:24 +0800887 if (likely(!i915_prefault_disable)) {
888 if (fault_in_multipages_readable(ptr, length))
889 return -EFAULT;
890 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000891 }
892
893 return 0;
894}
895
Ben Widawsky41bde552013-12-06 14:11:21 -0800896static struct i915_hw_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200897i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Daniel Vetter7c9c4b82013-12-18 16:37:49 +0100898 struct intel_ring_buffer *ring, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200899{
Ben Widawsky41bde552013-12-06 14:11:21 -0800900 struct i915_hw_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200901 struct i915_ctx_hang_stats *hs;
902
Daniel Vetter7c9c4b82013-12-18 16:37:49 +0100903 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_ID)
904 return ERR_PTR(-EINVAL);
905
Ben Widawsky41bde552013-12-06 14:11:21 -0800906 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
907 if (IS_ERR_OR_NULL(ctx))
908 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200909
Ben Widawsky41bde552013-12-06 14:11:21 -0800910 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200911 if (hs->banned) {
912 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -0800913 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200914 }
915
Ben Widawsky41bde552013-12-06 14:11:21 -0800916 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200917}
918
Chris Wilson432e58e2010-11-25 19:32:06 +0000919static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200920i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Chris Wilson9d7730912012-11-27 16:22:52 +0000921 struct intel_ring_buffer *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000922{
Ben Widawsky27173f12013-08-14 11:38:36 +0200923 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000924
Ben Widawsky27173f12013-08-14 11:38:36 +0200925 list_for_each_entry(vma, vmas, exec_list) {
926 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100927 u32 old_read = obj->base.read_domains;
928 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000929
Chris Wilson432e58e2010-11-25 19:32:06 +0000930 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100931 if (obj->base.write_domain == 0)
932 obj->base.pending_read_domains |= obj->base.read_domains;
933 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000934 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
935
Ben Widawskye2d05a82013-09-24 09:57:58 -0700936 i915_vma_move_to_active(vma, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000937 if (obj->base.write_domain) {
938 obj->dirty = 1;
Chris Wilson9d7730912012-11-27 16:22:52 +0000939 obj->last_write_seqno = intel_ring_get_seqno(ring);
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800940 /* check for potential scanout */
941 if (i915_gem_obj_ggtt_bound(obj) &&
942 i915_gem_obj_to_ggtt(obj)->pin_count)
Chris Wilsonc65355b2013-06-06 16:53:41 -0300943 intel_mark_fb_busy(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000944 }
945
Chris Wilsondb53a302011-02-03 11:57:46 +0000946 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000947 }
948}
949
Chris Wilson54cf91d2010-11-25 18:00:26 +0000950static void
951i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000952 struct drm_file *file,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300953 struct intel_ring_buffer *ring,
954 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000955{
Daniel Vettercc889e02012-06-13 20:45:19 +0200956 /* Unconditionally force add_request to emit a full flush. */
957 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000958
Chris Wilson432e58e2010-11-25 19:32:06 +0000959 /* Add a breadcrumb for the completion of the batch buffer */
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300960 (void)__i915_add_request(ring, file, obj, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000961}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000962
963static int
Eric Anholtae662d32012-01-03 09:23:29 -0800964i915_reset_gen7_sol_offsets(struct drm_device *dev,
965 struct intel_ring_buffer *ring)
966{
967 drm_i915_private_t *dev_priv = dev->dev_private;
968 int ret, i;
969
970 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
971 return 0;
972
973 ret = intel_ring_begin(ring, 4 * 3);
974 if (ret)
975 return ret;
976
977 for (i = 0; i < 4; i++) {
978 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
979 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
980 intel_ring_emit(ring, 0);
981 }
982
983 intel_ring_advance(ring);
984
985 return 0;
986}
987
988static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000989i915_gem_do_execbuffer(struct drm_device *dev, void *data,
990 struct drm_file *file,
991 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -0800992 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000993{
994 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +0200995 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000996 struct drm_i915_gem_object *batch_obj;
997 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000998 struct intel_ring_buffer *ring;
Ben Widawsky41bde552013-12-06 14:11:21 -0800999 struct i915_hw_context *ctx;
1000 struct i915_address_space *vm;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001001 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001002 u32 exec_start = args->batch_start_offset, exec_len;
Daniel Vettered5982e2013-01-17 22:23:36 +01001003 u32 mask, flags;
Chris Wilson72bfa192010-12-19 11:42:05 +00001004 int ret, mode, i;
Daniel Vettered5982e2013-01-17 22:23:36 +01001005 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001006
Daniel Vettered5982e2013-01-17 22:23:36 +01001007 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001008 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001009
1010 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001011 if (ret)
1012 return ret;
1013
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001014 flags = 0;
1015 if (args->flags & I915_EXEC_SECURE) {
1016 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1017 return -EPERM;
1018
1019 flags |= I915_DISPATCH_SECURE;
1020 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001021 if (args->flags & I915_EXEC_IS_PINNED)
1022 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001023
Ben Widawskyca01b122013-12-06 14:11:00 -08001024 if ((args->flags & I915_EXEC_RING_MASK) > I915_NUM_RINGS) {
Daniel Vetterff240192012-01-31 21:08:14 +01001025 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001026 (int)(args->flags & I915_EXEC_RING_MASK));
1027 return -EINVAL;
1028 }
Ben Widawskyca01b122013-12-06 14:11:00 -08001029
1030 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1031 ring = &dev_priv->ring[RCS];
1032 else
1033 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1034
Chris Wilsona15817c2012-05-11 14:29:31 +01001035 if (!intel_ring_initialized(ring)) {
1036 DRM_DEBUG("execbuf with invalid ring: %d\n",
1037 (int)(args->flags & I915_EXEC_RING_MASK));
1038 return -EINVAL;
1039 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001040
Chris Wilson72bfa192010-12-19 11:42:05 +00001041 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001042 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +00001043 switch (mode) {
1044 case I915_EXEC_CONSTANTS_REL_GENERAL:
1045 case I915_EXEC_CONSTANTS_ABSOLUTE:
1046 case I915_EXEC_CONSTANTS_REL_SURFACE:
1047 if (ring == &dev_priv->ring[RCS] &&
1048 mode != dev_priv->relative_constants_mode) {
1049 if (INTEL_INFO(dev)->gen < 4)
1050 return -EINVAL;
1051
1052 if (INTEL_INFO(dev)->gen > 5 &&
1053 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1054 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001055
1056 /* The HW changed the meaning on this bit on gen6 */
1057 if (INTEL_INFO(dev)->gen >= 6)
1058 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +00001059 }
1060 break;
1061 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001062 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +00001063 return -EINVAL;
1064 }
1065
Chris Wilson54cf91d2010-11-25 18:00:26 +00001066 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001067 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001068 return -EINVAL;
1069 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001070
1071 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001072 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +01001073 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001074 return -EINVAL;
1075 }
1076
Daniel Vetter6ebebc92012-04-26 23:28:11 +02001077 if (INTEL_INFO(dev)->gen >= 5) {
1078 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1079 return -EINVAL;
1080 }
1081
Xi Wang44afb3a2012-04-23 04:06:42 -04001082 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1083 DRM_DEBUG("execbuf with %u cliprects\n",
1084 args->num_cliprects);
1085 return -EINVAL;
1086 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001087
Daniel Vettera1e22652013-09-21 00:35:38 +02001088 cliprects = kcalloc(args->num_cliprects,
1089 sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001090 GFP_KERNEL);
1091 if (cliprects == NULL) {
1092 ret = -ENOMEM;
1093 goto pre_mutex_err;
1094 }
1095
Chris Wilson432e58e2010-11-25 19:32:06 +00001096 if (copy_from_user(cliprects,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001097 to_user_ptr(args->cliprects_ptr),
1098 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001099 ret = -EFAULT;
1100 goto pre_mutex_err;
1101 }
1102 }
1103
Chris Wilson54cf91d2010-11-25 18:00:26 +00001104 ret = i915_mutex_lock_interruptible(dev);
1105 if (ret)
1106 goto pre_mutex_err;
1107
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001108 if (dev_priv->ums.mm_suspended) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001109 mutex_unlock(&dev->struct_mutex);
1110 ret = -EBUSY;
1111 goto pre_mutex_err;
1112 }
1113
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001114 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -08001115 if (IS_ERR_OR_NULL(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001116 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001117 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001118 goto pre_mutex_err;
Ben Widawsky41bde552013-12-06 14:11:21 -08001119 }
1120
1121 i915_gem_context_reference(ctx);
1122
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001123 vm = ctx->vm;
1124 if (!USES_FULL_PPGTT(dev))
1125 vm = &dev_priv->gtt.base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001126
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001127 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001128 if (eb == NULL) {
1129 mutex_unlock(&dev->struct_mutex);
1130 ret = -ENOMEM;
1131 goto pre_mutex_err;
1132 }
1133
Chris Wilson54cf91d2010-11-25 18:00:26 +00001134 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001135 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001136 if (ret)
1137 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001138
Chris Wilson6fe4f142011-01-10 17:35:37 +00001139 /* take note of the batch buffer before we might reorder the lists */
Ben Widawsky27173f12013-08-14 11:38:36 +02001140 batch_obj = list_entry(eb->vmas.prev, struct i915_vma, exec_list)->obj;
Chris Wilson6fe4f142011-01-10 17:35:37 +00001141
Chris Wilson54cf91d2010-11-25 18:00:26 +00001142 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001143 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001144 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001145 if (ret)
1146 goto err;
1147
1148 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001149 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001150 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001151 if (ret) {
1152 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001153 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001154 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001155 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1156 }
1157 if (ret)
1158 goto err;
1159 }
1160
1161 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001162 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001163 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001164 ret = -EINVAL;
1165 goto err;
1166 }
1167 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1168
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001169 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1170 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001171 * hsw should have this fixed, but bdw mucks it up again. */
Ben Widawsky6f65e292013-12-06 14:10:56 -08001172 if (flags & I915_DISPATCH_SECURE &&
1173 !batch_obj->has_global_gtt_mapping) {
1174 /* When we have multiple VMs, we'll need to make sure that we
1175 * allocate space first */
1176 struct i915_vma *vma = i915_gem_obj_to_ggtt(batch_obj);
1177 BUG_ON(!vma);
1178 vma->bind_vma(vma, batch_obj->cache_level, GLOBAL_BIND);
1179 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001180
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001181 if (flags & I915_DISPATCH_SECURE)
1182 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
1183 else
1184 exec_start += i915_gem_obj_offset(batch_obj, vm);
1185
Ben Widawsky27173f12013-08-14 11:38:36 +02001186 ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001187 if (ret)
1188 goto err;
1189
Ben Widawsky41bde552013-12-06 14:11:21 -08001190 ret = i915_switch_context(ring, file, ctx);
Eric Anholt0da5cec2012-07-23 12:33:55 -07001191 if (ret)
1192 goto err;
1193
Ben Widawskye2971bd2011-12-12 19:21:57 -08001194 if (ring == &dev_priv->ring[RCS] &&
1195 mode != dev_priv->relative_constants_mode) {
1196 ret = intel_ring_begin(ring, 4);
1197 if (ret)
1198 goto err;
1199
1200 intel_ring_emit(ring, MI_NOOP);
1201 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1202 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001203 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001204 intel_ring_advance(ring);
1205
1206 dev_priv->relative_constants_mode = mode;
1207 }
1208
Eric Anholtae662d32012-01-03 09:23:29 -08001209 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1210 ret = i915_reset_gen7_sol_offsets(dev, ring);
1211 if (ret)
1212 goto err;
1213 }
1214
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001215
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001216 exec_len = args->batch_len;
1217 if (cliprects) {
1218 for (i = 0; i < args->num_cliprects; i++) {
1219 ret = i915_emit_box(dev, &cliprects[i],
1220 args->DR1, args->DR4);
1221 if (ret)
1222 goto err;
1223
1224 ret = ring->dispatch_execbuffer(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001225 exec_start, exec_len,
1226 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001227 if (ret)
1228 goto err;
1229 }
1230 } else {
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001231 ret = ring->dispatch_execbuffer(ring,
1232 exec_start, exec_len,
1233 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001234 if (ret)
1235 goto err;
1236 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001237
Chris Wilson9d7730912012-11-27 16:22:52 +00001238 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1239
Ben Widawsky27173f12013-08-14 11:38:36 +02001240 i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
Mika Kuoppala7d736f42013-06-12 15:01:39 +03001241 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001242
1243err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001244 /* the request owns the ref now */
1245 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001246 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001247
1248 mutex_unlock(&dev->struct_mutex);
1249
1250pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001251 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001252 return ret;
1253}
1254
1255/*
1256 * Legacy execbuffer just creates an exec2 list from the original exec object
1257 * list array and passes it to the real function.
1258 */
1259int
1260i915_gem_execbuffer(struct drm_device *dev, void *data,
1261 struct drm_file *file)
1262{
1263 struct drm_i915_gem_execbuffer *args = data;
1264 struct drm_i915_gem_execbuffer2 exec2;
1265 struct drm_i915_gem_exec_object *exec_list = NULL;
1266 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1267 int ret, i;
1268
Chris Wilson54cf91d2010-11-25 18:00:26 +00001269 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001270 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001271 return -EINVAL;
1272 }
1273
1274 /* Copy in the exec list from userland */
1275 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1276 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1277 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001278 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001279 args->buffer_count);
1280 drm_free_large(exec_list);
1281 drm_free_large(exec2_list);
1282 return -ENOMEM;
1283 }
1284 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001285 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001286 sizeof(*exec_list) * args->buffer_count);
1287 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001288 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001289 args->buffer_count, ret);
1290 drm_free_large(exec_list);
1291 drm_free_large(exec2_list);
1292 return -EFAULT;
1293 }
1294
1295 for (i = 0; i < args->buffer_count; i++) {
1296 exec2_list[i].handle = exec_list[i].handle;
1297 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1298 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1299 exec2_list[i].alignment = exec_list[i].alignment;
1300 exec2_list[i].offset = exec_list[i].offset;
1301 if (INTEL_INFO(dev)->gen < 4)
1302 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1303 else
1304 exec2_list[i].flags = 0;
1305 }
1306
1307 exec2.buffers_ptr = args->buffers_ptr;
1308 exec2.buffer_count = args->buffer_count;
1309 exec2.batch_start_offset = args->batch_start_offset;
1310 exec2.batch_len = args->batch_len;
1311 exec2.DR1 = args->DR1;
1312 exec2.DR4 = args->DR4;
1313 exec2.num_cliprects = args->num_cliprects;
1314 exec2.cliprects_ptr = args->cliprects_ptr;
1315 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001316 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001317
Ben Widawsky41bde552013-12-06 14:11:21 -08001318 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001319 if (!ret) {
1320 /* Copy the new buffer offsets back to the user's exec list. */
1321 for (i = 0; i < args->buffer_count; i++)
1322 exec_list[i].offset = exec2_list[i].offset;
1323 /* ... and back out to userspace */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001324 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001325 exec_list,
1326 sizeof(*exec_list) * args->buffer_count);
1327 if (ret) {
1328 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001329 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001330 "back to user (%d)\n",
1331 args->buffer_count, ret);
1332 }
1333 }
1334
1335 drm_free_large(exec_list);
1336 drm_free_large(exec2_list);
1337 return ret;
1338}
1339
1340int
1341i915_gem_execbuffer2(struct drm_device *dev, void *data,
1342 struct drm_file *file)
1343{
1344 struct drm_i915_gem_execbuffer2 *args = data;
1345 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1346 int ret;
1347
Xi Wanged8cd3b2012-04-23 04:06:41 -04001348 if (args->buffer_count < 1 ||
1349 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001350 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001351 return -EINVAL;
1352 }
1353
Chris Wilson8408c282011-02-21 12:54:48 +00001354 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001355 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001356 if (exec2_list == NULL)
1357 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1358 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001359 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001360 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001361 args->buffer_count);
1362 return -ENOMEM;
1363 }
1364 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001365 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001366 sizeof(*exec2_list) * args->buffer_count);
1367 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001368 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001369 args->buffer_count, ret);
1370 drm_free_large(exec2_list);
1371 return -EFAULT;
1372 }
1373
Ben Widawsky41bde552013-12-06 14:11:21 -08001374 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
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}