blob: 8513c04dc892f71555d8601a1b5b92272eae8821 [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;
Chris Wilsonc59a3332011-03-06 13:51:29 +000040 uint32_t flips;
Chris Wilson54cf91d2010-11-25 18:00:26 +000041};
42
43/*
44 * Set the next domain for the specified object. This
45 * may not actually perform the necessary flushing/invaliding though,
46 * as that may want to be batched with other set_domain operations
47 *
48 * This is (we hope) the only really tricky part of gem. The goal
49 * is fairly simple -- track which caches hold bits of the object
50 * and make sure they remain coherent. A few concrete examples may
51 * help to explain how it works. For shorthand, we use the notation
52 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
53 * a pair of read and write domain masks.
54 *
55 * Case 1: the batch buffer
56 *
57 * 1. Allocated
58 * 2. Written by CPU
59 * 3. Mapped to GTT
60 * 4. Read by GPU
61 * 5. Unmapped from GTT
62 * 6. Freed
63 *
64 * Let's take these a step at a time
65 *
66 * 1. Allocated
67 * Pages allocated from the kernel may still have
68 * cache contents, so we set them to (CPU, CPU) always.
69 * 2. Written by CPU (using pwrite)
70 * The pwrite function calls set_domain (CPU, CPU) and
71 * this function does nothing (as nothing changes)
72 * 3. Mapped by GTT
73 * This function asserts that the object is not
74 * currently in any GPU-based read or write domains
75 * 4. Read by GPU
76 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
77 * As write_domain is zero, this function adds in the
78 * current read domains (CPU+COMMAND, 0).
79 * flush_domains is set to CPU.
80 * invalidate_domains is set to COMMAND
81 * clflush is run to get data out of the CPU caches
82 * then i915_dev_set_domain calls i915_gem_flush to
83 * emit an MI_FLUSH and drm_agp_chipset_flush
84 * 5. Unmapped from GTT
85 * i915_gem_object_unbind calls set_domain (CPU, CPU)
86 * flush_domains and invalidate_domains end up both zero
87 * so no flushing/invalidating happens
88 * 6. Freed
89 * yay, done
90 *
91 * Case 2: The shared render buffer
92 *
93 * 1. Allocated
94 * 2. Mapped to GTT
95 * 3. Read/written by GPU
96 * 4. set_domain to (CPU,CPU)
97 * 5. Read/written by CPU
98 * 6. Read/written by GPU
99 *
100 * 1. Allocated
101 * Same as last example, (CPU, CPU)
102 * 2. Mapped to GTT
103 * Nothing changes (assertions find that it is not in the GPU)
104 * 3. Read/written by GPU
105 * execbuffer calls set_domain (RENDER, RENDER)
106 * flush_domains gets CPU
107 * invalidate_domains gets GPU
108 * clflush (obj)
109 * MI_FLUSH and drm_agp_chipset_flush
110 * 4. set_domain (CPU, CPU)
111 * flush_domains gets GPU
112 * invalidate_domains gets CPU
113 * wait_rendering (obj) to make sure all drawing is complete.
114 * This will include an MI_FLUSH to get the data from GPU
115 * to memory
116 * clflush (obj) to invalidate the CPU cache
117 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
118 * 5. Read/written by CPU
119 * cache lines are loaded and dirtied
120 * 6. Read written by GPU
121 * Same as last GPU access
122 *
123 * Case 3: The constant buffer
124 *
125 * 1. Allocated
126 * 2. Written by CPU
127 * 3. Read by GPU
128 * 4. Updated (written) by CPU again
129 * 5. Read by GPU
130 *
131 * 1. Allocated
132 * (CPU, CPU)
133 * 2. Written by CPU
134 * (CPU, CPU)
135 * 3. Read by GPU
136 * (CPU+RENDER, 0)
137 * flush_domains = CPU
138 * invalidate_domains = RENDER
139 * clflush (obj)
140 * MI_FLUSH
141 * drm_agp_chipset_flush
142 * 4. Updated (written) by CPU again
143 * (CPU, CPU)
144 * flush_domains = 0 (no previous write domain)
145 * invalidate_domains = 0 (no new read domains)
146 * 5. Read by GPU
147 * (CPU+RENDER, 0)
148 * flush_domains = CPU
149 * invalidate_domains = RENDER
150 * clflush (obj)
151 * MI_FLUSH
152 * drm_agp_chipset_flush
153 */
154static void
155i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
156 struct intel_ring_buffer *ring,
157 struct change_domains *cd)
158{
159 uint32_t invalidate_domains = 0, flush_domains = 0;
160
161 /*
162 * If the object isn't moving to a new write domain,
163 * let the object stay in multiple read domains
164 */
165 if (obj->base.pending_write_domain == 0)
166 obj->base.pending_read_domains |= obj->base.read_domains;
167
168 /*
169 * Flush the current write domain if
170 * the new read domains don't match. Invalidate
171 * any read domains which differ from the old
172 * write domain
173 */
174 if (obj->base.write_domain &&
175 (((obj->base.write_domain != obj->base.pending_read_domains ||
176 obj->ring != ring)) ||
177 (obj->fenced_gpu_access && !obj->pending_fenced_gpu_access))) {
178 flush_domains |= obj->base.write_domain;
179 invalidate_domains |=
180 obj->base.pending_read_domains & ~obj->base.write_domain;
181 }
182 /*
183 * Invalidate any read caches which may have
184 * stale data. That is, any new read domains.
185 */
186 invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
187 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
188 i915_gem_clflush_object(obj);
189
190 /* blow away mappings if mapped through GTT */
191 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
192 i915_gem_release_mmap(obj);
193
Chris Wilsonc59a3332011-03-06 13:51:29 +0000194 if (obj->base.pending_write_domain)
195 cd->flips |= atomic_read(&obj->pending_flip);
196
Chris Wilson54cf91d2010-11-25 18:00:26 +0000197 /* The actual obj->write_domain will be updated with
198 * pending_write_domain after we emit the accumulated flush for all
199 * of our domain changes in execbuffers (which clears objects'
200 * write_domains). So if we have a current write domain that we
201 * aren't changing, set pending_write_domain to that.
202 */
203 if (flush_domains == 0 && obj->base.pending_write_domain == 0)
204 obj->base.pending_write_domain = obj->base.write_domain;
205
206 cd->invalidate_domains |= invalidate_domains;
207 cd->flush_domains |= flush_domains;
208 if (flush_domains & I915_GEM_GPU_DOMAINS)
209 cd->flush_rings |= obj->ring->id;
210 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
211 cd->flush_rings |= ring->id;
212}
213
Chris Wilson67731b82010-12-08 10:38:14 +0000214struct eb_objects {
215 int and;
216 struct hlist_head buckets[0];
217};
218
219static struct eb_objects *
220eb_create(int size)
221{
222 struct eb_objects *eb;
223 int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
224 while (count > size)
225 count >>= 1;
226 eb = kzalloc(count*sizeof(struct hlist_head) +
227 sizeof(struct eb_objects),
228 GFP_KERNEL);
229 if (eb == NULL)
230 return eb;
231
232 eb->and = count - 1;
233 return eb;
234}
235
236static void
237eb_reset(struct eb_objects *eb)
238{
239 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
240}
241
242static void
243eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj)
244{
245 hlist_add_head(&obj->exec_node,
246 &eb->buckets[obj->exec_handle & eb->and]);
247}
248
249static struct drm_i915_gem_object *
250eb_get_object(struct eb_objects *eb, unsigned long handle)
251{
252 struct hlist_head *head;
253 struct hlist_node *node;
254 struct drm_i915_gem_object *obj;
255
256 head = &eb->buckets[handle & eb->and];
257 hlist_for_each(node, head) {
258 obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
259 if (obj->exec_handle == handle)
260 return obj;
261 }
262
263 return NULL;
264}
265
266static void
267eb_destroy(struct eb_objects *eb)
268{
269 kfree(eb);
270}
271
Chris Wilson54cf91d2010-11-25 18:00:26 +0000272static int
273i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000274 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000275 struct drm_i915_gem_relocation_entry *reloc)
276{
277 struct drm_device *dev = obj->base.dev;
278 struct drm_gem_object *target_obj;
279 uint32_t target_offset;
280 int ret = -EINVAL;
281
Chris Wilson67731b82010-12-08 10:38:14 +0000282 /* we've already hold a reference to all valid objects */
283 target_obj = &eb_get_object(eb, reloc->target_handle)->base;
284 if (unlikely(target_obj == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000285 return -ENOENT;
286
287 target_offset = to_intel_bo(target_obj)->gtt_offset;
288
Chris Wilson54cf91d2010-11-25 18:00:26 +0000289 /* The target buffer should have appeared before us in the
290 * exec_object list, so it should have a GTT space bound by now.
291 */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000292 if (unlikely(target_offset == 0)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000293 DRM_ERROR("No GTT space found for object %d\n",
294 reloc->target_handle);
Chris Wilson67731b82010-12-08 10:38:14 +0000295 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000296 }
297
298 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000299 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000300 DRM_ERROR("reloc with multiple write domains: "
301 "obj %p target %d offset %d "
302 "read %08x write %08x",
303 obj, reloc->target_handle,
304 (int) reloc->offset,
305 reloc->read_domains,
306 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000307 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000308 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000309 if (unlikely((reloc->write_domain | reloc->read_domains) & I915_GEM_DOMAIN_CPU)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000310 DRM_ERROR("reloc with read/write CPU domains: "
311 "obj %p target %d offset %d "
312 "read %08x write %08x",
313 obj, reloc->target_handle,
314 (int) reloc->offset,
315 reloc->read_domains,
316 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000317 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000318 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000319 if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
320 reloc->write_domain != target_obj->pending_write_domain)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000321 DRM_ERROR("Write domain conflict: "
322 "obj %p target %d offset %d "
323 "new %08x old %08x\n",
324 obj, reloc->target_handle,
325 (int) reloc->offset,
326 reloc->write_domain,
327 target_obj->pending_write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000328 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000329 }
330
331 target_obj->pending_read_domains |= reloc->read_domains;
332 target_obj->pending_write_domain |= reloc->write_domain;
333
334 /* If the relocation already has the right value in it, no
335 * more work needs to be done.
336 */
337 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000338 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000339
340 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000341 if (unlikely(reloc->offset > obj->base.size - 4)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000342 DRM_ERROR("Relocation beyond object bounds: "
343 "obj %p target %d offset %d size %d.\n",
344 obj, reloc->target_handle,
345 (int) reloc->offset,
346 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000347 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000348 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000349 if (unlikely(reloc->offset & 3)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000350 DRM_ERROR("Relocation not 4-byte aligned: "
351 "obj %p target %d offset %d.\n",
352 obj, reloc->target_handle,
353 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000354 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000355 }
356
Chris Wilson54cf91d2010-11-25 18:00:26 +0000357 reloc->delta += target_offset;
358 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
359 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
360 char *vaddr;
361
362 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
363 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
364 kunmap_atomic(vaddr);
365 } else {
366 struct drm_i915_private *dev_priv = dev->dev_private;
367 uint32_t __iomem *reloc_entry;
368 void __iomem *reloc_page;
369
370 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
371 if (ret)
Chris Wilson67731b82010-12-08 10:38:14 +0000372 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000373
374 /* Map the page containing the relocation we're going to perform. */
375 reloc->offset += obj->gtt_offset;
376 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
377 reloc->offset & PAGE_MASK);
378 reloc_entry = (uint32_t __iomem *)
379 (reloc_page + (reloc->offset & ~PAGE_MASK));
380 iowrite32(reloc->delta, reloc_entry);
381 io_mapping_unmap_atomic(reloc_page);
382 }
383
384 /* and update the user's relocation entry */
385 reloc->presumed_offset = target_offset;
386
Chris Wilson67731b82010-12-08 10:38:14 +0000387 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000388}
389
390static int
391i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000392 struct eb_objects *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000393{
394 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000395 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000396 int i, ret;
397
398 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
399 for (i = 0; i < entry->relocation_count; i++) {
400 struct drm_i915_gem_relocation_entry reloc;
401
402 if (__copy_from_user_inatomic(&reloc,
403 user_relocs+i,
404 sizeof(reloc)))
405 return -EFAULT;
406
Chris Wilson6fe4f142011-01-10 17:35:37 +0000407 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &reloc);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000408 if (ret)
409 return ret;
410
411 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
412 &reloc.presumed_offset,
413 sizeof(reloc.presumed_offset)))
414 return -EFAULT;
415 }
416
417 return 0;
418}
419
420static int
421i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000422 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000423 struct drm_i915_gem_relocation_entry *relocs)
424{
Chris Wilson6fe4f142011-01-10 17:35:37 +0000425 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000426 int i, ret;
427
428 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000429 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000430 if (ret)
431 return ret;
432 }
433
434 return 0;
435}
436
437static int
438i915_gem_execbuffer_relocate(struct drm_device *dev,
Chris Wilson67731b82010-12-08 10:38:14 +0000439 struct eb_objects *eb,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000440 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000441{
Chris Wilson432e58e2010-11-25 19:32:06 +0000442 struct drm_i915_gem_object *obj;
443 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000444
Chris Wilson432e58e2010-11-25 19:32:06 +0000445 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000446 ret = i915_gem_execbuffer_relocate_object(obj, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000447 if (ret)
448 return ret;
449 }
450
451 return 0;
452}
453
454static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000455i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000456 struct drm_file *file,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000457 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000458{
Chris Wilson432e58e2010-11-25 19:32:06 +0000459 struct drm_i915_gem_object *obj;
Chris Wilson432e58e2010-11-25 19:32:06 +0000460 int ret, retry;
Chris Wilson9b3826b2010-12-05 17:11:54 +0000461 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000462 struct list_head ordered_objects;
463
464 INIT_LIST_HEAD(&ordered_objects);
465 while (!list_empty(objects)) {
466 struct drm_i915_gem_exec_object2 *entry;
467 bool need_fence, need_mappable;
468
469 obj = list_first_entry(objects,
470 struct drm_i915_gem_object,
471 exec_list);
472 entry = obj->exec_entry;
473
474 need_fence =
475 has_fenced_gpu_access &&
476 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
477 obj->tiling_mode != I915_TILING_NONE;
478 need_mappable =
479 entry->relocation_count ? true : need_fence;
480
481 if (need_mappable)
482 list_move(&obj->exec_list, &ordered_objects);
483 else
484 list_move_tail(&obj->exec_list, &ordered_objects);
Chris Wilson595dad72011-01-13 11:03:48 +0000485
486 obj->base.pending_read_domains = 0;
487 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000488 }
489 list_splice(&ordered_objects, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000490
491 /* Attempt to pin all of the buffers into the GTT.
492 * This is done in 3 phases:
493 *
494 * 1a. Unbind all objects that do not match the GTT constraints for
495 * the execbuffer (fenceable, mappable, alignment etc).
496 * 1b. Increment pin count for already bound objects.
497 * 2. Bind new objects.
498 * 3. Decrement pin count.
499 *
500 * This avoid unnecessary unbinding of later objects in order to makr
501 * room for the earlier objects *unless* we need to defragment.
502 */
503 retry = 0;
504 do {
505 ret = 0;
506
507 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000508 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000509 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000510 bool need_fence, need_mappable;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000511 if (!obj->gtt_space)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000512 continue;
513
514 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000515 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000516 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
517 obj->tiling_mode != I915_TILING_NONE;
518 need_mappable =
519 entry->relocation_count ? true : need_fence;
520
521 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
522 (need_mappable && !obj->map_and_fenceable))
523 ret = i915_gem_object_unbind(obj);
524 else
525 ret = i915_gem_object_pin(obj,
526 entry->alignment,
527 need_mappable);
Chris Wilson432e58e2010-11-25 19:32:06 +0000528 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000529 goto err;
Chris Wilson432e58e2010-11-25 19:32:06 +0000530
531 entry++;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000532 }
533
534 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000535 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000536 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000537 bool need_fence;
538
539 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000540 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000541 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
542 obj->tiling_mode != I915_TILING_NONE;
543
544 if (!obj->gtt_space) {
545 bool need_mappable =
546 entry->relocation_count ? true : need_fence;
547
548 ret = i915_gem_object_pin(obj,
549 entry->alignment,
550 need_mappable);
551 if (ret)
552 break;
553 }
554
Chris Wilson9b3826b2010-12-05 17:11:54 +0000555 if (has_fenced_gpu_access) {
556 if (need_fence) {
Chris Wilsonce453d82011-02-21 14:43:56 +0000557 ret = i915_gem_object_get_fence(obj, ring);
Chris Wilson9b3826b2010-12-05 17:11:54 +0000558 if (ret)
559 break;
560 } else if (entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
561 obj->tiling_mode == I915_TILING_NONE) {
562 /* XXX pipelined! */
563 ret = i915_gem_object_put_fence(obj);
564 if (ret)
565 break;
566 }
567 obj->pending_fenced_gpu_access = need_fence;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000568 }
569
570 entry->offset = obj->gtt_offset;
571 }
572
Chris Wilson432e58e2010-11-25 19:32:06 +0000573 /* Decrement pin count for bound objects */
574 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000575 if (obj->gtt_space)
576 i915_gem_object_unpin(obj);
577 }
578
579 if (ret != -ENOSPC || retry > 1)
580 return ret;
581
582 /* First attempt, just clear anything that is purgeable.
583 * Second attempt, clear the entire GTT.
584 */
Chris Wilsond9e86c02010-11-10 16:40:20 +0000585 ret = i915_gem_evict_everything(ring->dev, retry == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000586 if (ret)
587 return ret;
588
589 retry++;
590 } while (1);
Chris Wilson432e58e2010-11-25 19:32:06 +0000591
592err:
Chris Wilson602606a2010-11-28 15:31:02 +0000593 obj = list_entry(obj->exec_list.prev,
594 struct drm_i915_gem_object,
595 exec_list);
Chris Wilson432e58e2010-11-25 19:32:06 +0000596 while (objects != &obj->exec_list) {
597 if (obj->gtt_space)
598 i915_gem_object_unpin(obj);
599
600 obj = list_entry(obj->exec_list.prev,
601 struct drm_i915_gem_object,
602 exec_list);
603 }
604
605 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000606}
607
608static int
609i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
610 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000611 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000612 struct list_head *objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000613 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000614 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000615 int count)
616{
617 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000618 struct drm_i915_gem_object *obj;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000619 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000620 int i, total, ret;
621
Chris Wilson67731b82010-12-08 10:38:14 +0000622 /* We may process another execbuffer during the unlock... */
Chris Wilson36cf1742011-01-10 12:09:12 +0000623 while (!list_empty(objects)) {
Chris Wilson67731b82010-12-08 10:38:14 +0000624 obj = list_first_entry(objects,
625 struct drm_i915_gem_object,
626 exec_list);
627 list_del_init(&obj->exec_list);
628 drm_gem_object_unreference(&obj->base);
629 }
630
Chris Wilson54cf91d2010-11-25 18:00:26 +0000631 mutex_unlock(&dev->struct_mutex);
632
633 total = 0;
634 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000635 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000636
Chris Wilsondd6864a2011-01-12 23:49:13 +0000637 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000638 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000639 if (reloc == NULL || reloc_offset == NULL) {
640 drm_free_large(reloc);
641 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000642 mutex_lock(&dev->struct_mutex);
643 return -ENOMEM;
644 }
645
646 total = 0;
647 for (i = 0; i < count; i++) {
648 struct drm_i915_gem_relocation_entry __user *user_relocs;
649
Chris Wilson432e58e2010-11-25 19:32:06 +0000650 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000651
652 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000653 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000654 ret = -EFAULT;
655 mutex_lock(&dev->struct_mutex);
656 goto err;
657 }
658
Chris Wilsondd6864a2011-01-12 23:49:13 +0000659 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000660 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000661 }
662
663 ret = i915_mutex_lock_interruptible(dev);
664 if (ret) {
665 mutex_lock(&dev->struct_mutex);
666 goto err;
667 }
668
Chris Wilson67731b82010-12-08 10:38:14 +0000669 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000670 eb_reset(eb);
671 for (i = 0; i < count; i++) {
Chris Wilson67731b82010-12-08 10:38:14 +0000672 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
673 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000674 if (&obj->base == NULL) {
Chris Wilson67731b82010-12-08 10:38:14 +0000675 DRM_ERROR("Invalid object handle %d at index %d\n",
676 exec[i].handle, i);
677 ret = -ENOENT;
678 goto err;
679 }
680
681 list_add_tail(&obj->exec_list, objects);
682 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000683 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000684 eb_add_object(eb, obj);
685 }
686
Chris Wilson6fe4f142011-01-10 17:35:37 +0000687 ret = i915_gem_execbuffer_reserve(ring, file, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000688 if (ret)
689 goto err;
690
Chris Wilson432e58e2010-11-25 19:32:06 +0000691 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondd6864a2011-01-12 23:49:13 +0000692 int offset = obj->exec_entry - exec;
Chris Wilson67731b82010-12-08 10:38:14 +0000693 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilsondd6864a2011-01-12 23:49:13 +0000694 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000695 if (ret)
696 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000697 }
698
699 /* Leave the user relocations as are, this is the painfully slow path,
700 * and we want to avoid the complication of dropping the lock whilst
701 * having buffers reserved in the aperture and so causing spurious
702 * ENOSPC for random operations.
703 */
704
705err:
706 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000707 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000708 return ret;
709}
710
Chris Wilson88241782011-01-07 17:09:48 +0000711static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000712i915_gem_execbuffer_flush(struct drm_device *dev,
713 uint32_t invalidate_domains,
714 uint32_t flush_domains,
715 uint32_t flush_rings)
716{
717 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson88241782011-01-07 17:09:48 +0000718 int i, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000719
720 if (flush_domains & I915_GEM_DOMAIN_CPU)
721 intel_gtt_chipset_flush();
722
Chris Wilson63256ec2011-01-04 18:42:07 +0000723 if (flush_domains & I915_GEM_DOMAIN_GTT)
724 wmb();
725
Chris Wilson54cf91d2010-11-25 18:00:26 +0000726 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000727 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilson88241782011-01-07 17:09:48 +0000728 if (flush_rings & (1 << i)) {
Chris Wilsondb53a302011-02-03 11:57:46 +0000729 ret = i915_gem_flush_ring(&dev_priv->ring[i],
Chris Wilson88241782011-01-07 17:09:48 +0000730 invalidate_domains,
731 flush_domains);
732 if (ret)
733 return ret;
734 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000735 }
Chris Wilson88241782011-01-07 17:09:48 +0000736
737 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000738}
739
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000740static int
741i915_gem_execbuffer_sync_rings(struct drm_i915_gem_object *obj,
742 struct intel_ring_buffer *to)
743{
744 struct intel_ring_buffer *from = obj->ring;
745 u32 seqno;
746 int ret, idx;
747
748 if (from == NULL || to == from)
749 return 0;
750
Chris Wilsone8b2c3c2011-03-01 19:22:52 +0000751 if (INTEL_INFO(obj->base.dev)->gen < 6)
Chris Wilsonce453d82011-02-21 14:43:56 +0000752 return i915_gem_object_wait_rendering(obj);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000753
754 idx = intel_ring_sync_index(from, to);
755
756 seqno = obj->last_rendering_seqno;
757 if (seqno <= from->sync_seqno[idx])
758 return 0;
759
760 if (seqno == from->outstanding_lazy_request) {
761 struct drm_i915_gem_request *request;
762
763 request = kzalloc(sizeof(*request), GFP_KERNEL);
764 if (request == NULL)
765 return -ENOMEM;
766
Chris Wilsondb53a302011-02-03 11:57:46 +0000767 ret = i915_add_request(from, NULL, request);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000768 if (ret) {
769 kfree(request);
770 return ret;
771 }
772
773 seqno = request->seqno;
774 }
775
776 from->sync_seqno[idx] = seqno;
777 return intel_ring_sync(to, from, seqno - 1);
778}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000779
780static int
Chris Wilsonc59a3332011-03-06 13:51:29 +0000781i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
782{
783 u32 plane, flip_mask;
784 int ret;
785
786 /* Check for any pending flips. As we only maintain a flip queue depth
787 * of 1, we can simply insert a WAIT for the next display flip prior
788 * to executing the batch and avoid stalling the CPU.
789 */
790
791 for (plane = 0; flips >> plane; plane++) {
792 if (((flips >> plane) & 1) == 0)
793 continue;
794
795 if (plane)
796 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
797 else
798 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
799
800 ret = intel_ring_begin(ring, 2);
801 if (ret)
802 return ret;
803
804 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
805 intel_ring_emit(ring, MI_NOOP);
806 intel_ring_advance(ring);
807 }
808
809 return 0;
810}
811
812
813static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000814i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
815 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000816{
Chris Wilson432e58e2010-11-25 19:32:06 +0000817 struct drm_i915_gem_object *obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000818 struct change_domains cd;
Chris Wilson432e58e2010-11-25 19:32:06 +0000819 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000820
Chris Wilsonc59a3332011-03-06 13:51:29 +0000821 memset(&cd, 0, sizeof(cd));
Chris Wilson432e58e2010-11-25 19:32:06 +0000822 list_for_each_entry(obj, objects, exec_list)
823 i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000824
825 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson88241782011-01-07 17:09:48 +0000826 ret = i915_gem_execbuffer_flush(ring->dev,
827 cd.invalidate_domains,
828 cd.flush_domains,
829 cd.flush_rings);
830 if (ret)
831 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000832 }
833
Chris Wilsonc59a3332011-03-06 13:51:29 +0000834 if (cd.flips) {
835 ret = i915_gem_execbuffer_wait_for_flips(ring, cd.flips);
836 if (ret)
837 return ret;
838 }
839
Chris Wilson432e58e2010-11-25 19:32:06 +0000840 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000841 ret = i915_gem_execbuffer_sync_rings(obj, ring);
842 if (ret)
843 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000844 }
845
846 return 0;
847}
848
Chris Wilson432e58e2010-11-25 19:32:06 +0000849static bool
850i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000851{
Chris Wilson432e58e2010-11-25 19:32:06 +0000852 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000853}
854
855static int
856validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
857 int count)
858{
859 int i;
860
861 for (i = 0; i < count; i++) {
862 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
863 int length; /* limited by fault_in_pages_readable() */
864
865 /* First check for malicious input causing overflow */
866 if (exec[i].relocation_count >
867 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
868 return -EINVAL;
869
870 length = exec[i].relocation_count *
871 sizeof(struct drm_i915_gem_relocation_entry);
872 if (!access_ok(VERIFY_READ, ptr, length))
873 return -EFAULT;
874
875 /* we may also need to update the presumed offsets */
876 if (!access_ok(VERIFY_WRITE, ptr, length))
877 return -EFAULT;
878
879 if (fault_in_pages_readable(ptr, length))
880 return -EFAULT;
881 }
882
883 return 0;
884}
885
Chris Wilson432e58e2010-11-25 19:32:06 +0000886static void
887i915_gem_execbuffer_move_to_active(struct list_head *objects,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000888 struct intel_ring_buffer *ring,
889 u32 seqno)
Chris Wilson432e58e2010-11-25 19:32:06 +0000890{
891 struct drm_i915_gem_object *obj;
892
893 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondb53a302011-02-03 11:57:46 +0000894 u32 old_read = obj->base.read_domains;
895 u32 old_write = obj->base.write_domain;
896
897
Chris Wilson432e58e2010-11-25 19:32:06 +0000898 obj->base.read_domains = obj->base.pending_read_domains;
899 obj->base.write_domain = obj->base.pending_write_domain;
900 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
901
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000902 i915_gem_object_move_to_active(obj, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +0000903 if (obj->base.write_domain) {
904 obj->dirty = 1;
Chris Wilson87ca9c82010-12-02 09:42:56 +0000905 obj->pending_gpu_write = true;
Chris Wilson432e58e2010-11-25 19:32:06 +0000906 list_move_tail(&obj->gpu_write_list,
907 &ring->gpu_write_list);
908 intel_mark_busy(ring->dev, obj);
909 }
910
Chris Wilsondb53a302011-02-03 11:57:46 +0000911 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000912 }
913}
914
Chris Wilson54cf91d2010-11-25 18:00:26 +0000915static void
916i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000917 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000918 struct intel_ring_buffer *ring)
919{
Chris Wilson432e58e2010-11-25 19:32:06 +0000920 struct drm_i915_gem_request *request;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000921 u32 invalidate;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000922
Chris Wilson432e58e2010-11-25 19:32:06 +0000923 /*
924 * Ensure that the commands in the batch buffer are
925 * finished before the interrupt fires.
926 *
927 * The sampler always gets flushed on i965 (sigh).
928 */
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000929 invalidate = I915_GEM_DOMAIN_COMMAND;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000930 if (INTEL_INFO(dev)->gen >= 4)
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000931 invalidate |= I915_GEM_DOMAIN_SAMPLER;
932 if (ring->flush(ring, invalidate, 0)) {
Chris Wilsondb53a302011-02-03 11:57:46 +0000933 i915_gem_next_request_seqno(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000934 return;
935 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000936
Chris Wilson432e58e2010-11-25 19:32:06 +0000937 /* Add a breadcrumb for the completion of the batch buffer */
938 request = kzalloc(sizeof(*request), GFP_KERNEL);
Chris Wilsondb53a302011-02-03 11:57:46 +0000939 if (request == NULL || i915_add_request(ring, file, request)) {
940 i915_gem_next_request_seqno(ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000941 kfree(request);
942 }
943}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000944
945static int
946i915_gem_do_execbuffer(struct drm_device *dev, void *data,
947 struct drm_file *file,
948 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +0000949 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000950{
951 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000952 struct list_head objects;
Chris Wilson67731b82010-12-08 10:38:14 +0000953 struct eb_objects *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000954 struct drm_i915_gem_object *batch_obj;
955 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000956 struct intel_ring_buffer *ring;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000957 u32 exec_start, exec_len;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000958 u32 seqno;
Chris Wilson72bfa192010-12-19 11:42:05 +0000959 int ret, mode, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000960
Chris Wilson432e58e2010-11-25 19:32:06 +0000961 if (!i915_gem_check_execbuffer(args)) {
962 DRM_ERROR("execbuf with invalid offset/length\n");
963 return -EINVAL;
964 }
965
966 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000967 if (ret)
968 return ret;
969
Chris Wilson54cf91d2010-11-25 18:00:26 +0000970 switch (args->flags & I915_EXEC_RING_MASK) {
971 case I915_EXEC_DEFAULT:
972 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000973 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000974 break;
975 case I915_EXEC_BSD:
976 if (!HAS_BSD(dev)) {
977 DRM_ERROR("execbuf with invalid ring (BSD)\n");
978 return -EINVAL;
979 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000980 ring = &dev_priv->ring[VCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000981 break;
982 case I915_EXEC_BLT:
983 if (!HAS_BLT(dev)) {
984 DRM_ERROR("execbuf with invalid ring (BLT)\n");
985 return -EINVAL;
986 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000987 ring = &dev_priv->ring[BCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000988 break;
989 default:
990 DRM_ERROR("execbuf with unknown ring: %d\n",
991 (int)(args->flags & I915_EXEC_RING_MASK));
992 return -EINVAL;
993 }
994
Chris Wilson72bfa192010-12-19 11:42:05 +0000995 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
996 switch (mode) {
997 case I915_EXEC_CONSTANTS_REL_GENERAL:
998 case I915_EXEC_CONSTANTS_ABSOLUTE:
999 case I915_EXEC_CONSTANTS_REL_SURFACE:
1000 if (ring == &dev_priv->ring[RCS] &&
1001 mode != dev_priv->relative_constants_mode) {
1002 if (INTEL_INFO(dev)->gen < 4)
1003 return -EINVAL;
1004
1005 if (INTEL_INFO(dev)->gen > 5 &&
1006 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1007 return -EINVAL;
1008
1009 ret = intel_ring_begin(ring, 4);
1010 if (ret)
1011 return ret;
1012
1013 intel_ring_emit(ring, MI_NOOP);
1014 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1015 intel_ring_emit(ring, INSTPM);
1016 intel_ring_emit(ring,
1017 I915_EXEC_CONSTANTS_MASK << 16 | mode);
1018 intel_ring_advance(ring);
1019
1020 dev_priv->relative_constants_mode = mode;
1021 }
1022 break;
1023 default:
1024 DRM_ERROR("execbuf with unknown constants: %d\n", mode);
1025 return -EINVAL;
1026 }
1027
Chris Wilson54cf91d2010-11-25 18:00:26 +00001028 if (args->buffer_count < 1) {
1029 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
1030 return -EINVAL;
1031 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001032
1033 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001034 if (ring != &dev_priv->ring[RCS]) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001035 DRM_ERROR("clip rectangles are only valid with the render ring\n");
1036 return -EINVAL;
1037 }
1038
Chris Wilson432e58e2010-11-25 19:32:06 +00001039 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001040 GFP_KERNEL);
1041 if (cliprects == NULL) {
1042 ret = -ENOMEM;
1043 goto pre_mutex_err;
1044 }
1045
Chris Wilson432e58e2010-11-25 19:32:06 +00001046 if (copy_from_user(cliprects,
1047 (struct drm_clip_rect __user *)(uintptr_t)
1048 args->cliprects_ptr,
1049 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001050 ret = -EFAULT;
1051 goto pre_mutex_err;
1052 }
1053 }
1054
Chris Wilson54cf91d2010-11-25 18:00:26 +00001055 ret = i915_mutex_lock_interruptible(dev);
1056 if (ret)
1057 goto pre_mutex_err;
1058
1059 if (dev_priv->mm.suspended) {
1060 mutex_unlock(&dev->struct_mutex);
1061 ret = -EBUSY;
1062 goto pre_mutex_err;
1063 }
1064
Chris Wilson67731b82010-12-08 10:38:14 +00001065 eb = eb_create(args->buffer_count);
1066 if (eb == NULL) {
1067 mutex_unlock(&dev->struct_mutex);
1068 ret = -ENOMEM;
1069 goto pre_mutex_err;
1070 }
1071
Chris Wilson54cf91d2010-11-25 18:00:26 +00001072 /* Look up object handles */
Chris Wilson432e58e2010-11-25 19:32:06 +00001073 INIT_LIST_HEAD(&objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001074 for (i = 0; i < args->buffer_count; i++) {
1075 struct drm_i915_gem_object *obj;
1076
Chris Wilson432e58e2010-11-25 19:32:06 +00001077 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
1078 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001079 if (&obj->base == NULL) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001080 DRM_ERROR("Invalid object handle %d at index %d\n",
Chris Wilson432e58e2010-11-25 19:32:06 +00001081 exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001082 /* prevent error path from reading uninitialized data */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001083 ret = -ENOENT;
1084 goto err;
1085 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001086
Chris Wilson432e58e2010-11-25 19:32:06 +00001087 if (!list_empty(&obj->exec_list)) {
1088 DRM_ERROR("Object %p [handle %d, index %d] appears more than once in object list\n",
1089 obj, exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001090 ret = -EINVAL;
1091 goto err;
1092 }
Chris Wilson432e58e2010-11-25 19:32:06 +00001093
1094 list_add_tail(&obj->exec_list, &objects);
Chris Wilson67731b82010-12-08 10:38:14 +00001095 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +00001096 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +00001097 eb_add_object(eb, obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001098 }
1099
Chris Wilson6fe4f142011-01-10 17:35:37 +00001100 /* take note of the batch buffer before we might reorder the lists */
1101 batch_obj = list_entry(objects.prev,
1102 struct drm_i915_gem_object,
1103 exec_list);
1104
Chris Wilson54cf91d2010-11-25 18:00:26 +00001105 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilson6fe4f142011-01-10 17:35:37 +00001106 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001107 if (ret)
1108 goto err;
1109
1110 /* The objects are in their final locations, apply the relocations. */
Chris Wilson6fe4f142011-01-10 17:35:37 +00001111 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001112 if (ret) {
1113 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00001114 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilson67731b82010-12-08 10:38:14 +00001115 &objects, eb,
1116 exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001117 args->buffer_count);
1118 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1119 }
1120 if (ret)
1121 goto err;
1122 }
1123
1124 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001125 if (batch_obj->base.pending_write_domain) {
1126 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
1127 ret = -EINVAL;
1128 goto err;
1129 }
1130 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1131
Chris Wilson432e58e2010-11-25 19:32:06 +00001132 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001133 if (ret)
1134 goto err;
1135
Chris Wilsondb53a302011-02-03 11:57:46 +00001136 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson076e2c02011-01-21 10:07:18 +00001137 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001138 if (seqno < ring->sync_seqno[i]) {
1139 /* The GPU can not handle its semaphore value wrapping,
1140 * so every billion or so execbuffers, we need to stall
1141 * the GPU in order to reset the counters.
1142 */
1143 ret = i915_gpu_idle(dev);
1144 if (ret)
1145 goto err;
1146
1147 BUG_ON(ring->sync_seqno[i]);
1148 }
1149 }
1150
Chris Wilsondb53a302011-02-03 11:57:46 +00001151 trace_i915_gem_ring_dispatch(ring, seqno);
1152
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001153 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1154 exec_len = args->batch_len;
1155 if (cliprects) {
1156 for (i = 0; i < args->num_cliprects; i++) {
1157 ret = i915_emit_box(dev, &cliprects[i],
1158 args->DR1, args->DR4);
1159 if (ret)
1160 goto err;
1161
1162 ret = ring->dispatch_execbuffer(ring,
1163 exec_start, exec_len);
1164 if (ret)
1165 goto err;
1166 }
1167 } else {
1168 ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
1169 if (ret)
1170 goto err;
1171 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001172
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001173 i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +00001174 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001175
1176err:
Chris Wilson67731b82010-12-08 10:38:14 +00001177 eb_destroy(eb);
Chris Wilson432e58e2010-11-25 19:32:06 +00001178 while (!list_empty(&objects)) {
1179 struct drm_i915_gem_object *obj;
1180
1181 obj = list_first_entry(&objects,
1182 struct drm_i915_gem_object,
1183 exec_list);
1184 list_del_init(&obj->exec_list);
1185 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001186 }
1187
1188 mutex_unlock(&dev->struct_mutex);
1189
1190pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001191 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001192 return ret;
1193}
1194
1195/*
1196 * Legacy execbuffer just creates an exec2 list from the original exec object
1197 * list array and passes it to the real function.
1198 */
1199int
1200i915_gem_execbuffer(struct drm_device *dev, void *data,
1201 struct drm_file *file)
1202{
1203 struct drm_i915_gem_execbuffer *args = data;
1204 struct drm_i915_gem_execbuffer2 exec2;
1205 struct drm_i915_gem_exec_object *exec_list = NULL;
1206 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1207 int ret, i;
1208
Chris Wilson54cf91d2010-11-25 18:00:26 +00001209 if (args->buffer_count < 1) {
1210 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
1211 return -EINVAL;
1212 }
1213
1214 /* Copy in the exec list from userland */
1215 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1216 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1217 if (exec_list == NULL || exec2_list == NULL) {
1218 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
1219 args->buffer_count);
1220 drm_free_large(exec_list);
1221 drm_free_large(exec2_list);
1222 return -ENOMEM;
1223 }
1224 ret = copy_from_user(exec_list,
1225 (struct drm_i915_relocation_entry __user *)
1226 (uintptr_t) args->buffers_ptr,
1227 sizeof(*exec_list) * args->buffer_count);
1228 if (ret != 0) {
1229 DRM_ERROR("copy %d exec entries failed %d\n",
1230 args->buffer_count, ret);
1231 drm_free_large(exec_list);
1232 drm_free_large(exec2_list);
1233 return -EFAULT;
1234 }
1235
1236 for (i = 0; i < args->buffer_count; i++) {
1237 exec2_list[i].handle = exec_list[i].handle;
1238 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1239 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1240 exec2_list[i].alignment = exec_list[i].alignment;
1241 exec2_list[i].offset = exec_list[i].offset;
1242 if (INTEL_INFO(dev)->gen < 4)
1243 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1244 else
1245 exec2_list[i].flags = 0;
1246 }
1247
1248 exec2.buffers_ptr = args->buffers_ptr;
1249 exec2.buffer_count = args->buffer_count;
1250 exec2.batch_start_offset = args->batch_start_offset;
1251 exec2.batch_len = args->batch_len;
1252 exec2.DR1 = args->DR1;
1253 exec2.DR4 = args->DR4;
1254 exec2.num_cliprects = args->num_cliprects;
1255 exec2.cliprects_ptr = args->cliprects_ptr;
1256 exec2.flags = I915_EXEC_RENDER;
1257
1258 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1259 if (!ret) {
1260 /* Copy the new buffer offsets back to the user's exec list. */
1261 for (i = 0; i < args->buffer_count; i++)
1262 exec_list[i].offset = exec2_list[i].offset;
1263 /* ... and back out to userspace */
1264 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1265 (uintptr_t) args->buffers_ptr,
1266 exec_list,
1267 sizeof(*exec_list) * args->buffer_count);
1268 if (ret) {
1269 ret = -EFAULT;
1270 DRM_ERROR("failed to copy %d exec entries "
1271 "back to user (%d)\n",
1272 args->buffer_count, ret);
1273 }
1274 }
1275
1276 drm_free_large(exec_list);
1277 drm_free_large(exec2_list);
1278 return ret;
1279}
1280
1281int
1282i915_gem_execbuffer2(struct drm_device *dev, void *data,
1283 struct drm_file *file)
1284{
1285 struct drm_i915_gem_execbuffer2 *args = data;
1286 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1287 int ret;
1288
Chris Wilson54cf91d2010-11-25 18:00:26 +00001289 if (args->buffer_count < 1) {
1290 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
1291 return -EINVAL;
1292 }
1293
Chris Wilson8408c282011-02-21 12:54:48 +00001294 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1295 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1296 if (exec2_list == NULL)
1297 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1298 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001299 if (exec2_list == NULL) {
1300 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
1301 args->buffer_count);
1302 return -ENOMEM;
1303 }
1304 ret = copy_from_user(exec2_list,
1305 (struct drm_i915_relocation_entry __user *)
1306 (uintptr_t) args->buffers_ptr,
1307 sizeof(*exec2_list) * args->buffer_count);
1308 if (ret != 0) {
1309 DRM_ERROR("copy %d exec entries failed %d\n",
1310 args->buffer_count, ret);
1311 drm_free_large(exec2_list);
1312 return -EFAULT;
1313 }
1314
1315 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1316 if (!ret) {
1317 /* Copy the new buffer offsets back to the user's exec list. */
1318 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1319 (uintptr_t) args->buffers_ptr,
1320 exec2_list,
1321 sizeof(*exec2_list) * args->buffer_count);
1322 if (ret) {
1323 ret = -EFAULT;
1324 DRM_ERROR("failed to copy %d exec entries "
1325 "back to user (%d)\n",
1326 args->buffer_count, ret);
1327 }
1328 }
1329
1330 drm_free_large(exec2_list);
1331 return ret;
1332}