blob: 277485505bcabd09e4726ef5c97c10e75348274b [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 */
140 if (exec[i].flags & EXEC_OBJECT_NEEDS_GTT ||
141 ((args->flags & I915_EXEC_SECURE) &&
142 (i == (args->buffer_count - 1))))
143 bind_vm = &dev_priv->gtt.base;
Ben Widawsky27173f12013-08-14 11:38:36 +0200144
Daniel Vettere656a6c2013-08-14 14:14:04 +0200145 /*
146 * NOTE: We can leak any vmas created here when something fails
147 * later on. But that's no issue since vma_unbind can deal with
148 * vmas which are not actually bound. And since only
149 * lookup_or_create exists as an interface to get at the vma
150 * from the (obj, vm) we don't run the risk of creating
151 * duplicated vmas for the same vm.
152 */
Ben Widawsky6f65e292013-12-06 14:10:56 -0800153 vma = i915_gem_obj_lookup_or_create_vma(obj, bind_vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200154 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200155 DRM_DEBUG("Failed to lookup VMA\n");
156 ret = PTR_ERR(vma);
157 goto out;
158 }
159
160 list_add_tail(&vma->exec_list, &eb->vmas);
161
162 vma->exec_entry = &exec[i];
163 if (eb->and < 0) {
164 eb->lut[i] = vma;
165 } else {
166 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
167 vma->exec_handle = handle;
168 hlist_add_head(&vma->exec_node,
169 &eb->buckets[handle & eb->and]);
170 }
171 ++i;
172 }
173
174
175out:
176 while (!list_empty(&objects)) {
177 obj = list_first_entry(&objects,
178 struct drm_i915_gem_object,
179 obj_exec_link);
180 list_del_init(&obj->obj_exec_link);
181 if (ret)
182 drm_gem_object_unreference(&obj->base);
183 }
184 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000185}
186
Ben Widawsky27173f12013-08-14 11:38:36 +0200187static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000188{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000189 if (eb->and < 0) {
190 if (handle >= -eb->and)
191 return NULL;
192 return eb->lut[handle];
193 } else {
194 struct hlist_head *head;
195 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000196
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000197 head = &eb->buckets[handle & eb->and];
198 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200199 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000200
Ben Widawsky27173f12013-08-14 11:38:36 +0200201 vma = hlist_entry(node, struct i915_vma, exec_node);
202 if (vma->exec_handle == handle)
203 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000204 }
205 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000206 }
Chris Wilson67731b82010-12-08 10:38:14 +0000207}
208
Chris Wilsona415d352013-11-26 11:23:15 +0000209static void
210i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
211{
212 struct drm_i915_gem_exec_object2 *entry;
213 struct drm_i915_gem_object *obj = vma->obj;
214
215 if (!drm_mm_node_allocated(&vma->node))
216 return;
217
218 entry = vma->exec_entry;
219
220 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
221 i915_gem_object_unpin_fence(obj);
222
223 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Daniel Vetter3d7f0f92013-12-18 16:23:37 +0100224 vma->pin_count--;
Chris Wilsona415d352013-11-26 11:23:15 +0000225
226 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
227}
228
229static void eb_destroy(struct eb_vmas *eb)
230{
Ben Widawsky27173f12013-08-14 11:38:36 +0200231 while (!list_empty(&eb->vmas)) {
232 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000233
Ben Widawsky27173f12013-08-14 11:38:36 +0200234 vma = list_first_entry(&eb->vmas,
235 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000236 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200237 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000238 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200239 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000240 }
Chris Wilson67731b82010-12-08 10:38:14 +0000241 kfree(eb);
242}
243
Chris Wilsondabdfe02012-03-26 10:10:27 +0200244static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
245{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300246 return (HAS_LLC(obj->base.dev) ||
247 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100248 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200249 obj->cache_level != I915_CACHE_NONE);
250}
251
Chris Wilson54cf91d2010-11-25 18:00:26 +0000252static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100253relocate_entry_cpu(struct drm_i915_gem_object *obj,
254 struct drm_i915_gem_relocation_entry *reloc)
255{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700256 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100257 uint32_t page_offset = offset_in_page(reloc->offset);
258 char *vaddr;
259 int ret = -EINVAL;
260
Chris Wilson2cc86b82013-08-26 19:51:00 -0300261 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100262 if (ret)
263 return ret;
264
265 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
266 reloc->offset >> PAGE_SHIFT));
267 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700268
269 if (INTEL_INFO(dev)->gen >= 8) {
270 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
271
272 if (page_offset == 0) {
273 kunmap_atomic(vaddr);
274 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
275 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
276 }
277
278 *(uint32_t *)(vaddr + page_offset) = 0;
279 }
280
Rafael Barbalho5032d872013-08-21 17:10:51 +0100281 kunmap_atomic(vaddr);
282
283 return 0;
284}
285
286static int
287relocate_entry_gtt(struct drm_i915_gem_object *obj,
288 struct drm_i915_gem_relocation_entry *reloc)
289{
290 struct drm_device *dev = obj->base.dev;
291 struct drm_i915_private *dev_priv = dev->dev_private;
292 uint32_t __iomem *reloc_entry;
293 void __iomem *reloc_page;
294 int ret = -EINVAL;
295
296 ret = i915_gem_object_set_to_gtt_domain(obj, true);
297 if (ret)
298 return ret;
299
300 ret = i915_gem_object_put_fence(obj);
301 if (ret)
302 return ret;
303
304 /* Map the page containing the relocation we're going to perform. */
305 reloc->offset += i915_gem_obj_ggtt_offset(obj);
306 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
307 reloc->offset & PAGE_MASK);
308 reloc_entry = (uint32_t __iomem *)
309 (reloc_page + offset_in_page(reloc->offset));
310 iowrite32(reloc->delta, reloc_entry);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700311
312 if (INTEL_INFO(dev)->gen >= 8) {
313 reloc_entry += 1;
314
315 if (offset_in_page(reloc->offset + sizeof(uint32_t)) == 0) {
316 io_mapping_unmap_atomic(reloc_page);
317 reloc_page = io_mapping_map_atomic_wc(
318 dev_priv->gtt.mappable,
319 reloc->offset + sizeof(uint32_t));
320 reloc_entry = reloc_page;
321 }
322
323 iowrite32(0, reloc_entry);
324 }
325
Rafael Barbalho5032d872013-08-21 17:10:51 +0100326 io_mapping_unmap_atomic(reloc_page);
327
328 return 0;
329}
330
331static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000332i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200333 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800334 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000335{
336 struct drm_device *dev = obj->base.dev;
337 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100338 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200339 struct i915_vma *target_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000340 uint32_t target_offset;
341 int ret = -EINVAL;
342
Chris Wilson67731b82010-12-08 10:38:14 +0000343 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200344 target_vma = eb_get_vma(eb, reloc->target_handle);
345 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000346 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200347 target_i915_obj = target_vma->obj;
348 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000349
Ben Widawsky5ce09722013-11-25 09:54:40 -0800350 target_offset = target_vma->node.start;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000351
Eric Anholte844b992012-07-31 15:35:01 -0700352 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
353 * pipe_control writes because the gpu doesn't properly redirect them
354 * through the ppgtt for non_secure batchbuffers. */
355 if (unlikely(IS_GEN6(dev) &&
356 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
357 !target_i915_obj->has_global_gtt_mapping)) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800358 struct i915_vma *vma =
359 list_first_entry(&target_i915_obj->vma_list,
360 typeof(*vma), vma_link);
Ben Widawsky6f65e292013-12-06 14:10:56 -0800361 vma->bind_vma(vma, target_i915_obj->cache_level, GLOBAL_BIND);
Eric Anholte844b992012-07-31 15:35:01 -0700362 }
363
Chris Wilson54cf91d2010-11-25 18:00:26 +0000364 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000365 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100366 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000367 "obj %p target %d offset %d "
368 "read %08x write %08x",
369 obj, reloc->target_handle,
370 (int) reloc->offset,
371 reloc->read_domains,
372 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000373 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000374 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100375 if (unlikely((reloc->write_domain | reloc->read_domains)
376 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100377 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000378 "obj %p target %d offset %d "
379 "read %08x write %08x",
380 obj, reloc->target_handle,
381 (int) reloc->offset,
382 reloc->read_domains,
383 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000384 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000385 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000386
387 target_obj->pending_read_domains |= reloc->read_domains;
388 target_obj->pending_write_domain |= reloc->write_domain;
389
390 /* If the relocation already has the right value in it, no
391 * more work needs to be done.
392 */
393 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000394 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000395
396 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700397 if (unlikely(reloc->offset >
398 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100399 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000400 "obj %p target %d offset %d size %d.\n",
401 obj, reloc->target_handle,
402 (int) reloc->offset,
403 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000404 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000405 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000406 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100407 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000408 "obj %p target %d offset %d.\n",
409 obj, reloc->target_handle,
410 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000411 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000412 }
413
Chris Wilsondabdfe02012-03-26 10:10:27 +0200414 /* We can't wait for rendering with pagefaults disabled */
415 if (obj->active && in_atomic())
416 return -EFAULT;
417
Chris Wilson54cf91d2010-11-25 18:00:26 +0000418 reloc->delta += target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100419 if (use_cpu_reloc(obj))
420 ret = relocate_entry_cpu(obj, reloc);
421 else
422 ret = relocate_entry_gtt(obj, reloc);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000423
Daniel Vetterd4d36012013-09-02 20:56:23 +0200424 if (ret)
425 return ret;
426
Chris Wilson54cf91d2010-11-25 18:00:26 +0000427 /* and update the user's relocation entry */
428 reloc->presumed_offset = target_offset;
429
Chris Wilson67731b82010-12-08 10:38:14 +0000430 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000431}
432
433static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200434i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
435 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000436{
Chris Wilson1d83f442012-03-24 20:12:53 +0000437#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
438 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000439 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200440 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000441 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000442
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200443 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000444
Chris Wilson1d83f442012-03-24 20:12:53 +0000445 remain = entry->relocation_count;
446 while (remain) {
447 struct drm_i915_gem_relocation_entry *r = stack_reloc;
448 int count = remain;
449 if (count > ARRAY_SIZE(stack_reloc))
450 count = ARRAY_SIZE(stack_reloc);
451 remain -= count;
452
453 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000454 return -EFAULT;
455
Chris Wilson1d83f442012-03-24 20:12:53 +0000456 do {
457 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000458
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800459 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000460 if (ret)
461 return ret;
462
463 if (r->presumed_offset != offset &&
464 __copy_to_user_inatomic(&user_relocs->presumed_offset,
465 &r->presumed_offset,
466 sizeof(r->presumed_offset))) {
467 return -EFAULT;
468 }
469
470 user_relocs++;
471 r++;
472 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000473 }
474
475 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000476#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000477}
478
479static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200480i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
481 struct eb_vmas *eb,
482 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000483{
Ben Widawsky27173f12013-08-14 11:38:36 +0200484 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000485 int i, ret;
486
487 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800488 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000489 if (ret)
490 return ret;
491 }
492
493 return 0;
494}
495
496static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800497i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000498{
Ben Widawsky27173f12013-08-14 11:38:36 +0200499 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000500 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000501
Chris Wilsond4aeee72011-03-14 15:11:24 +0000502 /* This is the fast path and we cannot handle a pagefault whilst
503 * holding the struct mutex lest the user pass in the relocations
504 * contained within a mmaped bo. For in such a case we, the page
505 * fault handler would call i915_gem_fault() and we would try to
506 * acquire the struct mutex again. Obviously this is bad and so
507 * lockdep complains vehemently.
508 */
509 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200510 list_for_each_entry(vma, &eb->vmas, exec_list) {
511 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000512 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000513 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000514 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000515 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000516
Chris Wilsond4aeee72011-03-14 15:11:24 +0000517 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000518}
519
Chris Wilson1690e1e2011-12-14 13:57:08 +0100520static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200521need_reloc_mappable(struct i915_vma *vma)
Chris Wilsondabdfe02012-03-26 10:10:27 +0200522{
Ben Widawsky27173f12013-08-14 11:38:36 +0200523 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
524 return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
525 i915_is_ggtt(vma->vm);
Chris Wilsondabdfe02012-03-26 10:10:27 +0200526}
527
528static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200529i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
530 struct intel_ring_buffer *ring,
531 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100532{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800533 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200534 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100535 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
536 bool need_fence, need_mappable;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800537 u32 flags = (entry->flags & EXEC_OBJECT_NEEDS_GTT) &&
538 !vma->obj->has_global_gtt_mapping ? GLOBAL_BIND : 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100539 int ret;
540
541 need_fence =
542 has_fenced_gpu_access &&
543 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
544 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200545 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100546
Ben Widawsky27173f12013-08-14 11:38:36 +0200547 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, need_mappable,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700548 false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100549 if (ret)
550 return ret;
551
Chris Wilson7788a762012-08-24 19:18:18 +0100552 entry->flags |= __EXEC_OBJECT_HAS_PIN;
553
Chris Wilson1690e1e2011-12-14 13:57:08 +0100554 if (has_fenced_gpu_access) {
555 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100556 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000557 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100558 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100559
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000560 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100561 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000562
Chris Wilson7dd49062012-03-21 10:48:18 +0000563 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100564 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100565 }
566
Ben Widawsky27173f12013-08-14 11:38:36 +0200567 if (entry->offset != vma->node.start) {
568 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100569 *need_reloc = true;
570 }
571
572 if (entry->flags & EXEC_OBJECT_WRITE) {
573 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
574 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
575 }
576
Ben Widawsky6f65e292013-12-06 14:10:56 -0800577 vma->bind_vma(vma, obj->cache_level, flags);
Daniel Vettered5982e2013-01-17 22:23:36 +0100578
Chris Wilson1690e1e2011-12-14 13:57:08 +0100579 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100580}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100581
Chris Wilson54cf91d2010-11-25 18:00:26 +0000582static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000583i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200584 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100585 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000586{
Chris Wilson432e58e2010-11-25 19:32:06 +0000587 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200588 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700589 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200590 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100591 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
592 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000593
Ben Widawsky68c8c172013-09-11 14:57:50 -0700594 if (list_empty(vmas))
595 return 0;
596
597 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
598
Ben Widawsky27173f12013-08-14 11:38:36 +0200599 INIT_LIST_HEAD(&ordered_vmas);
600 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000601 struct drm_i915_gem_exec_object2 *entry;
602 bool need_fence, need_mappable;
603
Ben Widawsky27173f12013-08-14 11:38:36 +0200604 vma = list_first_entry(vmas, struct i915_vma, exec_list);
605 obj = vma->obj;
606 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000607
608 need_fence =
609 has_fenced_gpu_access &&
610 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
611 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200612 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000613
614 if (need_mappable)
Ben Widawsky27173f12013-08-14 11:38:36 +0200615 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000616 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200617 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000618
Daniel Vettered5982e2013-01-17 22:23:36 +0100619 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000620 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100621 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000622 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200623 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000624
625 /* Attempt to pin all of the buffers into the GTT.
626 * This is done in 3 phases:
627 *
628 * 1a. Unbind all objects that do not match the GTT constraints for
629 * the execbuffer (fenceable, mappable, alignment etc).
630 * 1b. Increment pin count for already bound objects.
631 * 2. Bind new objects.
632 * 3. Decrement pin count.
633 *
Chris Wilson7788a762012-08-24 19:18:18 +0100634 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000635 * room for the earlier objects *unless* we need to defragment.
636 */
637 retry = 0;
638 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100639 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000640
641 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200642 list_for_each_entry(vma, vmas, exec_list) {
643 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000644 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100645
Ben Widawsky27173f12013-08-14 11:38:36 +0200646 obj = vma->obj;
647
648 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000649 continue;
650
651 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000652 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000653 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
654 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200655 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000656
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700657 WARN_ON((need_mappable || need_fence) &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200658 !i915_is_ggtt(vma->vm));
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700659
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700660 if ((entry->alignment &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200661 vma->node.start & (entry->alignment - 1)) ||
Chris Wilson54cf91d2010-11-25 18:00:26 +0000662 (need_mappable && !obj->map_and_fenceable))
Ben Widawsky27173f12013-08-14 11:38:36 +0200663 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000664 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200665 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000666 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000667 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000668 }
669
670 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200671 list_for_each_entry(vma, vmas, exec_list) {
672 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100673 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000674
Ben Widawsky27173f12013-08-14 11:38:36 +0200675 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100676 if (ret)
677 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000678 }
679
Chris Wilsona415d352013-11-26 11:23:15 +0000680err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200681 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000682 return ret;
683
Chris Wilsona415d352013-11-26 11:23:15 +0000684 /* Decrement pin count for bound objects */
685 list_for_each_entry(vma, vmas, exec_list)
686 i915_gem_execbuffer_unreserve_vma(vma);
687
Ben Widawsky68c8c172013-09-11 14:57:50 -0700688 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000689 if (ret)
690 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000691 } while (1);
692}
693
694static int
695i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100696 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000697 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000698 struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200699 struct eb_vmas *eb,
700 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000701{
702 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200703 struct i915_address_space *vm;
704 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100705 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000706 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000707 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200708 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000709
Ben Widawsky27173f12013-08-14 11:38:36 +0200710 if (WARN_ON(list_empty(&eb->vmas)))
711 return 0;
712
713 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
714
Chris Wilson67731b82010-12-08 10:38:14 +0000715 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200716 while (!list_empty(&eb->vmas)) {
717 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
718 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000719 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200720 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000721 }
722
Chris Wilson54cf91d2010-11-25 18:00:26 +0000723 mutex_unlock(&dev->struct_mutex);
724
725 total = 0;
726 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000727 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000728
Chris Wilsondd6864a2011-01-12 23:49:13 +0000729 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000730 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000731 if (reloc == NULL || reloc_offset == NULL) {
732 drm_free_large(reloc);
733 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000734 mutex_lock(&dev->struct_mutex);
735 return -ENOMEM;
736 }
737
738 total = 0;
739 for (i = 0; i < count; i++) {
740 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000741 u64 invalid_offset = (u64)-1;
742 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000743
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200744 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000745
746 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000747 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000748 ret = -EFAULT;
749 mutex_lock(&dev->struct_mutex);
750 goto err;
751 }
752
Chris Wilson262b6d32013-01-15 16:17:54 +0000753 /* As we do not update the known relocation offsets after
754 * relocating (due to the complexities in lock handling),
755 * we need to mark them as invalid now so that we force the
756 * relocation processing next time. Just in case the target
757 * object is evicted and then rebound into its old
758 * presumed_offset before the next execbuffer - if that
759 * happened we would make the mistake of assuming that the
760 * relocations were valid.
761 */
762 for (j = 0; j < exec[i].relocation_count; j++) {
763 if (copy_to_user(&user_relocs[j].presumed_offset,
764 &invalid_offset,
765 sizeof(invalid_offset))) {
766 ret = -EFAULT;
767 mutex_lock(&dev->struct_mutex);
768 goto err;
769 }
770 }
771
Chris Wilsondd6864a2011-01-12 23:49:13 +0000772 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000773 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000774 }
775
776 ret = i915_mutex_lock_interruptible(dev);
777 if (ret) {
778 mutex_lock(&dev->struct_mutex);
779 goto err;
780 }
781
Chris Wilson67731b82010-12-08 10:38:14 +0000782 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000783 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200784 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000785 if (ret)
786 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000787
Daniel Vettered5982e2013-01-17 22:23:36 +0100788 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200789 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000790 if (ret)
791 goto err;
792
Ben Widawsky27173f12013-08-14 11:38:36 +0200793 list_for_each_entry(vma, &eb->vmas, exec_list) {
794 int offset = vma->exec_entry - exec;
795 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
796 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000797 if (ret)
798 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000799 }
800
801 /* Leave the user relocations as are, this is the painfully slow path,
802 * and we want to avoid the complication of dropping the lock whilst
803 * having buffers reserved in the aperture and so causing spurious
804 * ENOSPC for random operations.
805 */
806
807err:
808 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000809 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000810 return ret;
811}
812
Chris Wilson54cf91d2010-11-25 18:00:26 +0000813static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000814i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200815 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000816{
Ben Widawsky27173f12013-08-14 11:38:36 +0200817 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200818 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100819 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000820 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000821
Ben Widawsky27173f12013-08-14 11:38:36 +0200822 list_for_each_entry(vma, vmas, exec_list) {
823 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700824 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000825 if (ret)
826 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200827
828 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100829 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200830
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200831 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000832 }
833
Chris Wilson000433b2013-08-08 14:41:09 +0100834 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800835 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200836
837 if (flush_domains & I915_GEM_DOMAIN_GTT)
838 wmb();
839
Chris Wilson09cf7c92012-07-13 14:14:08 +0100840 /* Unconditionally invalidate gpu caches and ensure that we do flush
841 * any residual writes from the previous batch.
842 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100843 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000844}
845
Chris Wilson432e58e2010-11-25 19:32:06 +0000846static bool
847i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000848{
Daniel Vettered5982e2013-01-17 22:23:36 +0100849 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
850 return false;
851
Chris Wilson432e58e2010-11-25 19:32:06 +0000852 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000853}
854
855static int
856validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
857 int count)
858{
859 int i;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200860 unsigned relocs_total = 0;
861 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000862
863 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200864 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000865 int length; /* limited by fault_in_pages_readable() */
866
Daniel Vettered5982e2013-01-17 22:23:36 +0100867 if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
868 return -EINVAL;
869
Kees Cook3118a4f2013-03-11 17:31:45 -0700870 /* First check for malicious input causing overflow in
871 * the worst case where we need to allocate the entire
872 * relocation tree as a single array.
873 */
874 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000875 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700876 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000877
878 length = exec[i].relocation_count *
879 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700880 /*
881 * We must check that the entire relocation array is safe
882 * to read, but since we may need to update the presumed
883 * offsets during execution, check for full write access.
884 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000885 if (!access_ok(VERIFY_WRITE, ptr, length))
886 return -EFAULT;
887
Xiong Zhang0b74b502013-07-19 13:51:24 +0800888 if (likely(!i915_prefault_disable)) {
889 if (fault_in_multipages_readable(ptr, length))
890 return -EFAULT;
891 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000892 }
893
894 return 0;
895}
896
Ben Widawsky41bde552013-12-06 14:11:21 -0800897static struct i915_hw_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200898i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Daniel Vetter7c9c4b82013-12-18 16:37:49 +0100899 struct intel_ring_buffer *ring, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200900{
Ben Widawsky41bde552013-12-06 14:11:21 -0800901 struct i915_hw_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200902 struct i915_ctx_hang_stats *hs;
903
Daniel Vetter7c9c4b82013-12-18 16:37:49 +0100904 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_ID)
905 return ERR_PTR(-EINVAL);
906
Ben Widawsky41bde552013-12-06 14:11:21 -0800907 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
908 if (IS_ERR_OR_NULL(ctx))
909 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200910
Ben Widawsky41bde552013-12-06 14:11:21 -0800911 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200912 if (hs->banned) {
913 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -0800914 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200915 }
916
Ben Widawsky41bde552013-12-06 14:11:21 -0800917 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200918}
919
Chris Wilson432e58e2010-11-25 19:32:06 +0000920static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200921i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Chris Wilson9d7730912012-11-27 16:22:52 +0000922 struct intel_ring_buffer *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000923{
Ben Widawsky27173f12013-08-14 11:38:36 +0200924 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000925
Ben Widawsky27173f12013-08-14 11:38:36 +0200926 list_for_each_entry(vma, vmas, exec_list) {
927 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100928 u32 old_read = obj->base.read_domains;
929 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000930
Chris Wilson432e58e2010-11-25 19:32:06 +0000931 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100932 if (obj->base.write_domain == 0)
933 obj->base.pending_read_domains |= obj->base.read_domains;
934 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000935 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
936
Ben Widawskye2d05a82013-09-24 09:57:58 -0700937 i915_vma_move_to_active(vma, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000938 if (obj->base.write_domain) {
939 obj->dirty = 1;
Chris Wilson9d7730912012-11-27 16:22:52 +0000940 obj->last_write_seqno = intel_ring_get_seqno(ring);
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800941 /* check for potential scanout */
942 if (i915_gem_obj_ggtt_bound(obj) &&
943 i915_gem_obj_to_ggtt(obj)->pin_count)
Chris Wilsonc65355b2013-06-06 16:53:41 -0300944 intel_mark_fb_busy(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000945 }
946
Chris Wilsondb53a302011-02-03 11:57:46 +0000947 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000948 }
949}
950
Chris Wilson54cf91d2010-11-25 18:00:26 +0000951static void
952i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000953 struct drm_file *file,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300954 struct intel_ring_buffer *ring,
955 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000956{
Daniel Vettercc889e02012-06-13 20:45:19 +0200957 /* Unconditionally force add_request to emit a full flush. */
958 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000959
Chris Wilson432e58e2010-11-25 19:32:06 +0000960 /* Add a breadcrumb for the completion of the batch buffer */
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300961 (void)__i915_add_request(ring, file, obj, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000962}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000963
964static int
Eric Anholtae662d32012-01-03 09:23:29 -0800965i915_reset_gen7_sol_offsets(struct drm_device *dev,
966 struct intel_ring_buffer *ring)
967{
968 drm_i915_private_t *dev_priv = dev->dev_private;
969 int ret, i;
970
971 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
972 return 0;
973
974 ret = intel_ring_begin(ring, 4 * 3);
975 if (ret)
976 return ret;
977
978 for (i = 0; i < 4; i++) {
979 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
980 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
981 intel_ring_emit(ring, 0);
982 }
983
984 intel_ring_advance(ring);
985
986 return 0;
987}
988
989static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000990i915_gem_do_execbuffer(struct drm_device *dev, void *data,
991 struct drm_file *file,
992 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -0800993 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000994{
995 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +0200996 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000997 struct drm_i915_gem_object *batch_obj;
998 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000999 struct intel_ring_buffer *ring;
Ben Widawsky41bde552013-12-06 14:11:21 -08001000 struct i915_hw_context *ctx;
1001 struct i915_address_space *vm;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001002 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001003 u32 exec_start = args->batch_start_offset, exec_len;
Daniel Vettered5982e2013-01-17 22:23:36 +01001004 u32 mask, flags;
Chris Wilson72bfa192010-12-19 11:42:05 +00001005 int ret, mode, i;
Daniel Vettered5982e2013-01-17 22:23:36 +01001006 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001007
Daniel Vettered5982e2013-01-17 22:23:36 +01001008 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001009 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001010
1011 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001012 if (ret)
1013 return ret;
1014
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001015 flags = 0;
1016 if (args->flags & I915_EXEC_SECURE) {
1017 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1018 return -EPERM;
1019
1020 flags |= I915_DISPATCH_SECURE;
1021 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001022 if (args->flags & I915_EXEC_IS_PINNED)
1023 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001024
Ben Widawskyca01b122013-12-06 14:11:00 -08001025 if ((args->flags & I915_EXEC_RING_MASK) > I915_NUM_RINGS) {
Daniel Vetterff240192012-01-31 21:08:14 +01001026 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001027 (int)(args->flags & I915_EXEC_RING_MASK));
1028 return -EINVAL;
1029 }
Ben Widawskyca01b122013-12-06 14:11:00 -08001030
1031 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1032 ring = &dev_priv->ring[RCS];
1033 else
1034 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1035
Chris Wilsona15817c2012-05-11 14:29:31 +01001036 if (!intel_ring_initialized(ring)) {
1037 DRM_DEBUG("execbuf with invalid ring: %d\n",
1038 (int)(args->flags & I915_EXEC_RING_MASK));
1039 return -EINVAL;
1040 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001041
Chris Wilson72bfa192010-12-19 11:42:05 +00001042 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001043 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +00001044 switch (mode) {
1045 case I915_EXEC_CONSTANTS_REL_GENERAL:
1046 case I915_EXEC_CONSTANTS_ABSOLUTE:
1047 case I915_EXEC_CONSTANTS_REL_SURFACE:
1048 if (ring == &dev_priv->ring[RCS] &&
1049 mode != dev_priv->relative_constants_mode) {
1050 if (INTEL_INFO(dev)->gen < 4)
1051 return -EINVAL;
1052
1053 if (INTEL_INFO(dev)->gen > 5 &&
1054 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1055 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001056
1057 /* The HW changed the meaning on this bit on gen6 */
1058 if (INTEL_INFO(dev)->gen >= 6)
1059 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +00001060 }
1061 break;
1062 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001063 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +00001064 return -EINVAL;
1065 }
1066
Chris Wilson54cf91d2010-11-25 18:00:26 +00001067 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001068 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001069 return -EINVAL;
1070 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001071
1072 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001073 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +01001074 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001075 return -EINVAL;
1076 }
1077
Daniel Vetter6ebebc92012-04-26 23:28:11 +02001078 if (INTEL_INFO(dev)->gen >= 5) {
1079 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1080 return -EINVAL;
1081 }
1082
Xi Wang44afb3a2012-04-23 04:06:42 -04001083 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1084 DRM_DEBUG("execbuf with %u cliprects\n",
1085 args->num_cliprects);
1086 return -EINVAL;
1087 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001088
Daniel Vettera1e22652013-09-21 00:35:38 +02001089 cliprects = kcalloc(args->num_cliprects,
1090 sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001091 GFP_KERNEL);
1092 if (cliprects == NULL) {
1093 ret = -ENOMEM;
1094 goto pre_mutex_err;
1095 }
1096
Chris Wilson432e58e2010-11-25 19:32:06 +00001097 if (copy_from_user(cliprects,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001098 to_user_ptr(args->cliprects_ptr),
1099 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001100 ret = -EFAULT;
1101 goto pre_mutex_err;
1102 }
1103 }
1104
Chris Wilson54cf91d2010-11-25 18:00:26 +00001105 ret = i915_mutex_lock_interruptible(dev);
1106 if (ret)
1107 goto pre_mutex_err;
1108
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001109 if (dev_priv->ums.mm_suspended) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001110 mutex_unlock(&dev->struct_mutex);
1111 ret = -EBUSY;
1112 goto pre_mutex_err;
1113 }
1114
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001115 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -08001116 if (IS_ERR_OR_NULL(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001117 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001118 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001119 goto pre_mutex_err;
Ben Widawsky41bde552013-12-06 14:11:21 -08001120 }
1121
1122 i915_gem_context_reference(ctx);
1123
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001124 vm = ctx->vm;
1125 if (!USES_FULL_PPGTT(dev))
1126 vm = &dev_priv->gtt.base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001127
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001128 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001129 if (eb == NULL) {
1130 mutex_unlock(&dev->struct_mutex);
1131 ret = -ENOMEM;
1132 goto pre_mutex_err;
1133 }
1134
Chris Wilson54cf91d2010-11-25 18:00:26 +00001135 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001136 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001137 if (ret)
1138 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001139
Chris Wilson6fe4f142011-01-10 17:35:37 +00001140 /* take note of the batch buffer before we might reorder the lists */
Ben Widawsky27173f12013-08-14 11:38:36 +02001141 batch_obj = list_entry(eb->vmas.prev, struct i915_vma, exec_list)->obj;
Chris Wilson6fe4f142011-01-10 17:35:37 +00001142
Chris Wilson54cf91d2010-11-25 18:00:26 +00001143 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001144 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001145 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001146 if (ret)
1147 goto err;
1148
1149 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001150 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001151 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001152 if (ret) {
1153 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001154 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001155 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001156 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1157 }
1158 if (ret)
1159 goto err;
1160 }
1161
1162 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001163 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001164 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001165 ret = -EINVAL;
1166 goto err;
1167 }
1168 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1169
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001170 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1171 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001172 * hsw should have this fixed, but bdw mucks it up again. */
Ben Widawsky6f65e292013-12-06 14:10:56 -08001173 if (flags & I915_DISPATCH_SECURE &&
1174 !batch_obj->has_global_gtt_mapping) {
1175 /* When we have multiple VMs, we'll need to make sure that we
1176 * allocate space first */
1177 struct i915_vma *vma = i915_gem_obj_to_ggtt(batch_obj);
1178 BUG_ON(!vma);
1179 vma->bind_vma(vma, batch_obj->cache_level, GLOBAL_BIND);
1180 }
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001181
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001182 if (flags & I915_DISPATCH_SECURE)
1183 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
1184 else
1185 exec_start += i915_gem_obj_offset(batch_obj, vm);
1186
Ben Widawsky27173f12013-08-14 11:38:36 +02001187 ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001188 if (ret)
1189 goto err;
1190
Ben Widawsky41bde552013-12-06 14:11:21 -08001191 ret = i915_switch_context(ring, file, ctx);
Eric Anholt0da5cec2012-07-23 12:33:55 -07001192 if (ret)
1193 goto err;
1194
Ben Widawskye2971bd2011-12-12 19:21:57 -08001195 if (ring == &dev_priv->ring[RCS] &&
1196 mode != dev_priv->relative_constants_mode) {
1197 ret = intel_ring_begin(ring, 4);
1198 if (ret)
1199 goto err;
1200
1201 intel_ring_emit(ring, MI_NOOP);
1202 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1203 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001204 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001205 intel_ring_advance(ring);
1206
1207 dev_priv->relative_constants_mode = mode;
1208 }
1209
Eric Anholtae662d32012-01-03 09:23:29 -08001210 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1211 ret = i915_reset_gen7_sol_offsets(dev, ring);
1212 if (ret)
1213 goto err;
1214 }
1215
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001216
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001217 exec_len = args->batch_len;
1218 if (cliprects) {
1219 for (i = 0; i < args->num_cliprects; i++) {
1220 ret = i915_emit_box(dev, &cliprects[i],
1221 args->DR1, args->DR4);
1222 if (ret)
1223 goto err;
1224
1225 ret = ring->dispatch_execbuffer(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001226 exec_start, exec_len,
1227 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001228 if (ret)
1229 goto err;
1230 }
1231 } else {
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001232 ret = ring->dispatch_execbuffer(ring,
1233 exec_start, exec_len,
1234 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001235 if (ret)
1236 goto err;
1237 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001238
Chris Wilson9d7730912012-11-27 16:22:52 +00001239 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1240
Ben Widawsky27173f12013-08-14 11:38:36 +02001241 i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
Mika Kuoppala7d736f42013-06-12 15:01:39 +03001242 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001243
1244err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001245 /* the request owns the ref now */
1246 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001247 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001248
1249 mutex_unlock(&dev->struct_mutex);
1250
1251pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001252 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001253 return ret;
1254}
1255
1256/*
1257 * Legacy execbuffer just creates an exec2 list from the original exec object
1258 * list array and passes it to the real function.
1259 */
1260int
1261i915_gem_execbuffer(struct drm_device *dev, void *data,
1262 struct drm_file *file)
1263{
1264 struct drm_i915_gem_execbuffer *args = data;
1265 struct drm_i915_gem_execbuffer2 exec2;
1266 struct drm_i915_gem_exec_object *exec_list = NULL;
1267 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1268 int ret, i;
1269
Chris Wilson54cf91d2010-11-25 18:00:26 +00001270 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001271 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001272 return -EINVAL;
1273 }
1274
1275 /* Copy in the exec list from userland */
1276 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1277 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1278 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001279 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001280 args->buffer_count);
1281 drm_free_large(exec_list);
1282 drm_free_large(exec2_list);
1283 return -ENOMEM;
1284 }
1285 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001286 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001287 sizeof(*exec_list) * args->buffer_count);
1288 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001289 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001290 args->buffer_count, ret);
1291 drm_free_large(exec_list);
1292 drm_free_large(exec2_list);
1293 return -EFAULT;
1294 }
1295
1296 for (i = 0; i < args->buffer_count; i++) {
1297 exec2_list[i].handle = exec_list[i].handle;
1298 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1299 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1300 exec2_list[i].alignment = exec_list[i].alignment;
1301 exec2_list[i].offset = exec_list[i].offset;
1302 if (INTEL_INFO(dev)->gen < 4)
1303 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1304 else
1305 exec2_list[i].flags = 0;
1306 }
1307
1308 exec2.buffers_ptr = args->buffers_ptr;
1309 exec2.buffer_count = args->buffer_count;
1310 exec2.batch_start_offset = args->batch_start_offset;
1311 exec2.batch_len = args->batch_len;
1312 exec2.DR1 = args->DR1;
1313 exec2.DR4 = args->DR4;
1314 exec2.num_cliprects = args->num_cliprects;
1315 exec2.cliprects_ptr = args->cliprects_ptr;
1316 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001317 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001318
Ben Widawsky41bde552013-12-06 14:11:21 -08001319 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001320 if (!ret) {
1321 /* Copy the new buffer offsets back to the user's exec list. */
1322 for (i = 0; i < args->buffer_count; i++)
1323 exec_list[i].offset = exec2_list[i].offset;
1324 /* ... and back out to userspace */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001325 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001326 exec_list,
1327 sizeof(*exec_list) * args->buffer_count);
1328 if (ret) {
1329 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001330 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001331 "back to user (%d)\n",
1332 args->buffer_count, ret);
1333 }
1334 }
1335
1336 drm_free_large(exec_list);
1337 drm_free_large(exec2_list);
1338 return ret;
1339}
1340
1341int
1342i915_gem_execbuffer2(struct drm_device *dev, void *data,
1343 struct drm_file *file)
1344{
1345 struct drm_i915_gem_execbuffer2 *args = data;
1346 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1347 int ret;
1348
Xi Wanged8cd3b2012-04-23 04:06:41 -04001349 if (args->buffer_count < 1 ||
1350 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001351 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001352 return -EINVAL;
1353 }
1354
Chris Wilson8408c282011-02-21 12:54:48 +00001355 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001356 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001357 if (exec2_list == NULL)
1358 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1359 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001360 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001361 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001362 args->buffer_count);
1363 return -ENOMEM;
1364 }
1365 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001366 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001367 sizeof(*exec2_list) * args->buffer_count);
1368 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001369 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001370 args->buffer_count, ret);
1371 drm_free_large(exec2_list);
1372 return -EFAULT;
1373 }
1374
Ben Widawsky41bde552013-12-06 14:11:21 -08001375 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001376 if (!ret) {
1377 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001378 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001379 exec2_list,
1380 sizeof(*exec2_list) * args->buffer_count);
1381 if (ret) {
1382 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001383 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001384 "back to user (%d)\n",
1385 args->buffer_count, ret);
1386 }
1387 }
1388
1389 drm_free_large(exec2_list);
1390 return ret;
1391}