blob: 55a94c1a1f5930ebc013a1da9c96a8218f7f6b7a [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
Chris Wilson67731b82010-12-08 10:38:14 +000037struct eb_objects {
38 int and;
39 struct hlist_head buckets[0];
40};
41
42static struct eb_objects *
43eb_create(int size)
44{
45 struct eb_objects *eb;
46 int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
47 while (count > size)
48 count >>= 1;
49 eb = kzalloc(count*sizeof(struct hlist_head) +
50 sizeof(struct eb_objects),
51 GFP_KERNEL);
52 if (eb == NULL)
53 return eb;
54
55 eb->and = count - 1;
56 return eb;
57}
58
59static void
60eb_reset(struct eb_objects *eb)
61{
62 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
63}
64
65static void
66eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj)
67{
68 hlist_add_head(&obj->exec_node,
69 &eb->buckets[obj->exec_handle & eb->and]);
70}
71
72static struct drm_i915_gem_object *
73eb_get_object(struct eb_objects *eb, unsigned long handle)
74{
75 struct hlist_head *head;
76 struct hlist_node *node;
77 struct drm_i915_gem_object *obj;
78
79 head = &eb->buckets[handle & eb->and];
80 hlist_for_each(node, head) {
81 obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
82 if (obj->exec_handle == handle)
83 return obj;
84 }
85
86 return NULL;
87}
88
89static void
90eb_destroy(struct eb_objects *eb)
91{
92 kfree(eb);
93}
94
Chris Wilsondabdfe02012-03-26 10:10:27 +020095static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
96{
97 return (obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
98 obj->cache_level != I915_CACHE_NONE);
99}
100
Chris Wilson54cf91d2010-11-25 18:00:26 +0000101static int
102i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000103 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000104 struct drm_i915_gem_relocation_entry *reloc)
105{
106 struct drm_device *dev = obj->base.dev;
107 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100108 struct drm_i915_gem_object *target_i915_obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000109 uint32_t target_offset;
110 int ret = -EINVAL;
111
Chris Wilson67731b82010-12-08 10:38:14 +0000112 /* we've already hold a reference to all valid objects */
113 target_obj = &eb_get_object(eb, reloc->target_handle)->base;
114 if (unlikely(target_obj == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000115 return -ENOENT;
116
Daniel Vetter149c8402012-02-15 23:50:23 +0100117 target_i915_obj = to_intel_bo(target_obj);
118 target_offset = target_i915_obj->gtt_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000119
Chris Wilson54cf91d2010-11-25 18:00:26 +0000120 /* The target buffer should have appeared before us in the
121 * exec_object list, so it should have a GTT space bound by now.
122 */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000123 if (unlikely(target_offset == 0)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100124 DRM_DEBUG("No GTT space found for object %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000125 reloc->target_handle);
Chris Wilson67731b82010-12-08 10:38:14 +0000126 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000127 }
128
129 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000130 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100131 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000132 "obj %p target %d offset %d "
133 "read %08x write %08x",
134 obj, reloc->target_handle,
135 (int) reloc->offset,
136 reloc->read_domains,
137 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000138 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000139 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100140 if (unlikely((reloc->write_domain | reloc->read_domains)
141 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100142 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000143 "obj %p target %d offset %d "
144 "read %08x write %08x",
145 obj, reloc->target_handle,
146 (int) reloc->offset,
147 reloc->read_domains,
148 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000149 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000150 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000151 if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
152 reloc->write_domain != target_obj->pending_write_domain)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100153 DRM_DEBUG("Write domain conflict: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000154 "obj %p target %d offset %d "
155 "new %08x old %08x\n",
156 obj, reloc->target_handle,
157 (int) reloc->offset,
158 reloc->write_domain,
159 target_obj->pending_write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000160 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000161 }
162
163 target_obj->pending_read_domains |= reloc->read_domains;
164 target_obj->pending_write_domain |= reloc->write_domain;
165
166 /* If the relocation already has the right value in it, no
167 * more work needs to be done.
168 */
169 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000170 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000171
172 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000173 if (unlikely(reloc->offset > obj->base.size - 4)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100174 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000175 "obj %p target %d offset %d size %d.\n",
176 obj, reloc->target_handle,
177 (int) reloc->offset,
178 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000179 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000180 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000181 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100182 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000183 "obj %p target %d offset %d.\n",
184 obj, reloc->target_handle,
185 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000186 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000187 }
188
Chris Wilsondabdfe02012-03-26 10:10:27 +0200189 /* We can't wait for rendering with pagefaults disabled */
190 if (obj->active && in_atomic())
191 return -EFAULT;
192
Chris Wilson54cf91d2010-11-25 18:00:26 +0000193 reloc->delta += target_offset;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200194 if (use_cpu_reloc(obj)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000195 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
196 char *vaddr;
197
Chris Wilsondabdfe02012-03-26 10:10:27 +0200198 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
199 if (ret)
200 return ret;
201
Chris Wilson54cf91d2010-11-25 18:00:26 +0000202 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
203 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
204 kunmap_atomic(vaddr);
205 } else {
206 struct drm_i915_private *dev_priv = dev->dev_private;
207 uint32_t __iomem *reloc_entry;
208 void __iomem *reloc_page;
209
Chris Wilson7b096382012-04-14 09:55:51 +0100210 ret = i915_gem_object_set_to_gtt_domain(obj, true);
211 if (ret)
212 return ret;
213
214 ret = i915_gem_object_put_fence(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000215 if (ret)
Chris Wilson67731b82010-12-08 10:38:14 +0000216 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000217
218 /* Map the page containing the relocation we're going to perform. */
219 reloc->offset += obj->gtt_offset;
220 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
221 reloc->offset & PAGE_MASK);
222 reloc_entry = (uint32_t __iomem *)
223 (reloc_page + (reloc->offset & ~PAGE_MASK));
224 iowrite32(reloc->delta, reloc_entry);
225 io_mapping_unmap_atomic(reloc_page);
226 }
227
Daniel Vetter149c8402012-02-15 23:50:23 +0100228 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
229 * pipe_control writes because the gpu doesn't properly redirect them
230 * through the ppgtt for non_secure batchbuffers. */
231 if (unlikely(IS_GEN6(dev) &&
232 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
233 !target_i915_obj->has_global_gtt_mapping)) {
234 i915_gem_gtt_bind_object(target_i915_obj,
235 target_i915_obj->cache_level);
236 }
237
Chris Wilson54cf91d2010-11-25 18:00:26 +0000238 /* and update the user's relocation entry */
239 reloc->presumed_offset = target_offset;
240
Chris Wilson67731b82010-12-08 10:38:14 +0000241 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000242}
243
244static int
245i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000246 struct eb_objects *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000247{
Chris Wilson1d83f442012-03-24 20:12:53 +0000248#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
249 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000250 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000251 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000252 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000253
254 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000255
Chris Wilson1d83f442012-03-24 20:12:53 +0000256 remain = entry->relocation_count;
257 while (remain) {
258 struct drm_i915_gem_relocation_entry *r = stack_reloc;
259 int count = remain;
260 if (count > ARRAY_SIZE(stack_reloc))
261 count = ARRAY_SIZE(stack_reloc);
262 remain -= count;
263
264 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000265 return -EFAULT;
266
Chris Wilson1d83f442012-03-24 20:12:53 +0000267 do {
268 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000269
Chris Wilson1d83f442012-03-24 20:12:53 +0000270 ret = i915_gem_execbuffer_relocate_entry(obj, eb, r);
271 if (ret)
272 return ret;
273
274 if (r->presumed_offset != offset &&
275 __copy_to_user_inatomic(&user_relocs->presumed_offset,
276 &r->presumed_offset,
277 sizeof(r->presumed_offset))) {
278 return -EFAULT;
279 }
280
281 user_relocs++;
282 r++;
283 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000284 }
285
286 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000287#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000288}
289
290static int
291i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000292 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000293 struct drm_i915_gem_relocation_entry *relocs)
294{
Chris Wilson6fe4f142011-01-10 17:35:37 +0000295 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000296 int i, ret;
297
298 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000299 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000300 if (ret)
301 return ret;
302 }
303
304 return 0;
305}
306
307static int
308i915_gem_execbuffer_relocate(struct drm_device *dev,
Chris Wilson67731b82010-12-08 10:38:14 +0000309 struct eb_objects *eb,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000310 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000311{
Chris Wilson432e58e2010-11-25 19:32:06 +0000312 struct drm_i915_gem_object *obj;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000313 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000314
Chris Wilsond4aeee72011-03-14 15:11:24 +0000315 /* This is the fast path and we cannot handle a pagefault whilst
316 * holding the struct mutex lest the user pass in the relocations
317 * contained within a mmaped bo. For in such a case we, the page
318 * fault handler would call i915_gem_fault() and we would try to
319 * acquire the struct mutex again. Obviously this is bad and so
320 * lockdep complains vehemently.
321 */
322 pagefault_disable();
Chris Wilson432e58e2010-11-25 19:32:06 +0000323 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000324 ret = i915_gem_execbuffer_relocate_object(obj, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000325 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000326 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000327 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000328 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000329
Chris Wilsond4aeee72011-03-14 15:11:24 +0000330 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000331}
332
Chris Wilson1690e1e2011-12-14 13:57:08 +0100333#define __EXEC_OBJECT_HAS_FENCE (1<<31)
334
335static int
Chris Wilsondabdfe02012-03-26 10:10:27 +0200336need_reloc_mappable(struct drm_i915_gem_object *obj)
337{
338 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
339 return entry->relocation_count && !use_cpu_reloc(obj);
340}
341
342static int
Chris Wilson1690e1e2011-12-14 13:57:08 +0100343pin_and_fence_object(struct drm_i915_gem_object *obj,
344 struct intel_ring_buffer *ring)
345{
346 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
347 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
348 bool need_fence, need_mappable;
349 int ret;
350
351 need_fence =
352 has_fenced_gpu_access &&
353 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
354 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200355 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100356
357 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable);
358 if (ret)
359 return ret;
360
361 if (has_fenced_gpu_access) {
362 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100363 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000364 if (ret)
365 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100366
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000367 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100368 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000369
Chris Wilson7dd49062012-03-21 10:48:18 +0000370 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100371 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100372 }
373
374 entry->offset = obj->gtt_offset;
375 return 0;
376
377err_unpin:
378 i915_gem_object_unpin(obj);
379 return ret;
380}
381
Chris Wilson54cf91d2010-11-25 18:00:26 +0000382static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000383i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000384 struct drm_file *file,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000385 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000386{
Daniel Vetter7bddb012012-02-09 17:15:47 +0100387 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000388 struct drm_i915_gem_object *obj;
Chris Wilson432e58e2010-11-25 19:32:06 +0000389 int ret, retry;
Chris Wilson9b3826b2010-12-05 17:11:54 +0000390 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000391 struct list_head ordered_objects;
392
393 INIT_LIST_HEAD(&ordered_objects);
394 while (!list_empty(objects)) {
395 struct drm_i915_gem_exec_object2 *entry;
396 bool need_fence, need_mappable;
397
398 obj = list_first_entry(objects,
399 struct drm_i915_gem_object,
400 exec_list);
401 entry = obj->exec_entry;
402
403 need_fence =
404 has_fenced_gpu_access &&
405 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
406 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200407 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000408
409 if (need_mappable)
410 list_move(&obj->exec_list, &ordered_objects);
411 else
412 list_move_tail(&obj->exec_list, &ordered_objects);
Chris Wilson595dad72011-01-13 11:03:48 +0000413
414 obj->base.pending_read_domains = 0;
415 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100416 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000417 }
418 list_splice(&ordered_objects, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000419
420 /* Attempt to pin all of the buffers into the GTT.
421 * This is done in 3 phases:
422 *
423 * 1a. Unbind all objects that do not match the GTT constraints for
424 * the execbuffer (fenceable, mappable, alignment etc).
425 * 1b. Increment pin count for already bound objects.
426 * 2. Bind new objects.
427 * 3. Decrement pin count.
428 *
429 * This avoid unnecessary unbinding of later objects in order to makr
430 * room for the earlier objects *unless* we need to defragment.
431 */
432 retry = 0;
433 do {
434 ret = 0;
435
436 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000437 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000438 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000439 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100440
Chris Wilson6fe4f142011-01-10 17:35:37 +0000441 if (!obj->gtt_space)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000442 continue;
443
444 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000445 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000446 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
447 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200448 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000449
450 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
451 (need_mappable && !obj->map_and_fenceable))
452 ret = i915_gem_object_unbind(obj);
453 else
Chris Wilson1690e1e2011-12-14 13:57:08 +0100454 ret = pin_and_fence_object(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000455 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000456 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000457 }
458
459 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000460 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100461 if (obj->gtt_space)
462 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000463
Chris Wilson1690e1e2011-12-14 13:57:08 +0100464 ret = pin_and_fence_object(obj, ring);
465 if (ret) {
466 int ret_ignore;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000467
Chris Wilson1690e1e2011-12-14 13:57:08 +0100468 /* This can potentially raise a harmless
469 * -EINVAL if we failed to bind in the above
470 * call. It cannot raise -EINTR since we know
471 * that the bo is freshly bound and so will
472 * not need to be flushed or waited upon.
473 */
474 ret_ignore = i915_gem_object_unbind(obj);
475 (void)ret_ignore;
476 WARN_ON(obj->gtt_space);
477 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000478 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000479 }
480
Chris Wilson432e58e2010-11-25 19:32:06 +0000481 /* Decrement pin count for bound objects */
482 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100483 struct drm_i915_gem_exec_object2 *entry;
484
485 if (!obj->gtt_space)
486 continue;
487
488 entry = obj->exec_entry;
489 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
490 i915_gem_object_unpin_fence(obj);
491 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
492 }
493
494 i915_gem_object_unpin(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100495
496 /* ... and ensure ppgtt mapping exist if needed. */
497 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
498 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
499 obj, obj->cache_level);
500
501 obj->has_aliasing_ppgtt_mapping = 1;
502 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000503 }
504
505 if (ret != -ENOSPC || retry > 1)
506 return ret;
507
508 /* First attempt, just clear anything that is purgeable.
509 * Second attempt, clear the entire GTT.
510 */
Chris Wilsond9e86c02010-11-10 16:40:20 +0000511 ret = i915_gem_evict_everything(ring->dev, retry == 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000512 if (ret)
513 return ret;
514
515 retry++;
516 } while (1);
Chris Wilson432e58e2010-11-25 19:32:06 +0000517
518err:
Chris Wilson1690e1e2011-12-14 13:57:08 +0100519 list_for_each_entry_continue_reverse(obj, objects, exec_list) {
520 struct drm_i915_gem_exec_object2 *entry;
Chris Wilson432e58e2010-11-25 19:32:06 +0000521
Chris Wilson1690e1e2011-12-14 13:57:08 +0100522 if (!obj->gtt_space)
523 continue;
524
525 entry = obj->exec_entry;
526 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
527 i915_gem_object_unpin_fence(obj);
528 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
529 }
530
531 i915_gem_object_unpin(obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000532 }
533
534 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000535}
536
537static int
538i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
539 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000540 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000541 struct list_head *objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000542 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000543 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000544 int count)
545{
546 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000547 struct drm_i915_gem_object *obj;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000548 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000549 int i, total, ret;
550
Chris Wilson67731b82010-12-08 10:38:14 +0000551 /* We may process another execbuffer during the unlock... */
Chris Wilson36cf1742011-01-10 12:09:12 +0000552 while (!list_empty(objects)) {
Chris Wilson67731b82010-12-08 10:38:14 +0000553 obj = list_first_entry(objects,
554 struct drm_i915_gem_object,
555 exec_list);
556 list_del_init(&obj->exec_list);
557 drm_gem_object_unreference(&obj->base);
558 }
559
Chris Wilson54cf91d2010-11-25 18:00:26 +0000560 mutex_unlock(&dev->struct_mutex);
561
562 total = 0;
563 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000564 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000565
Chris Wilsondd6864a2011-01-12 23:49:13 +0000566 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000567 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000568 if (reloc == NULL || reloc_offset == NULL) {
569 drm_free_large(reloc);
570 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000571 mutex_lock(&dev->struct_mutex);
572 return -ENOMEM;
573 }
574
575 total = 0;
576 for (i = 0; i < count; i++) {
577 struct drm_i915_gem_relocation_entry __user *user_relocs;
578
Chris Wilson432e58e2010-11-25 19:32:06 +0000579 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000580
581 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000582 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000583 ret = -EFAULT;
584 mutex_lock(&dev->struct_mutex);
585 goto err;
586 }
587
Chris Wilsondd6864a2011-01-12 23:49:13 +0000588 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000589 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000590 }
591
592 ret = i915_mutex_lock_interruptible(dev);
593 if (ret) {
594 mutex_lock(&dev->struct_mutex);
595 goto err;
596 }
597
Chris Wilson67731b82010-12-08 10:38:14 +0000598 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000599 eb_reset(eb);
600 for (i = 0; i < count; i++) {
Chris Wilson67731b82010-12-08 10:38:14 +0000601 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
602 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000603 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100604 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson67731b82010-12-08 10:38:14 +0000605 exec[i].handle, i);
606 ret = -ENOENT;
607 goto err;
608 }
609
610 list_add_tail(&obj->exec_list, objects);
611 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000612 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000613 eb_add_object(eb, obj);
614 }
615
Chris Wilson6fe4f142011-01-10 17:35:37 +0000616 ret = i915_gem_execbuffer_reserve(ring, file, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000617 if (ret)
618 goto err;
619
Chris Wilson432e58e2010-11-25 19:32:06 +0000620 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondd6864a2011-01-12 23:49:13 +0000621 int offset = obj->exec_entry - exec;
Chris Wilson67731b82010-12-08 10:38:14 +0000622 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilsondd6864a2011-01-12 23:49:13 +0000623 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000624 if (ret)
625 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000626 }
627
628 /* Leave the user relocations as are, this is the painfully slow path,
629 * and we want to avoid the complication of dropping the lock whilst
630 * having buffers reserved in the aperture and so causing spurious
631 * ENOSPC for random operations.
632 */
633
634err:
635 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000636 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000637 return ret;
638}
639
Chris Wilson54cf91d2010-11-25 18:00:26 +0000640static int
Chris Wilsonc59a3332011-03-06 13:51:29 +0000641i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
642{
643 u32 plane, flip_mask;
644 int ret;
645
646 /* Check for any pending flips. As we only maintain a flip queue depth
647 * of 1, we can simply insert a WAIT for the next display flip prior
648 * to executing the batch and avoid stalling the CPU.
649 */
650
651 for (plane = 0; flips >> plane; plane++) {
652 if (((flips >> plane) & 1) == 0)
653 continue;
654
655 if (plane)
656 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
657 else
658 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
659
660 ret = intel_ring_begin(ring, 2);
661 if (ret)
662 return ret;
663
664 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
665 intel_ring_emit(ring, MI_NOOP);
666 intel_ring_advance(ring);
667 }
668
669 return 0;
670}
671
Chris Wilsonc59a3332011-03-06 13:51:29 +0000672static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000673i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
674 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000675{
Chris Wilson432e58e2010-11-25 19:32:06 +0000676 struct drm_i915_gem_object *obj;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200677 uint32_t flush_domains = 0;
678 uint32_t flips = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000679 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000680
Chris Wilson432e58e2010-11-25 19:32:06 +0000681 list_for_each_entry(obj, objects, exec_list) {
Ben Widawsky2911a352012-04-05 14:47:36 -0700682 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000683 if (ret)
684 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200685
686 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
687 i915_gem_clflush_object(obj);
688
689 if (obj->base.pending_write_domain)
690 flips |= atomic_read(&obj->pending_flip);
691
692 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000693 }
694
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200695 if (flips) {
696 ret = i915_gem_execbuffer_wait_for_flips(ring, flips);
697 if (ret)
698 return ret;
699 }
700
701 if (flush_domains & I915_GEM_DOMAIN_CPU)
702 intel_gtt_chipset_flush();
703
704 if (flush_domains & I915_GEM_DOMAIN_GTT)
705 wmb();
706
Chris Wilson09cf7c92012-07-13 14:14:08 +0100707 /* Unconditionally invalidate gpu caches and ensure that we do flush
708 * any residual writes from the previous batch.
709 */
710 ret = i915_gem_flush_ring(ring,
711 I915_GEM_GPU_DOMAINS,
712 ring->gpu_caches_dirty ? I915_GEM_GPU_DOMAINS : 0);
Daniel Vettercc889e02012-06-13 20:45:19 +0200713 if (ret)
714 return ret;
715
Chris Wilson09cf7c92012-07-13 14:14:08 +0100716 ring->gpu_caches_dirty = false;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000717 return 0;
718}
719
Chris Wilson432e58e2010-11-25 19:32:06 +0000720static bool
721i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000722{
Chris Wilson432e58e2010-11-25 19:32:06 +0000723 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000724}
725
726static int
727validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
728 int count)
729{
730 int i;
731
732 for (i = 0; i < count; i++) {
733 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
734 int length; /* limited by fault_in_pages_readable() */
735
736 /* First check for malicious input causing overflow */
737 if (exec[i].relocation_count >
738 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
739 return -EINVAL;
740
741 length = exec[i].relocation_count *
742 sizeof(struct drm_i915_gem_relocation_entry);
743 if (!access_ok(VERIFY_READ, ptr, length))
744 return -EFAULT;
745
746 /* we may also need to update the presumed offsets */
747 if (!access_ok(VERIFY_WRITE, ptr, length))
748 return -EFAULT;
749
Daniel Vetterf56f8212012-03-25 19:47:41 +0200750 if (fault_in_multipages_readable(ptr, length))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000751 return -EFAULT;
752 }
753
754 return 0;
755}
756
Chris Wilson432e58e2010-11-25 19:32:06 +0000757static void
758i915_gem_execbuffer_move_to_active(struct list_head *objects,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000759 struct intel_ring_buffer *ring,
760 u32 seqno)
Chris Wilson432e58e2010-11-25 19:32:06 +0000761{
762 struct drm_i915_gem_object *obj;
763
764 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson69c2fc82012-07-20 12:41:03 +0100765 u32 old_read = obj->base.read_domains;
766 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000767
Chris Wilson432e58e2010-11-25 19:32:06 +0000768 obj->base.read_domains = obj->base.pending_read_domains;
769 obj->base.write_domain = obj->base.pending_write_domain;
770 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
771
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000772 i915_gem_object_move_to_active(obj, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +0000773 if (obj->base.write_domain) {
774 obj->dirty = 1;
Chris Wilson0201f1e2012-07-20 12:41:01 +0100775 obj->last_write_seqno = seqno;
Chris Wilsonacb87df2012-05-03 15:47:57 +0100776 if (obj->pin_count) /* check for potential scanout */
777 intel_mark_busy(ring->dev, obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000778 }
779
Chris Wilsondb53a302011-02-03 11:57:46 +0000780 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000781 }
Chris Wilsonacb87df2012-05-03 15:47:57 +0100782
783 intel_mark_busy(ring->dev, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000784}
785
Chris Wilson54cf91d2010-11-25 18:00:26 +0000786static void
787i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000788 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000789 struct intel_ring_buffer *ring)
790{
Daniel Vettercc889e02012-06-13 20:45:19 +0200791 /* Unconditionally force add_request to emit a full flush. */
792 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000793
Chris Wilson432e58e2010-11-25 19:32:06 +0000794 /* Add a breadcrumb for the completion of the batch buffer */
Chris Wilson3bb73ab2012-07-20 12:40:59 +0100795 (void)i915_add_request(ring, file, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000796}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000797
798static int
Eric Anholtae662d32012-01-03 09:23:29 -0800799i915_reset_gen7_sol_offsets(struct drm_device *dev,
800 struct intel_ring_buffer *ring)
801{
802 drm_i915_private_t *dev_priv = dev->dev_private;
803 int ret, i;
804
805 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
806 return 0;
807
808 ret = intel_ring_begin(ring, 4 * 3);
809 if (ret)
810 return ret;
811
812 for (i = 0; i < 4; i++) {
813 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
814 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
815 intel_ring_emit(ring, 0);
816 }
817
818 intel_ring_advance(ring);
819
820 return 0;
821}
822
823static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000824i915_gem_do_execbuffer(struct drm_device *dev, void *data,
825 struct drm_file *file,
826 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +0000827 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000828{
829 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000830 struct list_head objects;
Chris Wilson67731b82010-12-08 10:38:14 +0000831 struct eb_objects *eb;
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;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700835 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000836 u32 exec_start, exec_len;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000837 u32 seqno;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800838 u32 mask;
Chris Wilson72bfa192010-12-19 11:42:05 +0000839 int ret, mode, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000840
Chris Wilson432e58e2010-11-25 19:32:06 +0000841 if (!i915_gem_check_execbuffer(args)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100842 DRM_DEBUG("execbuf with invalid offset/length\n");
Chris Wilson432e58e2010-11-25 19:32:06 +0000843 return -EINVAL;
844 }
845
846 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000847 if (ret)
848 return ret;
849
Chris Wilson54cf91d2010-11-25 18:00:26 +0000850 switch (args->flags & I915_EXEC_RING_MASK) {
851 case I915_EXEC_DEFAULT:
852 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000853 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000854 break;
855 case I915_EXEC_BSD:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000856 ring = &dev_priv->ring[VCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700857 if (ctx_id != 0) {
858 DRM_DEBUG("Ring %s doesn't support contexts\n",
859 ring->name);
860 return -EPERM;
861 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000862 break;
863 case I915_EXEC_BLT:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000864 ring = &dev_priv->ring[BCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700865 if (ctx_id != 0) {
866 DRM_DEBUG("Ring %s doesn't support contexts\n",
867 ring->name);
868 return -EPERM;
869 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000870 break;
871 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100872 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000873 (int)(args->flags & I915_EXEC_RING_MASK));
874 return -EINVAL;
875 }
Chris Wilsona15817c2012-05-11 14:29:31 +0100876 if (!intel_ring_initialized(ring)) {
877 DRM_DEBUG("execbuf with invalid ring: %d\n",
878 (int)(args->flags & I915_EXEC_RING_MASK));
879 return -EINVAL;
880 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000881
Chris Wilson72bfa192010-12-19 11:42:05 +0000882 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800883 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +0000884 switch (mode) {
885 case I915_EXEC_CONSTANTS_REL_GENERAL:
886 case I915_EXEC_CONSTANTS_ABSOLUTE:
887 case I915_EXEC_CONSTANTS_REL_SURFACE:
888 if (ring == &dev_priv->ring[RCS] &&
889 mode != dev_priv->relative_constants_mode) {
890 if (INTEL_INFO(dev)->gen < 4)
891 return -EINVAL;
892
893 if (INTEL_INFO(dev)->gen > 5 &&
894 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
895 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800896
897 /* The HW changed the meaning on this bit on gen6 */
898 if (INTEL_INFO(dev)->gen >= 6)
899 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +0000900 }
901 break;
902 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100903 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +0000904 return -EINVAL;
905 }
906
Chris Wilson54cf91d2010-11-25 18:00:26 +0000907 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +0100908 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000909 return -EINVAL;
910 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000911
912 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000913 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +0100914 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000915 return -EINVAL;
916 }
917
Daniel Vetter6ebebc92012-04-26 23:28:11 +0200918 if (INTEL_INFO(dev)->gen >= 5) {
919 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
920 return -EINVAL;
921 }
922
Xi Wang44afb3a2012-04-23 04:06:42 -0400923 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
924 DRM_DEBUG("execbuf with %u cliprects\n",
925 args->num_cliprects);
926 return -EINVAL;
927 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200928
Chris Wilson432e58e2010-11-25 19:32:06 +0000929 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +0000930 GFP_KERNEL);
931 if (cliprects == NULL) {
932 ret = -ENOMEM;
933 goto pre_mutex_err;
934 }
935
Chris Wilson432e58e2010-11-25 19:32:06 +0000936 if (copy_from_user(cliprects,
937 (struct drm_clip_rect __user *)(uintptr_t)
938 args->cliprects_ptr,
939 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000940 ret = -EFAULT;
941 goto pre_mutex_err;
942 }
943 }
944
Chris Wilson54cf91d2010-11-25 18:00:26 +0000945 ret = i915_mutex_lock_interruptible(dev);
946 if (ret)
947 goto pre_mutex_err;
948
949 if (dev_priv->mm.suspended) {
950 mutex_unlock(&dev->struct_mutex);
951 ret = -EBUSY;
952 goto pre_mutex_err;
953 }
954
Chris Wilson67731b82010-12-08 10:38:14 +0000955 eb = eb_create(args->buffer_count);
956 if (eb == NULL) {
957 mutex_unlock(&dev->struct_mutex);
958 ret = -ENOMEM;
959 goto pre_mutex_err;
960 }
961
Chris Wilson54cf91d2010-11-25 18:00:26 +0000962 /* Look up object handles */
Chris Wilson432e58e2010-11-25 19:32:06 +0000963 INIT_LIST_HEAD(&objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000964 for (i = 0; i < args->buffer_count; i++) {
965 struct drm_i915_gem_object *obj;
966
Chris Wilson432e58e2010-11-25 19:32:06 +0000967 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
968 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000969 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100970 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000971 exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000972 /* prevent error path from reading uninitialized data */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000973 ret = -ENOENT;
974 goto err;
975 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000976
Chris Wilson432e58e2010-11-25 19:32:06 +0000977 if (!list_empty(&obj->exec_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100978 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000979 obj, exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000980 ret = -EINVAL;
981 goto err;
982 }
Chris Wilson432e58e2010-11-25 19:32:06 +0000983
984 list_add_tail(&obj->exec_list, &objects);
Chris Wilson67731b82010-12-08 10:38:14 +0000985 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000986 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000987 eb_add_object(eb, obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000988 }
989
Chris Wilson6fe4f142011-01-10 17:35:37 +0000990 /* take note of the batch buffer before we might reorder the lists */
991 batch_obj = list_entry(objects.prev,
992 struct drm_i915_gem_object,
993 exec_list);
994
Chris Wilson54cf91d2010-11-25 18:00:26 +0000995 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilson6fe4f142011-01-10 17:35:37 +0000996 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000997 if (ret)
998 goto err;
999
1000 /* The objects are in their final locations, apply the relocations. */
Chris Wilson6fe4f142011-01-10 17:35:37 +00001001 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001002 if (ret) {
1003 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00001004 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilson67731b82010-12-08 10:38:14 +00001005 &objects, eb,
1006 exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001007 args->buffer_count);
1008 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1009 }
1010 if (ret)
1011 goto err;
1012 }
1013
1014 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001015 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001016 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001017 ret = -EINVAL;
1018 goto err;
1019 }
1020 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1021
Chris Wilson432e58e2010-11-25 19:32:06 +00001022 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001023 if (ret)
1024 goto err;
1025
Chris Wilsondb53a302011-02-03 11:57:46 +00001026 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson076e2c02011-01-21 10:07:18 +00001027 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001028 if (seqno < ring->sync_seqno[i]) {
1029 /* The GPU can not handle its semaphore value wrapping,
1030 * so every billion or so execbuffers, we need to stall
1031 * the GPU in order to reset the counters.
1032 */
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001033 ret = i915_gpu_idle(dev);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001034 if (ret)
1035 goto err;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001036 i915_gem_retire_requests(dev);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001037
1038 BUG_ON(ring->sync_seqno[i]);
1039 }
1040 }
1041
Eric Anholt0da5cec2012-07-23 12:33:55 -07001042 ret = i915_switch_context(ring, file, ctx_id);
1043 if (ret)
1044 goto err;
1045
Ben Widawskye2971bd2011-12-12 19:21:57 -08001046 if (ring == &dev_priv->ring[RCS] &&
1047 mode != dev_priv->relative_constants_mode) {
1048 ret = intel_ring_begin(ring, 4);
1049 if (ret)
1050 goto err;
1051
1052 intel_ring_emit(ring, MI_NOOP);
1053 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1054 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001055 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001056 intel_ring_advance(ring);
1057
1058 dev_priv->relative_constants_mode = mode;
1059 }
1060
Eric Anholtae662d32012-01-03 09:23:29 -08001061 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1062 ret = i915_reset_gen7_sol_offsets(dev, ring);
1063 if (ret)
1064 goto err;
1065 }
1066
Chris Wilsondb53a302011-02-03 11:57:46 +00001067 trace_i915_gem_ring_dispatch(ring, seqno);
1068
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001069 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1070 exec_len = args->batch_len;
1071 if (cliprects) {
1072 for (i = 0; i < args->num_cliprects; i++) {
1073 ret = i915_emit_box(dev, &cliprects[i],
1074 args->DR1, args->DR4);
1075 if (ret)
1076 goto err;
1077
1078 ret = ring->dispatch_execbuffer(ring,
1079 exec_start, exec_len);
1080 if (ret)
1081 goto err;
1082 }
1083 } else {
1084 ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
1085 if (ret)
1086 goto err;
1087 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001088
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001089 i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +00001090 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001091
1092err:
Chris Wilson67731b82010-12-08 10:38:14 +00001093 eb_destroy(eb);
Chris Wilson432e58e2010-11-25 19:32:06 +00001094 while (!list_empty(&objects)) {
1095 struct drm_i915_gem_object *obj;
1096
1097 obj = list_first_entry(&objects,
1098 struct drm_i915_gem_object,
1099 exec_list);
1100 list_del_init(&obj->exec_list);
1101 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001102 }
1103
1104 mutex_unlock(&dev->struct_mutex);
1105
1106pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001107 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001108 return ret;
1109}
1110
1111/*
1112 * Legacy execbuffer just creates an exec2 list from the original exec object
1113 * list array and passes it to the real function.
1114 */
1115int
1116i915_gem_execbuffer(struct drm_device *dev, void *data,
1117 struct drm_file *file)
1118{
1119 struct drm_i915_gem_execbuffer *args = data;
1120 struct drm_i915_gem_execbuffer2 exec2;
1121 struct drm_i915_gem_exec_object *exec_list = NULL;
1122 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1123 int ret, i;
1124
Chris Wilson54cf91d2010-11-25 18:00:26 +00001125 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001126 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001127 return -EINVAL;
1128 }
1129
1130 /* Copy in the exec list from userland */
1131 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1132 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1133 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001134 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001135 args->buffer_count);
1136 drm_free_large(exec_list);
1137 drm_free_large(exec2_list);
1138 return -ENOMEM;
1139 }
1140 ret = copy_from_user(exec_list,
1141 (struct drm_i915_relocation_entry __user *)
1142 (uintptr_t) args->buffers_ptr,
1143 sizeof(*exec_list) * args->buffer_count);
1144 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001145 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001146 args->buffer_count, ret);
1147 drm_free_large(exec_list);
1148 drm_free_large(exec2_list);
1149 return -EFAULT;
1150 }
1151
1152 for (i = 0; i < args->buffer_count; i++) {
1153 exec2_list[i].handle = exec_list[i].handle;
1154 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1155 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1156 exec2_list[i].alignment = exec_list[i].alignment;
1157 exec2_list[i].offset = exec_list[i].offset;
1158 if (INTEL_INFO(dev)->gen < 4)
1159 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1160 else
1161 exec2_list[i].flags = 0;
1162 }
1163
1164 exec2.buffers_ptr = args->buffers_ptr;
1165 exec2.buffer_count = args->buffer_count;
1166 exec2.batch_start_offset = args->batch_start_offset;
1167 exec2.batch_len = args->batch_len;
1168 exec2.DR1 = args->DR1;
1169 exec2.DR4 = args->DR4;
1170 exec2.num_cliprects = args->num_cliprects;
1171 exec2.cliprects_ptr = args->cliprects_ptr;
1172 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001173 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001174
1175 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1176 if (!ret) {
1177 /* Copy the new buffer offsets back to the user's exec list. */
1178 for (i = 0; i < args->buffer_count; i++)
1179 exec_list[i].offset = exec2_list[i].offset;
1180 /* ... and back out to userspace */
1181 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1182 (uintptr_t) args->buffers_ptr,
1183 exec_list,
1184 sizeof(*exec_list) * args->buffer_count);
1185 if (ret) {
1186 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001187 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001188 "back to user (%d)\n",
1189 args->buffer_count, ret);
1190 }
1191 }
1192
1193 drm_free_large(exec_list);
1194 drm_free_large(exec2_list);
1195 return ret;
1196}
1197
1198int
1199i915_gem_execbuffer2(struct drm_device *dev, void *data,
1200 struct drm_file *file)
1201{
1202 struct drm_i915_gem_execbuffer2 *args = data;
1203 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1204 int ret;
1205
Xi Wanged8cd3b2012-04-23 04:06:41 -04001206 if (args->buffer_count < 1 ||
1207 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001208 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001209 return -EINVAL;
1210 }
1211
Chris Wilson8408c282011-02-21 12:54:48 +00001212 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1213 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1214 if (exec2_list == NULL)
1215 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1216 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001217 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001218 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001219 args->buffer_count);
1220 return -ENOMEM;
1221 }
1222 ret = copy_from_user(exec2_list,
1223 (struct drm_i915_relocation_entry __user *)
1224 (uintptr_t) args->buffers_ptr,
1225 sizeof(*exec2_list) * args->buffer_count);
1226 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001227 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001228 args->buffer_count, ret);
1229 drm_free_large(exec2_list);
1230 return -EFAULT;
1231 }
1232
1233 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1234 if (!ret) {
1235 /* Copy the new buffer offsets back to the user's exec list. */
1236 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1237 (uintptr_t) args->buffers_ptr,
1238 exec2_list,
1239 sizeof(*exec2_list) * args->buffer_count);
1240 if (ret) {
1241 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001242 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001243 "back to user (%d)\n",
1244 args->buffer_count, ret);
1245 }
1246 }
1247
1248 drm_free_large(exec2_list);
1249 return ret;
1250}