blob: 9bdc495e17bb50c9a5399811c539c91b420ab264 [file] [log] [blame]
Chris Wilson54cf91d2010-11-25 18:00:26 +00001/*
2 * Copyright © 2008,2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uk>
26 *
27 */
28
29#include "drmP.h"
30#include "drm.h"
31#include "i915_drm.h"
32#include "i915_drv.h"
33#include "i915_trace.h"
34#include "intel_drv.h"
35
36struct change_domains {
37 uint32_t invalidate_domains;
38 uint32_t flush_domains;
39 uint32_t flush_rings;
40};
41
42/*
43 * Set the next domain for the specified object. This
44 * may not actually perform the necessary flushing/invaliding though,
45 * as that may want to be batched with other set_domain operations
46 *
47 * This is (we hope) the only really tricky part of gem. The goal
48 * is fairly simple -- track which caches hold bits of the object
49 * and make sure they remain coherent. A few concrete examples may
50 * help to explain how it works. For shorthand, we use the notation
51 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
52 * a pair of read and write domain masks.
53 *
54 * Case 1: the batch buffer
55 *
56 * 1. Allocated
57 * 2. Written by CPU
58 * 3. Mapped to GTT
59 * 4. Read by GPU
60 * 5. Unmapped from GTT
61 * 6. Freed
62 *
63 * Let's take these a step at a time
64 *
65 * 1. Allocated
66 * Pages allocated from the kernel may still have
67 * cache contents, so we set them to (CPU, CPU) always.
68 * 2. Written by CPU (using pwrite)
69 * The pwrite function calls set_domain (CPU, CPU) and
70 * this function does nothing (as nothing changes)
71 * 3. Mapped by GTT
72 * This function asserts that the object is not
73 * currently in any GPU-based read or write domains
74 * 4. Read by GPU
75 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
76 * As write_domain is zero, this function adds in the
77 * current read domains (CPU+COMMAND, 0).
78 * flush_domains is set to CPU.
79 * invalidate_domains is set to COMMAND
80 * clflush is run to get data out of the CPU caches
81 * then i915_dev_set_domain calls i915_gem_flush to
82 * emit an MI_FLUSH and drm_agp_chipset_flush
83 * 5. Unmapped from GTT
84 * i915_gem_object_unbind calls set_domain (CPU, CPU)
85 * flush_domains and invalidate_domains end up both zero
86 * so no flushing/invalidating happens
87 * 6. Freed
88 * yay, done
89 *
90 * Case 2: The shared render buffer
91 *
92 * 1. Allocated
93 * 2. Mapped to GTT
94 * 3. Read/written by GPU
95 * 4. set_domain to (CPU,CPU)
96 * 5. Read/written by CPU
97 * 6. Read/written by GPU
98 *
99 * 1. Allocated
100 * Same as last example, (CPU, CPU)
101 * 2. Mapped to GTT
102 * Nothing changes (assertions find that it is not in the GPU)
103 * 3. Read/written by GPU
104 * execbuffer calls set_domain (RENDER, RENDER)
105 * flush_domains gets CPU
106 * invalidate_domains gets GPU
107 * clflush (obj)
108 * MI_FLUSH and drm_agp_chipset_flush
109 * 4. set_domain (CPU, CPU)
110 * flush_domains gets GPU
111 * invalidate_domains gets CPU
112 * wait_rendering (obj) to make sure all drawing is complete.
113 * This will include an MI_FLUSH to get the data from GPU
114 * to memory
115 * clflush (obj) to invalidate the CPU cache
116 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
117 * 5. Read/written by CPU
118 * cache lines are loaded and dirtied
119 * 6. Read written by GPU
120 * Same as last GPU access
121 *
122 * Case 3: The constant buffer
123 *
124 * 1. Allocated
125 * 2. Written by CPU
126 * 3. Read by GPU
127 * 4. Updated (written) by CPU again
128 * 5. Read by GPU
129 *
130 * 1. Allocated
131 * (CPU, CPU)
132 * 2. Written by CPU
133 * (CPU, CPU)
134 * 3. Read by GPU
135 * (CPU+RENDER, 0)
136 * flush_domains = CPU
137 * invalidate_domains = RENDER
138 * clflush (obj)
139 * MI_FLUSH
140 * drm_agp_chipset_flush
141 * 4. Updated (written) by CPU again
142 * (CPU, CPU)
143 * flush_domains = 0 (no previous write domain)
144 * invalidate_domains = 0 (no new read domains)
145 * 5. Read by GPU
146 * (CPU+RENDER, 0)
147 * flush_domains = CPU
148 * invalidate_domains = RENDER
149 * clflush (obj)
150 * MI_FLUSH
151 * drm_agp_chipset_flush
152 */
153static void
154i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
155 struct intel_ring_buffer *ring,
156 struct change_domains *cd)
157{
158 uint32_t invalidate_domains = 0, flush_domains = 0;
159
160 /*
161 * If the object isn't moving to a new write domain,
162 * let the object stay in multiple read domains
163 */
164 if (obj->base.pending_write_domain == 0)
165 obj->base.pending_read_domains |= obj->base.read_domains;
166
167 /*
168 * Flush the current write domain if
169 * the new read domains don't match. Invalidate
170 * any read domains which differ from the old
171 * write domain
172 */
173 if (obj->base.write_domain &&
174 (((obj->base.write_domain != obj->base.pending_read_domains ||
175 obj->ring != ring)) ||
176 (obj->fenced_gpu_access && !obj->pending_fenced_gpu_access))) {
177 flush_domains |= obj->base.write_domain;
178 invalidate_domains |=
179 obj->base.pending_read_domains & ~obj->base.write_domain;
180 }
181 /*
182 * Invalidate any read caches which may have
183 * stale data. That is, any new read domains.
184 */
185 invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
186 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
187 i915_gem_clflush_object(obj);
188
189 /* blow away mappings if mapped through GTT */
190 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
191 i915_gem_release_mmap(obj);
192
193 /* The actual obj->write_domain will be updated with
194 * pending_write_domain after we emit the accumulated flush for all
195 * of our domain changes in execbuffers (which clears objects'
196 * write_domains). So if we have a current write domain that we
197 * aren't changing, set pending_write_domain to that.
198 */
199 if (flush_domains == 0 && obj->base.pending_write_domain == 0)
200 obj->base.pending_write_domain = obj->base.write_domain;
201
202 cd->invalidate_domains |= invalidate_domains;
203 cd->flush_domains |= flush_domains;
204 if (flush_domains & I915_GEM_GPU_DOMAINS)
205 cd->flush_rings |= obj->ring->id;
206 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
207 cd->flush_rings |= ring->id;
208}
209
210static int
211i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
212 struct drm_file *file_priv,
213 struct drm_i915_gem_exec_object2 *entry,
214 struct drm_i915_gem_relocation_entry *reloc)
215{
216 struct drm_device *dev = obj->base.dev;
217 struct drm_gem_object *target_obj;
218 uint32_t target_offset;
219 int ret = -EINVAL;
220
221 target_obj = drm_gem_object_lookup(dev, file_priv,
222 reloc->target_handle);
223 if (target_obj == NULL)
224 return -ENOENT;
225
226 target_offset = to_intel_bo(target_obj)->gtt_offset;
227
228#if WATCH_RELOC
229 DRM_INFO("%s: obj %p offset %08x target %d "
230 "read %08x write %08x gtt %08x "
231 "presumed %08x delta %08x\n",
232 __func__,
233 obj,
234 (int) reloc->offset,
235 (int) reloc->target_handle,
236 (int) reloc->read_domains,
237 (int) reloc->write_domain,
238 (int) target_offset,
239 (int) reloc->presumed_offset,
240 reloc->delta);
241#endif
242
243 /* The target buffer should have appeared before us in the
244 * exec_object list, so it should have a GTT space bound by now.
245 */
246 if (target_offset == 0) {
247 DRM_ERROR("No GTT space found for object %d\n",
248 reloc->target_handle);
249 goto err;
250 }
251
252 /* Validate that the target is in a valid r/w GPU domain */
253 if (reloc->write_domain & (reloc->write_domain - 1)) {
254 DRM_ERROR("reloc with multiple write domains: "
255 "obj %p target %d offset %d "
256 "read %08x write %08x",
257 obj, reloc->target_handle,
258 (int) reloc->offset,
259 reloc->read_domains,
260 reloc->write_domain);
261 goto err;
262 }
263 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
264 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
265 DRM_ERROR("reloc with read/write CPU domains: "
266 "obj %p target %d offset %d "
267 "read %08x write %08x",
268 obj, reloc->target_handle,
269 (int) reloc->offset,
270 reloc->read_domains,
271 reloc->write_domain);
272 goto err;
273 }
274 if (reloc->write_domain && target_obj->pending_write_domain &&
275 reloc->write_domain != target_obj->pending_write_domain) {
276 DRM_ERROR("Write domain conflict: "
277 "obj %p target %d offset %d "
278 "new %08x old %08x\n",
279 obj, reloc->target_handle,
280 (int) reloc->offset,
281 reloc->write_domain,
282 target_obj->pending_write_domain);
283 goto err;
284 }
285
286 target_obj->pending_read_domains |= reloc->read_domains;
287 target_obj->pending_write_domain |= reloc->write_domain;
288
289 /* If the relocation already has the right value in it, no
290 * more work needs to be done.
291 */
292 if (target_offset == reloc->presumed_offset)
293 goto out;
294
295 /* Check that the relocation address is valid... */
296 if (reloc->offset > obj->base.size - 4) {
297 DRM_ERROR("Relocation beyond object bounds: "
298 "obj %p target %d offset %d size %d.\n",
299 obj, reloc->target_handle,
300 (int) reloc->offset,
301 (int) obj->base.size);
302 goto err;
303 }
304 if (reloc->offset & 3) {
305 DRM_ERROR("Relocation not 4-byte aligned: "
306 "obj %p target %d offset %d.\n",
307 obj, reloc->target_handle,
308 (int) reloc->offset);
309 goto err;
310 }
311
312 /* and points to somewhere within the target object. */
313 if (reloc->delta >= target_obj->size) {
314 DRM_ERROR("Relocation beyond target object bounds: "
315 "obj %p target %d delta %d size %d.\n",
316 obj, reloc->target_handle,
317 (int) reloc->delta,
318 (int) target_obj->size);
319 goto err;
320 }
321
322 reloc->delta += target_offset;
323 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
324 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
325 char *vaddr;
326
327 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
328 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
329 kunmap_atomic(vaddr);
330 } else {
331 struct drm_i915_private *dev_priv = dev->dev_private;
332 uint32_t __iomem *reloc_entry;
333 void __iomem *reloc_page;
334
335 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
336 if (ret)
337 goto err;
338
339 /* Map the page containing the relocation we're going to perform. */
340 reloc->offset += obj->gtt_offset;
341 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
342 reloc->offset & PAGE_MASK);
343 reloc_entry = (uint32_t __iomem *)
344 (reloc_page + (reloc->offset & ~PAGE_MASK));
345 iowrite32(reloc->delta, reloc_entry);
346 io_mapping_unmap_atomic(reloc_page);
347 }
348
349 /* and update the user's relocation entry */
350 reloc->presumed_offset = target_offset;
351
352out:
353 ret = 0;
354err:
355 drm_gem_object_unreference(target_obj);
356 return ret;
357}
358
359static int
360i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
361 struct drm_file *file_priv,
362 struct drm_i915_gem_exec_object2 *entry)
363{
364 struct drm_i915_gem_relocation_entry __user *user_relocs;
365 int i, ret;
366
367 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
368 for (i = 0; i < entry->relocation_count; i++) {
369 struct drm_i915_gem_relocation_entry reloc;
370
371 if (__copy_from_user_inatomic(&reloc,
372 user_relocs+i,
373 sizeof(reloc)))
374 return -EFAULT;
375
376 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
377 if (ret)
378 return ret;
379
380 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
381 &reloc.presumed_offset,
382 sizeof(reloc.presumed_offset)))
383 return -EFAULT;
384 }
385
386 return 0;
387}
388
389static int
390i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
391 struct drm_file *file_priv,
392 struct drm_i915_gem_exec_object2 *entry,
393 struct drm_i915_gem_relocation_entry *relocs)
394{
395 int i, ret;
396
397 for (i = 0; i < entry->relocation_count; i++) {
398 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
399 if (ret)
400 return ret;
401 }
402
403 return 0;
404}
405
406static int
407i915_gem_execbuffer_relocate(struct drm_device *dev,
408 struct drm_file *file,
Chris Wilson432e58e2010-11-25 19:32:06 +0000409 struct list_head *objects,
410 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000411{
Chris Wilson432e58e2010-11-25 19:32:06 +0000412 struct drm_i915_gem_object *obj;
413 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000414
Chris Wilson432e58e2010-11-25 19:32:06 +0000415 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000416 obj->base.pending_read_domains = 0;
417 obj->base.pending_write_domain = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000418 ret = i915_gem_execbuffer_relocate_object(obj, file, exec++);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000419 if (ret)
420 return ret;
421 }
422
423 return 0;
424}
425
426static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000427i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000428 struct drm_file *file,
Chris Wilson432e58e2010-11-25 19:32:06 +0000429 struct list_head *objects,
430 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000431{
Chris Wilson432e58e2010-11-25 19:32:06 +0000432 struct drm_i915_gem_object *obj;
433 struct drm_i915_gem_exec_object2 *entry;
434 int ret, retry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000435
436 /* Attempt to pin all of the buffers into the GTT.
437 * This is done in 3 phases:
438 *
439 * 1a. Unbind all objects that do not match the GTT constraints for
440 * the execbuffer (fenceable, mappable, alignment etc).
441 * 1b. Increment pin count for already bound objects.
442 * 2. Bind new objects.
443 * 3. Decrement pin count.
444 *
445 * This avoid unnecessary unbinding of later objects in order to makr
446 * room for the earlier objects *unless* we need to defragment.
447 */
448 retry = 0;
449 do {
450 ret = 0;
451
452 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000453 entry = exec;
454 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000455 bool need_fence, need_mappable;
456
Chris Wilson432e58e2010-11-25 19:32:06 +0000457 if (!obj->gtt_space) {
458 entry++;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000459 continue;
Chris Wilson432e58e2010-11-25 19:32:06 +0000460 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000461
462 need_fence =
463 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
464 obj->tiling_mode != I915_TILING_NONE;
465 need_mappable =
466 entry->relocation_count ? true : need_fence;
467
468 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
469 (need_mappable && !obj->map_and_fenceable))
470 ret = i915_gem_object_unbind(obj);
471 else
472 ret = i915_gem_object_pin(obj,
473 entry->alignment,
474 need_mappable);
Chris Wilson432e58e2010-11-25 19:32:06 +0000475 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000476 goto err;
Chris Wilson432e58e2010-11-25 19:32:06 +0000477
478 entry++;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000479 }
480
481 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000482 entry = exec;
483 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000484 bool need_fence;
485
486 need_fence =
487 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
488 obj->tiling_mode != I915_TILING_NONE;
489
490 if (!obj->gtt_space) {
491 bool need_mappable =
492 entry->relocation_count ? true : need_fence;
493
494 ret = i915_gem_object_pin(obj,
495 entry->alignment,
496 need_mappable);
497 if (ret)
498 break;
499 }
500
501 if (need_fence) {
Chris Wilsond9e86c02010-11-10 16:40:20 +0000502 ret = i915_gem_object_get_fence(obj, ring, 1);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000503 if (ret)
504 break;
Chris Wilsond9e86c02010-11-10 16:40:20 +0000505 } else if (entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
506 obj->tiling_mode == I915_TILING_NONE) {
507 /* XXX pipelined! */
508 ret = i915_gem_object_put_fence(obj);
509 if (ret)
510 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000511 }
Chris Wilson432e58e2010-11-25 19:32:06 +0000512 obj->pending_fenced_gpu_access = need_fence;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000513
514 entry->offset = obj->gtt_offset;
Chris Wilson432e58e2010-11-25 19:32:06 +0000515 entry++;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000516 }
517
Chris Wilson432e58e2010-11-25 19:32:06 +0000518 /* Decrement pin count for bound objects */
519 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000520 if (obj->gtt_space)
521 i915_gem_object_unpin(obj);
522 }
523
524 if (ret != -ENOSPC || retry > 1)
525 return ret;
526
527 /* First attempt, just clear anything that is purgeable.
528 * Second attempt, clear the entire GTT.
529 */
Chris Wilsond9e86c02010-11-10 16:40:20 +0000530 ret = i915_gem_evict_everything(ring->dev, retry == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000531 if (ret)
532 return ret;
533
534 retry++;
535 } while (1);
Chris Wilson432e58e2010-11-25 19:32:06 +0000536
537err:
Chris Wilson602606a2010-11-28 15:31:02 +0000538 obj = list_entry(obj->exec_list.prev,
539 struct drm_i915_gem_object,
540 exec_list);
Chris Wilson432e58e2010-11-25 19:32:06 +0000541 while (objects != &obj->exec_list) {
542 if (obj->gtt_space)
543 i915_gem_object_unpin(obj);
544
545 obj = list_entry(obj->exec_list.prev,
546 struct drm_i915_gem_object,
547 exec_list);
548 }
549
550 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000551}
552
553static int
554i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
555 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000556 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000557 struct list_head *objects,
558 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000559 int count)
560{
561 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000562 struct drm_i915_gem_object *obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000563 int i, total, ret;
564
Chris Wilson54cf91d2010-11-25 18:00:26 +0000565 mutex_unlock(&dev->struct_mutex);
566
567 total = 0;
568 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000569 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000570
571 reloc = drm_malloc_ab(total, sizeof(*reloc));
572 if (reloc == NULL) {
573 mutex_lock(&dev->struct_mutex);
574 return -ENOMEM;
575 }
576
577 total = 0;
578 for (i = 0; i < count; i++) {
579 struct drm_i915_gem_relocation_entry __user *user_relocs;
580
Chris Wilson432e58e2010-11-25 19:32:06 +0000581 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000582
583 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000584 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000585 ret = -EFAULT;
586 mutex_lock(&dev->struct_mutex);
587 goto err;
588 }
589
Chris Wilson432e58e2010-11-25 19:32:06 +0000590 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000591 }
592
593 ret = i915_mutex_lock_interruptible(dev);
594 if (ret) {
595 mutex_lock(&dev->struct_mutex);
596 goto err;
597 }
598
Chris Wilsond9e86c02010-11-10 16:40:20 +0000599 ret = i915_gem_execbuffer_reserve(ring, file, objects, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000600 if (ret)
601 goto err;
602
603 total = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000604 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000605 obj->base.pending_read_domains = 0;
606 obj->base.pending_write_domain = 0;
607 ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
Chris Wilson432e58e2010-11-25 19:32:06 +0000608 exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000609 reloc + total);
610 if (ret)
611 goto err;
612
Chris Wilson432e58e2010-11-25 19:32:06 +0000613 total += exec->relocation_count;
614 exec++;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000615 }
616
617 /* Leave the user relocations as are, this is the painfully slow path,
618 * and we want to avoid the complication of dropping the lock whilst
619 * having buffers reserved in the aperture and so causing spurious
620 * ENOSPC for random operations.
621 */
622
623err:
624 drm_free_large(reloc);
625 return ret;
626}
627
628static void
629i915_gem_execbuffer_flush(struct drm_device *dev,
630 uint32_t invalidate_domains,
631 uint32_t flush_domains,
632 uint32_t flush_rings)
633{
634 drm_i915_private_t *dev_priv = dev->dev_private;
635
636 if (flush_domains & I915_GEM_DOMAIN_CPU)
637 intel_gtt_chipset_flush();
638
639 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
640 if (flush_rings & RING_RENDER)
641 i915_gem_flush_ring(dev, &dev_priv->render_ring,
642 invalidate_domains, flush_domains);
643 if (flush_rings & RING_BSD)
644 i915_gem_flush_ring(dev, &dev_priv->bsd_ring,
645 invalidate_domains, flush_domains);
646 if (flush_rings & RING_BLT)
647 i915_gem_flush_ring(dev, &dev_priv->blt_ring,
648 invalidate_domains, flush_domains);
649 }
650}
651
652
653static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000654i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
655 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000656{
Chris Wilson432e58e2010-11-25 19:32:06 +0000657 struct drm_i915_gem_object *obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000658 struct change_domains cd;
Chris Wilson432e58e2010-11-25 19:32:06 +0000659 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000660
661 cd.invalidate_domains = 0;
662 cd.flush_domains = 0;
663 cd.flush_rings = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000664 list_for_each_entry(obj, objects, exec_list)
665 i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000666
667 if (cd.invalidate_domains | cd.flush_domains) {
668#if WATCH_EXEC
669 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
670 __func__,
671 cd.invalidate_domains,
672 cd.flush_domains);
673#endif
Chris Wilson432e58e2010-11-25 19:32:06 +0000674 i915_gem_execbuffer_flush(ring->dev,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000675 cd.invalidate_domains,
676 cd.flush_domains,
677 cd.flush_rings);
678 }
679
Chris Wilson432e58e2010-11-25 19:32:06 +0000680 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000681 /* XXX replace with semaphores */
682 if (obj->ring && ring != obj->ring) {
683 ret = i915_gem_object_wait_rendering(obj, true);
684 if (ret)
685 return ret;
686 }
687 }
688
689 return 0;
690}
691
Chris Wilson432e58e2010-11-25 19:32:06 +0000692static bool
693i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000694{
Chris Wilson432e58e2010-11-25 19:32:06 +0000695 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000696}
697
698static int
699validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
700 int count)
701{
702 int i;
703
704 for (i = 0; i < count; i++) {
705 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
706 int length; /* limited by fault_in_pages_readable() */
707
708 /* First check for malicious input causing overflow */
709 if (exec[i].relocation_count >
710 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
711 return -EINVAL;
712
713 length = exec[i].relocation_count *
714 sizeof(struct drm_i915_gem_relocation_entry);
715 if (!access_ok(VERIFY_READ, ptr, length))
716 return -EFAULT;
717
718 /* we may also need to update the presumed offsets */
719 if (!access_ok(VERIFY_WRITE, ptr, length))
720 return -EFAULT;
721
722 if (fault_in_pages_readable(ptr, length))
723 return -EFAULT;
724 }
725
726 return 0;
727}
728
Chris Wilson432e58e2010-11-25 19:32:06 +0000729static int
730i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring,
731 struct list_head *objects)
732{
733 struct drm_i915_gem_object *obj;
734 int flips;
735
736 /* Check for any pending flips. As we only maintain a flip queue depth
737 * of 1, we can simply insert a WAIT for the next display flip prior
738 * to executing the batch and avoid stalling the CPU.
739 */
740 flips = 0;
741 list_for_each_entry(obj, objects, exec_list) {
742 if (obj->base.write_domain)
743 flips |= atomic_read(&obj->pending_flip);
744 }
745 if (flips) {
746 int plane, flip_mask, ret;
747
748 for (plane = 0; flips >> plane; plane++) {
749 if (((flips >> plane) & 1) == 0)
750 continue;
751
752 if (plane)
753 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
754 else
755 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
756
757 ret = intel_ring_begin(ring, 2);
758 if (ret)
759 return ret;
760
761 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
762 intel_ring_emit(ring, MI_NOOP);
763 intel_ring_advance(ring);
764 }
765 }
766
767 return 0;
768}
769
770static void
771i915_gem_execbuffer_move_to_active(struct list_head *objects,
772 struct intel_ring_buffer *ring)
773{
774 struct drm_i915_gem_object *obj;
775
776 list_for_each_entry(obj, objects, exec_list) {
777 obj->base.read_domains = obj->base.pending_read_domains;
778 obj->base.write_domain = obj->base.pending_write_domain;
779 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
780
781 i915_gem_object_move_to_active(obj, ring);
782 if (obj->base.write_domain) {
783 obj->dirty = 1;
Chris Wilson87ca9c82010-12-02 09:42:56 +0000784 obj->pending_gpu_write = true;
Chris Wilson432e58e2010-11-25 19:32:06 +0000785 list_move_tail(&obj->gpu_write_list,
786 &ring->gpu_write_list);
787 intel_mark_busy(ring->dev, obj);
788 }
789
790 trace_i915_gem_object_change_domain(obj,
791 obj->base.read_domains,
792 obj->base.write_domain);
793 }
794}
795
Chris Wilson54cf91d2010-11-25 18:00:26 +0000796static void
797i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000798 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000799 struct intel_ring_buffer *ring)
800{
Chris Wilson432e58e2010-11-25 19:32:06 +0000801 struct drm_i915_gem_request *request;
802 u32 flush_domains;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000803
Chris Wilson432e58e2010-11-25 19:32:06 +0000804 /*
805 * Ensure that the commands in the batch buffer are
806 * finished before the interrupt fires.
807 *
808 * The sampler always gets flushed on i965 (sigh).
809 */
810 flush_domains = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000811 if (INTEL_INFO(dev)->gen >= 4)
812 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
813
814 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000815
Chris Wilson432e58e2010-11-25 19:32:06 +0000816 /* Add a breadcrumb for the completion of the batch buffer */
817 request = kzalloc(sizeof(*request), GFP_KERNEL);
818 if (request == NULL || i915_add_request(dev, file, request, ring)) {
819 i915_gem_next_request_seqno(dev, ring);
820 kfree(request);
821 }
822}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000823
824static int
825i915_gem_do_execbuffer(struct drm_device *dev, void *data,
826 struct drm_file *file,
827 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +0000828 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000829{
830 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000831 struct list_head objects;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000832 struct drm_i915_gem_object *batch_obj;
833 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000834 struct intel_ring_buffer *ring;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000835 u32 exec_start, exec_len;
Chris Wilson432e58e2010-11-25 19:32:06 +0000836 int ret, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000837
Chris Wilson432e58e2010-11-25 19:32:06 +0000838 if (!i915_gem_check_execbuffer(args)) {
839 DRM_ERROR("execbuf with invalid offset/length\n");
840 return -EINVAL;
841 }
842
843 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000844 if (ret)
845 return ret;
846
847#if WATCH_EXEC
848 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
849 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
850#endif
851 switch (args->flags & I915_EXEC_RING_MASK) {
852 case I915_EXEC_DEFAULT:
853 case I915_EXEC_RENDER:
854 ring = &dev_priv->render_ring;
855 break;
856 case I915_EXEC_BSD:
857 if (!HAS_BSD(dev)) {
858 DRM_ERROR("execbuf with invalid ring (BSD)\n");
859 return -EINVAL;
860 }
861 ring = &dev_priv->bsd_ring;
862 break;
863 case I915_EXEC_BLT:
864 if (!HAS_BLT(dev)) {
865 DRM_ERROR("execbuf with invalid ring (BLT)\n");
866 return -EINVAL;
867 }
868 ring = &dev_priv->blt_ring;
869 break;
870 default:
871 DRM_ERROR("execbuf with unknown ring: %d\n",
872 (int)(args->flags & I915_EXEC_RING_MASK));
873 return -EINVAL;
874 }
875
876 if (args->buffer_count < 1) {
877 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
878 return -EINVAL;
879 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000880
881 if (args->num_cliprects != 0) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000882 if (ring != &dev_priv->render_ring) {
883 DRM_ERROR("clip rectangles are only valid with the render ring\n");
884 return -EINVAL;
885 }
886
Chris Wilson432e58e2010-11-25 19:32:06 +0000887 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +0000888 GFP_KERNEL);
889 if (cliprects == NULL) {
890 ret = -ENOMEM;
891 goto pre_mutex_err;
892 }
893
Chris Wilson432e58e2010-11-25 19:32:06 +0000894 if (copy_from_user(cliprects,
895 (struct drm_clip_rect __user *)(uintptr_t)
896 args->cliprects_ptr,
897 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000898 ret = -EFAULT;
899 goto pre_mutex_err;
900 }
901 }
902
Chris Wilson54cf91d2010-11-25 18:00:26 +0000903 ret = i915_mutex_lock_interruptible(dev);
904 if (ret)
905 goto pre_mutex_err;
906
907 if (dev_priv->mm.suspended) {
908 mutex_unlock(&dev->struct_mutex);
909 ret = -EBUSY;
910 goto pre_mutex_err;
911 }
912
913 /* Look up object handles */
Chris Wilson432e58e2010-11-25 19:32:06 +0000914 INIT_LIST_HEAD(&objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000915 for (i = 0; i < args->buffer_count; i++) {
916 struct drm_i915_gem_object *obj;
917
Chris Wilson432e58e2010-11-25 19:32:06 +0000918 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
919 exec[i].handle));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000920 if (obj == NULL) {
921 DRM_ERROR("Invalid object handle %d at index %d\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000922 exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000923 /* prevent error path from reading uninitialized data */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000924 ret = -ENOENT;
925 goto err;
926 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000927
Chris Wilson432e58e2010-11-25 19:32:06 +0000928 if (!list_empty(&obj->exec_list)) {
929 DRM_ERROR("Object %p [handle %d, index %d] appears more than once in object list\n",
930 obj, exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000931 ret = -EINVAL;
932 goto err;
933 }
Chris Wilson432e58e2010-11-25 19:32:06 +0000934
935 list_add_tail(&obj->exec_list, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000936 }
937
938 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilsond9e86c02010-11-10 16:40:20 +0000939 ret = i915_gem_execbuffer_reserve(ring, file, &objects, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000940 if (ret)
941 goto err;
942
943 /* The objects are in their final locations, apply the relocations. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000944 ret = i915_gem_execbuffer_relocate(dev, file, &objects, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000945 if (ret) {
946 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +0000947 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000948 &objects, exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000949 args->buffer_count);
950 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
951 }
952 if (ret)
953 goto err;
954 }
955
956 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson432e58e2010-11-25 19:32:06 +0000957 batch_obj = list_entry(objects.prev,
958 struct drm_i915_gem_object,
959 exec_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000960 if (batch_obj->base.pending_write_domain) {
961 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
962 ret = -EINVAL;
963 goto err;
964 }
965 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
966
Chris Wilson432e58e2010-11-25 19:32:06 +0000967 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000968 if (ret)
969 goto err;
970
Chris Wilson432e58e2010-11-25 19:32:06 +0000971 ret = i915_gem_execbuffer_wait_for_flips(ring, &objects);
972 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000973 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000974
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000975 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
976 exec_len = args->batch_len;
977 if (cliprects) {
978 for (i = 0; i < args->num_cliprects; i++) {
979 ret = i915_emit_box(dev, &cliprects[i],
980 args->DR1, args->DR4);
981 if (ret)
982 goto err;
983
984 ret = ring->dispatch_execbuffer(ring,
985 exec_start, exec_len);
986 if (ret)
987 goto err;
988 }
989 } else {
990 ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
991 if (ret)
992 goto err;
993 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000994
Chris Wilson432e58e2010-11-25 19:32:06 +0000995 i915_gem_execbuffer_move_to_active(&objects, ring);
996 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000997
998err:
Chris Wilson432e58e2010-11-25 19:32:06 +0000999 while (!list_empty(&objects)) {
1000 struct drm_i915_gem_object *obj;
1001
1002 obj = list_first_entry(&objects,
1003 struct drm_i915_gem_object,
1004 exec_list);
1005 list_del_init(&obj->exec_list);
1006 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001007 }
1008
1009 mutex_unlock(&dev->struct_mutex);
1010
1011pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001012 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001013 return ret;
1014}
1015
1016/*
1017 * Legacy execbuffer just creates an exec2 list from the original exec object
1018 * list array and passes it to the real function.
1019 */
1020int
1021i915_gem_execbuffer(struct drm_device *dev, void *data,
1022 struct drm_file *file)
1023{
1024 struct drm_i915_gem_execbuffer *args = data;
1025 struct drm_i915_gem_execbuffer2 exec2;
1026 struct drm_i915_gem_exec_object *exec_list = NULL;
1027 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1028 int ret, i;
1029
1030#if WATCH_EXEC
1031 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
1032 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
1033#endif
1034
1035 if (args->buffer_count < 1) {
1036 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
1037 return -EINVAL;
1038 }
1039
1040 /* Copy in the exec list from userland */
1041 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1042 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1043 if (exec_list == NULL || exec2_list == NULL) {
1044 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
1045 args->buffer_count);
1046 drm_free_large(exec_list);
1047 drm_free_large(exec2_list);
1048 return -ENOMEM;
1049 }
1050 ret = copy_from_user(exec_list,
1051 (struct drm_i915_relocation_entry __user *)
1052 (uintptr_t) args->buffers_ptr,
1053 sizeof(*exec_list) * args->buffer_count);
1054 if (ret != 0) {
1055 DRM_ERROR("copy %d exec entries failed %d\n",
1056 args->buffer_count, ret);
1057 drm_free_large(exec_list);
1058 drm_free_large(exec2_list);
1059 return -EFAULT;
1060 }
1061
1062 for (i = 0; i < args->buffer_count; i++) {
1063 exec2_list[i].handle = exec_list[i].handle;
1064 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1065 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1066 exec2_list[i].alignment = exec_list[i].alignment;
1067 exec2_list[i].offset = exec_list[i].offset;
1068 if (INTEL_INFO(dev)->gen < 4)
1069 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1070 else
1071 exec2_list[i].flags = 0;
1072 }
1073
1074 exec2.buffers_ptr = args->buffers_ptr;
1075 exec2.buffer_count = args->buffer_count;
1076 exec2.batch_start_offset = args->batch_start_offset;
1077 exec2.batch_len = args->batch_len;
1078 exec2.DR1 = args->DR1;
1079 exec2.DR4 = args->DR4;
1080 exec2.num_cliprects = args->num_cliprects;
1081 exec2.cliprects_ptr = args->cliprects_ptr;
1082 exec2.flags = I915_EXEC_RENDER;
1083
1084 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1085 if (!ret) {
1086 /* Copy the new buffer offsets back to the user's exec list. */
1087 for (i = 0; i < args->buffer_count; i++)
1088 exec_list[i].offset = exec2_list[i].offset;
1089 /* ... and back out to userspace */
1090 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1091 (uintptr_t) args->buffers_ptr,
1092 exec_list,
1093 sizeof(*exec_list) * args->buffer_count);
1094 if (ret) {
1095 ret = -EFAULT;
1096 DRM_ERROR("failed to copy %d exec entries "
1097 "back to user (%d)\n",
1098 args->buffer_count, ret);
1099 }
1100 }
1101
1102 drm_free_large(exec_list);
1103 drm_free_large(exec2_list);
1104 return ret;
1105}
1106
1107int
1108i915_gem_execbuffer2(struct drm_device *dev, void *data,
1109 struct drm_file *file)
1110{
1111 struct drm_i915_gem_execbuffer2 *args = data;
1112 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1113 int ret;
1114
1115#if WATCH_EXEC
1116 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
1117 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
1118#endif
1119
1120 if (args->buffer_count < 1) {
1121 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
1122 return -EINVAL;
1123 }
1124
1125 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1126 if (exec2_list == NULL) {
1127 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
1128 args->buffer_count);
1129 return -ENOMEM;
1130 }
1131 ret = copy_from_user(exec2_list,
1132 (struct drm_i915_relocation_entry __user *)
1133 (uintptr_t) args->buffers_ptr,
1134 sizeof(*exec2_list) * args->buffer_count);
1135 if (ret != 0) {
1136 DRM_ERROR("copy %d exec entries failed %d\n",
1137 args->buffer_count, ret);
1138 drm_free_large(exec2_list);
1139 return -EFAULT;
1140 }
1141
1142 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1143 if (!ret) {
1144 /* Copy the new buffer offsets back to the user's exec list. */
1145 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1146 (uintptr_t) args->buffers_ptr,
1147 exec2_list,
1148 sizeof(*exec2_list) * args->buffer_count);
1149 if (ret) {
1150 ret = -EFAULT;
1151 DRM_ERROR("failed to copy %d exec entries "
1152 "back to user (%d)\n",
1153 args->buffer_count, ret);
1154 }
1155 }
1156
1157 drm_free_large(exec2_list);
1158 return ret;
1159}
1160