blob: e69834341ef068b4ebbbb500fc4484d64826a879 [file] [log] [blame]
Chris Wilson54cf91d2010-11-25 18:00:26 +00001/*
2 * Copyright © 2008,2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
26 *
27 */
28
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"
35
36struct change_domains {
37 uint32_t invalidate_domains;
38 uint32_t flush_domains;
39 uint32_t flush_rings;
40};
41
42/*
43 * Set the next domain for the specified object. This
44 * may not actually perform the necessary flushing/invaliding though,
45 * as that may want to be batched with other set_domain operations
46 *
47 * This is (we hope) the only really tricky part of gem. The goal
48 * is fairly simple -- track which caches hold bits of the object
49 * and make sure they remain coherent. A few concrete examples may
50 * help to explain how it works. For shorthand, we use the notation
51 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
52 * a pair of read and write domain masks.
53 *
54 * Case 1: the batch buffer
55 *
56 * 1. Allocated
57 * 2. Written by CPU
58 * 3. Mapped to GTT
59 * 4. Read by GPU
60 * 5. Unmapped from GTT
61 * 6. Freed
62 *
63 * Let's take these a step at a time
64 *
65 * 1. Allocated
66 * Pages allocated from the kernel may still have
67 * cache contents, so we set them to (CPU, CPU) always.
68 * 2. Written by CPU (using pwrite)
69 * The pwrite function calls set_domain (CPU, CPU) and
70 * this function does nothing (as nothing changes)
71 * 3. Mapped by GTT
72 * This function asserts that the object is not
73 * currently in any GPU-based read or write domains
74 * 4. Read by GPU
75 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
76 * As write_domain is zero, this function adds in the
77 * current read domains (CPU+COMMAND, 0).
78 * flush_domains is set to CPU.
79 * invalidate_domains is set to COMMAND
80 * clflush is run to get data out of the CPU caches
81 * then i915_dev_set_domain calls i915_gem_flush to
82 * emit an MI_FLUSH and drm_agp_chipset_flush
83 * 5. Unmapped from GTT
84 * i915_gem_object_unbind calls set_domain (CPU, CPU)
85 * flush_domains and invalidate_domains end up both zero
86 * so no flushing/invalidating happens
87 * 6. Freed
88 * yay, done
89 *
90 * Case 2: The shared render buffer
91 *
92 * 1. Allocated
93 * 2. Mapped to GTT
94 * 3. Read/written by GPU
95 * 4. set_domain to (CPU,CPU)
96 * 5. Read/written by CPU
97 * 6. Read/written by GPU
98 *
99 * 1. Allocated
100 * Same as last example, (CPU, CPU)
101 * 2. Mapped to GTT
102 * Nothing changes (assertions find that it is not in the GPU)
103 * 3. Read/written by GPU
104 * execbuffer calls set_domain (RENDER, RENDER)
105 * flush_domains gets CPU
106 * invalidate_domains gets GPU
107 * clflush (obj)
108 * MI_FLUSH and drm_agp_chipset_flush
109 * 4. set_domain (CPU, CPU)
110 * flush_domains gets GPU
111 * invalidate_domains gets CPU
112 * wait_rendering (obj) to make sure all drawing is complete.
113 * This will include an MI_FLUSH to get the data from GPU
114 * to memory
115 * clflush (obj) to invalidate the CPU cache
116 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
117 * 5. Read/written by CPU
118 * cache lines are loaded and dirtied
119 * 6. Read written by GPU
120 * Same as last GPU access
121 *
122 * Case 3: The constant buffer
123 *
124 * 1. Allocated
125 * 2. Written by CPU
126 * 3. Read by GPU
127 * 4. Updated (written) by CPU again
128 * 5. Read by GPU
129 *
130 * 1. Allocated
131 * (CPU, CPU)
132 * 2. Written by CPU
133 * (CPU, CPU)
134 * 3. Read by GPU
135 * (CPU+RENDER, 0)
136 * flush_domains = CPU
137 * invalidate_domains = RENDER
138 * clflush (obj)
139 * MI_FLUSH
140 * drm_agp_chipset_flush
141 * 4. Updated (written) by CPU again
142 * (CPU, CPU)
143 * flush_domains = 0 (no previous write domain)
144 * invalidate_domains = 0 (no new read domains)
145 * 5. Read by GPU
146 * (CPU+RENDER, 0)
147 * flush_domains = CPU
148 * invalidate_domains = RENDER
149 * clflush (obj)
150 * MI_FLUSH
151 * drm_agp_chipset_flush
152 */
153static void
154i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
155 struct intel_ring_buffer *ring,
156 struct change_domains *cd)
157{
158 uint32_t invalidate_domains = 0, flush_domains = 0;
159
160 /*
161 * If the object isn't moving to a new write domain,
162 * let the object stay in multiple read domains
163 */
164 if (obj->base.pending_write_domain == 0)
165 obj->base.pending_read_domains |= obj->base.read_domains;
166
167 /*
168 * Flush the current write domain if
169 * the new read domains don't match. Invalidate
170 * any read domains which differ from the old
171 * write domain
172 */
173 if (obj->base.write_domain &&
174 (((obj->base.write_domain != obj->base.pending_read_domains ||
175 obj->ring != ring)) ||
176 (obj->fenced_gpu_access && !obj->pending_fenced_gpu_access))) {
177 flush_domains |= obj->base.write_domain;
178 invalidate_domains |=
179 obj->base.pending_read_domains & ~obj->base.write_domain;
180 }
181 /*
182 * Invalidate any read caches which may have
183 * stale data. That is, any new read domains.
184 */
185 invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
186 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
187 i915_gem_clflush_object(obj);
188
189 /* blow away mappings if mapped through GTT */
190 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
191 i915_gem_release_mmap(obj);
192
193 /* The actual obj->write_domain will be updated with
194 * pending_write_domain after we emit the accumulated flush for all
195 * of our domain changes in execbuffers (which clears objects'
196 * write_domains). So if we have a current write domain that we
197 * aren't changing, set pending_write_domain to that.
198 */
199 if (flush_domains == 0 && obj->base.pending_write_domain == 0)
200 obj->base.pending_write_domain = obj->base.write_domain;
201
202 cd->invalidate_domains |= invalidate_domains;
203 cd->flush_domains |= flush_domains;
204 if (flush_domains & I915_GEM_GPU_DOMAINS)
205 cd->flush_rings |= obj->ring->id;
206 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
207 cd->flush_rings |= ring->id;
208}
209
Chris Wilson67731b82010-12-08 10:38:14 +0000210struct eb_objects {
211 int and;
212 struct hlist_head buckets[0];
213};
214
215static struct eb_objects *
216eb_create(int size)
217{
218 struct eb_objects *eb;
219 int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
220 while (count > size)
221 count >>= 1;
222 eb = kzalloc(count*sizeof(struct hlist_head) +
223 sizeof(struct eb_objects),
224 GFP_KERNEL);
225 if (eb == NULL)
226 return eb;
227
228 eb->and = count - 1;
229 return eb;
230}
231
232static void
233eb_reset(struct eb_objects *eb)
234{
235 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
236}
237
238static void
239eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj)
240{
241 hlist_add_head(&obj->exec_node,
242 &eb->buckets[obj->exec_handle & eb->and]);
243}
244
245static struct drm_i915_gem_object *
246eb_get_object(struct eb_objects *eb, unsigned long handle)
247{
248 struct hlist_head *head;
249 struct hlist_node *node;
250 struct drm_i915_gem_object *obj;
251
252 head = &eb->buckets[handle & eb->and];
253 hlist_for_each(node, head) {
254 obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
255 if (obj->exec_handle == handle)
256 return obj;
257 }
258
259 return NULL;
260}
261
262static void
263eb_destroy(struct eb_objects *eb)
264{
265 kfree(eb);
266}
267
Chris Wilson54cf91d2010-11-25 18:00:26 +0000268static int
269i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000270 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000271 struct drm_i915_gem_relocation_entry *reloc)
272{
273 struct drm_device *dev = obj->base.dev;
274 struct drm_gem_object *target_obj;
275 uint32_t target_offset;
276 int ret = -EINVAL;
277
Chris Wilson67731b82010-12-08 10:38:14 +0000278 /* we've already hold a reference to all valid objects */
279 target_obj = &eb_get_object(eb, reloc->target_handle)->base;
280 if (unlikely(target_obj == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000281 return -ENOENT;
282
283 target_offset = to_intel_bo(target_obj)->gtt_offset;
284
285#if WATCH_RELOC
286 DRM_INFO("%s: obj %p offset %08x target %d "
287 "read %08x write %08x gtt %08x "
288 "presumed %08x delta %08x\n",
289 __func__,
290 obj,
291 (int) reloc->offset,
292 (int) reloc->target_handle,
293 (int) reloc->read_domains,
294 (int) reloc->write_domain,
295 (int) target_offset,
296 (int) reloc->presumed_offset,
297 reloc->delta);
298#endif
299
300 /* The target buffer should have appeared before us in the
301 * exec_object list, so it should have a GTT space bound by now.
302 */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000303 if (unlikely(target_offset == 0)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000304 DRM_ERROR("No GTT space found for object %d\n",
305 reloc->target_handle);
Chris Wilson67731b82010-12-08 10:38:14 +0000306 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000307 }
308
309 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000310 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000311 DRM_ERROR("reloc with multiple write domains: "
312 "obj %p target %d offset %d "
313 "read %08x write %08x",
314 obj, reloc->target_handle,
315 (int) reloc->offset,
316 reloc->read_domains,
317 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000318 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000319 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000320 if (unlikely((reloc->write_domain | reloc->read_domains) & I915_GEM_DOMAIN_CPU)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000321 DRM_ERROR("reloc with read/write CPU domains: "
322 "obj %p target %d offset %d "
323 "read %08x write %08x",
324 obj, reloc->target_handle,
325 (int) reloc->offset,
326 reloc->read_domains,
327 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000328 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000329 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000330 if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
331 reloc->write_domain != target_obj->pending_write_domain)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000332 DRM_ERROR("Write domain conflict: "
333 "obj %p target %d offset %d "
334 "new %08x old %08x\n",
335 obj, reloc->target_handle,
336 (int) reloc->offset,
337 reloc->write_domain,
338 target_obj->pending_write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000339 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000340 }
341
342 target_obj->pending_read_domains |= reloc->read_domains;
343 target_obj->pending_write_domain |= reloc->write_domain;
344
345 /* If the relocation already has the right value in it, no
346 * more work needs to be done.
347 */
348 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000349 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000350
351 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000352 if (unlikely(reloc->offset > obj->base.size - 4)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000353 DRM_ERROR("Relocation beyond object bounds: "
354 "obj %p target %d offset %d size %d.\n",
355 obj, reloc->target_handle,
356 (int) reloc->offset,
357 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000358 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000359 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000360 if (unlikely(reloc->offset & 3)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000361 DRM_ERROR("Relocation not 4-byte aligned: "
362 "obj %p target %d offset %d.\n",
363 obj, reloc->target_handle,
364 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000365 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000366 }
367
368 /* and points to somewhere within the target object. */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000369 if (unlikely(reloc->delta >= target_obj->size)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000370 DRM_ERROR("Relocation beyond target object bounds: "
371 "obj %p target %d delta %d size %d.\n",
372 obj, reloc->target_handle,
373 (int) reloc->delta,
374 (int) target_obj->size);
Chris Wilson67731b82010-12-08 10:38:14 +0000375 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000376 }
377
378 reloc->delta += target_offset;
379 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
380 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
381 char *vaddr;
382
383 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
384 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
385 kunmap_atomic(vaddr);
386 } else {
387 struct drm_i915_private *dev_priv = dev->dev_private;
388 uint32_t __iomem *reloc_entry;
389 void __iomem *reloc_page;
390
391 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
392 if (ret)
Chris Wilson67731b82010-12-08 10:38:14 +0000393 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000394
395 /* Map the page containing the relocation we're going to perform. */
396 reloc->offset += obj->gtt_offset;
397 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
398 reloc->offset & PAGE_MASK);
399 reloc_entry = (uint32_t __iomem *)
400 (reloc_page + (reloc->offset & ~PAGE_MASK));
401 iowrite32(reloc->delta, reloc_entry);
402 io_mapping_unmap_atomic(reloc_page);
403 }
404
405 /* and update the user's relocation entry */
406 reloc->presumed_offset = target_offset;
407
Chris Wilson67731b82010-12-08 10:38:14 +0000408 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000409}
410
411static int
412i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000413 struct eb_objects *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000414{
415 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000416 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000417 int i, ret;
418
419 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
420 for (i = 0; i < entry->relocation_count; i++) {
421 struct drm_i915_gem_relocation_entry reloc;
422
423 if (__copy_from_user_inatomic(&reloc,
424 user_relocs+i,
425 sizeof(reloc)))
426 return -EFAULT;
427
Chris Wilson6fe4f142011-01-10 17:35:37 +0000428 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &reloc);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000429 if (ret)
430 return ret;
431
432 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
433 &reloc.presumed_offset,
434 sizeof(reloc.presumed_offset)))
435 return -EFAULT;
436 }
437
438 return 0;
439}
440
441static int
442i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000443 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000444 struct drm_i915_gem_relocation_entry *relocs)
445{
Chris Wilson6fe4f142011-01-10 17:35:37 +0000446 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000447 int i, ret;
448
449 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000450 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000451 if (ret)
452 return ret;
453 }
454
455 return 0;
456}
457
458static int
459i915_gem_execbuffer_relocate(struct drm_device *dev,
Chris Wilson67731b82010-12-08 10:38:14 +0000460 struct eb_objects *eb,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000461 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000462{
Chris Wilson432e58e2010-11-25 19:32:06 +0000463 struct drm_i915_gem_object *obj;
464 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000465
Chris Wilson432e58e2010-11-25 19:32:06 +0000466 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000467 obj->base.pending_read_domains = 0;
468 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000469 ret = i915_gem_execbuffer_relocate_object(obj, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000470 if (ret)
471 return ret;
472 }
473
474 return 0;
475}
476
477static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000478i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000479 struct drm_file *file,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000480 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000481{
Chris Wilson432e58e2010-11-25 19:32:06 +0000482 struct drm_i915_gem_object *obj;
Chris Wilson432e58e2010-11-25 19:32:06 +0000483 int ret, retry;
Chris Wilson9b3826b2010-12-05 17:11:54 +0000484 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000485 struct list_head ordered_objects;
486
487 INIT_LIST_HEAD(&ordered_objects);
488 while (!list_empty(objects)) {
489 struct drm_i915_gem_exec_object2 *entry;
490 bool need_fence, need_mappable;
491
492 obj = list_first_entry(objects,
493 struct drm_i915_gem_object,
494 exec_list);
495 entry = obj->exec_entry;
496
497 need_fence =
498 has_fenced_gpu_access &&
499 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
500 obj->tiling_mode != I915_TILING_NONE;
501 need_mappable =
502 entry->relocation_count ? true : need_fence;
503
504 if (need_mappable)
505 list_move(&obj->exec_list, &ordered_objects);
506 else
507 list_move_tail(&obj->exec_list, &ordered_objects);
508 }
509 list_splice(&ordered_objects, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000510
511 /* Attempt to pin all of the buffers into the GTT.
512 * This is done in 3 phases:
513 *
514 * 1a. Unbind all objects that do not match the GTT constraints for
515 * the execbuffer (fenceable, mappable, alignment etc).
516 * 1b. Increment pin count for already bound objects.
517 * 2. Bind new objects.
518 * 3. Decrement pin count.
519 *
520 * This avoid unnecessary unbinding of later objects in order to makr
521 * room for the earlier objects *unless* we need to defragment.
522 */
523 retry = 0;
524 do {
525 ret = 0;
526
527 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000528 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000529 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000530 bool need_fence, need_mappable;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000531 if (!obj->gtt_space)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000532 continue;
533
534 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000535 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000536 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
537 obj->tiling_mode != I915_TILING_NONE;
538 need_mappable =
539 entry->relocation_count ? true : need_fence;
540
541 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
542 (need_mappable && !obj->map_and_fenceable))
543 ret = i915_gem_object_unbind(obj);
544 else
545 ret = i915_gem_object_pin(obj,
546 entry->alignment,
547 need_mappable);
Chris Wilson432e58e2010-11-25 19:32:06 +0000548 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000549 goto err;
Chris Wilson432e58e2010-11-25 19:32:06 +0000550
551 entry++;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000552 }
553
554 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000555 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000556 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000557 bool need_fence;
558
559 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000560 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000561 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
562 obj->tiling_mode != I915_TILING_NONE;
563
564 if (!obj->gtt_space) {
565 bool need_mappable =
566 entry->relocation_count ? true : need_fence;
567
568 ret = i915_gem_object_pin(obj,
569 entry->alignment,
570 need_mappable);
571 if (ret)
572 break;
573 }
574
Chris Wilson9b3826b2010-12-05 17:11:54 +0000575 if (has_fenced_gpu_access) {
576 if (need_fence) {
577 ret = i915_gem_object_get_fence(obj, ring, 1);
578 if (ret)
579 break;
580 } else if (entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
581 obj->tiling_mode == I915_TILING_NONE) {
582 /* XXX pipelined! */
583 ret = i915_gem_object_put_fence(obj);
584 if (ret)
585 break;
586 }
587 obj->pending_fenced_gpu_access = need_fence;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000588 }
589
590 entry->offset = obj->gtt_offset;
591 }
592
Chris Wilson432e58e2010-11-25 19:32:06 +0000593 /* Decrement pin count for bound objects */
594 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000595 if (obj->gtt_space)
596 i915_gem_object_unpin(obj);
597 }
598
599 if (ret != -ENOSPC || retry > 1)
600 return ret;
601
602 /* First attempt, just clear anything that is purgeable.
603 * Second attempt, clear the entire GTT.
604 */
Chris Wilsond9e86c02010-11-10 16:40:20 +0000605 ret = i915_gem_evict_everything(ring->dev, retry == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000606 if (ret)
607 return ret;
608
609 retry++;
610 } while (1);
Chris Wilson432e58e2010-11-25 19:32:06 +0000611
612err:
Chris Wilson602606a2010-11-28 15:31:02 +0000613 obj = list_entry(obj->exec_list.prev,
614 struct drm_i915_gem_object,
615 exec_list);
Chris Wilson432e58e2010-11-25 19:32:06 +0000616 while (objects != &obj->exec_list) {
617 if (obj->gtt_space)
618 i915_gem_object_unpin(obj);
619
620 obj = list_entry(obj->exec_list.prev,
621 struct drm_i915_gem_object,
622 exec_list);
623 }
624
625 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000626}
627
628static int
629i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
630 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000631 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000632 struct list_head *objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000633 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000634 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000635 int count)
636{
637 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000638 struct drm_i915_gem_object *obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000639 int i, total, ret;
640
Chris Wilson67731b82010-12-08 10:38:14 +0000641 /* We may process another execbuffer during the unlock... */
Chris Wilson36cf1742011-01-10 12:09:12 +0000642 while (!list_empty(objects)) {
Chris Wilson67731b82010-12-08 10:38:14 +0000643 obj = list_first_entry(objects,
644 struct drm_i915_gem_object,
645 exec_list);
646 list_del_init(&obj->exec_list);
647 drm_gem_object_unreference(&obj->base);
648 }
649
Chris Wilson54cf91d2010-11-25 18:00:26 +0000650 mutex_unlock(&dev->struct_mutex);
651
652 total = 0;
653 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000654 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000655
656 reloc = drm_malloc_ab(total, sizeof(*reloc));
657 if (reloc == NULL) {
658 mutex_lock(&dev->struct_mutex);
659 return -ENOMEM;
660 }
661
662 total = 0;
663 for (i = 0; i < count; i++) {
664 struct drm_i915_gem_relocation_entry __user *user_relocs;
665
Chris Wilson432e58e2010-11-25 19:32:06 +0000666 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000667
668 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000669 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000670 ret = -EFAULT;
671 mutex_lock(&dev->struct_mutex);
672 goto err;
673 }
674
Chris Wilson432e58e2010-11-25 19:32:06 +0000675 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000676 }
677
678 ret = i915_mutex_lock_interruptible(dev);
679 if (ret) {
680 mutex_lock(&dev->struct_mutex);
681 goto err;
682 }
683
Chris Wilson67731b82010-12-08 10:38:14 +0000684 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000685 eb_reset(eb);
686 for (i = 0; i < count; i++) {
687 struct drm_i915_gem_object *obj;
688
689 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
690 exec[i].handle));
691 if (obj == NULL) {
692 DRM_ERROR("Invalid object handle %d at index %d\n",
693 exec[i].handle, i);
694 ret = -ENOENT;
695 goto err;
696 }
697
698 list_add_tail(&obj->exec_list, objects);
699 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000700 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000701 eb_add_object(eb, obj);
702 }
703
Chris Wilson6fe4f142011-01-10 17:35:37 +0000704 ret = i915_gem_execbuffer_reserve(ring, file, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000705 if (ret)
706 goto err;
707
708 total = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000709 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000710 obj->base.pending_read_domains = 0;
711 obj->base.pending_write_domain = 0;
Chris Wilson67731b82010-12-08 10:38:14 +0000712 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000713 reloc + total);
714 if (ret)
715 goto err;
716
Chris Wilson432e58e2010-11-25 19:32:06 +0000717 total += exec->relocation_count;
718 exec++;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000719 }
720
721 /* Leave the user relocations as are, this is the painfully slow path,
722 * and we want to avoid the complication of dropping the lock whilst
723 * having buffers reserved in the aperture and so causing spurious
724 * ENOSPC for random operations.
725 */
726
727err:
728 drm_free_large(reloc);
729 return ret;
730}
731
Chris Wilson88241782011-01-07 17:09:48 +0000732static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000733i915_gem_execbuffer_flush(struct drm_device *dev,
734 uint32_t invalidate_domains,
735 uint32_t flush_domains,
736 uint32_t flush_rings)
737{
738 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson88241782011-01-07 17:09:48 +0000739 int i, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000740
741 if (flush_domains & I915_GEM_DOMAIN_CPU)
742 intel_gtt_chipset_flush();
743
Chris Wilson63256ec2011-01-04 18:42:07 +0000744 if (flush_domains & I915_GEM_DOMAIN_GTT)
745 wmb();
746
Chris Wilson54cf91d2010-11-25 18:00:26 +0000747 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000748 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilson88241782011-01-07 17:09:48 +0000749 if (flush_rings & (1 << i)) {
750 ret = i915_gem_flush_ring(dev,
751 &dev_priv->ring[i],
752 invalidate_domains,
753 flush_domains);
754 if (ret)
755 return ret;
756 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000757 }
Chris Wilson88241782011-01-07 17:09:48 +0000758
759 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000760}
761
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000762static int
763i915_gem_execbuffer_sync_rings(struct drm_i915_gem_object *obj,
764 struct intel_ring_buffer *to)
765{
766 struct intel_ring_buffer *from = obj->ring;
767 u32 seqno;
768 int ret, idx;
769
770 if (from == NULL || to == from)
771 return 0;
772
773 if (INTEL_INFO(obj->base.dev)->gen < 6)
774 return i915_gem_object_wait_rendering(obj, true);
775
776 idx = intel_ring_sync_index(from, to);
777
778 seqno = obj->last_rendering_seqno;
779 if (seqno <= from->sync_seqno[idx])
780 return 0;
781
782 if (seqno == from->outstanding_lazy_request) {
783 struct drm_i915_gem_request *request;
784
785 request = kzalloc(sizeof(*request), GFP_KERNEL);
786 if (request == NULL)
787 return -ENOMEM;
788
789 ret = i915_add_request(obj->base.dev, NULL, request, from);
790 if (ret) {
791 kfree(request);
792 return ret;
793 }
794
795 seqno = request->seqno;
796 }
797
798 from->sync_seqno[idx] = seqno;
799 return intel_ring_sync(to, from, seqno - 1);
800}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000801
802static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000803i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
804 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000805{
Chris Wilson432e58e2010-11-25 19:32:06 +0000806 struct drm_i915_gem_object *obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000807 struct change_domains cd;
Chris Wilson432e58e2010-11-25 19:32:06 +0000808 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000809
810 cd.invalidate_domains = 0;
811 cd.flush_domains = 0;
812 cd.flush_rings = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000813 list_for_each_entry(obj, objects, exec_list)
814 i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000815
816 if (cd.invalidate_domains | cd.flush_domains) {
817#if WATCH_EXEC
818 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
819 __func__,
820 cd.invalidate_domains,
821 cd.flush_domains);
822#endif
Chris Wilson88241782011-01-07 17:09:48 +0000823 ret = i915_gem_execbuffer_flush(ring->dev,
824 cd.invalidate_domains,
825 cd.flush_domains,
826 cd.flush_rings);
827 if (ret)
828 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000829 }
830
Chris Wilson432e58e2010-11-25 19:32:06 +0000831 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000832 ret = i915_gem_execbuffer_sync_rings(obj, ring);
833 if (ret)
834 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000835 }
836
837 return 0;
838}
839
Chris Wilson432e58e2010-11-25 19:32:06 +0000840static bool
841i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000842{
Chris Wilson432e58e2010-11-25 19:32:06 +0000843 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000844}
845
846static int
847validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
848 int count)
849{
850 int i;
851
852 for (i = 0; i < count; i++) {
853 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
854 int length; /* limited by fault_in_pages_readable() */
855
856 /* First check for malicious input causing overflow */
857 if (exec[i].relocation_count >
858 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
859 return -EINVAL;
860
861 length = exec[i].relocation_count *
862 sizeof(struct drm_i915_gem_relocation_entry);
863 if (!access_ok(VERIFY_READ, ptr, length))
864 return -EFAULT;
865
866 /* we may also need to update the presumed offsets */
867 if (!access_ok(VERIFY_WRITE, ptr, length))
868 return -EFAULT;
869
870 if (fault_in_pages_readable(ptr, length))
871 return -EFAULT;
872 }
873
874 return 0;
875}
876
Chris Wilson432e58e2010-11-25 19:32:06 +0000877static int
878i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring,
879 struct list_head *objects)
880{
881 struct drm_i915_gem_object *obj;
882 int flips;
883
884 /* Check for any pending flips. As we only maintain a flip queue depth
885 * of 1, we can simply insert a WAIT for the next display flip prior
886 * to executing the batch and avoid stalling the CPU.
887 */
888 flips = 0;
889 list_for_each_entry(obj, objects, exec_list) {
890 if (obj->base.write_domain)
891 flips |= atomic_read(&obj->pending_flip);
892 }
893 if (flips) {
894 int plane, flip_mask, ret;
895
896 for (plane = 0; flips >> plane; plane++) {
897 if (((flips >> plane) & 1) == 0)
898 continue;
899
900 if (plane)
901 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
902 else
903 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
904
905 ret = intel_ring_begin(ring, 2);
906 if (ret)
907 return ret;
908
909 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
910 intel_ring_emit(ring, MI_NOOP);
911 intel_ring_advance(ring);
912 }
913 }
914
915 return 0;
916}
917
918static void
919i915_gem_execbuffer_move_to_active(struct list_head *objects,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000920 struct intel_ring_buffer *ring,
921 u32 seqno)
Chris Wilson432e58e2010-11-25 19:32:06 +0000922{
923 struct drm_i915_gem_object *obj;
924
925 list_for_each_entry(obj, objects, exec_list) {
926 obj->base.read_domains = obj->base.pending_read_domains;
927 obj->base.write_domain = obj->base.pending_write_domain;
928 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
929
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000930 i915_gem_object_move_to_active(obj, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +0000931 if (obj->base.write_domain) {
932 obj->dirty = 1;
Chris Wilson87ca9c82010-12-02 09:42:56 +0000933 obj->pending_gpu_write = true;
Chris Wilson432e58e2010-11-25 19:32:06 +0000934 list_move_tail(&obj->gpu_write_list,
935 &ring->gpu_write_list);
936 intel_mark_busy(ring->dev, obj);
937 }
938
939 trace_i915_gem_object_change_domain(obj,
940 obj->base.read_domains,
941 obj->base.write_domain);
942 }
943}
944
Chris Wilson54cf91d2010-11-25 18:00:26 +0000945static void
946i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000947 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000948 struct intel_ring_buffer *ring)
949{
Chris Wilson432e58e2010-11-25 19:32:06 +0000950 struct drm_i915_gem_request *request;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000951 u32 invalidate;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000952
Chris Wilson432e58e2010-11-25 19:32:06 +0000953 /*
954 * Ensure that the commands in the batch buffer are
955 * finished before the interrupt fires.
956 *
957 * The sampler always gets flushed on i965 (sigh).
958 */
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000959 invalidate = I915_GEM_DOMAIN_COMMAND;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000960 if (INTEL_INFO(dev)->gen >= 4)
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000961 invalidate |= I915_GEM_DOMAIN_SAMPLER;
962 if (ring->flush(ring, invalidate, 0)) {
963 i915_gem_next_request_seqno(dev, ring);
964 return;
965 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000966
Chris Wilson432e58e2010-11-25 19:32:06 +0000967 /* Add a breadcrumb for the completion of the batch buffer */
968 request = kzalloc(sizeof(*request), GFP_KERNEL);
969 if (request == NULL || i915_add_request(dev, file, request, ring)) {
970 i915_gem_next_request_seqno(dev, ring);
971 kfree(request);
972 }
973}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000974
975static int
976i915_gem_do_execbuffer(struct drm_device *dev, void *data,
977 struct drm_file *file,
978 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +0000979 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000980{
981 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000982 struct list_head objects;
Chris Wilson67731b82010-12-08 10:38:14 +0000983 struct eb_objects *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000984 struct drm_i915_gem_object *batch_obj;
985 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000986 struct intel_ring_buffer *ring;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000987 u32 exec_start, exec_len;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000988 u32 seqno;
Chris Wilson72bfa192010-12-19 11:42:05 +0000989 int ret, mode, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000990
Chris Wilson432e58e2010-11-25 19:32:06 +0000991 if (!i915_gem_check_execbuffer(args)) {
992 DRM_ERROR("execbuf with invalid offset/length\n");
993 return -EINVAL;
994 }
995
996 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000997 if (ret)
998 return ret;
999
1000#if WATCH_EXEC
1001 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
1002 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
1003#endif
1004 switch (args->flags & I915_EXEC_RING_MASK) {
1005 case I915_EXEC_DEFAULT:
1006 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001007 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +00001008 break;
1009 case I915_EXEC_BSD:
1010 if (!HAS_BSD(dev)) {
1011 DRM_ERROR("execbuf with invalid ring (BSD)\n");
1012 return -EINVAL;
1013 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001014 ring = &dev_priv->ring[VCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +00001015 break;
1016 case I915_EXEC_BLT:
1017 if (!HAS_BLT(dev)) {
1018 DRM_ERROR("execbuf with invalid ring (BLT)\n");
1019 return -EINVAL;
1020 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001021 ring = &dev_priv->ring[BCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +00001022 break;
1023 default:
1024 DRM_ERROR("execbuf with unknown ring: %d\n",
1025 (int)(args->flags & I915_EXEC_RING_MASK));
1026 return -EINVAL;
1027 }
1028
Chris Wilson72bfa192010-12-19 11:42:05 +00001029 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1030 switch (mode) {
1031 case I915_EXEC_CONSTANTS_REL_GENERAL:
1032 case I915_EXEC_CONSTANTS_ABSOLUTE:
1033 case I915_EXEC_CONSTANTS_REL_SURFACE:
1034 if (ring == &dev_priv->ring[RCS] &&
1035 mode != dev_priv->relative_constants_mode) {
1036 if (INTEL_INFO(dev)->gen < 4)
1037 return -EINVAL;
1038
1039 if (INTEL_INFO(dev)->gen > 5 &&
1040 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1041 return -EINVAL;
1042
1043 ret = intel_ring_begin(ring, 4);
1044 if (ret)
1045 return ret;
1046
1047 intel_ring_emit(ring, MI_NOOP);
1048 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1049 intel_ring_emit(ring, INSTPM);
1050 intel_ring_emit(ring,
1051 I915_EXEC_CONSTANTS_MASK << 16 | mode);
1052 intel_ring_advance(ring);
1053
1054 dev_priv->relative_constants_mode = mode;
1055 }
1056 break;
1057 default:
1058 DRM_ERROR("execbuf with unknown constants: %d\n", mode);
1059 return -EINVAL;
1060 }
1061
Chris Wilson54cf91d2010-11-25 18:00:26 +00001062 if (args->buffer_count < 1) {
1063 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
1064 return -EINVAL;
1065 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001066
1067 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001068 if (ring != &dev_priv->ring[RCS]) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001069 DRM_ERROR("clip rectangles are only valid with the render ring\n");
1070 return -EINVAL;
1071 }
1072
Chris Wilson432e58e2010-11-25 19:32:06 +00001073 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001074 GFP_KERNEL);
1075 if (cliprects == NULL) {
1076 ret = -ENOMEM;
1077 goto pre_mutex_err;
1078 }
1079
Chris Wilson432e58e2010-11-25 19:32:06 +00001080 if (copy_from_user(cliprects,
1081 (struct drm_clip_rect __user *)(uintptr_t)
1082 args->cliprects_ptr,
1083 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001084 ret = -EFAULT;
1085 goto pre_mutex_err;
1086 }
1087 }
1088
Chris Wilson54cf91d2010-11-25 18:00:26 +00001089 ret = i915_mutex_lock_interruptible(dev);
1090 if (ret)
1091 goto pre_mutex_err;
1092
1093 if (dev_priv->mm.suspended) {
1094 mutex_unlock(&dev->struct_mutex);
1095 ret = -EBUSY;
1096 goto pre_mutex_err;
1097 }
1098
Chris Wilson67731b82010-12-08 10:38:14 +00001099 eb = eb_create(args->buffer_count);
1100 if (eb == NULL) {
1101 mutex_unlock(&dev->struct_mutex);
1102 ret = -ENOMEM;
1103 goto pre_mutex_err;
1104 }
1105
Chris Wilson54cf91d2010-11-25 18:00:26 +00001106 /* Look up object handles */
Chris Wilson432e58e2010-11-25 19:32:06 +00001107 INIT_LIST_HEAD(&objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001108 for (i = 0; i < args->buffer_count; i++) {
1109 struct drm_i915_gem_object *obj;
1110
Chris Wilson432e58e2010-11-25 19:32:06 +00001111 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
1112 exec[i].handle));
Chris Wilson54cf91d2010-11-25 18:00:26 +00001113 if (obj == NULL) {
1114 DRM_ERROR("Invalid object handle %d at index %d\n",
Chris Wilson432e58e2010-11-25 19:32:06 +00001115 exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001116 /* prevent error path from reading uninitialized data */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001117 ret = -ENOENT;
1118 goto err;
1119 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001120
Chris Wilson432e58e2010-11-25 19:32:06 +00001121 if (!list_empty(&obj->exec_list)) {
1122 DRM_ERROR("Object %p [handle %d, index %d] appears more than once in object list\n",
1123 obj, exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001124 ret = -EINVAL;
1125 goto err;
1126 }
Chris Wilson432e58e2010-11-25 19:32:06 +00001127
1128 list_add_tail(&obj->exec_list, &objects);
Chris Wilson67731b82010-12-08 10:38:14 +00001129 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +00001130 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +00001131 eb_add_object(eb, obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001132 }
1133
Chris Wilson6fe4f142011-01-10 17:35:37 +00001134 /* take note of the batch buffer before we might reorder the lists */
1135 batch_obj = list_entry(objects.prev,
1136 struct drm_i915_gem_object,
1137 exec_list);
1138
Chris Wilson54cf91d2010-11-25 18:00:26 +00001139 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilson6fe4f142011-01-10 17:35:37 +00001140 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001141 if (ret)
1142 goto err;
1143
1144 /* The objects are in their final locations, apply the relocations. */
Chris Wilson6fe4f142011-01-10 17:35:37 +00001145 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001146 if (ret) {
1147 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00001148 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilson67731b82010-12-08 10:38:14 +00001149 &objects, eb,
1150 exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001151 args->buffer_count);
1152 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1153 }
1154 if (ret)
1155 goto err;
1156 }
1157
1158 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001159 if (batch_obj->base.pending_write_domain) {
1160 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
1161 ret = -EINVAL;
1162 goto err;
1163 }
1164 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1165
Chris Wilson432e58e2010-11-25 19:32:06 +00001166 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001167 if (ret)
1168 goto err;
1169
Chris Wilson432e58e2010-11-25 19:32:06 +00001170 ret = i915_gem_execbuffer_wait_for_flips(ring, &objects);
1171 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001172 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001173
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001174 seqno = i915_gem_next_request_seqno(dev, ring);
1175 for (i = 0; i < I915_NUM_RINGS-1; i++) {
1176 if (seqno < ring->sync_seqno[i]) {
1177 /* The GPU can not handle its semaphore value wrapping,
1178 * so every billion or so execbuffers, we need to stall
1179 * the GPU in order to reset the counters.
1180 */
1181 ret = i915_gpu_idle(dev);
1182 if (ret)
1183 goto err;
1184
1185 BUG_ON(ring->sync_seqno[i]);
1186 }
1187 }
1188
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001189 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1190 exec_len = args->batch_len;
1191 if (cliprects) {
1192 for (i = 0; i < args->num_cliprects; i++) {
1193 ret = i915_emit_box(dev, &cliprects[i],
1194 args->DR1, args->DR4);
1195 if (ret)
1196 goto err;
1197
1198 ret = ring->dispatch_execbuffer(ring,
1199 exec_start, exec_len);
1200 if (ret)
1201 goto err;
1202 }
1203 } else {
1204 ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
1205 if (ret)
1206 goto err;
1207 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001208
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001209 i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +00001210 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001211
1212err:
Chris Wilson67731b82010-12-08 10:38:14 +00001213 eb_destroy(eb);
Chris Wilson432e58e2010-11-25 19:32:06 +00001214 while (!list_empty(&objects)) {
1215 struct drm_i915_gem_object *obj;
1216
1217 obj = list_first_entry(&objects,
1218 struct drm_i915_gem_object,
1219 exec_list);
1220 list_del_init(&obj->exec_list);
1221 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001222 }
1223
1224 mutex_unlock(&dev->struct_mutex);
1225
1226pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001227 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001228 return ret;
1229}
1230
1231/*
1232 * Legacy execbuffer just creates an exec2 list from the original exec object
1233 * list array and passes it to the real function.
1234 */
1235int
1236i915_gem_execbuffer(struct drm_device *dev, void *data,
1237 struct drm_file *file)
1238{
1239 struct drm_i915_gem_execbuffer *args = data;
1240 struct drm_i915_gem_execbuffer2 exec2;
1241 struct drm_i915_gem_exec_object *exec_list = NULL;
1242 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1243 int ret, i;
1244
1245#if WATCH_EXEC
1246 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
1247 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
1248#endif
1249
1250 if (args->buffer_count < 1) {
1251 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
1252 return -EINVAL;
1253 }
1254
1255 /* Copy in the exec list from userland */
1256 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1257 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1258 if (exec_list == NULL || exec2_list == NULL) {
1259 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
1260 args->buffer_count);
1261 drm_free_large(exec_list);
1262 drm_free_large(exec2_list);
1263 return -ENOMEM;
1264 }
1265 ret = copy_from_user(exec_list,
1266 (struct drm_i915_relocation_entry __user *)
1267 (uintptr_t) args->buffers_ptr,
1268 sizeof(*exec_list) * args->buffer_count);
1269 if (ret != 0) {
1270 DRM_ERROR("copy %d exec entries failed %d\n",
1271 args->buffer_count, ret);
1272 drm_free_large(exec_list);
1273 drm_free_large(exec2_list);
1274 return -EFAULT;
1275 }
1276
1277 for (i = 0; i < args->buffer_count; i++) {
1278 exec2_list[i].handle = exec_list[i].handle;
1279 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1280 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1281 exec2_list[i].alignment = exec_list[i].alignment;
1282 exec2_list[i].offset = exec_list[i].offset;
1283 if (INTEL_INFO(dev)->gen < 4)
1284 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1285 else
1286 exec2_list[i].flags = 0;
1287 }
1288
1289 exec2.buffers_ptr = args->buffers_ptr;
1290 exec2.buffer_count = args->buffer_count;
1291 exec2.batch_start_offset = args->batch_start_offset;
1292 exec2.batch_len = args->batch_len;
1293 exec2.DR1 = args->DR1;
1294 exec2.DR4 = args->DR4;
1295 exec2.num_cliprects = args->num_cliprects;
1296 exec2.cliprects_ptr = args->cliprects_ptr;
1297 exec2.flags = I915_EXEC_RENDER;
1298
1299 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1300 if (!ret) {
1301 /* Copy the new buffer offsets back to the user's exec list. */
1302 for (i = 0; i < args->buffer_count; i++)
1303 exec_list[i].offset = exec2_list[i].offset;
1304 /* ... and back out to userspace */
1305 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1306 (uintptr_t) args->buffers_ptr,
1307 exec_list,
1308 sizeof(*exec_list) * args->buffer_count);
1309 if (ret) {
1310 ret = -EFAULT;
1311 DRM_ERROR("failed to copy %d exec entries "
1312 "back to user (%d)\n",
1313 args->buffer_count, ret);
1314 }
1315 }
1316
1317 drm_free_large(exec_list);
1318 drm_free_large(exec2_list);
1319 return ret;
1320}
1321
1322int
1323i915_gem_execbuffer2(struct drm_device *dev, void *data,
1324 struct drm_file *file)
1325{
1326 struct drm_i915_gem_execbuffer2 *args = data;
1327 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1328 int ret;
1329
1330#if WATCH_EXEC
1331 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
1332 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
1333#endif
1334
1335 if (args->buffer_count < 1) {
1336 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
1337 return -EINVAL;
1338 }
1339
1340 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1341 if (exec2_list == NULL) {
1342 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
1343 args->buffer_count);
1344 return -ENOMEM;
1345 }
1346 ret = copy_from_user(exec2_list,
1347 (struct drm_i915_relocation_entry __user *)
1348 (uintptr_t) args->buffers_ptr,
1349 sizeof(*exec2_list) * args->buffer_count);
1350 if (ret != 0) {
1351 DRM_ERROR("copy %d exec entries failed %d\n",
1352 args->buffer_count, ret);
1353 drm_free_large(exec2_list);
1354 return -EFAULT;
1355 }
1356
1357 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1358 if (!ret) {
1359 /* Copy the new buffer offsets back to the user's exec list. */
1360 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1361 (uintptr_t) args->buffers_ptr,
1362 exec2_list,
1363 sizeof(*exec2_list) * args->buffer_count);
1364 if (ret) {
1365 ret = -EFAULT;
1366 DRM_ERROR("failed to copy %d exec entries "
1367 "back to user (%d)\n",
1368 args->buffer_count, ret);
1369 }
1370 }
1371
1372 drm_free_large(exec2_list);
1373 return ret;
1374}