drm/i915: Don't look at pg_dirty_rings for aliasing ppgtt
[deliverable/linux.git] / drivers / gpu / drm / i915 / i915_gem_execbuffer.c
CommitLineData
54cf91dc
CW
1/*
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
760285e7
DH
29#include <drm/drmP.h>
30#include <drm/i915_drm.h>
54cf91dc
CW
31#include "i915_drv.h"
32#include "i915_trace.h"
33#include "intel_drv.h"
f45b5557 34#include <linux/dma_remapping.h>
54cf91dc 35
a415d355
CW
36#define __EXEC_OBJECT_HAS_PIN (1<<31)
37#define __EXEC_OBJECT_HAS_FENCE (1<<30)
e6a84468 38#define __EXEC_OBJECT_NEEDS_MAP (1<<29)
d23db88c
CW
39#define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
40
41#define BATCH_OFFSET_BIAS (256*1024)
a415d355 42
27173f1f
BW
43struct eb_vmas {
44 struct list_head vmas;
67731b87 45 int and;
eef90ccb 46 union {
27173f1f 47 struct i915_vma *lut[0];
eef90ccb
CW
48 struct hlist_head buckets[0];
49 };
67731b87
CW
50};
51
27173f1f 52static struct eb_vmas *
17601cbc 53eb_create(struct drm_i915_gem_execbuffer2 *args)
67731b87 54{
27173f1f 55 struct eb_vmas *eb = NULL;
eef90ccb
CW
56
57 if (args->flags & I915_EXEC_HANDLE_LUT) {
b205ca57 58 unsigned size = args->buffer_count;
27173f1f
BW
59 size *= sizeof(struct i915_vma *);
60 size += sizeof(struct eb_vmas);
eef90ccb
CW
61 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
62 }
63
64 if (eb == NULL) {
b205ca57
DV
65 unsigned size = args->buffer_count;
66 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
27b7c63a 67 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
eef90ccb
CW
68 while (count > 2*size)
69 count >>= 1;
70 eb = kzalloc(count*sizeof(struct hlist_head) +
27173f1f 71 sizeof(struct eb_vmas),
eef90ccb
CW
72 GFP_TEMPORARY);
73 if (eb == NULL)
74 return eb;
75
76 eb->and = count - 1;
77 } else
78 eb->and = -args->buffer_count;
79
27173f1f 80 INIT_LIST_HEAD(&eb->vmas);
67731b87
CW
81 return eb;
82}
83
84static void
27173f1f 85eb_reset(struct eb_vmas *eb)
67731b87 86{
eef90ccb
CW
87 if (eb->and >= 0)
88 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
67731b87
CW
89}
90
3b96eff4 91static int
27173f1f
BW
92eb_lookup_vmas(struct eb_vmas *eb,
93 struct drm_i915_gem_exec_object2 *exec,
94 const struct drm_i915_gem_execbuffer2 *args,
95 struct i915_address_space *vm,
96 struct drm_file *file)
3b96eff4 97{
27173f1f
BW
98 struct drm_i915_gem_object *obj;
99 struct list_head objects;
9ae9ab52 100 int i, ret;
3b96eff4 101
27173f1f 102 INIT_LIST_HEAD(&objects);
3b96eff4 103 spin_lock(&file->table_lock);
27173f1f
BW
104 /* Grab a reference to the object and release the lock so we can lookup
105 * or create the VMA without using GFP_ATOMIC */
eef90ccb 106 for (i = 0; i < args->buffer_count; i++) {
3b96eff4
CW
107 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
108 if (obj == NULL) {
109 spin_unlock(&file->table_lock);
110 DRM_DEBUG("Invalid object handle %d at index %d\n",
111 exec[i].handle, i);
27173f1f 112 ret = -ENOENT;
9ae9ab52 113 goto err;
3b96eff4
CW
114 }
115
27173f1f 116 if (!list_empty(&obj->obj_exec_link)) {
3b96eff4
CW
117 spin_unlock(&file->table_lock);
118 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
119 obj, exec[i].handle, i);
27173f1f 120 ret = -EINVAL;
9ae9ab52 121 goto err;
3b96eff4
CW
122 }
123
124 drm_gem_object_reference(&obj->base);
27173f1f
BW
125 list_add_tail(&obj->obj_exec_link, &objects);
126 }
127 spin_unlock(&file->table_lock);
3b96eff4 128
27173f1f 129 i = 0;
9ae9ab52 130 while (!list_empty(&objects)) {
27173f1f 131 struct i915_vma *vma;
6f65e29a 132
9ae9ab52
CW
133 obj = list_first_entry(&objects,
134 struct drm_i915_gem_object,
135 obj_exec_link);
136
e656a6cb
DV
137 /*
138 * NOTE: We can leak any vmas created here when something fails
139 * later on. But that's no issue since vma_unbind can deal with
140 * vmas which are not actually bound. And since only
141 * lookup_or_create exists as an interface to get at the vma
142 * from the (obj, vm) we don't run the risk of creating
143 * duplicated vmas for the same vm.
144 */
da51a1e7 145 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
27173f1f 146 if (IS_ERR(vma)) {
27173f1f
BW
147 DRM_DEBUG("Failed to lookup VMA\n");
148 ret = PTR_ERR(vma);
9ae9ab52 149 goto err;
27173f1f
BW
150 }
151
9ae9ab52 152 /* Transfer ownership from the objects list to the vmas list. */
27173f1f 153 list_add_tail(&vma->exec_list, &eb->vmas);
9ae9ab52 154 list_del_init(&obj->obj_exec_link);
27173f1f
BW
155
156 vma->exec_entry = &exec[i];
eef90ccb 157 if (eb->and < 0) {
27173f1f 158 eb->lut[i] = vma;
eef90ccb
CW
159 } else {
160 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
27173f1f
BW
161 vma->exec_handle = handle;
162 hlist_add_head(&vma->exec_node,
eef90ccb
CW
163 &eb->buckets[handle & eb->and]);
164 }
27173f1f 165 ++i;
3b96eff4 166 }
3b96eff4 167
9ae9ab52 168 return 0;
27173f1f 169
27173f1f 170
9ae9ab52 171err:
27173f1f
BW
172 while (!list_empty(&objects)) {
173 obj = list_first_entry(&objects,
174 struct drm_i915_gem_object,
175 obj_exec_link);
176 list_del_init(&obj->obj_exec_link);
9ae9ab52 177 drm_gem_object_unreference(&obj->base);
27173f1f 178 }
9ae9ab52
CW
179 /*
180 * Objects already transfered to the vmas list will be unreferenced by
181 * eb_destroy.
182 */
183
27173f1f 184 return ret;
3b96eff4
CW
185}
186
27173f1f 187static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
67731b87 188{
eef90ccb
CW
189 if (eb->and < 0) {
190 if (handle >= -eb->and)
191 return NULL;
192 return eb->lut[handle];
193 } else {
194 struct hlist_head *head;
195 struct hlist_node *node;
67731b87 196
eef90ccb
CW
197 head = &eb->buckets[handle & eb->and];
198 hlist_for_each(node, head) {
27173f1f 199 struct i915_vma *vma;
67731b87 200
27173f1f
BW
201 vma = hlist_entry(node, struct i915_vma, exec_node);
202 if (vma->exec_handle == handle)
203 return vma;
eef90ccb
CW
204 }
205 return NULL;
206 }
67731b87
CW
207}
208
a415d355
CW
209static void
210i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
211{
212 struct drm_i915_gem_exec_object2 *entry;
213 struct drm_i915_gem_object *obj = vma->obj;
214
215 if (!drm_mm_node_allocated(&vma->node))
216 return;
217
218 entry = vma->exec_entry;
219
220 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
221 i915_gem_object_unpin_fence(obj);
222
223 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
3d7f0f9d 224 vma->pin_count--;
a415d355 225
de4e783a 226 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
a415d355
CW
227}
228
229static void eb_destroy(struct eb_vmas *eb)
230{
27173f1f
BW
231 while (!list_empty(&eb->vmas)) {
232 struct i915_vma *vma;
bcffc3fa 233
27173f1f
BW
234 vma = list_first_entry(&eb->vmas,
235 struct i915_vma,
bcffc3fa 236 exec_list);
27173f1f 237 list_del_init(&vma->exec_list);
a415d355 238 i915_gem_execbuffer_unreserve_vma(vma);
27173f1f 239 drm_gem_object_unreference(&vma->obj->base);
bcffc3fa 240 }
67731b87
CW
241 kfree(eb);
242}
243
dabdfe02
CW
244static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
245{
2cc86b82
CW
246 return (HAS_LLC(obj->base.dev) ||
247 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
dabdfe02
CW
248 obj->cache_level != I915_CACHE_NONE);
249}
250
5032d871
RB
251static int
252relocate_entry_cpu(struct drm_i915_gem_object *obj,
d9ceb957
BW
253 struct drm_i915_gem_relocation_entry *reloc,
254 uint64_t target_offset)
5032d871 255{
3c94ceee 256 struct drm_device *dev = obj->base.dev;
5032d871 257 uint32_t page_offset = offset_in_page(reloc->offset);
d9ceb957 258 uint64_t delta = reloc->delta + target_offset;
5032d871 259 char *vaddr;
8b78f0e5 260 int ret;
5032d871 261
2cc86b82 262 ret = i915_gem_object_set_to_cpu_domain(obj, true);
5032d871
RB
263 if (ret)
264 return ret;
265
266 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
267 reloc->offset >> PAGE_SHIFT));
d9ceb957 268 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
3c94ceee
BW
269
270 if (INTEL_INFO(dev)->gen >= 8) {
271 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
272
273 if (page_offset == 0) {
274 kunmap_atomic(vaddr);
275 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
276 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
277 }
278
d9ceb957 279 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
3c94ceee
BW
280 }
281
5032d871
RB
282 kunmap_atomic(vaddr);
283
284 return 0;
285}
286
287static int
288relocate_entry_gtt(struct drm_i915_gem_object *obj,
d9ceb957
BW
289 struct drm_i915_gem_relocation_entry *reloc,
290 uint64_t target_offset)
5032d871
RB
291{
292 struct drm_device *dev = obj->base.dev;
293 struct drm_i915_private *dev_priv = dev->dev_private;
d9ceb957 294 uint64_t delta = reloc->delta + target_offset;
906843c3 295 uint64_t offset;
5032d871 296 void __iomem *reloc_page;
8b78f0e5 297 int ret;
5032d871
RB
298
299 ret = i915_gem_object_set_to_gtt_domain(obj, true);
300 if (ret)
301 return ret;
302
303 ret = i915_gem_object_put_fence(obj);
304 if (ret)
305 return ret;
306
307 /* Map the page containing the relocation we're going to perform. */
906843c3
CW
308 offset = i915_gem_obj_ggtt_offset(obj);
309 offset += reloc->offset;
5032d871 310 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
906843c3
CW
311 offset & PAGE_MASK);
312 iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
3c94ceee
BW
313
314 if (INTEL_INFO(dev)->gen >= 8) {
906843c3 315 offset += sizeof(uint32_t);
3c94ceee 316
906843c3 317 if (offset_in_page(offset) == 0) {
3c94ceee 318 io_mapping_unmap_atomic(reloc_page);
906843c3
CW
319 reloc_page =
320 io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
321 offset);
3c94ceee
BW
322 }
323
906843c3
CW
324 iowrite32(upper_32_bits(delta),
325 reloc_page + offset_in_page(offset));
3c94ceee
BW
326 }
327
5032d871
RB
328 io_mapping_unmap_atomic(reloc_page);
329
330 return 0;
331}
332
edf4427b
CW
333static void
334clflush_write32(void *addr, uint32_t value)
335{
336 /* This is not a fast path, so KISS. */
337 drm_clflush_virt_range(addr, sizeof(uint32_t));
338 *(uint32_t *)addr = value;
339 drm_clflush_virt_range(addr, sizeof(uint32_t));
340}
341
342static int
343relocate_entry_clflush(struct drm_i915_gem_object *obj,
344 struct drm_i915_gem_relocation_entry *reloc,
345 uint64_t target_offset)
346{
347 struct drm_device *dev = obj->base.dev;
348 uint32_t page_offset = offset_in_page(reloc->offset);
349 uint64_t delta = (int)reloc->delta + target_offset;
350 char *vaddr;
351 int ret;
352
353 ret = i915_gem_object_set_to_gtt_domain(obj, true);
354 if (ret)
355 return ret;
356
357 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
358 reloc->offset >> PAGE_SHIFT));
359 clflush_write32(vaddr + page_offset, lower_32_bits(delta));
360
361 if (INTEL_INFO(dev)->gen >= 8) {
362 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
363
364 if (page_offset == 0) {
365 kunmap_atomic(vaddr);
366 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
367 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
368 }
369
370 clflush_write32(vaddr + page_offset, upper_32_bits(delta));
371 }
372
373 kunmap_atomic(vaddr);
374
375 return 0;
376}
377
54cf91dc
CW
378static int
379i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
27173f1f 380 struct eb_vmas *eb,
3e7a0322 381 struct drm_i915_gem_relocation_entry *reloc)
54cf91dc
CW
382{
383 struct drm_device *dev = obj->base.dev;
384 struct drm_gem_object *target_obj;
149c8407 385 struct drm_i915_gem_object *target_i915_obj;
27173f1f 386 struct i915_vma *target_vma;
d9ceb957 387 uint64_t target_offset;
8b78f0e5 388 int ret;
54cf91dc 389
67731b87 390 /* we've already hold a reference to all valid objects */
27173f1f
BW
391 target_vma = eb_get_vma(eb, reloc->target_handle);
392 if (unlikely(target_vma == NULL))
54cf91dc 393 return -ENOENT;
27173f1f
BW
394 target_i915_obj = target_vma->obj;
395 target_obj = &target_vma->obj->base;
54cf91dc 396
5ce09725 397 target_offset = target_vma->node.start;
54cf91dc 398
e844b990
EA
399 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
400 * pipe_control writes because the gpu doesn't properly redirect them
401 * through the ppgtt for non_secure batchbuffers. */
402 if (unlikely(IS_GEN6(dev) &&
403 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
fe14d5f4
TU
404 !(target_vma->bound & GLOBAL_BIND))) {
405 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
406 GLOBAL_BIND);
407 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
408 return ret;
409 }
e844b990 410
54cf91dc 411 /* Validate that the target is in a valid r/w GPU domain */
b8f7ab17 412 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
ff240199 413 DRM_DEBUG("reloc with multiple write domains: "
54cf91dc
CW
414 "obj %p target %d offset %d "
415 "read %08x write %08x",
416 obj, reloc->target_handle,
417 (int) reloc->offset,
418 reloc->read_domains,
419 reloc->write_domain);
8b78f0e5 420 return -EINVAL;
54cf91dc 421 }
4ca4a250
DV
422 if (unlikely((reloc->write_domain | reloc->read_domains)
423 & ~I915_GEM_GPU_DOMAINS)) {
ff240199 424 DRM_DEBUG("reloc with read/write non-GPU domains: "
54cf91dc
CW
425 "obj %p target %d offset %d "
426 "read %08x write %08x",
427 obj, reloc->target_handle,
428 (int) reloc->offset,
429 reloc->read_domains,
430 reloc->write_domain);
8b78f0e5 431 return -EINVAL;
54cf91dc 432 }
54cf91dc
CW
433
434 target_obj->pending_read_domains |= reloc->read_domains;
435 target_obj->pending_write_domain |= reloc->write_domain;
436
437 /* If the relocation already has the right value in it, no
438 * more work needs to be done.
439 */
440 if (target_offset == reloc->presumed_offset)
67731b87 441 return 0;
54cf91dc
CW
442
443 /* Check that the relocation address is valid... */
3c94ceee
BW
444 if (unlikely(reloc->offset >
445 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
ff240199 446 DRM_DEBUG("Relocation beyond object bounds: "
54cf91dc
CW
447 "obj %p target %d offset %d size %d.\n",
448 obj, reloc->target_handle,
449 (int) reloc->offset,
450 (int) obj->base.size);
8b78f0e5 451 return -EINVAL;
54cf91dc 452 }
b8f7ab17 453 if (unlikely(reloc->offset & 3)) {
ff240199 454 DRM_DEBUG("Relocation not 4-byte aligned: "
54cf91dc
CW
455 "obj %p target %d offset %d.\n",
456 obj, reloc->target_handle,
457 (int) reloc->offset);
8b78f0e5 458 return -EINVAL;
54cf91dc
CW
459 }
460
dabdfe02
CW
461 /* We can't wait for rendering with pagefaults disabled */
462 if (obj->active && in_atomic())
463 return -EFAULT;
464
5032d871 465 if (use_cpu_reloc(obj))
d9ceb957 466 ret = relocate_entry_cpu(obj, reloc, target_offset);
edf4427b 467 else if (obj->map_and_fenceable)
d9ceb957 468 ret = relocate_entry_gtt(obj, reloc, target_offset);
edf4427b
CW
469 else if (cpu_has_clflush)
470 ret = relocate_entry_clflush(obj, reloc, target_offset);
471 else {
472 WARN_ONCE(1, "Impossible case in relocation handling\n");
473 ret = -ENODEV;
474 }
54cf91dc 475
d4d36014
DV
476 if (ret)
477 return ret;
478
54cf91dc
CW
479 /* and update the user's relocation entry */
480 reloc->presumed_offset = target_offset;
481
67731b87 482 return 0;
54cf91dc
CW
483}
484
485static int
27173f1f
BW
486i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
487 struct eb_vmas *eb)
54cf91dc 488{
1d83f442
CW
489#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
490 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
54cf91dc 491 struct drm_i915_gem_relocation_entry __user *user_relocs;
27173f1f 492 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
1d83f442 493 int remain, ret;
54cf91dc 494
2bb4629a 495 user_relocs = to_user_ptr(entry->relocs_ptr);
54cf91dc 496
1d83f442
CW
497 remain = entry->relocation_count;
498 while (remain) {
499 struct drm_i915_gem_relocation_entry *r = stack_reloc;
500 int count = remain;
501 if (count > ARRAY_SIZE(stack_reloc))
502 count = ARRAY_SIZE(stack_reloc);
503 remain -= count;
504
505 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
54cf91dc
CW
506 return -EFAULT;
507
1d83f442
CW
508 do {
509 u64 offset = r->presumed_offset;
54cf91dc 510
3e7a0322 511 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
1d83f442
CW
512 if (ret)
513 return ret;
514
515 if (r->presumed_offset != offset &&
516 __copy_to_user_inatomic(&user_relocs->presumed_offset,
517 &r->presumed_offset,
518 sizeof(r->presumed_offset))) {
519 return -EFAULT;
520 }
521
522 user_relocs++;
523 r++;
524 } while (--count);
54cf91dc
CW
525 }
526
527 return 0;
1d83f442 528#undef N_RELOC
54cf91dc
CW
529}
530
531static int
27173f1f
BW
532i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
533 struct eb_vmas *eb,
534 struct drm_i915_gem_relocation_entry *relocs)
54cf91dc 535{
27173f1f 536 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
54cf91dc
CW
537 int i, ret;
538
539 for (i = 0; i < entry->relocation_count; i++) {
3e7a0322 540 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
54cf91dc
CW
541 if (ret)
542 return ret;
543 }
544
545 return 0;
546}
547
548static int
17601cbc 549i915_gem_execbuffer_relocate(struct eb_vmas *eb)
54cf91dc 550{
27173f1f 551 struct i915_vma *vma;
d4aeee77
CW
552 int ret = 0;
553
554 /* This is the fast path and we cannot handle a pagefault whilst
555 * holding the struct mutex lest the user pass in the relocations
556 * contained within a mmaped bo. For in such a case we, the page
557 * fault handler would call i915_gem_fault() and we would try to
558 * acquire the struct mutex again. Obviously this is bad and so
559 * lockdep complains vehemently.
560 */
561 pagefault_disable();
27173f1f
BW
562 list_for_each_entry(vma, &eb->vmas, exec_list) {
563 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
54cf91dc 564 if (ret)
d4aeee77 565 break;
54cf91dc 566 }
d4aeee77 567 pagefault_enable();
54cf91dc 568
d4aeee77 569 return ret;
54cf91dc
CW
570}
571
edf4427b
CW
572static bool only_mappable_for_reloc(unsigned int flags)
573{
574 return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
575 __EXEC_OBJECT_NEEDS_MAP;
576}
577
1690e1eb 578static int
27173f1f 579i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
a4872ba6 580 struct intel_engine_cs *ring,
27173f1f 581 bool *need_reloc)
1690e1eb 582{
6f65e29a 583 struct drm_i915_gem_object *obj = vma->obj;
27173f1f 584 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
d23db88c 585 uint64_t flags;
1690e1eb
CW
586 int ret;
587
1ec9e26d 588 flags = 0;
0229da32
DV
589 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
590 flags |= PIN_GLOBAL;
591
edf4427b
CW
592 if (!drm_mm_node_allocated(&vma->node)) {
593 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
594 flags |= PIN_GLOBAL | PIN_MAPPABLE;
edf4427b
CW
595 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
596 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
597 }
1ec9e26d
DV
598
599 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
edf4427b
CW
600 if ((ret == -ENOSPC || ret == -E2BIG) &&
601 only_mappable_for_reloc(entry->flags))
602 ret = i915_gem_object_pin(obj, vma->vm,
603 entry->alignment,
0229da32 604 flags & ~PIN_MAPPABLE);
1690e1eb
CW
605 if (ret)
606 return ret;
607
7788a765
CW
608 entry->flags |= __EXEC_OBJECT_HAS_PIN;
609
82b6b6d7
CW
610 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
611 ret = i915_gem_object_get_fence(obj);
612 if (ret)
613 return ret;
9a5a53b3 614
82b6b6d7
CW
615 if (i915_gem_object_pin_fence(obj))
616 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
1690e1eb
CW
617 }
618
27173f1f
BW
619 if (entry->offset != vma->node.start) {
620 entry->offset = vma->node.start;
ed5982e6
DV
621 *need_reloc = true;
622 }
623
624 if (entry->flags & EXEC_OBJECT_WRITE) {
625 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
626 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
627 }
628
1690e1eb 629 return 0;
7788a765 630}
1690e1eb 631
d23db88c 632static bool
e6a84468 633need_reloc_mappable(struct i915_vma *vma)
d23db88c
CW
634{
635 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
d23db88c 636
e6a84468
CW
637 if (entry->relocation_count == 0)
638 return false;
639
640 if (!i915_is_ggtt(vma->vm))
641 return false;
642
643 /* See also use_cpu_reloc() */
644 if (HAS_LLC(vma->obj->base.dev))
645 return false;
646
647 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
648 return false;
649
650 return true;
651}
652
653static bool
654eb_vma_misplaced(struct i915_vma *vma)
655{
656 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
657 struct drm_i915_gem_object *obj = vma->obj;
d23db88c 658
e6a84468 659 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
d23db88c
CW
660 !i915_is_ggtt(vma->vm));
661
662 if (entry->alignment &&
663 vma->node.start & (entry->alignment - 1))
664 return true;
665
d23db88c
CW
666 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
667 vma->node.start < BATCH_OFFSET_BIAS)
668 return true;
669
edf4427b
CW
670 /* avoid costly ping-pong once a batch bo ended up non-mappable */
671 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
672 return !only_mappable_for_reloc(entry->flags);
673
d23db88c
CW
674 return false;
675}
676
54cf91dc 677static int
a4872ba6 678i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
27173f1f 679 struct list_head *vmas,
ed5982e6 680 bool *need_relocs)
54cf91dc 681{
432e58ed 682 struct drm_i915_gem_object *obj;
27173f1f 683 struct i915_vma *vma;
68c8c17f 684 struct i915_address_space *vm;
27173f1f 685 struct list_head ordered_vmas;
7788a765
CW
686 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
687 int retry;
6fe4f140 688
227f782e
CW
689 i915_gem_retire_requests_ring(ring);
690
68c8c17f
BW
691 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
692
27173f1f
BW
693 INIT_LIST_HEAD(&ordered_vmas);
694 while (!list_empty(vmas)) {
6fe4f140
CW
695 struct drm_i915_gem_exec_object2 *entry;
696 bool need_fence, need_mappable;
697
27173f1f
BW
698 vma = list_first_entry(vmas, struct i915_vma, exec_list);
699 obj = vma->obj;
700 entry = vma->exec_entry;
6fe4f140 701
82b6b6d7
CW
702 if (!has_fenced_gpu_access)
703 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
6fe4f140 704 need_fence =
6fe4f140
CW
705 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
706 obj->tiling_mode != I915_TILING_NONE;
27173f1f 707 need_mappable = need_fence || need_reloc_mappable(vma);
6fe4f140 708
e6a84468
CW
709 if (need_mappable) {
710 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
27173f1f 711 list_move(&vma->exec_list, &ordered_vmas);
e6a84468 712 } else
27173f1f 713 list_move_tail(&vma->exec_list, &ordered_vmas);
595dad76 714
ed5982e6 715 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
595dad76 716 obj->base.pending_write_domain = 0;
6fe4f140 717 }
27173f1f 718 list_splice(&ordered_vmas, vmas);
54cf91dc
CW
719
720 /* Attempt to pin all of the buffers into the GTT.
721 * This is done in 3 phases:
722 *
723 * 1a. Unbind all objects that do not match the GTT constraints for
724 * the execbuffer (fenceable, mappable, alignment etc).
725 * 1b. Increment pin count for already bound objects.
726 * 2. Bind new objects.
727 * 3. Decrement pin count.
728 *
7788a765 729 * This avoid unnecessary unbinding of later objects in order to make
54cf91dc
CW
730 * room for the earlier objects *unless* we need to defragment.
731 */
732 retry = 0;
733 do {
7788a765 734 int ret = 0;
54cf91dc
CW
735
736 /* Unbind any ill-fitting objects or pin. */
27173f1f 737 list_for_each_entry(vma, vmas, exec_list) {
27173f1f 738 if (!drm_mm_node_allocated(&vma->node))
54cf91dc
CW
739 continue;
740
e6a84468 741 if (eb_vma_misplaced(vma))
27173f1f 742 ret = i915_vma_unbind(vma);
54cf91dc 743 else
27173f1f 744 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
432e58ed 745 if (ret)
54cf91dc 746 goto err;
54cf91dc
CW
747 }
748
749 /* Bind fresh objects */
27173f1f
BW
750 list_for_each_entry(vma, vmas, exec_list) {
751 if (drm_mm_node_allocated(&vma->node))
1690e1eb 752 continue;
54cf91dc 753
27173f1f 754 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
7788a765
CW
755 if (ret)
756 goto err;
54cf91dc
CW
757 }
758
a415d355 759err:
6c085a72 760 if (ret != -ENOSPC || retry++)
54cf91dc
CW
761 return ret;
762
a415d355
CW
763 /* Decrement pin count for bound objects */
764 list_for_each_entry(vma, vmas, exec_list)
765 i915_gem_execbuffer_unreserve_vma(vma);
766
68c8c17f 767 ret = i915_gem_evict_vm(vm, true);
54cf91dc
CW
768 if (ret)
769 return ret;
54cf91dc
CW
770 } while (1);
771}
772
773static int
774i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
ed5982e6 775 struct drm_i915_gem_execbuffer2 *args,
54cf91dc 776 struct drm_file *file,
a4872ba6 777 struct intel_engine_cs *ring,
27173f1f
BW
778 struct eb_vmas *eb,
779 struct drm_i915_gem_exec_object2 *exec)
54cf91dc
CW
780{
781 struct drm_i915_gem_relocation_entry *reloc;
27173f1f
BW
782 struct i915_address_space *vm;
783 struct i915_vma *vma;
ed5982e6 784 bool need_relocs;
dd6864a4 785 int *reloc_offset;
54cf91dc 786 int i, total, ret;
b205ca57 787 unsigned count = args->buffer_count;
54cf91dc 788
27173f1f
BW
789 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
790
67731b87 791 /* We may process another execbuffer during the unlock... */
27173f1f
BW
792 while (!list_empty(&eb->vmas)) {
793 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
794 list_del_init(&vma->exec_list);
a415d355 795 i915_gem_execbuffer_unreserve_vma(vma);
27173f1f 796 drm_gem_object_unreference(&vma->obj->base);
67731b87
CW
797 }
798
54cf91dc
CW
799 mutex_unlock(&dev->struct_mutex);
800
801 total = 0;
802 for (i = 0; i < count; i++)
432e58ed 803 total += exec[i].relocation_count;
54cf91dc 804
dd6864a4 805 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
54cf91dc 806 reloc = drm_malloc_ab(total, sizeof(*reloc));
dd6864a4
CW
807 if (reloc == NULL || reloc_offset == NULL) {
808 drm_free_large(reloc);
809 drm_free_large(reloc_offset);
54cf91dc
CW
810 mutex_lock(&dev->struct_mutex);
811 return -ENOMEM;
812 }
813
814 total = 0;
815 for (i = 0; i < count; i++) {
816 struct drm_i915_gem_relocation_entry __user *user_relocs;
262b6d36
CW
817 u64 invalid_offset = (u64)-1;
818 int j;
54cf91dc 819
2bb4629a 820 user_relocs = to_user_ptr(exec[i].relocs_ptr);
54cf91dc
CW
821
822 if (copy_from_user(reloc+total, user_relocs,
432e58ed 823 exec[i].relocation_count * sizeof(*reloc))) {
54cf91dc
CW
824 ret = -EFAULT;
825 mutex_lock(&dev->struct_mutex);
826 goto err;
827 }
828
262b6d36
CW
829 /* As we do not update the known relocation offsets after
830 * relocating (due to the complexities in lock handling),
831 * we need to mark them as invalid now so that we force the
832 * relocation processing next time. Just in case the target
833 * object is evicted and then rebound into its old
834 * presumed_offset before the next execbuffer - if that
835 * happened we would make the mistake of assuming that the
836 * relocations were valid.
837 */
838 for (j = 0; j < exec[i].relocation_count; j++) {
9aab8bff
CW
839 if (__copy_to_user(&user_relocs[j].presumed_offset,
840 &invalid_offset,
841 sizeof(invalid_offset))) {
262b6d36
CW
842 ret = -EFAULT;
843 mutex_lock(&dev->struct_mutex);
844 goto err;
845 }
846 }
847
dd6864a4 848 reloc_offset[i] = total;
432e58ed 849 total += exec[i].relocation_count;
54cf91dc
CW
850 }
851
852 ret = i915_mutex_lock_interruptible(dev);
853 if (ret) {
854 mutex_lock(&dev->struct_mutex);
855 goto err;
856 }
857
67731b87 858 /* reacquire the objects */
67731b87 859 eb_reset(eb);
27173f1f 860 ret = eb_lookup_vmas(eb, exec, args, vm, file);
3b96eff4
CW
861 if (ret)
862 goto err;
67731b87 863
ed5982e6 864 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
27173f1f 865 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
54cf91dc
CW
866 if (ret)
867 goto err;
868
27173f1f
BW
869 list_for_each_entry(vma, &eb->vmas, exec_list) {
870 int offset = vma->exec_entry - exec;
871 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
872 reloc + reloc_offset[offset]);
54cf91dc
CW
873 if (ret)
874 goto err;
54cf91dc
CW
875 }
876
877 /* Leave the user relocations as are, this is the painfully slow path,
878 * and we want to avoid the complication of dropping the lock whilst
879 * having buffers reserved in the aperture and so causing spurious
880 * ENOSPC for random operations.
881 */
882
883err:
884 drm_free_large(reloc);
dd6864a4 885 drm_free_large(reloc_offset);
54cf91dc
CW
886 return ret;
887}
888
54cf91dc 889static int
a4872ba6 890i915_gem_execbuffer_move_to_gpu(struct intel_engine_cs *ring,
27173f1f 891 struct list_head *vmas)
54cf91dc 892{
27173f1f 893 struct i915_vma *vma;
6ac42f41 894 uint32_t flush_domains = 0;
000433b6 895 bool flush_chipset = false;
432e58ed 896 int ret;
54cf91dc 897
27173f1f
BW
898 list_for_each_entry(vma, vmas, exec_list) {
899 struct drm_i915_gem_object *obj = vma->obj;
6ac42f41 900 ret = i915_gem_object_sync(obj, ring);
c59a333f
CW
901 if (ret)
902 return ret;
6ac42f41
DV
903
904 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
000433b6 905 flush_chipset |= i915_gem_clflush_object(obj, false);
6ac42f41 906
6ac42f41 907 flush_domains |= obj->base.write_domain;
c59a333f
CW
908 }
909
000433b6 910 if (flush_chipset)
e76e9aeb 911 i915_gem_chipset_flush(ring->dev);
6ac42f41
DV
912
913 if (flush_domains & I915_GEM_DOMAIN_GTT)
914 wmb();
915
09cf7c9a
CW
916 /* Unconditionally invalidate gpu caches and ensure that we do flush
917 * any residual writes from the previous batch.
918 */
a7b9761d 919 return intel_ring_invalidate_all_caches(ring);
54cf91dc
CW
920}
921
432e58ed
CW
922static bool
923i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
54cf91dc 924{
ed5982e6
DV
925 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
926 return false;
927
432e58ed 928 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
54cf91dc
CW
929}
930
931static int
ad19f10b
CW
932validate_exec_list(struct drm_device *dev,
933 struct drm_i915_gem_exec_object2 *exec,
54cf91dc
CW
934 int count)
935{
b205ca57
DV
936 unsigned relocs_total = 0;
937 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
ad19f10b
CW
938 unsigned invalid_flags;
939 int i;
940
941 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
942 if (USES_FULL_PPGTT(dev))
943 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
54cf91dc
CW
944
945 for (i = 0; i < count; i++) {
2bb4629a 946 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
54cf91dc
CW
947 int length; /* limited by fault_in_pages_readable() */
948
ad19f10b 949 if (exec[i].flags & invalid_flags)
ed5982e6
DV
950 return -EINVAL;
951
3118a4f6
KC
952 /* First check for malicious input causing overflow in
953 * the worst case where we need to allocate the entire
954 * relocation tree as a single array.
955 */
956 if (exec[i].relocation_count > relocs_max - relocs_total)
54cf91dc 957 return -EINVAL;
3118a4f6 958 relocs_total += exec[i].relocation_count;
54cf91dc
CW
959
960 length = exec[i].relocation_count *
961 sizeof(struct drm_i915_gem_relocation_entry);
30587535
KC
962 /*
963 * We must check that the entire relocation array is safe
964 * to read, but since we may need to update the presumed
965 * offsets during execution, check for full write access.
966 */
54cf91dc
CW
967 if (!access_ok(VERIFY_WRITE, ptr, length))
968 return -EFAULT;
969
d330a953 970 if (likely(!i915.prefault_disable)) {
0b74b508
XZ
971 if (fault_in_multipages_readable(ptr, length))
972 return -EFAULT;
973 }
54cf91dc
CW
974 }
975
976 return 0;
977}
978
273497e5 979static struct intel_context *
d299cce7 980i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
a4872ba6 981 struct intel_engine_cs *ring, const u32 ctx_id)
d299cce7 982{
273497e5 983 struct intel_context *ctx = NULL;
d299cce7
MK
984 struct i915_ctx_hang_stats *hs;
985
821d66dd 986 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
7c9c4b8f
DV
987 return ERR_PTR(-EINVAL);
988
41bde553 989 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
72ad5c45 990 if (IS_ERR(ctx))
41bde553 991 return ctx;
d299cce7 992
41bde553 993 hs = &ctx->hang_stats;
d299cce7
MK
994 if (hs->banned) {
995 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
41bde553 996 return ERR_PTR(-EIO);
d299cce7
MK
997 }
998
ec3e9963
OM
999 if (i915.enable_execlists && !ctx->engine[ring->id].state) {
1000 int ret = intel_lr_context_deferred_create(ctx, ring);
1001 if (ret) {
1002 DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
1003 return ERR_PTR(ret);
1004 }
1005 }
1006
41bde553 1007 return ctx;
d299cce7
MK
1008}
1009
ba8b7ccb 1010void
27173f1f 1011i915_gem_execbuffer_move_to_active(struct list_head *vmas,
a4872ba6 1012 struct intel_engine_cs *ring)
432e58ed 1013{
97b2a6a1 1014 struct drm_i915_gem_request *req = intel_ring_get_request(ring);
27173f1f 1015 struct i915_vma *vma;
432e58ed 1016
27173f1f 1017 list_for_each_entry(vma, vmas, exec_list) {
82b6b6d7 1018 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
27173f1f 1019 struct drm_i915_gem_object *obj = vma->obj;
69c2fc89
CW
1020 u32 old_read = obj->base.read_domains;
1021 u32 old_write = obj->base.write_domain;
db53a302 1022
432e58ed 1023 obj->base.write_domain = obj->base.pending_write_domain;
ed5982e6
DV
1024 if (obj->base.write_domain == 0)
1025 obj->base.pending_read_domains |= obj->base.read_domains;
1026 obj->base.read_domains = obj->base.pending_read_domains;
432e58ed 1027
e2d05a8b 1028 i915_vma_move_to_active(vma, ring);
432e58ed
CW
1029 if (obj->base.write_domain) {
1030 obj->dirty = 1;
97b2a6a1 1031 i915_gem_request_assign(&obj->last_write_req, req);
f99d7069 1032
a4001f1b 1033 intel_fb_obj_invalidate(obj, ring, ORIGIN_CS);
c8725f3d
CW
1034
1035 /* update for the implicit flush after a batch */
1036 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
432e58ed 1037 }
82b6b6d7 1038 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
97b2a6a1 1039 i915_gem_request_assign(&obj->last_fenced_req, req);
82b6b6d7
CW
1040 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
1041 struct drm_i915_private *dev_priv = to_i915(ring->dev);
1042 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
1043 &dev_priv->mm.fence_list);
1044 }
1045 }
432e58ed 1046
db53a302 1047 trace_i915_gem_object_change_domain(obj, old_read, old_write);
432e58ed
CW
1048 }
1049}
1050
ba8b7ccb 1051void
54cf91dc 1052i915_gem_execbuffer_retire_commands(struct drm_device *dev,
432e58ed 1053 struct drm_file *file,
a4872ba6 1054 struct intel_engine_cs *ring,
7d736f4f 1055 struct drm_i915_gem_object *obj)
54cf91dc 1056{
cc889e0f
DV
1057 /* Unconditionally force add_request to emit a full flush. */
1058 ring->gpu_caches_dirty = true;
54cf91dc 1059
432e58ed 1060 /* Add a breadcrumb for the completion of the batch buffer */
9400ae5c 1061 (void)__i915_add_request(ring, file, obj);
432e58ed 1062}
54cf91dc 1063
ae662d31
EA
1064static int
1065i915_reset_gen7_sol_offsets(struct drm_device *dev,
a4872ba6 1066 struct intel_engine_cs *ring)
ae662d31 1067{
50227e1c 1068 struct drm_i915_private *dev_priv = dev->dev_private;
ae662d31
EA
1069 int ret, i;
1070
9d662da8
DV
1071 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
1072 DRM_DEBUG("sol reset is gen7/rcs only\n");
1073 return -EINVAL;
1074 }
ae662d31
EA
1075
1076 ret = intel_ring_begin(ring, 4 * 3);
1077 if (ret)
1078 return ret;
1079
1080 for (i = 0; i < 4; i++) {
1081 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1082 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
1083 intel_ring_emit(ring, 0);
1084 }
1085
1086 intel_ring_advance(ring);
1087
1088 return 0;
1089}
1090
5c6c6003
CW
1091static int
1092i915_emit_box(struct intel_engine_cs *ring,
1093 struct drm_clip_rect *box,
1094 int DR1, int DR4)
1095{
1096 int ret;
1097
1098 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
1099 box->y2 <= 0 || box->x2 <= 0) {
1100 DRM_ERROR("Bad box %d,%d..%d,%d\n",
1101 box->x1, box->y1, box->x2, box->y2);
1102 return -EINVAL;
1103 }
1104
1105 if (INTEL_INFO(ring->dev)->gen >= 4) {
1106 ret = intel_ring_begin(ring, 4);
1107 if (ret)
1108 return ret;
1109
1110 intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO_I965);
1111 intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
1112 intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
1113 intel_ring_emit(ring, DR4);
1114 } else {
1115 ret = intel_ring_begin(ring, 6);
1116 if (ret)
1117 return ret;
1118
1119 intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO);
1120 intel_ring_emit(ring, DR1);
1121 intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
1122 intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
1123 intel_ring_emit(ring, DR4);
1124 intel_ring_emit(ring, 0);
1125 }
1126 intel_ring_advance(ring);
1127
1128 return 0;
1129}
1130
71745376
BV
1131static struct drm_i915_gem_object*
1132i915_gem_execbuffer_parse(struct intel_engine_cs *ring,
1133 struct drm_i915_gem_exec_object2 *shadow_exec_entry,
1134 struct eb_vmas *eb,
1135 struct drm_i915_gem_object *batch_obj,
1136 u32 batch_start_offset,
1137 u32 batch_len,
17cabf57 1138 bool is_master)
71745376 1139{
71745376 1140 struct drm_i915_gem_object *shadow_batch_obj;
17cabf57 1141 struct i915_vma *vma;
71745376
BV
1142 int ret;
1143
06fbca71 1144 shadow_batch_obj = i915_gem_batch_pool_get(&ring->batch_pool,
17cabf57 1145 PAGE_ALIGN(batch_len));
71745376
BV
1146 if (IS_ERR(shadow_batch_obj))
1147 return shadow_batch_obj;
1148
1149 ret = i915_parse_cmds(ring,
1150 batch_obj,
1151 shadow_batch_obj,
1152 batch_start_offset,
1153 batch_len,
1154 is_master);
17cabf57
CW
1155 if (ret)
1156 goto err;
71745376 1157
17cabf57
CW
1158 ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
1159 if (ret)
1160 goto err;
71745376 1161
de4e783a
CW
1162 i915_gem_object_unpin_pages(shadow_batch_obj);
1163
17cabf57 1164 memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
71745376 1165
17cabf57
CW
1166 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1167 vma->exec_entry = shadow_exec_entry;
de4e783a 1168 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
17cabf57
CW
1169 drm_gem_object_reference(&shadow_batch_obj->base);
1170 list_add_tail(&vma->exec_list, &eb->vmas);
71745376 1171
17cabf57
CW
1172 shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
1173
1174 return shadow_batch_obj;
71745376 1175
17cabf57 1176err:
de4e783a 1177 i915_gem_object_unpin_pages(shadow_batch_obj);
17cabf57
CW
1178 if (ret == -EACCES) /* unhandled chained batch */
1179 return batch_obj;
1180 else
1181 return ERR_PTR(ret);
71745376 1182}
5c6c6003 1183
a83014d3
OM
1184int
1185i915_gem_ringbuffer_submission(struct drm_device *dev, struct drm_file *file,
1186 struct intel_engine_cs *ring,
1187 struct intel_context *ctx,
1188 struct drm_i915_gem_execbuffer2 *args,
1189 struct list_head *vmas,
1190 struct drm_i915_gem_object *batch_obj,
8e004efc 1191 u64 exec_start, u32 dispatch_flags)
78382593
OM
1192{
1193 struct drm_clip_rect *cliprects = NULL;
1194 struct drm_i915_private *dev_priv = dev->dev_private;
1195 u64 exec_len;
1196 int instp_mode;
1197 u32 instp_mask;
1198 int i, ret = 0;
1199
1200 if (args->num_cliprects != 0) {
1201 if (ring != &dev_priv->ring[RCS]) {
1202 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
1203 return -EINVAL;
1204 }
1205
1206 if (INTEL_INFO(dev)->gen >= 5) {
1207 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1208 return -EINVAL;
1209 }
1210
1211 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1212 DRM_DEBUG("execbuf with %u cliprects\n",
1213 args->num_cliprects);
1214 return -EINVAL;
1215 }
1216
1217 cliprects = kcalloc(args->num_cliprects,
1218 sizeof(*cliprects),
1219 GFP_KERNEL);
1220 if (cliprects == NULL) {
1221 ret = -ENOMEM;
1222 goto error;
1223 }
1224
1225 if (copy_from_user(cliprects,
1226 to_user_ptr(args->cliprects_ptr),
1227 sizeof(*cliprects)*args->num_cliprects)) {
1228 ret = -EFAULT;
1229 goto error;
1230 }
1231 } else {
1232 if (args->DR4 == 0xffffffff) {
1233 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1234 args->DR4 = 0;
1235 }
1236
1237 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
1238 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
1239 return -EINVAL;
1240 }
1241 }
1242
1243 ret = i915_gem_execbuffer_move_to_gpu(ring, vmas);
1244 if (ret)
1245 goto error;
1246
1247 ret = i915_switch_context(ring, ctx);
1248 if (ret)
1249 goto error;
1250
563222a7
BW
1251 if (ctx->ppgtt)
1252 WARN(ctx->ppgtt->pd_dirty_rings & (1<<ring->id),
1253 "%s didn't clear reload\n", ring->name);
563222a7 1254
78382593
OM
1255 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1256 instp_mask = I915_EXEC_CONSTANTS_MASK;
1257 switch (instp_mode) {
1258 case I915_EXEC_CONSTANTS_REL_GENERAL:
1259 case I915_EXEC_CONSTANTS_ABSOLUTE:
1260 case I915_EXEC_CONSTANTS_REL_SURFACE:
1261 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
1262 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
1263 ret = -EINVAL;
1264 goto error;
1265 }
1266
1267 if (instp_mode != dev_priv->relative_constants_mode) {
1268 if (INTEL_INFO(dev)->gen < 4) {
1269 DRM_DEBUG("no rel constants on pre-gen4\n");
1270 ret = -EINVAL;
1271 goto error;
1272 }
1273
1274 if (INTEL_INFO(dev)->gen > 5 &&
1275 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1276 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
1277 ret = -EINVAL;
1278 goto error;
1279 }
1280
1281 /* The HW changed the meaning on this bit on gen6 */
1282 if (INTEL_INFO(dev)->gen >= 6)
1283 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1284 }
1285 break;
1286 default:
1287 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
1288 ret = -EINVAL;
1289 goto error;
1290 }
1291
1292 if (ring == &dev_priv->ring[RCS] &&
1293 instp_mode != dev_priv->relative_constants_mode) {
1294 ret = intel_ring_begin(ring, 4);
1295 if (ret)
1296 goto error;
1297
1298 intel_ring_emit(ring, MI_NOOP);
1299 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1300 intel_ring_emit(ring, INSTPM);
1301 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1302 intel_ring_advance(ring);
1303
1304 dev_priv->relative_constants_mode = instp_mode;
1305 }
1306
1307 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1308 ret = i915_reset_gen7_sol_offsets(dev, ring);
1309 if (ret)
1310 goto error;
1311 }
1312
1313 exec_len = args->batch_len;
1314 if (cliprects) {
1315 for (i = 0; i < args->num_cliprects; i++) {
5c6c6003 1316 ret = i915_emit_box(ring, &cliprects[i],
78382593
OM
1317 args->DR1, args->DR4);
1318 if (ret)
1319 goto error;
1320
1321 ret = ring->dispatch_execbuffer(ring,
1322 exec_start, exec_len,
8e004efc 1323 dispatch_flags);
78382593
OM
1324 if (ret)
1325 goto error;
1326 }
1327 } else {
1328 ret = ring->dispatch_execbuffer(ring,
1329 exec_start, exec_len,
8e004efc 1330 dispatch_flags);
78382593
OM
1331 if (ret)
1332 return ret;
1333 }
1334
8e004efc 1335 trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), dispatch_flags);
78382593
OM
1336
1337 i915_gem_execbuffer_move_to_active(vmas, ring);
1338 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
1339
1340error:
1341 kfree(cliprects);
1342 return ret;
1343}
1344
a8ebba75
ZY
1345/**
1346 * Find one BSD ring to dispatch the corresponding BSD command.
1347 * The Ring ID is returned.
1348 */
1349static int gen8_dispatch_bsd_ring(struct drm_device *dev,
1350 struct drm_file *file)
1351{
1352 struct drm_i915_private *dev_priv = dev->dev_private;
1353 struct drm_i915_file_private *file_priv = file->driver_priv;
1354
1355 /* Check whether the file_priv is using one ring */
1356 if (file_priv->bsd_ring)
1357 return file_priv->bsd_ring->id;
1358 else {
1359 /* If no, use the ping-pong mechanism to select one ring */
1360 int ring_id;
1361
1362 mutex_lock(&dev->struct_mutex);
bdf1e7e3 1363 if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
a8ebba75 1364 ring_id = VCS;
bdf1e7e3 1365 dev_priv->mm.bsd_ring_dispatch_index = 1;
a8ebba75
ZY
1366 } else {
1367 ring_id = VCS2;
bdf1e7e3 1368 dev_priv->mm.bsd_ring_dispatch_index = 0;
a8ebba75
ZY
1369 }
1370 file_priv->bsd_ring = &dev_priv->ring[ring_id];
1371 mutex_unlock(&dev->struct_mutex);
1372 return ring_id;
1373 }
1374}
1375
d23db88c
CW
1376static struct drm_i915_gem_object *
1377eb_get_batch(struct eb_vmas *eb)
1378{
1379 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
1380
1381 /*
1382 * SNA is doing fancy tricks with compressing batch buffers, which leads
1383 * to negative relocation deltas. Usually that works out ok since the
1384 * relocate address is still positive, except when the batch is placed
1385 * very low in the GTT. Ensure this doesn't happen.
1386 *
1387 * Note that actual hangs have only been observed on gen7, but for
1388 * paranoia do it everywhere.
1389 */
1390 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
1391
1392 return vma->obj;
1393}
1394
54cf91dc
CW
1395static int
1396i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1397 struct drm_file *file,
1398 struct drm_i915_gem_execbuffer2 *args,
41bde553 1399 struct drm_i915_gem_exec_object2 *exec)
54cf91dc 1400{
50227e1c 1401 struct drm_i915_private *dev_priv = dev->dev_private;
27173f1f 1402 struct eb_vmas *eb;
54cf91dc 1403 struct drm_i915_gem_object *batch_obj;
78a42377 1404 struct drm_i915_gem_exec_object2 shadow_exec_entry;
a4872ba6 1405 struct intel_engine_cs *ring;
273497e5 1406 struct intel_context *ctx;
41bde553 1407 struct i915_address_space *vm;
d299cce7 1408 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
78382593 1409 u64 exec_start = args->batch_start_offset;
8e004efc 1410 u32 dispatch_flags;
78382593 1411 int ret;
ed5982e6 1412 bool need_relocs;
54cf91dc 1413
ed5982e6 1414 if (!i915_gem_check_execbuffer(args))
432e58ed 1415 return -EINVAL;
432e58ed 1416
ad19f10b 1417 ret = validate_exec_list(dev, exec, args->buffer_count);
54cf91dc
CW
1418 if (ret)
1419 return ret;
1420
8e004efc 1421 dispatch_flags = 0;
d7d4eedd
CW
1422 if (args->flags & I915_EXEC_SECURE) {
1423 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1424 return -EPERM;
1425
8e004efc 1426 dispatch_flags |= I915_DISPATCH_SECURE;
d7d4eedd 1427 }
b45305fc 1428 if (args->flags & I915_EXEC_IS_PINNED)
8e004efc 1429 dispatch_flags |= I915_DISPATCH_PINNED;
d7d4eedd 1430
b1a93306 1431 if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
ff240199 1432 DRM_DEBUG("execbuf with unknown ring: %d\n",
54cf91dc
CW
1433 (int)(args->flags & I915_EXEC_RING_MASK));
1434 return -EINVAL;
1435 }
ca01b12b 1436
8d360dff
ZG
1437 if (((args->flags & I915_EXEC_RING_MASK) != I915_EXEC_BSD) &&
1438 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1439 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1440 "bsd dispatch flags: %d\n", (int)(args->flags));
1441 return -EINVAL;
1442 }
1443
ca01b12b
BW
1444 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1445 ring = &dev_priv->ring[RCS];
a8ebba75
ZY
1446 else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
1447 if (HAS_BSD2(dev)) {
1448 int ring_id;
8d360dff
ZG
1449
1450 switch (args->flags & I915_EXEC_BSD_MASK) {
1451 case I915_EXEC_BSD_DEFAULT:
1452 ring_id = gen8_dispatch_bsd_ring(dev, file);
1453 ring = &dev_priv->ring[ring_id];
1454 break;
1455 case I915_EXEC_BSD_RING1:
1456 ring = &dev_priv->ring[VCS];
1457 break;
1458 case I915_EXEC_BSD_RING2:
1459 ring = &dev_priv->ring[VCS2];
1460 break;
1461 default:
1462 DRM_DEBUG("execbuf with unknown bsd ring: %d\n",
1463 (int)(args->flags & I915_EXEC_BSD_MASK));
1464 return -EINVAL;
1465 }
a8ebba75
ZY
1466 } else
1467 ring = &dev_priv->ring[VCS];
1468 } else
ca01b12b
BW
1469 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1470
a15817cf
CW
1471 if (!intel_ring_initialized(ring)) {
1472 DRM_DEBUG("execbuf with invalid ring: %d\n",
1473 (int)(args->flags & I915_EXEC_RING_MASK));
1474 return -EINVAL;
1475 }
54cf91dc
CW
1476
1477 if (args->buffer_count < 1) {
ff240199 1478 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
54cf91dc
CW
1479 return -EINVAL;
1480 }
54cf91dc 1481
f65c9168
PZ
1482 intel_runtime_pm_get(dev_priv);
1483
54cf91dc
CW
1484 ret = i915_mutex_lock_interruptible(dev);
1485 if (ret)
1486 goto pre_mutex_err;
1487
7c9c4b8f 1488 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
72ad5c45 1489 if (IS_ERR(ctx)) {
d299cce7 1490 mutex_unlock(&dev->struct_mutex);
41bde553 1491 ret = PTR_ERR(ctx);
d299cce7 1492 goto pre_mutex_err;
935f38d6 1493 }
41bde553
BW
1494
1495 i915_gem_context_reference(ctx);
1496
ae6c4806
DV
1497 if (ctx->ppgtt)
1498 vm = &ctx->ppgtt->base;
1499 else
7e0d96bc 1500 vm = &dev_priv->gtt.base;
d299cce7 1501
17601cbc 1502 eb = eb_create(args);
67731b87 1503 if (eb == NULL) {
935f38d6 1504 i915_gem_context_unreference(ctx);
67731b87
CW
1505 mutex_unlock(&dev->struct_mutex);
1506 ret = -ENOMEM;
1507 goto pre_mutex_err;
1508 }
1509
54cf91dc 1510 /* Look up object handles */
27173f1f 1511 ret = eb_lookup_vmas(eb, exec, args, vm, file);
3b96eff4
CW
1512 if (ret)
1513 goto err;
54cf91dc 1514
6fe4f140 1515 /* take note of the batch buffer before we might reorder the lists */
d23db88c 1516 batch_obj = eb_get_batch(eb);
6fe4f140 1517
54cf91dc 1518 /* Move the objects en-masse into the GTT, evicting if necessary. */
ed5982e6 1519 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
27173f1f 1520 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
54cf91dc
CW
1521 if (ret)
1522 goto err;
1523
1524 /* The objects are in their final locations, apply the relocations. */
ed5982e6 1525 if (need_relocs)
17601cbc 1526 ret = i915_gem_execbuffer_relocate(eb);
54cf91dc
CW
1527 if (ret) {
1528 if (ret == -EFAULT) {
ed5982e6 1529 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
27173f1f 1530 eb, exec);
54cf91dc
CW
1531 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1532 }
1533 if (ret)
1534 goto err;
1535 }
1536
1537 /* Set the pending read domains for the batch buffer to COMMAND */
54cf91dc 1538 if (batch_obj->base.pending_write_domain) {
ff240199 1539 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
54cf91dc
CW
1540 ret = -EINVAL;
1541 goto err;
1542 }
54cf91dc 1543
743e78c1 1544 if (i915_needs_cmd_parser(ring) && args->batch_len) {
71745376
BV
1545 batch_obj = i915_gem_execbuffer_parse(ring,
1546 &shadow_exec_entry,
1547 eb,
1548 batch_obj,
1549 args->batch_start_offset,
1550 args->batch_len,
17cabf57 1551 file->is_master);
71745376
BV
1552 if (IS_ERR(batch_obj)) {
1553 ret = PTR_ERR(batch_obj);
78a42377
BV
1554 goto err;
1555 }
17cabf57
CW
1556
1557 /*
1558 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1559 * bit from MI_BATCH_BUFFER_START commands issued in the
1560 * dispatch_execbuffer implementations. We specifically
1561 * don't want that set when the command parser is
1562 * enabled.
1563 *
1564 * FIXME: with aliasing ppgtt, buffers that should only
1565 * be in ggtt still end up in the aliasing ppgtt. remove
1566 * this check when that is fixed.
1567 */
1568 if (USES_FULL_PPGTT(dev))
8e004efc 1569 dispatch_flags |= I915_DISPATCH_SECURE;
17cabf57
CW
1570
1571 exec_start = 0;
351e3db2
BV
1572 }
1573
78a42377
BV
1574 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1575
d7d4eedd
CW
1576 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1577 * batch" bit. Hence we need to pin secure batches into the global gtt.
28cf5415 1578 * hsw should have this fixed, but bdw mucks it up again. */
8e004efc 1579 if (dispatch_flags & I915_DISPATCH_SECURE) {
da51a1e7
DV
1580 /*
1581 * So on first glance it looks freaky that we pin the batch here
1582 * outside of the reservation loop. But:
1583 * - The batch is already pinned into the relevant ppgtt, so we
1584 * already have the backing storage fully allocated.
1585 * - No other BO uses the global gtt (well contexts, but meh),
fd0753cf 1586 * so we don't really have issues with multiple objects not
da51a1e7
DV
1587 * fitting due to fragmentation.
1588 * So this is actually safe.
1589 */
1590 ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
1591 if (ret)
1592 goto err;
d7d4eedd 1593
7e0d96bc 1594 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
da51a1e7 1595 } else
7e0d96bc 1596 exec_start += i915_gem_obj_offset(batch_obj, vm);
d7d4eedd 1597
f3dc74c0
JH
1598 ret = dev_priv->gt.execbuf_submit(dev, file, ring, ctx, args,
1599 &eb->vmas, batch_obj, exec_start,
1600 dispatch_flags);
54cf91dc 1601
da51a1e7
DV
1602 /*
1603 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1604 * batch vma for correctness. For less ugly and less fragility this
1605 * needs to be adjusted to also track the ggtt batch vma properly as
1606 * active.
1607 */
8e004efc 1608 if (dispatch_flags & I915_DISPATCH_SECURE)
da51a1e7 1609 i915_gem_object_ggtt_unpin(batch_obj);
54cf91dc 1610err:
41bde553
BW
1611 /* the request owns the ref now */
1612 i915_gem_context_unreference(ctx);
67731b87 1613 eb_destroy(eb);
54cf91dc
CW
1614
1615 mutex_unlock(&dev->struct_mutex);
1616
1617pre_mutex_err:
f65c9168
PZ
1618 /* intel_gpu_busy should also get a ref, so it will free when the device
1619 * is really idle. */
1620 intel_runtime_pm_put(dev_priv);
54cf91dc
CW
1621 return ret;
1622}
1623
1624/*
1625 * Legacy execbuffer just creates an exec2 list from the original exec object
1626 * list array and passes it to the real function.
1627 */
1628int
1629i915_gem_execbuffer(struct drm_device *dev, void *data,
1630 struct drm_file *file)
1631{
1632 struct drm_i915_gem_execbuffer *args = data;
1633 struct drm_i915_gem_execbuffer2 exec2;
1634 struct drm_i915_gem_exec_object *exec_list = NULL;
1635 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1636 int ret, i;
1637
54cf91dc 1638 if (args->buffer_count < 1) {
ff240199 1639 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
54cf91dc
CW
1640 return -EINVAL;
1641 }
1642
1643 /* Copy in the exec list from userland */
1644 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1645 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1646 if (exec_list == NULL || exec2_list == NULL) {
ff240199 1647 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
54cf91dc
CW
1648 args->buffer_count);
1649 drm_free_large(exec_list);
1650 drm_free_large(exec2_list);
1651 return -ENOMEM;
1652 }
1653 ret = copy_from_user(exec_list,
2bb4629a 1654 to_user_ptr(args->buffers_ptr),
54cf91dc
CW
1655 sizeof(*exec_list) * args->buffer_count);
1656 if (ret != 0) {
ff240199 1657 DRM_DEBUG("copy %d exec entries failed %d\n",
54cf91dc
CW
1658 args->buffer_count, ret);
1659 drm_free_large(exec_list);
1660 drm_free_large(exec2_list);
1661 return -EFAULT;
1662 }
1663
1664 for (i = 0; i < args->buffer_count; i++) {
1665 exec2_list[i].handle = exec_list[i].handle;
1666 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1667 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1668 exec2_list[i].alignment = exec_list[i].alignment;
1669 exec2_list[i].offset = exec_list[i].offset;
1670 if (INTEL_INFO(dev)->gen < 4)
1671 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1672 else
1673 exec2_list[i].flags = 0;
1674 }
1675
1676 exec2.buffers_ptr = args->buffers_ptr;
1677 exec2.buffer_count = args->buffer_count;
1678 exec2.batch_start_offset = args->batch_start_offset;
1679 exec2.batch_len = args->batch_len;
1680 exec2.DR1 = args->DR1;
1681 exec2.DR4 = args->DR4;
1682 exec2.num_cliprects = args->num_cliprects;
1683 exec2.cliprects_ptr = args->cliprects_ptr;
1684 exec2.flags = I915_EXEC_RENDER;
6e0a69db 1685 i915_execbuffer2_set_context_id(exec2, 0);
54cf91dc 1686
41bde553 1687 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
54cf91dc 1688 if (!ret) {
9aab8bff
CW
1689 struct drm_i915_gem_exec_object __user *user_exec_list =
1690 to_user_ptr(args->buffers_ptr);
1691
54cf91dc 1692 /* Copy the new buffer offsets back to the user's exec list. */
9aab8bff
CW
1693 for (i = 0; i < args->buffer_count; i++) {
1694 ret = __copy_to_user(&user_exec_list[i].offset,
1695 &exec2_list[i].offset,
1696 sizeof(user_exec_list[i].offset));
1697 if (ret) {
1698 ret = -EFAULT;
1699 DRM_DEBUG("failed to copy %d exec entries "
1700 "back to user (%d)\n",
1701 args->buffer_count, ret);
1702 break;
1703 }
54cf91dc
CW
1704 }
1705 }
1706
1707 drm_free_large(exec_list);
1708 drm_free_large(exec2_list);
1709 return ret;
1710}
1711
1712int
1713i915_gem_execbuffer2(struct drm_device *dev, void *data,
1714 struct drm_file *file)
1715{
1716 struct drm_i915_gem_execbuffer2 *args = data;
1717 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1718 int ret;
1719
ed8cd3b2
XW
1720 if (args->buffer_count < 1 ||
1721 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
ff240199 1722 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
54cf91dc
CW
1723 return -EINVAL;
1724 }
1725
9cb34664
DV
1726 if (args->rsvd2 != 0) {
1727 DRM_DEBUG("dirty rvsd2 field\n");
1728 return -EINVAL;
1729 }
1730
8408c282 1731 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
419fa72a 1732 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
8408c282
CW
1733 if (exec2_list == NULL)
1734 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1735 args->buffer_count);
54cf91dc 1736 if (exec2_list == NULL) {
ff240199 1737 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
54cf91dc
CW
1738 args->buffer_count);
1739 return -ENOMEM;
1740 }
1741 ret = copy_from_user(exec2_list,
2bb4629a 1742 to_user_ptr(args->buffers_ptr),
54cf91dc
CW
1743 sizeof(*exec2_list) * args->buffer_count);
1744 if (ret != 0) {
ff240199 1745 DRM_DEBUG("copy %d exec entries failed %d\n",
54cf91dc
CW
1746 args->buffer_count, ret);
1747 drm_free_large(exec2_list);
1748 return -EFAULT;
1749 }
1750
41bde553 1751 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
54cf91dc
CW
1752 if (!ret) {
1753 /* Copy the new buffer offsets back to the user's exec list. */
d593d992 1754 struct drm_i915_gem_exec_object2 __user *user_exec_list =
9aab8bff
CW
1755 to_user_ptr(args->buffers_ptr);
1756 int i;
1757
1758 for (i = 0; i < args->buffer_count; i++) {
1759 ret = __copy_to_user(&user_exec_list[i].offset,
1760 &exec2_list[i].offset,
1761 sizeof(user_exec_list[i].offset));
1762 if (ret) {
1763 ret = -EFAULT;
1764 DRM_DEBUG("failed to copy %d exec entries "
1765 "back to user\n",
1766 args->buffer_count);
1767 break;
1768 }
54cf91dc
CW
1769 }
1770 }
1771
1772 drm_free_large(exec2_list);
1773 return ret;
1774}
This page took 0.389396 seconds and 5 git commands to generate.