drm/i915: Include a little more information about why ring init fails
[linux-2.6-block.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)
38
27173f1f
BW
39struct eb_vmas {
40 struct list_head vmas;
67731b87 41 int and;
eef90ccb 42 union {
27173f1f 43 struct i915_vma *lut[0];
eef90ccb
CW
44 struct hlist_head buckets[0];
45 };
67731b87
CW
46};
47
27173f1f 48static struct eb_vmas *
17601cbc 49eb_create(struct drm_i915_gem_execbuffer2 *args)
67731b87 50{
27173f1f 51 struct eb_vmas *eb = NULL;
eef90ccb
CW
52
53 if (args->flags & I915_EXEC_HANDLE_LUT) {
b205ca57 54 unsigned size = args->buffer_count;
27173f1f
BW
55 size *= sizeof(struct i915_vma *);
56 size += sizeof(struct eb_vmas);
eef90ccb
CW
57 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
58 }
59
60 if (eb == NULL) {
b205ca57
DV
61 unsigned size = args->buffer_count;
62 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
27b7c63a 63 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
eef90ccb
CW
64 while (count > 2*size)
65 count >>= 1;
66 eb = kzalloc(count*sizeof(struct hlist_head) +
27173f1f 67 sizeof(struct eb_vmas),
eef90ccb
CW
68 GFP_TEMPORARY);
69 if (eb == NULL)
70 return eb;
71
72 eb->and = count - 1;
73 } else
74 eb->and = -args->buffer_count;
75
27173f1f 76 INIT_LIST_HEAD(&eb->vmas);
67731b87
CW
77 return eb;
78}
79
80static void
27173f1f 81eb_reset(struct eb_vmas *eb)
67731b87 82{
eef90ccb
CW
83 if (eb->and >= 0)
84 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
67731b87
CW
85}
86
3b96eff4 87static int
27173f1f
BW
88eb_lookup_vmas(struct eb_vmas *eb,
89 struct drm_i915_gem_exec_object2 *exec,
90 const struct drm_i915_gem_execbuffer2 *args,
91 struct i915_address_space *vm,
92 struct drm_file *file)
3b96eff4 93{
6f65e29a 94 struct drm_i915_private *dev_priv = vm->dev->dev_private;
27173f1f
BW
95 struct drm_i915_gem_object *obj;
96 struct list_head objects;
9ae9ab52 97 int i, ret;
3b96eff4 98
27173f1f 99 INIT_LIST_HEAD(&objects);
3b96eff4 100 spin_lock(&file->table_lock);
27173f1f
BW
101 /* Grab a reference to the object and release the lock so we can lookup
102 * or create the VMA without using GFP_ATOMIC */
eef90ccb 103 for (i = 0; i < args->buffer_count; i++) {
3b96eff4
CW
104 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
105 if (obj == NULL) {
106 spin_unlock(&file->table_lock);
107 DRM_DEBUG("Invalid object handle %d at index %d\n",
108 exec[i].handle, i);
27173f1f 109 ret = -ENOENT;
9ae9ab52 110 goto err;
3b96eff4
CW
111 }
112
27173f1f 113 if (!list_empty(&obj->obj_exec_link)) {
3b96eff4
CW
114 spin_unlock(&file->table_lock);
115 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
116 obj, exec[i].handle, i);
27173f1f 117 ret = -EINVAL;
9ae9ab52 118 goto err;
3b96eff4
CW
119 }
120
121 drm_gem_object_reference(&obj->base);
27173f1f
BW
122 list_add_tail(&obj->obj_exec_link, &objects);
123 }
124 spin_unlock(&file->table_lock);
3b96eff4 125
27173f1f 126 i = 0;
9ae9ab52 127 while (!list_empty(&objects)) {
27173f1f 128 struct i915_vma *vma;
6f65e29a
BW
129 struct i915_address_space *bind_vm = vm;
130
2c9f8d56
DV
131 if (exec[i].flags & EXEC_OBJECT_NEEDS_GTT &&
132 USES_FULL_PPGTT(vm->dev)) {
133 ret = -EINVAL;
a25eebb0 134 goto err;
2c9f8d56
DV
135 }
136
6f65e29a
BW
137 /* If we have secure dispatch, or the userspace assures us that
138 * they know what they're doing, use the GGTT VM.
139 */
a7c1d426 140 if (((args->flags & I915_EXEC_SECURE) &&
6f65e29a
BW
141 (i == (args->buffer_count - 1))))
142 bind_vm = &dev_priv->gtt.base;
27173f1f 143
9ae9ab52
CW
144 obj = list_first_entry(&objects,
145 struct drm_i915_gem_object,
146 obj_exec_link);
147
e656a6cb
DV
148 /*
149 * NOTE: We can leak any vmas created here when something fails
150 * later on. But that's no issue since vma_unbind can deal with
151 * vmas which are not actually bound. And since only
152 * lookup_or_create exists as an interface to get at the vma
153 * from the (obj, vm) we don't run the risk of creating
154 * duplicated vmas for the same vm.
155 */
6f65e29a 156 vma = i915_gem_obj_lookup_or_create_vma(obj, bind_vm);
27173f1f 157 if (IS_ERR(vma)) {
27173f1f
BW
158 DRM_DEBUG("Failed to lookup VMA\n");
159 ret = PTR_ERR(vma);
9ae9ab52 160 goto err;
27173f1f
BW
161 }
162
9ae9ab52 163 /* Transfer ownership from the objects list to the vmas list. */
27173f1f 164 list_add_tail(&vma->exec_list, &eb->vmas);
9ae9ab52 165 list_del_init(&obj->obj_exec_link);
27173f1f
BW
166
167 vma->exec_entry = &exec[i];
eef90ccb 168 if (eb->and < 0) {
27173f1f 169 eb->lut[i] = vma;
eef90ccb
CW
170 } else {
171 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
27173f1f
BW
172 vma->exec_handle = handle;
173 hlist_add_head(&vma->exec_node,
eef90ccb
CW
174 &eb->buckets[handle & eb->and]);
175 }
27173f1f 176 ++i;
3b96eff4 177 }
3b96eff4 178
9ae9ab52 179 return 0;
27173f1f 180
27173f1f 181
9ae9ab52 182err:
27173f1f
BW
183 while (!list_empty(&objects)) {
184 obj = list_first_entry(&objects,
185 struct drm_i915_gem_object,
186 obj_exec_link);
187 list_del_init(&obj->obj_exec_link);
9ae9ab52 188 drm_gem_object_unreference(&obj->base);
27173f1f 189 }
9ae9ab52
CW
190 /*
191 * Objects already transfered to the vmas list will be unreferenced by
192 * eb_destroy.
193 */
194
27173f1f 195 return ret;
3b96eff4
CW
196}
197
27173f1f 198static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
67731b87 199{
eef90ccb
CW
200 if (eb->and < 0) {
201 if (handle >= -eb->and)
202 return NULL;
203 return eb->lut[handle];
204 } else {
205 struct hlist_head *head;
206 struct hlist_node *node;
67731b87 207
eef90ccb
CW
208 head = &eb->buckets[handle & eb->and];
209 hlist_for_each(node, head) {
27173f1f 210 struct i915_vma *vma;
67731b87 211
27173f1f
BW
212 vma = hlist_entry(node, struct i915_vma, exec_node);
213 if (vma->exec_handle == handle)
214 return vma;
eef90ccb
CW
215 }
216 return NULL;
217 }
67731b87
CW
218}
219
a415d355
CW
220static void
221i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
222{
223 struct drm_i915_gem_exec_object2 *entry;
224 struct drm_i915_gem_object *obj = vma->obj;
225
226 if (!drm_mm_node_allocated(&vma->node))
227 return;
228
229 entry = vma->exec_entry;
230
231 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
232 i915_gem_object_unpin_fence(obj);
233
234 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
3d7f0f9d 235 vma->pin_count--;
a415d355
CW
236
237 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
238}
239
240static void eb_destroy(struct eb_vmas *eb)
241{
27173f1f
BW
242 while (!list_empty(&eb->vmas)) {
243 struct i915_vma *vma;
bcffc3fa 244
27173f1f
BW
245 vma = list_first_entry(&eb->vmas,
246 struct i915_vma,
bcffc3fa 247 exec_list);
27173f1f 248 list_del_init(&vma->exec_list);
a415d355 249 i915_gem_execbuffer_unreserve_vma(vma);
27173f1f 250 drm_gem_object_unreference(&vma->obj->base);
bcffc3fa 251 }
67731b87
CW
252 kfree(eb);
253}
254
dabdfe02
CW
255static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
256{
2cc86b82
CW
257 return (HAS_LLC(obj->base.dev) ||
258 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
504c7267 259 !obj->map_and_fenceable ||
dabdfe02
CW
260 obj->cache_level != I915_CACHE_NONE);
261}
262
5032d871
RB
263static int
264relocate_entry_cpu(struct drm_i915_gem_object *obj,
265 struct drm_i915_gem_relocation_entry *reloc)
266{
3c94ceee 267 struct drm_device *dev = obj->base.dev;
5032d871
RB
268 uint32_t page_offset = offset_in_page(reloc->offset);
269 char *vaddr;
8b78f0e5 270 int ret;
5032d871 271
2cc86b82 272 ret = i915_gem_object_set_to_cpu_domain(obj, true);
5032d871
RB
273 if (ret)
274 return ret;
275
276 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
277 reloc->offset >> PAGE_SHIFT));
278 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
3c94ceee
BW
279
280 if (INTEL_INFO(dev)->gen >= 8) {
281 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
282
283 if (page_offset == 0) {
284 kunmap_atomic(vaddr);
285 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
286 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
287 }
288
289 *(uint32_t *)(vaddr + page_offset) = 0;
290 }
291
5032d871
RB
292 kunmap_atomic(vaddr);
293
294 return 0;
295}
296
297static int
298relocate_entry_gtt(struct drm_i915_gem_object *obj,
299 struct drm_i915_gem_relocation_entry *reloc)
300{
301 struct drm_device *dev = obj->base.dev;
302 struct drm_i915_private *dev_priv = dev->dev_private;
303 uint32_t __iomem *reloc_entry;
304 void __iomem *reloc_page;
8b78f0e5 305 int ret;
5032d871
RB
306
307 ret = i915_gem_object_set_to_gtt_domain(obj, true);
308 if (ret)
309 return ret;
310
311 ret = i915_gem_object_put_fence(obj);
312 if (ret)
313 return ret;
314
315 /* Map the page containing the relocation we're going to perform. */
316 reloc->offset += i915_gem_obj_ggtt_offset(obj);
317 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
318 reloc->offset & PAGE_MASK);
319 reloc_entry = (uint32_t __iomem *)
320 (reloc_page + offset_in_page(reloc->offset));
321 iowrite32(reloc->delta, reloc_entry);
3c94ceee
BW
322
323 if (INTEL_INFO(dev)->gen >= 8) {
324 reloc_entry += 1;
325
326 if (offset_in_page(reloc->offset + sizeof(uint32_t)) == 0) {
327 io_mapping_unmap_atomic(reloc_page);
328 reloc_page = io_mapping_map_atomic_wc(
329 dev_priv->gtt.mappable,
330 reloc->offset + sizeof(uint32_t));
331 reloc_entry = reloc_page;
332 }
333
334 iowrite32(0, reloc_entry);
335 }
336
5032d871
RB
337 io_mapping_unmap_atomic(reloc_page);
338
339 return 0;
340}
341
54cf91dc
CW
342static int
343i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
27173f1f 344 struct eb_vmas *eb,
3e7a0322 345 struct drm_i915_gem_relocation_entry *reloc)
54cf91dc
CW
346{
347 struct drm_device *dev = obj->base.dev;
348 struct drm_gem_object *target_obj;
149c8407 349 struct drm_i915_gem_object *target_i915_obj;
27173f1f 350 struct i915_vma *target_vma;
54cf91dc 351 uint32_t target_offset;
8b78f0e5 352 int ret;
54cf91dc 353
67731b87 354 /* we've already hold a reference to all valid objects */
27173f1f
BW
355 target_vma = eb_get_vma(eb, reloc->target_handle);
356 if (unlikely(target_vma == NULL))
54cf91dc 357 return -ENOENT;
27173f1f
BW
358 target_i915_obj = target_vma->obj;
359 target_obj = &target_vma->obj->base;
54cf91dc 360
5ce09725 361 target_offset = target_vma->node.start;
54cf91dc 362
e844b990
EA
363 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
364 * pipe_control writes because the gpu doesn't properly redirect them
365 * through the ppgtt for non_secure batchbuffers. */
366 if (unlikely(IS_GEN6(dev) &&
367 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
368 !target_i915_obj->has_global_gtt_mapping)) {
3e7a0322
BW
369 struct i915_vma *vma =
370 list_first_entry(&target_i915_obj->vma_list,
371 typeof(*vma), vma_link);
6f65e29a 372 vma->bind_vma(vma, target_i915_obj->cache_level, GLOBAL_BIND);
e844b990
EA
373 }
374
54cf91dc 375 /* Validate that the target is in a valid r/w GPU domain */
b8f7ab17 376 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
ff240199 377 DRM_DEBUG("reloc with multiple write domains: "
54cf91dc
CW
378 "obj %p target %d offset %d "
379 "read %08x write %08x",
380 obj, reloc->target_handle,
381 (int) reloc->offset,
382 reloc->read_domains,
383 reloc->write_domain);
8b78f0e5 384 return -EINVAL;
54cf91dc 385 }
4ca4a250
DV
386 if (unlikely((reloc->write_domain | reloc->read_domains)
387 & ~I915_GEM_GPU_DOMAINS)) {
ff240199 388 DRM_DEBUG("reloc with read/write non-GPU domains: "
54cf91dc
CW
389 "obj %p target %d offset %d "
390 "read %08x write %08x",
391 obj, reloc->target_handle,
392 (int) reloc->offset,
393 reloc->read_domains,
394 reloc->write_domain);
8b78f0e5 395 return -EINVAL;
54cf91dc 396 }
54cf91dc
CW
397
398 target_obj->pending_read_domains |= reloc->read_domains;
399 target_obj->pending_write_domain |= reloc->write_domain;
400
401 /* If the relocation already has the right value in it, no
402 * more work needs to be done.
403 */
404 if (target_offset == reloc->presumed_offset)
67731b87 405 return 0;
54cf91dc
CW
406
407 /* Check that the relocation address is valid... */
3c94ceee
BW
408 if (unlikely(reloc->offset >
409 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
ff240199 410 DRM_DEBUG("Relocation beyond object bounds: "
54cf91dc
CW
411 "obj %p target %d offset %d size %d.\n",
412 obj, reloc->target_handle,
413 (int) reloc->offset,
414 (int) obj->base.size);
8b78f0e5 415 return -EINVAL;
54cf91dc 416 }
b8f7ab17 417 if (unlikely(reloc->offset & 3)) {
ff240199 418 DRM_DEBUG("Relocation not 4-byte aligned: "
54cf91dc
CW
419 "obj %p target %d offset %d.\n",
420 obj, reloc->target_handle,
421 (int) reloc->offset);
8b78f0e5 422 return -EINVAL;
54cf91dc
CW
423 }
424
dabdfe02
CW
425 /* We can't wait for rendering with pagefaults disabled */
426 if (obj->active && in_atomic())
427 return -EFAULT;
428
54cf91dc 429 reloc->delta += target_offset;
5032d871
RB
430 if (use_cpu_reloc(obj))
431 ret = relocate_entry_cpu(obj, reloc);
432 else
433 ret = relocate_entry_gtt(obj, reloc);
54cf91dc 434
d4d36014
DV
435 if (ret)
436 return ret;
437
54cf91dc
CW
438 /* and update the user's relocation entry */
439 reloc->presumed_offset = target_offset;
440
67731b87 441 return 0;
54cf91dc
CW
442}
443
444static int
27173f1f
BW
445i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
446 struct eb_vmas *eb)
54cf91dc 447{
1d83f442
CW
448#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
449 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
54cf91dc 450 struct drm_i915_gem_relocation_entry __user *user_relocs;
27173f1f 451 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
1d83f442 452 int remain, ret;
54cf91dc 453
2bb4629a 454 user_relocs = to_user_ptr(entry->relocs_ptr);
54cf91dc 455
1d83f442
CW
456 remain = entry->relocation_count;
457 while (remain) {
458 struct drm_i915_gem_relocation_entry *r = stack_reloc;
459 int count = remain;
460 if (count > ARRAY_SIZE(stack_reloc))
461 count = ARRAY_SIZE(stack_reloc);
462 remain -= count;
463
464 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
54cf91dc
CW
465 return -EFAULT;
466
1d83f442
CW
467 do {
468 u64 offset = r->presumed_offset;
54cf91dc 469
3e7a0322 470 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
1d83f442
CW
471 if (ret)
472 return ret;
473
474 if (r->presumed_offset != offset &&
475 __copy_to_user_inatomic(&user_relocs->presumed_offset,
476 &r->presumed_offset,
477 sizeof(r->presumed_offset))) {
478 return -EFAULT;
479 }
480
481 user_relocs++;
482 r++;
483 } while (--count);
54cf91dc
CW
484 }
485
486 return 0;
1d83f442 487#undef N_RELOC
54cf91dc
CW
488}
489
490static int
27173f1f
BW
491i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
492 struct eb_vmas *eb,
493 struct drm_i915_gem_relocation_entry *relocs)
54cf91dc 494{
27173f1f 495 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
54cf91dc
CW
496 int i, ret;
497
498 for (i = 0; i < entry->relocation_count; i++) {
3e7a0322 499 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
54cf91dc
CW
500 if (ret)
501 return ret;
502 }
503
504 return 0;
505}
506
507static int
17601cbc 508i915_gem_execbuffer_relocate(struct eb_vmas *eb)
54cf91dc 509{
27173f1f 510 struct i915_vma *vma;
d4aeee77
CW
511 int ret = 0;
512
513 /* This is the fast path and we cannot handle a pagefault whilst
514 * holding the struct mutex lest the user pass in the relocations
515 * contained within a mmaped bo. For in such a case we, the page
516 * fault handler would call i915_gem_fault() and we would try to
517 * acquire the struct mutex again. Obviously this is bad and so
518 * lockdep complains vehemently.
519 */
520 pagefault_disable();
27173f1f
BW
521 list_for_each_entry(vma, &eb->vmas, exec_list) {
522 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
54cf91dc 523 if (ret)
d4aeee77 524 break;
54cf91dc 525 }
d4aeee77 526 pagefault_enable();
54cf91dc 527
d4aeee77 528 return ret;
54cf91dc
CW
529}
530
dabdfe02 531static int
27173f1f 532need_reloc_mappable(struct i915_vma *vma)
dabdfe02 533{
27173f1f
BW
534 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
535 return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
536 i915_is_ggtt(vma->vm);
dabdfe02
CW
537}
538
1690e1eb 539static int
27173f1f
BW
540i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
541 struct intel_ring_buffer *ring,
542 bool *need_reloc)
1690e1eb 543{
6f65e29a 544 struct drm_i915_gem_object *obj = vma->obj;
27173f1f 545 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
1690e1eb 546 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
1ec9e26d
DV
547 bool need_fence;
548 unsigned flags;
1690e1eb
CW
549 int ret;
550
1ec9e26d
DV
551 flags = 0;
552
1690e1eb
CW
553 need_fence =
554 has_fenced_gpu_access &&
555 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
556 obj->tiling_mode != I915_TILING_NONE;
1ec9e26d
DV
557 if (need_fence || need_reloc_mappable(vma))
558 flags |= PIN_MAPPABLE;
1690e1eb 559
1ec9e26d 560 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
bf3d149b 561 flags |= PIN_GLOBAL;
1ec9e26d
DV
562
563 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
1690e1eb
CW
564 if (ret)
565 return ret;
566
7788a765
CW
567 entry->flags |= __EXEC_OBJECT_HAS_PIN;
568
1690e1eb
CW
569 if (has_fenced_gpu_access) {
570 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
06d98131 571 ret = i915_gem_object_get_fence(obj);
9a5a53b3 572 if (ret)
7788a765 573 return ret;
1690e1eb 574
9a5a53b3 575 if (i915_gem_object_pin_fence(obj))
1690e1eb 576 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
9a5a53b3 577
7dd49065 578 obj->pending_fenced_gpu_access = true;
1690e1eb 579 }
1690e1eb
CW
580 }
581
27173f1f
BW
582 if (entry->offset != vma->node.start) {
583 entry->offset = vma->node.start;
ed5982e6
DV
584 *need_reloc = true;
585 }
586
587 if (entry->flags & EXEC_OBJECT_WRITE) {
588 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
589 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
590 }
591
1690e1eb 592 return 0;
7788a765 593}
1690e1eb 594
54cf91dc 595static int
d9e86c0e 596i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
27173f1f 597 struct list_head *vmas,
ed5982e6 598 bool *need_relocs)
54cf91dc 599{
432e58ed 600 struct drm_i915_gem_object *obj;
27173f1f 601 struct i915_vma *vma;
68c8c17f 602 struct i915_address_space *vm;
27173f1f 603 struct list_head ordered_vmas;
7788a765
CW
604 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
605 int retry;
6fe4f140 606
68c8c17f
BW
607 if (list_empty(vmas))
608 return 0;
609
610 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
611
27173f1f
BW
612 INIT_LIST_HEAD(&ordered_vmas);
613 while (!list_empty(vmas)) {
6fe4f140
CW
614 struct drm_i915_gem_exec_object2 *entry;
615 bool need_fence, need_mappable;
616
27173f1f
BW
617 vma = list_first_entry(vmas, struct i915_vma, exec_list);
618 obj = vma->obj;
619 entry = vma->exec_entry;
6fe4f140
CW
620
621 need_fence =
622 has_fenced_gpu_access &&
623 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
624 obj->tiling_mode != I915_TILING_NONE;
27173f1f 625 need_mappable = need_fence || need_reloc_mappable(vma);
6fe4f140
CW
626
627 if (need_mappable)
27173f1f 628 list_move(&vma->exec_list, &ordered_vmas);
6fe4f140 629 else
27173f1f 630 list_move_tail(&vma->exec_list, &ordered_vmas);
595dad76 631
ed5982e6 632 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
595dad76 633 obj->base.pending_write_domain = 0;
016fd0c1 634 obj->pending_fenced_gpu_access = false;
6fe4f140 635 }
27173f1f 636 list_splice(&ordered_vmas, vmas);
54cf91dc
CW
637
638 /* Attempt to pin all of the buffers into the GTT.
639 * This is done in 3 phases:
640 *
641 * 1a. Unbind all objects that do not match the GTT constraints for
642 * the execbuffer (fenceable, mappable, alignment etc).
643 * 1b. Increment pin count for already bound objects.
644 * 2. Bind new objects.
645 * 3. Decrement pin count.
646 *
7788a765 647 * This avoid unnecessary unbinding of later objects in order to make
54cf91dc
CW
648 * room for the earlier objects *unless* we need to defragment.
649 */
650 retry = 0;
651 do {
7788a765 652 int ret = 0;
54cf91dc
CW
653
654 /* Unbind any ill-fitting objects or pin. */
27173f1f
BW
655 list_for_each_entry(vma, vmas, exec_list) {
656 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
54cf91dc 657 bool need_fence, need_mappable;
1690e1eb 658
27173f1f
BW
659 obj = vma->obj;
660
661 if (!drm_mm_node_allocated(&vma->node))
54cf91dc
CW
662 continue;
663
664 need_fence =
9b3826bf 665 has_fenced_gpu_access &&
54cf91dc
CW
666 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
667 obj->tiling_mode != I915_TILING_NONE;
27173f1f 668 need_mappable = need_fence || need_reloc_mappable(vma);
54cf91dc 669
28d6a7bf 670 WARN_ON((need_mappable || need_fence) &&
27173f1f 671 !i915_is_ggtt(vma->vm));
28d6a7bf 672
f343c5f6 673 if ((entry->alignment &&
27173f1f 674 vma->node.start & (entry->alignment - 1)) ||
54cf91dc 675 (need_mappable && !obj->map_and_fenceable))
27173f1f 676 ret = i915_vma_unbind(vma);
54cf91dc 677 else
27173f1f 678 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
432e58ed 679 if (ret)
54cf91dc 680 goto err;
54cf91dc
CW
681 }
682
683 /* Bind fresh objects */
27173f1f
BW
684 list_for_each_entry(vma, vmas, exec_list) {
685 if (drm_mm_node_allocated(&vma->node))
1690e1eb 686 continue;
54cf91dc 687
27173f1f 688 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
7788a765
CW
689 if (ret)
690 goto err;
54cf91dc
CW
691 }
692
a415d355 693err:
6c085a72 694 if (ret != -ENOSPC || retry++)
54cf91dc
CW
695 return ret;
696
a415d355
CW
697 /* Decrement pin count for bound objects */
698 list_for_each_entry(vma, vmas, exec_list)
699 i915_gem_execbuffer_unreserve_vma(vma);
700
68c8c17f 701 ret = i915_gem_evict_vm(vm, true);
54cf91dc
CW
702 if (ret)
703 return ret;
54cf91dc
CW
704 } while (1);
705}
706
707static int
708i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
ed5982e6 709 struct drm_i915_gem_execbuffer2 *args,
54cf91dc 710 struct drm_file *file,
d9e86c0e 711 struct intel_ring_buffer *ring,
27173f1f
BW
712 struct eb_vmas *eb,
713 struct drm_i915_gem_exec_object2 *exec)
54cf91dc
CW
714{
715 struct drm_i915_gem_relocation_entry *reloc;
27173f1f
BW
716 struct i915_address_space *vm;
717 struct i915_vma *vma;
ed5982e6 718 bool need_relocs;
dd6864a4 719 int *reloc_offset;
54cf91dc 720 int i, total, ret;
b205ca57 721 unsigned count = args->buffer_count;
54cf91dc 722
27173f1f
BW
723 if (WARN_ON(list_empty(&eb->vmas)))
724 return 0;
725
726 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
727
67731b87 728 /* We may process another execbuffer during the unlock... */
27173f1f
BW
729 while (!list_empty(&eb->vmas)) {
730 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
731 list_del_init(&vma->exec_list);
a415d355 732 i915_gem_execbuffer_unreserve_vma(vma);
27173f1f 733 drm_gem_object_unreference(&vma->obj->base);
67731b87
CW
734 }
735
54cf91dc
CW
736 mutex_unlock(&dev->struct_mutex);
737
738 total = 0;
739 for (i = 0; i < count; i++)
432e58ed 740 total += exec[i].relocation_count;
54cf91dc 741
dd6864a4 742 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
54cf91dc 743 reloc = drm_malloc_ab(total, sizeof(*reloc));
dd6864a4
CW
744 if (reloc == NULL || reloc_offset == NULL) {
745 drm_free_large(reloc);
746 drm_free_large(reloc_offset);
54cf91dc
CW
747 mutex_lock(&dev->struct_mutex);
748 return -ENOMEM;
749 }
750
751 total = 0;
752 for (i = 0; i < count; i++) {
753 struct drm_i915_gem_relocation_entry __user *user_relocs;
262b6d36
CW
754 u64 invalid_offset = (u64)-1;
755 int j;
54cf91dc 756
2bb4629a 757 user_relocs = to_user_ptr(exec[i].relocs_ptr);
54cf91dc
CW
758
759 if (copy_from_user(reloc+total, user_relocs,
432e58ed 760 exec[i].relocation_count * sizeof(*reloc))) {
54cf91dc
CW
761 ret = -EFAULT;
762 mutex_lock(&dev->struct_mutex);
763 goto err;
764 }
765
262b6d36
CW
766 /* As we do not update the known relocation offsets after
767 * relocating (due to the complexities in lock handling),
768 * we need to mark them as invalid now so that we force the
769 * relocation processing next time. Just in case the target
770 * object is evicted and then rebound into its old
771 * presumed_offset before the next execbuffer - if that
772 * happened we would make the mistake of assuming that the
773 * relocations were valid.
774 */
775 for (j = 0; j < exec[i].relocation_count; j++) {
776 if (copy_to_user(&user_relocs[j].presumed_offset,
777 &invalid_offset,
778 sizeof(invalid_offset))) {
779 ret = -EFAULT;
780 mutex_lock(&dev->struct_mutex);
781 goto err;
782 }
783 }
784
dd6864a4 785 reloc_offset[i] = total;
432e58ed 786 total += exec[i].relocation_count;
54cf91dc
CW
787 }
788
789 ret = i915_mutex_lock_interruptible(dev);
790 if (ret) {
791 mutex_lock(&dev->struct_mutex);
792 goto err;
793 }
794
67731b87 795 /* reacquire the objects */
67731b87 796 eb_reset(eb);
27173f1f 797 ret = eb_lookup_vmas(eb, exec, args, vm, file);
3b96eff4
CW
798 if (ret)
799 goto err;
67731b87 800
ed5982e6 801 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
27173f1f 802 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
54cf91dc
CW
803 if (ret)
804 goto err;
805
27173f1f
BW
806 list_for_each_entry(vma, &eb->vmas, exec_list) {
807 int offset = vma->exec_entry - exec;
808 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
809 reloc + reloc_offset[offset]);
54cf91dc
CW
810 if (ret)
811 goto err;
54cf91dc
CW
812 }
813
814 /* Leave the user relocations as are, this is the painfully slow path,
815 * and we want to avoid the complication of dropping the lock whilst
816 * having buffers reserved in the aperture and so causing spurious
817 * ENOSPC for random operations.
818 */
819
820err:
821 drm_free_large(reloc);
dd6864a4 822 drm_free_large(reloc_offset);
54cf91dc
CW
823 return ret;
824}
825
54cf91dc 826static int
432e58ed 827i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
27173f1f 828 struct list_head *vmas)
54cf91dc 829{
27173f1f 830 struct i915_vma *vma;
6ac42f41 831 uint32_t flush_domains = 0;
000433b6 832 bool flush_chipset = false;
432e58ed 833 int ret;
54cf91dc 834
27173f1f
BW
835 list_for_each_entry(vma, vmas, exec_list) {
836 struct drm_i915_gem_object *obj = vma->obj;
6ac42f41 837 ret = i915_gem_object_sync(obj, ring);
c59a333f
CW
838 if (ret)
839 return ret;
6ac42f41
DV
840
841 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
000433b6 842 flush_chipset |= i915_gem_clflush_object(obj, false);
6ac42f41 843
6ac42f41 844 flush_domains |= obj->base.write_domain;
c59a333f
CW
845 }
846
000433b6 847 if (flush_chipset)
e76e9aeb 848 i915_gem_chipset_flush(ring->dev);
6ac42f41
DV
849
850 if (flush_domains & I915_GEM_DOMAIN_GTT)
851 wmb();
852
09cf7c9a
CW
853 /* Unconditionally invalidate gpu caches and ensure that we do flush
854 * any residual writes from the previous batch.
855 */
a7b9761d 856 return intel_ring_invalidate_all_caches(ring);
54cf91dc
CW
857}
858
432e58ed
CW
859static bool
860i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
54cf91dc 861{
ed5982e6
DV
862 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
863 return false;
864
432e58ed 865 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
54cf91dc
CW
866}
867
868static int
869validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
870 int count)
871{
872 int i;
b205ca57
DV
873 unsigned relocs_total = 0;
874 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
54cf91dc
CW
875
876 for (i = 0; i < count; i++) {
2bb4629a 877 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
54cf91dc
CW
878 int length; /* limited by fault_in_pages_readable() */
879
ed5982e6
DV
880 if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
881 return -EINVAL;
882
3118a4f6
KC
883 /* First check for malicious input causing overflow in
884 * the worst case where we need to allocate the entire
885 * relocation tree as a single array.
886 */
887 if (exec[i].relocation_count > relocs_max - relocs_total)
54cf91dc 888 return -EINVAL;
3118a4f6 889 relocs_total += exec[i].relocation_count;
54cf91dc
CW
890
891 length = exec[i].relocation_count *
892 sizeof(struct drm_i915_gem_relocation_entry);
30587535
KC
893 /*
894 * We must check that the entire relocation array is safe
895 * to read, but since we may need to update the presumed
896 * offsets during execution, check for full write access.
897 */
54cf91dc
CW
898 if (!access_ok(VERIFY_WRITE, ptr, length))
899 return -EFAULT;
900
d330a953 901 if (likely(!i915.prefault_disable)) {
0b74b508
XZ
902 if (fault_in_multipages_readable(ptr, length))
903 return -EFAULT;
904 }
54cf91dc
CW
905 }
906
907 return 0;
908}
909
41bde553 910static struct i915_hw_context *
d299cce7 911i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
7c9c4b8f 912 struct intel_ring_buffer *ring, const u32 ctx_id)
d299cce7 913{
41bde553 914 struct i915_hw_context *ctx = NULL;
d299cce7
MK
915 struct i915_ctx_hang_stats *hs;
916
7c9c4b8f
DV
917 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_ID)
918 return ERR_PTR(-EINVAL);
919
41bde553 920 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
72ad5c45 921 if (IS_ERR(ctx))
41bde553 922 return ctx;
d299cce7 923
41bde553 924 hs = &ctx->hang_stats;
d299cce7
MK
925 if (hs->banned) {
926 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
41bde553 927 return ERR_PTR(-EIO);
d299cce7
MK
928 }
929
41bde553 930 return ctx;
d299cce7
MK
931}
932
432e58ed 933static void
27173f1f 934i915_gem_execbuffer_move_to_active(struct list_head *vmas,
9d773091 935 struct intel_ring_buffer *ring)
432e58ed 936{
27173f1f 937 struct i915_vma *vma;
432e58ed 938
27173f1f
BW
939 list_for_each_entry(vma, vmas, exec_list) {
940 struct drm_i915_gem_object *obj = vma->obj;
69c2fc89
CW
941 u32 old_read = obj->base.read_domains;
942 u32 old_write = obj->base.write_domain;
db53a302 943
432e58ed 944 obj->base.write_domain = obj->base.pending_write_domain;
ed5982e6
DV
945 if (obj->base.write_domain == 0)
946 obj->base.pending_read_domains |= obj->base.read_domains;
947 obj->base.read_domains = obj->base.pending_read_domains;
432e58ed
CW
948 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
949
e2d05a8b 950 i915_vma_move_to_active(vma, ring);
432e58ed
CW
951 if (obj->base.write_domain) {
952 obj->dirty = 1;
9d773091 953 obj->last_write_seqno = intel_ring_get_seqno(ring);
d7f46fc4
BW
954 /* check for potential scanout */
955 if (i915_gem_obj_ggtt_bound(obj) &&
956 i915_gem_obj_to_ggtt(obj)->pin_count)
c65355bb 957 intel_mark_fb_busy(obj, ring);
432e58ed
CW
958 }
959
db53a302 960 trace_i915_gem_object_change_domain(obj, old_read, old_write);
432e58ed
CW
961 }
962}
963
54cf91dc
CW
964static void
965i915_gem_execbuffer_retire_commands(struct drm_device *dev,
432e58ed 966 struct drm_file *file,
7d736f4f
MK
967 struct intel_ring_buffer *ring,
968 struct drm_i915_gem_object *obj)
54cf91dc 969{
cc889e0f
DV
970 /* Unconditionally force add_request to emit a full flush. */
971 ring->gpu_caches_dirty = true;
54cf91dc 972
432e58ed 973 /* Add a breadcrumb for the completion of the batch buffer */
7d736f4f 974 (void)__i915_add_request(ring, file, obj, NULL);
432e58ed 975}
54cf91dc 976
ae662d31
EA
977static int
978i915_reset_gen7_sol_offsets(struct drm_device *dev,
979 struct intel_ring_buffer *ring)
980{
50227e1c 981 struct drm_i915_private *dev_priv = dev->dev_private;
ae662d31
EA
982 int ret, i;
983
984 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
985 return 0;
986
987 ret = intel_ring_begin(ring, 4 * 3);
988 if (ret)
989 return ret;
990
991 for (i = 0; i < 4; i++) {
992 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
993 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
994 intel_ring_emit(ring, 0);
995 }
996
997 intel_ring_advance(ring);
998
999 return 0;
1000}
1001
54cf91dc
CW
1002static int
1003i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1004 struct drm_file *file,
1005 struct drm_i915_gem_execbuffer2 *args,
41bde553 1006 struct drm_i915_gem_exec_object2 *exec)
54cf91dc 1007{
50227e1c 1008 struct drm_i915_private *dev_priv = dev->dev_private;
27173f1f 1009 struct eb_vmas *eb;
54cf91dc
CW
1010 struct drm_i915_gem_object *batch_obj;
1011 struct drm_clip_rect *cliprects = NULL;
54cf91dc 1012 struct intel_ring_buffer *ring;
41bde553
BW
1013 struct i915_hw_context *ctx;
1014 struct i915_address_space *vm;
d299cce7 1015 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
7e0d96bc 1016 u32 exec_start = args->batch_start_offset, exec_len;
ed5982e6 1017 u32 mask, flags;
72bfa19c 1018 int ret, mode, i;
ed5982e6 1019 bool need_relocs;
54cf91dc 1020
ed5982e6 1021 if (!i915_gem_check_execbuffer(args))
432e58ed 1022 return -EINVAL;
432e58ed
CW
1023
1024 ret = validate_exec_list(exec, args->buffer_count);
54cf91dc
CW
1025 if (ret)
1026 return ret;
1027
d7d4eedd
CW
1028 flags = 0;
1029 if (args->flags & I915_EXEC_SECURE) {
1030 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1031 return -EPERM;
1032
1033 flags |= I915_DISPATCH_SECURE;
1034 }
b45305fc
DV
1035 if (args->flags & I915_EXEC_IS_PINNED)
1036 flags |= I915_DISPATCH_PINNED;
d7d4eedd 1037
ca01b12b 1038 if ((args->flags & I915_EXEC_RING_MASK) > I915_NUM_RINGS) {
ff240199 1039 DRM_DEBUG("execbuf with unknown ring: %d\n",
54cf91dc
CW
1040 (int)(args->flags & I915_EXEC_RING_MASK));
1041 return -EINVAL;
1042 }
ca01b12b
BW
1043
1044 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1045 ring = &dev_priv->ring[RCS];
1046 else
1047 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1048
a15817cf
CW
1049 if (!intel_ring_initialized(ring)) {
1050 DRM_DEBUG("execbuf with invalid ring: %d\n",
1051 (int)(args->flags & I915_EXEC_RING_MASK));
1052 return -EINVAL;
1053 }
54cf91dc 1054
72bfa19c 1055 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
84f9f938 1056 mask = I915_EXEC_CONSTANTS_MASK;
72bfa19c
CW
1057 switch (mode) {
1058 case I915_EXEC_CONSTANTS_REL_GENERAL:
1059 case I915_EXEC_CONSTANTS_ABSOLUTE:
1060 case I915_EXEC_CONSTANTS_REL_SURFACE:
1061 if (ring == &dev_priv->ring[RCS] &&
1062 mode != dev_priv->relative_constants_mode) {
1063 if (INTEL_INFO(dev)->gen < 4)
1064 return -EINVAL;
1065
1066 if (INTEL_INFO(dev)->gen > 5 &&
1067 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1068 return -EINVAL;
84f9f938
BW
1069
1070 /* The HW changed the meaning on this bit on gen6 */
1071 if (INTEL_INFO(dev)->gen >= 6)
1072 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
72bfa19c
CW
1073 }
1074 break;
1075 default:
ff240199 1076 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
72bfa19c
CW
1077 return -EINVAL;
1078 }
1079
54cf91dc 1080 if (args->buffer_count < 1) {
ff240199 1081 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
54cf91dc
CW
1082 return -EINVAL;
1083 }
54cf91dc
CW
1084
1085 if (args->num_cliprects != 0) {
1ec14ad3 1086 if (ring != &dev_priv->ring[RCS]) {
ff240199 1087 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
c4e7a414
CW
1088 return -EINVAL;
1089 }
1090
6ebebc92
DV
1091 if (INTEL_INFO(dev)->gen >= 5) {
1092 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1093 return -EINVAL;
1094 }
1095
44afb3a0
XW
1096 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1097 DRM_DEBUG("execbuf with %u cliprects\n",
1098 args->num_cliprects);
1099 return -EINVAL;
1100 }
5e13a0c5 1101
a1e22653
DV
1102 cliprects = kcalloc(args->num_cliprects,
1103 sizeof(*cliprects),
54cf91dc
CW
1104 GFP_KERNEL);
1105 if (cliprects == NULL) {
1106 ret = -ENOMEM;
1107 goto pre_mutex_err;
1108 }
1109
432e58ed 1110 if (copy_from_user(cliprects,
2bb4629a
VS
1111 to_user_ptr(args->cliprects_ptr),
1112 sizeof(*cliprects)*args->num_cliprects)) {
54cf91dc
CW
1113 ret = -EFAULT;
1114 goto pre_mutex_err;
1115 }
1116 }
1117
f65c9168
PZ
1118 intel_runtime_pm_get(dev_priv);
1119
54cf91dc
CW
1120 ret = i915_mutex_lock_interruptible(dev);
1121 if (ret)
1122 goto pre_mutex_err;
1123
db1b76ca 1124 if (dev_priv->ums.mm_suspended) {
54cf91dc
CW
1125 mutex_unlock(&dev->struct_mutex);
1126 ret = -EBUSY;
1127 goto pre_mutex_err;
1128 }
1129
7c9c4b8f 1130 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
72ad5c45 1131 if (IS_ERR(ctx)) {
d299cce7 1132 mutex_unlock(&dev->struct_mutex);
41bde553 1133 ret = PTR_ERR(ctx);
d299cce7 1134 goto pre_mutex_err;
935f38d6 1135 }
41bde553
BW
1136
1137 i915_gem_context_reference(ctx);
1138
7e0d96bc
BW
1139 vm = ctx->vm;
1140 if (!USES_FULL_PPGTT(dev))
1141 vm = &dev_priv->gtt.base;
d299cce7 1142
17601cbc 1143 eb = eb_create(args);
67731b87 1144 if (eb == NULL) {
935f38d6 1145 i915_gem_context_unreference(ctx);
67731b87
CW
1146 mutex_unlock(&dev->struct_mutex);
1147 ret = -ENOMEM;
1148 goto pre_mutex_err;
1149 }
1150
54cf91dc 1151 /* Look up object handles */
27173f1f 1152 ret = eb_lookup_vmas(eb, exec, args, vm, file);
3b96eff4
CW
1153 if (ret)
1154 goto err;
54cf91dc 1155
6fe4f140 1156 /* take note of the batch buffer before we might reorder the lists */
27173f1f 1157 batch_obj = list_entry(eb->vmas.prev, struct i915_vma, exec_list)->obj;
6fe4f140 1158
54cf91dc 1159 /* Move the objects en-masse into the GTT, evicting if necessary. */
ed5982e6 1160 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
27173f1f 1161 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
54cf91dc
CW
1162 if (ret)
1163 goto err;
1164
1165 /* The objects are in their final locations, apply the relocations. */
ed5982e6 1166 if (need_relocs)
17601cbc 1167 ret = i915_gem_execbuffer_relocate(eb);
54cf91dc
CW
1168 if (ret) {
1169 if (ret == -EFAULT) {
ed5982e6 1170 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
27173f1f 1171 eb, exec);
54cf91dc
CW
1172 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1173 }
1174 if (ret)
1175 goto err;
1176 }
1177
1178 /* Set the pending read domains for the batch buffer to COMMAND */
54cf91dc 1179 if (batch_obj->base.pending_write_domain) {
ff240199 1180 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
54cf91dc
CW
1181 ret = -EINVAL;
1182 goto err;
1183 }
1184 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1185
351e3db2
BV
1186 if (i915_needs_cmd_parser(ring)) {
1187 ret = i915_parse_cmds(ring,
1188 batch_obj,
1189 args->batch_start_offset,
1190 file->is_master);
1191 if (ret)
1192 goto err;
1193
1194 /*
1195 * XXX: Actually do this when enabling batch copy...
1196 *
1197 * Set the DISPATCH_SECURE bit to remove the NON_SECURE bit
1198 * from MI_BATCH_BUFFER_START commands issued in the
1199 * dispatch_execbuffer implementations. We specifically don't
1200 * want that set when the command parser is enabled.
1201 */
1202 }
1203
d7d4eedd
CW
1204 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1205 * batch" bit. Hence we need to pin secure batches into the global gtt.
28cf5415 1206 * hsw should have this fixed, but bdw mucks it up again. */
6f65e29a
BW
1207 if (flags & I915_DISPATCH_SECURE &&
1208 !batch_obj->has_global_gtt_mapping) {
1209 /* When we have multiple VMs, we'll need to make sure that we
1210 * allocate space first */
1211 struct i915_vma *vma = i915_gem_obj_to_ggtt(batch_obj);
1212 BUG_ON(!vma);
1213 vma->bind_vma(vma, batch_obj->cache_level, GLOBAL_BIND);
1214 }
d7d4eedd 1215
7e0d96bc
BW
1216 if (flags & I915_DISPATCH_SECURE)
1217 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
1218 else
1219 exec_start += i915_gem_obj_offset(batch_obj, vm);
d7d4eedd 1220
27173f1f 1221 ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
432e58ed 1222 if (ret)
54cf91dc 1223 goto err;
54cf91dc 1224
691e6415 1225 ret = i915_switch_context(ring, ctx);
0da5cec1
EA
1226 if (ret)
1227 goto err;
1228
e2971bda
BW
1229 if (ring == &dev_priv->ring[RCS] &&
1230 mode != dev_priv->relative_constants_mode) {
1231 ret = intel_ring_begin(ring, 4);
1232 if (ret)
1233 goto err;
1234
1235 intel_ring_emit(ring, MI_NOOP);
1236 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1237 intel_ring_emit(ring, INSTPM);
84f9f938 1238 intel_ring_emit(ring, mask << 16 | mode);
e2971bda
BW
1239 intel_ring_advance(ring);
1240
1241 dev_priv->relative_constants_mode = mode;
1242 }
1243
ae662d31
EA
1244 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1245 ret = i915_reset_gen7_sol_offsets(dev, ring);
1246 if (ret)
1247 goto err;
1248 }
1249
7e0d96bc 1250
c4e7a414
CW
1251 exec_len = args->batch_len;
1252 if (cliprects) {
1253 for (i = 0; i < args->num_cliprects; i++) {
1254 ret = i915_emit_box(dev, &cliprects[i],
1255 args->DR1, args->DR4);
1256 if (ret)
1257 goto err;
1258
1259 ret = ring->dispatch_execbuffer(ring,
d7d4eedd
CW
1260 exec_start, exec_len,
1261 flags);
c4e7a414
CW
1262 if (ret)
1263 goto err;
1264 }
1265 } else {
d7d4eedd
CW
1266 ret = ring->dispatch_execbuffer(ring,
1267 exec_start, exec_len,
1268 flags);
c4e7a414
CW
1269 if (ret)
1270 goto err;
1271 }
54cf91dc 1272
9d773091
CW
1273 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1274
27173f1f 1275 i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
7d736f4f 1276 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
54cf91dc
CW
1277
1278err:
41bde553
BW
1279 /* the request owns the ref now */
1280 i915_gem_context_unreference(ctx);
67731b87 1281 eb_destroy(eb);
54cf91dc
CW
1282
1283 mutex_unlock(&dev->struct_mutex);
1284
1285pre_mutex_err:
54cf91dc 1286 kfree(cliprects);
f65c9168
PZ
1287
1288 /* intel_gpu_busy should also get a ref, so it will free when the device
1289 * is really idle. */
1290 intel_runtime_pm_put(dev_priv);
54cf91dc
CW
1291 return ret;
1292}
1293
1294/*
1295 * Legacy execbuffer just creates an exec2 list from the original exec object
1296 * list array and passes it to the real function.
1297 */
1298int
1299i915_gem_execbuffer(struct drm_device *dev, void *data,
1300 struct drm_file *file)
1301{
1302 struct drm_i915_gem_execbuffer *args = data;
1303 struct drm_i915_gem_execbuffer2 exec2;
1304 struct drm_i915_gem_exec_object *exec_list = NULL;
1305 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1306 int ret, i;
1307
54cf91dc 1308 if (args->buffer_count < 1) {
ff240199 1309 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
54cf91dc
CW
1310 return -EINVAL;
1311 }
1312
1313 /* Copy in the exec list from userland */
1314 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1315 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1316 if (exec_list == NULL || exec2_list == NULL) {
ff240199 1317 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
54cf91dc
CW
1318 args->buffer_count);
1319 drm_free_large(exec_list);
1320 drm_free_large(exec2_list);
1321 return -ENOMEM;
1322 }
1323 ret = copy_from_user(exec_list,
2bb4629a 1324 to_user_ptr(args->buffers_ptr),
54cf91dc
CW
1325 sizeof(*exec_list) * args->buffer_count);
1326 if (ret != 0) {
ff240199 1327 DRM_DEBUG("copy %d exec entries failed %d\n",
54cf91dc
CW
1328 args->buffer_count, ret);
1329 drm_free_large(exec_list);
1330 drm_free_large(exec2_list);
1331 return -EFAULT;
1332 }
1333
1334 for (i = 0; i < args->buffer_count; i++) {
1335 exec2_list[i].handle = exec_list[i].handle;
1336 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1337 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1338 exec2_list[i].alignment = exec_list[i].alignment;
1339 exec2_list[i].offset = exec_list[i].offset;
1340 if (INTEL_INFO(dev)->gen < 4)
1341 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1342 else
1343 exec2_list[i].flags = 0;
1344 }
1345
1346 exec2.buffers_ptr = args->buffers_ptr;
1347 exec2.buffer_count = args->buffer_count;
1348 exec2.batch_start_offset = args->batch_start_offset;
1349 exec2.batch_len = args->batch_len;
1350 exec2.DR1 = args->DR1;
1351 exec2.DR4 = args->DR4;
1352 exec2.num_cliprects = args->num_cliprects;
1353 exec2.cliprects_ptr = args->cliprects_ptr;
1354 exec2.flags = I915_EXEC_RENDER;
6e0a69db 1355 i915_execbuffer2_set_context_id(exec2, 0);
54cf91dc 1356
41bde553 1357 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
54cf91dc
CW
1358 if (!ret) {
1359 /* Copy the new buffer offsets back to the user's exec list. */
1360 for (i = 0; i < args->buffer_count; i++)
1361 exec_list[i].offset = exec2_list[i].offset;
1362 /* ... and back out to userspace */
2bb4629a 1363 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
54cf91dc
CW
1364 exec_list,
1365 sizeof(*exec_list) * args->buffer_count);
1366 if (ret) {
1367 ret = -EFAULT;
ff240199 1368 DRM_DEBUG("failed to copy %d exec entries "
54cf91dc
CW
1369 "back to user (%d)\n",
1370 args->buffer_count, ret);
1371 }
1372 }
1373
1374 drm_free_large(exec_list);
1375 drm_free_large(exec2_list);
1376 return ret;
1377}
1378
1379int
1380i915_gem_execbuffer2(struct drm_device *dev, void *data,
1381 struct drm_file *file)
1382{
1383 struct drm_i915_gem_execbuffer2 *args = data;
1384 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1385 int ret;
1386
ed8cd3b2
XW
1387 if (args->buffer_count < 1 ||
1388 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
ff240199 1389 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
54cf91dc
CW
1390 return -EINVAL;
1391 }
1392
8408c282 1393 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
419fa72a 1394 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
8408c282
CW
1395 if (exec2_list == NULL)
1396 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1397 args->buffer_count);
54cf91dc 1398 if (exec2_list == NULL) {
ff240199 1399 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
54cf91dc
CW
1400 args->buffer_count);
1401 return -ENOMEM;
1402 }
1403 ret = copy_from_user(exec2_list,
2bb4629a 1404 to_user_ptr(args->buffers_ptr),
54cf91dc
CW
1405 sizeof(*exec2_list) * args->buffer_count);
1406 if (ret != 0) {
ff240199 1407 DRM_DEBUG("copy %d exec entries failed %d\n",
54cf91dc
CW
1408 args->buffer_count, ret);
1409 drm_free_large(exec2_list);
1410 return -EFAULT;
1411 }
1412
41bde553 1413 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
54cf91dc
CW
1414 if (!ret) {
1415 /* Copy the new buffer offsets back to the user's exec list. */
2bb4629a 1416 ret = copy_to_user(to_user_ptr(args->buffers_ptr),
54cf91dc
CW
1417 exec2_list,
1418 sizeof(*exec2_list) * args->buffer_count);
1419 if (ret) {
1420 ret = -EFAULT;
ff240199 1421 DRM_DEBUG("failed to copy %d exec entries "
54cf91dc
CW
1422 "back to user (%d)\n",
1423 args->buffer_count, ret);
1424 }
1425 }
1426
1427 drm_free_large(exec2_list);
1428 return ret;
1429}