blob: 5dcfd4f59f87c7b1e287e02a8799eac1da9bbd80 [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
126 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
127 if (IS_ERR(vma)) {
128 /* XXX: We don't need an error path fro vma because if
129 * the vma was created just for this execbuf, object
130 * unreference should kill it off.*/
131 DRM_DEBUG("Failed to lookup VMA\n");
132 ret = PTR_ERR(vma);
133 goto out;
134 }
135
136 list_add_tail(&vma->exec_list, &eb->vmas);
137
138 vma->exec_entry = &exec[i];
139 if (eb->and < 0) {
140 eb->lut[i] = vma;
141 } else {
142 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
143 vma->exec_handle = handle;
144 hlist_add_head(&vma->exec_node,
145 &eb->buckets[handle & eb->and]);
146 }
147 ++i;
148 }
149
150
151out:
152 while (!list_empty(&objects)) {
153 obj = list_first_entry(&objects,
154 struct drm_i915_gem_object,
155 obj_exec_link);
156 list_del_init(&obj->obj_exec_link);
157 if (ret)
158 drm_gem_object_unreference(&obj->base);
159 }
160 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000161}
162
Ben Widawsky27173f12013-08-14 11:38:36 +0200163static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000164{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000165 if (eb->and < 0) {
166 if (handle >= -eb->and)
167 return NULL;
168 return eb->lut[handle];
169 } else {
170 struct hlist_head *head;
171 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000172
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000173 head = &eb->buckets[handle & eb->and];
174 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200175 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000176
Ben Widawsky27173f12013-08-14 11:38:36 +0200177 vma = hlist_entry(node, struct i915_vma, exec_node);
178 if (vma->exec_handle == handle)
179 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000180 }
181 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000182 }
Chris Wilson67731b82010-12-08 10:38:14 +0000183}
184
Ben Widawsky27173f12013-08-14 11:38:36 +0200185static void eb_destroy(struct eb_vmas *eb) {
186 while (!list_empty(&eb->vmas)) {
187 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000188
Ben Widawsky27173f12013-08-14 11:38:36 +0200189 vma = list_first_entry(&eb->vmas,
190 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000191 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200192 list_del_init(&vma->exec_list);
193 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000194 }
Chris Wilson67731b82010-12-08 10:38:14 +0000195 kfree(eb);
196}
197
Chris Wilsondabdfe02012-03-26 10:10:27 +0200198static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
199{
200 return (obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100201 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200202 obj->cache_level != I915_CACHE_NONE);
203}
204
Chris Wilson54cf91d2010-11-25 18:00:26 +0000205static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100206relocate_entry_cpu(struct drm_i915_gem_object *obj,
207 struct drm_i915_gem_relocation_entry *reloc)
208{
209 uint32_t page_offset = offset_in_page(reloc->offset);
210 char *vaddr;
211 int ret = -EINVAL;
212
213 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
214 if (ret)
215 return ret;
216
217 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
218 reloc->offset >> PAGE_SHIFT));
219 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
220 kunmap_atomic(vaddr);
221
222 return 0;
223}
224
225static int
226relocate_entry_gtt(struct drm_i915_gem_object *obj,
227 struct drm_i915_gem_relocation_entry *reloc)
228{
229 struct drm_device *dev = obj->base.dev;
230 struct drm_i915_private *dev_priv = dev->dev_private;
231 uint32_t __iomem *reloc_entry;
232 void __iomem *reloc_page;
233 int ret = -EINVAL;
234
235 ret = i915_gem_object_set_to_gtt_domain(obj, true);
236 if (ret)
237 return ret;
238
239 ret = i915_gem_object_put_fence(obj);
240 if (ret)
241 return ret;
242
243 /* Map the page containing the relocation we're going to perform. */
244 reloc->offset += i915_gem_obj_ggtt_offset(obj);
245 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
246 reloc->offset & PAGE_MASK);
247 reloc_entry = (uint32_t __iomem *)
248 (reloc_page + offset_in_page(reloc->offset));
249 iowrite32(reloc->delta, reloc_entry);
250 io_mapping_unmap_atomic(reloc_page);
251
252 return 0;
253}
254
255static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000256i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200257 struct eb_vmas *eb,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700258 struct drm_i915_gem_relocation_entry *reloc,
259 struct i915_address_space *vm)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000260{
261 struct drm_device *dev = obj->base.dev;
262 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100263 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200264 struct i915_vma *target_vma;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000265 uint32_t target_offset;
266 int ret = -EINVAL;
267
Chris Wilson67731b82010-12-08 10:38:14 +0000268 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200269 target_vma = eb_get_vma(eb, reloc->target_handle);
270 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000271 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200272 target_i915_obj = target_vma->obj;
273 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000274
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700275 target_offset = i915_gem_obj_ggtt_offset(target_i915_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000276
Eric Anholte844b992012-07-31 15:35:01 -0700277 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
278 * pipe_control writes because the gpu doesn't properly redirect them
279 * through the ppgtt for non_secure batchbuffers. */
280 if (unlikely(IS_GEN6(dev) &&
281 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
282 !target_i915_obj->has_global_gtt_mapping)) {
283 i915_gem_gtt_bind_object(target_i915_obj,
284 target_i915_obj->cache_level);
285 }
286
Chris Wilson54cf91d2010-11-25 18:00:26 +0000287 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000288 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100289 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000290 "obj %p target %d offset %d "
291 "read %08x write %08x",
292 obj, reloc->target_handle,
293 (int) reloc->offset,
294 reloc->read_domains,
295 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000296 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000297 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100298 if (unlikely((reloc->write_domain | reloc->read_domains)
299 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100300 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000301 "obj %p target %d offset %d "
302 "read %08x write %08x",
303 obj, reloc->target_handle,
304 (int) reloc->offset,
305 reloc->read_domains,
306 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000307 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000308 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000309
310 target_obj->pending_read_domains |= reloc->read_domains;
311 target_obj->pending_write_domain |= reloc->write_domain;
312
313 /* If the relocation already has the right value in it, no
314 * more work needs to be done.
315 */
316 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000317 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000318
319 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000320 if (unlikely(reloc->offset > obj->base.size - 4)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100321 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000322 "obj %p target %d offset %d size %d.\n",
323 obj, reloc->target_handle,
324 (int) reloc->offset,
325 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000326 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000327 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000328 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100329 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000330 "obj %p target %d offset %d.\n",
331 obj, reloc->target_handle,
332 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000333 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000334 }
335
Chris Wilsondabdfe02012-03-26 10:10:27 +0200336 /* We can't wait for rendering with pagefaults disabled */
337 if (obj->active && in_atomic())
338 return -EFAULT;
339
Chris Wilson54cf91d2010-11-25 18:00:26 +0000340 reloc->delta += target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100341 if (use_cpu_reloc(obj))
342 ret = relocate_entry_cpu(obj, reloc);
343 else
344 ret = relocate_entry_gtt(obj, reloc);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000345
Daniel Vetterd4d36012013-09-02 20:56:23 +0200346 if (ret)
347 return ret;
348
Chris Wilson54cf91d2010-11-25 18:00:26 +0000349 /* and update the user's relocation entry */
350 reloc->presumed_offset = target_offset;
351
Chris Wilson67731b82010-12-08 10:38:14 +0000352 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000353}
354
355static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200356i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
357 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000358{
Chris Wilson1d83f442012-03-24 20:12:53 +0000359#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
360 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000361 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200362 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000363 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000364
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200365 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000366
Chris Wilson1d83f442012-03-24 20:12:53 +0000367 remain = entry->relocation_count;
368 while (remain) {
369 struct drm_i915_gem_relocation_entry *r = stack_reloc;
370 int count = remain;
371 if (count > ARRAY_SIZE(stack_reloc))
372 count = ARRAY_SIZE(stack_reloc);
373 remain -= count;
374
375 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000376 return -EFAULT;
377
Chris Wilson1d83f442012-03-24 20:12:53 +0000378 do {
379 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000380
Ben Widawsky27173f12013-08-14 11:38:36 +0200381 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r,
382 vma->vm);
Chris Wilson1d83f442012-03-24 20:12:53 +0000383 if (ret)
384 return ret;
385
386 if (r->presumed_offset != offset &&
387 __copy_to_user_inatomic(&user_relocs->presumed_offset,
388 &r->presumed_offset,
389 sizeof(r->presumed_offset))) {
390 return -EFAULT;
391 }
392
393 user_relocs++;
394 r++;
395 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000396 }
397
398 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000399#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000400}
401
402static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200403i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
404 struct eb_vmas *eb,
405 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000406{
Ben Widawsky27173f12013-08-14 11:38:36 +0200407 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000408 int i, ret;
409
410 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200411 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i],
412 vma->vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000413 if (ret)
414 return ret;
415 }
416
417 return 0;
418}
419
420static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200421i915_gem_execbuffer_relocate(struct eb_vmas *eb,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700422 struct i915_address_space *vm)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000423{
Ben Widawsky27173f12013-08-14 11:38:36 +0200424 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000425 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000426
Chris Wilsond4aeee72011-03-14 15:11:24 +0000427 /* This is the fast path and we cannot handle a pagefault whilst
428 * holding the struct mutex lest the user pass in the relocations
429 * contained within a mmaped bo. For in such a case we, the page
430 * fault handler would call i915_gem_fault() and we would try to
431 * acquire the struct mutex again. Obviously this is bad and so
432 * lockdep complains vehemently.
433 */
434 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200435 list_for_each_entry(vma, &eb->vmas, exec_list) {
436 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000437 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000438 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000439 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000440 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000441
Chris Wilsond4aeee72011-03-14 15:11:24 +0000442 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000443}
444
Chris Wilson7788a762012-08-24 19:18:18 +0100445#define __EXEC_OBJECT_HAS_PIN (1<<31)
446#define __EXEC_OBJECT_HAS_FENCE (1<<30)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100447
448static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200449need_reloc_mappable(struct i915_vma *vma)
Chris Wilsondabdfe02012-03-26 10:10:27 +0200450{
Ben Widawsky27173f12013-08-14 11:38:36 +0200451 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
452 return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
453 i915_is_ggtt(vma->vm);
Chris Wilsondabdfe02012-03-26 10:10:27 +0200454}
455
456static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200457i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
458 struct intel_ring_buffer *ring,
459 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100460{
Ben Widawsky27173f12013-08-14 11:38:36 +0200461 struct drm_i915_private *dev_priv = ring->dev->dev_private;
462 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100463 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
464 bool need_fence, need_mappable;
Ben Widawsky27173f12013-08-14 11:38:36 +0200465 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100466 int ret;
467
468 need_fence =
469 has_fenced_gpu_access &&
470 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
471 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200472 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100473
Ben Widawsky27173f12013-08-14 11:38:36 +0200474 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, need_mappable,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700475 false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100476 if (ret)
477 return ret;
478
Chris Wilson7788a762012-08-24 19:18:18 +0100479 entry->flags |= __EXEC_OBJECT_HAS_PIN;
480
Chris Wilson1690e1e2011-12-14 13:57:08 +0100481 if (has_fenced_gpu_access) {
482 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100483 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000484 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100485 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100486
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000487 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100488 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000489
Chris Wilson7dd49062012-03-21 10:48:18 +0000490 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100491 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100492 }
493
Chris Wilson7788a762012-08-24 19:18:18 +0100494 /* Ensure ppgtt mapping exists if needed */
495 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
496 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
497 obj, obj->cache_level);
498
499 obj->has_aliasing_ppgtt_mapping = 1;
500 }
501
Ben Widawsky27173f12013-08-14 11:38:36 +0200502 if (entry->offset != vma->node.start) {
503 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100504 *need_reloc = true;
505 }
506
507 if (entry->flags & EXEC_OBJECT_WRITE) {
508 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
509 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
510 }
511
512 if (entry->flags & EXEC_OBJECT_NEEDS_GTT &&
513 !obj->has_global_gtt_mapping)
514 i915_gem_gtt_bind_object(obj, obj->cache_level);
515
Chris Wilson1690e1e2011-12-14 13:57:08 +0100516 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100517}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100518
Chris Wilson7788a762012-08-24 19:18:18 +0100519static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200520i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
Chris Wilson7788a762012-08-24 19:18:18 +0100521{
522 struct drm_i915_gem_exec_object2 *entry;
Ben Widawsky27173f12013-08-14 11:38:36 +0200523 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson7788a762012-08-24 19:18:18 +0100524
Ben Widawsky27173f12013-08-14 11:38:36 +0200525 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson7788a762012-08-24 19:18:18 +0100526 return;
527
Ben Widawsky27173f12013-08-14 11:38:36 +0200528 entry = vma->exec_entry;
Chris Wilson7788a762012-08-24 19:18:18 +0100529
530 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
531 i915_gem_object_unpin_fence(obj);
532
533 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
534 i915_gem_object_unpin(obj);
535
536 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100537}
538
Chris Wilson54cf91d2010-11-25 18:00:26 +0000539static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000540i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200541 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100542 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000543{
Chris Wilson432e58e2010-11-25 19:32:06 +0000544 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200545 struct i915_vma *vma;
546 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100547 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
548 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000549
Ben Widawsky27173f12013-08-14 11:38:36 +0200550 INIT_LIST_HEAD(&ordered_vmas);
551 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000552 struct drm_i915_gem_exec_object2 *entry;
553 bool need_fence, need_mappable;
554
Ben Widawsky27173f12013-08-14 11:38:36 +0200555 vma = list_first_entry(vmas, struct i915_vma, exec_list);
556 obj = vma->obj;
557 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000558
559 need_fence =
560 has_fenced_gpu_access &&
561 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
562 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200563 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000564
565 if (need_mappable)
Ben Widawsky27173f12013-08-14 11:38:36 +0200566 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000567 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200568 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000569
Daniel Vettered5982e2013-01-17 22:23:36 +0100570 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000571 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100572 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000573 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200574 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000575
576 /* Attempt to pin all of the buffers into the GTT.
577 * This is done in 3 phases:
578 *
579 * 1a. Unbind all objects that do not match the GTT constraints for
580 * the execbuffer (fenceable, mappable, alignment etc).
581 * 1b. Increment pin count for already bound objects.
582 * 2. Bind new objects.
583 * 3. Decrement pin count.
584 *
Chris Wilson7788a762012-08-24 19:18:18 +0100585 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000586 * room for the earlier objects *unless* we need to defragment.
587 */
588 retry = 0;
589 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100590 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000591
592 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200593 list_for_each_entry(vma, vmas, exec_list) {
594 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000595 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100596
Ben Widawsky27173f12013-08-14 11:38:36 +0200597 obj = vma->obj;
598
599 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000600 continue;
601
602 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000603 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000604 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
605 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200606 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000607
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700608 WARN_ON((need_mappable || need_fence) &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200609 !i915_is_ggtt(vma->vm));
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700610
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700611 if ((entry->alignment &&
Ben Widawsky27173f12013-08-14 11:38:36 +0200612 vma->node.start & (entry->alignment - 1)) ||
Chris Wilson54cf91d2010-11-25 18:00:26 +0000613 (need_mappable && !obj->map_and_fenceable))
Ben Widawsky27173f12013-08-14 11:38:36 +0200614 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000615 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200616 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000617 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000618 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000619 }
620
621 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200622 list_for_each_entry(vma, vmas, exec_list) {
623 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100624 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000625
Ben Widawsky27173f12013-08-14 11:38:36 +0200626 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100627 if (ret)
628 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000629 }
630
Chris Wilson7788a762012-08-24 19:18:18 +0100631err: /* Decrement pin count for bound objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200632 list_for_each_entry(vma, vmas, exec_list)
633 i915_gem_execbuffer_unreserve_vma(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000634
Chris Wilson6c085a72012-08-20 11:40:46 +0200635 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000636 return ret;
637
Chris Wilson6c085a72012-08-20 11:40:46 +0200638 ret = i915_gem_evict_everything(ring->dev);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000639 if (ret)
640 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000641 } while (1);
642}
643
644static int
645i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100646 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000647 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000648 struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200649 struct eb_vmas *eb,
650 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000651{
652 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200653 struct i915_address_space *vm;
654 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100655 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000656 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000657 int i, total, ret;
Daniel Vettered5982e2013-01-17 22:23:36 +0100658 int count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000659
Ben Widawsky27173f12013-08-14 11:38:36 +0200660 if (WARN_ON(list_empty(&eb->vmas)))
661 return 0;
662
663 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
664
Chris Wilson67731b82010-12-08 10:38:14 +0000665 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200666 while (!list_empty(&eb->vmas)) {
667 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
668 list_del_init(&vma->exec_list);
669 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000670 }
671
Chris Wilson54cf91d2010-11-25 18:00:26 +0000672 mutex_unlock(&dev->struct_mutex);
673
674 total = 0;
675 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000676 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000677
Chris Wilsondd6864a2011-01-12 23:49:13 +0000678 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000679 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000680 if (reloc == NULL || reloc_offset == NULL) {
681 drm_free_large(reloc);
682 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000683 mutex_lock(&dev->struct_mutex);
684 return -ENOMEM;
685 }
686
687 total = 0;
688 for (i = 0; i < count; i++) {
689 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000690 u64 invalid_offset = (u64)-1;
691 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000692
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200693 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000694
695 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000696 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000697 ret = -EFAULT;
698 mutex_lock(&dev->struct_mutex);
699 goto err;
700 }
701
Chris Wilson262b6d32013-01-15 16:17:54 +0000702 /* As we do not update the known relocation offsets after
703 * relocating (due to the complexities in lock handling),
704 * we need to mark them as invalid now so that we force the
705 * relocation processing next time. Just in case the target
706 * object is evicted and then rebound into its old
707 * presumed_offset before the next execbuffer - if that
708 * happened we would make the mistake of assuming that the
709 * relocations were valid.
710 */
711 for (j = 0; j < exec[i].relocation_count; j++) {
712 if (copy_to_user(&user_relocs[j].presumed_offset,
713 &invalid_offset,
714 sizeof(invalid_offset))) {
715 ret = -EFAULT;
716 mutex_lock(&dev->struct_mutex);
717 goto err;
718 }
719 }
720
Chris Wilsondd6864a2011-01-12 23:49:13 +0000721 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000722 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000723 }
724
725 ret = i915_mutex_lock_interruptible(dev);
726 if (ret) {
727 mutex_lock(&dev->struct_mutex);
728 goto err;
729 }
730
Chris Wilson67731b82010-12-08 10:38:14 +0000731 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000732 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200733 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000734 if (ret)
735 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000736
Daniel Vettered5982e2013-01-17 22:23:36 +0100737 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200738 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000739 if (ret)
740 goto err;
741
Ben Widawsky27173f12013-08-14 11:38:36 +0200742 list_for_each_entry(vma, &eb->vmas, exec_list) {
743 int offset = vma->exec_entry - exec;
744 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
745 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000746 if (ret)
747 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000748 }
749
750 /* Leave the user relocations as are, this is the painfully slow path,
751 * and we want to avoid the complication of dropping the lock whilst
752 * having buffers reserved in the aperture and so causing spurious
753 * ENOSPC for random operations.
754 */
755
756err:
757 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000758 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000759 return ret;
760}
761
Chris Wilson54cf91d2010-11-25 18:00:26 +0000762static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000763i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200764 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000765{
Ben Widawsky27173f12013-08-14 11:38:36 +0200766 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200767 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100768 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000769 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000770
Ben Widawsky27173f12013-08-14 11:38:36 +0200771 list_for_each_entry(vma, vmas, exec_list) {
772 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700773 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000774 if (ret)
775 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200776
777 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100778 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200779
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200780 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000781 }
782
Chris Wilson000433b2013-08-08 14:41:09 +0100783 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800784 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200785
786 if (flush_domains & I915_GEM_DOMAIN_GTT)
787 wmb();
788
Chris Wilson09cf7c92012-07-13 14:14:08 +0100789 /* Unconditionally invalidate gpu caches and ensure that we do flush
790 * any residual writes from the previous batch.
791 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100792 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000793}
794
Chris Wilson432e58e2010-11-25 19:32:06 +0000795static bool
796i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000797{
Daniel Vettered5982e2013-01-17 22:23:36 +0100798 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
799 return false;
800
Chris Wilson432e58e2010-11-25 19:32:06 +0000801 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000802}
803
804static int
805validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
806 int count)
807{
808 int i;
Kees Cook3118a4f2013-03-11 17:31:45 -0700809 int relocs_total = 0;
810 int relocs_max = INT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000811
812 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200813 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000814 int length; /* limited by fault_in_pages_readable() */
815
Daniel Vettered5982e2013-01-17 22:23:36 +0100816 if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
817 return -EINVAL;
818
Kees Cook3118a4f2013-03-11 17:31:45 -0700819 /* First check for malicious input causing overflow in
820 * the worst case where we need to allocate the entire
821 * relocation tree as a single array.
822 */
823 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000824 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700825 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000826
827 length = exec[i].relocation_count *
828 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700829 /*
830 * We must check that the entire relocation array is safe
831 * to read, but since we may need to update the presumed
832 * offsets during execution, check for full write access.
833 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000834 if (!access_ok(VERIFY_WRITE, ptr, length))
835 return -EFAULT;
836
Xiong Zhang0b74b502013-07-19 13:51:24 +0800837 if (likely(!i915_prefault_disable)) {
838 if (fault_in_multipages_readable(ptr, length))
839 return -EFAULT;
840 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000841 }
842
843 return 0;
844}
845
Chris Wilson432e58e2010-11-25 19:32:06 +0000846static void
Ben Widawsky27173f12013-08-14 11:38:36 +0200847i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Chris Wilson9d7730912012-11-27 16:22:52 +0000848 struct intel_ring_buffer *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000849{
Ben Widawsky27173f12013-08-14 11:38:36 +0200850 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000851
Ben Widawsky27173f12013-08-14 11:38:36 +0200852 list_for_each_entry(vma, vmas, exec_list) {
853 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100854 u32 old_read = obj->base.read_domains;
855 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000856
Chris Wilson432e58e2010-11-25 19:32:06 +0000857 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100858 if (obj->base.write_domain == 0)
859 obj->base.pending_read_domains |= obj->base.read_domains;
860 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000861 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
862
Ben Widawsky27173f12013-08-14 11:38:36 +0200863 list_move_tail(&vma->mm_list, &vma->vm->active_list);
Chris Wilson9d7730912012-11-27 16:22:52 +0000864 i915_gem_object_move_to_active(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000865 if (obj->base.write_domain) {
866 obj->dirty = 1;
Chris Wilson9d7730912012-11-27 16:22:52 +0000867 obj->last_write_seqno = intel_ring_get_seqno(ring);
Chris Wilsonacb87df2012-05-03 15:47:57 +0100868 if (obj->pin_count) /* check for potential scanout */
Chris Wilsonc65355b2013-06-06 16:53:41 -0300869 intel_mark_fb_busy(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000870 }
871
Chris Wilsondb53a302011-02-03 11:57:46 +0000872 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000873 }
874}
875
Chris Wilson54cf91d2010-11-25 18:00:26 +0000876static void
877i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000878 struct drm_file *file,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300879 struct intel_ring_buffer *ring,
880 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000881{
Daniel Vettercc889e02012-06-13 20:45:19 +0200882 /* Unconditionally force add_request to emit a full flush. */
883 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000884
Chris Wilson432e58e2010-11-25 19:32:06 +0000885 /* Add a breadcrumb for the completion of the batch buffer */
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300886 (void)__i915_add_request(ring, file, obj, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000887}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000888
889static int
Eric Anholtae662d32012-01-03 09:23:29 -0800890i915_reset_gen7_sol_offsets(struct drm_device *dev,
891 struct intel_ring_buffer *ring)
892{
893 drm_i915_private_t *dev_priv = dev->dev_private;
894 int ret, i;
895
896 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
897 return 0;
898
899 ret = intel_ring_begin(ring, 4 * 3);
900 if (ret)
901 return ret;
902
903 for (i = 0; i < 4; i++) {
904 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
905 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
906 intel_ring_emit(ring, 0);
907 }
908
909 intel_ring_advance(ring);
910
911 return 0;
912}
913
914static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000915i915_gem_do_execbuffer(struct drm_device *dev, void *data,
916 struct drm_file *file,
917 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky28d6a7b2013-07-31 17:00:02 -0700918 struct drm_i915_gem_exec_object2 *exec,
919 struct i915_address_space *vm)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000920{
921 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +0200922 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000923 struct drm_i915_gem_object *batch_obj;
924 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000925 struct intel_ring_buffer *ring;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700926 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000927 u32 exec_start, exec_len;
Daniel Vettered5982e2013-01-17 22:23:36 +0100928 u32 mask, flags;
Chris Wilson72bfa192010-12-19 11:42:05 +0000929 int ret, mode, i;
Daniel Vettered5982e2013-01-17 22:23:36 +0100930 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000931
Daniel Vettered5982e2013-01-17 22:23:36 +0100932 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +0000933 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +0000934
935 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000936 if (ret)
937 return ret;
938
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100939 flags = 0;
940 if (args->flags & I915_EXEC_SECURE) {
941 if (!file->is_master || !capable(CAP_SYS_ADMIN))
942 return -EPERM;
943
944 flags |= I915_DISPATCH_SECURE;
945 }
Daniel Vetterb45305f2012-12-17 16:21:27 +0100946 if (args->flags & I915_EXEC_IS_PINNED)
947 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100948
Chris Wilson54cf91d2010-11-25 18:00:26 +0000949 switch (args->flags & I915_EXEC_RING_MASK) {
950 case I915_EXEC_DEFAULT:
951 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000952 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000953 break;
954 case I915_EXEC_BSD:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000955 ring = &dev_priv->ring[VCS];
Chris Wilsone8520962013-07-03 17:22:07 +0300956 if (ctx_id != DEFAULT_CONTEXT_ID) {
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700957 DRM_DEBUG("Ring %s doesn't support contexts\n",
958 ring->name);
959 return -EPERM;
960 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000961 break;
962 case I915_EXEC_BLT:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000963 ring = &dev_priv->ring[BCS];
Chris Wilsone8520962013-07-03 17:22:07 +0300964 if (ctx_id != DEFAULT_CONTEXT_ID) {
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700965 DRM_DEBUG("Ring %s doesn't support contexts\n",
966 ring->name);
967 return -EPERM;
968 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000969 break;
Xiang, Haihao82f91b62013-05-28 19:22:33 -0700970 case I915_EXEC_VEBOX:
971 ring = &dev_priv->ring[VECS];
Chris Wilsone8520962013-07-03 17:22:07 +0300972 if (ctx_id != DEFAULT_CONTEXT_ID) {
Xiang, Haihao82f91b62013-05-28 19:22:33 -0700973 DRM_DEBUG("Ring %s doesn't support contexts\n",
974 ring->name);
975 return -EPERM;
976 }
977 break;
978
Chris Wilson54cf91d2010-11-25 18:00:26 +0000979 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100980 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000981 (int)(args->flags & I915_EXEC_RING_MASK));
982 return -EINVAL;
983 }
Chris Wilsona15817c2012-05-11 14:29:31 +0100984 if (!intel_ring_initialized(ring)) {
985 DRM_DEBUG("execbuf with invalid ring: %d\n",
986 (int)(args->flags & I915_EXEC_RING_MASK));
987 return -EINVAL;
988 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000989
Chris Wilson72bfa192010-12-19 11:42:05 +0000990 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800991 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +0000992 switch (mode) {
993 case I915_EXEC_CONSTANTS_REL_GENERAL:
994 case I915_EXEC_CONSTANTS_ABSOLUTE:
995 case I915_EXEC_CONSTANTS_REL_SURFACE:
996 if (ring == &dev_priv->ring[RCS] &&
997 mode != dev_priv->relative_constants_mode) {
998 if (INTEL_INFO(dev)->gen < 4)
999 return -EINVAL;
1000
1001 if (INTEL_INFO(dev)->gen > 5 &&
1002 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1003 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001004
1005 /* The HW changed the meaning on this bit on gen6 */
1006 if (INTEL_INFO(dev)->gen >= 6)
1007 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +00001008 }
1009 break;
1010 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001011 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +00001012 return -EINVAL;
1013 }
1014
Chris Wilson54cf91d2010-11-25 18:00:26 +00001015 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001016 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001017 return -EINVAL;
1018 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001019
1020 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001021 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +01001022 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001023 return -EINVAL;
1024 }
1025
Daniel Vetter6ebebc92012-04-26 23:28:11 +02001026 if (INTEL_INFO(dev)->gen >= 5) {
1027 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1028 return -EINVAL;
1029 }
1030
Xi Wang44afb3a2012-04-23 04:06:42 -04001031 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1032 DRM_DEBUG("execbuf with %u cliprects\n",
1033 args->num_cliprects);
1034 return -EINVAL;
1035 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001036
Chris Wilson432e58e2010-11-25 19:32:06 +00001037 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001038 GFP_KERNEL);
1039 if (cliprects == NULL) {
1040 ret = -ENOMEM;
1041 goto pre_mutex_err;
1042 }
1043
Chris Wilson432e58e2010-11-25 19:32:06 +00001044 if (copy_from_user(cliprects,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001045 to_user_ptr(args->cliprects_ptr),
1046 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001047 ret = -EFAULT;
1048 goto pre_mutex_err;
1049 }
1050 }
1051
Chris Wilson54cf91d2010-11-25 18:00:26 +00001052 ret = i915_mutex_lock_interruptible(dev);
1053 if (ret)
1054 goto pre_mutex_err;
1055
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001056 if (dev_priv->ums.mm_suspended) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001057 mutex_unlock(&dev->struct_mutex);
1058 ret = -EBUSY;
1059 goto pre_mutex_err;
1060 }
1061
Ben Widawsky27173f12013-08-14 11:38:36 +02001062 eb = eb_create(args, vm);
Chris Wilson67731b82010-12-08 10:38:14 +00001063 if (eb == NULL) {
1064 mutex_unlock(&dev->struct_mutex);
1065 ret = -ENOMEM;
1066 goto pre_mutex_err;
1067 }
1068
Chris Wilson54cf91d2010-11-25 18:00:26 +00001069 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001070 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001071 if (ret)
1072 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001073
Chris Wilson6fe4f142011-01-10 17:35:37 +00001074 /* take note of the batch buffer before we might reorder the lists */
Ben Widawsky27173f12013-08-14 11:38:36 +02001075 batch_obj = list_entry(eb->vmas.prev, struct i915_vma, exec_list)->obj;
Chris Wilson6fe4f142011-01-10 17:35:37 +00001076
Chris Wilson54cf91d2010-11-25 18:00:26 +00001077 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001078 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001079 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001080 if (ret)
1081 goto err;
1082
1083 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001084 if (need_relocs)
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001085 ret = i915_gem_execbuffer_relocate(eb, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001086 if (ret) {
1087 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001088 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001089 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001090 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1091 }
1092 if (ret)
1093 goto err;
1094 }
1095
1096 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001097 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001098 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001099 ret = -EINVAL;
1100 goto err;
1101 }
1102 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1103
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001104 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1105 * batch" bit. Hence we need to pin secure batches into the global gtt.
1106 * hsw should have this fixed, but let's be paranoid and do it
1107 * unconditionally for now. */
1108 if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
1109 i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
1110
Ben Widawsky27173f12013-08-14 11:38:36 +02001111 ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001112 if (ret)
1113 goto err;
1114
Eric Anholt0da5cec2012-07-23 12:33:55 -07001115 ret = i915_switch_context(ring, file, ctx_id);
1116 if (ret)
1117 goto err;
1118
Ben Widawskye2971bd2011-12-12 19:21:57 -08001119 if (ring == &dev_priv->ring[RCS] &&
1120 mode != dev_priv->relative_constants_mode) {
1121 ret = intel_ring_begin(ring, 4);
1122 if (ret)
1123 goto err;
1124
1125 intel_ring_emit(ring, MI_NOOP);
1126 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1127 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001128 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001129 intel_ring_advance(ring);
1130
1131 dev_priv->relative_constants_mode = mode;
1132 }
1133
Eric Anholtae662d32012-01-03 09:23:29 -08001134 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1135 ret = i915_reset_gen7_sol_offsets(dev, ring);
1136 if (ret)
1137 goto err;
1138 }
1139
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001140 exec_start = i915_gem_obj_offset(batch_obj, vm) +
1141 args->batch_start_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001142 exec_len = args->batch_len;
1143 if (cliprects) {
1144 for (i = 0; i < args->num_cliprects; i++) {
1145 ret = i915_emit_box(dev, &cliprects[i],
1146 args->DR1, args->DR4);
1147 if (ret)
1148 goto err;
1149
1150 ret = ring->dispatch_execbuffer(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001151 exec_start, exec_len,
1152 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001153 if (ret)
1154 goto err;
1155 }
1156 } else {
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001157 ret = ring->dispatch_execbuffer(ring,
1158 exec_start, exec_len,
1159 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001160 if (ret)
1161 goto err;
1162 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001163
Chris Wilson9d7730912012-11-27 16:22:52 +00001164 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1165
Ben Widawsky27173f12013-08-14 11:38:36 +02001166 i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
Mika Kuoppala7d736f42013-06-12 15:01:39 +03001167 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001168
1169err:
Chris Wilson67731b82010-12-08 10:38:14 +00001170 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001171
1172 mutex_unlock(&dev->struct_mutex);
1173
1174pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001175 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001176 return ret;
1177}
1178
1179/*
1180 * Legacy execbuffer just creates an exec2 list from the original exec object
1181 * list array and passes it to the real function.
1182 */
1183int
1184i915_gem_execbuffer(struct drm_device *dev, void *data,
1185 struct drm_file *file)
1186{
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001187 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001188 struct drm_i915_gem_execbuffer *args = data;
1189 struct drm_i915_gem_execbuffer2 exec2;
1190 struct drm_i915_gem_exec_object *exec_list = NULL;
1191 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1192 int ret, i;
1193
Chris Wilson54cf91d2010-11-25 18:00:26 +00001194 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001195 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001196 return -EINVAL;
1197 }
1198
1199 /* Copy in the exec list from userland */
1200 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1201 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1202 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001203 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001204 args->buffer_count);
1205 drm_free_large(exec_list);
1206 drm_free_large(exec2_list);
1207 return -ENOMEM;
1208 }
1209 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001210 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001211 sizeof(*exec_list) * args->buffer_count);
1212 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001213 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001214 args->buffer_count, ret);
1215 drm_free_large(exec_list);
1216 drm_free_large(exec2_list);
1217 return -EFAULT;
1218 }
1219
1220 for (i = 0; i < args->buffer_count; i++) {
1221 exec2_list[i].handle = exec_list[i].handle;
1222 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1223 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1224 exec2_list[i].alignment = exec_list[i].alignment;
1225 exec2_list[i].offset = exec_list[i].offset;
1226 if (INTEL_INFO(dev)->gen < 4)
1227 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1228 else
1229 exec2_list[i].flags = 0;
1230 }
1231
1232 exec2.buffers_ptr = args->buffers_ptr;
1233 exec2.buffer_count = args->buffer_count;
1234 exec2.batch_start_offset = args->batch_start_offset;
1235 exec2.batch_len = args->batch_len;
1236 exec2.DR1 = args->DR1;
1237 exec2.DR4 = args->DR4;
1238 exec2.num_cliprects = args->num_cliprects;
1239 exec2.cliprects_ptr = args->cliprects_ptr;
1240 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001241 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001242
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001243 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list,
1244 &dev_priv->gtt.base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001245 if (!ret) {
1246 /* Copy the new buffer offsets back to the user's exec list. */
1247 for (i = 0; i < args->buffer_count; i++)
1248 exec_list[i].offset = exec2_list[i].offset;
1249 /* ... and back out to userspace */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001250 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001251 exec_list,
1252 sizeof(*exec_list) * args->buffer_count);
1253 if (ret) {
1254 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001255 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001256 "back to user (%d)\n",
1257 args->buffer_count, ret);
1258 }
1259 }
1260
1261 drm_free_large(exec_list);
1262 drm_free_large(exec2_list);
1263 return ret;
1264}
1265
1266int
1267i915_gem_execbuffer2(struct drm_device *dev, void *data,
1268 struct drm_file *file)
1269{
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001270 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001271 struct drm_i915_gem_execbuffer2 *args = data;
1272 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1273 int ret;
1274
Xi Wanged8cd3b2012-04-23 04:06:41 -04001275 if (args->buffer_count < 1 ||
1276 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001277 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001278 return -EINVAL;
1279 }
1280
Chris Wilson8408c282011-02-21 12:54:48 +00001281 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001282 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001283 if (exec2_list == NULL)
1284 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1285 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001286 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001287 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001288 args->buffer_count);
1289 return -ENOMEM;
1290 }
1291 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001292 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001293 sizeof(*exec2_list) * args->buffer_count);
1294 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001295 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001296 args->buffer_count, ret);
1297 drm_free_large(exec2_list);
1298 return -EFAULT;
1299 }
1300
Ben Widawsky28d6a7b2013-07-31 17:00:02 -07001301 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list,
1302 &dev_priv->gtt.base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001303 if (!ret) {
1304 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001305 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001306 exec2_list,
1307 sizeof(*exec2_list) * args->buffer_count);
1308 if (ret) {
1309 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001310 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001311 "back to user (%d)\n",
1312 args->buffer_count, ret);
1313 }
1314 }
1315
1316 drm_free_large(exec2_list);
1317 return ret;
1318}