blob: a3ba9a8cd68794bbfd163c9236c91c7be9d15965 [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 *
49eb_create(struct drm_i915_gem_execbuffer2 *args, struct i915_address_space *vm)
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 Widawsky27173f12013-08-14 11:38:36 +020094 struct drm_i915_gem_object *obj;
95 struct list_head objects;
Chris Wilson9ae9ab52013-12-04 09:52:58 +000096 int i, ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +000097
Ben Widawsky27173f12013-08-14 11:38:36 +020098 INIT_LIST_HEAD(&objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +000099 spin_lock(&file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +0200100 /* Grab a reference to the object and release the lock so we can lookup
101 * or create the VMA without using GFP_ATOMIC */
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000102 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000103 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
104 if (obj == NULL) {
105 spin_unlock(&file->table_lock);
106 DRM_DEBUG("Invalid object handle %d at index %d\n",
107 exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200108 ret = -ENOENT;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000109 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000110 }
111
Ben Widawsky27173f12013-08-14 11:38:36 +0200112 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000113 spin_unlock(&file->table_lock);
114 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
115 obj, exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200116 ret = -EINVAL;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000117 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000118 }
119
120 drm_gem_object_reference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200121 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000122 }
123 spin_unlock(&file->table_lock);
124
Ben Widawsky27173f12013-08-14 11:38:36 +0200125 i = 0;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000126 while (!list_empty(&objects)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200127 struct i915_vma *vma;
128
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000129 obj = list_first_entry(&objects,
130 struct drm_i915_gem_object,
131 obj_exec_link);
132
Daniel Vettere656a6c2013-08-14 14:14:04 +0200133 /*
134 * NOTE: We can leak any vmas created here when something fails
135 * later on. But that's no issue since vma_unbind can deal with
136 * vmas which are not actually bound. And since only
137 * lookup_or_create exists as an interface to get at the vma
138 * from the (obj, vm) we don't run the risk of creating
139 * duplicated vmas for the same vm.
140 */
Ben Widawsky27173f12013-08-14 11:38:36 +0200141 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
142 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200143 DRM_DEBUG("Failed to lookup VMA\n");
144 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000145 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200146 }
147
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000148 /* Transfer ownership from the objects list to the vmas list. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200149 list_add_tail(&vma->exec_list, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000150 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200151
152 vma->exec_entry = &exec[i];
153 if (eb->and < 0) {
154 eb->lut[i] = vma;
155 } else {
156 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
157 vma->exec_handle = handle;
158 hlist_add_head(&vma->exec_node,
159 &eb->buckets[handle & eb->and]);
160 }
161 ++i;
162 }
163
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000164 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200165
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000166
167err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200168 while (!list_empty(&objects)) {
169 obj = list_first_entry(&objects,
170 struct drm_i915_gem_object,
171 obj_exec_link);
172 list_del_init(&obj->obj_exec_link);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000173 drm_gem_object_unreference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200174 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000175 /*
176 * Objects already transfered to the vmas list will be unreferenced by
177 * eb_destroy.
178 */
179
Ben Widawsky27173f12013-08-14 11:38:36 +0200180 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000181}
182
Ben Widawsky27173f12013-08-14 11:38:36 +0200183static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000184{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000185 if (eb->and < 0) {
186 if (handle >= -eb->and)
187 return NULL;
188 return eb->lut[handle];
189 } else {
190 struct hlist_head *head;
191 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000192
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000193 head = &eb->buckets[handle & eb->and];
194 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200195 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000196
Ben Widawsky27173f12013-08-14 11:38:36 +0200197 vma = hlist_entry(node, struct i915_vma, exec_node);
198 if (vma->exec_handle == handle)
199 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000200 }
201 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000202 }
Chris Wilson67731b82010-12-08 10:38:14 +0000203}
204
Chris Wilsona415d352013-11-26 11:23:15 +0000205static void
206i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
207{
208 struct drm_i915_gem_exec_object2 *entry;
209 struct drm_i915_gem_object *obj = vma->obj;
210
211 if (!drm_mm_node_allocated(&vma->node))
212 return;
213
214 entry = vma->exec_entry;
215
216 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
217 i915_gem_object_unpin_fence(obj);
218
219 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
220 i915_gem_object_unpin(obj);
221
222 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
223}
224
225static void eb_destroy(struct eb_vmas *eb)
226{
Ben Widawsky27173f12013-08-14 11:38:36 +0200227 while (!list_empty(&eb->vmas)) {
228 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000229
Ben Widawsky27173f12013-08-14 11:38:36 +0200230 vma = list_first_entry(&eb->vmas,
231 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000232 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200233 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000234 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200235 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000236 }
Chris Wilson67731b82010-12-08 10:38:14 +0000237 kfree(eb);
238}
239
Chris Wilsondabdfe02012-03-26 10:10:27 +0200240static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
241{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300242 return (HAS_LLC(obj->base.dev) ||
243 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100244 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200245 obj->cache_level != I915_CACHE_NONE);
246}
247
Chris Wilson54cf91d2010-11-25 18:00:26 +0000248static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100249relocate_entry_cpu(struct drm_i915_gem_object *obj,
250 struct drm_i915_gem_relocation_entry *reloc)
251{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700252 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100253 uint32_t page_offset = offset_in_page(reloc->offset);
254 char *vaddr;
255 int ret = -EINVAL;
256
Chris Wilson2cc86b82013-08-26 19:51:00 -0300257 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100258 if (ret)
259 return ret;
260
261 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
262 reloc->offset >> PAGE_SHIFT));
263 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700264
265 if (INTEL_INFO(dev)->gen >= 8) {
266 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
267
268 if (page_offset == 0) {
269 kunmap_atomic(vaddr);
270 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
271 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
272 }
273
274 *(uint32_t *)(vaddr + page_offset) = 0;
275 }
276
Rafael Barbalho5032d872013-08-21 17:10:51 +0100277 kunmap_atomic(vaddr);
278
279 return 0;
280}
281
282static int
283relocate_entry_gtt(struct drm_i915_gem_object *obj,
284 struct drm_i915_gem_relocation_entry *reloc)
285{
286 struct drm_device *dev = obj->base.dev;
287 struct drm_i915_private *dev_priv = dev->dev_private;
288 uint32_t __iomem *reloc_entry;
289 void __iomem *reloc_page;
290 int ret = -EINVAL;
291
292 ret = i915_gem_object_set_to_gtt_domain(obj, true);
293 if (ret)
294 return ret;
295
296 ret = i915_gem_object_put_fence(obj);
297 if (ret)
298 return ret;
299
300 /* Map the page containing the relocation we're going to perform. */
301 reloc->offset += i915_gem_obj_ggtt_offset(obj);
302 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
303 reloc->offset & PAGE_MASK);
304 reloc_entry = (uint32_t __iomem *)
305 (reloc_page + offset_in_page(reloc->offset));
306 iowrite32(reloc->delta, reloc_entry);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700307
308 if (INTEL_INFO(dev)->gen >= 8) {
309 reloc_entry += 1;
310
311 if (offset_in_page(reloc->offset + sizeof(uint32_t)) == 0) {
312 io_mapping_unmap_atomic(reloc_page);
313 reloc_page = io_mapping_map_atomic_wc(
314 dev_priv->gtt.mappable,
315 reloc->offset + sizeof(uint32_t));
316 reloc_entry = reloc_page;
317 }
318
319 iowrite32(0, reloc_entry);
320 }
321
Rafael Barbalho5032d872013-08-21 17:10:51 +0100322 io_mapping_unmap_atomic(reloc_page);
323
324 return 0;
325}
326
327static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000328i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200329 struct eb_vmas *eb,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700330 struct drm_i915_gem_relocation_entry *reloc,
331 struct i915_address_space *vm)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000332{
333 struct drm_device *dev = obj->base.dev;
334 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100335 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200336 struct i915_vma *target_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000337 uint32_t target_offset;
338 int ret = -EINVAL;
339
Chris Wilson67731b82010-12-08 10:38:14 +0000340 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200341 target_vma = eb_get_vma(eb, reloc->target_handle);
342 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000343 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200344 target_i915_obj = target_vma->obj;
345 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000346
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700347 target_offset = i915_gem_obj_ggtt_offset(target_i915_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000348
Eric Anholte844b992012-07-31 15:35:01 -0700349 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
350 * pipe_control writes because the gpu doesn't properly redirect them
351 * through the ppgtt for non_secure batchbuffers. */
352 if (unlikely(IS_GEN6(dev) &&
353 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
354 !target_i915_obj->has_global_gtt_mapping)) {
355 i915_gem_gtt_bind_object(target_i915_obj,
356 target_i915_obj->cache_level);
357 }
358
Chris Wilson54cf91d2010-11-25 18:00:26 +0000359 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000360 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100361 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000362 "obj %p target %d offset %d "
363 "read %08x write %08x",
364 obj, reloc->target_handle,
365 (int) reloc->offset,
366 reloc->read_domains,
367 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000368 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000369 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100370 if (unlikely((reloc->write_domain | reloc->read_domains)
371 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100372 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000373 "obj %p target %d offset %d "
374 "read %08x write %08x",
375 obj, reloc->target_handle,
376 (int) reloc->offset,
377 reloc->read_domains,
378 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000379 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000380 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000381
382 target_obj->pending_read_domains |= reloc->read_domains;
383 target_obj->pending_write_domain |= reloc->write_domain;
384
385 /* If the relocation already has the right value in it, no
386 * more work needs to be done.
387 */
388 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000389 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000390
391 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700392 if (unlikely(reloc->offset >
393 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100394 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000395 "obj %p target %d offset %d size %d.\n",
396 obj, reloc->target_handle,
397 (int) reloc->offset,
398 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000399 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000400 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000401 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100402 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000403 "obj %p target %d offset %d.\n",
404 obj, reloc->target_handle,
405 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000406 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000407 }
408
Chris Wilsondabdfe02012-03-26 10:10:27 +0200409 /* We can't wait for rendering with pagefaults disabled */
410 if (obj->active && in_atomic())
411 return -EFAULT;
412
Chris Wilson54cf91d2010-11-25 18:00:26 +0000413 reloc->delta += target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100414 if (use_cpu_reloc(obj))
415 ret = relocate_entry_cpu(obj, reloc);
416 else
417 ret = relocate_entry_gtt(obj, reloc);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000418
Daniel Vetterd4d36012013-09-02 20:56:23 +0200419 if (ret)
420 return ret;
421
Chris Wilson54cf91d2010-11-25 18:00:26 +0000422 /* and update the user's relocation entry */
423 reloc->presumed_offset = target_offset;
424
Chris Wilson67731b82010-12-08 10:38:14 +0000425 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000426}
427
428static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200429i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
430 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000431{
Chris Wilson1d83f442012-03-24 20:12:53 +0000432#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
433 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000434 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200435 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000436 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000437
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200438 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000439
Chris Wilson1d83f442012-03-24 20:12:53 +0000440 remain = entry->relocation_count;
441 while (remain) {
442 struct drm_i915_gem_relocation_entry *r = stack_reloc;
443 int count = remain;
444 if (count > ARRAY_SIZE(stack_reloc))
445 count = ARRAY_SIZE(stack_reloc);
446 remain -= count;
447
448 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000449 return -EFAULT;
450
Chris Wilson1d83f442012-03-24 20:12:53 +0000451 do {
452 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000453
Ben Widawsky27173f12013-08-14 11:38:36 +0200454 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r,
455 vma->vm);
Chris Wilson1d83f442012-03-24 20:12:53 +0000456 if (ret)
457 return ret;
458
459 if (r->presumed_offset != offset &&
460 __copy_to_user_inatomic(&user_relocs->presumed_offset,
461 &r->presumed_offset,
462 sizeof(r->presumed_offset))) {
463 return -EFAULT;
464 }
465
466 user_relocs++;
467 r++;
468 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000469 }
470
471 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000472#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000473}
474
475static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200476i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
477 struct eb_vmas *eb,
478 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000479{
Ben Widawsky27173f12013-08-14 11:38:36 +0200480 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000481 int i, ret;
482
483 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200484 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i],
485 vma->vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000486 if (ret)
487 return ret;
488 }
489
490 return 0;
491}
492
493static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200494i915_gem_execbuffer_relocate(struct eb_vmas *eb,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700495 struct i915_address_space *vm)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000496{
Ben Widawsky27173f12013-08-14 11:38:36 +0200497 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000498 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000499
Chris Wilsond4aeee72011-03-14 15:11:24 +0000500 /* This is the fast path and we cannot handle a pagefault whilst
501 * holding the struct mutex lest the user pass in the relocations
502 * contained within a mmaped bo. For in such a case we, the page
503 * fault handler would call i915_gem_fault() and we would try to
504 * acquire the struct mutex again. Obviously this is bad and so
505 * lockdep complains vehemently.
506 */
507 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200508 list_for_each_entry(vma, &eb->vmas, exec_list) {
509 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000510 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000511 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000512 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000513 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000514
Chris Wilsond4aeee72011-03-14 15:11:24 +0000515 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000516}
517
Chris Wilson1690e1e2011-12-14 13:57:08 +0100518static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200519need_reloc_mappable(struct i915_vma *vma)
Chris Wilsondabdfe02012-03-26 10:10:27 +0200520{
Ben Widawsky27173f12013-08-14 11:38:36 +0200521 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
522 return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
523 i915_is_ggtt(vma->vm);
Chris Wilsondabdfe02012-03-26 10:10:27 +0200524}
525
526static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200527i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
528 struct intel_ring_buffer *ring,
529 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100530{
Ben Widawsky27173f12013-08-14 11:38:36 +0200531 struct drm_i915_private *dev_priv = ring->dev->dev_private;
532 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100533 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
534 bool need_fence, need_mappable;
Ben Widawsky27173f12013-08-14 11:38:36 +0200535 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100536 int ret;
537
538 need_fence =
539 has_fenced_gpu_access &&
540 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
541 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200542 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100543
Ben Widawsky27173f12013-08-14 11:38:36 +0200544 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, need_mappable,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700545 false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100546 if (ret)
547 return ret;
548
Chris Wilson7788a762012-08-24 19:18:18 +0100549 entry->flags |= __EXEC_OBJECT_HAS_PIN;
550
Chris Wilson1690e1e2011-12-14 13:57:08 +0100551 if (has_fenced_gpu_access) {
552 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100553 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000554 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100555 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100556
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000557 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100558 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000559
Chris Wilson7dd49062012-03-21 10:48:18 +0000560 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100561 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100562 }
563
Chris Wilson7788a762012-08-24 19:18:18 +0100564 /* Ensure ppgtt mapping exists if needed */
565 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
566 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
567 obj, obj->cache_level);
568
569 obj->has_aliasing_ppgtt_mapping = 1;
570 }
571
Ben Widawsky27173f12013-08-14 11:38:36 +0200572 if (entry->offset != vma->node.start) {
573 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100574 *need_reloc = true;
575 }
576
577 if (entry->flags & EXEC_OBJECT_WRITE) {
578 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
579 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
580 }
581
582 if (entry->flags & EXEC_OBJECT_NEEDS_GTT &&
583 !obj->has_global_gtt_mapping)
584 i915_gem_gtt_bind_object(obj, obj->cache_level);
585
Chris Wilson1690e1e2011-12-14 13:57:08 +0100586 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100587}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100588
Chris Wilson54cf91d2010-11-25 18:00:26 +0000589static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000590i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200591 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100592 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000593{
Chris Wilson432e58e2010-11-25 19:32:06 +0000594 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200595 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700596 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200597 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100598 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
599 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000600
Ben Widawsky68c8c172013-09-11 14:57:50 -0700601 if (list_empty(vmas))
602 return 0;
603
604 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
605
Ben Widawsky27173f12013-08-14 11:38:36 +0200606 INIT_LIST_HEAD(&ordered_vmas);
607 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000608 struct drm_i915_gem_exec_object2 *entry;
609 bool need_fence, need_mappable;
610
Ben Widawsky27173f12013-08-14 11:38:36 +0200611 vma = list_first_entry(vmas, struct i915_vma, exec_list);
612 obj = vma->obj;
613 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000614
615 need_fence =
616 has_fenced_gpu_access &&
617 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
618 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200619 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000620
621 if (need_mappable)
Ben Widawsky27173f12013-08-14 11:38:36 +0200622 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000623 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200624 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000625
Daniel Vettered5982e2013-01-17 22:23:36 +0100626 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000627 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100628 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000629 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200630 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000631
632 /* Attempt to pin all of the buffers into the GTT.
633 * This is done in 3 phases:
634 *
635 * 1a. Unbind all objects that do not match the GTT constraints for
636 * the execbuffer (fenceable, mappable, alignment etc).
637 * 1b. Increment pin count for already bound objects.
638 * 2. Bind new objects.
639 * 3. Decrement pin count.
640 *
Chris Wilson7788a762012-08-24 19:18:18 +0100641 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000642 * room for the earlier objects *unless* we need to defragment.
643 */
644 retry = 0;
645 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100646 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000647
648 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200649 list_for_each_entry(vma, vmas, exec_list) {
650 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000651 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100652
Ben Widawsky27173f12013-08-14 11:38:36 +0200653 obj = vma->obj;
654
655 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000656 continue;
657
658 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000659 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000660 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
661 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200662 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000663
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700664 WARN_ON((need_mappable || need_fence) &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200665 !i915_is_ggtt(vma->vm));
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700666
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700667 if ((entry->alignment &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200668 vma->node.start & (entry->alignment - 1)) ||
Chris Wilson54cf91d2010-11-25 18:00:26 +0000669 (need_mappable && !obj->map_and_fenceable))
Ben Widawsky27173f12013-08-14 11:38:36 +0200670 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000671 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200672 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000673 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000674 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000675 }
676
677 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200678 list_for_each_entry(vma, vmas, exec_list) {
679 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100680 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000681
Ben Widawsky27173f12013-08-14 11:38:36 +0200682 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100683 if (ret)
684 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000685 }
686
Chris Wilsona415d352013-11-26 11:23:15 +0000687err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200688 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000689 return ret;
690
Chris Wilsona415d352013-11-26 11:23:15 +0000691 /* Decrement pin count for bound objects */
692 list_for_each_entry(vma, vmas, exec_list)
693 i915_gem_execbuffer_unreserve_vma(vma);
694
Ben Widawsky68c8c172013-09-11 14:57:50 -0700695 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000696 if (ret)
697 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000698 } while (1);
699}
700
701static int
702i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100703 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000704 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000705 struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200706 struct eb_vmas *eb,
707 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000708{
709 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200710 struct i915_address_space *vm;
711 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100712 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000713 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000714 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200715 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000716
Ben Widawsky27173f12013-08-14 11:38:36 +0200717 if (WARN_ON(list_empty(&eb->vmas)))
718 return 0;
719
720 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
721
Chris Wilson67731b82010-12-08 10:38:14 +0000722 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200723 while (!list_empty(&eb->vmas)) {
724 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
725 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000726 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200727 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000728 }
729
Chris Wilson54cf91d2010-11-25 18:00:26 +0000730 mutex_unlock(&dev->struct_mutex);
731
732 total = 0;
733 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000734 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000735
Chris Wilsondd6864a2011-01-12 23:49:13 +0000736 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000737 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000738 if (reloc == NULL || reloc_offset == NULL) {
739 drm_free_large(reloc);
740 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000741 mutex_lock(&dev->struct_mutex);
742 return -ENOMEM;
743 }
744
745 total = 0;
746 for (i = 0; i < count; i++) {
747 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000748 u64 invalid_offset = (u64)-1;
749 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000750
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200751 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000752
753 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000754 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000755 ret = -EFAULT;
756 mutex_lock(&dev->struct_mutex);
757 goto err;
758 }
759
Chris Wilson262b6d32013-01-15 16:17:54 +0000760 /* As we do not update the known relocation offsets after
761 * relocating (due to the complexities in lock handling),
762 * we need to mark them as invalid now so that we force the
763 * relocation processing next time. Just in case the target
764 * object is evicted and then rebound into its old
765 * presumed_offset before the next execbuffer - if that
766 * happened we would make the mistake of assuming that the
767 * relocations were valid.
768 */
769 for (j = 0; j < exec[i].relocation_count; j++) {
770 if (copy_to_user(&user_relocs[j].presumed_offset,
771 &invalid_offset,
772 sizeof(invalid_offset))) {
773 ret = -EFAULT;
774 mutex_lock(&dev->struct_mutex);
775 goto err;
776 }
777 }
778
Chris Wilsondd6864a2011-01-12 23:49:13 +0000779 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000780 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000781 }
782
783 ret = i915_mutex_lock_interruptible(dev);
784 if (ret) {
785 mutex_lock(&dev->struct_mutex);
786 goto err;
787 }
788
Chris Wilson67731b82010-12-08 10:38:14 +0000789 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000790 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200791 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000792 if (ret)
793 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000794
Daniel Vettered5982e2013-01-17 22:23:36 +0100795 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200796 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000797 if (ret)
798 goto err;
799
Ben Widawsky27173f12013-08-14 11:38:36 +0200800 list_for_each_entry(vma, &eb->vmas, exec_list) {
801 int offset = vma->exec_entry - exec;
802 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
803 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000804 if (ret)
805 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000806 }
807
808 /* Leave the user relocations as are, this is the painfully slow path,
809 * and we want to avoid the complication of dropping the lock whilst
810 * having buffers reserved in the aperture and so causing spurious
811 * ENOSPC for random operations.
812 */
813
814err:
815 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000816 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000817 return ret;
818}
819
Chris Wilson54cf91d2010-11-25 18:00:26 +0000820static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000821i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200822 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000823{
Ben Widawsky27173f12013-08-14 11:38:36 +0200824 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200825 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100826 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000827 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000828
Ben Widawsky27173f12013-08-14 11:38:36 +0200829 list_for_each_entry(vma, vmas, exec_list) {
830 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700831 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000832 if (ret)
833 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200834
835 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100836 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200837
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200838 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000839 }
840
Chris Wilson000433b2013-08-08 14:41:09 +0100841 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800842 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200843
844 if (flush_domains & I915_GEM_DOMAIN_GTT)
845 wmb();
846
Chris Wilson09cf7c92012-07-13 14:14:08 +0100847 /* Unconditionally invalidate gpu caches and ensure that we do flush
848 * any residual writes from the previous batch.
849 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100850 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000851}
852
Chris Wilson432e58e2010-11-25 19:32:06 +0000853static bool
854i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000855{
Daniel Vettered5982e2013-01-17 22:23:36 +0100856 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
857 return false;
858
Chris Wilson432e58e2010-11-25 19:32:06 +0000859 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000860}
861
862static int
863validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
864 int count)
865{
866 int i;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200867 unsigned relocs_total = 0;
868 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000869
870 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200871 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000872 int length; /* limited by fault_in_pages_readable() */
873
Daniel Vettered5982e2013-01-17 22:23:36 +0100874 if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
875 return -EINVAL;
876
Kees Cook3118a4f2013-03-11 17:31:45 -0700877 /* First check for malicious input causing overflow in
878 * the worst case where we need to allocate the entire
879 * relocation tree as a single array.
880 */
881 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000882 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700883 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000884
885 length = exec[i].relocation_count *
886 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700887 /*
888 * We must check that the entire relocation array is safe
889 * to read, but since we may need to update the presumed
890 * offsets during execution, check for full write access.
891 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000892 if (!access_ok(VERIFY_WRITE, ptr, length))
893 return -EFAULT;
894
Xiong Zhang0b74b502013-07-19 13:51:24 +0800895 if (likely(!i915_prefault_disable)) {
896 if (fault_in_multipages_readable(ptr, length))
897 return -EFAULT;
898 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000899 }
900
901 return 0;
902}
903
Chris Wilson432e58e2010-11-25 19:32:06 +0000904static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200905i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Chris Wilson9d7730912012-11-27 16:22:52 +0000906 struct intel_ring_buffer *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000907{
Ben Widawsky27173f12013-08-14 11:38:36 +0200908 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000909
Ben Widawsky27173f12013-08-14 11:38:36 +0200910 list_for_each_entry(vma, vmas, exec_list) {
911 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100912 u32 old_read = obj->base.read_domains;
913 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000914
Chris Wilson432e58e2010-11-25 19:32:06 +0000915 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100916 if (obj->base.write_domain == 0)
917 obj->base.pending_read_domains |= obj->base.read_domains;
918 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000919 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
920
Ben Widawskye2d05a82013-09-24 09:57:58 -0700921 i915_vma_move_to_active(vma, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000922 if (obj->base.write_domain) {
923 obj->dirty = 1;
Chris Wilson9d7730912012-11-27 16:22:52 +0000924 obj->last_write_seqno = intel_ring_get_seqno(ring);
Chris Wilsonacb87df2012-05-03 15:47:57 +0100925 if (obj->pin_count) /* check for potential scanout */
Chris Wilsonc65355b2013-06-06 16:53:41 -0300926 intel_mark_fb_busy(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000927 }
928
Chris Wilsondb53a302011-02-03 11:57:46 +0000929 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000930 }
931}
932
Chris Wilson54cf91d2010-11-25 18:00:26 +0000933static void
934i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000935 struct drm_file *file,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300936 struct intel_ring_buffer *ring,
937 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000938{
Daniel Vettercc889e02012-06-13 20:45:19 +0200939 /* Unconditionally force add_request to emit a full flush. */
940 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000941
Chris Wilson432e58e2010-11-25 19:32:06 +0000942 /* Add a breadcrumb for the completion of the batch buffer */
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300943 (void)__i915_add_request(ring, file, obj, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000944}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000945
946static int
Eric Anholtae662d32012-01-03 09:23:29 -0800947i915_reset_gen7_sol_offsets(struct drm_device *dev,
948 struct intel_ring_buffer *ring)
949{
950 drm_i915_private_t *dev_priv = dev->dev_private;
951 int ret, i;
952
953 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
954 return 0;
955
956 ret = intel_ring_begin(ring, 4 * 3);
957 if (ret)
958 return ret;
959
960 for (i = 0; i < 4; i++) {
961 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
962 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
963 intel_ring_emit(ring, 0);
964 }
965
966 intel_ring_advance(ring);
967
968 return 0;
969}
970
971static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000972i915_gem_do_execbuffer(struct drm_device *dev, void *data,
973 struct drm_file *file,
974 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700975 struct drm_i915_gem_exec_object2 *exec,
976 struct i915_address_space *vm)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000977{
978 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +0200979 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000980 struct drm_i915_gem_object *batch_obj;
981 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000982 struct intel_ring_buffer *ring;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +0300983 struct i915_ctx_hang_stats *hs;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700984 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000985 u32 exec_start, exec_len;
Daniel Vettered5982e2013-01-17 22:23:36 +0100986 u32 mask, flags;
Chris Wilson72bfa192010-12-19 11:42:05 +0000987 int ret, mode, i;
Daniel Vettered5982e2013-01-17 22:23:36 +0100988 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000989
Daniel Vettered5982e2013-01-17 22:23:36 +0100990 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +0000991 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +0000992
993 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000994 if (ret)
995 return ret;
996
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100997 flags = 0;
998 if (args->flags & I915_EXEC_SECURE) {
999 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1000 return -EPERM;
1001
1002 flags |= I915_DISPATCH_SECURE;
1003 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001004 if (args->flags & I915_EXEC_IS_PINNED)
1005 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001006
Chris Wilson54cf91d2010-11-25 18:00:26 +00001007 switch (args->flags & I915_EXEC_RING_MASK) {
1008 case I915_EXEC_DEFAULT:
1009 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001010 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +00001011 break;
1012 case I915_EXEC_BSD:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001013 ring = &dev_priv->ring[VCS];
Chris Wilsone8520962013-07-03 17:22:07 +03001014 if (ctx_id != DEFAULT_CONTEXT_ID) {
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001015 DRM_DEBUG("Ring %s doesn't support contexts\n",
1016 ring->name);
1017 return -EPERM;
1018 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001019 break;
1020 case I915_EXEC_BLT:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001021 ring = &dev_priv->ring[BCS];
Chris Wilsone8520962013-07-03 17:22:07 +03001022 if (ctx_id != DEFAULT_CONTEXT_ID) {
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001023 DRM_DEBUG("Ring %s doesn't support contexts\n",
1024 ring->name);
1025 return -EPERM;
1026 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001027 break;
Xiang, Haihao82f91b62013-05-28 19:22:33 -07001028 case I915_EXEC_VEBOX:
1029 ring = &dev_priv->ring[VECS];
Chris Wilsone8520962013-07-03 17:22:07 +03001030 if (ctx_id != DEFAULT_CONTEXT_ID) {
Xiang, Haihao82f91b62013-05-28 19:22:33 -07001031 DRM_DEBUG("Ring %s doesn't support contexts\n",
1032 ring->name);
1033 return -EPERM;
1034 }
1035 break;
1036
Chris Wilson54cf91d2010-11-25 18:00:26 +00001037 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001038 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001039 (int)(args->flags & I915_EXEC_RING_MASK));
1040 return -EINVAL;
1041 }
Chris Wilsona15817c2012-05-11 14:29:31 +01001042 if (!intel_ring_initialized(ring)) {
1043 DRM_DEBUG("execbuf with invalid ring: %d\n",
1044 (int)(args->flags & I915_EXEC_RING_MASK));
1045 return -EINVAL;
1046 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001047
Chris Wilson72bfa192010-12-19 11:42:05 +00001048 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001049 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +00001050 switch (mode) {
1051 case I915_EXEC_CONSTANTS_REL_GENERAL:
1052 case I915_EXEC_CONSTANTS_ABSOLUTE:
1053 case I915_EXEC_CONSTANTS_REL_SURFACE:
1054 if (ring == &dev_priv->ring[RCS] &&
1055 mode != dev_priv->relative_constants_mode) {
1056 if (INTEL_INFO(dev)->gen < 4)
1057 return -EINVAL;
1058
1059 if (INTEL_INFO(dev)->gen > 5 &&
1060 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1061 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001062
1063 /* The HW changed the meaning on this bit on gen6 */
1064 if (INTEL_INFO(dev)->gen >= 6)
1065 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +00001066 }
1067 break;
1068 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001069 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +00001070 return -EINVAL;
1071 }
1072
Chris Wilson54cf91d2010-11-25 18:00:26 +00001073 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001074 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001075 return -EINVAL;
1076 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001077
1078 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001079 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +01001080 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001081 return -EINVAL;
1082 }
1083
Daniel Vetter6ebebc92012-04-26 23:28:11 +02001084 if (INTEL_INFO(dev)->gen >= 5) {
1085 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1086 return -EINVAL;
1087 }
1088
Xi Wang44afb3a2012-04-23 04:06:42 -04001089 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1090 DRM_DEBUG("execbuf with %u cliprects\n",
1091 args->num_cliprects);
1092 return -EINVAL;
1093 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001094
Daniel Vettera1e22652013-09-21 00:35:38 +02001095 cliprects = kcalloc(args->num_cliprects,
1096 sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001097 GFP_KERNEL);
1098 if (cliprects == NULL) {
1099 ret = -ENOMEM;
1100 goto pre_mutex_err;
1101 }
1102
Chris Wilson432e58e2010-11-25 19:32:06 +00001103 if (copy_from_user(cliprects,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001104 to_user_ptr(args->cliprects_ptr),
1105 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001106 ret = -EFAULT;
1107 goto pre_mutex_err;
1108 }
1109 }
1110
Chris Wilson54cf91d2010-11-25 18:00:26 +00001111 ret = i915_mutex_lock_interruptible(dev);
1112 if (ret)
1113 goto pre_mutex_err;
1114
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001115 if (dev_priv->ums.mm_suspended) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001116 mutex_unlock(&dev->struct_mutex);
1117 ret = -EBUSY;
1118 goto pre_mutex_err;
1119 }
1120
Ben Widawsky27173f12013-08-14 11:38:36 +02001121 eb = eb_create(args, vm);
Chris Wilson67731b82010-12-08 10:38:14 +00001122 if (eb == NULL) {
1123 mutex_unlock(&dev->struct_mutex);
1124 ret = -ENOMEM;
1125 goto pre_mutex_err;
1126 }
1127
Chris Wilson54cf91d2010-11-25 18:00:26 +00001128 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001129 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001130 if (ret)
1131 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001132
Chris Wilson6fe4f142011-01-10 17:35:37 +00001133 /* take note of the batch buffer before we might reorder the lists */
Ben Widawsky27173f12013-08-14 11:38:36 +02001134 batch_obj = list_entry(eb->vmas.prev, struct i915_vma, exec_list)->obj;
Chris Wilson6fe4f142011-01-10 17:35:37 +00001135
Chris Wilson54cf91d2010-11-25 18:00:26 +00001136 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001137 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001138 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001139 if (ret)
1140 goto err;
1141
1142 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001143 if (need_relocs)
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001144 ret = i915_gem_execbuffer_relocate(eb, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001145 if (ret) {
1146 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001147 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001148 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001149 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1150 }
1151 if (ret)
1152 goto err;
1153 }
1154
1155 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001156 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001157 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001158 ret = -EINVAL;
1159 goto err;
1160 }
1161 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1162
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001163 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1164 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001165 * hsw should have this fixed, but bdw mucks it up again. */
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001166 if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
1167 i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
1168
Ben Widawsky27173f12013-08-14 11:38:36 +02001169 ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001170 if (ret)
1171 goto err;
1172
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03001173 hs = i915_gem_context_get_hang_stats(dev, file, ctx_id);
1174 if (IS_ERR(hs)) {
1175 ret = PTR_ERR(hs);
1176 goto err;
1177 }
1178
1179 if (hs->banned) {
1180 ret = -EIO;
1181 goto err;
1182 }
1183
Eric Anholt0da5cec2012-07-23 12:33:55 -07001184 ret = i915_switch_context(ring, file, ctx_id);
1185 if (ret)
1186 goto err;
1187
Ben Widawskye2971bd2011-12-12 19:21:57 -08001188 if (ring == &dev_priv->ring[RCS] &&
1189 mode != dev_priv->relative_constants_mode) {
1190 ret = intel_ring_begin(ring, 4);
1191 if (ret)
1192 goto err;
1193
1194 intel_ring_emit(ring, MI_NOOP);
1195 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1196 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001197 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001198 intel_ring_advance(ring);
1199
1200 dev_priv->relative_constants_mode = mode;
1201 }
1202
Eric Anholtae662d32012-01-03 09:23:29 -08001203 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1204 ret = i915_reset_gen7_sol_offsets(dev, ring);
1205 if (ret)
1206 goto err;
1207 }
1208
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001209 exec_start = i915_gem_obj_offset(batch_obj, vm) +
1210 args->batch_start_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001211 exec_len = args->batch_len;
1212 if (cliprects) {
1213 for (i = 0; i < args->num_cliprects; i++) {
1214 ret = i915_emit_box(dev, &cliprects[i],
1215 args->DR1, args->DR4);
1216 if (ret)
1217 goto err;
1218
1219 ret = ring->dispatch_execbuffer(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001220 exec_start, exec_len,
1221 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001222 if (ret)
1223 goto err;
1224 }
1225 } else {
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001226 ret = ring->dispatch_execbuffer(ring,
1227 exec_start, exec_len,
1228 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001229 if (ret)
1230 goto err;
1231 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001232
Chris Wilson9d7730912012-11-27 16:22:52 +00001233 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1234
Ben Widawsky27173f12013-08-14 11:38:36 +02001235 i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
Mika Kuoppala7d736f42013-06-12 15:01:39 +03001236 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001237
1238err:
Chris Wilson67731b82010-12-08 10:38:14 +00001239 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001240
1241 mutex_unlock(&dev->struct_mutex);
1242
1243pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001244 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001245 return ret;
1246}
1247
1248/*
1249 * Legacy execbuffer just creates an exec2 list from the original exec object
1250 * list array and passes it to the real function.
1251 */
1252int
1253i915_gem_execbuffer(struct drm_device *dev, void *data,
1254 struct drm_file *file)
1255{
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001256 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001257 struct drm_i915_gem_execbuffer *args = data;
1258 struct drm_i915_gem_execbuffer2 exec2;
1259 struct drm_i915_gem_exec_object *exec_list = NULL;
1260 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1261 int ret, i;
1262
Chris Wilson54cf91d2010-11-25 18:00:26 +00001263 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001264 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001265 return -EINVAL;
1266 }
1267
1268 /* Copy in the exec list from userland */
1269 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1270 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1271 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001272 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001273 args->buffer_count);
1274 drm_free_large(exec_list);
1275 drm_free_large(exec2_list);
1276 return -ENOMEM;
1277 }
1278 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001279 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001280 sizeof(*exec_list) * args->buffer_count);
1281 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001282 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001283 args->buffer_count, ret);
1284 drm_free_large(exec_list);
1285 drm_free_large(exec2_list);
1286 return -EFAULT;
1287 }
1288
1289 for (i = 0; i < args->buffer_count; i++) {
1290 exec2_list[i].handle = exec_list[i].handle;
1291 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1292 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1293 exec2_list[i].alignment = exec_list[i].alignment;
1294 exec2_list[i].offset = exec_list[i].offset;
1295 if (INTEL_INFO(dev)->gen < 4)
1296 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1297 else
1298 exec2_list[i].flags = 0;
1299 }
1300
1301 exec2.buffers_ptr = args->buffers_ptr;
1302 exec2.buffer_count = args->buffer_count;
1303 exec2.batch_start_offset = args->batch_start_offset;
1304 exec2.batch_len = args->batch_len;
1305 exec2.DR1 = args->DR1;
1306 exec2.DR4 = args->DR4;
1307 exec2.num_cliprects = args->num_cliprects;
1308 exec2.cliprects_ptr = args->cliprects_ptr;
1309 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001310 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001311
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001312 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list,
1313 &dev_priv->gtt.base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001314 if (!ret) {
1315 /* Copy the new buffer offsets back to the user's exec list. */
1316 for (i = 0; i < args->buffer_count; i++)
1317 exec_list[i].offset = exec2_list[i].offset;
1318 /* ... and back out to userspace */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001319 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001320 exec_list,
1321 sizeof(*exec_list) * args->buffer_count);
1322 if (ret) {
1323 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001324 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001325 "back to user (%d)\n",
1326 args->buffer_count, ret);
1327 }
1328 }
1329
1330 drm_free_large(exec_list);
1331 drm_free_large(exec2_list);
1332 return ret;
1333}
1334
1335int
1336i915_gem_execbuffer2(struct drm_device *dev, void *data,
1337 struct drm_file *file)
1338{
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001339 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001340 struct drm_i915_gem_execbuffer2 *args = data;
1341 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1342 int ret;
1343
Xi Wanged8cd3b2012-04-23 04:06:41 -04001344 if (args->buffer_count < 1 ||
1345 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001346 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001347 return -EINVAL;
1348 }
1349
Chris Wilson8408c282011-02-21 12:54:48 +00001350 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001351 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001352 if (exec2_list == NULL)
1353 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1354 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001355 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001356 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001357 args->buffer_count);
1358 return -ENOMEM;
1359 }
1360 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001361 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001362 sizeof(*exec2_list) * args->buffer_count);
1363 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001364 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001365 args->buffer_count, ret);
1366 drm_free_large(exec2_list);
1367 return -EFAULT;
1368 }
1369
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001370 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list,
1371 &dev_priv->gtt.base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001372 if (!ret) {
1373 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001374 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001375 exec2_list,
1376 sizeof(*exec2_list) * args->buffer_count);
1377 if (ret) {
1378 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001379 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001380 "back to user (%d)\n",
1381 args->buffer_count, ret);
1382 }
1383 }
1384
1385 drm_free_large(exec2_list);
1386 return ret;
1387}