blob: 0e051eca36395bbff50c987c491c3aa199fc16a7 [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 Wilson54cf91d2010-11-25 18:00:26 +0000269static int
270i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000271 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000272 struct drm_i915_gem_relocation_entry *reloc)
273{
274 struct drm_device *dev = obj->base.dev;
275 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100276 struct drm_i915_gem_object *target_i915_obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000277 uint32_t target_offset;
278 int ret = -EINVAL;
279
Chris Wilson67731b82010-12-08 10:38:14 +0000280 /* we've already hold a reference to all valid objects */
281 target_obj = &eb_get_object(eb, reloc->target_handle)->base;
282 if (unlikely(target_obj == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000283 return -ENOENT;
284
Daniel Vetter149c8402012-02-15 23:50:23 +0100285 target_i915_obj = to_intel_bo(target_obj);
286 target_offset = target_i915_obj->gtt_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000287
Chris Wilson54cf91d2010-11-25 18:00:26 +0000288 /* The target buffer should have appeared before us in the
289 * exec_object list, so it should have a GTT space bound by now.
290 */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000291 if (unlikely(target_offset == 0)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100292 DRM_DEBUG("No GTT space found for object %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000293 reloc->target_handle);
Chris Wilson67731b82010-12-08 10:38:14 +0000294 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000295 }
296
297 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000298 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100299 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000300 "obj %p target %d offset %d "
301 "read %08x write %08x",
302 obj, reloc->target_handle,
303 (int) reloc->offset,
304 reloc->read_domains,
305 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000306 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000307 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100308 if (unlikely((reloc->write_domain | reloc->read_domains)
309 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100310 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000311 "obj %p target %d offset %d "
312 "read %08x write %08x",
313 obj, reloc->target_handle,
314 (int) reloc->offset,
315 reloc->read_domains,
316 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000317 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000318 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000319 if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
320 reloc->write_domain != target_obj->pending_write_domain)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100321 DRM_DEBUG("Write domain conflict: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000322 "obj %p target %d offset %d "
323 "new %08x old %08x\n",
324 obj, reloc->target_handle,
325 (int) reloc->offset,
326 reloc->write_domain,
327 target_obj->pending_write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000328 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000329 }
330
331 target_obj->pending_read_domains |= reloc->read_domains;
332 target_obj->pending_write_domain |= reloc->write_domain;
333
334 /* If the relocation already has the right value in it, no
335 * more work needs to be done.
336 */
337 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000338 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000339
340 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000341 if (unlikely(reloc->offset > obj->base.size - 4)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100342 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000343 "obj %p target %d offset %d size %d.\n",
344 obj, reloc->target_handle,
345 (int) reloc->offset,
346 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000347 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000348 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000349 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100350 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000351 "obj %p target %d offset %d.\n",
352 obj, reloc->target_handle,
353 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000354 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000355 }
356
Chris Wilson54cf91d2010-11-25 18:00:26 +0000357 reloc->delta += target_offset;
358 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
359 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
360 char *vaddr;
361
362 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
363 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
364 kunmap_atomic(vaddr);
365 } else {
366 struct drm_i915_private *dev_priv = dev->dev_private;
367 uint32_t __iomem *reloc_entry;
368 void __iomem *reloc_page;
369
Chris Wilsond4aeee72011-03-14 15:11:24 +0000370 /* We can't wait for rendering with pagefaults disabled */
371 if (obj->active && in_atomic())
372 return -EFAULT;
373
Chris Wilson54cf91d2010-11-25 18:00:26 +0000374 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
375 if (ret)
Chris Wilson67731b82010-12-08 10:38:14 +0000376 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000377
378 /* Map the page containing the relocation we're going to perform. */
379 reloc->offset += obj->gtt_offset;
380 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
381 reloc->offset & PAGE_MASK);
382 reloc_entry = (uint32_t __iomem *)
383 (reloc_page + (reloc->offset & ~PAGE_MASK));
384 iowrite32(reloc->delta, reloc_entry);
385 io_mapping_unmap_atomic(reloc_page);
386 }
387
Daniel Vetter149c8402012-02-15 23:50:23 +0100388 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
389 * pipe_control writes because the gpu doesn't properly redirect them
390 * through the ppgtt for non_secure batchbuffers. */
391 if (unlikely(IS_GEN6(dev) &&
392 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
393 !target_i915_obj->has_global_gtt_mapping)) {
394 i915_gem_gtt_bind_object(target_i915_obj,
395 target_i915_obj->cache_level);
396 }
397
Chris Wilson54cf91d2010-11-25 18:00:26 +0000398 /* and update the user's relocation entry */
399 reloc->presumed_offset = target_offset;
400
Chris Wilson67731b82010-12-08 10:38:14 +0000401 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000402}
403
404static int
405i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000406 struct eb_objects *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000407{
408 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000409 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000410 int i, ret;
411
412 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
413 for (i = 0; i < entry->relocation_count; i++) {
414 struct drm_i915_gem_relocation_entry reloc;
415
416 if (__copy_from_user_inatomic(&reloc,
417 user_relocs+i,
418 sizeof(reloc)))
419 return -EFAULT;
420
Chris Wilson6fe4f142011-01-10 17:35:37 +0000421 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &reloc);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000422 if (ret)
423 return ret;
424
425 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
426 &reloc.presumed_offset,
427 sizeof(reloc.presumed_offset)))
428 return -EFAULT;
429 }
430
431 return 0;
432}
433
434static int
435i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000436 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000437 struct drm_i915_gem_relocation_entry *relocs)
438{
Chris Wilson6fe4f142011-01-10 17:35:37 +0000439 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000440 int i, ret;
441
442 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000443 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000444 if (ret)
445 return ret;
446 }
447
448 return 0;
449}
450
451static int
452i915_gem_execbuffer_relocate(struct drm_device *dev,
Chris Wilson67731b82010-12-08 10:38:14 +0000453 struct eb_objects *eb,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000454 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000455{
Chris Wilson432e58e2010-11-25 19:32:06 +0000456 struct drm_i915_gem_object *obj;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000457 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000458
Chris Wilsond4aeee72011-03-14 15:11:24 +0000459 /* This is the fast path and we cannot handle a pagefault whilst
460 * holding the struct mutex lest the user pass in the relocations
461 * contained within a mmaped bo. For in such a case we, the page
462 * fault handler would call i915_gem_fault() and we would try to
463 * acquire the struct mutex again. Obviously this is bad and so
464 * lockdep complains vehemently.
465 */
466 pagefault_disable();
Chris Wilson432e58e2010-11-25 19:32:06 +0000467 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000468 ret = i915_gem_execbuffer_relocate_object(obj, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000469 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000470 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000471 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000472 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000473
Chris Wilsond4aeee72011-03-14 15:11:24 +0000474 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000475}
476
Chris Wilson1690e1e2011-12-14 13:57:08 +0100477#define __EXEC_OBJECT_HAS_FENCE (1<<31)
478
479static int
480pin_and_fence_object(struct drm_i915_gem_object *obj,
481 struct intel_ring_buffer *ring)
482{
483 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
484 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
485 bool need_fence, need_mappable;
486 int ret;
487
488 need_fence =
489 has_fenced_gpu_access &&
490 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
491 obj->tiling_mode != I915_TILING_NONE;
492 need_mappable =
493 entry->relocation_count ? true : need_fence;
494
495 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable);
496 if (ret)
497 return ret;
498
499 if (has_fenced_gpu_access) {
500 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
501 if (obj->tiling_mode) {
502 ret = i915_gem_object_get_fence(obj, ring);
503 if (ret)
504 goto err_unpin;
505
506 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
507 i915_gem_object_pin_fence(obj);
508 } else {
509 ret = i915_gem_object_put_fence(obj);
510 if (ret)
511 goto err_unpin;
512 }
513 }
514 obj->pending_fenced_gpu_access = need_fence;
515 }
516
517 entry->offset = obj->gtt_offset;
518 return 0;
519
520err_unpin:
521 i915_gem_object_unpin(obj);
522 return ret;
523}
524
Chris Wilson54cf91d2010-11-25 18:00:26 +0000525static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000526i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000527 struct drm_file *file,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000528 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000529{
Daniel Vetter7bddb012012-02-09 17:15:47 +0100530 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000531 struct drm_i915_gem_object *obj;
Chris Wilson432e58e2010-11-25 19:32:06 +0000532 int ret, retry;
Chris Wilson9b3826b2010-12-05 17:11:54 +0000533 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000534 struct list_head ordered_objects;
535
536 INIT_LIST_HEAD(&ordered_objects);
537 while (!list_empty(objects)) {
538 struct drm_i915_gem_exec_object2 *entry;
539 bool need_fence, need_mappable;
540
541 obj = list_first_entry(objects,
542 struct drm_i915_gem_object,
543 exec_list);
544 entry = obj->exec_entry;
545
546 need_fence =
547 has_fenced_gpu_access &&
548 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
549 obj->tiling_mode != I915_TILING_NONE;
550 need_mappable =
551 entry->relocation_count ? true : need_fence;
552
553 if (need_mappable)
554 list_move(&obj->exec_list, &ordered_objects);
555 else
556 list_move_tail(&obj->exec_list, &ordered_objects);
Chris Wilson595dad72011-01-13 11:03:48 +0000557
558 obj->base.pending_read_domains = 0;
559 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000560 }
561 list_splice(&ordered_objects, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000562
563 /* Attempt to pin all of the buffers into the GTT.
564 * This is done in 3 phases:
565 *
566 * 1a. Unbind all objects that do not match the GTT constraints for
567 * the execbuffer (fenceable, mappable, alignment etc).
568 * 1b. Increment pin count for already bound objects.
569 * 2. Bind new objects.
570 * 3. Decrement pin count.
571 *
572 * This avoid unnecessary unbinding of later objects in order to makr
573 * room for the earlier objects *unless* we need to defragment.
574 */
575 retry = 0;
576 do {
577 ret = 0;
578
579 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000580 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000581 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000582 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100583
Chris Wilson6fe4f142011-01-10 17:35:37 +0000584 if (!obj->gtt_space)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000585 continue;
586
587 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000588 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000589 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
590 obj->tiling_mode != I915_TILING_NONE;
591 need_mappable =
592 entry->relocation_count ? true : need_fence;
593
594 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
595 (need_mappable && !obj->map_and_fenceable))
596 ret = i915_gem_object_unbind(obj);
597 else
Chris Wilson1690e1e2011-12-14 13:57:08 +0100598 ret = pin_and_fence_object(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000599 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000600 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000601 }
602
603 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000604 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100605 if (obj->gtt_space)
606 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000607
Chris Wilson1690e1e2011-12-14 13:57:08 +0100608 ret = pin_and_fence_object(obj, ring);
609 if (ret) {
610 int ret_ignore;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000611
Chris Wilson1690e1e2011-12-14 13:57:08 +0100612 /* This can potentially raise a harmless
613 * -EINVAL if we failed to bind in the above
614 * call. It cannot raise -EINTR since we know
615 * that the bo is freshly bound and so will
616 * not need to be flushed or waited upon.
617 */
618 ret_ignore = i915_gem_object_unbind(obj);
619 (void)ret_ignore;
620 WARN_ON(obj->gtt_space);
621 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000622 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000623 }
624
Chris Wilson432e58e2010-11-25 19:32:06 +0000625 /* Decrement pin count for bound objects */
626 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100627 struct drm_i915_gem_exec_object2 *entry;
628
629 if (!obj->gtt_space)
630 continue;
631
632 entry = obj->exec_entry;
633 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
634 i915_gem_object_unpin_fence(obj);
635 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
636 }
637
638 i915_gem_object_unpin(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100639
640 /* ... and ensure ppgtt mapping exist if needed. */
641 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
642 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
643 obj, obj->cache_level);
644
645 obj->has_aliasing_ppgtt_mapping = 1;
646 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000647 }
648
649 if (ret != -ENOSPC || retry > 1)
650 return ret;
651
652 /* First attempt, just clear anything that is purgeable.
653 * Second attempt, clear the entire GTT.
654 */
Chris Wilsond9e86c02010-11-10 16:40:20 +0000655 ret = i915_gem_evict_everything(ring->dev, retry == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000656 if (ret)
657 return ret;
658
659 retry++;
660 } while (1);
Chris Wilson432e58e2010-11-25 19:32:06 +0000661
662err:
Chris Wilson1690e1e2011-12-14 13:57:08 +0100663 list_for_each_entry_continue_reverse(obj, objects, exec_list) {
664 struct drm_i915_gem_exec_object2 *entry;
Chris Wilson432e58e2010-11-25 19:32:06 +0000665
Chris Wilson1690e1e2011-12-14 13:57:08 +0100666 if (!obj->gtt_space)
667 continue;
668
669 entry = obj->exec_entry;
670 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
671 i915_gem_object_unpin_fence(obj);
672 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
673 }
674
675 i915_gem_object_unpin(obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000676 }
677
678 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000679}
680
681static int
682i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
683 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000684 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000685 struct list_head *objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000686 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000687 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000688 int count)
689{
690 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000691 struct drm_i915_gem_object *obj;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000692 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000693 int i, total, ret;
694
Chris Wilson67731b82010-12-08 10:38:14 +0000695 /* We may process another execbuffer during the unlock... */
Chris Wilson36cf1742011-01-10 12:09:12 +0000696 while (!list_empty(objects)) {
Chris Wilson67731b82010-12-08 10:38:14 +0000697 obj = list_first_entry(objects,
698 struct drm_i915_gem_object,
699 exec_list);
700 list_del_init(&obj->exec_list);
701 drm_gem_object_unreference(&obj->base);
702 }
703
Chris Wilson54cf91d2010-11-25 18:00:26 +0000704 mutex_unlock(&dev->struct_mutex);
705
706 total = 0;
707 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000708 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000709
Chris Wilsondd6864a2011-01-12 23:49:13 +0000710 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000711 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000712 if (reloc == NULL || reloc_offset == NULL) {
713 drm_free_large(reloc);
714 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000715 mutex_lock(&dev->struct_mutex);
716 return -ENOMEM;
717 }
718
719 total = 0;
720 for (i = 0; i < count; i++) {
721 struct drm_i915_gem_relocation_entry __user *user_relocs;
722
Chris Wilson432e58e2010-11-25 19:32:06 +0000723 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000724
725 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000726 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000727 ret = -EFAULT;
728 mutex_lock(&dev->struct_mutex);
729 goto err;
730 }
731
Chris Wilsondd6864a2011-01-12 23:49:13 +0000732 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000733 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000734 }
735
736 ret = i915_mutex_lock_interruptible(dev);
737 if (ret) {
738 mutex_lock(&dev->struct_mutex);
739 goto err;
740 }
741
Chris Wilson67731b82010-12-08 10:38:14 +0000742 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000743 eb_reset(eb);
744 for (i = 0; i < count; i++) {
Chris Wilson67731b82010-12-08 10:38:14 +0000745 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
746 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000747 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100748 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson67731b82010-12-08 10:38:14 +0000749 exec[i].handle, i);
750 ret = -ENOENT;
751 goto err;
752 }
753
754 list_add_tail(&obj->exec_list, objects);
755 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000756 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000757 eb_add_object(eb, obj);
758 }
759
Chris Wilson6fe4f142011-01-10 17:35:37 +0000760 ret = i915_gem_execbuffer_reserve(ring, file, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000761 if (ret)
762 goto err;
763
Chris Wilson432e58e2010-11-25 19:32:06 +0000764 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondd6864a2011-01-12 23:49:13 +0000765 int offset = obj->exec_entry - exec;
Chris Wilson67731b82010-12-08 10:38:14 +0000766 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilsondd6864a2011-01-12 23:49:13 +0000767 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000768 if (ret)
769 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000770 }
771
772 /* Leave the user relocations as are, this is the painfully slow path,
773 * and we want to avoid the complication of dropping the lock whilst
774 * having buffers reserved in the aperture and so causing spurious
775 * ENOSPC for random operations.
776 */
777
778err:
779 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000780 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000781 return ret;
782}
783
Chris Wilson88241782011-01-07 17:09:48 +0000784static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000785i915_gem_execbuffer_flush(struct drm_device *dev,
786 uint32_t invalidate_domains,
787 uint32_t flush_domains,
788 uint32_t flush_rings)
789{
790 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson88241782011-01-07 17:09:48 +0000791 int i, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000792
793 if (flush_domains & I915_GEM_DOMAIN_CPU)
794 intel_gtt_chipset_flush();
795
Chris Wilson63256ec2011-01-04 18:42:07 +0000796 if (flush_domains & I915_GEM_DOMAIN_GTT)
797 wmb();
798
Chris Wilson54cf91d2010-11-25 18:00:26 +0000799 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000800 for (i = 0; i < I915_NUM_RINGS; i++)
Chris Wilson88241782011-01-07 17:09:48 +0000801 if (flush_rings & (1 << i)) {
Chris Wilsondb53a302011-02-03 11:57:46 +0000802 ret = i915_gem_flush_ring(&dev_priv->ring[i],
Chris Wilson88241782011-01-07 17:09:48 +0000803 invalidate_domains,
804 flush_domains);
805 if (ret)
806 return ret;
807 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000808 }
Chris Wilson88241782011-01-07 17:09:48 +0000809
810 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000811}
812
Eugeni Dodonovf45b5552011-12-09 17:16:37 -0800813static bool
814intel_enable_semaphores(struct drm_device *dev)
815{
816 if (INTEL_INFO(dev)->gen < 6)
817 return 0;
818
819 if (i915_semaphores >= 0)
820 return i915_semaphores;
821
Keith Packardebbd8572011-12-26 17:02:10 -0800822 /* Disable semaphores on SNB */
Eugeni Dodonovf45b5552011-12-09 17:16:37 -0800823 if (INTEL_INFO(dev)->gen == 6)
Keith Packardebbd8572011-12-26 17:02:10 -0800824 return 0;
Eugeni Dodonovf45b5552011-12-09 17:16:37 -0800825
826 return 1;
827}
828
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000829static int
830i915_gem_execbuffer_sync_rings(struct drm_i915_gem_object *obj,
831 struct intel_ring_buffer *to)
832{
833 struct intel_ring_buffer *from = obj->ring;
834 u32 seqno;
835 int ret, idx;
836
837 if (from == NULL || to == from)
838 return 0;
839
Chris Wilsona1656b92011-03-04 18:48:03 +0000840 /* XXX gpu semaphores are implicated in various hard hangs on SNB */
Eugeni Dodonovf45b5552011-12-09 17:16:37 -0800841 if (!intel_enable_semaphores(obj->base.dev))
Chris Wilsonce453d82011-02-21 14:43:56 +0000842 return i915_gem_object_wait_rendering(obj);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000843
844 idx = intel_ring_sync_index(from, to);
845
846 seqno = obj->last_rendering_seqno;
847 if (seqno <= from->sync_seqno[idx])
848 return 0;
849
850 if (seqno == from->outstanding_lazy_request) {
851 struct drm_i915_gem_request *request;
852
853 request = kzalloc(sizeof(*request), GFP_KERNEL);
854 if (request == NULL)
855 return -ENOMEM;
856
Chris Wilsondb53a302011-02-03 11:57:46 +0000857 ret = i915_add_request(from, NULL, request);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000858 if (ret) {
859 kfree(request);
860 return ret;
861 }
862
863 seqno = request->seqno;
864 }
865
866 from->sync_seqno[idx] = seqno;
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700867
868 return to->sync_to(to, from, seqno - 1);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000869}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000870
871static int
Chris Wilsonc59a3332011-03-06 13:51:29 +0000872i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
873{
874 u32 plane, flip_mask;
875 int ret;
876
877 /* Check for any pending flips. As we only maintain a flip queue depth
878 * of 1, we can simply insert a WAIT for the next display flip prior
879 * to executing the batch and avoid stalling the CPU.
880 */
881
882 for (plane = 0; flips >> plane; plane++) {
883 if (((flips >> plane) & 1) == 0)
884 continue;
885
886 if (plane)
887 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
888 else
889 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
890
891 ret = intel_ring_begin(ring, 2);
892 if (ret)
893 return ret;
894
895 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
896 intel_ring_emit(ring, MI_NOOP);
897 intel_ring_advance(ring);
898 }
899
900 return 0;
901}
902
903
904static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000905i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
906 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000907{
Chris Wilson432e58e2010-11-25 19:32:06 +0000908 struct drm_i915_gem_object *obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000909 struct change_domains cd;
Chris Wilson432e58e2010-11-25 19:32:06 +0000910 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000911
Chris Wilsonc59a3332011-03-06 13:51:29 +0000912 memset(&cd, 0, sizeof(cd));
Chris Wilson432e58e2010-11-25 19:32:06 +0000913 list_for_each_entry(obj, objects, exec_list)
914 i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000915
916 if (cd.invalidate_domains | cd.flush_domains) {
Chris Wilson88241782011-01-07 17:09:48 +0000917 ret = i915_gem_execbuffer_flush(ring->dev,
918 cd.invalidate_domains,
919 cd.flush_domains,
920 cd.flush_rings);
921 if (ret)
922 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000923 }
924
Chris Wilsonc59a3332011-03-06 13:51:29 +0000925 if (cd.flips) {
926 ret = i915_gem_execbuffer_wait_for_flips(ring, cd.flips);
927 if (ret)
928 return ret;
929 }
930
Chris Wilson432e58e2010-11-25 19:32:06 +0000931 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000932 ret = i915_gem_execbuffer_sync_rings(obj, ring);
933 if (ret)
934 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000935 }
936
937 return 0;
938}
939
Chris Wilson432e58e2010-11-25 19:32:06 +0000940static bool
941i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000942{
Chris Wilson432e58e2010-11-25 19:32:06 +0000943 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000944}
945
946static int
947validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
948 int count)
949{
950 int i;
951
952 for (i = 0; i < count; i++) {
953 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
954 int length; /* limited by fault_in_pages_readable() */
955
956 /* First check for malicious input causing overflow */
957 if (exec[i].relocation_count >
958 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
959 return -EINVAL;
960
961 length = exec[i].relocation_count *
962 sizeof(struct drm_i915_gem_relocation_entry);
963 if (!access_ok(VERIFY_READ, ptr, length))
964 return -EFAULT;
965
966 /* we may also need to update the presumed offsets */
967 if (!access_ok(VERIFY_WRITE, ptr, length))
968 return -EFAULT;
969
970 if (fault_in_pages_readable(ptr, length))
971 return -EFAULT;
972 }
973
974 return 0;
975}
976
Chris Wilson432e58e2010-11-25 19:32:06 +0000977static void
978i915_gem_execbuffer_move_to_active(struct list_head *objects,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000979 struct intel_ring_buffer *ring,
980 u32 seqno)
Chris Wilson432e58e2010-11-25 19:32:06 +0000981{
982 struct drm_i915_gem_object *obj;
983
984 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondb53a302011-02-03 11:57:46 +0000985 u32 old_read = obj->base.read_domains;
986 u32 old_write = obj->base.write_domain;
987
988
Chris Wilson432e58e2010-11-25 19:32:06 +0000989 obj->base.read_domains = obj->base.pending_read_domains;
990 obj->base.write_domain = obj->base.pending_write_domain;
991 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
992
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000993 i915_gem_object_move_to_active(obj, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +0000994 if (obj->base.write_domain) {
995 obj->dirty = 1;
Chris Wilson87ca9c82010-12-02 09:42:56 +0000996 obj->pending_gpu_write = true;
Chris Wilson432e58e2010-11-25 19:32:06 +0000997 list_move_tail(&obj->gpu_write_list,
998 &ring->gpu_write_list);
999 intel_mark_busy(ring->dev, obj);
1000 }
1001
Chris Wilsondb53a302011-02-03 11:57:46 +00001002 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +00001003 }
1004}
1005
Chris Wilson54cf91d2010-11-25 18:00:26 +00001006static void
1007i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +00001008 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001009 struct intel_ring_buffer *ring)
1010{
Chris Wilson432e58e2010-11-25 19:32:06 +00001011 struct drm_i915_gem_request *request;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001012 u32 invalidate;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001013
Chris Wilson432e58e2010-11-25 19:32:06 +00001014 /*
1015 * Ensure that the commands in the batch buffer are
1016 * finished before the interrupt fires.
1017 *
1018 * The sampler always gets flushed on i965 (sigh).
1019 */
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001020 invalidate = I915_GEM_DOMAIN_COMMAND;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001021 if (INTEL_INFO(dev)->gen >= 4)
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001022 invalidate |= I915_GEM_DOMAIN_SAMPLER;
1023 if (ring->flush(ring, invalidate, 0)) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001024 i915_gem_next_request_seqno(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001025 return;
1026 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001027
Chris Wilson432e58e2010-11-25 19:32:06 +00001028 /* Add a breadcrumb for the completion of the batch buffer */
1029 request = kzalloc(sizeof(*request), GFP_KERNEL);
Chris Wilsondb53a302011-02-03 11:57:46 +00001030 if (request == NULL || i915_add_request(ring, file, request)) {
1031 i915_gem_next_request_seqno(ring);
Chris Wilson432e58e2010-11-25 19:32:06 +00001032 kfree(request);
1033 }
1034}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001035
1036static int
Eric Anholtae662d32012-01-03 09:23:29 -08001037i915_reset_gen7_sol_offsets(struct drm_device *dev,
1038 struct intel_ring_buffer *ring)
1039{
1040 drm_i915_private_t *dev_priv = dev->dev_private;
1041 int ret, i;
1042
1043 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
1044 return 0;
1045
1046 ret = intel_ring_begin(ring, 4 * 3);
1047 if (ret)
1048 return ret;
1049
1050 for (i = 0; i < 4; i++) {
1051 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1052 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
1053 intel_ring_emit(ring, 0);
1054 }
1055
1056 intel_ring_advance(ring);
1057
1058 return 0;
1059}
1060
1061static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001062i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1063 struct drm_file *file,
1064 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +00001065 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001066{
1067 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +00001068 struct list_head objects;
Chris Wilson67731b82010-12-08 10:38:14 +00001069 struct eb_objects *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001070 struct drm_i915_gem_object *batch_obj;
1071 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001072 struct intel_ring_buffer *ring;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001073 u32 exec_start, exec_len;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001074 u32 seqno;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001075 u32 mask;
Chris Wilson72bfa192010-12-19 11:42:05 +00001076 int ret, mode, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001077
Chris Wilson432e58e2010-11-25 19:32:06 +00001078 if (!i915_gem_check_execbuffer(args)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001079 DRM_DEBUG("execbuf with invalid offset/length\n");
Chris Wilson432e58e2010-11-25 19:32:06 +00001080 return -EINVAL;
1081 }
1082
1083 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001084 if (ret)
1085 return ret;
1086
Chris Wilson54cf91d2010-11-25 18:00:26 +00001087 switch (args->flags & I915_EXEC_RING_MASK) {
1088 case I915_EXEC_DEFAULT:
1089 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001090 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +00001091 break;
1092 case I915_EXEC_BSD:
1093 if (!HAS_BSD(dev)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001094 DRM_DEBUG("execbuf with invalid ring (BSD)\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001095 return -EINVAL;
1096 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001097 ring = &dev_priv->ring[VCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +00001098 break;
1099 case I915_EXEC_BLT:
1100 if (!HAS_BLT(dev)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001101 DRM_DEBUG("execbuf with invalid ring (BLT)\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001102 return -EINVAL;
1103 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001104 ring = &dev_priv->ring[BCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +00001105 break;
1106 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001107 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001108 (int)(args->flags & I915_EXEC_RING_MASK));
1109 return -EINVAL;
1110 }
1111
Chris Wilson72bfa192010-12-19 11:42:05 +00001112 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001113 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +00001114 switch (mode) {
1115 case I915_EXEC_CONSTANTS_REL_GENERAL:
1116 case I915_EXEC_CONSTANTS_ABSOLUTE:
1117 case I915_EXEC_CONSTANTS_REL_SURFACE:
1118 if (ring == &dev_priv->ring[RCS] &&
1119 mode != dev_priv->relative_constants_mode) {
1120 if (INTEL_INFO(dev)->gen < 4)
1121 return -EINVAL;
1122
1123 if (INTEL_INFO(dev)->gen > 5 &&
1124 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
1125 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -08001126
1127 /* The HW changed the meaning on this bit on gen6 */
1128 if (INTEL_INFO(dev)->gen >= 6)
1129 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +00001130 }
1131 break;
1132 default:
Daniel Vetterff240192012-01-31 21:08:14 +01001133 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +00001134 return -EINVAL;
1135 }
1136
Chris Wilson54cf91d2010-11-25 18:00:26 +00001137 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001138 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001139 return -EINVAL;
1140 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001141
1142 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001143 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +01001144 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001145 return -EINVAL;
1146 }
1147
Chris Wilson432e58e2010-11-25 19:32:06 +00001148 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001149 GFP_KERNEL);
1150 if (cliprects == NULL) {
1151 ret = -ENOMEM;
1152 goto pre_mutex_err;
1153 }
1154
Chris Wilson432e58e2010-11-25 19:32:06 +00001155 if (copy_from_user(cliprects,
1156 (struct drm_clip_rect __user *)(uintptr_t)
1157 args->cliprects_ptr,
1158 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +00001159 ret = -EFAULT;
1160 goto pre_mutex_err;
1161 }
1162 }
1163
Chris Wilson54cf91d2010-11-25 18:00:26 +00001164 ret = i915_mutex_lock_interruptible(dev);
1165 if (ret)
1166 goto pre_mutex_err;
1167
1168 if (dev_priv->mm.suspended) {
1169 mutex_unlock(&dev->struct_mutex);
1170 ret = -EBUSY;
1171 goto pre_mutex_err;
1172 }
1173
Chris Wilson67731b82010-12-08 10:38:14 +00001174 eb = eb_create(args->buffer_count);
1175 if (eb == NULL) {
1176 mutex_unlock(&dev->struct_mutex);
1177 ret = -ENOMEM;
1178 goto pre_mutex_err;
1179 }
1180
Chris Wilson54cf91d2010-11-25 18:00:26 +00001181 /* Look up object handles */
Chris Wilson432e58e2010-11-25 19:32:06 +00001182 INIT_LIST_HEAD(&objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001183 for (i = 0; i < args->buffer_count; i++) {
1184 struct drm_i915_gem_object *obj;
1185
Chris Wilson432e58e2010-11-25 19:32:06 +00001186 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
1187 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001188 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001189 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson432e58e2010-11-25 19:32:06 +00001190 exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001191 /* prevent error path from reading uninitialized data */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001192 ret = -ENOENT;
1193 goto err;
1194 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001195
Chris Wilson432e58e2010-11-25 19:32:06 +00001196 if (!list_empty(&obj->exec_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001197 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
Chris Wilson432e58e2010-11-25 19:32:06 +00001198 obj, exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001199 ret = -EINVAL;
1200 goto err;
1201 }
Chris Wilson432e58e2010-11-25 19:32:06 +00001202
1203 list_add_tail(&obj->exec_list, &objects);
Chris Wilson67731b82010-12-08 10:38:14 +00001204 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +00001205 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +00001206 eb_add_object(eb, obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001207 }
1208
Chris Wilson6fe4f142011-01-10 17:35:37 +00001209 /* take note of the batch buffer before we might reorder the lists */
1210 batch_obj = list_entry(objects.prev,
1211 struct drm_i915_gem_object,
1212 exec_list);
1213
Chris Wilson54cf91d2010-11-25 18:00:26 +00001214 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilson6fe4f142011-01-10 17:35:37 +00001215 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001216 if (ret)
1217 goto err;
1218
1219 /* The objects are in their final locations, apply the relocations. */
Chris Wilson6fe4f142011-01-10 17:35:37 +00001220 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001221 if (ret) {
1222 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00001223 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilson67731b82010-12-08 10:38:14 +00001224 &objects, eb,
1225 exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001226 args->buffer_count);
1227 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1228 }
1229 if (ret)
1230 goto err;
1231 }
1232
1233 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001234 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001235 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001236 ret = -EINVAL;
1237 goto err;
1238 }
1239 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1240
Chris Wilson432e58e2010-11-25 19:32:06 +00001241 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001242 if (ret)
1243 goto err;
1244
Chris Wilsondb53a302011-02-03 11:57:46 +00001245 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson076e2c02011-01-21 10:07:18 +00001246 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001247 if (seqno < ring->sync_seqno[i]) {
1248 /* The GPU can not handle its semaphore value wrapping,
1249 * so every billion or so execbuffers, we need to stall
1250 * the GPU in order to reset the counters.
1251 */
Ben Widawskyb93f9cf2012-01-25 15:39:34 -08001252 ret = i915_gpu_idle(dev, true);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001253 if (ret)
1254 goto err;
1255
1256 BUG_ON(ring->sync_seqno[i]);
1257 }
1258 }
1259
Ben Widawskye2971bd2011-12-12 19:21:57 -08001260 if (ring == &dev_priv->ring[RCS] &&
1261 mode != dev_priv->relative_constants_mode) {
1262 ret = intel_ring_begin(ring, 4);
1263 if (ret)
1264 goto err;
1265
1266 intel_ring_emit(ring, MI_NOOP);
1267 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1268 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001269 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001270 intel_ring_advance(ring);
1271
1272 dev_priv->relative_constants_mode = mode;
1273 }
1274
Eric Anholtae662d32012-01-03 09:23:29 -08001275 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1276 ret = i915_reset_gen7_sol_offsets(dev, ring);
1277 if (ret)
1278 goto err;
1279 }
1280
Chris Wilsondb53a302011-02-03 11:57:46 +00001281 trace_i915_gem_ring_dispatch(ring, seqno);
1282
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001283 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1284 exec_len = args->batch_len;
1285 if (cliprects) {
1286 for (i = 0; i < args->num_cliprects; i++) {
1287 ret = i915_emit_box(dev, &cliprects[i],
1288 args->DR1, args->DR4);
1289 if (ret)
1290 goto err;
1291
1292 ret = ring->dispatch_execbuffer(ring,
1293 exec_start, exec_len);
1294 if (ret)
1295 goto err;
1296 }
1297 } else {
1298 ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
1299 if (ret)
1300 goto err;
1301 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001302
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001303 i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +00001304 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001305
1306err:
Chris Wilson67731b82010-12-08 10:38:14 +00001307 eb_destroy(eb);
Chris Wilson432e58e2010-11-25 19:32:06 +00001308 while (!list_empty(&objects)) {
1309 struct drm_i915_gem_object *obj;
1310
1311 obj = list_first_entry(&objects,
1312 struct drm_i915_gem_object,
1313 exec_list);
1314 list_del_init(&obj->exec_list);
1315 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001316 }
1317
1318 mutex_unlock(&dev->struct_mutex);
1319
1320pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001321 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001322 return ret;
1323}
1324
1325/*
1326 * Legacy execbuffer just creates an exec2 list from the original exec object
1327 * list array and passes it to the real function.
1328 */
1329int
1330i915_gem_execbuffer(struct drm_device *dev, void *data,
1331 struct drm_file *file)
1332{
1333 struct drm_i915_gem_execbuffer *args = data;
1334 struct drm_i915_gem_execbuffer2 exec2;
1335 struct drm_i915_gem_exec_object *exec_list = NULL;
1336 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1337 int ret, i;
1338
Chris Wilson54cf91d2010-11-25 18:00:26 +00001339 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001340 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001341 return -EINVAL;
1342 }
1343
1344 /* Copy in the exec list from userland */
1345 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1346 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1347 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001348 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001349 args->buffer_count);
1350 drm_free_large(exec_list);
1351 drm_free_large(exec2_list);
1352 return -ENOMEM;
1353 }
1354 ret = copy_from_user(exec_list,
1355 (struct drm_i915_relocation_entry __user *)
1356 (uintptr_t) args->buffers_ptr,
1357 sizeof(*exec_list) * args->buffer_count);
1358 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001359 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001360 args->buffer_count, ret);
1361 drm_free_large(exec_list);
1362 drm_free_large(exec2_list);
1363 return -EFAULT;
1364 }
1365
1366 for (i = 0; i < args->buffer_count; i++) {
1367 exec2_list[i].handle = exec_list[i].handle;
1368 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1369 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1370 exec2_list[i].alignment = exec_list[i].alignment;
1371 exec2_list[i].offset = exec_list[i].offset;
1372 if (INTEL_INFO(dev)->gen < 4)
1373 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1374 else
1375 exec2_list[i].flags = 0;
1376 }
1377
1378 exec2.buffers_ptr = args->buffers_ptr;
1379 exec2.buffer_count = args->buffer_count;
1380 exec2.batch_start_offset = args->batch_start_offset;
1381 exec2.batch_len = args->batch_len;
1382 exec2.DR1 = args->DR1;
1383 exec2.DR4 = args->DR4;
1384 exec2.num_cliprects = args->num_cliprects;
1385 exec2.cliprects_ptr = args->cliprects_ptr;
1386 exec2.flags = I915_EXEC_RENDER;
1387
1388 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1389 if (!ret) {
1390 /* Copy the new buffer offsets back to the user's exec list. */
1391 for (i = 0; i < args->buffer_count; i++)
1392 exec_list[i].offset = exec2_list[i].offset;
1393 /* ... and back out to userspace */
1394 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1395 (uintptr_t) args->buffers_ptr,
1396 exec_list,
1397 sizeof(*exec_list) * args->buffer_count);
1398 if (ret) {
1399 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001400 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001401 "back to user (%d)\n",
1402 args->buffer_count, ret);
1403 }
1404 }
1405
1406 drm_free_large(exec_list);
1407 drm_free_large(exec2_list);
1408 return ret;
1409}
1410
1411int
1412i915_gem_execbuffer2(struct drm_device *dev, void *data,
1413 struct drm_file *file)
1414{
1415 struct drm_i915_gem_execbuffer2 *args = data;
1416 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1417 int ret;
1418
Chris Wilson54cf91d2010-11-25 18:00:26 +00001419 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001420 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001421 return -EINVAL;
1422 }
1423
Chris Wilson8408c282011-02-21 12:54:48 +00001424 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1425 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1426 if (exec2_list == NULL)
1427 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1428 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001429 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001430 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001431 args->buffer_count);
1432 return -ENOMEM;
1433 }
1434 ret = copy_from_user(exec2_list,
1435 (struct drm_i915_relocation_entry __user *)
1436 (uintptr_t) args->buffers_ptr,
1437 sizeof(*exec2_list) * args->buffer_count);
1438 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001439 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001440 args->buffer_count, ret);
1441 drm_free_large(exec2_list);
1442 return -EFAULT;
1443 }
1444
1445 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1446 if (!ret) {
1447 /* Copy the new buffer offsets back to the user's exec list. */
1448 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1449 (uintptr_t) args->buffers_ptr,
1450 exec2_list,
1451 sizeof(*exec2_list) * args->buffer_count);
1452 if (ret) {
1453 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001454 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001455 "back to user (%d)\n",
1456 args->buffer_count, ret);
1457 }
1458 }
1459
1460 drm_free_large(exec2_list);
1461 return ret;
1462}