blob: eb85860001ec16eefffb9cd1aa4fc331341bae22 [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"
Eugeni Dodonovf45b5552011-12-09 17:16:37 -080035#include <linux/dma_remapping.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000036
37struct change_domains {
38 uint32_t invalidate_domains;
39 uint32_t flush_domains;
40 uint32_t flush_rings;
Chris Wilsonc59a3332011-03-06 13:51:29 +000041 uint32_t flips;
Chris Wilson54cf91d2010-11-25 18:00:26 +000042};
43
44/*
45 * Set the next domain for the specified object. This
46 * may not actually perform the necessary flushing/invaliding though,
47 * as that may want to be batched with other set_domain operations
48 *
49 * This is (we hope) the only really tricky part of gem. The goal
50 * is fairly simple -- track which caches hold bits of the object
51 * and make sure they remain coherent. A few concrete examples may
52 * help to explain how it works. For shorthand, we use the notation
53 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
54 * a pair of read and write domain masks.
55 *
56 * Case 1: the batch buffer
57 *
58 * 1. Allocated
59 * 2. Written by CPU
60 * 3. Mapped to GTT
61 * 4. Read by GPU
62 * 5. Unmapped from GTT
63 * 6. Freed
64 *
65 * Let's take these a step at a time
66 *
67 * 1. Allocated
68 * Pages allocated from the kernel may still have
69 * cache contents, so we set them to (CPU, CPU) always.
70 * 2. Written by CPU (using pwrite)
71 * The pwrite function calls set_domain (CPU, CPU) and
72 * this function does nothing (as nothing changes)
73 * 3. Mapped by GTT
74 * This function asserts that the object is not
75 * currently in any GPU-based read or write domains
76 * 4. Read by GPU
77 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
78 * As write_domain is zero, this function adds in the
79 * current read domains (CPU+COMMAND, 0).
80 * flush_domains is set to CPU.
81 * invalidate_domains is set to COMMAND
82 * clflush is run to get data out of the CPU caches
83 * then i915_dev_set_domain calls i915_gem_flush to
84 * emit an MI_FLUSH and drm_agp_chipset_flush
85 * 5. Unmapped from GTT
86 * i915_gem_object_unbind calls set_domain (CPU, CPU)
87 * flush_domains and invalidate_domains end up both zero
88 * so no flushing/invalidating happens
89 * 6. Freed
90 * yay, done
91 *
92 * Case 2: The shared render buffer
93 *
94 * 1. Allocated
95 * 2. Mapped to GTT
96 * 3. Read/written by GPU
97 * 4. set_domain to (CPU,CPU)
98 * 5. Read/written by CPU
99 * 6. Read/written by GPU
100 *
101 * 1. Allocated
102 * Same as last example, (CPU, CPU)
103 * 2. Mapped to GTT
104 * Nothing changes (assertions find that it is not in the GPU)
105 * 3. Read/written by GPU
106 * execbuffer calls set_domain (RENDER, RENDER)
107 * flush_domains gets CPU
108 * invalidate_domains gets GPU
109 * clflush (obj)
110 * MI_FLUSH and drm_agp_chipset_flush
111 * 4. set_domain (CPU, CPU)
112 * flush_domains gets GPU
113 * invalidate_domains gets CPU
114 * wait_rendering (obj) to make sure all drawing is complete.
115 * This will include an MI_FLUSH to get the data from GPU
116 * to memory
117 * clflush (obj) to invalidate the CPU cache
118 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
119 * 5. Read/written by CPU
120 * cache lines are loaded and dirtied
121 * 6. Read written by GPU
122 * Same as last GPU access
123 *
124 * Case 3: The constant buffer
125 *
126 * 1. Allocated
127 * 2. Written by CPU
128 * 3. Read by GPU
129 * 4. Updated (written) by CPU again
130 * 5. Read by GPU
131 *
132 * 1. Allocated
133 * (CPU, CPU)
134 * 2. Written by CPU
135 * (CPU, CPU)
136 * 3. Read by GPU
137 * (CPU+RENDER, 0)
138 * flush_domains = CPU
139 * invalidate_domains = RENDER
140 * clflush (obj)
141 * MI_FLUSH
142 * drm_agp_chipset_flush
143 * 4. Updated (written) by CPU again
144 * (CPU, CPU)
145 * flush_domains = 0 (no previous write domain)
146 * invalidate_domains = 0 (no new read domains)
147 * 5. Read by GPU
148 * (CPU+RENDER, 0)
149 * flush_domains = CPU
150 * invalidate_domains = RENDER
151 * clflush (obj)
152 * MI_FLUSH
153 * drm_agp_chipset_flush
154 */
155static void
156i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
157 struct intel_ring_buffer *ring,
158 struct change_domains *cd)
159{
160 uint32_t invalidate_domains = 0, flush_domains = 0;
161
162 /*
163 * If the object isn't moving to a new write domain,
164 * let the object stay in multiple read domains
165 */
166 if (obj->base.pending_write_domain == 0)
167 obj->base.pending_read_domains |= obj->base.read_domains;
168
169 /*
170 * Flush the current write domain if
171 * the new read domains don't match. Invalidate
172 * any read domains which differ from the old
173 * write domain
174 */
175 if (obj->base.write_domain &&
176 (((obj->base.write_domain != obj->base.pending_read_domains ||
177 obj->ring != ring)) ||
178 (obj->fenced_gpu_access && !obj->pending_fenced_gpu_access))) {
179 flush_domains |= obj->base.write_domain;
180 invalidate_domains |=
181 obj->base.pending_read_domains & ~obj->base.write_domain;
182 }
183 /*
184 * Invalidate any read caches which may have
185 * stale data. That is, any new read domains.
186 */
187 invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
188 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
189 i915_gem_clflush_object(obj);
190
Chris Wilsonc59a3332011-03-06 13:51:29 +0000191 if (obj->base.pending_write_domain)
192 cd->flips |= atomic_read(&obj->pending_flip);
193
Chris Wilson54cf91d2010-11-25 18:00:26 +0000194 /* The actual obj->write_domain will be updated with
195 * pending_write_domain after we emit the accumulated flush for all
196 * of our domain changes in execbuffers (which clears objects'
197 * write_domains). So if we have a current write domain that we
198 * aren't changing, set pending_write_domain to that.
199 */
200 if (flush_domains == 0 && obj->base.pending_write_domain == 0)
201 obj->base.pending_write_domain = obj->base.write_domain;
202
203 cd->invalidate_domains |= invalidate_domains;
204 cd->flush_domains |= flush_domains;
205 if (flush_domains & I915_GEM_GPU_DOMAINS)
Daniel Vetter96154f22011-12-14 13:57:00 +0100206 cd->flush_rings |= intel_ring_flag(obj->ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000207 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
Daniel Vetter96154f22011-12-14 13:57:00 +0100208 cd->flush_rings |= intel_ring_flag(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000209}
210
Chris Wilson67731b82010-12-08 10:38:14 +0000211struct eb_objects {
212 int and;
213 struct hlist_head buckets[0];
214};
215
216static struct eb_objects *
217eb_create(int size)
218{
219 struct eb_objects *eb;
220 int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
221 while (count > size)
222 count >>= 1;
223 eb = kzalloc(count*sizeof(struct hlist_head) +
224 sizeof(struct eb_objects),
225 GFP_KERNEL);
226 if (eb == NULL)
227 return eb;
228
229 eb->and = count - 1;
230 return eb;
231}
232
233static void
234eb_reset(struct eb_objects *eb)
235{
236 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
237}
238
239static void
240eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj)
241{
242 hlist_add_head(&obj->exec_node,
243 &eb->buckets[obj->exec_handle & eb->and]);
244}
245
246static struct drm_i915_gem_object *
247eb_get_object(struct eb_objects *eb, unsigned long handle)
248{
249 struct hlist_head *head;
250 struct hlist_node *node;
251 struct drm_i915_gem_object *obj;
252
253 head = &eb->buckets[handle & eb->and];
254 hlist_for_each(node, head) {
255 obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
256 if (obj->exec_handle == handle)
257 return obj;
258 }
259
260 return NULL;
261}
262
263static void
264eb_destroy(struct eb_objects *eb)
265{
266 kfree(eb);
267}
268
Chris Wilsondabdfe02012-03-26 10:10:27 +0200269static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
270{
271 return (obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
272 obj->cache_level != I915_CACHE_NONE);
273}
274
Chris Wilson54cf91d2010-11-25 18:00:26 +0000275static int
276i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000277 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000278 struct drm_i915_gem_relocation_entry *reloc)
279{
280 struct drm_device *dev = obj->base.dev;
281 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100282 struct drm_i915_gem_object *target_i915_obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000283 uint32_t target_offset;
284 int ret = -EINVAL;
285
Chris Wilson67731b82010-12-08 10:38:14 +0000286 /* we've already hold a reference to all valid objects */
287 target_obj = &eb_get_object(eb, reloc->target_handle)->base;
288 if (unlikely(target_obj == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000289 return -ENOENT;
290
Daniel Vetter149c8402012-02-15 23:50:23 +0100291 target_i915_obj = to_intel_bo(target_obj);
292 target_offset = target_i915_obj->gtt_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000293
Chris Wilson54cf91d2010-11-25 18:00:26 +0000294 /* The target buffer should have appeared before us in the
295 * exec_object list, so it should have a GTT space bound by now.
296 */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000297 if (unlikely(target_offset == 0)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100298 DRM_DEBUG("No GTT space found for object %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000299 reloc->target_handle);
Chris Wilson67731b82010-12-08 10:38:14 +0000300 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000301 }
302
303 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000304 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100305 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000306 "obj %p target %d offset %d "
307 "read %08x write %08x",
308 obj, reloc->target_handle,
309 (int) reloc->offset,
310 reloc->read_domains,
311 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000312 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000313 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100314 if (unlikely((reloc->write_domain | reloc->read_domains)
315 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100316 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000317 "obj %p target %d offset %d "
318 "read %08x write %08x",
319 obj, reloc->target_handle,
320 (int) reloc->offset,
321 reloc->read_domains,
322 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000323 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000324 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000325 if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
326 reloc->write_domain != target_obj->pending_write_domain)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100327 DRM_DEBUG("Write domain conflict: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000328 "obj %p target %d offset %d "
329 "new %08x old %08x\n",
330 obj, reloc->target_handle,
331 (int) reloc->offset,
332 reloc->write_domain,
333 target_obj->pending_write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000334 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000335 }
336
337 target_obj->pending_read_domains |= reloc->read_domains;
338 target_obj->pending_write_domain |= reloc->write_domain;
339
340 /* If the relocation already has the right value in it, no
341 * more work needs to be done.
342 */
343 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000344 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000345
346 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000347 if (unlikely(reloc->offset > obj->base.size - 4)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100348 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000349 "obj %p target %d offset %d size %d.\n",
350 obj, reloc->target_handle,
351 (int) reloc->offset,
352 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000353 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000354 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000355 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100356 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000357 "obj %p target %d offset %d.\n",
358 obj, reloc->target_handle,
359 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000360 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000361 }
362
Chris Wilsondabdfe02012-03-26 10:10:27 +0200363 /* We can't wait for rendering with pagefaults disabled */
364 if (obj->active && in_atomic())
365 return -EFAULT;
366
Chris Wilson54cf91d2010-11-25 18:00:26 +0000367 reloc->delta += target_offset;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200368 if (use_cpu_reloc(obj)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000369 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
370 char *vaddr;
371
Chris Wilsondabdfe02012-03-26 10:10:27 +0200372 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
373 if (ret)
374 return ret;
375
Chris Wilson54cf91d2010-11-25 18:00:26 +0000376 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
377 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
378 kunmap_atomic(vaddr);
379 } else {
380 struct drm_i915_private *dev_priv = dev->dev_private;
381 uint32_t __iomem *reloc_entry;
382 void __iomem *reloc_page;
383
384 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
385 if (ret)
Chris Wilson67731b82010-12-08 10:38:14 +0000386 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000387
388 /* Map the page containing the relocation we're going to perform. */
389 reloc->offset += obj->gtt_offset;
390 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
391 reloc->offset & PAGE_MASK);
392 reloc_entry = (uint32_t __iomem *)
393 (reloc_page + (reloc->offset & ~PAGE_MASK));
394 iowrite32(reloc->delta, reloc_entry);
395 io_mapping_unmap_atomic(reloc_page);
396 }
397
Daniel Vetter149c8402012-02-15 23:50:23 +0100398 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
399 * pipe_control writes because the gpu doesn't properly redirect them
400 * through the ppgtt for non_secure batchbuffers. */
401 if (unlikely(IS_GEN6(dev) &&
402 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
403 !target_i915_obj->has_global_gtt_mapping)) {
404 i915_gem_gtt_bind_object(target_i915_obj,
405 target_i915_obj->cache_level);
406 }
407
Chris Wilson54cf91d2010-11-25 18:00:26 +0000408 /* and update the user's relocation entry */
409 reloc->presumed_offset = target_offset;
410
Chris Wilson67731b82010-12-08 10:38:14 +0000411 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000412}
413
414static int
415i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000416 struct eb_objects *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000417{
Chris Wilson1d83f442012-03-24 20:12:53 +0000418#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
419 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000420 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000421 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000422 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000423
424 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000425
Chris Wilson1d83f442012-03-24 20:12:53 +0000426 remain = entry->relocation_count;
427 while (remain) {
428 struct drm_i915_gem_relocation_entry *r = stack_reloc;
429 int count = remain;
430 if (count > ARRAY_SIZE(stack_reloc))
431 count = ARRAY_SIZE(stack_reloc);
432 remain -= count;
433
434 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000435 return -EFAULT;
436
Chris Wilson1d83f442012-03-24 20:12:53 +0000437 do {
438 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000439
Chris Wilson1d83f442012-03-24 20:12:53 +0000440 ret = i915_gem_execbuffer_relocate_entry(obj, eb, r);
441 if (ret)
442 return ret;
443
444 if (r->presumed_offset != offset &&
445 __copy_to_user_inatomic(&user_relocs->presumed_offset,
446 &r->presumed_offset,
447 sizeof(r->presumed_offset))) {
448 return -EFAULT;
449 }
450
451 user_relocs++;
452 r++;
453 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000454 }
455
456 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000457#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000458}
459
460static int
461i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000462 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000463 struct drm_i915_gem_relocation_entry *relocs)
464{
Chris Wilson6fe4f142011-01-10 17:35:37 +0000465 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000466 int i, ret;
467
468 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000469 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000470 if (ret)
471 return ret;
472 }
473
474 return 0;
475}
476
477static int
478i915_gem_execbuffer_relocate(struct drm_device *dev,
Chris Wilson67731b82010-12-08 10:38:14 +0000479 struct eb_objects *eb,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000480 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000481{
Chris Wilson432e58e2010-11-25 19:32:06 +0000482 struct drm_i915_gem_object *obj;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000483 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000484
Chris Wilsond4aeee72011-03-14 15:11:24 +0000485 /* This is the fast path and we cannot handle a pagefault whilst
486 * holding the struct mutex lest the user pass in the relocations
487 * contained within a mmaped bo. For in such a case we, the page
488 * fault handler would call i915_gem_fault() and we would try to
489 * acquire the struct mutex again. Obviously this is bad and so
490 * lockdep complains vehemently.
491 */
492 pagefault_disable();
Chris Wilson432e58e2010-11-25 19:32:06 +0000493 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000494 ret = i915_gem_execbuffer_relocate_object(obj, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000495 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000496 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000497 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000498 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000499
Chris Wilsond4aeee72011-03-14 15:11:24 +0000500 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000501}
502
Chris Wilson1690e1e2011-12-14 13:57:08 +0100503#define __EXEC_OBJECT_HAS_FENCE (1<<31)
504
505static int
Chris Wilsondabdfe02012-03-26 10:10:27 +0200506need_reloc_mappable(struct drm_i915_gem_object *obj)
507{
508 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
509 return entry->relocation_count && !use_cpu_reloc(obj);
510}
511
512static int
Chris Wilson1690e1e2011-12-14 13:57:08 +0100513pin_and_fence_object(struct drm_i915_gem_object *obj,
514 struct intel_ring_buffer *ring)
515{
516 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
517 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
518 bool need_fence, need_mappable;
519 int ret;
520
521 need_fence =
522 has_fenced_gpu_access &&
523 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
524 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200525 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100526
527 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable);
528 if (ret)
529 return ret;
530
531 if (has_fenced_gpu_access) {
532 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
533 if (obj->tiling_mode) {
534 ret = i915_gem_object_get_fence(obj, ring);
535 if (ret)
536 goto err_unpin;
537
538 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
539 i915_gem_object_pin_fence(obj);
540 } else {
541 ret = i915_gem_object_put_fence(obj);
542 if (ret)
543 goto err_unpin;
544 }
545 }
546 obj->pending_fenced_gpu_access = need_fence;
547 }
548
549 entry->offset = obj->gtt_offset;
550 return 0;
551
552err_unpin:
553 i915_gem_object_unpin(obj);
554 return ret;
555}
556
Chris Wilson54cf91d2010-11-25 18:00:26 +0000557static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000558i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000559 struct drm_file *file,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000560 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000561{
Daniel Vetter7bddb012012-02-09 17:15:47 +0100562 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000563 struct drm_i915_gem_object *obj;
Chris Wilson432e58e2010-11-25 19:32:06 +0000564 int ret, retry;
Chris Wilson9b3826b2010-12-05 17:11:54 +0000565 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000566 struct list_head ordered_objects;
567
568 INIT_LIST_HEAD(&ordered_objects);
569 while (!list_empty(objects)) {
570 struct drm_i915_gem_exec_object2 *entry;
571 bool need_fence, need_mappable;
572
573 obj = list_first_entry(objects,
574 struct drm_i915_gem_object,
575 exec_list);
576 entry = obj->exec_entry;
577
578 need_fence =
579 has_fenced_gpu_access &&
580 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
581 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200582 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000583
584 if (need_mappable)
585 list_move(&obj->exec_list, &ordered_objects);
586 else
587 list_move_tail(&obj->exec_list, &ordered_objects);
Chris Wilson595dad72011-01-13 11:03:48 +0000588
589 obj->base.pending_read_domains = 0;
590 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000591 }
592 list_splice(&ordered_objects, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000593
594 /* Attempt to pin all of the buffers into the GTT.
595 * This is done in 3 phases:
596 *
597 * 1a. Unbind all objects that do not match the GTT constraints for
598 * the execbuffer (fenceable, mappable, alignment etc).
599 * 1b. Increment pin count for already bound objects.
600 * 2. Bind new objects.
601 * 3. Decrement pin count.
602 *
603 * This avoid unnecessary unbinding of later objects in order to makr
604 * room for the earlier objects *unless* we need to defragment.
605 */
606 retry = 0;
607 do {
608 ret = 0;
609
610 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000611 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000612 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000613 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100614
Chris Wilson6fe4f142011-01-10 17:35:37 +0000615 if (!obj->gtt_space)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000616 continue;
617
618 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000619 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000620 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
621 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200622 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000623
624 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
625 (need_mappable && !obj->map_and_fenceable))
626 ret = i915_gem_object_unbind(obj);
627 else
Chris Wilson1690e1e2011-12-14 13:57:08 +0100628 ret = pin_and_fence_object(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000629 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000630 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000631 }
632
633 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000634 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100635 if (obj->gtt_space)
636 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000637
Chris Wilson1690e1e2011-12-14 13:57:08 +0100638 ret = pin_and_fence_object(obj, ring);
639 if (ret) {
640 int ret_ignore;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000641
Chris Wilson1690e1e2011-12-14 13:57:08 +0100642 /* This can potentially raise a harmless
643 * -EINVAL if we failed to bind in the above
644 * call. It cannot raise -EINTR since we know
645 * that the bo is freshly bound and so will
646 * not need to be flushed or waited upon.
647 */
648 ret_ignore = i915_gem_object_unbind(obj);
649 (void)ret_ignore;
650 WARN_ON(obj->gtt_space);
651 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000652 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000653 }
654
Chris Wilson432e58e2010-11-25 19:32:06 +0000655 /* Decrement pin count for bound objects */
656 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100657 struct drm_i915_gem_exec_object2 *entry;
658
659 if (!obj->gtt_space)
660 continue;
661
662 entry = obj->exec_entry;
663 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
664 i915_gem_object_unpin_fence(obj);
665 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
666 }
667
668 i915_gem_object_unpin(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100669
670 /* ... and ensure ppgtt mapping exist if needed. */
671 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
672 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
673 obj, obj->cache_level);
674
675 obj->has_aliasing_ppgtt_mapping = 1;
676 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000677 }
678
679 if (ret != -ENOSPC || retry > 1)
680 return ret;
681
682 /* First attempt, just clear anything that is purgeable.
683 * Second attempt, clear the entire GTT.
684 */
Chris Wilsond9e86c02010-11-10 16:40:20 +0000685 ret = i915_gem_evict_everything(ring->dev, retry == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000686 if (ret)
687 return ret;
688
689 retry++;
690 } while (1);
Chris Wilson432e58e2010-11-25 19:32:06 +0000691
692err:
Chris Wilson1690e1e2011-12-14 13:57:08 +0100693 list_for_each_entry_continue_reverse(obj, objects, exec_list) {
694 struct drm_i915_gem_exec_object2 *entry;
Chris Wilson432e58e2010-11-25 19:32:06 +0000695
Chris Wilson1690e1e2011-12-14 13:57:08 +0100696 if (!obj->gtt_space)
697 continue;
698
699 entry = obj->exec_entry;
700 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
701 i915_gem_object_unpin_fence(obj);
702 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
703 }
704
705 i915_gem_object_unpin(obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000706 }
707
708 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000709}
710
711static int
712i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
713 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000714 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000715 struct list_head *objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000716 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000717 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000718 int count)
719{
720 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000721 struct drm_i915_gem_object *obj;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000722 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000723 int i, total, ret;
724
Chris Wilson67731b82010-12-08 10:38:14 +0000725 /* We may process another execbuffer during the unlock... */
Chris Wilson36cf1742011-01-10 12:09:12 +0000726 while (!list_empty(objects)) {
Chris Wilson67731b82010-12-08 10:38:14 +0000727 obj = list_first_entry(objects,
728 struct drm_i915_gem_object,
729 exec_list);
730 list_del_init(&obj->exec_list);
731 drm_gem_object_unreference(&obj->base);
732 }
733
Chris Wilson54cf91d2010-11-25 18:00:26 +0000734 mutex_unlock(&dev->struct_mutex);
735
736 total = 0;
737 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000738 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000739
Chris Wilsondd6864a2011-01-12 23:49:13 +0000740 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000741 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000742 if (reloc == NULL || reloc_offset == NULL) {
743 drm_free_large(reloc);
744 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000745 mutex_lock(&dev->struct_mutex);
746 return -ENOMEM;
747 }
748
749 total = 0;
750 for (i = 0; i < count; i++) {
751 struct drm_i915_gem_relocation_entry __user *user_relocs;
752
Chris Wilson432e58e2010-11-25 19:32:06 +0000753 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000754
755 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000756 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000757 ret = -EFAULT;
758 mutex_lock(&dev->struct_mutex);
759 goto err;
760 }
761
Chris Wilsondd6864a2011-01-12 23:49:13 +0000762 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000763 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000764 }
765
766 ret = i915_mutex_lock_interruptible(dev);
767 if (ret) {
768 mutex_lock(&dev->struct_mutex);
769 goto err;
770 }
771
Chris Wilson67731b82010-12-08 10:38:14 +0000772 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000773 eb_reset(eb);
774 for (i = 0; i < count; i++) {
Chris Wilson67731b82010-12-08 10:38:14 +0000775 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
776 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000777 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100778 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson67731b82010-12-08 10:38:14 +0000779 exec[i].handle, i);
780 ret = -ENOENT;
781 goto err;
782 }
783
784 list_add_tail(&obj->exec_list, objects);
785 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000786 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000787 eb_add_object(eb, obj);
788 }
789
Chris Wilson6fe4f142011-01-10 17:35:37 +0000790 ret = i915_gem_execbuffer_reserve(ring, file, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000791 if (ret)
792 goto err;
793
Chris Wilson432e58e2010-11-25 19:32:06 +0000794 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondd6864a2011-01-12 23:49:13 +0000795 int offset = obj->exec_entry - exec;
Chris Wilson67731b82010-12-08 10:38:14 +0000796 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilsondd6864a2011-01-12 23:49:13 +0000797 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000798 if (ret)
799 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000800 }
801
802 /* Leave the user relocations as are, this is the painfully slow path,
803 * and we want to avoid the complication of dropping the lock whilst
804 * having buffers reserved in the aperture and so causing spurious
805 * ENOSPC for random operations.
806 */
807
808err:
809 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000810 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000811 return ret;
812}
813
Chris Wilson88241782011-01-07 17:09:48 +0000814static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000815i915_gem_execbuffer_flush(struct drm_device *dev,
816 uint32_t invalidate_domains,
817 uint32_t flush_domains,
818 uint32_t flush_rings)
819{
820 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson88241782011-01-07 17:09:48 +0000821 int i, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000822
823 if (flush_domains & I915_GEM_DOMAIN_CPU)
824 intel_gtt_chipset_flush();
825
Chris Wilson63256ec2011-01-04 18:42:07 +0000826 if (flush_domains & I915_GEM_DOMAIN_GTT)
827 wmb();
828
Chris Wilson54cf91d2010-11-25 18:00:26 +0000829 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000830 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilson88241782011-01-07 17:09:48 +0000831 if (flush_rings & (1 << i)) {
Chris Wilsondb53a302011-02-03 11:57:46 +0000832 ret = i915_gem_flush_ring(&dev_priv->ring[i],
Chris Wilson88241782011-01-07 17:09:48 +0000833 invalidate_domains,
834 flush_domains);
835 if (ret)
836 return ret;
837 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000838 }
Chris Wilson88241782011-01-07 17:09:48 +0000839
840 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000841}
842
Eugeni Dodonovf45b5552011-12-09 17:16:37 -0800843static bool
844intel_enable_semaphores(struct drm_device *dev)
845{
846 if (INTEL_INFO(dev)->gen < 6)
847 return 0;
848
849 if (i915_semaphores >= 0)
850 return i915_semaphores;
851
Keith Packardebbd8572011-12-26 17:02:10 -0800852 /* Disable semaphores on SNB */
Eugeni Dodonovf45b5552011-12-09 17:16:37 -0800853 if (INTEL_INFO(dev)->gen == 6)
Keith Packardebbd8572011-12-26 17:02:10 -0800854 return 0;
Eugeni Dodonovf45b5552011-12-09 17:16:37 -0800855
856 return 1;
857}
858
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000859static int
860i915_gem_execbuffer_sync_rings(struct drm_i915_gem_object *obj,
861 struct intel_ring_buffer *to)
862{
863 struct intel_ring_buffer *from = obj->ring;
864 u32 seqno;
865 int ret, idx;
866
867 if (from == NULL || to == from)
868 return 0;
869
Chris Wilsona1656b92011-03-04 18:48:03 +0000870 /* XXX gpu semaphores are implicated in various hard hangs on SNB */
Eugeni Dodonovf45b5552011-12-09 17:16:37 -0800871 if (!intel_enable_semaphores(obj->base.dev))
Chris Wilsonce453d82011-02-21 14:43:56 +0000872 return i915_gem_object_wait_rendering(obj);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000873
874 idx = intel_ring_sync_index(from, to);
875
876 seqno = obj->last_rendering_seqno;
877 if (seqno <= from->sync_seqno[idx])
878 return 0;
879
880 if (seqno == from->outstanding_lazy_request) {
881 struct drm_i915_gem_request *request;
882
883 request = kzalloc(sizeof(*request), GFP_KERNEL);
884 if (request == NULL)
885 return -ENOMEM;
886
Chris Wilsondb53a302011-02-03 11:57:46 +0000887 ret = i915_add_request(from, NULL, request);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000888 if (ret) {
889 kfree(request);
890 return ret;
891 }
892
893 seqno = request->seqno;
894 }
895
896 from->sync_seqno[idx] = seqno;
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700897
898 return to->sync_to(to, from, seqno - 1);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000899}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000900
901static int
Chris Wilsonc59a3332011-03-06 13:51:29 +0000902i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
903{
904 u32 plane, flip_mask;
905 int ret;
906
907 /* Check for any pending flips. As we only maintain a flip queue depth
908 * of 1, we can simply insert a WAIT for the next display flip prior
909 * to executing the batch and avoid stalling the CPU.
910 */
911
912 for (plane = 0; flips >> plane; plane++) {
913 if (((flips >> plane) & 1) == 0)
914 continue;
915
916 if (plane)
917 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
918 else
919 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
920
921 ret = intel_ring_begin(ring, 2);
922 if (ret)
923 return ret;
924
925 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
926 intel_ring_emit(ring, MI_NOOP);
927 intel_ring_advance(ring);
928 }
929
930 return 0;
931}
932
933
934static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000935i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
936 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000937{
Chris Wilson432e58e2010-11-25 19:32:06 +0000938 struct drm_i915_gem_object *obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000939 struct change_domains cd;
Chris Wilson432e58e2010-11-25 19:32:06 +0000940 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000941
Chris Wilsonc59a3332011-03-06 13:51:29 +0000942 memset(&cd, 0, sizeof(cd));
Chris Wilson432e58e2010-11-25 19:32:06 +0000943 list_for_each_entry(obj, objects, exec_list)
944 i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000945
946 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson88241782011-01-07 17:09:48 +0000947 ret = i915_gem_execbuffer_flush(ring->dev,
948 cd.invalidate_domains,
949 cd.flush_domains,
950 cd.flush_rings);
951 if (ret)
952 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000953 }
954
Chris Wilsonc59a3332011-03-06 13:51:29 +0000955 if (cd.flips) {
956 ret = i915_gem_execbuffer_wait_for_flips(ring, cd.flips);
957 if (ret)
958 return ret;
959 }
960
Chris Wilson432e58e2010-11-25 19:32:06 +0000961 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000962 ret = i915_gem_execbuffer_sync_rings(obj, ring);
963 if (ret)
964 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000965 }
966
967 return 0;
968}
969
Chris Wilson432e58e2010-11-25 19:32:06 +0000970static bool
971i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000972{
Chris Wilson432e58e2010-11-25 19:32:06 +0000973 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000974}
975
976static int
977validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
978 int count)
979{
980 int i;
981
982 for (i = 0; i < count; i++) {
983 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
984 int length; /* limited by fault_in_pages_readable() */
985
986 /* First check for malicious input causing overflow */
987 if (exec[i].relocation_count >
988 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
989 return -EINVAL;
990
991 length = exec[i].relocation_count *
992 sizeof(struct drm_i915_gem_relocation_entry);
993 if (!access_ok(VERIFY_READ, ptr, length))
994 return -EFAULT;
995
996 /* we may also need to update the presumed offsets */
997 if (!access_ok(VERIFY_WRITE, ptr, length))
998 return -EFAULT;
999
1000 if (fault_in_pages_readable(ptr, length))
1001 return -EFAULT;
1002 }
1003
1004 return 0;
1005}
1006
Chris Wilson432e58e2010-11-25 19:32:06 +00001007static void
1008i915_gem_execbuffer_move_to_active(struct list_head *objects,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001009 struct intel_ring_buffer *ring,
1010 u32 seqno)
Chris Wilson432e58e2010-11-25 19:32:06 +00001011{
1012 struct drm_i915_gem_object *obj;
1013
1014 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001015 u32 old_read = obj->base.read_domains;
1016 u32 old_write = obj->base.write_domain;
1017
1018
Chris Wilson432e58e2010-11-25 19:32:06 +00001019 obj->base.read_domains = obj->base.pending_read_domains;
1020 obj->base.write_domain = obj->base.pending_write_domain;
1021 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
1022
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001023 i915_gem_object_move_to_active(obj, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +00001024 if (obj->base.write_domain) {
1025 obj->dirty = 1;
Chris Wilson87ca9c82010-12-02 09:42:56 +00001026 obj->pending_gpu_write = true;
Chris Wilson432e58e2010-11-25 19:32:06 +00001027 list_move_tail(&obj->gpu_write_list,
1028 &ring->gpu_write_list);
1029 intel_mark_busy(ring->dev, obj);
1030 }
1031
Chris Wilsondb53a302011-02-03 11:57:46 +00001032 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +00001033 }
1034}
1035
Chris Wilson54cf91d2010-11-25 18:00:26 +00001036static void
1037i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +00001038 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001039 struct intel_ring_buffer *ring)
1040{
Chris Wilson432e58e2010-11-25 19:32:06 +00001041 struct drm_i915_gem_request *request;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001042 u32 invalidate;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001043
Chris Wilson432e58e2010-11-25 19:32:06 +00001044 /*
1045 * Ensure that the commands in the batch buffer are
1046 * finished before the interrupt fires.
1047 *
1048 * The sampler always gets flushed on i965 (sigh).
1049 */
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001050 invalidate = I915_GEM_DOMAIN_COMMAND;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001051 if (INTEL_INFO(dev)->gen >= 4)
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001052 invalidate |= I915_GEM_DOMAIN_SAMPLER;
1053 if (ring->flush(ring, invalidate, 0)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001054 i915_gem_next_request_seqno(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001055 return;
1056 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001057
Chris Wilson432e58e2010-11-25 19:32:06 +00001058 /* Add a breadcrumb for the completion of the batch buffer */
1059 request = kzalloc(sizeof(*request), GFP_KERNEL);
Chris Wilsondb53a302011-02-03 11:57:46 +00001060 if (request == NULL || i915_add_request(ring, file, request)) {
1061 i915_gem_next_request_seqno(ring);
Chris Wilson432e58e2010-11-25 19:32:06 +00001062 kfree(request);
1063 }
1064}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001065
1066static int
Eric Anholtae662d32012-01-03 09:23:29 -08001067i915_reset_gen7_sol_offsets(struct drm_device *dev,
1068 struct intel_ring_buffer *ring)
1069{
1070 drm_i915_private_t *dev_priv = dev->dev_private;
1071 int ret, i;
1072
1073 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
1074 return 0;
1075
1076 ret = intel_ring_begin(ring, 4 * 3);
1077 if (ret)
1078 return ret;
1079
1080 for (i = 0; i < 4; i++) {
1081 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1082 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
1083 intel_ring_emit(ring, 0);
1084 }
1085
1086 intel_ring_advance(ring);
1087
1088 return 0;
1089}
1090
1091static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001092i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1093 struct drm_file *file,
1094 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +00001095 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001096{
1097 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +00001098 struct list_head objects;
Chris Wilson67731b82010-12-08 10:38:14 +00001099 struct eb_objects *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001100 struct drm_i915_gem_object *batch_obj;
1101 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001102 struct intel_ring_buffer *ring;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001103 u32 exec_start, exec_len;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001104 u32 seqno;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001105 u32 mask;
Chris Wilson72bfa192010-12-19 11:42:05 +00001106 int ret, mode, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001107
Chris Wilson432e58e2010-11-25 19:32:06 +00001108 if (!i915_gem_check_execbuffer(args)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001109 DRM_DEBUG("execbuf with invalid offset/length\n");
Chris Wilson432e58e2010-11-25 19:32:06 +00001110 return -EINVAL;
1111 }
1112
1113 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001114 if (ret)
1115 return ret;
1116
Chris Wilson54cf91d2010-11-25 18:00:26 +00001117 switch (args->flags & I915_EXEC_RING_MASK) {
1118 case I915_EXEC_DEFAULT:
1119 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001120 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +00001121 break;
1122 case I915_EXEC_BSD:
1123 if (!HAS_BSD(dev)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001124 DRM_DEBUG("execbuf with invalid ring (BSD)\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001125 return -EINVAL;
1126 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001127 ring = &dev_priv->ring[VCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +00001128 break;
1129 case I915_EXEC_BLT:
1130 if (!HAS_BLT(dev)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001131 DRM_DEBUG("execbuf with invalid ring (BLT)\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001132 return -EINVAL;
1133 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001134 ring = &dev_priv->ring[BCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +00001135 break;
1136 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001137 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001138 (int)(args->flags & I915_EXEC_RING_MASK));
1139 return -EINVAL;
1140 }
1141
Chris Wilson72bfa192010-12-19 11:42:05 +00001142 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001143 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +00001144 switch (mode) {
1145 case I915_EXEC_CONSTANTS_REL_GENERAL:
1146 case I915_EXEC_CONSTANTS_ABSOLUTE:
1147 case I915_EXEC_CONSTANTS_REL_SURFACE:
1148 if (ring == &dev_priv->ring[RCS] &&
1149 mode != dev_priv->relative_constants_mode) {
1150 if (INTEL_INFO(dev)->gen < 4)
1151 return -EINVAL;
1152
1153 if (INTEL_INFO(dev)->gen > 5 &&
1154 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1155 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001156
1157 /* The HW changed the meaning on this bit on gen6 */
1158 if (INTEL_INFO(dev)->gen >= 6)
1159 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +00001160 }
1161 break;
1162 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001163 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +00001164 return -EINVAL;
1165 }
1166
Chris Wilson54cf91d2010-11-25 18:00:26 +00001167 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001168 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001169 return -EINVAL;
1170 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001171
1172 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001173 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +01001174 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001175 return -EINVAL;
1176 }
1177
Chris Wilson432e58e2010-11-25 19:32:06 +00001178 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001179 GFP_KERNEL);
1180 if (cliprects == NULL) {
1181 ret = -ENOMEM;
1182 goto pre_mutex_err;
1183 }
1184
Chris Wilson432e58e2010-11-25 19:32:06 +00001185 if (copy_from_user(cliprects,
1186 (struct drm_clip_rect __user *)(uintptr_t)
1187 args->cliprects_ptr,
1188 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001189 ret = -EFAULT;
1190 goto pre_mutex_err;
1191 }
1192 }
1193
Chris Wilson54cf91d2010-11-25 18:00:26 +00001194 ret = i915_mutex_lock_interruptible(dev);
1195 if (ret)
1196 goto pre_mutex_err;
1197
1198 if (dev_priv->mm.suspended) {
1199 mutex_unlock(&dev->struct_mutex);
1200 ret = -EBUSY;
1201 goto pre_mutex_err;
1202 }
1203
Chris Wilson67731b82010-12-08 10:38:14 +00001204 eb = eb_create(args->buffer_count);
1205 if (eb == NULL) {
1206 mutex_unlock(&dev->struct_mutex);
1207 ret = -ENOMEM;
1208 goto pre_mutex_err;
1209 }
1210
Chris Wilson54cf91d2010-11-25 18:00:26 +00001211 /* Look up object handles */
Chris Wilson432e58e2010-11-25 19:32:06 +00001212 INIT_LIST_HEAD(&objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001213 for (i = 0; i < args->buffer_count; i++) {
1214 struct drm_i915_gem_object *obj;
1215
Chris Wilson432e58e2010-11-25 19:32:06 +00001216 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
1217 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001218 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001219 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson432e58e2010-11-25 19:32:06 +00001220 exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001221 /* prevent error path from reading uninitialized data */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001222 ret = -ENOENT;
1223 goto err;
1224 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001225
Chris Wilson432e58e2010-11-25 19:32:06 +00001226 if (!list_empty(&obj->exec_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001227 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
Chris Wilson432e58e2010-11-25 19:32:06 +00001228 obj, exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001229 ret = -EINVAL;
1230 goto err;
1231 }
Chris Wilson432e58e2010-11-25 19:32:06 +00001232
1233 list_add_tail(&obj->exec_list, &objects);
Chris Wilson67731b82010-12-08 10:38:14 +00001234 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +00001235 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +00001236 eb_add_object(eb, obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001237 }
1238
Chris Wilson6fe4f142011-01-10 17:35:37 +00001239 /* take note of the batch buffer before we might reorder the lists */
1240 batch_obj = list_entry(objects.prev,
1241 struct drm_i915_gem_object,
1242 exec_list);
1243
Chris Wilson54cf91d2010-11-25 18:00:26 +00001244 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilson6fe4f142011-01-10 17:35:37 +00001245 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001246 if (ret)
1247 goto err;
1248
1249 /* The objects are in their final locations, apply the relocations. */
Chris Wilson6fe4f142011-01-10 17:35:37 +00001250 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001251 if (ret) {
1252 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00001253 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilson67731b82010-12-08 10:38:14 +00001254 &objects, eb,
1255 exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001256 args->buffer_count);
1257 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1258 }
1259 if (ret)
1260 goto err;
1261 }
1262
1263 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001264 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001265 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001266 ret = -EINVAL;
1267 goto err;
1268 }
1269 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1270
Chris Wilson432e58e2010-11-25 19:32:06 +00001271 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001272 if (ret)
1273 goto err;
1274
Chris Wilsondb53a302011-02-03 11:57:46 +00001275 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson076e2c02011-01-21 10:07:18 +00001276 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001277 if (seqno < ring->sync_seqno[i]) {
1278 /* The GPU can not handle its semaphore value wrapping,
1279 * so every billion or so execbuffers, we need to stall
1280 * the GPU in order to reset the counters.
1281 */
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001282 ret = i915_gpu_idle(dev, true);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001283 if (ret)
1284 goto err;
1285
1286 BUG_ON(ring->sync_seqno[i]);
1287 }
1288 }
1289
Ben Widawskye2971bd2011-12-12 19:21:57 -08001290 if (ring == &dev_priv->ring[RCS] &&
1291 mode != dev_priv->relative_constants_mode) {
1292 ret = intel_ring_begin(ring, 4);
1293 if (ret)
1294 goto err;
1295
1296 intel_ring_emit(ring, MI_NOOP);
1297 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1298 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001299 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001300 intel_ring_advance(ring);
1301
1302 dev_priv->relative_constants_mode = mode;
1303 }
1304
Eric Anholtae662d32012-01-03 09:23:29 -08001305 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1306 ret = i915_reset_gen7_sol_offsets(dev, ring);
1307 if (ret)
1308 goto err;
1309 }
1310
Chris Wilsondb53a302011-02-03 11:57:46 +00001311 trace_i915_gem_ring_dispatch(ring, seqno);
1312
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001313 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1314 exec_len = args->batch_len;
1315 if (cliprects) {
1316 for (i = 0; i < args->num_cliprects; i++) {
1317 ret = i915_emit_box(dev, &cliprects[i],
1318 args->DR1, args->DR4);
1319 if (ret)
1320 goto err;
1321
1322 ret = ring->dispatch_execbuffer(ring,
1323 exec_start, exec_len);
1324 if (ret)
1325 goto err;
1326 }
1327 } else {
1328 ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
1329 if (ret)
1330 goto err;
1331 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001332
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001333 i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +00001334 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001335
1336err:
Chris Wilson67731b82010-12-08 10:38:14 +00001337 eb_destroy(eb);
Chris Wilson432e58e2010-11-25 19:32:06 +00001338 while (!list_empty(&objects)) {
1339 struct drm_i915_gem_object *obj;
1340
1341 obj = list_first_entry(&objects,
1342 struct drm_i915_gem_object,
1343 exec_list);
1344 list_del_init(&obj->exec_list);
1345 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001346 }
1347
1348 mutex_unlock(&dev->struct_mutex);
1349
1350pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001351 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001352 return ret;
1353}
1354
1355/*
1356 * Legacy execbuffer just creates an exec2 list from the original exec object
1357 * list array and passes it to the real function.
1358 */
1359int
1360i915_gem_execbuffer(struct drm_device *dev, void *data,
1361 struct drm_file *file)
1362{
1363 struct drm_i915_gem_execbuffer *args = data;
1364 struct drm_i915_gem_execbuffer2 exec2;
1365 struct drm_i915_gem_exec_object *exec_list = NULL;
1366 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1367 int ret, i;
1368
Chris Wilson54cf91d2010-11-25 18:00:26 +00001369 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001370 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001371 return -EINVAL;
1372 }
1373
1374 /* Copy in the exec list from userland */
1375 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1376 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1377 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001378 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001379 args->buffer_count);
1380 drm_free_large(exec_list);
1381 drm_free_large(exec2_list);
1382 return -ENOMEM;
1383 }
1384 ret = copy_from_user(exec_list,
1385 (struct drm_i915_relocation_entry __user *)
1386 (uintptr_t) args->buffers_ptr,
1387 sizeof(*exec_list) * args->buffer_count);
1388 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001389 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001390 args->buffer_count, ret);
1391 drm_free_large(exec_list);
1392 drm_free_large(exec2_list);
1393 return -EFAULT;
1394 }
1395
1396 for (i = 0; i < args->buffer_count; i++) {
1397 exec2_list[i].handle = exec_list[i].handle;
1398 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1399 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1400 exec2_list[i].alignment = exec_list[i].alignment;
1401 exec2_list[i].offset = exec_list[i].offset;
1402 if (INTEL_INFO(dev)->gen < 4)
1403 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1404 else
1405 exec2_list[i].flags = 0;
1406 }
1407
1408 exec2.buffers_ptr = args->buffers_ptr;
1409 exec2.buffer_count = args->buffer_count;
1410 exec2.batch_start_offset = args->batch_start_offset;
1411 exec2.batch_len = args->batch_len;
1412 exec2.DR1 = args->DR1;
1413 exec2.DR4 = args->DR4;
1414 exec2.num_cliprects = args->num_cliprects;
1415 exec2.cliprects_ptr = args->cliprects_ptr;
1416 exec2.flags = I915_EXEC_RENDER;
1417
1418 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1419 if (!ret) {
1420 /* Copy the new buffer offsets back to the user's exec list. */
1421 for (i = 0; i < args->buffer_count; i++)
1422 exec_list[i].offset = exec2_list[i].offset;
1423 /* ... and back out to userspace */
1424 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1425 (uintptr_t) args->buffers_ptr,
1426 exec_list,
1427 sizeof(*exec_list) * args->buffer_count);
1428 if (ret) {
1429 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001430 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001431 "back to user (%d)\n",
1432 args->buffer_count, ret);
1433 }
1434 }
1435
1436 drm_free_large(exec_list);
1437 drm_free_large(exec2_list);
1438 return ret;
1439}
1440
1441int
1442i915_gem_execbuffer2(struct drm_device *dev, void *data,
1443 struct drm_file *file)
1444{
1445 struct drm_i915_gem_execbuffer2 *args = data;
1446 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1447 int ret;
1448
Chris Wilson54cf91d2010-11-25 18:00:26 +00001449 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001450 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001451 return -EINVAL;
1452 }
1453
Chris Wilson8408c282011-02-21 12:54:48 +00001454 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1455 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1456 if (exec2_list == NULL)
1457 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1458 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001459 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001460 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001461 args->buffer_count);
1462 return -ENOMEM;
1463 }
1464 ret = copy_from_user(exec2_list,
1465 (struct drm_i915_relocation_entry __user *)
1466 (uintptr_t) args->buffers_ptr,
1467 sizeof(*exec2_list) * args->buffer_count);
1468 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001469 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001470 args->buffer_count, ret);
1471 drm_free_large(exec2_list);
1472 return -EFAULT;
1473 }
1474
1475 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1476 if (!ret) {
1477 /* Copy the new buffer offsets back to the user's exec list. */
1478 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1479 (uintptr_t) args->buffers_ptr,
1480 exec2_list,
1481 sizeof(*exec2_list) * args->buffer_count);
1482 if (ret) {
1483 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001484 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001485 "back to user (%d)\n",
1486 args->buffer_count, ret);
1487 }
1488 }
1489
1490 drm_free_large(exec2_list);
1491 return ret;
1492}