drm/i915: Remove the per-ring write list
[deliverable/linux.git] / drivers / gpu / drm / i915 / i915_gem_execbuffer.c
CommitLineData
54cf91dc
CW
1/*
2 * Copyright © 2008,2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
26 *
27 */
28
29#include "drmP.h"
30#include "drm.h"
31#include "i915_drm.h"
32#include "i915_drv.h"
33#include "i915_trace.h"
34#include "intel_drv.h"
f45b5557 35#include <linux/dma_remapping.h>
54cf91dc
CW
36
37struct change_domains {
38 uint32_t invalidate_domains;
39 uint32_t flush_domains;
40 uint32_t flush_rings;
c59a333f 41 uint32_t flips;
54cf91dc
CW
42};
43
44/*
45 * Set the next domain for the specified object. This
46 * may not actually perform the necessary flushing/invaliding though,
47 * as that may want to be batched with other set_domain operations
48 *
49 * This is (we hope) the only really tricky part of gem. The goal
50 * is fairly simple -- track which caches hold bits of the object
51 * and make sure they remain coherent. A few concrete examples may
52 * help to explain how it works. For shorthand, we use the notation
53 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
54 * a pair of read and write domain masks.
55 *
56 * Case 1: the batch buffer
57 *
58 * 1. Allocated
59 * 2. Written by CPU
60 * 3. Mapped to GTT
61 * 4. Read by GPU
62 * 5. Unmapped from GTT
63 * 6. Freed
64 *
65 * Let's take these a step at a time
66 *
67 * 1. Allocated
68 * Pages allocated from the kernel may still have
69 * cache contents, so we set them to (CPU, CPU) always.
70 * 2. Written by CPU (using pwrite)
71 * The pwrite function calls set_domain (CPU, CPU) and
72 * this function does nothing (as nothing changes)
73 * 3. Mapped by GTT
74 * This function asserts that the object is not
75 * currently in any GPU-based read or write domains
76 * 4. Read by GPU
77 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
78 * As write_domain is zero, this function adds in the
79 * current read domains (CPU+COMMAND, 0).
80 * flush_domains is set to CPU.
81 * invalidate_domains is set to COMMAND
82 * clflush is run to get data out of the CPU caches
83 * then i915_dev_set_domain calls i915_gem_flush to
84 * emit an MI_FLUSH and drm_agp_chipset_flush
85 * 5. Unmapped from GTT
86 * i915_gem_object_unbind calls set_domain (CPU, CPU)
87 * flush_domains and invalidate_domains end up both zero
88 * so no flushing/invalidating happens
89 * 6. Freed
90 * yay, done
91 *
92 * Case 2: The shared render buffer
93 *
94 * 1. Allocated
95 * 2. Mapped to GTT
96 * 3. Read/written by GPU
97 * 4. set_domain to (CPU,CPU)
98 * 5. Read/written by CPU
99 * 6. Read/written by GPU
100 *
101 * 1. Allocated
102 * Same as last example, (CPU, CPU)
103 * 2. Mapped to GTT
104 * Nothing changes (assertions find that it is not in the GPU)
105 * 3. Read/written by GPU
106 * execbuffer calls set_domain (RENDER, RENDER)
107 * flush_domains gets CPU
108 * invalidate_domains gets GPU
109 * clflush (obj)
110 * MI_FLUSH and drm_agp_chipset_flush
111 * 4. set_domain (CPU, CPU)
112 * flush_domains gets GPU
113 * invalidate_domains gets CPU
114 * wait_rendering (obj) to make sure all drawing is complete.
115 * This will include an MI_FLUSH to get the data from GPU
116 * to memory
117 * clflush (obj) to invalidate the CPU cache
118 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
119 * 5. Read/written by CPU
120 * cache lines are loaded and dirtied
121 * 6. Read written by GPU
122 * Same as last GPU access
123 *
124 * Case 3: The constant buffer
125 *
126 * 1. Allocated
127 * 2. Written by CPU
128 * 3. Read by GPU
129 * 4. Updated (written) by CPU again
130 * 5. Read by GPU
131 *
132 * 1. Allocated
133 * (CPU, CPU)
134 * 2. Written by CPU
135 * (CPU, CPU)
136 * 3. Read by GPU
137 * (CPU+RENDER, 0)
138 * flush_domains = CPU
139 * invalidate_domains = RENDER
140 * clflush (obj)
141 * MI_FLUSH
142 * drm_agp_chipset_flush
143 * 4. Updated (written) by CPU again
144 * (CPU, CPU)
145 * flush_domains = 0 (no previous write domain)
146 * invalidate_domains = 0 (no new read domains)
147 * 5. Read by GPU
148 * (CPU+RENDER, 0)
149 * flush_domains = CPU
150 * invalidate_domains = RENDER
151 * clflush (obj)
152 * MI_FLUSH
153 * drm_agp_chipset_flush
154 */
155static void
156i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
157 struct intel_ring_buffer *ring,
158 struct change_domains *cd)
159{
160 uint32_t invalidate_domains = 0, flush_domains = 0;
161
162 /*
163 * If the object isn't moving to a new write domain,
164 * let the object stay in multiple read domains
165 */
166 if (obj->base.pending_write_domain == 0)
167 obj->base.pending_read_domains |= obj->base.read_domains;
168
169 /*
170 * Flush the current write domain if
171 * the new read domains don't match. Invalidate
172 * any read domains which differ from the old
173 * write domain
174 */
175 if (obj->base.write_domain &&
176 (((obj->base.write_domain != obj->base.pending_read_domains ||
177 obj->ring != ring)) ||
178 (obj->fenced_gpu_access && !obj->pending_fenced_gpu_access))) {
179 flush_domains |= obj->base.write_domain;
180 invalidate_domains |=
181 obj->base.pending_read_domains & ~obj->base.write_domain;
182 }
183 /*
184 * Invalidate any read caches which may have
185 * stale data. That is, any new read domains.
186 */
187 invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
188 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
189 i915_gem_clflush_object(obj);
190
c59a333f
CW
191 if (obj->base.pending_write_domain)
192 cd->flips |= atomic_read(&obj->pending_flip);
193
54cf91dc
CW
194 /* The actual obj->write_domain will be updated with
195 * pending_write_domain after we emit the accumulated flush for all
196 * of our domain changes in execbuffers (which clears objects'
197 * write_domains). So if we have a current write domain that we
198 * aren't changing, set pending_write_domain to that.
199 */
200 if (flush_domains == 0 && obj->base.pending_write_domain == 0)
201 obj->base.pending_write_domain = obj->base.write_domain;
202
203 cd->invalidate_domains |= invalidate_domains;
204 cd->flush_domains |= flush_domains;
205 if (flush_domains & I915_GEM_GPU_DOMAINS)
96154f2f 206 cd->flush_rings |= intel_ring_flag(obj->ring);
54cf91dc 207 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
96154f2f 208 cd->flush_rings |= intel_ring_flag(ring);
54cf91dc
CW
209}
210
67731b87
CW
211struct eb_objects {
212 int and;
213 struct hlist_head buckets[0];
214};
215
216static struct eb_objects *
217eb_create(int size)
218{
219 struct eb_objects *eb;
220 int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
221 while (count > size)
222 count >>= 1;
223 eb = kzalloc(count*sizeof(struct hlist_head) +
224 sizeof(struct eb_objects),
225 GFP_KERNEL);
226 if (eb == NULL)
227 return eb;
228
229 eb->and = count - 1;
230 return eb;
231}
232
233static void
234eb_reset(struct eb_objects *eb)
235{
236 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
237}
238
239static void
240eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj)
241{
242 hlist_add_head(&obj->exec_node,
243 &eb->buckets[obj->exec_handle & eb->and]);
244}
245
246static struct drm_i915_gem_object *
247eb_get_object(struct eb_objects *eb, unsigned long handle)
248{
249 struct hlist_head *head;
250 struct hlist_node *node;
251 struct drm_i915_gem_object *obj;
252
253 head = &eb->buckets[handle & eb->and];
254 hlist_for_each(node, head) {
255 obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
256 if (obj->exec_handle == handle)
257 return obj;
258 }
259
260 return NULL;
261}
262
263static void
264eb_destroy(struct eb_objects *eb)
265{
266 kfree(eb);
267}
268
dabdfe02
CW
269static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
270{
271 return (obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
272 obj->cache_level != I915_CACHE_NONE);
273}
274
54cf91dc
CW
275static int
276i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
67731b87 277 struct eb_objects *eb,
54cf91dc
CW
278 struct drm_i915_gem_relocation_entry *reloc)
279{
280 struct drm_device *dev = obj->base.dev;
281 struct drm_gem_object *target_obj;
149c8407 282 struct drm_i915_gem_object *target_i915_obj;
54cf91dc
CW
283 uint32_t target_offset;
284 int ret = -EINVAL;
285
67731b87
CW
286 /* we've already hold a reference to all valid objects */
287 target_obj = &eb_get_object(eb, reloc->target_handle)->base;
288 if (unlikely(target_obj == NULL))
54cf91dc
CW
289 return -ENOENT;
290
149c8407
DV
291 target_i915_obj = to_intel_bo(target_obj);
292 target_offset = target_i915_obj->gtt_offset;
54cf91dc 293
54cf91dc
CW
294 /* The target buffer should have appeared before us in the
295 * exec_object list, so it should have a GTT space bound by now.
296 */
b8f7ab17 297 if (unlikely(target_offset == 0)) {
ff240199 298 DRM_DEBUG("No GTT space found for object %d\n",
54cf91dc 299 reloc->target_handle);
67731b87 300 return ret;
54cf91dc
CW
301 }
302
303 /* Validate that the target is in a valid r/w GPU domain */
b8f7ab17 304 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
ff240199 305 DRM_DEBUG("reloc with multiple write domains: "
54cf91dc
CW
306 "obj %p target %d offset %d "
307 "read %08x write %08x",
308 obj, reloc->target_handle,
309 (int) reloc->offset,
310 reloc->read_domains,
311 reloc->write_domain);
67731b87 312 return ret;
54cf91dc 313 }
4ca4a250
DV
314 if (unlikely((reloc->write_domain | reloc->read_domains)
315 & ~I915_GEM_GPU_DOMAINS)) {
ff240199 316 DRM_DEBUG("reloc with read/write non-GPU domains: "
54cf91dc
CW
317 "obj %p target %d offset %d "
318 "read %08x write %08x",
319 obj, reloc->target_handle,
320 (int) reloc->offset,
321 reloc->read_domains,
322 reloc->write_domain);
67731b87 323 return ret;
54cf91dc 324 }
b8f7ab17
CW
325 if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
326 reloc->write_domain != target_obj->pending_write_domain)) {
ff240199 327 DRM_DEBUG("Write domain conflict: "
54cf91dc
CW
328 "obj %p target %d offset %d "
329 "new %08x old %08x\n",
330 obj, reloc->target_handle,
331 (int) reloc->offset,
332 reloc->write_domain,
333 target_obj->pending_write_domain);
67731b87 334 return ret;
54cf91dc
CW
335 }
336
337 target_obj->pending_read_domains |= reloc->read_domains;
338 target_obj->pending_write_domain |= reloc->write_domain;
339
340 /* If the relocation already has the right value in it, no
341 * more work needs to be done.
342 */
343 if (target_offset == reloc->presumed_offset)
67731b87 344 return 0;
54cf91dc
CW
345
346 /* Check that the relocation address is valid... */
b8f7ab17 347 if (unlikely(reloc->offset > obj->base.size - 4)) {
ff240199 348 DRM_DEBUG("Relocation beyond object bounds: "
54cf91dc
CW
349 "obj %p target %d offset %d size %d.\n",
350 obj, reloc->target_handle,
351 (int) reloc->offset,
352 (int) obj->base.size);
67731b87 353 return ret;
54cf91dc 354 }
b8f7ab17 355 if (unlikely(reloc->offset & 3)) {
ff240199 356 DRM_DEBUG("Relocation not 4-byte aligned: "
54cf91dc
CW
357 "obj %p target %d offset %d.\n",
358 obj, reloc->target_handle,
359 (int) reloc->offset);
67731b87 360 return ret;
54cf91dc
CW
361 }
362
dabdfe02
CW
363 /* We can't wait for rendering with pagefaults disabled */
364 if (obj->active && in_atomic())
365 return -EFAULT;
366
54cf91dc 367 reloc->delta += target_offset;
dabdfe02 368 if (use_cpu_reloc(obj)) {
54cf91dc
CW
369 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
370 char *vaddr;
371
dabdfe02
CW
372 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
373 if (ret)
374 return ret;
375
54cf91dc
CW
376 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
377 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
378 kunmap_atomic(vaddr);
379 } else {
380 struct drm_i915_private *dev_priv = dev->dev_private;
381 uint32_t __iomem *reloc_entry;
382 void __iomem *reloc_page;
383
7b09638f
CW
384 ret = i915_gem_object_set_to_gtt_domain(obj, true);
385 if (ret)
386 return ret;
387
388 ret = i915_gem_object_put_fence(obj);
54cf91dc 389 if (ret)
67731b87 390 return ret;
54cf91dc
CW
391
392 /* Map the page containing the relocation we're going to perform. */
393 reloc->offset += obj->gtt_offset;
394 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
395 reloc->offset & PAGE_MASK);
396 reloc_entry = (uint32_t __iomem *)
397 (reloc_page + (reloc->offset & ~PAGE_MASK));
398 iowrite32(reloc->delta, reloc_entry);
399 io_mapping_unmap_atomic(reloc_page);
400 }
401
149c8407
DV
402 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
403 * pipe_control writes because the gpu doesn't properly redirect them
404 * through the ppgtt for non_secure batchbuffers. */
405 if (unlikely(IS_GEN6(dev) &&
406 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
407 !target_i915_obj->has_global_gtt_mapping)) {
408 i915_gem_gtt_bind_object(target_i915_obj,
409 target_i915_obj->cache_level);
410 }
411
54cf91dc
CW
412 /* and update the user's relocation entry */
413 reloc->presumed_offset = target_offset;
414
67731b87 415 return 0;
54cf91dc
CW
416}
417
418static int
419i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
6fe4f140 420 struct eb_objects *eb)
54cf91dc 421{
1d83f442
CW
422#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
423 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
54cf91dc 424 struct drm_i915_gem_relocation_entry __user *user_relocs;
6fe4f140 425 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
1d83f442 426 int remain, ret;
54cf91dc
CW
427
428 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
54cf91dc 429
1d83f442
CW
430 remain = entry->relocation_count;
431 while (remain) {
432 struct drm_i915_gem_relocation_entry *r = stack_reloc;
433 int count = remain;
434 if (count > ARRAY_SIZE(stack_reloc))
435 count = ARRAY_SIZE(stack_reloc);
436 remain -= count;
437
438 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
54cf91dc
CW
439 return -EFAULT;
440
1d83f442
CW
441 do {
442 u64 offset = r->presumed_offset;
54cf91dc 443
1d83f442
CW
444 ret = i915_gem_execbuffer_relocate_entry(obj, eb, r);
445 if (ret)
446 return ret;
447
448 if (r->presumed_offset != offset &&
449 __copy_to_user_inatomic(&user_relocs->presumed_offset,
450 &r->presumed_offset,
451 sizeof(r->presumed_offset))) {
452 return -EFAULT;
453 }
454
455 user_relocs++;
456 r++;
457 } while (--count);
54cf91dc
CW
458 }
459
460 return 0;
1d83f442 461#undef N_RELOC
54cf91dc
CW
462}
463
464static int
465i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
67731b87 466 struct eb_objects *eb,
54cf91dc
CW
467 struct drm_i915_gem_relocation_entry *relocs)
468{
6fe4f140 469 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
54cf91dc
CW
470 int i, ret;
471
472 for (i = 0; i < entry->relocation_count; i++) {
6fe4f140 473 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
54cf91dc
CW
474 if (ret)
475 return ret;
476 }
477
478 return 0;
479}
480
481static int
482i915_gem_execbuffer_relocate(struct drm_device *dev,
67731b87 483 struct eb_objects *eb,
6fe4f140 484 struct list_head *objects)
54cf91dc 485{
432e58ed 486 struct drm_i915_gem_object *obj;
d4aeee77
CW
487 int ret = 0;
488
489 /* This is the fast path and we cannot handle a pagefault whilst
490 * holding the struct mutex lest the user pass in the relocations
491 * contained within a mmaped bo. For in such a case we, the page
492 * fault handler would call i915_gem_fault() and we would try to
493 * acquire the struct mutex again. Obviously this is bad and so
494 * lockdep complains vehemently.
495 */
496 pagefault_disable();
432e58ed 497 list_for_each_entry(obj, objects, exec_list) {
6fe4f140 498 ret = i915_gem_execbuffer_relocate_object(obj, eb);
54cf91dc 499 if (ret)
d4aeee77 500 break;
54cf91dc 501 }
d4aeee77 502 pagefault_enable();
54cf91dc 503
d4aeee77 504 return ret;
54cf91dc
CW
505}
506
1690e1eb
CW
507#define __EXEC_OBJECT_HAS_FENCE (1<<31)
508
dabdfe02
CW
509static int
510need_reloc_mappable(struct drm_i915_gem_object *obj)
511{
512 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
513 return entry->relocation_count && !use_cpu_reloc(obj);
514}
515
1690e1eb
CW
516static int
517pin_and_fence_object(struct drm_i915_gem_object *obj,
518 struct intel_ring_buffer *ring)
519{
520 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
521 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
522 bool need_fence, need_mappable;
523 int ret;
524
525 need_fence =
526 has_fenced_gpu_access &&
527 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
528 obj->tiling_mode != I915_TILING_NONE;
dabdfe02 529 need_mappable = need_fence || need_reloc_mappable(obj);
1690e1eb
CW
530
531 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable);
532 if (ret)
533 return ret;
534
535 if (has_fenced_gpu_access) {
536 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
06d98131 537 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
538 if (ret)
539 goto err_unpin;
1690e1eb 540
9a5a53b3 541 if (i915_gem_object_pin_fence(obj))
1690e1eb 542 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
9a5a53b3 543
7dd49065 544 obj->pending_fenced_gpu_access = true;
1690e1eb 545 }
1690e1eb
CW
546 }
547
548 entry->offset = obj->gtt_offset;
549 return 0;
550
551err_unpin:
552 i915_gem_object_unpin(obj);
553 return ret;
554}
555
54cf91dc 556static int
d9e86c0e 557i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
54cf91dc 558 struct drm_file *file,
6fe4f140 559 struct list_head *objects)
54cf91dc 560{
7bddb01f 561 drm_i915_private_t *dev_priv = ring->dev->dev_private;
432e58ed 562 struct drm_i915_gem_object *obj;
432e58ed 563 int ret, retry;
9b3826bf 564 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
6fe4f140
CW
565 struct list_head ordered_objects;
566
567 INIT_LIST_HEAD(&ordered_objects);
568 while (!list_empty(objects)) {
569 struct drm_i915_gem_exec_object2 *entry;
570 bool need_fence, need_mappable;
571
572 obj = list_first_entry(objects,
573 struct drm_i915_gem_object,
574 exec_list);
575 entry = obj->exec_entry;
576
577 need_fence =
578 has_fenced_gpu_access &&
579 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
580 obj->tiling_mode != I915_TILING_NONE;
dabdfe02 581 need_mappable = need_fence || need_reloc_mappable(obj);
6fe4f140
CW
582
583 if (need_mappable)
584 list_move(&obj->exec_list, &ordered_objects);
585 else
586 list_move_tail(&obj->exec_list, &ordered_objects);
595dad76
CW
587
588 obj->base.pending_read_domains = 0;
589 obj->base.pending_write_domain = 0;
6fe4f140
CW
590 }
591 list_splice(&ordered_objects, objects);
54cf91dc
CW
592
593 /* Attempt to pin all of the buffers into the GTT.
594 * This is done in 3 phases:
595 *
596 * 1a. Unbind all objects that do not match the GTT constraints for
597 * the execbuffer (fenceable, mappable, alignment etc).
598 * 1b. Increment pin count for already bound objects.
599 * 2. Bind new objects.
600 * 3. Decrement pin count.
601 *
602 * This avoid unnecessary unbinding of later objects in order to makr
603 * room for the earlier objects *unless* we need to defragment.
604 */
605 retry = 0;
606 do {
607 ret = 0;
608
609 /* Unbind any ill-fitting objects or pin. */
432e58ed 610 list_for_each_entry(obj, objects, exec_list) {
6fe4f140 611 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
54cf91dc 612 bool need_fence, need_mappable;
1690e1eb 613
6fe4f140 614 if (!obj->gtt_space)
54cf91dc
CW
615 continue;
616
617 need_fence =
9b3826bf 618 has_fenced_gpu_access &&
54cf91dc
CW
619 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
620 obj->tiling_mode != I915_TILING_NONE;
dabdfe02 621 need_mappable = need_fence || need_reloc_mappable(obj);
54cf91dc
CW
622
623 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
624 (need_mappable && !obj->map_and_fenceable))
625 ret = i915_gem_object_unbind(obj);
626 else
1690e1eb 627 ret = pin_and_fence_object(obj, ring);
432e58ed 628 if (ret)
54cf91dc 629 goto err;
54cf91dc
CW
630 }
631
632 /* Bind fresh objects */
432e58ed 633 list_for_each_entry(obj, objects, exec_list) {
1690e1eb
CW
634 if (obj->gtt_space)
635 continue;
54cf91dc 636
1690e1eb
CW
637 ret = pin_and_fence_object(obj, ring);
638 if (ret) {
639 int ret_ignore;
640
641 /* This can potentially raise a harmless
642 * -EINVAL if we failed to bind in the above
643 * call. It cannot raise -EINTR since we know
644 * that the bo is freshly bound and so will
645 * not need to be flushed or waited upon.
646 */
647 ret_ignore = i915_gem_object_unbind(obj);
648 (void)ret_ignore;
649 WARN_ON(obj->gtt_space);
650 break;
54cf91dc 651 }
54cf91dc
CW
652 }
653
432e58ed
CW
654 /* Decrement pin count for bound objects */
655 list_for_each_entry(obj, objects, exec_list) {
1690e1eb
CW
656 struct drm_i915_gem_exec_object2 *entry;
657
658 if (!obj->gtt_space)
659 continue;
660
661 entry = obj->exec_entry;
662 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
663 i915_gem_object_unpin_fence(obj);
664 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
665 }
666
667 i915_gem_object_unpin(obj);
7bddb01f
DV
668
669 /* ... and ensure ppgtt mapping exist if needed. */
670 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
671 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
672 obj, obj->cache_level);
673
674 obj->has_aliasing_ppgtt_mapping = 1;
675 }
54cf91dc
CW
676 }
677
678 if (ret != -ENOSPC || retry > 1)
679 return ret;
680
681 /* First attempt, just clear anything that is purgeable.
682 * Second attempt, clear the entire GTT.
683 */
d9e86c0e 684 ret = i915_gem_evict_everything(ring->dev, retry == 0);
54cf91dc
CW
685 if (ret)
686 return ret;
687
688 retry++;
689 } while (1);
432e58ed
CW
690
691err:
1690e1eb
CW
692 list_for_each_entry_continue_reverse(obj, objects, exec_list) {
693 struct drm_i915_gem_exec_object2 *entry;
694
695 if (!obj->gtt_space)
696 continue;
697
698 entry = obj->exec_entry;
699 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
700 i915_gem_object_unpin_fence(obj);
701 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
702 }
432e58ed 703
1690e1eb 704 i915_gem_object_unpin(obj);
432e58ed
CW
705 }
706
707 return ret;
54cf91dc
CW
708}
709
710static int
711i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
712 struct drm_file *file,
d9e86c0e 713 struct intel_ring_buffer *ring,
432e58ed 714 struct list_head *objects,
67731b87 715 struct eb_objects *eb,
432e58ed 716 struct drm_i915_gem_exec_object2 *exec,
54cf91dc
CW
717 int count)
718{
719 struct drm_i915_gem_relocation_entry *reloc;
432e58ed 720 struct drm_i915_gem_object *obj;
dd6864a4 721 int *reloc_offset;
54cf91dc
CW
722 int i, total, ret;
723
67731b87 724 /* We may process another execbuffer during the unlock... */
36cf1742 725 while (!list_empty(objects)) {
67731b87
CW
726 obj = list_first_entry(objects,
727 struct drm_i915_gem_object,
728 exec_list);
729 list_del_init(&obj->exec_list);
730 drm_gem_object_unreference(&obj->base);
731 }
732
54cf91dc
CW
733 mutex_unlock(&dev->struct_mutex);
734
735 total = 0;
736 for (i = 0; i < count; i++)
432e58ed 737 total += exec[i].relocation_count;
54cf91dc 738
dd6864a4 739 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
54cf91dc 740 reloc = drm_malloc_ab(total, sizeof(*reloc));
dd6864a4
CW
741 if (reloc == NULL || reloc_offset == NULL) {
742 drm_free_large(reloc);
743 drm_free_large(reloc_offset);
54cf91dc
CW
744 mutex_lock(&dev->struct_mutex);
745 return -ENOMEM;
746 }
747
748 total = 0;
749 for (i = 0; i < count; i++) {
750 struct drm_i915_gem_relocation_entry __user *user_relocs;
751
432e58ed 752 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
54cf91dc
CW
753
754 if (copy_from_user(reloc+total, user_relocs,
432e58ed 755 exec[i].relocation_count * sizeof(*reloc))) {
54cf91dc
CW
756 ret = -EFAULT;
757 mutex_lock(&dev->struct_mutex);
758 goto err;
759 }
760
dd6864a4 761 reloc_offset[i] = total;
432e58ed 762 total += exec[i].relocation_count;
54cf91dc
CW
763 }
764
765 ret = i915_mutex_lock_interruptible(dev);
766 if (ret) {
767 mutex_lock(&dev->struct_mutex);
768 goto err;
769 }
770
67731b87 771 /* reacquire the objects */
67731b87
CW
772 eb_reset(eb);
773 for (i = 0; i < count; i++) {
67731b87
CW
774 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
775 exec[i].handle));
c8725226 776 if (&obj->base == NULL) {
ff240199 777 DRM_DEBUG("Invalid object handle %d at index %d\n",
67731b87
CW
778 exec[i].handle, i);
779 ret = -ENOENT;
780 goto err;
781 }
782
783 list_add_tail(&obj->exec_list, objects);
784 obj->exec_handle = exec[i].handle;
6fe4f140 785 obj->exec_entry = &exec[i];
67731b87
CW
786 eb_add_object(eb, obj);
787 }
788
6fe4f140 789 ret = i915_gem_execbuffer_reserve(ring, file, objects);
54cf91dc
CW
790 if (ret)
791 goto err;
792
432e58ed 793 list_for_each_entry(obj, objects, exec_list) {
dd6864a4 794 int offset = obj->exec_entry - exec;
67731b87 795 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
dd6864a4 796 reloc + reloc_offset[offset]);
54cf91dc
CW
797 if (ret)
798 goto err;
54cf91dc
CW
799 }
800
801 /* Leave the user relocations as are, this is the painfully slow path,
802 * and we want to avoid the complication of dropping the lock whilst
803 * having buffers reserved in the aperture and so causing spurious
804 * ENOSPC for random operations.
805 */
806
807err:
808 drm_free_large(reloc);
dd6864a4 809 drm_free_large(reloc_offset);
54cf91dc
CW
810 return ret;
811}
812
cc889e0f 813static void
54cf91dc
CW
814i915_gem_execbuffer_flush(struct drm_device *dev,
815 uint32_t invalidate_domains,
cc889e0f 816 uint32_t flush_domains)
54cf91dc 817{
54cf91dc
CW
818 if (flush_domains & I915_GEM_DOMAIN_CPU)
819 intel_gtt_chipset_flush();
820
63256ec5
CW
821 if (flush_domains & I915_GEM_DOMAIN_GTT)
822 wmb();
54cf91dc
CW
823}
824
c59a333f
CW
825static int
826i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
827{
828 u32 plane, flip_mask;
829 int ret;
830
831 /* Check for any pending flips. As we only maintain a flip queue depth
832 * of 1, we can simply insert a WAIT for the next display flip prior
833 * to executing the batch and avoid stalling the CPU.
834 */
835
836 for (plane = 0; flips >> plane; plane++) {
837 if (((flips >> plane) & 1) == 0)
838 continue;
839
840 if (plane)
841 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
842 else
843 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
844
845 ret = intel_ring_begin(ring, 2);
846 if (ret)
847 return ret;
848
849 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
850 intel_ring_emit(ring, MI_NOOP);
851 intel_ring_advance(ring);
852 }
853
854 return 0;
855}
856
857
54cf91dc 858static int
432e58ed
CW
859i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
860 struct list_head *objects)
54cf91dc 861{
432e58ed 862 struct drm_i915_gem_object *obj;
54cf91dc 863 struct change_domains cd;
432e58ed 864 int ret;
54cf91dc 865
c59a333f 866 memset(&cd, 0, sizeof(cd));
432e58ed
CW
867 list_for_each_entry(obj, objects, exec_list)
868 i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
54cf91dc
CW
869
870 if (cd.invalidate_domains | cd.flush_domains) {
cc889e0f
DV
871 i915_gem_execbuffer_flush(ring->dev,
872 cd.invalidate_domains,
873 cd.flush_domains);
54cf91dc
CW
874 }
875
c59a333f
CW
876 if (cd.flips) {
877 ret = i915_gem_execbuffer_wait_for_flips(ring, cd.flips);
878 if (ret)
879 return ret;
880 }
881
432e58ed 882 list_for_each_entry(obj, objects, exec_list) {
2911a35b 883 ret = i915_gem_object_sync(obj, ring);
1ec14ad3
CW
884 if (ret)
885 return ret;
54cf91dc
CW
886 }
887
09cf7c9a
CW
888 /* Unconditionally invalidate gpu caches and ensure that we do flush
889 * any residual writes from the previous batch.
890 */
891 ret = i915_gem_flush_ring(ring,
892 I915_GEM_GPU_DOMAINS,
893 ring->gpu_caches_dirty ? I915_GEM_GPU_DOMAINS : 0);
cc889e0f
DV
894 if (ret)
895 return ret;
896
09cf7c9a 897 ring->gpu_caches_dirty = false;
54cf91dc
CW
898 return 0;
899}
900
432e58ed
CW
901static bool
902i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
54cf91dc 903{
432e58ed 904 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
54cf91dc
CW
905}
906
907static int
908validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
909 int count)
910{
911 int i;
912
913 for (i = 0; i < count; i++) {
914 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
915 int length; /* limited by fault_in_pages_readable() */
916
917 /* First check for malicious input causing overflow */
918 if (exec[i].relocation_count >
919 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
920 return -EINVAL;
921
922 length = exec[i].relocation_count *
923 sizeof(struct drm_i915_gem_relocation_entry);
924 if (!access_ok(VERIFY_READ, ptr, length))
925 return -EFAULT;
926
927 /* we may also need to update the presumed offsets */
928 if (!access_ok(VERIFY_WRITE, ptr, length))
929 return -EFAULT;
930
f56f821f 931 if (fault_in_multipages_readable(ptr, length))
54cf91dc
CW
932 return -EFAULT;
933 }
934
935 return 0;
936}
937
432e58ed
CW
938static void
939i915_gem_execbuffer_move_to_active(struct list_head *objects,
1ec14ad3
CW
940 struct intel_ring_buffer *ring,
941 u32 seqno)
432e58ed
CW
942{
943 struct drm_i915_gem_object *obj;
944
945 list_for_each_entry(obj, objects, exec_list) {
69c2fc89
CW
946 u32 old_read = obj->base.read_domains;
947 u32 old_write = obj->base.write_domain;
db53a302 948
432e58ed
CW
949 obj->base.read_domains = obj->base.pending_read_domains;
950 obj->base.write_domain = obj->base.pending_write_domain;
951 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
952
1ec14ad3 953 i915_gem_object_move_to_active(obj, ring, seqno);
432e58ed
CW
954 if (obj->base.write_domain) {
955 obj->dirty = 1;
0201f1ec 956 obj->last_write_seqno = seqno;
acb87dfb
CW
957 if (obj->pin_count) /* check for potential scanout */
958 intel_mark_busy(ring->dev, obj);
432e58ed
CW
959 }
960
db53a302 961 trace_i915_gem_object_change_domain(obj, old_read, old_write);
432e58ed 962 }
acb87dfb
CW
963
964 intel_mark_busy(ring->dev, NULL);
432e58ed
CW
965}
966
54cf91dc
CW
967static void
968i915_gem_execbuffer_retire_commands(struct drm_device *dev,
432e58ed 969 struct drm_file *file,
54cf91dc
CW
970 struct intel_ring_buffer *ring)
971{
cc889e0f
DV
972 /* Unconditionally force add_request to emit a full flush. */
973 ring->gpu_caches_dirty = true;
54cf91dc 974
432e58ed 975 /* Add a breadcrumb for the completion of the batch buffer */
3bb73aba 976 (void)i915_add_request(ring, file, NULL);
432e58ed 977}
54cf91dc 978
ae662d31
EA
979static int
980i915_reset_gen7_sol_offsets(struct drm_device *dev,
981 struct intel_ring_buffer *ring)
982{
983 drm_i915_private_t *dev_priv = dev->dev_private;
984 int ret, i;
985
986 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
987 return 0;
988
989 ret = intel_ring_begin(ring, 4 * 3);
990 if (ret)
991 return ret;
992
993 for (i = 0; i < 4; i++) {
994 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
995 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
996 intel_ring_emit(ring, 0);
997 }
998
999 intel_ring_advance(ring);
1000
1001 return 0;
1002}
1003
54cf91dc
CW
1004static int
1005i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1006 struct drm_file *file,
1007 struct drm_i915_gem_execbuffer2 *args,
432e58ed 1008 struct drm_i915_gem_exec_object2 *exec)
54cf91dc
CW
1009{
1010 drm_i915_private_t *dev_priv = dev->dev_private;
432e58ed 1011 struct list_head objects;
67731b87 1012 struct eb_objects *eb;
54cf91dc
CW
1013 struct drm_i915_gem_object *batch_obj;
1014 struct drm_clip_rect *cliprects = NULL;
54cf91dc 1015 struct intel_ring_buffer *ring;
6e0a69db 1016 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
c4e7a414 1017 u32 exec_start, exec_len;
1ec14ad3 1018 u32 seqno;
84f9f938 1019 u32 mask;
72bfa19c 1020 int ret, mode, i;
54cf91dc 1021
432e58ed 1022 if (!i915_gem_check_execbuffer(args)) {
ff240199 1023 DRM_DEBUG("execbuf with invalid offset/length\n");
432e58ed
CW
1024 return -EINVAL;
1025 }
1026
1027 ret = validate_exec_list(exec, args->buffer_count);
54cf91dc
CW
1028 if (ret)
1029 return ret;
1030
54cf91dc
CW
1031 switch (args->flags & I915_EXEC_RING_MASK) {
1032 case I915_EXEC_DEFAULT:
1033 case I915_EXEC_RENDER:
1ec14ad3 1034 ring = &dev_priv->ring[RCS];
54cf91dc
CW
1035 break;
1036 case I915_EXEC_BSD:
1ec14ad3 1037 ring = &dev_priv->ring[VCS];
6e0a69db
BW
1038 if (ctx_id != 0) {
1039 DRM_DEBUG("Ring %s doesn't support contexts\n",
1040 ring->name);
1041 return -EPERM;
1042 }
54cf91dc
CW
1043 break;
1044 case I915_EXEC_BLT:
1ec14ad3 1045 ring = &dev_priv->ring[BCS];
6e0a69db
BW
1046 if (ctx_id != 0) {
1047 DRM_DEBUG("Ring %s doesn't support contexts\n",
1048 ring->name);
1049 return -EPERM;
1050 }
54cf91dc
CW
1051 break;
1052 default:
ff240199 1053 DRM_DEBUG("execbuf with unknown ring: %d\n",
54cf91dc
CW
1054 (int)(args->flags & I915_EXEC_RING_MASK));
1055 return -EINVAL;
1056 }
a15817cf
CW
1057 if (!intel_ring_initialized(ring)) {
1058 DRM_DEBUG("execbuf with invalid ring: %d\n",
1059 (int)(args->flags & I915_EXEC_RING_MASK));
1060 return -EINVAL;
1061 }
54cf91dc 1062
72bfa19c 1063 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
84f9f938 1064 mask = I915_EXEC_CONSTANTS_MASK;
72bfa19c
CW
1065 switch (mode) {
1066 case I915_EXEC_CONSTANTS_REL_GENERAL:
1067 case I915_EXEC_CONSTANTS_ABSOLUTE:
1068 case I915_EXEC_CONSTANTS_REL_SURFACE:
1069 if (ring == &dev_priv->ring[RCS] &&
1070 mode != dev_priv->relative_constants_mode) {
1071 if (INTEL_INFO(dev)->gen < 4)
1072 return -EINVAL;
1073
1074 if (INTEL_INFO(dev)->gen > 5 &&
1075 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1076 return -EINVAL;
84f9f938
BW
1077
1078 /* The HW changed the meaning on this bit on gen6 */
1079 if (INTEL_INFO(dev)->gen >= 6)
1080 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
72bfa19c
CW
1081 }
1082 break;
1083 default:
ff240199 1084 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
72bfa19c
CW
1085 return -EINVAL;
1086 }
1087
54cf91dc 1088 if (args->buffer_count < 1) {
ff240199 1089 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
54cf91dc
CW
1090 return -EINVAL;
1091 }
54cf91dc
CW
1092
1093 if (args->num_cliprects != 0) {
1ec14ad3 1094 if (ring != &dev_priv->ring[RCS]) {
ff240199 1095 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
c4e7a414
CW
1096 return -EINVAL;
1097 }
1098
6ebebc92
DV
1099 if (INTEL_INFO(dev)->gen >= 5) {
1100 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1101 return -EINVAL;
1102 }
1103
44afb3a0
XW
1104 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1105 DRM_DEBUG("execbuf with %u cliprects\n",
1106 args->num_cliprects);
1107 return -EINVAL;
1108 }
5e13a0c5 1109
432e58ed 1110 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
54cf91dc
CW
1111 GFP_KERNEL);
1112 if (cliprects == NULL) {
1113 ret = -ENOMEM;
1114 goto pre_mutex_err;
1115 }
1116
432e58ed
CW
1117 if (copy_from_user(cliprects,
1118 (struct drm_clip_rect __user *)(uintptr_t)
1119 args->cliprects_ptr,
1120 sizeof(*cliprects)*args->num_cliprects)) {
54cf91dc
CW
1121 ret = -EFAULT;
1122 goto pre_mutex_err;
1123 }
1124 }
1125
54cf91dc
CW
1126 ret = i915_mutex_lock_interruptible(dev);
1127 if (ret)
1128 goto pre_mutex_err;
1129
1130 if (dev_priv->mm.suspended) {
1131 mutex_unlock(&dev->struct_mutex);
1132 ret = -EBUSY;
1133 goto pre_mutex_err;
1134 }
1135
67731b87
CW
1136 eb = eb_create(args->buffer_count);
1137 if (eb == NULL) {
1138 mutex_unlock(&dev->struct_mutex);
1139 ret = -ENOMEM;
1140 goto pre_mutex_err;
1141 }
1142
54cf91dc 1143 /* Look up object handles */
432e58ed 1144 INIT_LIST_HEAD(&objects);
54cf91dc
CW
1145 for (i = 0; i < args->buffer_count; i++) {
1146 struct drm_i915_gem_object *obj;
1147
432e58ed
CW
1148 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
1149 exec[i].handle));
c8725226 1150 if (&obj->base == NULL) {
ff240199 1151 DRM_DEBUG("Invalid object handle %d at index %d\n",
432e58ed 1152 exec[i].handle, i);
54cf91dc 1153 /* prevent error path from reading uninitialized data */
54cf91dc
CW
1154 ret = -ENOENT;
1155 goto err;
1156 }
54cf91dc 1157
432e58ed 1158 if (!list_empty(&obj->exec_list)) {
ff240199 1159 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
432e58ed 1160 obj, exec[i].handle, i);
54cf91dc
CW
1161 ret = -EINVAL;
1162 goto err;
1163 }
432e58ed
CW
1164
1165 list_add_tail(&obj->exec_list, &objects);
67731b87 1166 obj->exec_handle = exec[i].handle;
6fe4f140 1167 obj->exec_entry = &exec[i];
67731b87 1168 eb_add_object(eb, obj);
54cf91dc
CW
1169 }
1170
6fe4f140
CW
1171 /* take note of the batch buffer before we might reorder the lists */
1172 batch_obj = list_entry(objects.prev,
1173 struct drm_i915_gem_object,
1174 exec_list);
1175
54cf91dc 1176 /* Move the objects en-masse into the GTT, evicting if necessary. */
6fe4f140 1177 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
54cf91dc
CW
1178 if (ret)
1179 goto err;
1180
1181 /* The objects are in their final locations, apply the relocations. */
6fe4f140 1182 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
54cf91dc
CW
1183 if (ret) {
1184 if (ret == -EFAULT) {
d9e86c0e 1185 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
67731b87
CW
1186 &objects, eb,
1187 exec,
54cf91dc
CW
1188 args->buffer_count);
1189 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1190 }
1191 if (ret)
1192 goto err;
1193 }
1194
1195 /* Set the pending read domains for the batch buffer to COMMAND */
54cf91dc 1196 if (batch_obj->base.pending_write_domain) {
ff240199 1197 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
54cf91dc
CW
1198 ret = -EINVAL;
1199 goto err;
1200 }
1201 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1202
432e58ed
CW
1203 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
1204 if (ret)
54cf91dc 1205 goto err;
54cf91dc 1206
db53a302 1207 seqno = i915_gem_next_request_seqno(ring);
076e2c0e 1208 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
1ec14ad3
CW
1209 if (seqno < ring->sync_seqno[i]) {
1210 /* The GPU can not handle its semaphore value wrapping,
1211 * so every billion or so execbuffers, we need to stall
1212 * the GPU in order to reset the counters.
1213 */
b2da9fe5 1214 ret = i915_gpu_idle(dev);
1ec14ad3
CW
1215 if (ret)
1216 goto err;
b2da9fe5 1217 i915_gem_retire_requests(dev);
1ec14ad3
CW
1218
1219 BUG_ON(ring->sync_seqno[i]);
1220 }
1221 }
1222
0da5cec1
EA
1223 ret = i915_switch_context(ring, file, ctx_id);
1224 if (ret)
1225 goto err;
1226
e2971bda
BW
1227 if (ring == &dev_priv->ring[RCS] &&
1228 mode != dev_priv->relative_constants_mode) {
1229 ret = intel_ring_begin(ring, 4);
1230 if (ret)
1231 goto err;
1232
1233 intel_ring_emit(ring, MI_NOOP);
1234 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1235 intel_ring_emit(ring, INSTPM);
84f9f938 1236 intel_ring_emit(ring, mask << 16 | mode);
e2971bda
BW
1237 intel_ring_advance(ring);
1238
1239 dev_priv->relative_constants_mode = mode;
1240 }
1241
ae662d31
EA
1242 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1243 ret = i915_reset_gen7_sol_offsets(dev, ring);
1244 if (ret)
1245 goto err;
1246 }
1247
db53a302
CW
1248 trace_i915_gem_ring_dispatch(ring, seqno);
1249
c4e7a414
CW
1250 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
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,
1260 exec_start, exec_len);
1261 if (ret)
1262 goto err;
1263 }
1264 } else {
1265 ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
1266 if (ret)
1267 goto err;
1268 }
54cf91dc 1269
1ec14ad3 1270 i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
432e58ed 1271 i915_gem_execbuffer_retire_commands(dev, file, ring);
54cf91dc
CW
1272
1273err:
67731b87 1274 eb_destroy(eb);
432e58ed
CW
1275 while (!list_empty(&objects)) {
1276 struct drm_i915_gem_object *obj;
1277
1278 obj = list_first_entry(&objects,
1279 struct drm_i915_gem_object,
1280 exec_list);
1281 list_del_init(&obj->exec_list);
1282 drm_gem_object_unreference(&obj->base);
54cf91dc
CW
1283 }
1284
1285 mutex_unlock(&dev->struct_mutex);
1286
1287pre_mutex_err:
54cf91dc 1288 kfree(cliprects);
54cf91dc
CW
1289 return ret;
1290}
1291
1292/*
1293 * Legacy execbuffer just creates an exec2 list from the original exec object
1294 * list array and passes it to the real function.
1295 */
1296int
1297i915_gem_execbuffer(struct drm_device *dev, void *data,
1298 struct drm_file *file)
1299{
1300 struct drm_i915_gem_execbuffer *args = data;
1301 struct drm_i915_gem_execbuffer2 exec2;
1302 struct drm_i915_gem_exec_object *exec_list = NULL;
1303 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1304 int ret, i;
1305
54cf91dc 1306 if (args->buffer_count < 1) {
ff240199 1307 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
54cf91dc
CW
1308 return -EINVAL;
1309 }
1310
1311 /* Copy in the exec list from userland */
1312 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1313 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1314 if (exec_list == NULL || exec2_list == NULL) {
ff240199 1315 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
54cf91dc
CW
1316 args->buffer_count);
1317 drm_free_large(exec_list);
1318 drm_free_large(exec2_list);
1319 return -ENOMEM;
1320 }
1321 ret = copy_from_user(exec_list,
1322 (struct drm_i915_relocation_entry __user *)
1323 (uintptr_t) args->buffers_ptr,
1324 sizeof(*exec_list) * args->buffer_count);
1325 if (ret != 0) {
ff240199 1326 DRM_DEBUG("copy %d exec entries failed %d\n",
54cf91dc
CW
1327 args->buffer_count, ret);
1328 drm_free_large(exec_list);
1329 drm_free_large(exec2_list);
1330 return -EFAULT;
1331 }
1332
1333 for (i = 0; i < args->buffer_count; i++) {
1334 exec2_list[i].handle = exec_list[i].handle;
1335 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1336 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1337 exec2_list[i].alignment = exec_list[i].alignment;
1338 exec2_list[i].offset = exec_list[i].offset;
1339 if (INTEL_INFO(dev)->gen < 4)
1340 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1341 else
1342 exec2_list[i].flags = 0;
1343 }
1344
1345 exec2.buffers_ptr = args->buffers_ptr;
1346 exec2.buffer_count = args->buffer_count;
1347 exec2.batch_start_offset = args->batch_start_offset;
1348 exec2.batch_len = args->batch_len;
1349 exec2.DR1 = args->DR1;
1350 exec2.DR4 = args->DR4;
1351 exec2.num_cliprects = args->num_cliprects;
1352 exec2.cliprects_ptr = args->cliprects_ptr;
1353 exec2.flags = I915_EXEC_RENDER;
6e0a69db 1354 i915_execbuffer2_set_context_id(exec2, 0);
54cf91dc
CW
1355
1356 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1357 if (!ret) {
1358 /* Copy the new buffer offsets back to the user's exec list. */
1359 for (i = 0; i < args->buffer_count; i++)
1360 exec_list[i].offset = exec2_list[i].offset;
1361 /* ... and back out to userspace */
1362 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1363 (uintptr_t) args->buffers_ptr,
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
CW
1393 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1394 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
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,
1404 (struct drm_i915_relocation_entry __user *)
1405 (uintptr_t) args->buffers_ptr,
1406 sizeof(*exec2_list) * args->buffer_count);
1407 if (ret != 0) {
ff240199 1408 DRM_DEBUG("copy %d exec entries failed %d\n",
54cf91dc
CW
1409 args->buffer_count, ret);
1410 drm_free_large(exec2_list);
1411 return -EFAULT;
1412 }
1413
1414 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1415 if (!ret) {
1416 /* Copy the new buffer offsets back to the user's exec list. */
1417 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1418 (uintptr_t) args->buffers_ptr,
1419 exec2_list,
1420 sizeof(*exec2_list) * args->buffer_count);
1421 if (ret) {
1422 ret = -EFAULT;
ff240199 1423 DRM_DEBUG("failed to copy %d exec entries "
54cf91dc
CW
1424 "back to user (%d)\n",
1425 args->buffer_count, ret);
1426 }
1427 }
1428
1429 drm_free_large(exec2_list);
1430 return ret;
1431}
This page took 0.196925 seconds and 5 git commands to generate.