blob: 981e14f000e373eac39057239667dc299d80cf47 [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
Chris Wilson7b096382012-04-14 09:55:51 +0100384 ret = i915_gem_object_set_to_gtt_domain(obj, true);
385 if (ret)
386 return ret;
387
388 ret = i915_gem_object_put_fence(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000389 if (ret)
Chris Wilson67731b82010-12-08 10:38:14 +0000390 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000391
392 /* Map the page containing the relocation we're going to perform. */
393 reloc->offset += obj->gtt_offset;
394 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
395 reloc->offset & PAGE_MASK);
396 reloc_entry = (uint32_t __iomem *)
397 (reloc_page + (reloc->offset & ~PAGE_MASK));
398 iowrite32(reloc->delta, reloc_entry);
399 io_mapping_unmap_atomic(reloc_page);
400 }
401
Daniel Vetter149c8402012-02-15 23:50:23 +0100402 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
403 * pipe_control writes because the gpu doesn't properly redirect them
404 * through the ppgtt for non_secure batchbuffers. */
405 if (unlikely(IS_GEN6(dev) &&
406 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
407 !target_i915_obj->has_global_gtt_mapping)) {
408 i915_gem_gtt_bind_object(target_i915_obj,
409 target_i915_obj->cache_level);
410 }
411
Chris Wilson54cf91d2010-11-25 18:00:26 +0000412 /* and update the user's relocation entry */
413 reloc->presumed_offset = target_offset;
414
Chris Wilson67731b82010-12-08 10:38:14 +0000415 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000416}
417
418static int
419i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000420 struct eb_objects *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000421{
Chris Wilson1d83f442012-03-24 20:12:53 +0000422#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
423 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000424 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000425 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000426 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000427
428 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000429
Chris Wilson1d83f442012-03-24 20:12:53 +0000430 remain = entry->relocation_count;
431 while (remain) {
432 struct drm_i915_gem_relocation_entry *r = stack_reloc;
433 int count = remain;
434 if (count > ARRAY_SIZE(stack_reloc))
435 count = ARRAY_SIZE(stack_reloc);
436 remain -= count;
437
438 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000439 return -EFAULT;
440
Chris Wilson1d83f442012-03-24 20:12:53 +0000441 do {
442 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000443
Chris Wilson1d83f442012-03-24 20:12:53 +0000444 ret = i915_gem_execbuffer_relocate_entry(obj, eb, r);
445 if (ret)
446 return ret;
447
448 if (r->presumed_offset != offset &&
449 __copy_to_user_inatomic(&user_relocs->presumed_offset,
450 &r->presumed_offset,
451 sizeof(r->presumed_offset))) {
452 return -EFAULT;
453 }
454
455 user_relocs++;
456 r++;
457 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000458 }
459
460 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000461#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000462}
463
464static int
465i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000466 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000467 struct drm_i915_gem_relocation_entry *relocs)
468{
Chris Wilson6fe4f142011-01-10 17:35:37 +0000469 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000470 int i, ret;
471
472 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000473 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000474 if (ret)
475 return ret;
476 }
477
478 return 0;
479}
480
481static int
482i915_gem_execbuffer_relocate(struct drm_device *dev,
Chris Wilson67731b82010-12-08 10:38:14 +0000483 struct eb_objects *eb,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000484 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000485{
Chris Wilson432e58e2010-11-25 19:32:06 +0000486 struct drm_i915_gem_object *obj;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000487 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000488
Chris Wilsond4aeee72011-03-14 15:11:24 +0000489 /* This is the fast path and we cannot handle a pagefault whilst
490 * holding the struct mutex lest the user pass in the relocations
491 * contained within a mmaped bo. For in such a case we, the page
492 * fault handler would call i915_gem_fault() and we would try to
493 * acquire the struct mutex again. Obviously this is bad and so
494 * lockdep complains vehemently.
495 */
496 pagefault_disable();
Chris Wilson432e58e2010-11-25 19:32:06 +0000497 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000498 ret = i915_gem_execbuffer_relocate_object(obj, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000499 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000500 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000501 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000502 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000503
Chris Wilsond4aeee72011-03-14 15:11:24 +0000504 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000505}
506
Chris Wilson1690e1e2011-12-14 13:57:08 +0100507#define __EXEC_OBJECT_HAS_FENCE (1<<31)
508
509static int
Chris Wilsondabdfe02012-03-26 10:10:27 +0200510need_reloc_mappable(struct drm_i915_gem_object *obj)
511{
512 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
513 return entry->relocation_count && !use_cpu_reloc(obj);
514}
515
516static int
Chris Wilson1690e1e2011-12-14 13:57:08 +0100517pin_and_fence_object(struct drm_i915_gem_object *obj,
518 struct intel_ring_buffer *ring)
519{
520 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
521 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
522 bool need_fence, need_mappable;
523 int ret;
524
525 need_fence =
526 has_fenced_gpu_access &&
527 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
528 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200529 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100530
531 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable);
532 if (ret)
533 return ret;
534
535 if (has_fenced_gpu_access) {
536 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100537 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000538 if (ret)
539 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100540
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000541 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100542 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000543
Chris Wilson7dd49062012-03-21 10:48:18 +0000544 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100545 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100546 }
547
548 entry->offset = obj->gtt_offset;
549 return 0;
550
551err_unpin:
552 i915_gem_object_unpin(obj);
553 return ret;
554}
555
Chris Wilson54cf91d2010-11-25 18:00:26 +0000556static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000557i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000558 struct drm_file *file,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000559 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000560{
Daniel Vetter7bddb012012-02-09 17:15:47 +0100561 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000562 struct drm_i915_gem_object *obj;
Chris Wilson432e58e2010-11-25 19:32:06 +0000563 int ret, retry;
Chris Wilson9b3826b2010-12-05 17:11:54 +0000564 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000565 struct list_head ordered_objects;
566
567 INIT_LIST_HEAD(&ordered_objects);
568 while (!list_empty(objects)) {
569 struct drm_i915_gem_exec_object2 *entry;
570 bool need_fence, need_mappable;
571
572 obj = list_first_entry(objects,
573 struct drm_i915_gem_object,
574 exec_list);
575 entry = obj->exec_entry;
576
577 need_fence =
578 has_fenced_gpu_access &&
579 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
580 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200581 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000582
583 if (need_mappable)
584 list_move(&obj->exec_list, &ordered_objects);
585 else
586 list_move_tail(&obj->exec_list, &ordered_objects);
Chris Wilson595dad72011-01-13 11:03:48 +0000587
588 obj->base.pending_read_domains = 0;
589 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000590 }
591 list_splice(&ordered_objects, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000592
593 /* Attempt to pin all of the buffers into the GTT.
594 * This is done in 3 phases:
595 *
596 * 1a. Unbind all objects that do not match the GTT constraints for
597 * the execbuffer (fenceable, mappable, alignment etc).
598 * 1b. Increment pin count for already bound objects.
599 * 2. Bind new objects.
600 * 3. Decrement pin count.
601 *
602 * This avoid unnecessary unbinding of later objects in order to makr
603 * room for the earlier objects *unless* we need to defragment.
604 */
605 retry = 0;
606 do {
607 ret = 0;
608
609 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000610 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000611 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000612 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100613
Chris Wilson6fe4f142011-01-10 17:35:37 +0000614 if (!obj->gtt_space)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000615 continue;
616
617 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000618 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000619 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
620 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200621 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000622
623 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
624 (need_mappable && !obj->map_and_fenceable))
625 ret = i915_gem_object_unbind(obj);
626 else
Chris Wilson1690e1e2011-12-14 13:57:08 +0100627 ret = pin_and_fence_object(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000628 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000629 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000630 }
631
632 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000633 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100634 if (obj->gtt_space)
635 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000636
Chris Wilson1690e1e2011-12-14 13:57:08 +0100637 ret = pin_and_fence_object(obj, ring);
638 if (ret) {
639 int ret_ignore;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000640
Chris Wilson1690e1e2011-12-14 13:57:08 +0100641 /* This can potentially raise a harmless
642 * -EINVAL if we failed to bind in the above
643 * call. It cannot raise -EINTR since we know
644 * that the bo is freshly bound and so will
645 * not need to be flushed or waited upon.
646 */
647 ret_ignore = i915_gem_object_unbind(obj);
648 (void)ret_ignore;
649 WARN_ON(obj->gtt_space);
650 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000651 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000652 }
653
Chris Wilson432e58e2010-11-25 19:32:06 +0000654 /* Decrement pin count for bound objects */
655 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100656 struct drm_i915_gem_exec_object2 *entry;
657
658 if (!obj->gtt_space)
659 continue;
660
661 entry = obj->exec_entry;
662 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
663 i915_gem_object_unpin_fence(obj);
664 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
665 }
666
667 i915_gem_object_unpin(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100668
669 /* ... and ensure ppgtt mapping exist if needed. */
670 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
671 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
672 obj, obj->cache_level);
673
674 obj->has_aliasing_ppgtt_mapping = 1;
675 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000676 }
677
678 if (ret != -ENOSPC || retry > 1)
679 return ret;
680
681 /* First attempt, just clear anything that is purgeable.
682 * Second attempt, clear the entire GTT.
683 */
Chris Wilsond9e86c02010-11-10 16:40:20 +0000684 ret = i915_gem_evict_everything(ring->dev, retry == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000685 if (ret)
686 return ret;
687
688 retry++;
689 } while (1);
Chris Wilson432e58e2010-11-25 19:32:06 +0000690
691err:
Chris Wilson1690e1e2011-12-14 13:57:08 +0100692 list_for_each_entry_continue_reverse(obj, objects, exec_list) {
693 struct drm_i915_gem_exec_object2 *entry;
Chris Wilson432e58e2010-11-25 19:32:06 +0000694
Chris Wilson1690e1e2011-12-14 13:57:08 +0100695 if (!obj->gtt_space)
696 continue;
697
698 entry = obj->exec_entry;
699 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
700 i915_gem_object_unpin_fence(obj);
701 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
702 }
703
704 i915_gem_object_unpin(obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000705 }
706
707 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000708}
709
710static int
711i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
712 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000713 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000714 struct list_head *objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000715 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000716 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000717 int count)
718{
719 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000720 struct drm_i915_gem_object *obj;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000721 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000722 int i, total, ret;
723
Chris Wilson67731b82010-12-08 10:38:14 +0000724 /* We may process another execbuffer during the unlock... */
Chris Wilson36cf1742011-01-10 12:09:12 +0000725 while (!list_empty(objects)) {
Chris Wilson67731b82010-12-08 10:38:14 +0000726 obj = list_first_entry(objects,
727 struct drm_i915_gem_object,
728 exec_list);
729 list_del_init(&obj->exec_list);
730 drm_gem_object_unreference(&obj->base);
731 }
732
Chris Wilson54cf91d2010-11-25 18:00:26 +0000733 mutex_unlock(&dev->struct_mutex);
734
735 total = 0;
736 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000737 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000738
Chris Wilsondd6864a2011-01-12 23:49:13 +0000739 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000740 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000741 if (reloc == NULL || reloc_offset == NULL) {
742 drm_free_large(reloc);
743 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000744 mutex_lock(&dev->struct_mutex);
745 return -ENOMEM;
746 }
747
748 total = 0;
749 for (i = 0; i < count; i++) {
750 struct drm_i915_gem_relocation_entry __user *user_relocs;
751
Chris Wilson432e58e2010-11-25 19:32:06 +0000752 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000753
754 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000755 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000756 ret = -EFAULT;
757 mutex_lock(&dev->struct_mutex);
758 goto err;
759 }
760
Chris Wilsondd6864a2011-01-12 23:49:13 +0000761 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000762 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000763 }
764
765 ret = i915_mutex_lock_interruptible(dev);
766 if (ret) {
767 mutex_lock(&dev->struct_mutex);
768 goto err;
769 }
770
Chris Wilson67731b82010-12-08 10:38:14 +0000771 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000772 eb_reset(eb);
773 for (i = 0; i < count; i++) {
Chris Wilson67731b82010-12-08 10:38:14 +0000774 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
775 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000776 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100777 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson67731b82010-12-08 10:38:14 +0000778 exec[i].handle, i);
779 ret = -ENOENT;
780 goto err;
781 }
782
783 list_add_tail(&obj->exec_list, objects);
784 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000785 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000786 eb_add_object(eb, obj);
787 }
788
Chris Wilson6fe4f142011-01-10 17:35:37 +0000789 ret = i915_gem_execbuffer_reserve(ring, file, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000790 if (ret)
791 goto err;
792
Chris Wilson432e58e2010-11-25 19:32:06 +0000793 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondd6864a2011-01-12 23:49:13 +0000794 int offset = obj->exec_entry - exec;
Chris Wilson67731b82010-12-08 10:38:14 +0000795 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilsondd6864a2011-01-12 23:49:13 +0000796 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000797 if (ret)
798 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000799 }
800
801 /* Leave the user relocations as are, this is the painfully slow path,
802 * and we want to avoid the complication of dropping the lock whilst
803 * having buffers reserved in the aperture and so causing spurious
804 * ENOSPC for random operations.
805 */
806
807err:
808 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000809 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000810 return ret;
811}
812
Daniel Vettercc889e02012-06-13 20:45:19 +0200813static void
Chris Wilson54cf91d2010-11-25 18:00:26 +0000814i915_gem_execbuffer_flush(struct drm_device *dev,
815 uint32_t invalidate_domains,
Daniel Vettercc889e02012-06-13 20:45:19 +0200816 uint32_t flush_domains)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000817{
Chris Wilson54cf91d2010-11-25 18:00:26 +0000818 if (flush_domains & I915_GEM_DOMAIN_CPU)
819 intel_gtt_chipset_flush();
820
Chris Wilson63256ec2011-01-04 18:42:07 +0000821 if (flush_domains & I915_GEM_DOMAIN_GTT)
822 wmb();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000823}
824
Chris Wilson54cf91d2010-11-25 18:00:26 +0000825static int
Chris Wilsonc59a3332011-03-06 13:51:29 +0000826i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
827{
828 u32 plane, flip_mask;
829 int ret;
830
831 /* Check for any pending flips. As we only maintain a flip queue depth
832 * of 1, we can simply insert a WAIT for the next display flip prior
833 * to executing the batch and avoid stalling the CPU.
834 */
835
836 for (plane = 0; flips >> plane; plane++) {
837 if (((flips >> plane) & 1) == 0)
838 continue;
839
840 if (plane)
841 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
842 else
843 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
844
845 ret = intel_ring_begin(ring, 2);
846 if (ret)
847 return ret;
848
849 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
850 intel_ring_emit(ring, MI_NOOP);
851 intel_ring_advance(ring);
852 }
853
854 return 0;
855}
856
857
858static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000859i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
860 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000861{
Chris Wilson432e58e2010-11-25 19:32:06 +0000862 struct drm_i915_gem_object *obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000863 struct change_domains cd;
Chris Wilson432e58e2010-11-25 19:32:06 +0000864 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000865
Chris Wilsonc59a3332011-03-06 13:51:29 +0000866 memset(&cd, 0, sizeof(cd));
Chris Wilson432e58e2010-11-25 19:32:06 +0000867 list_for_each_entry(obj, objects, exec_list)
868 i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000869
870 if (cd.invalidate_domains | cd.flush_domains) {
Daniel Vettercc889e02012-06-13 20:45:19 +0200871 i915_gem_execbuffer_flush(ring->dev,
872 cd.invalidate_domains,
873 cd.flush_domains);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000874 }
875
Chris Wilsonc59a3332011-03-06 13:51:29 +0000876 if (cd.flips) {
877 ret = i915_gem_execbuffer_wait_for_flips(ring, cd.flips);
878 if (ret)
879 return ret;
880 }
881
Chris Wilson432e58e2010-11-25 19:32:06 +0000882 list_for_each_entry(obj, objects, exec_list) {
Ben Widawsky2911a352012-04-05 14:47:36 -0700883 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000884 if (ret)
885 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000886 }
887
Chris Wilson09cf7c92012-07-13 14:14:08 +0100888 /* Unconditionally invalidate gpu caches and ensure that we do flush
889 * any residual writes from the previous batch.
890 */
891 ret = i915_gem_flush_ring(ring,
892 I915_GEM_GPU_DOMAINS,
893 ring->gpu_caches_dirty ? I915_GEM_GPU_DOMAINS : 0);
Daniel Vettercc889e02012-06-13 20:45:19 +0200894 if (ret)
895 return ret;
896
Chris Wilson09cf7c92012-07-13 14:14:08 +0100897 ring->gpu_caches_dirty = false;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000898 return 0;
899}
900
Chris Wilson432e58e2010-11-25 19:32:06 +0000901static bool
902i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000903{
Chris Wilson432e58e2010-11-25 19:32:06 +0000904 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000905}
906
907static int
908validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
909 int count)
910{
911 int i;
912
913 for (i = 0; i < count; i++) {
914 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
915 int length; /* limited by fault_in_pages_readable() */
916
917 /* First check for malicious input causing overflow */
918 if (exec[i].relocation_count >
919 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
920 return -EINVAL;
921
922 length = exec[i].relocation_count *
923 sizeof(struct drm_i915_gem_relocation_entry);
924 if (!access_ok(VERIFY_READ, ptr, length))
925 return -EFAULT;
926
927 /* we may also need to update the presumed offsets */
928 if (!access_ok(VERIFY_WRITE, ptr, length))
929 return -EFAULT;
930
Daniel Vetterf56f8212012-03-25 19:47:41 +0200931 if (fault_in_multipages_readable(ptr, length))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000932 return -EFAULT;
933 }
934
935 return 0;
936}
937
Chris Wilson432e58e2010-11-25 19:32:06 +0000938static void
939i915_gem_execbuffer_move_to_active(struct list_head *objects,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000940 struct intel_ring_buffer *ring,
941 u32 seqno)
Chris Wilson432e58e2010-11-25 19:32:06 +0000942{
943 struct drm_i915_gem_object *obj;
944
945 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondb53a302011-02-03 11:57:46 +0000946 u32 old_read = obj->base.read_domains;
947 u32 old_write = obj->base.write_domain;
948
949
Chris Wilson432e58e2010-11-25 19:32:06 +0000950 obj->base.read_domains = obj->base.pending_read_domains;
951 obj->base.write_domain = obj->base.pending_write_domain;
952 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
953
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000954 i915_gem_object_move_to_active(obj, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +0000955 if (obj->base.write_domain) {
956 obj->dirty = 1;
Chris Wilson87ca9c82010-12-02 09:42:56 +0000957 obj->pending_gpu_write = true;
Chris Wilson432e58e2010-11-25 19:32:06 +0000958 list_move_tail(&obj->gpu_write_list,
959 &ring->gpu_write_list);
Chris Wilsonacb87df2012-05-03 15:47:57 +0100960 if (obj->pin_count) /* check for potential scanout */
961 intel_mark_busy(ring->dev, obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000962 }
963
Chris Wilsondb53a302011-02-03 11:57:46 +0000964 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000965 }
Chris Wilsonacb87df2012-05-03 15:47:57 +0100966
967 intel_mark_busy(ring->dev, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000968}
969
Chris Wilson54cf91d2010-11-25 18:00:26 +0000970static void
971i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000972 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000973 struct intel_ring_buffer *ring)
974{
Chris Wilson432e58e2010-11-25 19:32:06 +0000975 struct drm_i915_gem_request *request;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000976
Daniel Vettercc889e02012-06-13 20:45:19 +0200977 /* Unconditionally force add_request to emit a full flush. */
978 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000979
Chris Wilson432e58e2010-11-25 19:32:06 +0000980 /* Add a breadcrumb for the completion of the batch buffer */
981 request = kzalloc(sizeof(*request), GFP_KERNEL);
Chris Wilsondb53a302011-02-03 11:57:46 +0000982 if (request == NULL || i915_add_request(ring, file, request)) {
Chris Wilson432e58e2010-11-25 19:32:06 +0000983 kfree(request);
984 }
985}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000986
987static int
Eric Anholtae662d32012-01-03 09:23:29 -0800988i915_reset_gen7_sol_offsets(struct drm_device *dev,
989 struct intel_ring_buffer *ring)
990{
991 drm_i915_private_t *dev_priv = dev->dev_private;
992 int ret, i;
993
994 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
995 return 0;
996
997 ret = intel_ring_begin(ring, 4 * 3);
998 if (ret)
999 return ret;
1000
1001 for (i = 0; i < 4; i++) {
1002 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1003 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
1004 intel_ring_emit(ring, 0);
1005 }
1006
1007 intel_ring_advance(ring);
1008
1009 return 0;
1010}
1011
1012static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001013i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1014 struct drm_file *file,
1015 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +00001016 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001017{
1018 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +00001019 struct list_head objects;
Chris Wilson67731b82010-12-08 10:38:14 +00001020 struct eb_objects *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001021 struct drm_i915_gem_object *batch_obj;
1022 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001023 struct intel_ring_buffer *ring;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001024 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001025 u32 exec_start, exec_len;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001026 u32 seqno;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001027 u32 mask;
Chris Wilson72bfa192010-12-19 11:42:05 +00001028 int ret, mode, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001029
Chris Wilson432e58e2010-11-25 19:32:06 +00001030 if (!i915_gem_check_execbuffer(args)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001031 DRM_DEBUG("execbuf with invalid offset/length\n");
Chris Wilson432e58e2010-11-25 19:32:06 +00001032 return -EINVAL;
1033 }
1034
1035 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001036 if (ret)
1037 return ret;
1038
Chris Wilson54cf91d2010-11-25 18:00:26 +00001039 switch (args->flags & I915_EXEC_RING_MASK) {
1040 case I915_EXEC_DEFAULT:
1041 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001042 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +00001043 break;
1044 case I915_EXEC_BSD:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001045 ring = &dev_priv->ring[VCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001046 if (ctx_id != 0) {
1047 DRM_DEBUG("Ring %s doesn't support contexts\n",
1048 ring->name);
1049 return -EPERM;
1050 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001051 break;
1052 case I915_EXEC_BLT:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001053 ring = &dev_priv->ring[BCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001054 if (ctx_id != 0) {
1055 DRM_DEBUG("Ring %s doesn't support contexts\n",
1056 ring->name);
1057 return -EPERM;
1058 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001059 break;
1060 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001061 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001062 (int)(args->flags & I915_EXEC_RING_MASK));
1063 return -EINVAL;
1064 }
Chris Wilsona15817c2012-05-11 14:29:31 +01001065 if (!intel_ring_initialized(ring)) {
1066 DRM_DEBUG("execbuf with invalid ring: %d\n",
1067 (int)(args->flags & I915_EXEC_RING_MASK));
1068 return -EINVAL;
1069 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001070
Chris Wilson72bfa192010-12-19 11:42:05 +00001071 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001072 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +00001073 switch (mode) {
1074 case I915_EXEC_CONSTANTS_REL_GENERAL:
1075 case I915_EXEC_CONSTANTS_ABSOLUTE:
1076 case I915_EXEC_CONSTANTS_REL_SURFACE:
1077 if (ring == &dev_priv->ring[RCS] &&
1078 mode != dev_priv->relative_constants_mode) {
1079 if (INTEL_INFO(dev)->gen < 4)
1080 return -EINVAL;
1081
1082 if (INTEL_INFO(dev)->gen > 5 &&
1083 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1084 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001085
1086 /* The HW changed the meaning on this bit on gen6 */
1087 if (INTEL_INFO(dev)->gen >= 6)
1088 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +00001089 }
1090 break;
1091 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001092 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +00001093 return -EINVAL;
1094 }
1095
Chris Wilson54cf91d2010-11-25 18:00:26 +00001096 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001097 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001098 return -EINVAL;
1099 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001100
1101 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001102 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +01001103 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001104 return -EINVAL;
1105 }
1106
Daniel Vetter6ebebc92012-04-26 23:28:11 +02001107 if (INTEL_INFO(dev)->gen >= 5) {
1108 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1109 return -EINVAL;
1110 }
1111
Xi Wang44afb3a2012-04-23 04:06:42 -04001112 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1113 DRM_DEBUG("execbuf with %u cliprects\n",
1114 args->num_cliprects);
1115 return -EINVAL;
1116 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001117
Chris Wilson432e58e2010-11-25 19:32:06 +00001118 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001119 GFP_KERNEL);
1120 if (cliprects == NULL) {
1121 ret = -ENOMEM;
1122 goto pre_mutex_err;
1123 }
1124
Chris Wilson432e58e2010-11-25 19:32:06 +00001125 if (copy_from_user(cliprects,
1126 (struct drm_clip_rect __user *)(uintptr_t)
1127 args->cliprects_ptr,
1128 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001129 ret = -EFAULT;
1130 goto pre_mutex_err;
1131 }
1132 }
1133
Chris Wilson54cf91d2010-11-25 18:00:26 +00001134 ret = i915_mutex_lock_interruptible(dev);
1135 if (ret)
1136 goto pre_mutex_err;
1137
1138 if (dev_priv->mm.suspended) {
1139 mutex_unlock(&dev->struct_mutex);
1140 ret = -EBUSY;
1141 goto pre_mutex_err;
1142 }
1143
Chris Wilson67731b82010-12-08 10:38:14 +00001144 eb = eb_create(args->buffer_count);
1145 if (eb == NULL) {
1146 mutex_unlock(&dev->struct_mutex);
1147 ret = -ENOMEM;
1148 goto pre_mutex_err;
1149 }
1150
Chris Wilson54cf91d2010-11-25 18:00:26 +00001151 /* Look up object handles */
Chris Wilson432e58e2010-11-25 19:32:06 +00001152 INIT_LIST_HEAD(&objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001153 for (i = 0; i < args->buffer_count; i++) {
1154 struct drm_i915_gem_object *obj;
1155
Chris Wilson432e58e2010-11-25 19:32:06 +00001156 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
1157 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001158 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001159 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson432e58e2010-11-25 19:32:06 +00001160 exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001161 /* prevent error path from reading uninitialized data */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001162 ret = -ENOENT;
1163 goto err;
1164 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001165
Chris Wilson432e58e2010-11-25 19:32:06 +00001166 if (!list_empty(&obj->exec_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001167 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
Chris Wilson432e58e2010-11-25 19:32:06 +00001168 obj, exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001169 ret = -EINVAL;
1170 goto err;
1171 }
Chris Wilson432e58e2010-11-25 19:32:06 +00001172
1173 list_add_tail(&obj->exec_list, &objects);
Chris Wilson67731b82010-12-08 10:38:14 +00001174 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +00001175 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +00001176 eb_add_object(eb, obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001177 }
1178
Chris Wilson6fe4f142011-01-10 17:35:37 +00001179 /* take note of the batch buffer before we might reorder the lists */
1180 batch_obj = list_entry(objects.prev,
1181 struct drm_i915_gem_object,
1182 exec_list);
1183
Chris Wilson54cf91d2010-11-25 18:00:26 +00001184 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilson6fe4f142011-01-10 17:35:37 +00001185 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001186 if (ret)
1187 goto err;
1188
1189 /* The objects are in their final locations, apply the relocations. */
Chris Wilson6fe4f142011-01-10 17:35:37 +00001190 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001191 if (ret) {
1192 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00001193 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilson67731b82010-12-08 10:38:14 +00001194 &objects, eb,
1195 exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001196 args->buffer_count);
1197 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1198 }
1199 if (ret)
1200 goto err;
1201 }
1202
1203 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001204 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001205 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001206 ret = -EINVAL;
1207 goto err;
1208 }
1209 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1210
Chris Wilson432e58e2010-11-25 19:32:06 +00001211 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001212 if (ret)
1213 goto err;
1214
Chris Wilsondb53a302011-02-03 11:57:46 +00001215 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson076e2c02011-01-21 10:07:18 +00001216 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001217 if (seqno < ring->sync_seqno[i]) {
1218 /* The GPU can not handle its semaphore value wrapping,
1219 * so every billion or so execbuffers, we need to stall
1220 * the GPU in order to reset the counters.
1221 */
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001222 ret = i915_gpu_idle(dev);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001223 if (ret)
1224 goto err;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001225 i915_gem_retire_requests(dev);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001226
1227 BUG_ON(ring->sync_seqno[i]);
1228 }
1229 }
1230
Ben Widawskye2971bd2011-12-12 19:21:57 -08001231 if (ring == &dev_priv->ring[RCS] &&
1232 mode != dev_priv->relative_constants_mode) {
1233 ret = intel_ring_begin(ring, 4);
1234 if (ret)
1235 goto err;
1236
1237 intel_ring_emit(ring, MI_NOOP);
1238 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1239 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001240 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001241 intel_ring_advance(ring);
1242
1243 dev_priv->relative_constants_mode = mode;
1244 }
1245
Eric Anholtae662d32012-01-03 09:23:29 -08001246 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1247 ret = i915_reset_gen7_sol_offsets(dev, ring);
1248 if (ret)
1249 goto err;
1250 }
1251
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001252 ret = i915_switch_context(ring, file, ctx_id);
1253 if (ret)
1254 goto err;
1255
Chris Wilsondb53a302011-02-03 11:57:46 +00001256 trace_i915_gem_ring_dispatch(ring, seqno);
1257
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001258 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1259 exec_len = args->batch_len;
1260 if (cliprects) {
1261 for (i = 0; i < args->num_cliprects; i++) {
1262 ret = i915_emit_box(dev, &cliprects[i],
1263 args->DR1, args->DR4);
1264 if (ret)
1265 goto err;
1266
1267 ret = ring->dispatch_execbuffer(ring,
1268 exec_start, exec_len);
1269 if (ret)
1270 goto err;
1271 }
1272 } else {
1273 ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
1274 if (ret)
1275 goto err;
1276 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001277
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001278 i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +00001279 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001280
1281err:
Chris Wilson67731b82010-12-08 10:38:14 +00001282 eb_destroy(eb);
Chris Wilson432e58e2010-11-25 19:32:06 +00001283 while (!list_empty(&objects)) {
1284 struct drm_i915_gem_object *obj;
1285
1286 obj = list_first_entry(&objects,
1287 struct drm_i915_gem_object,
1288 exec_list);
1289 list_del_init(&obj->exec_list);
1290 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001291 }
1292
1293 mutex_unlock(&dev->struct_mutex);
1294
1295pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001296 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001297 return ret;
1298}
1299
1300/*
1301 * Legacy execbuffer just creates an exec2 list from the original exec object
1302 * list array and passes it to the real function.
1303 */
1304int
1305i915_gem_execbuffer(struct drm_device *dev, void *data,
1306 struct drm_file *file)
1307{
1308 struct drm_i915_gem_execbuffer *args = data;
1309 struct drm_i915_gem_execbuffer2 exec2;
1310 struct drm_i915_gem_exec_object *exec_list = NULL;
1311 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1312 int ret, i;
1313
Chris Wilson54cf91d2010-11-25 18:00:26 +00001314 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001315 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001316 return -EINVAL;
1317 }
1318
1319 /* Copy in the exec list from userland */
1320 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1321 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1322 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001323 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001324 args->buffer_count);
1325 drm_free_large(exec_list);
1326 drm_free_large(exec2_list);
1327 return -ENOMEM;
1328 }
1329 ret = copy_from_user(exec_list,
1330 (struct drm_i915_relocation_entry __user *)
1331 (uintptr_t) args->buffers_ptr,
1332 sizeof(*exec_list) * args->buffer_count);
1333 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001334 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001335 args->buffer_count, ret);
1336 drm_free_large(exec_list);
1337 drm_free_large(exec2_list);
1338 return -EFAULT;
1339 }
1340
1341 for (i = 0; i < args->buffer_count; i++) {
1342 exec2_list[i].handle = exec_list[i].handle;
1343 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1344 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1345 exec2_list[i].alignment = exec_list[i].alignment;
1346 exec2_list[i].offset = exec_list[i].offset;
1347 if (INTEL_INFO(dev)->gen < 4)
1348 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1349 else
1350 exec2_list[i].flags = 0;
1351 }
1352
1353 exec2.buffers_ptr = args->buffers_ptr;
1354 exec2.buffer_count = args->buffer_count;
1355 exec2.batch_start_offset = args->batch_start_offset;
1356 exec2.batch_len = args->batch_len;
1357 exec2.DR1 = args->DR1;
1358 exec2.DR4 = args->DR4;
1359 exec2.num_cliprects = args->num_cliprects;
1360 exec2.cliprects_ptr = args->cliprects_ptr;
1361 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001362 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001363
1364 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1365 if (!ret) {
1366 /* Copy the new buffer offsets back to the user's exec list. */
1367 for (i = 0; i < args->buffer_count; i++)
1368 exec_list[i].offset = exec2_list[i].offset;
1369 /* ... and back out to userspace */
1370 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1371 (uintptr_t) args->buffers_ptr,
1372 exec_list,
1373 sizeof(*exec_list) * args->buffer_count);
1374 if (ret) {
1375 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001376 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001377 "back to user (%d)\n",
1378 args->buffer_count, ret);
1379 }
1380 }
1381
1382 drm_free_large(exec_list);
1383 drm_free_large(exec2_list);
1384 return ret;
1385}
1386
1387int
1388i915_gem_execbuffer2(struct drm_device *dev, void *data,
1389 struct drm_file *file)
1390{
1391 struct drm_i915_gem_execbuffer2 *args = data;
1392 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1393 int ret;
1394
Xi Wanged8cd3b2012-04-23 04:06:41 -04001395 if (args->buffer_count < 1 ||
1396 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001397 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001398 return -EINVAL;
1399 }
1400
Chris Wilson8408c282011-02-21 12:54:48 +00001401 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1402 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1403 if (exec2_list == NULL)
1404 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1405 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001406 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001407 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001408 args->buffer_count);
1409 return -ENOMEM;
1410 }
1411 ret = copy_from_user(exec2_list,
1412 (struct drm_i915_relocation_entry __user *)
1413 (uintptr_t) args->buffers_ptr,
1414 sizeof(*exec2_list) * args->buffer_count);
1415 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001416 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001417 args->buffer_count, ret);
1418 drm_free_large(exec2_list);
1419 return -EFAULT;
1420 }
1421
1422 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1423 if (!ret) {
1424 /* Copy the new buffer offsets back to the user's exec list. */
1425 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1426 (uintptr_t) args->buffers_ptr,
1427 exec2_list,
1428 sizeof(*exec2_list) * args->buffer_count);
1429 if (ret) {
1430 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001431 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001432 "back to user (%d)\n",
1433 args->buffer_count, ret);
1434 }
1435 }
1436
1437 drm_free_large(exec2_list);
1438 return ret;
1439}