blob: c8a01c141644d80e93936d858b21f0ada1fe6f51 [file] [log] [blame]
Chris Wilson54cf91d2010-11-25 18:00:26 +00001/*
2 * Copyright © 2008,2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
26 *
27 */
28
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
30#include <drm/i915_drm.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000031#include "i915_drv.h"
32#include "i915_trace.h"
33#include "intel_drv.h"
Eugeni Dodonovf45b5552011-12-09 17:16:37 -080034#include <linux/dma_remapping.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000035
Ben Widawsky27173f12013-08-14 11:38:36 +020036struct eb_vmas {
37 struct list_head vmas;
Chris Wilson67731b82010-12-08 10:38:14 +000038 int and;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000039 union {
Ben Widawsky27173f12013-08-14 11:38:36 +020040 struct i915_vma *lut[0];
Chris Wilsoneef90cc2013-01-08 10:53:17 +000041 struct hlist_head buckets[0];
42 };
Chris Wilson67731b82010-12-08 10:38:14 +000043};
44
Ben Widawsky27173f12013-08-14 11:38:36 +020045static struct eb_vmas *
46eb_create(struct drm_i915_gem_execbuffer2 *args, struct i915_address_space *vm)
Chris Wilson67731b82010-12-08 10:38:14 +000047{
Ben Widawsky27173f12013-08-14 11:38:36 +020048 struct eb_vmas *eb = NULL;
Chris Wilson67731b82010-12-08 10:38:14 +000049
Chris Wilsoneef90cc2013-01-08 10:53:17 +000050 if (args->flags & I915_EXEC_HANDLE_LUT) {
51 int size = args->buffer_count;
Ben Widawsky27173f12013-08-14 11:38:36 +020052 size *= sizeof(struct i915_vma *);
53 size += sizeof(struct eb_vmas);
Chris Wilsoneef90cc2013-01-08 10:53:17 +000054 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
55 }
56
57 if (eb == NULL) {
58 int size = args->buffer_count;
59 int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Lauri Kasanen27b7c632013-03-27 15:04:55 +020060 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
Chris Wilsoneef90cc2013-01-08 10:53:17 +000061 while (count > 2*size)
62 count >>= 1;
63 eb = kzalloc(count*sizeof(struct hlist_head) +
Ben Widawsky27173f12013-08-14 11:38:36 +020064 sizeof(struct eb_vmas),
Chris Wilsoneef90cc2013-01-08 10:53:17 +000065 GFP_TEMPORARY);
66 if (eb == NULL)
67 return eb;
68
69 eb->and = count - 1;
70 } else
71 eb->and = -args->buffer_count;
72
Ben Widawsky27173f12013-08-14 11:38:36 +020073 INIT_LIST_HEAD(&eb->vmas);
Chris Wilson67731b82010-12-08 10:38:14 +000074 return eb;
75}
76
77static void
Ben Widawsky27173f12013-08-14 11:38:36 +020078eb_reset(struct eb_vmas *eb)
Chris Wilson67731b82010-12-08 10:38:14 +000079{
Chris Wilsoneef90cc2013-01-08 10:53:17 +000080 if (eb->and >= 0)
81 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
Chris Wilson67731b82010-12-08 10:38:14 +000082}
83
Chris Wilson3b96eff2013-01-08 10:53:14 +000084static int
Ben Widawsky27173f12013-08-14 11:38:36 +020085eb_lookup_vmas(struct eb_vmas *eb,
86 struct drm_i915_gem_exec_object2 *exec,
87 const struct drm_i915_gem_execbuffer2 *args,
88 struct i915_address_space *vm,
89 struct drm_file *file)
Chris Wilson3b96eff2013-01-08 10:53:14 +000090{
Ben Widawsky27173f12013-08-14 11:38:36 +020091 struct drm_i915_gem_object *obj;
92 struct list_head objects;
93 int i, ret = 0;
Chris Wilson3b96eff2013-01-08 10:53:14 +000094
Ben Widawsky27173f12013-08-14 11:38:36 +020095 INIT_LIST_HEAD(&objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +000096 spin_lock(&file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +020097 /* Grab a reference to the object and release the lock so we can lookup
98 * or create the VMA without using GFP_ATOMIC */
Chris Wilsoneef90cc2013-01-08 10:53:17 +000099 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000100 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
101 if (obj == NULL) {
102 spin_unlock(&file->table_lock);
103 DRM_DEBUG("Invalid object handle %d at index %d\n",
104 exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200105 ret = -ENOENT;
106 goto out;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000107 }
108
Ben Widawsky27173f12013-08-14 11:38:36 +0200109 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000110 spin_unlock(&file->table_lock);
111 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
112 obj, exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200113 ret = -EINVAL;
114 goto out;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000115 }
116
117 drm_gem_object_reference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200118 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000119 }
120 spin_unlock(&file->table_lock);
121
Ben Widawsky27173f12013-08-14 11:38:36 +0200122 i = 0;
123 list_for_each_entry(obj, &objects, obj_exec_link) {
124 struct i915_vma *vma;
125
Daniel Vettere656a6c2013-08-14 14:14:04 +0200126 /*
127 * NOTE: We can leak any vmas created here when something fails
128 * later on. But that's no issue since vma_unbind can deal with
129 * vmas which are not actually bound. And since only
130 * lookup_or_create exists as an interface to get at the vma
131 * from the (obj, vm) we don't run the risk of creating
132 * duplicated vmas for the same vm.
133 */
Ben Widawsky27173f12013-08-14 11:38:36 +0200134 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
135 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200136 DRM_DEBUG("Failed to lookup VMA\n");
137 ret = PTR_ERR(vma);
138 goto out;
139 }
140
141 list_add_tail(&vma->exec_list, &eb->vmas);
142
143 vma->exec_entry = &exec[i];
144 if (eb->and < 0) {
145 eb->lut[i] = vma;
146 } else {
147 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
148 vma->exec_handle = handle;
149 hlist_add_head(&vma->exec_node,
150 &eb->buckets[handle & eb->and]);
151 }
152 ++i;
153 }
154
155
156out:
157 while (!list_empty(&objects)) {
158 obj = list_first_entry(&objects,
159 struct drm_i915_gem_object,
160 obj_exec_link);
161 list_del_init(&obj->obj_exec_link);
162 if (ret)
163 drm_gem_object_unreference(&obj->base);
164 }
165 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000166}
167
Ben Widawsky27173f12013-08-14 11:38:36 +0200168static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000169{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000170 if (eb->and < 0) {
171 if (handle >= -eb->and)
172 return NULL;
173 return eb->lut[handle];
174 } else {
175 struct hlist_head *head;
176 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000177
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000178 head = &eb->buckets[handle & eb->and];
179 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200180 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000181
Ben Widawsky27173f12013-08-14 11:38:36 +0200182 vma = hlist_entry(node, struct i915_vma, exec_node);
183 if (vma->exec_handle == handle)
184 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000185 }
186 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000187 }
Chris Wilson67731b82010-12-08 10:38:14 +0000188}
189
Ben Widawsky27173f12013-08-14 11:38:36 +0200190static void eb_destroy(struct eb_vmas *eb) {
191 while (!list_empty(&eb->vmas)) {
192 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000193
Ben Widawsky27173f12013-08-14 11:38:36 +0200194 vma = list_first_entry(&eb->vmas,
195 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000196 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200197 list_del_init(&vma->exec_list);
198 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000199 }
Chris Wilson67731b82010-12-08 10:38:14 +0000200 kfree(eb);
201}
202
Chris Wilsondabdfe02012-03-26 10:10:27 +0200203static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
204{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300205 return (HAS_LLC(obj->base.dev) ||
206 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100207 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200208 obj->cache_level != I915_CACHE_NONE);
209}
210
Chris Wilson54cf91d2010-11-25 18:00:26 +0000211static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100212relocate_entry_cpu(struct drm_i915_gem_object *obj,
213 struct drm_i915_gem_relocation_entry *reloc)
214{
215 uint32_t page_offset = offset_in_page(reloc->offset);
216 char *vaddr;
217 int ret = -EINVAL;
218
Chris Wilson2cc86b82013-08-26 19:51:00 -0300219 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100220 if (ret)
221 return ret;
222
223 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
224 reloc->offset >> PAGE_SHIFT));
225 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
226 kunmap_atomic(vaddr);
227
228 return 0;
229}
230
231static int
232relocate_entry_gtt(struct drm_i915_gem_object *obj,
233 struct drm_i915_gem_relocation_entry *reloc)
234{
235 struct drm_device *dev = obj->base.dev;
236 struct drm_i915_private *dev_priv = dev->dev_private;
237 uint32_t __iomem *reloc_entry;
238 void __iomem *reloc_page;
239 int ret = -EINVAL;
240
241 ret = i915_gem_object_set_to_gtt_domain(obj, true);
242 if (ret)
243 return ret;
244
245 ret = i915_gem_object_put_fence(obj);
246 if (ret)
247 return ret;
248
249 /* Map the page containing the relocation we're going to perform. */
250 reloc->offset += i915_gem_obj_ggtt_offset(obj);
251 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
252 reloc->offset & PAGE_MASK);
253 reloc_entry = (uint32_t __iomem *)
254 (reloc_page + offset_in_page(reloc->offset));
255 iowrite32(reloc->delta, reloc_entry);
256 io_mapping_unmap_atomic(reloc_page);
257
258 return 0;
259}
260
261static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000262i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200263 struct eb_vmas *eb,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700264 struct drm_i915_gem_relocation_entry *reloc,
265 struct i915_address_space *vm)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000266{
267 struct drm_device *dev = obj->base.dev;
268 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100269 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200270 struct i915_vma *target_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000271 uint32_t target_offset;
272 int ret = -EINVAL;
273
Chris Wilson67731b82010-12-08 10:38:14 +0000274 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200275 target_vma = eb_get_vma(eb, reloc->target_handle);
276 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000277 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200278 target_i915_obj = target_vma->obj;
279 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000280
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700281 target_offset = i915_gem_obj_ggtt_offset(target_i915_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000282
Eric Anholte844b992012-07-31 15:35:01 -0700283 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
284 * pipe_control writes because the gpu doesn't properly redirect them
285 * through the ppgtt for non_secure batchbuffers. */
286 if (unlikely(IS_GEN6(dev) &&
287 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
288 !target_i915_obj->has_global_gtt_mapping)) {
289 i915_gem_gtt_bind_object(target_i915_obj,
290 target_i915_obj->cache_level);
291 }
292
Chris Wilson54cf91d2010-11-25 18:00:26 +0000293 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000294 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100295 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000296 "obj %p target %d offset %d "
297 "read %08x write %08x",
298 obj, reloc->target_handle,
299 (int) reloc->offset,
300 reloc->read_domains,
301 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000302 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000303 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100304 if (unlikely((reloc->write_domain | reloc->read_domains)
305 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100306 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000307 "obj %p target %d offset %d "
308 "read %08x write %08x",
309 obj, reloc->target_handle,
310 (int) reloc->offset,
311 reloc->read_domains,
312 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000313 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000314 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000315
316 target_obj->pending_read_domains |= reloc->read_domains;
317 target_obj->pending_write_domain |= reloc->write_domain;
318
319 /* If the relocation already has the right value in it, no
320 * more work needs to be done.
321 */
322 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000323 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000324
325 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000326 if (unlikely(reloc->offset > obj->base.size - 4)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100327 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000328 "obj %p target %d offset %d size %d.\n",
329 obj, reloc->target_handle,
330 (int) reloc->offset,
331 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000332 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000333 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000334 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100335 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000336 "obj %p target %d offset %d.\n",
337 obj, reloc->target_handle,
338 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000339 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000340 }
341
Chris Wilsondabdfe02012-03-26 10:10:27 +0200342 /* We can't wait for rendering with pagefaults disabled */
343 if (obj->active && in_atomic())
344 return -EFAULT;
345
Chris Wilson54cf91d2010-11-25 18:00:26 +0000346 reloc->delta += target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100347 if (use_cpu_reloc(obj))
348 ret = relocate_entry_cpu(obj, reloc);
349 else
350 ret = relocate_entry_gtt(obj, reloc);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000351
Daniel Vetterd4d36012013-09-02 20:56:23 +0200352 if (ret)
353 return ret;
354
Chris Wilson54cf91d2010-11-25 18:00:26 +0000355 /* and update the user's relocation entry */
356 reloc->presumed_offset = target_offset;
357
Chris Wilson67731b82010-12-08 10:38:14 +0000358 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000359}
360
361static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200362i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
363 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000364{
Chris Wilson1d83f442012-03-24 20:12:53 +0000365#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
366 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000367 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200368 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000369 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000370
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200371 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000372
Chris Wilson1d83f442012-03-24 20:12:53 +0000373 remain = entry->relocation_count;
374 while (remain) {
375 struct drm_i915_gem_relocation_entry *r = stack_reloc;
376 int count = remain;
377 if (count > ARRAY_SIZE(stack_reloc))
378 count = ARRAY_SIZE(stack_reloc);
379 remain -= count;
380
381 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000382 return -EFAULT;
383
Chris Wilson1d83f442012-03-24 20:12:53 +0000384 do {
385 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000386
Ben Widawsky27173f12013-08-14 11:38:36 +0200387 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r,
388 vma->vm);
Chris Wilson1d83f442012-03-24 20:12:53 +0000389 if (ret)
390 return ret;
391
392 if (r->presumed_offset != offset &&
393 __copy_to_user_inatomic(&user_relocs->presumed_offset,
394 &r->presumed_offset,
395 sizeof(r->presumed_offset))) {
396 return -EFAULT;
397 }
398
399 user_relocs++;
400 r++;
401 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000402 }
403
404 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000405#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000406}
407
408static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200409i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
410 struct eb_vmas *eb,
411 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000412{
Ben Widawsky27173f12013-08-14 11:38:36 +0200413 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000414 int i, ret;
415
416 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200417 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i],
418 vma->vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000419 if (ret)
420 return ret;
421 }
422
423 return 0;
424}
425
426static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200427i915_gem_execbuffer_relocate(struct eb_vmas *eb,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700428 struct i915_address_space *vm)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000429{
Ben Widawsky27173f12013-08-14 11:38:36 +0200430 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000431 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000432
Chris Wilsond4aeee72011-03-14 15:11:24 +0000433 /* This is the fast path and we cannot handle a pagefault whilst
434 * holding the struct mutex lest the user pass in the relocations
435 * contained within a mmaped bo. For in such a case we, the page
436 * fault handler would call i915_gem_fault() and we would try to
437 * acquire the struct mutex again. Obviously this is bad and so
438 * lockdep complains vehemently.
439 */
440 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200441 list_for_each_entry(vma, &eb->vmas, exec_list) {
442 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000443 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000444 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000445 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000446 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000447
Chris Wilsond4aeee72011-03-14 15:11:24 +0000448 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000449}
450
Chris Wilson7788a762012-08-24 19:18:18 +0100451#define __EXEC_OBJECT_HAS_PIN (1<<31)
452#define __EXEC_OBJECT_HAS_FENCE (1<<30)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100453
454static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200455need_reloc_mappable(struct i915_vma *vma)
Chris Wilsondabdfe02012-03-26 10:10:27 +0200456{
Ben Widawsky27173f12013-08-14 11:38:36 +0200457 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
458 return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
459 i915_is_ggtt(vma->vm);
Chris Wilsondabdfe02012-03-26 10:10:27 +0200460}
461
462static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200463i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
464 struct intel_ring_buffer *ring,
465 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100466{
Ben Widawsky27173f12013-08-14 11:38:36 +0200467 struct drm_i915_private *dev_priv = ring->dev->dev_private;
468 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100469 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
470 bool need_fence, need_mappable;
Ben Widawsky27173f12013-08-14 11:38:36 +0200471 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100472 int ret;
473
474 need_fence =
475 has_fenced_gpu_access &&
476 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
477 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200478 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100479
Ben Widawsky27173f12013-08-14 11:38:36 +0200480 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, need_mappable,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700481 false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100482 if (ret)
483 return ret;
484
Chris Wilson7788a762012-08-24 19:18:18 +0100485 entry->flags |= __EXEC_OBJECT_HAS_PIN;
486
Chris Wilson1690e1e2011-12-14 13:57:08 +0100487 if (has_fenced_gpu_access) {
488 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100489 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000490 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100491 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100492
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000493 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100494 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000495
Chris Wilson7dd49062012-03-21 10:48:18 +0000496 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100497 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100498 }
499
Chris Wilson7788a762012-08-24 19:18:18 +0100500 /* Ensure ppgtt mapping exists if needed */
501 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
502 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
503 obj, obj->cache_level);
504
505 obj->has_aliasing_ppgtt_mapping = 1;
506 }
507
Ben Widawsky27173f12013-08-14 11:38:36 +0200508 if (entry->offset != vma->node.start) {
509 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100510 *need_reloc = true;
511 }
512
513 if (entry->flags & EXEC_OBJECT_WRITE) {
514 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
515 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
516 }
517
518 if (entry->flags & EXEC_OBJECT_NEEDS_GTT &&
519 !obj->has_global_gtt_mapping)
520 i915_gem_gtt_bind_object(obj, obj->cache_level);
521
Chris Wilson1690e1e2011-12-14 13:57:08 +0100522 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100523}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100524
Chris Wilson7788a762012-08-24 19:18:18 +0100525static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200526i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
Chris Wilson7788a762012-08-24 19:18:18 +0100527{
528 struct drm_i915_gem_exec_object2 *entry;
Ben Widawsky27173f12013-08-14 11:38:36 +0200529 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson7788a762012-08-24 19:18:18 +0100530
Ben Widawsky27173f12013-08-14 11:38:36 +0200531 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson7788a762012-08-24 19:18:18 +0100532 return;
533
Ben Widawsky27173f12013-08-14 11:38:36 +0200534 entry = vma->exec_entry;
Chris Wilson7788a762012-08-24 19:18:18 +0100535
536 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
537 i915_gem_object_unpin_fence(obj);
538
539 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
540 i915_gem_object_unpin(obj);
541
542 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100543}
544
Chris Wilson54cf91d2010-11-25 18:00:26 +0000545static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000546i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200547 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100548 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000549{
Chris Wilson432e58e2010-11-25 19:32:06 +0000550 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200551 struct i915_vma *vma;
552 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100553 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
554 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000555
Ben Widawsky27173f12013-08-14 11:38:36 +0200556 INIT_LIST_HEAD(&ordered_vmas);
557 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000558 struct drm_i915_gem_exec_object2 *entry;
559 bool need_fence, need_mappable;
560
Ben Widawsky27173f12013-08-14 11:38:36 +0200561 vma = list_first_entry(vmas, struct i915_vma, exec_list);
562 obj = vma->obj;
563 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000564
565 need_fence =
566 has_fenced_gpu_access &&
567 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
568 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200569 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000570
571 if (need_mappable)
Ben Widawsky27173f12013-08-14 11:38:36 +0200572 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000573 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200574 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000575
Daniel Vettered5982e2013-01-17 22:23:36 +0100576 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000577 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100578 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000579 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200580 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000581
582 /* Attempt to pin all of the buffers into the GTT.
583 * This is done in 3 phases:
584 *
585 * 1a. Unbind all objects that do not match the GTT constraints for
586 * the execbuffer (fenceable, mappable, alignment etc).
587 * 1b. Increment pin count for already bound objects.
588 * 2. Bind new objects.
589 * 3. Decrement pin count.
590 *
Chris Wilson7788a762012-08-24 19:18:18 +0100591 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000592 * room for the earlier objects *unless* we need to defragment.
593 */
594 retry = 0;
595 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100596 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000597
598 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200599 list_for_each_entry(vma, vmas, exec_list) {
600 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000601 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100602
Ben Widawsky27173f12013-08-14 11:38:36 +0200603 obj = vma->obj;
604
605 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000606 continue;
607
608 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000609 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000610 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 Wilson54cf91d2010-11-25 18:00:26 +0000613
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700614 WARN_ON((need_mappable || need_fence) &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200615 !i915_is_ggtt(vma->vm));
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700616
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700617 if ((entry->alignment &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200618 vma->node.start & (entry->alignment - 1)) ||
Chris Wilson54cf91d2010-11-25 18:00:26 +0000619 (need_mappable && !obj->map_and_fenceable))
Ben Widawsky27173f12013-08-14 11:38:36 +0200620 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000621 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200622 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000623 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000624 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000625 }
626
627 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200628 list_for_each_entry(vma, vmas, exec_list) {
629 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100630 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000631
Ben Widawsky27173f12013-08-14 11:38:36 +0200632 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100633 if (ret)
634 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000635 }
636
Chris Wilson7788a762012-08-24 19:18:18 +0100637err: /* Decrement pin count for bound objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200638 list_for_each_entry(vma, vmas, exec_list)
639 i915_gem_execbuffer_unreserve_vma(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000640
Chris Wilson6c085a72012-08-20 11:40:46 +0200641 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000642 return ret;
643
Chris Wilson6c085a72012-08-20 11:40:46 +0200644 ret = i915_gem_evict_everything(ring->dev);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000645 if (ret)
646 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000647 } while (1);
648}
649
650static int
651i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100652 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000653 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000654 struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200655 struct eb_vmas *eb,
656 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000657{
658 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200659 struct i915_address_space *vm;
660 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100661 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000662 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000663 int i, total, ret;
Daniel Vettered5982e2013-01-17 22:23:36 +0100664 int count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000665
Ben Widawsky27173f12013-08-14 11:38:36 +0200666 if (WARN_ON(list_empty(&eb->vmas)))
667 return 0;
668
669 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
670
Chris Wilson67731b82010-12-08 10:38:14 +0000671 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200672 while (!list_empty(&eb->vmas)) {
673 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
674 list_del_init(&vma->exec_list);
675 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000676 }
677
Chris Wilson54cf91d2010-11-25 18:00:26 +0000678 mutex_unlock(&dev->struct_mutex);
679
680 total = 0;
681 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000682 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000683
Chris Wilsondd6864a2011-01-12 23:49:13 +0000684 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000685 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000686 if (reloc == NULL || reloc_offset == NULL) {
687 drm_free_large(reloc);
688 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000689 mutex_lock(&dev->struct_mutex);
690 return -ENOMEM;
691 }
692
693 total = 0;
694 for (i = 0; i < count; i++) {
695 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000696 u64 invalid_offset = (u64)-1;
697 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000698
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200699 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000700
701 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000702 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000703 ret = -EFAULT;
704 mutex_lock(&dev->struct_mutex);
705 goto err;
706 }
707
Chris Wilson262b6d32013-01-15 16:17:54 +0000708 /* As we do not update the known relocation offsets after
709 * relocating (due to the complexities in lock handling),
710 * we need to mark them as invalid now so that we force the
711 * relocation processing next time. Just in case the target
712 * object is evicted and then rebound into its old
713 * presumed_offset before the next execbuffer - if that
714 * happened we would make the mistake of assuming that the
715 * relocations were valid.
716 */
717 for (j = 0; j < exec[i].relocation_count; j++) {
718 if (copy_to_user(&user_relocs[j].presumed_offset,
719 &invalid_offset,
720 sizeof(invalid_offset))) {
721 ret = -EFAULT;
722 mutex_lock(&dev->struct_mutex);
723 goto err;
724 }
725 }
726
Chris Wilsondd6864a2011-01-12 23:49:13 +0000727 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000728 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000729 }
730
731 ret = i915_mutex_lock_interruptible(dev);
732 if (ret) {
733 mutex_lock(&dev->struct_mutex);
734 goto err;
735 }
736
Chris Wilson67731b82010-12-08 10:38:14 +0000737 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000738 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200739 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000740 if (ret)
741 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000742
Daniel Vettered5982e2013-01-17 22:23:36 +0100743 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200744 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000745 if (ret)
746 goto err;
747
Ben Widawsky27173f12013-08-14 11:38:36 +0200748 list_for_each_entry(vma, &eb->vmas, exec_list) {
749 int offset = vma->exec_entry - exec;
750 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
751 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000752 if (ret)
753 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000754 }
755
756 /* Leave the user relocations as are, this is the painfully slow path,
757 * and we want to avoid the complication of dropping the lock whilst
758 * having buffers reserved in the aperture and so causing spurious
759 * ENOSPC for random operations.
760 */
761
762err:
763 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000764 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000765 return ret;
766}
767
Chris Wilson54cf91d2010-11-25 18:00:26 +0000768static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000769i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200770 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000771{
Ben Widawsky27173f12013-08-14 11:38:36 +0200772 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200773 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100774 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000775 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000776
Ben Widawsky27173f12013-08-14 11:38:36 +0200777 list_for_each_entry(vma, vmas, exec_list) {
778 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700779 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000780 if (ret)
781 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200782
783 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100784 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200785
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200786 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000787 }
788
Chris Wilson000433b2013-08-08 14:41:09 +0100789 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800790 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200791
792 if (flush_domains & I915_GEM_DOMAIN_GTT)
793 wmb();
794
Chris Wilson09cf7c92012-07-13 14:14:08 +0100795 /* Unconditionally invalidate gpu caches and ensure that we do flush
796 * any residual writes from the previous batch.
797 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100798 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000799}
800
Chris Wilson432e58e2010-11-25 19:32:06 +0000801static bool
802i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000803{
Daniel Vettered5982e2013-01-17 22:23:36 +0100804 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
805 return false;
806
Chris Wilson432e58e2010-11-25 19:32:06 +0000807 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000808}
809
810static int
811validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
812 int count)
813{
814 int i;
Kees Cook3118a4f2013-03-11 17:31:45 -0700815 int relocs_total = 0;
816 int relocs_max = INT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000817
818 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200819 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000820 int length; /* limited by fault_in_pages_readable() */
821
Daniel Vettered5982e2013-01-17 22:23:36 +0100822 if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
823 return -EINVAL;
824
Kees Cook3118a4f2013-03-11 17:31:45 -0700825 /* First check for malicious input causing overflow in
826 * the worst case where we need to allocate the entire
827 * relocation tree as a single array.
828 */
829 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000830 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700831 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000832
833 length = exec[i].relocation_count *
834 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700835 /*
836 * We must check that the entire relocation array is safe
837 * to read, but since we may need to update the presumed
838 * offsets during execution, check for full write access.
839 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000840 if (!access_ok(VERIFY_WRITE, ptr, length))
841 return -EFAULT;
842
Xiong Zhang0b74b502013-07-19 13:51:24 +0800843 if (likely(!i915_prefault_disable)) {
844 if (fault_in_multipages_readable(ptr, length))
845 return -EFAULT;
846 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000847 }
848
849 return 0;
850}
851
Chris Wilson432e58e2010-11-25 19:32:06 +0000852static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200853i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Chris Wilson9d7730912012-11-27 16:22:52 +0000854 struct intel_ring_buffer *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000855{
Ben Widawsky27173f12013-08-14 11:38:36 +0200856 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000857
Ben Widawsky27173f12013-08-14 11:38:36 +0200858 list_for_each_entry(vma, vmas, exec_list) {
859 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100860 u32 old_read = obj->base.read_domains;
861 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000862
Chris Wilson432e58e2010-11-25 19:32:06 +0000863 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100864 if (obj->base.write_domain == 0)
865 obj->base.pending_read_domains |= obj->base.read_domains;
866 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000867 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
868
Ben Widawsky27173f12013-08-14 11:38:36 +0200869 list_move_tail(&vma->mm_list, &vma->vm->active_list);
Chris Wilson9d7730912012-11-27 16:22:52 +0000870 i915_gem_object_move_to_active(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000871 if (obj->base.write_domain) {
872 obj->dirty = 1;
Chris Wilson9d7730912012-11-27 16:22:52 +0000873 obj->last_write_seqno = intel_ring_get_seqno(ring);
Chris Wilsonacb87df2012-05-03 15:47:57 +0100874 if (obj->pin_count) /* check for potential scanout */
Chris Wilsonc65355b2013-06-06 16:53:41 -0300875 intel_mark_fb_busy(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000876 }
877
Chris Wilsondb53a302011-02-03 11:57:46 +0000878 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000879 }
880}
881
Chris Wilson54cf91d2010-11-25 18:00:26 +0000882static void
883i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000884 struct drm_file *file,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300885 struct intel_ring_buffer *ring,
886 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000887{
Daniel Vettercc889e02012-06-13 20:45:19 +0200888 /* Unconditionally force add_request to emit a full flush. */
889 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000890
Chris Wilson432e58e2010-11-25 19:32:06 +0000891 /* Add a breadcrumb for the completion of the batch buffer */
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300892 (void)__i915_add_request(ring, file, obj, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000893}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000894
895static int
Eric Anholtae662d32012-01-03 09:23:29 -0800896i915_reset_gen7_sol_offsets(struct drm_device *dev,
897 struct intel_ring_buffer *ring)
898{
899 drm_i915_private_t *dev_priv = dev->dev_private;
900 int ret, i;
901
902 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
903 return 0;
904
905 ret = intel_ring_begin(ring, 4 * 3);
906 if (ret)
907 return ret;
908
909 for (i = 0; i < 4; i++) {
910 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
911 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
912 intel_ring_emit(ring, 0);
913 }
914
915 intel_ring_advance(ring);
916
917 return 0;
918}
919
920static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000921i915_gem_do_execbuffer(struct drm_device *dev, void *data,
922 struct drm_file *file,
923 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700924 struct drm_i915_gem_exec_object2 *exec,
925 struct i915_address_space *vm)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000926{
927 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +0200928 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000929 struct drm_i915_gem_object *batch_obj;
930 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000931 struct intel_ring_buffer *ring;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +0300932 struct i915_ctx_hang_stats *hs;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700933 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000934 u32 exec_start, exec_len;
Daniel Vettered5982e2013-01-17 22:23:36 +0100935 u32 mask, flags;
Chris Wilson72bfa192010-12-19 11:42:05 +0000936 int ret, mode, i;
Daniel Vettered5982e2013-01-17 22:23:36 +0100937 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000938
Daniel Vettered5982e2013-01-17 22:23:36 +0100939 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +0000940 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +0000941
942 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000943 if (ret)
944 return ret;
945
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100946 flags = 0;
947 if (args->flags & I915_EXEC_SECURE) {
948 if (!file->is_master || !capable(CAP_SYS_ADMIN))
949 return -EPERM;
950
951 flags |= I915_DISPATCH_SECURE;
952 }
Daniel Vetterb45305f2012-12-17 16:21:27 +0100953 if (args->flags & I915_EXEC_IS_PINNED)
954 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100955
Chris Wilson54cf91d2010-11-25 18:00:26 +0000956 switch (args->flags & I915_EXEC_RING_MASK) {
957 case I915_EXEC_DEFAULT:
958 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000959 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000960 break;
961 case I915_EXEC_BSD:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000962 ring = &dev_priv->ring[VCS];
Chris Wilsone8520962013-07-03 17:22:07 +0300963 if (ctx_id != DEFAULT_CONTEXT_ID) {
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700964 DRM_DEBUG("Ring %s doesn't support contexts\n",
965 ring->name);
966 return -EPERM;
967 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000968 break;
969 case I915_EXEC_BLT:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000970 ring = &dev_priv->ring[BCS];
Chris Wilsone8520962013-07-03 17:22:07 +0300971 if (ctx_id != DEFAULT_CONTEXT_ID) {
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700972 DRM_DEBUG("Ring %s doesn't support contexts\n",
973 ring->name);
974 return -EPERM;
975 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000976 break;
Xiang, Haihao82f91b62013-05-28 19:22:33 -0700977 case I915_EXEC_VEBOX:
978 ring = &dev_priv->ring[VECS];
Chris Wilsone8520962013-07-03 17:22:07 +0300979 if (ctx_id != DEFAULT_CONTEXT_ID) {
Xiang, Haihao82f91b62013-05-28 19:22:33 -0700980 DRM_DEBUG("Ring %s doesn't support contexts\n",
981 ring->name);
982 return -EPERM;
983 }
984 break;
985
Chris Wilson54cf91d2010-11-25 18:00:26 +0000986 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100987 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000988 (int)(args->flags & I915_EXEC_RING_MASK));
989 return -EINVAL;
990 }
Chris Wilsona15817c2012-05-11 14:29:31 +0100991 if (!intel_ring_initialized(ring)) {
992 DRM_DEBUG("execbuf with invalid ring: %d\n",
993 (int)(args->flags & I915_EXEC_RING_MASK));
994 return -EINVAL;
995 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000996
Chris Wilson72bfa192010-12-19 11:42:05 +0000997 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800998 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +0000999 switch (mode) {
1000 case I915_EXEC_CONSTANTS_REL_GENERAL:
1001 case I915_EXEC_CONSTANTS_ABSOLUTE:
1002 case I915_EXEC_CONSTANTS_REL_SURFACE:
1003 if (ring == &dev_priv->ring[RCS] &&
1004 mode != dev_priv->relative_constants_mode) {
1005 if (INTEL_INFO(dev)->gen < 4)
1006 return -EINVAL;
1007
1008 if (INTEL_INFO(dev)->gen > 5 &&
1009 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1010 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001011
1012 /* The HW changed the meaning on this bit on gen6 */
1013 if (INTEL_INFO(dev)->gen >= 6)
1014 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +00001015 }
1016 break;
1017 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001018 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +00001019 return -EINVAL;
1020 }
1021
Chris Wilson54cf91d2010-11-25 18:00:26 +00001022 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001023 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001024 return -EINVAL;
1025 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001026
1027 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001028 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +01001029 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001030 return -EINVAL;
1031 }
1032
Daniel Vetter6ebebc92012-04-26 23:28:11 +02001033 if (INTEL_INFO(dev)->gen >= 5) {
1034 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1035 return -EINVAL;
1036 }
1037
Xi Wang44afb3a2012-04-23 04:06:42 -04001038 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1039 DRM_DEBUG("execbuf with %u cliprects\n",
1040 args->num_cliprects);
1041 return -EINVAL;
1042 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001043
Chris Wilson432e58e2010-11-25 19:32:06 +00001044 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001045 GFP_KERNEL);
1046 if (cliprects == NULL) {
1047 ret = -ENOMEM;
1048 goto pre_mutex_err;
1049 }
1050
Chris Wilson432e58e2010-11-25 19:32:06 +00001051 if (copy_from_user(cliprects,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001052 to_user_ptr(args->cliprects_ptr),
1053 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001054 ret = -EFAULT;
1055 goto pre_mutex_err;
1056 }
1057 }
1058
Chris Wilson54cf91d2010-11-25 18:00:26 +00001059 ret = i915_mutex_lock_interruptible(dev);
1060 if (ret)
1061 goto pre_mutex_err;
1062
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001063 if (dev_priv->ums.mm_suspended) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001064 mutex_unlock(&dev->struct_mutex);
1065 ret = -EBUSY;
1066 goto pre_mutex_err;
1067 }
1068
Ben Widawsky27173f12013-08-14 11:38:36 +02001069 eb = eb_create(args, vm);
Chris Wilson67731b82010-12-08 10:38:14 +00001070 if (eb == NULL) {
1071 mutex_unlock(&dev->struct_mutex);
1072 ret = -ENOMEM;
1073 goto pre_mutex_err;
1074 }
1075
Chris Wilson54cf91d2010-11-25 18:00:26 +00001076 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001077 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001078 if (ret)
1079 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001080
Chris Wilson6fe4f142011-01-10 17:35:37 +00001081 /* take note of the batch buffer before we might reorder the lists */
Ben Widawsky27173f12013-08-14 11:38:36 +02001082 batch_obj = list_entry(eb->vmas.prev, struct i915_vma, exec_list)->obj;
Chris Wilson6fe4f142011-01-10 17:35:37 +00001083
Chris Wilson54cf91d2010-11-25 18:00:26 +00001084 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001085 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001086 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001087 if (ret)
1088 goto err;
1089
1090 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001091 if (need_relocs)
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001092 ret = i915_gem_execbuffer_relocate(eb, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001093 if (ret) {
1094 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001095 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001096 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001097 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1098 }
1099 if (ret)
1100 goto err;
1101 }
1102
1103 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001104 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001105 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001106 ret = -EINVAL;
1107 goto err;
1108 }
1109 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1110
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001111 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1112 * batch" bit. Hence we need to pin secure batches into the global gtt.
1113 * hsw should have this fixed, but let's be paranoid and do it
1114 * unconditionally for now. */
1115 if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
1116 i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
1117
Ben Widawsky27173f12013-08-14 11:38:36 +02001118 ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001119 if (ret)
1120 goto err;
1121
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03001122 hs = i915_gem_context_get_hang_stats(dev, file, ctx_id);
1123 if (IS_ERR(hs)) {
1124 ret = PTR_ERR(hs);
1125 goto err;
1126 }
1127
1128 if (hs->banned) {
1129 ret = -EIO;
1130 goto err;
1131 }
1132
Eric Anholt0da5cec2012-07-23 12:33:55 -07001133 ret = i915_switch_context(ring, file, ctx_id);
1134 if (ret)
1135 goto err;
1136
Ben Widawskye2971bd2011-12-12 19:21:57 -08001137 if (ring == &dev_priv->ring[RCS] &&
1138 mode != dev_priv->relative_constants_mode) {
1139 ret = intel_ring_begin(ring, 4);
1140 if (ret)
1141 goto err;
1142
1143 intel_ring_emit(ring, MI_NOOP);
1144 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1145 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001146 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001147 intel_ring_advance(ring);
1148
1149 dev_priv->relative_constants_mode = mode;
1150 }
1151
Eric Anholtae662d32012-01-03 09:23:29 -08001152 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1153 ret = i915_reset_gen7_sol_offsets(dev, ring);
1154 if (ret)
1155 goto err;
1156 }
1157
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001158 exec_start = i915_gem_obj_offset(batch_obj, vm) +
1159 args->batch_start_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001160 exec_len = args->batch_len;
1161 if (cliprects) {
1162 for (i = 0; i < args->num_cliprects; i++) {
1163 ret = i915_emit_box(dev, &cliprects[i],
1164 args->DR1, args->DR4);
1165 if (ret)
1166 goto err;
1167
1168 ret = ring->dispatch_execbuffer(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001169 exec_start, exec_len,
1170 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001171 if (ret)
1172 goto err;
1173 }
1174 } else {
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001175 ret = ring->dispatch_execbuffer(ring,
1176 exec_start, exec_len,
1177 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001178 if (ret)
1179 goto err;
1180 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001181
Chris Wilson9d7730912012-11-27 16:22:52 +00001182 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1183
Ben Widawsky27173f12013-08-14 11:38:36 +02001184 i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
Mika Kuoppala7d736f42013-06-12 15:01:39 +03001185 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001186
1187err:
Chris Wilson67731b82010-12-08 10:38:14 +00001188 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001189
1190 mutex_unlock(&dev->struct_mutex);
1191
1192pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001193 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001194 return ret;
1195}
1196
1197/*
1198 * Legacy execbuffer just creates an exec2 list from the original exec object
1199 * list array and passes it to the real function.
1200 */
1201int
1202i915_gem_execbuffer(struct drm_device *dev, void *data,
1203 struct drm_file *file)
1204{
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001205 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001206 struct drm_i915_gem_execbuffer *args = data;
1207 struct drm_i915_gem_execbuffer2 exec2;
1208 struct drm_i915_gem_exec_object *exec_list = NULL;
1209 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1210 int ret, i;
1211
Chris Wilson54cf91d2010-11-25 18:00:26 +00001212 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001213 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001214 return -EINVAL;
1215 }
1216
1217 /* Copy in the exec list from userland */
1218 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1219 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1220 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001221 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001222 args->buffer_count);
1223 drm_free_large(exec_list);
1224 drm_free_large(exec2_list);
1225 return -ENOMEM;
1226 }
1227 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001228 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001229 sizeof(*exec_list) * args->buffer_count);
1230 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001231 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001232 args->buffer_count, ret);
1233 drm_free_large(exec_list);
1234 drm_free_large(exec2_list);
1235 return -EFAULT;
1236 }
1237
1238 for (i = 0; i < args->buffer_count; i++) {
1239 exec2_list[i].handle = exec_list[i].handle;
1240 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1241 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1242 exec2_list[i].alignment = exec_list[i].alignment;
1243 exec2_list[i].offset = exec_list[i].offset;
1244 if (INTEL_INFO(dev)->gen < 4)
1245 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1246 else
1247 exec2_list[i].flags = 0;
1248 }
1249
1250 exec2.buffers_ptr = args->buffers_ptr;
1251 exec2.buffer_count = args->buffer_count;
1252 exec2.batch_start_offset = args->batch_start_offset;
1253 exec2.batch_len = args->batch_len;
1254 exec2.DR1 = args->DR1;
1255 exec2.DR4 = args->DR4;
1256 exec2.num_cliprects = args->num_cliprects;
1257 exec2.cliprects_ptr = args->cliprects_ptr;
1258 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001259 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001260
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001261 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list,
1262 &dev_priv->gtt.base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001263 if (!ret) {
1264 /* Copy the new buffer offsets back to the user's exec list. */
1265 for (i = 0; i < args->buffer_count; i++)
1266 exec_list[i].offset = exec2_list[i].offset;
1267 /* ... and back out to userspace */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001268 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001269 exec_list,
1270 sizeof(*exec_list) * args->buffer_count);
1271 if (ret) {
1272 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001273 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001274 "back to user (%d)\n",
1275 args->buffer_count, ret);
1276 }
1277 }
1278
1279 drm_free_large(exec_list);
1280 drm_free_large(exec2_list);
1281 return ret;
1282}
1283
1284int
1285i915_gem_execbuffer2(struct drm_device *dev, void *data,
1286 struct drm_file *file)
1287{
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001288 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001289 struct drm_i915_gem_execbuffer2 *args = data;
1290 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1291 int ret;
1292
Xi Wanged8cd3b2012-04-23 04:06:41 -04001293 if (args->buffer_count < 1 ||
1294 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001295 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001296 return -EINVAL;
1297 }
1298
Chris Wilson8408c282011-02-21 12:54:48 +00001299 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001300 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001301 if (exec2_list == NULL)
1302 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1303 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001304 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001305 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001306 args->buffer_count);
1307 return -ENOMEM;
1308 }
1309 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001310 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001311 sizeof(*exec2_list) * args->buffer_count);
1312 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001313 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001314 args->buffer_count, ret);
1315 drm_free_large(exec2_list);
1316 return -EFAULT;
1317 }
1318
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001319 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list,
1320 &dev_priv->gtt.base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001321 if (!ret) {
1322 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001323 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001324 exec2_list,
1325 sizeof(*exec2_list) * args->buffer_count);
1326 if (ret) {
1327 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001328 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001329 "back to user (%d)\n",
1330 args->buffer_count, ret);
1331 }
1332 }
1333
1334 drm_free_large(exec2_list);
1335 return ret;
1336}