blob: d461ad5f929020c0f2b8c4d96fee44f302815bc5 [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
Chris Wilson54cf91d2010-11-25 18:00:26 +0000285 /* The target buffer should have appeared before us in the
286 * exec_object list, so it should have a GTT space bound by now.
287 */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000288 if (unlikely(target_offset == 0)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000289 DRM_ERROR("No GTT space found for object %d\n",
290 reloc->target_handle);
Chris Wilson67731b82010-12-08 10:38:14 +0000291 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000292 }
293
294 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000295 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000296 DRM_ERROR("reloc with multiple write domains: "
297 "obj %p target %d offset %d "
298 "read %08x write %08x",
299 obj, reloc->target_handle,
300 (int) reloc->offset,
301 reloc->read_domains,
302 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000303 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000304 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000305 if (unlikely((reloc->write_domain | reloc->read_domains) & I915_GEM_DOMAIN_CPU)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000306 DRM_ERROR("reloc with read/write CPU domains: "
307 "obj %p target %d offset %d "
308 "read %08x write %08x",
309 obj, reloc->target_handle,
310 (int) reloc->offset,
311 reloc->read_domains,
312 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000313 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000314 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000315 if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
316 reloc->write_domain != target_obj->pending_write_domain)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000317 DRM_ERROR("Write domain conflict: "
318 "obj %p target %d offset %d "
319 "new %08x old %08x\n",
320 obj, reloc->target_handle,
321 (int) reloc->offset,
322 reloc->write_domain,
323 target_obj->pending_write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000324 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000325 }
326
327 target_obj->pending_read_domains |= reloc->read_domains;
328 target_obj->pending_write_domain |= reloc->write_domain;
329
330 /* If the relocation already has the right value in it, no
331 * more work needs to be done.
332 */
333 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000334 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000335
336 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000337 if (unlikely(reloc->offset > obj->base.size - 4)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000338 DRM_ERROR("Relocation beyond object bounds: "
339 "obj %p target %d offset %d size %d.\n",
340 obj, reloc->target_handle,
341 (int) reloc->offset,
342 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000343 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000344 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000345 if (unlikely(reloc->offset & 3)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000346 DRM_ERROR("Relocation not 4-byte aligned: "
347 "obj %p target %d offset %d.\n",
348 obj, reloc->target_handle,
349 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000350 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000351 }
352
Chris Wilson54cf91d2010-11-25 18:00:26 +0000353 reloc->delta += target_offset;
354 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
355 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
356 char *vaddr;
357
358 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
359 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
360 kunmap_atomic(vaddr);
361 } else {
362 struct drm_i915_private *dev_priv = dev->dev_private;
363 uint32_t __iomem *reloc_entry;
364 void __iomem *reloc_page;
365
366 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
367 if (ret)
Chris Wilson67731b82010-12-08 10:38:14 +0000368 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000369
370 /* Map the page containing the relocation we're going to perform. */
371 reloc->offset += obj->gtt_offset;
372 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
373 reloc->offset & PAGE_MASK);
374 reloc_entry = (uint32_t __iomem *)
375 (reloc_page + (reloc->offset & ~PAGE_MASK));
376 iowrite32(reloc->delta, reloc_entry);
377 io_mapping_unmap_atomic(reloc_page);
378 }
379
380 /* and update the user's relocation entry */
381 reloc->presumed_offset = target_offset;
382
Chris Wilson67731b82010-12-08 10:38:14 +0000383 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000384}
385
386static int
387i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000388 struct eb_objects *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000389{
390 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000391 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000392 int i, ret;
393
394 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
395 for (i = 0; i < entry->relocation_count; i++) {
396 struct drm_i915_gem_relocation_entry reloc;
397
398 if (__copy_from_user_inatomic(&reloc,
399 user_relocs+i,
400 sizeof(reloc)))
401 return -EFAULT;
402
Chris Wilson6fe4f142011-01-10 17:35:37 +0000403 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &reloc);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000404 if (ret)
405 return ret;
406
407 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
408 &reloc.presumed_offset,
409 sizeof(reloc.presumed_offset)))
410 return -EFAULT;
411 }
412
413 return 0;
414}
415
416static int
417i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000418 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000419 struct drm_i915_gem_relocation_entry *relocs)
420{
Chris Wilson6fe4f142011-01-10 17:35:37 +0000421 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000422 int i, ret;
423
424 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000425 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000426 if (ret)
427 return ret;
428 }
429
430 return 0;
431}
432
433static int
434i915_gem_execbuffer_relocate(struct drm_device *dev,
Chris Wilson67731b82010-12-08 10:38:14 +0000435 struct eb_objects *eb,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000436 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000437{
Chris Wilson432e58e2010-11-25 19:32:06 +0000438 struct drm_i915_gem_object *obj;
439 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000440
Chris Wilson432e58e2010-11-25 19:32:06 +0000441 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000442 ret = i915_gem_execbuffer_relocate_object(obj, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000443 if (ret)
444 return ret;
445 }
446
447 return 0;
448}
449
450static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000451i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000452 struct drm_file *file,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000453 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000454{
Chris Wilson432e58e2010-11-25 19:32:06 +0000455 struct drm_i915_gem_object *obj;
Chris Wilson432e58e2010-11-25 19:32:06 +0000456 int ret, retry;
Chris Wilson9b3826b2010-12-05 17:11:54 +0000457 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000458 struct list_head ordered_objects;
459
460 INIT_LIST_HEAD(&ordered_objects);
461 while (!list_empty(objects)) {
462 struct drm_i915_gem_exec_object2 *entry;
463 bool need_fence, need_mappable;
464
465 obj = list_first_entry(objects,
466 struct drm_i915_gem_object,
467 exec_list);
468 entry = obj->exec_entry;
469
470 need_fence =
471 has_fenced_gpu_access &&
472 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
473 obj->tiling_mode != I915_TILING_NONE;
474 need_mappable =
475 entry->relocation_count ? true : need_fence;
476
477 if (need_mappable)
478 list_move(&obj->exec_list, &ordered_objects);
479 else
480 list_move_tail(&obj->exec_list, &ordered_objects);
Chris Wilson595dad72011-01-13 11:03:48 +0000481
482 obj->base.pending_read_domains = 0;
483 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000484 }
485 list_splice(&ordered_objects, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000486
487 /* Attempt to pin all of the buffers into the GTT.
488 * This is done in 3 phases:
489 *
490 * 1a. Unbind all objects that do not match the GTT constraints for
491 * the execbuffer (fenceable, mappable, alignment etc).
492 * 1b. Increment pin count for already bound objects.
493 * 2. Bind new objects.
494 * 3. Decrement pin count.
495 *
496 * This avoid unnecessary unbinding of later objects in order to makr
497 * room for the earlier objects *unless* we need to defragment.
498 */
499 retry = 0;
500 do {
501 ret = 0;
502
503 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000504 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000505 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000506 bool need_fence, need_mappable;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000507 if (!obj->gtt_space)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000508 continue;
509
510 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000511 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000512 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
513 obj->tiling_mode != I915_TILING_NONE;
514 need_mappable =
515 entry->relocation_count ? true : need_fence;
516
517 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
518 (need_mappable && !obj->map_and_fenceable))
519 ret = i915_gem_object_unbind(obj);
520 else
521 ret = i915_gem_object_pin(obj,
522 entry->alignment,
523 need_mappable);
Chris Wilson432e58e2010-11-25 19:32:06 +0000524 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000525 goto err;
Chris Wilson432e58e2010-11-25 19:32:06 +0000526
527 entry++;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000528 }
529
530 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000531 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000532 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000533 bool need_fence;
534
535 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000536 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000537 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
538 obj->tiling_mode != I915_TILING_NONE;
539
540 if (!obj->gtt_space) {
541 bool need_mappable =
542 entry->relocation_count ? true : need_fence;
543
544 ret = i915_gem_object_pin(obj,
545 entry->alignment,
546 need_mappable);
547 if (ret)
548 break;
549 }
550
Chris Wilson9b3826b2010-12-05 17:11:54 +0000551 if (has_fenced_gpu_access) {
552 if (need_fence) {
Chris Wilsonce453d82011-02-21 14:43:56 +0000553 ret = i915_gem_object_get_fence(obj, ring);
Chris Wilson9b3826b2010-12-05 17:11:54 +0000554 if (ret)
555 break;
556 } else if (entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
557 obj->tiling_mode == I915_TILING_NONE) {
558 /* XXX pipelined! */
559 ret = i915_gem_object_put_fence(obj);
560 if (ret)
561 break;
562 }
563 obj->pending_fenced_gpu_access = need_fence;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000564 }
565
566 entry->offset = obj->gtt_offset;
567 }
568
Chris Wilson432e58e2010-11-25 19:32:06 +0000569 /* Decrement pin count for bound objects */
570 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000571 if (obj->gtt_space)
572 i915_gem_object_unpin(obj);
573 }
574
575 if (ret != -ENOSPC || retry > 1)
576 return ret;
577
578 /* First attempt, just clear anything that is purgeable.
579 * Second attempt, clear the entire GTT.
580 */
Chris Wilsond9e86c02010-11-10 16:40:20 +0000581 ret = i915_gem_evict_everything(ring->dev, retry == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000582 if (ret)
583 return ret;
584
585 retry++;
586 } while (1);
Chris Wilson432e58e2010-11-25 19:32:06 +0000587
588err:
Chris Wilson602606a2010-11-28 15:31:02 +0000589 obj = list_entry(obj->exec_list.prev,
590 struct drm_i915_gem_object,
591 exec_list);
Chris Wilson432e58e2010-11-25 19:32:06 +0000592 while (objects != &obj->exec_list) {
593 if (obj->gtt_space)
594 i915_gem_object_unpin(obj);
595
596 obj = list_entry(obj->exec_list.prev,
597 struct drm_i915_gem_object,
598 exec_list);
599 }
600
601 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000602}
603
604static int
605i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
606 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000607 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000608 struct list_head *objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000609 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000610 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000611 int count)
612{
613 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000614 struct drm_i915_gem_object *obj;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000615 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000616 int i, total, ret;
617
Chris Wilson67731b82010-12-08 10:38:14 +0000618 /* We may process another execbuffer during the unlock... */
Chris Wilson36cf1742011-01-10 12:09:12 +0000619 while (!list_empty(objects)) {
Chris Wilson67731b82010-12-08 10:38:14 +0000620 obj = list_first_entry(objects,
621 struct drm_i915_gem_object,
622 exec_list);
623 list_del_init(&obj->exec_list);
624 drm_gem_object_unreference(&obj->base);
625 }
626
Chris Wilson54cf91d2010-11-25 18:00:26 +0000627 mutex_unlock(&dev->struct_mutex);
628
629 total = 0;
630 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000631 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000632
Chris Wilsondd6864a2011-01-12 23:49:13 +0000633 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000634 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000635 if (reloc == NULL || reloc_offset == NULL) {
636 drm_free_large(reloc);
637 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000638 mutex_lock(&dev->struct_mutex);
639 return -ENOMEM;
640 }
641
642 total = 0;
643 for (i = 0; i < count; i++) {
644 struct drm_i915_gem_relocation_entry __user *user_relocs;
645
Chris Wilson432e58e2010-11-25 19:32:06 +0000646 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000647
648 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000649 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000650 ret = -EFAULT;
651 mutex_lock(&dev->struct_mutex);
652 goto err;
653 }
654
Chris Wilsondd6864a2011-01-12 23:49:13 +0000655 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000656 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000657 }
658
659 ret = i915_mutex_lock_interruptible(dev);
660 if (ret) {
661 mutex_lock(&dev->struct_mutex);
662 goto err;
663 }
664
Chris Wilson67731b82010-12-08 10:38:14 +0000665 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000666 eb_reset(eb);
667 for (i = 0; i < count; i++) {
Chris Wilson67731b82010-12-08 10:38:14 +0000668 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
669 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000670 if (&obj->base == NULL) {
Chris Wilson67731b82010-12-08 10:38:14 +0000671 DRM_ERROR("Invalid object handle %d at index %d\n",
672 exec[i].handle, i);
673 ret = -ENOENT;
674 goto err;
675 }
676
677 list_add_tail(&obj->exec_list, objects);
678 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000679 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000680 eb_add_object(eb, obj);
681 }
682
Chris Wilson6fe4f142011-01-10 17:35:37 +0000683 ret = i915_gem_execbuffer_reserve(ring, file, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000684 if (ret)
685 goto err;
686
Chris Wilson432e58e2010-11-25 19:32:06 +0000687 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondd6864a2011-01-12 23:49:13 +0000688 int offset = obj->exec_entry - exec;
Chris Wilson67731b82010-12-08 10:38:14 +0000689 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilsondd6864a2011-01-12 23:49:13 +0000690 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000691 if (ret)
692 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000693 }
694
695 /* Leave the user relocations as are, this is the painfully slow path,
696 * and we want to avoid the complication of dropping the lock whilst
697 * having buffers reserved in the aperture and so causing spurious
698 * ENOSPC for random operations.
699 */
700
701err:
702 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000703 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000704 return ret;
705}
706
Chris Wilson88241782011-01-07 17:09:48 +0000707static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000708i915_gem_execbuffer_flush(struct drm_device *dev,
709 uint32_t invalidate_domains,
710 uint32_t flush_domains,
711 uint32_t flush_rings)
712{
713 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson88241782011-01-07 17:09:48 +0000714 int i, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000715
716 if (flush_domains & I915_GEM_DOMAIN_CPU)
717 intel_gtt_chipset_flush();
718
Chris Wilson63256ec2011-01-04 18:42:07 +0000719 if (flush_domains & I915_GEM_DOMAIN_GTT)
720 wmb();
721
Chris Wilson54cf91d2010-11-25 18:00:26 +0000722 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000723 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilson88241782011-01-07 17:09:48 +0000724 if (flush_rings & (1 << i)) {
Chris Wilsondb53a302011-02-03 11:57:46 +0000725 ret = i915_gem_flush_ring(&dev_priv->ring[i],
Chris Wilson88241782011-01-07 17:09:48 +0000726 invalidate_domains,
727 flush_domains);
728 if (ret)
729 return ret;
730 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000731 }
Chris Wilson88241782011-01-07 17:09:48 +0000732
733 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000734}
735
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000736static int
737i915_gem_execbuffer_sync_rings(struct drm_i915_gem_object *obj,
738 struct intel_ring_buffer *to)
739{
740 struct intel_ring_buffer *from = obj->ring;
741 u32 seqno;
742 int ret, idx;
743
744 if (from == NULL || to == from)
745 return 0;
746
Chris Wilsone8b2c3c2011-03-01 19:22:52 +0000747 if (INTEL_INFO(obj->base.dev)->gen < 6)
Chris Wilsonce453d82011-02-21 14:43:56 +0000748 return i915_gem_object_wait_rendering(obj);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000749
750 idx = intel_ring_sync_index(from, to);
751
752 seqno = obj->last_rendering_seqno;
753 if (seqno <= from->sync_seqno[idx])
754 return 0;
755
756 if (seqno == from->outstanding_lazy_request) {
757 struct drm_i915_gem_request *request;
758
759 request = kzalloc(sizeof(*request), GFP_KERNEL);
760 if (request == NULL)
761 return -ENOMEM;
762
Chris Wilsondb53a302011-02-03 11:57:46 +0000763 ret = i915_add_request(from, NULL, request);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000764 if (ret) {
765 kfree(request);
766 return ret;
767 }
768
769 seqno = request->seqno;
770 }
771
772 from->sync_seqno[idx] = seqno;
773 return intel_ring_sync(to, from, seqno - 1);
774}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000775
776static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000777i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
778 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000779{
Chris Wilson432e58e2010-11-25 19:32:06 +0000780 struct drm_i915_gem_object *obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000781 struct change_domains cd;
Chris Wilson432e58e2010-11-25 19:32:06 +0000782 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000783
784 cd.invalidate_domains = 0;
785 cd.flush_domains = 0;
786 cd.flush_rings = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000787 list_for_each_entry(obj, objects, exec_list)
788 i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000789
790 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson88241782011-01-07 17:09:48 +0000791 ret = i915_gem_execbuffer_flush(ring->dev,
792 cd.invalidate_domains,
793 cd.flush_domains,
794 cd.flush_rings);
795 if (ret)
796 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000797 }
798
Chris Wilson432e58e2010-11-25 19:32:06 +0000799 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000800 ret = i915_gem_execbuffer_sync_rings(obj, ring);
801 if (ret)
802 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000803 }
804
805 return 0;
806}
807
Chris Wilson432e58e2010-11-25 19:32:06 +0000808static bool
809i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000810{
Chris Wilson432e58e2010-11-25 19:32:06 +0000811 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000812}
813
814static int
815validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
816 int count)
817{
818 int i;
819
820 for (i = 0; i < count; i++) {
821 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
822 int length; /* limited by fault_in_pages_readable() */
823
824 /* First check for malicious input causing overflow */
825 if (exec[i].relocation_count >
826 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
827 return -EINVAL;
828
829 length = exec[i].relocation_count *
830 sizeof(struct drm_i915_gem_relocation_entry);
831 if (!access_ok(VERIFY_READ, ptr, length))
832 return -EFAULT;
833
834 /* we may also need to update the presumed offsets */
835 if (!access_ok(VERIFY_WRITE, ptr, length))
836 return -EFAULT;
837
838 if (fault_in_pages_readable(ptr, length))
839 return -EFAULT;
840 }
841
842 return 0;
843}
844
Chris Wilson432e58e2010-11-25 19:32:06 +0000845static int
846i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring,
847 struct list_head *objects)
848{
849 struct drm_i915_gem_object *obj;
850 int flips;
851
852 /* Check for any pending flips. As we only maintain a flip queue depth
853 * of 1, we can simply insert a WAIT for the next display flip prior
854 * to executing the batch and avoid stalling the CPU.
855 */
856 flips = 0;
857 list_for_each_entry(obj, objects, exec_list) {
858 if (obj->base.write_domain)
859 flips |= atomic_read(&obj->pending_flip);
860 }
861 if (flips) {
862 int plane, flip_mask, ret;
863
864 for (plane = 0; flips >> plane; plane++) {
865 if (((flips >> plane) & 1) == 0)
866 continue;
867
868 if (plane)
869 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
870 else
871 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
872
873 ret = intel_ring_begin(ring, 2);
874 if (ret)
875 return ret;
876
877 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
878 intel_ring_emit(ring, MI_NOOP);
879 intel_ring_advance(ring);
880 }
881 }
882
883 return 0;
884}
885
886static 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 Wilson432e58e2010-11-25 19:32:06 +00001136 ret = i915_gem_execbuffer_wait_for_flips(ring, &objects);
1137 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001138 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001139
Chris Wilsondb53a302011-02-03 11:57:46 +00001140 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson076e2c02011-01-21 10:07:18 +00001141 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001142 if (seqno < ring->sync_seqno[i]) {
1143 /* The GPU can not handle its semaphore value wrapping,
1144 * so every billion or so execbuffers, we need to stall
1145 * the GPU in order to reset the counters.
1146 */
1147 ret = i915_gpu_idle(dev);
1148 if (ret)
1149 goto err;
1150
1151 BUG_ON(ring->sync_seqno[i]);
1152 }
1153 }
1154
Chris Wilsondb53a302011-02-03 11:57:46 +00001155 trace_i915_gem_ring_dispatch(ring, seqno);
1156
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001157 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1158 exec_len = args->batch_len;
1159 if (cliprects) {
1160 for (i = 0; i < args->num_cliprects; i++) {
1161 ret = i915_emit_box(dev, &cliprects[i],
1162 args->DR1, args->DR4);
1163 if (ret)
1164 goto err;
1165
1166 ret = ring->dispatch_execbuffer(ring,
1167 exec_start, exec_len);
1168 if (ret)
1169 goto err;
1170 }
1171 } else {
1172 ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
1173 if (ret)
1174 goto err;
1175 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001176
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001177 i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +00001178 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001179
1180err:
Chris Wilson67731b82010-12-08 10:38:14 +00001181 eb_destroy(eb);
Chris Wilson432e58e2010-11-25 19:32:06 +00001182 while (!list_empty(&objects)) {
1183 struct drm_i915_gem_object *obj;
1184
1185 obj = list_first_entry(&objects,
1186 struct drm_i915_gem_object,
1187 exec_list);
1188 list_del_init(&obj->exec_list);
1189 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001190 }
1191
1192 mutex_unlock(&dev->struct_mutex);
1193
1194pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001195 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001196 return ret;
1197}
1198
1199/*
1200 * Legacy execbuffer just creates an exec2 list from the original exec object
1201 * list array and passes it to the real function.
1202 */
1203int
1204i915_gem_execbuffer(struct drm_device *dev, void *data,
1205 struct drm_file *file)
1206{
1207 struct drm_i915_gem_execbuffer *args = data;
1208 struct drm_i915_gem_execbuffer2 exec2;
1209 struct drm_i915_gem_exec_object *exec_list = NULL;
1210 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1211 int ret, i;
1212
Chris Wilson54cf91d2010-11-25 18:00:26 +00001213 if (args->buffer_count < 1) {
1214 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
1215 return -EINVAL;
1216 }
1217
1218 /* Copy in the exec list from userland */
1219 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1220 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1221 if (exec_list == NULL || exec2_list == NULL) {
1222 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
1223 args->buffer_count);
1224 drm_free_large(exec_list);
1225 drm_free_large(exec2_list);
1226 return -ENOMEM;
1227 }
1228 ret = copy_from_user(exec_list,
1229 (struct drm_i915_relocation_entry __user *)
1230 (uintptr_t) args->buffers_ptr,
1231 sizeof(*exec_list) * args->buffer_count);
1232 if (ret != 0) {
1233 DRM_ERROR("copy %d exec entries failed %d\n",
1234 args->buffer_count, ret);
1235 drm_free_large(exec_list);
1236 drm_free_large(exec2_list);
1237 return -EFAULT;
1238 }
1239
1240 for (i = 0; i < args->buffer_count; i++) {
1241 exec2_list[i].handle = exec_list[i].handle;
1242 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1243 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1244 exec2_list[i].alignment = exec_list[i].alignment;
1245 exec2_list[i].offset = exec_list[i].offset;
1246 if (INTEL_INFO(dev)->gen < 4)
1247 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1248 else
1249 exec2_list[i].flags = 0;
1250 }
1251
1252 exec2.buffers_ptr = args->buffers_ptr;
1253 exec2.buffer_count = args->buffer_count;
1254 exec2.batch_start_offset = args->batch_start_offset;
1255 exec2.batch_len = args->batch_len;
1256 exec2.DR1 = args->DR1;
1257 exec2.DR4 = args->DR4;
1258 exec2.num_cliprects = args->num_cliprects;
1259 exec2.cliprects_ptr = args->cliprects_ptr;
1260 exec2.flags = I915_EXEC_RENDER;
1261
1262 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1263 if (!ret) {
1264 /* Copy the new buffer offsets back to the user's exec list. */
1265 for (i = 0; i < args->buffer_count; i++)
1266 exec_list[i].offset = exec2_list[i].offset;
1267 /* ... and back out to userspace */
1268 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1269 (uintptr_t) args->buffers_ptr,
1270 exec_list,
1271 sizeof(*exec_list) * args->buffer_count);
1272 if (ret) {
1273 ret = -EFAULT;
1274 DRM_ERROR("failed to copy %d exec entries "
1275 "back to user (%d)\n",
1276 args->buffer_count, ret);
1277 }
1278 }
1279
1280 drm_free_large(exec_list);
1281 drm_free_large(exec2_list);
1282 return ret;
1283}
1284
1285int
1286i915_gem_execbuffer2(struct drm_device *dev, void *data,
1287 struct drm_file *file)
1288{
1289 struct drm_i915_gem_execbuffer2 *args = data;
1290 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1291 int ret;
1292
Chris Wilson54cf91d2010-11-25 18:00:26 +00001293 if (args->buffer_count < 1) {
1294 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
1295 return -EINVAL;
1296 }
1297
Chris Wilson8408c282011-02-21 12:54:48 +00001298 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1299 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1300 if (exec2_list == NULL)
1301 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1302 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001303 if (exec2_list == NULL) {
1304 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
1305 args->buffer_count);
1306 return -ENOMEM;
1307 }
1308 ret = copy_from_user(exec2_list,
1309 (struct drm_i915_relocation_entry __user *)
1310 (uintptr_t) args->buffers_ptr,
1311 sizeof(*exec2_list) * args->buffer_count);
1312 if (ret != 0) {
1313 DRM_ERROR("copy %d exec entries failed %d\n",
1314 args->buffer_count, ret);
1315 drm_free_large(exec2_list);
1316 return -EFAULT;
1317 }
1318
1319 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1320 if (!ret) {
1321 /* Copy the new buffer offsets back to the user's exec list. */
1322 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1323 (uintptr_t) args->buffers_ptr,
1324 exec2_list,
1325 sizeof(*exec2_list) * args->buffer_count);
1326 if (ret) {
1327 ret = -EFAULT;
1328 DRM_ERROR("failed to copy %d exec entries "
1329 "back to user (%d)\n",
1330 args->buffer_count, ret);
1331 }
1332 }
1333
1334 drm_free_large(exec2_list);
1335 return ret;
1336}