blob: 2e80f8c6d12311f2147591a4c72b2e7d4365b57b [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
131 /* If we have secure dispatch, or the userspace assures us that
132 * they know what they're doing, use the GGTT VM.
133 */
134 if (exec[i].flags & EXEC_OBJECT_NEEDS_GTT ||
135 ((args->flags & I915_EXEC_SECURE) &&
136 (i == (args->buffer_count - 1))))
137 bind_vm = &dev_priv->gtt.base;
Ben Widawsky27173f12013-08-14 11:38:36 +0200138
Daniel Vettere656a6c2013-08-14 14:14:04 +0200139 /*
140 * NOTE: We can leak any vmas created here when something fails
141 * later on. But that's no issue since vma_unbind can deal with
142 * vmas which are not actually bound. And since only
143 * lookup_or_create exists as an interface to get at the vma
144 * from the (obj, vm) we don't run the risk of creating
145 * duplicated vmas for the same vm.
146 */
Ben Widawsky6f65e292013-12-06 14:10:56 -0800147 vma = i915_gem_obj_lookup_or_create_vma(obj, bind_vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200148 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200149 DRM_DEBUG("Failed to lookup VMA\n");
150 ret = PTR_ERR(vma);
151 goto out;
152 }
153
154 list_add_tail(&vma->exec_list, &eb->vmas);
155
156 vma->exec_entry = &exec[i];
157 if (eb->and < 0) {
158 eb->lut[i] = vma;
159 } else {
160 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
161 vma->exec_handle = handle;
162 hlist_add_head(&vma->exec_node,
163 &eb->buckets[handle & eb->and]);
164 }
165 ++i;
166 }
167
168
169out:
170 while (!list_empty(&objects)) {
171 obj = list_first_entry(&objects,
172 struct drm_i915_gem_object,
173 obj_exec_link);
174 list_del_init(&obj->obj_exec_link);
175 if (ret)
176 drm_gem_object_unreference(&obj->base);
177 }
178 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000179}
180
Ben Widawsky27173f12013-08-14 11:38:36 +0200181static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000182{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000183 if (eb->and < 0) {
184 if (handle >= -eb->and)
185 return NULL;
186 return eb->lut[handle];
187 } else {
188 struct hlist_head *head;
189 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000190
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000191 head = &eb->buckets[handle & eb->and];
192 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200193 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000194
Ben Widawsky27173f12013-08-14 11:38:36 +0200195 vma = hlist_entry(node, struct i915_vma, exec_node);
196 if (vma->exec_handle == handle)
197 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000198 }
199 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000200 }
Chris Wilson67731b82010-12-08 10:38:14 +0000201}
202
Chris Wilsona415d352013-11-26 11:23:15 +0000203static void
204i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
205{
206 struct drm_i915_gem_exec_object2 *entry;
207 struct drm_i915_gem_object *obj = vma->obj;
208
209 if (!drm_mm_node_allocated(&vma->node))
210 return;
211
212 entry = vma->exec_entry;
213
214 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
215 i915_gem_object_unpin_fence(obj);
216
217 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Daniel Vetter3d7f0f92013-12-18 16:23:37 +0100218 vma->pin_count--;
Chris Wilsona415d352013-11-26 11:23:15 +0000219
220 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
221}
222
223static void eb_destroy(struct eb_vmas *eb)
224{
Ben Widawsky27173f12013-08-14 11:38:36 +0200225 while (!list_empty(&eb->vmas)) {
226 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000227
Ben Widawsky27173f12013-08-14 11:38:36 +0200228 vma = list_first_entry(&eb->vmas,
229 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000230 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200231 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000232 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200233 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000234 }
Chris Wilson67731b82010-12-08 10:38:14 +0000235 kfree(eb);
236}
237
Chris Wilsondabdfe02012-03-26 10:10:27 +0200238static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
239{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300240 return (HAS_LLC(obj->base.dev) ||
241 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100242 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200243 obj->cache_level != I915_CACHE_NONE);
244}
245
Chris Wilson54cf91d2010-11-25 18:00:26 +0000246static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100247relocate_entry_cpu(struct drm_i915_gem_object *obj,
248 struct drm_i915_gem_relocation_entry *reloc)
249{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700250 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100251 uint32_t page_offset = offset_in_page(reloc->offset);
252 char *vaddr;
253 int ret = -EINVAL;
254
Chris Wilson2cc86b82013-08-26 19:51:00 -0300255 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100256 if (ret)
257 return ret;
258
259 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
260 reloc->offset >> PAGE_SHIFT));
261 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700262
263 if (INTEL_INFO(dev)->gen >= 8) {
264 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
265
266 if (page_offset == 0) {
267 kunmap_atomic(vaddr);
268 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
269 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
270 }
271
272 *(uint32_t *)(vaddr + page_offset) = 0;
273 }
274
Rafael Barbalho5032d872013-08-21 17:10:51 +0100275 kunmap_atomic(vaddr);
276
277 return 0;
278}
279
280static int
281relocate_entry_gtt(struct drm_i915_gem_object *obj,
282 struct drm_i915_gem_relocation_entry *reloc)
283{
284 struct drm_device *dev = obj->base.dev;
285 struct drm_i915_private *dev_priv = dev->dev_private;
286 uint32_t __iomem *reloc_entry;
287 void __iomem *reloc_page;
288 int ret = -EINVAL;
289
290 ret = i915_gem_object_set_to_gtt_domain(obj, true);
291 if (ret)
292 return ret;
293
294 ret = i915_gem_object_put_fence(obj);
295 if (ret)
296 return ret;
297
298 /* Map the page containing the relocation we're going to perform. */
299 reloc->offset += i915_gem_obj_ggtt_offset(obj);
300 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
301 reloc->offset & PAGE_MASK);
302 reloc_entry = (uint32_t __iomem *)
303 (reloc_page + offset_in_page(reloc->offset));
304 iowrite32(reloc->delta, reloc_entry);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700305
306 if (INTEL_INFO(dev)->gen >= 8) {
307 reloc_entry += 1;
308
309 if (offset_in_page(reloc->offset + sizeof(uint32_t)) == 0) {
310 io_mapping_unmap_atomic(reloc_page);
311 reloc_page = io_mapping_map_atomic_wc(
312 dev_priv->gtt.mappable,
313 reloc->offset + sizeof(uint32_t));
314 reloc_entry = reloc_page;
315 }
316
317 iowrite32(0, reloc_entry);
318 }
319
Rafael Barbalho5032d872013-08-21 17:10:51 +0100320 io_mapping_unmap_atomic(reloc_page);
321
322 return 0;
323}
324
325static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000326i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200327 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800328 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000329{
330 struct drm_device *dev = obj->base.dev;
331 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100332 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200333 struct i915_vma *target_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000334 uint32_t target_offset;
335 int ret = -EINVAL;
336
Chris Wilson67731b82010-12-08 10:38:14 +0000337 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200338 target_vma = eb_get_vma(eb, reloc->target_handle);
339 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000340 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200341 target_i915_obj = target_vma->obj;
342 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000343
Ben Widawsky5ce09722013-11-25 09:54:40 -0800344 target_offset = target_vma->node.start;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000345
Eric Anholte844b992012-07-31 15:35:01 -0700346 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
347 * pipe_control writes because the gpu doesn't properly redirect them
348 * through the ppgtt for non_secure batchbuffers. */
349 if (unlikely(IS_GEN6(dev) &&
350 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
351 !target_i915_obj->has_global_gtt_mapping)) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800352 struct i915_vma *vma =
353 list_first_entry(&target_i915_obj->vma_list,
354 typeof(*vma), vma_link);
Ben Widawsky6f65e292013-12-06 14:10:56 -0800355 vma->bind_vma(vma, target_i915_obj->cache_level, GLOBAL_BIND);
Eric Anholte844b992012-07-31 15:35:01 -0700356 }
357
Chris Wilson54cf91d2010-11-25 18:00:26 +0000358 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000359 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100360 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000361 "obj %p target %d offset %d "
362 "read %08x write %08x",
363 obj, reloc->target_handle,
364 (int) reloc->offset,
365 reloc->read_domains,
366 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000367 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000368 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100369 if (unlikely((reloc->write_domain | reloc->read_domains)
370 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100371 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000372 "obj %p target %d offset %d "
373 "read %08x write %08x",
374 obj, reloc->target_handle,
375 (int) reloc->offset,
376 reloc->read_domains,
377 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000378 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000379 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000380
381 target_obj->pending_read_domains |= reloc->read_domains;
382 target_obj->pending_write_domain |= reloc->write_domain;
383
384 /* If the relocation already has the right value in it, no
385 * more work needs to be done.
386 */
387 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000388 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000389
390 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700391 if (unlikely(reloc->offset >
392 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100393 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000394 "obj %p target %d offset %d size %d.\n",
395 obj, reloc->target_handle,
396 (int) reloc->offset,
397 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000398 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000399 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000400 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100401 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000402 "obj %p target %d offset %d.\n",
403 obj, reloc->target_handle,
404 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000405 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000406 }
407
Chris Wilsondabdfe02012-03-26 10:10:27 +0200408 /* We can't wait for rendering with pagefaults disabled */
409 if (obj->active && in_atomic())
410 return -EFAULT;
411
Chris Wilson54cf91d2010-11-25 18:00:26 +0000412 reloc->delta += target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100413 if (use_cpu_reloc(obj))
414 ret = relocate_entry_cpu(obj, reloc);
415 else
416 ret = relocate_entry_gtt(obj, reloc);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000417
Daniel Vetterd4d36012013-09-02 20:56:23 +0200418 if (ret)
419 return ret;
420
Chris Wilson54cf91d2010-11-25 18:00:26 +0000421 /* and update the user's relocation entry */
422 reloc->presumed_offset = target_offset;
423
Chris Wilson67731b82010-12-08 10:38:14 +0000424 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000425}
426
427static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200428i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
429 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000430{
Chris Wilson1d83f442012-03-24 20:12:53 +0000431#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
432 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000433 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200434 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000435 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000436
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200437 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000438
Chris Wilson1d83f442012-03-24 20:12:53 +0000439 remain = entry->relocation_count;
440 while (remain) {
441 struct drm_i915_gem_relocation_entry *r = stack_reloc;
442 int count = remain;
443 if (count > ARRAY_SIZE(stack_reloc))
444 count = ARRAY_SIZE(stack_reloc);
445 remain -= count;
446
447 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000448 return -EFAULT;
449
Chris Wilson1d83f442012-03-24 20:12:53 +0000450 do {
451 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000452
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800453 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000454 if (ret)
455 return ret;
456
457 if (r->presumed_offset != offset &&
458 __copy_to_user_inatomic(&user_relocs->presumed_offset,
459 &r->presumed_offset,
460 sizeof(r->presumed_offset))) {
461 return -EFAULT;
462 }
463
464 user_relocs++;
465 r++;
466 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000467 }
468
469 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000470#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000471}
472
473static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200474i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
475 struct eb_vmas *eb,
476 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000477{
Ben Widawsky27173f12013-08-14 11:38:36 +0200478 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000479 int i, ret;
480
481 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800482 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000483 if (ret)
484 return ret;
485 }
486
487 return 0;
488}
489
490static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800491i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000492{
Ben Widawsky27173f12013-08-14 11:38:36 +0200493 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000494 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000495
Chris Wilsond4aeee72011-03-14 15:11:24 +0000496 /* This is the fast path and we cannot handle a pagefault whilst
497 * holding the struct mutex lest the user pass in the relocations
498 * contained within a mmaped bo. For in such a case we, the page
499 * fault handler would call i915_gem_fault() and we would try to
500 * acquire the struct mutex again. Obviously this is bad and so
501 * lockdep complains vehemently.
502 */
503 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200504 list_for_each_entry(vma, &eb->vmas, exec_list) {
505 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000506 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000507 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000508 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000509 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000510
Chris Wilsond4aeee72011-03-14 15:11:24 +0000511 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000512}
513
Chris Wilson1690e1e2011-12-14 13:57:08 +0100514static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200515need_reloc_mappable(struct i915_vma *vma)
Chris Wilsondabdfe02012-03-26 10:10:27 +0200516{
Ben Widawsky27173f12013-08-14 11:38:36 +0200517 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
518 return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
519 i915_is_ggtt(vma->vm);
Chris Wilsondabdfe02012-03-26 10:10:27 +0200520}
521
522static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200523i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
524 struct intel_ring_buffer *ring,
525 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100526{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800527 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200528 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100529 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
530 bool need_fence, need_mappable;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800531 u32 flags = (entry->flags & EXEC_OBJECT_NEEDS_GTT) &&
532 !vma->obj->has_global_gtt_mapping ? GLOBAL_BIND : 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100533 int ret;
534
535 need_fence =
536 has_fenced_gpu_access &&
537 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
538 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200539 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100540
Ben Widawsky27173f12013-08-14 11:38:36 +0200541 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, need_mappable,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700542 false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100543 if (ret)
544 return ret;
545
Chris Wilson7788a762012-08-24 19:18:18 +0100546 entry->flags |= __EXEC_OBJECT_HAS_PIN;
547
Chris Wilson1690e1e2011-12-14 13:57:08 +0100548 if (has_fenced_gpu_access) {
549 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100550 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000551 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100552 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100553
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000554 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100555 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000556
Chris Wilson7dd49062012-03-21 10:48:18 +0000557 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100558 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100559 }
560
Ben Widawsky27173f12013-08-14 11:38:36 +0200561 if (entry->offset != vma->node.start) {
562 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100563 *need_reloc = true;
564 }
565
566 if (entry->flags & EXEC_OBJECT_WRITE) {
567 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
568 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
569 }
570
Ben Widawsky6f65e292013-12-06 14:10:56 -0800571 vma->bind_vma(vma, obj->cache_level, flags);
Daniel Vettered5982e2013-01-17 22:23:36 +0100572
Chris Wilson1690e1e2011-12-14 13:57:08 +0100573 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100574}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100575
Chris Wilson54cf91d2010-11-25 18:00:26 +0000576static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000577i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200578 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100579 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000580{
Chris Wilson432e58e2010-11-25 19:32:06 +0000581 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200582 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700583 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200584 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100585 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
586 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000587
Ben Widawsky68c8c172013-09-11 14:57:50 -0700588 if (list_empty(vmas))
589 return 0;
590
591 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
592
Ben Widawsky27173f12013-08-14 11:38:36 +0200593 INIT_LIST_HEAD(&ordered_vmas);
594 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000595 struct drm_i915_gem_exec_object2 *entry;
596 bool need_fence, need_mappable;
597
Ben Widawsky27173f12013-08-14 11:38:36 +0200598 vma = list_first_entry(vmas, struct i915_vma, exec_list);
599 obj = vma->obj;
600 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000601
602 need_fence =
603 has_fenced_gpu_access &&
604 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
605 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200606 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000607
608 if (need_mappable)
Ben Widawsky27173f12013-08-14 11:38:36 +0200609 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000610 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200611 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000612
Daniel Vettered5982e2013-01-17 22:23:36 +0100613 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000614 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100615 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000616 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200617 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000618
619 /* Attempt to pin all of the buffers into the GTT.
620 * This is done in 3 phases:
621 *
622 * 1a. Unbind all objects that do not match the GTT constraints for
623 * the execbuffer (fenceable, mappable, alignment etc).
624 * 1b. Increment pin count for already bound objects.
625 * 2. Bind new objects.
626 * 3. Decrement pin count.
627 *
Chris Wilson7788a762012-08-24 19:18:18 +0100628 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000629 * room for the earlier objects *unless* we need to defragment.
630 */
631 retry = 0;
632 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100633 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000634
635 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200636 list_for_each_entry(vma, vmas, exec_list) {
637 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000638 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100639
Ben Widawsky27173f12013-08-14 11:38:36 +0200640 obj = vma->obj;
641
642 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000643 continue;
644
645 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000646 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000647 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
648 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200649 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000650
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700651 WARN_ON((need_mappable || need_fence) &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200652 !i915_is_ggtt(vma->vm));
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700653
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700654 if ((entry->alignment &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200655 vma->node.start & (entry->alignment - 1)) ||
Chris Wilson54cf91d2010-11-25 18:00:26 +0000656 (need_mappable && !obj->map_and_fenceable))
Ben Widawsky27173f12013-08-14 11:38:36 +0200657 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000658 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200659 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000660 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000661 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000662 }
663
664 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200665 list_for_each_entry(vma, vmas, exec_list) {
666 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100667 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000668
Ben Widawsky27173f12013-08-14 11:38:36 +0200669 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100670 if (ret)
671 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000672 }
673
Chris Wilsona415d352013-11-26 11:23:15 +0000674err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200675 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000676 return ret;
677
Chris Wilsona415d352013-11-26 11:23:15 +0000678 /* Decrement pin count for bound objects */
679 list_for_each_entry(vma, vmas, exec_list)
680 i915_gem_execbuffer_unreserve_vma(vma);
681
Ben Widawsky68c8c172013-09-11 14:57:50 -0700682 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000683 if (ret)
684 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000685 } while (1);
686}
687
688static int
689i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100690 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000691 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000692 struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200693 struct eb_vmas *eb,
694 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000695{
696 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200697 struct i915_address_space *vm;
698 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100699 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000700 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000701 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200702 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000703
Ben Widawsky27173f12013-08-14 11:38:36 +0200704 if (WARN_ON(list_empty(&eb->vmas)))
705 return 0;
706
707 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
708
Chris Wilson67731b82010-12-08 10:38:14 +0000709 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200710 while (!list_empty(&eb->vmas)) {
711 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
712 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000713 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200714 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000715 }
716
Chris Wilson54cf91d2010-11-25 18:00:26 +0000717 mutex_unlock(&dev->struct_mutex);
718
719 total = 0;
720 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000721 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000722
Chris Wilsondd6864a2011-01-12 23:49:13 +0000723 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000724 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000725 if (reloc == NULL || reloc_offset == NULL) {
726 drm_free_large(reloc);
727 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000728 mutex_lock(&dev->struct_mutex);
729 return -ENOMEM;
730 }
731
732 total = 0;
733 for (i = 0; i < count; i++) {
734 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000735 u64 invalid_offset = (u64)-1;
736 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000737
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200738 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000739
740 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000741 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000742 ret = -EFAULT;
743 mutex_lock(&dev->struct_mutex);
744 goto err;
745 }
746
Chris Wilson262b6d32013-01-15 16:17:54 +0000747 /* As we do not update the known relocation offsets after
748 * relocating (due to the complexities in lock handling),
749 * we need to mark them as invalid now so that we force the
750 * relocation processing next time. Just in case the target
751 * object is evicted and then rebound into its old
752 * presumed_offset before the next execbuffer - if that
753 * happened we would make the mistake of assuming that the
754 * relocations were valid.
755 */
756 for (j = 0; j < exec[i].relocation_count; j++) {
757 if (copy_to_user(&user_relocs[j].presumed_offset,
758 &invalid_offset,
759 sizeof(invalid_offset))) {
760 ret = -EFAULT;
761 mutex_lock(&dev->struct_mutex);
762 goto err;
763 }
764 }
765
Chris Wilsondd6864a2011-01-12 23:49:13 +0000766 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000767 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000768 }
769
770 ret = i915_mutex_lock_interruptible(dev);
771 if (ret) {
772 mutex_lock(&dev->struct_mutex);
773 goto err;
774 }
775
Chris Wilson67731b82010-12-08 10:38:14 +0000776 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000777 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200778 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000779 if (ret)
780 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000781
Daniel Vettered5982e2013-01-17 22:23:36 +0100782 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200783 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000784 if (ret)
785 goto err;
786
Ben Widawsky27173f12013-08-14 11:38:36 +0200787 list_for_each_entry(vma, &eb->vmas, exec_list) {
788 int offset = vma->exec_entry - exec;
789 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
790 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000791 if (ret)
792 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000793 }
794
795 /* Leave the user relocations as are, this is the painfully slow path,
796 * and we want to avoid the complication of dropping the lock whilst
797 * having buffers reserved in the aperture and so causing spurious
798 * ENOSPC for random operations.
799 */
800
801err:
802 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000803 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000804 return ret;
805}
806
Chris Wilson54cf91d2010-11-25 18:00:26 +0000807static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000808i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200809 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000810{
Ben Widawsky27173f12013-08-14 11:38:36 +0200811 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200812 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100813 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000814 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000815
Ben Widawsky27173f12013-08-14 11:38:36 +0200816 list_for_each_entry(vma, vmas, exec_list) {
817 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700818 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000819 if (ret)
820 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200821
822 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100823 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200824
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200825 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000826 }
827
Chris Wilson000433b2013-08-08 14:41:09 +0100828 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800829 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200830
831 if (flush_domains & I915_GEM_DOMAIN_GTT)
832 wmb();
833
Chris Wilson09cf7c92012-07-13 14:14:08 +0100834 /* Unconditionally invalidate gpu caches and ensure that we do flush
835 * any residual writes from the previous batch.
836 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100837 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000838}
839
Chris Wilson432e58e2010-11-25 19:32:06 +0000840static bool
841i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000842{
Daniel Vettered5982e2013-01-17 22:23:36 +0100843 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
844 return false;
845
Chris Wilson432e58e2010-11-25 19:32:06 +0000846 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000847}
848
849static int
850validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
851 int count)
852{
853 int i;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200854 unsigned relocs_total = 0;
855 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000856
857 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200858 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000859 int length; /* limited by fault_in_pages_readable() */
860
Daniel Vettered5982e2013-01-17 22:23:36 +0100861 if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
862 return -EINVAL;
863
Kees Cook3118a4f2013-03-11 17:31:45 -0700864 /* First check for malicious input causing overflow in
865 * the worst case where we need to allocate the entire
866 * relocation tree as a single array.
867 */
868 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000869 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700870 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000871
872 length = exec[i].relocation_count *
873 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700874 /*
875 * We must check that the entire relocation array is safe
876 * to read, but since we may need to update the presumed
877 * offsets during execution, check for full write access.
878 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000879 if (!access_ok(VERIFY_WRITE, ptr, length))
880 return -EFAULT;
881
Xiong Zhang0b74b502013-07-19 13:51:24 +0800882 if (likely(!i915_prefault_disable)) {
883 if (fault_in_multipages_readable(ptr, length))
884 return -EFAULT;
885 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000886 }
887
888 return 0;
889}
890
Ben Widawsky41bde552013-12-06 14:11:21 -0800891static struct i915_hw_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200892i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
893 const u32 ctx_id)
894{
Ben Widawsky41bde552013-12-06 14:11:21 -0800895 struct i915_hw_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200896 struct i915_ctx_hang_stats *hs;
897
Ben Widawsky41bde552013-12-06 14:11:21 -0800898 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
899 if (IS_ERR_OR_NULL(ctx))
900 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200901
Ben Widawsky41bde552013-12-06 14:11:21 -0800902 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200903 if (hs->banned) {
904 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -0800905 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200906 }
907
Ben Widawsky41bde552013-12-06 14:11:21 -0800908 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200909}
910
Chris Wilson432e58e2010-11-25 19:32:06 +0000911static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200912i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Chris Wilson9d7730912012-11-27 16:22:52 +0000913 struct intel_ring_buffer *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000914{
Ben Widawsky27173f12013-08-14 11:38:36 +0200915 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000916
Ben Widawsky27173f12013-08-14 11:38:36 +0200917 list_for_each_entry(vma, vmas, exec_list) {
918 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100919 u32 old_read = obj->base.read_domains;
920 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000921
Chris Wilson432e58e2010-11-25 19:32:06 +0000922 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100923 if (obj->base.write_domain == 0)
924 obj->base.pending_read_domains |= obj->base.read_domains;
925 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000926 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
927
Ben Widawskye2d05a82013-09-24 09:57:58 -0700928 i915_vma_move_to_active(vma, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000929 if (obj->base.write_domain) {
930 obj->dirty = 1;
Chris Wilson9d7730912012-11-27 16:22:52 +0000931 obj->last_write_seqno = intel_ring_get_seqno(ring);
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800932 /* check for potential scanout */
933 if (i915_gem_obj_ggtt_bound(obj) &&
934 i915_gem_obj_to_ggtt(obj)->pin_count)
Chris Wilsonc65355b2013-06-06 16:53:41 -0300935 intel_mark_fb_busy(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000936 }
937
Chris Wilsondb53a302011-02-03 11:57:46 +0000938 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000939 }
940}
941
Chris Wilson54cf91d2010-11-25 18:00:26 +0000942static void
943i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000944 struct drm_file *file,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300945 struct intel_ring_buffer *ring,
946 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000947{
Daniel Vettercc889e02012-06-13 20:45:19 +0200948 /* Unconditionally force add_request to emit a full flush. */
949 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000950
Chris Wilson432e58e2010-11-25 19:32:06 +0000951 /* Add a breadcrumb for the completion of the batch buffer */
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300952 (void)__i915_add_request(ring, file, obj, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000953}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000954
955static int
Eric Anholtae662d32012-01-03 09:23:29 -0800956i915_reset_gen7_sol_offsets(struct drm_device *dev,
957 struct intel_ring_buffer *ring)
958{
959 drm_i915_private_t *dev_priv = dev->dev_private;
960 int ret, i;
961
962 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
963 return 0;
964
965 ret = intel_ring_begin(ring, 4 * 3);
966 if (ret)
967 return ret;
968
969 for (i = 0; i < 4; i++) {
970 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
971 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
972 intel_ring_emit(ring, 0);
973 }
974
975 intel_ring_advance(ring);
976
977 return 0;
978}
979
980static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000981i915_gem_do_execbuffer(struct drm_device *dev, void *data,
982 struct drm_file *file,
983 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -0800984 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000985{
986 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +0200987 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000988 struct drm_i915_gem_object *batch_obj;
989 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000990 struct intel_ring_buffer *ring;
Ben Widawsky41bde552013-12-06 14:11:21 -0800991 struct i915_hw_context *ctx;
992 struct i915_address_space *vm;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200993 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -0800994 u32 exec_start = args->batch_start_offset, exec_len;
Daniel Vettered5982e2013-01-17 22:23:36 +0100995 u32 mask, flags;
Chris Wilson72bfa192010-12-19 11:42:05 +0000996 int ret, mode, i;
Daniel Vettered5982e2013-01-17 22:23:36 +0100997 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000998
Daniel Vettered5982e2013-01-17 22:23:36 +0100999 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001000 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001001
1002 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001003 if (ret)
1004 return ret;
1005
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001006 flags = 0;
1007 if (args->flags & I915_EXEC_SECURE) {
1008 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1009 return -EPERM;
1010
1011 flags |= I915_DISPATCH_SECURE;
1012 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001013 if (args->flags & I915_EXEC_IS_PINNED)
1014 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001015
Ben Widawskyca01b122013-12-06 14:11:00 -08001016 if ((args->flags & I915_EXEC_RING_MASK) > I915_NUM_RINGS) {
Daniel Vetterff240192012-01-31 21:08:14 +01001017 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001018 (int)(args->flags & I915_EXEC_RING_MASK));
1019 return -EINVAL;
1020 }
Ben Widawskyca01b122013-12-06 14:11:00 -08001021
1022 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1023 ring = &dev_priv->ring[RCS];
1024 else
1025 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1026
Chris Wilsona15817c2012-05-11 14:29:31 +01001027 if (!intel_ring_initialized(ring)) {
1028 DRM_DEBUG("execbuf with invalid ring: %d\n",
1029 (int)(args->flags & I915_EXEC_RING_MASK));
1030 return -EINVAL;
1031 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001032
Chris Wilson72bfa192010-12-19 11:42:05 +00001033 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001034 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +00001035 switch (mode) {
1036 case I915_EXEC_CONSTANTS_REL_GENERAL:
1037 case I915_EXEC_CONSTANTS_ABSOLUTE:
1038 case I915_EXEC_CONSTANTS_REL_SURFACE:
1039 if (ring == &dev_priv->ring[RCS] &&
1040 mode != dev_priv->relative_constants_mode) {
1041 if (INTEL_INFO(dev)->gen < 4)
1042 return -EINVAL;
1043
1044 if (INTEL_INFO(dev)->gen > 5 &&
1045 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1046 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001047
1048 /* The HW changed the meaning on this bit on gen6 */
1049 if (INTEL_INFO(dev)->gen >= 6)
1050 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +00001051 }
1052 break;
1053 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001054 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +00001055 return -EINVAL;
1056 }
1057
Chris Wilson54cf91d2010-11-25 18:00:26 +00001058 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001059 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001060 return -EINVAL;
1061 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001062
1063 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001064 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +01001065 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001066 return -EINVAL;
1067 }
1068
Daniel Vetter6ebebc92012-04-26 23:28:11 +02001069 if (INTEL_INFO(dev)->gen >= 5) {
1070 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1071 return -EINVAL;
1072 }
1073
Xi Wang44afb3a2012-04-23 04:06:42 -04001074 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1075 DRM_DEBUG("execbuf with %u cliprects\n",
1076 args->num_cliprects);
1077 return -EINVAL;
1078 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001079
Daniel Vettera1e22652013-09-21 00:35:38 +02001080 cliprects = kcalloc(args->num_cliprects,
1081 sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001082 GFP_KERNEL);
1083 if (cliprects == NULL) {
1084 ret = -ENOMEM;
1085 goto pre_mutex_err;
1086 }
1087
Chris Wilson432e58e2010-11-25 19:32:06 +00001088 if (copy_from_user(cliprects,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001089 to_user_ptr(args->cliprects_ptr),
1090 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001091 ret = -EFAULT;
1092 goto pre_mutex_err;
1093 }
1094 }
1095
Chris Wilson54cf91d2010-11-25 18:00:26 +00001096 ret = i915_mutex_lock_interruptible(dev);
1097 if (ret)
1098 goto pre_mutex_err;
1099
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001100 if (dev_priv->ums.mm_suspended) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001101 mutex_unlock(&dev->struct_mutex);
1102 ret = -EBUSY;
1103 goto pre_mutex_err;
1104 }
1105
Ben Widawsky41bde552013-12-06 14:11:21 -08001106 ctx = i915_gem_validate_context(dev, file, ctx_id);
1107 if (IS_ERR_OR_NULL(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001108 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001109 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001110 goto pre_mutex_err;
Ben Widawsky41bde552013-12-06 14:11:21 -08001111 }
1112
1113 i915_gem_context_reference(ctx);
1114
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001115 vm = ctx->vm;
1116 if (!USES_FULL_PPGTT(dev))
1117 vm = &dev_priv->gtt.base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001118
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001119 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001120 if (eb == NULL) {
1121 mutex_unlock(&dev->struct_mutex);
1122 ret = -ENOMEM;
1123 goto pre_mutex_err;
1124 }
1125
Chris Wilson54cf91d2010-11-25 18:00:26 +00001126 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001127 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001128 if (ret)
1129 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001130
Chris Wilson6fe4f142011-01-10 17:35:37 +00001131 /* take note of the batch buffer before we might reorder the lists */
Ben Widawsky27173f12013-08-14 11:38:36 +02001132 batch_obj = list_entry(eb->vmas.prev, struct i915_vma, exec_list)->obj;
Chris Wilson6fe4f142011-01-10 17:35:37 +00001133
Chris Wilson54cf91d2010-11-25 18:00:26 +00001134 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001135 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001136 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001137 if (ret)
1138 goto err;
1139
1140 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001141 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001142 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001143 if (ret) {
1144 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001145 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001146 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001147 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1148 }
1149 if (ret)
1150 goto err;
1151 }
1152
1153 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001154 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001155 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001156 ret = -EINVAL;
1157 goto err;
1158 }
1159 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1160
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001161 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1162 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001163 * hsw should have this fixed, but bdw mucks it up again. */
Ben Widawsky6f65e292013-12-06 14:10:56 -08001164 if (flags & I915_DISPATCH_SECURE &&
1165 !batch_obj->has_global_gtt_mapping) {
1166 /* When we have multiple VMs, we'll need to make sure that we
1167 * allocate space first */
1168 struct i915_vma *vma = i915_gem_obj_to_ggtt(batch_obj);
1169 BUG_ON(!vma);
1170 vma->bind_vma(vma, batch_obj->cache_level, GLOBAL_BIND);
1171 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001172
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001173 if (flags & I915_DISPATCH_SECURE)
1174 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
1175 else
1176 exec_start += i915_gem_obj_offset(batch_obj, vm);
1177
Ben Widawsky27173f12013-08-14 11:38:36 +02001178 ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001179 if (ret)
1180 goto err;
1181
Ben Widawsky41bde552013-12-06 14:11:21 -08001182 ret = i915_switch_context(ring, file, ctx);
Eric Anholt0da5cec2012-07-23 12:33:55 -07001183 if (ret)
1184 goto err;
1185
Ben Widawskye2971bd2011-12-12 19:21:57 -08001186 if (ring == &dev_priv->ring[RCS] &&
1187 mode != dev_priv->relative_constants_mode) {
1188 ret = intel_ring_begin(ring, 4);
1189 if (ret)
1190 goto err;
1191
1192 intel_ring_emit(ring, MI_NOOP);
1193 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1194 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001195 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001196 intel_ring_advance(ring);
1197
1198 dev_priv->relative_constants_mode = mode;
1199 }
1200
Eric Anholtae662d32012-01-03 09:23:29 -08001201 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1202 ret = i915_reset_gen7_sol_offsets(dev, ring);
1203 if (ret)
1204 goto err;
1205 }
1206
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001207
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001208 exec_len = args->batch_len;
1209 if (cliprects) {
1210 for (i = 0; i < args->num_cliprects; i++) {
1211 ret = i915_emit_box(dev, &cliprects[i],
1212 args->DR1, args->DR4);
1213 if (ret)
1214 goto err;
1215
1216 ret = ring->dispatch_execbuffer(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001217 exec_start, exec_len,
1218 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001219 if (ret)
1220 goto err;
1221 }
1222 } else {
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001223 ret = ring->dispatch_execbuffer(ring,
1224 exec_start, exec_len,
1225 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001226 if (ret)
1227 goto err;
1228 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001229
Chris Wilson9d7730912012-11-27 16:22:52 +00001230 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1231
Ben Widawsky27173f12013-08-14 11:38:36 +02001232 i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
Mika Kuoppala7d736f42013-06-12 15:01:39 +03001233 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001234
1235err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001236 /* the request owns the ref now */
1237 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001238 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001239
1240 mutex_unlock(&dev->struct_mutex);
1241
1242pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001243 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001244 return ret;
1245}
1246
1247/*
1248 * Legacy execbuffer just creates an exec2 list from the original exec object
1249 * list array and passes it to the real function.
1250 */
1251int
1252i915_gem_execbuffer(struct drm_device *dev, void *data,
1253 struct drm_file *file)
1254{
1255 struct drm_i915_gem_execbuffer *args = data;
1256 struct drm_i915_gem_execbuffer2 exec2;
1257 struct drm_i915_gem_exec_object *exec_list = NULL;
1258 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1259 int ret, i;
1260
Chris Wilson54cf91d2010-11-25 18:00:26 +00001261 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001262 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001263 return -EINVAL;
1264 }
1265
1266 /* Copy in the exec list from userland */
1267 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1268 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1269 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001270 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001271 args->buffer_count);
1272 drm_free_large(exec_list);
1273 drm_free_large(exec2_list);
1274 return -ENOMEM;
1275 }
1276 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001277 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001278 sizeof(*exec_list) * args->buffer_count);
1279 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001280 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001281 args->buffer_count, ret);
1282 drm_free_large(exec_list);
1283 drm_free_large(exec2_list);
1284 return -EFAULT;
1285 }
1286
1287 for (i = 0; i < args->buffer_count; i++) {
1288 exec2_list[i].handle = exec_list[i].handle;
1289 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1290 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1291 exec2_list[i].alignment = exec_list[i].alignment;
1292 exec2_list[i].offset = exec_list[i].offset;
1293 if (INTEL_INFO(dev)->gen < 4)
1294 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1295 else
1296 exec2_list[i].flags = 0;
1297 }
1298
1299 exec2.buffers_ptr = args->buffers_ptr;
1300 exec2.buffer_count = args->buffer_count;
1301 exec2.batch_start_offset = args->batch_start_offset;
1302 exec2.batch_len = args->batch_len;
1303 exec2.DR1 = args->DR1;
1304 exec2.DR4 = args->DR4;
1305 exec2.num_cliprects = args->num_cliprects;
1306 exec2.cliprects_ptr = args->cliprects_ptr;
1307 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001308 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001309
Ben Widawsky41bde552013-12-06 14:11:21 -08001310 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001311 if (!ret) {
1312 /* Copy the new buffer offsets back to the user's exec list. */
1313 for (i = 0; i < args->buffer_count; i++)
1314 exec_list[i].offset = exec2_list[i].offset;
1315 /* ... and back out to userspace */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001316 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001317 exec_list,
1318 sizeof(*exec_list) * args->buffer_count);
1319 if (ret) {
1320 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001321 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001322 "back to user (%d)\n",
1323 args->buffer_count, ret);
1324 }
1325 }
1326
1327 drm_free_large(exec_list);
1328 drm_free_large(exec2_list);
1329 return ret;
1330}
1331
1332int
1333i915_gem_execbuffer2(struct drm_device *dev, void *data,
1334 struct drm_file *file)
1335{
1336 struct drm_i915_gem_execbuffer2 *args = data;
1337 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1338 int ret;
1339
Xi Wanged8cd3b2012-04-23 04:06:41 -04001340 if (args->buffer_count < 1 ||
1341 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001342 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001343 return -EINVAL;
1344 }
1345
Chris Wilson8408c282011-02-21 12:54:48 +00001346 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001347 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001348 if (exec2_list == NULL)
1349 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1350 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001351 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001352 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001353 args->buffer_count);
1354 return -ENOMEM;
1355 }
1356 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001357 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001358 sizeof(*exec2_list) * args->buffer_count);
1359 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001360 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001361 args->buffer_count, ret);
1362 drm_free_large(exec2_list);
1363 return -EFAULT;
1364 }
1365
Ben Widawsky41bde552013-12-06 14:11:21 -08001366 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001367 if (!ret) {
1368 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001369 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001370 exec2_list,
1371 sizeof(*exec2_list) * args->buffer_count);
1372 if (ret) {
1373 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001374 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001375 "back to user (%d)\n",
1376 args->buffer_count, ret);
1377 }
1378 }
1379
1380 drm_free_large(exec2_list);
1381 return ret;
1382}