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