blob: f7346d87655129863d0af297063f25f7c281b4e3 [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
Eric Anholte844b992012-07-31 15:35:01 -0700120 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
121 * pipe_control writes because the gpu doesn't properly redirect them
122 * through the ppgtt for non_secure batchbuffers. */
123 if (unlikely(IS_GEN6(dev) &&
124 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
125 !target_i915_obj->has_global_gtt_mapping)) {
126 i915_gem_gtt_bind_object(target_i915_obj,
127 target_i915_obj->cache_level);
128 }
129
Chris Wilson54cf91d2010-11-25 18:00:26 +0000130 /* The target buffer should have appeared before us in the
131 * exec_object list, so it should have a GTT space bound by now.
132 */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000133 if (unlikely(target_offset == 0)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100134 DRM_DEBUG("No GTT space found for object %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000135 reloc->target_handle);
Chris Wilson67731b82010-12-08 10:38:14 +0000136 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000137 }
138
139 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000140 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100141 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000142 "obj %p target %d offset %d "
143 "read %08x write %08x",
144 obj, reloc->target_handle,
145 (int) reloc->offset,
146 reloc->read_domains,
147 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000148 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000149 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100150 if (unlikely((reloc->write_domain | reloc->read_domains)
151 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100152 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000153 "obj %p target %d offset %d "
154 "read %08x write %08x",
155 obj, reloc->target_handle,
156 (int) reloc->offset,
157 reloc->read_domains,
158 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000159 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000160 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000161 if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
162 reloc->write_domain != target_obj->pending_write_domain)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100163 DRM_DEBUG("Write domain conflict: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000164 "obj %p target %d offset %d "
165 "new %08x old %08x\n",
166 obj, reloc->target_handle,
167 (int) reloc->offset,
168 reloc->write_domain,
169 target_obj->pending_write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000170 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000171 }
172
173 target_obj->pending_read_domains |= reloc->read_domains;
174 target_obj->pending_write_domain |= reloc->write_domain;
175
176 /* If the relocation already has the right value in it, no
177 * more work needs to be done.
178 */
179 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000180 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000181
182 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000183 if (unlikely(reloc->offset > obj->base.size - 4)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100184 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000185 "obj %p target %d offset %d size %d.\n",
186 obj, reloc->target_handle,
187 (int) reloc->offset,
188 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000189 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000190 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000191 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100192 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000193 "obj %p target %d offset %d.\n",
194 obj, reloc->target_handle,
195 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000196 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000197 }
198
Chris Wilsondabdfe02012-03-26 10:10:27 +0200199 /* We can't wait for rendering with pagefaults disabled */
200 if (obj->active && in_atomic())
201 return -EFAULT;
202
Chris Wilson54cf91d2010-11-25 18:00:26 +0000203 reloc->delta += target_offset;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200204 if (use_cpu_reloc(obj)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000205 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
206 char *vaddr;
207
Chris Wilsondabdfe02012-03-26 10:10:27 +0200208 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
209 if (ret)
210 return ret;
211
Chris Wilson54cf91d2010-11-25 18:00:26 +0000212 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
213 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
214 kunmap_atomic(vaddr);
215 } else {
216 struct drm_i915_private *dev_priv = dev->dev_private;
217 uint32_t __iomem *reloc_entry;
218 void __iomem *reloc_page;
219
Chris Wilson7b096382012-04-14 09:55:51 +0100220 ret = i915_gem_object_set_to_gtt_domain(obj, true);
221 if (ret)
222 return ret;
223
224 ret = i915_gem_object_put_fence(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000225 if (ret)
Chris Wilson67731b82010-12-08 10:38:14 +0000226 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000227
228 /* Map the page containing the relocation we're going to perform. */
229 reloc->offset += obj->gtt_offset;
230 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
231 reloc->offset & PAGE_MASK);
232 reloc_entry = (uint32_t __iomem *)
233 (reloc_page + (reloc->offset & ~PAGE_MASK));
234 iowrite32(reloc->delta, reloc_entry);
235 io_mapping_unmap_atomic(reloc_page);
236 }
237
238 /* 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
Chris Wilson86a1ee22012-08-11 15:41:04 +0100357 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable, false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100358 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
Chris Wilson6c085a72012-08-20 11:40:46 +0200505 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000506 return ret;
507
Chris Wilson6c085a72012-08-20 11:40:46 +0200508 ret = i915_gem_evict_everything(ring->dev);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000509 if (ret)
510 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000511 } while (1);
Chris Wilson432e58e2010-11-25 19:32:06 +0000512
513err:
Chris Wilson1690e1e2011-12-14 13:57:08 +0100514 list_for_each_entry_continue_reverse(obj, objects, exec_list) {
515 struct drm_i915_gem_exec_object2 *entry;
Chris Wilson432e58e2010-11-25 19:32:06 +0000516
Chris Wilson1690e1e2011-12-14 13:57:08 +0100517 if (!obj->gtt_space)
518 continue;
519
520 entry = obj->exec_entry;
521 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
522 i915_gem_object_unpin_fence(obj);
523 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
524 }
525
526 i915_gem_object_unpin(obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000527 }
528
529 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000530}
531
532static int
533i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
534 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000535 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000536 struct list_head *objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000537 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000538 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000539 int count)
540{
541 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000542 struct drm_i915_gem_object *obj;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000543 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000544 int i, total, ret;
545
Chris Wilson67731b82010-12-08 10:38:14 +0000546 /* We may process another execbuffer during the unlock... */
Chris Wilson36cf1742011-01-10 12:09:12 +0000547 while (!list_empty(objects)) {
Chris Wilson67731b82010-12-08 10:38:14 +0000548 obj = list_first_entry(objects,
549 struct drm_i915_gem_object,
550 exec_list);
551 list_del_init(&obj->exec_list);
552 drm_gem_object_unreference(&obj->base);
553 }
554
Chris Wilson54cf91d2010-11-25 18:00:26 +0000555 mutex_unlock(&dev->struct_mutex);
556
557 total = 0;
558 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000559 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000560
Chris Wilsondd6864a2011-01-12 23:49:13 +0000561 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000562 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000563 if (reloc == NULL || reloc_offset == NULL) {
564 drm_free_large(reloc);
565 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000566 mutex_lock(&dev->struct_mutex);
567 return -ENOMEM;
568 }
569
570 total = 0;
571 for (i = 0; i < count; i++) {
572 struct drm_i915_gem_relocation_entry __user *user_relocs;
573
Chris Wilson432e58e2010-11-25 19:32:06 +0000574 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000575
576 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000577 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000578 ret = -EFAULT;
579 mutex_lock(&dev->struct_mutex);
580 goto err;
581 }
582
Chris Wilsondd6864a2011-01-12 23:49:13 +0000583 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000584 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000585 }
586
587 ret = i915_mutex_lock_interruptible(dev);
588 if (ret) {
589 mutex_lock(&dev->struct_mutex);
590 goto err;
591 }
592
Chris Wilson67731b82010-12-08 10:38:14 +0000593 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000594 eb_reset(eb);
595 for (i = 0; i < count; i++) {
Chris Wilson67731b82010-12-08 10:38:14 +0000596 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
597 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000598 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100599 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson67731b82010-12-08 10:38:14 +0000600 exec[i].handle, i);
601 ret = -ENOENT;
602 goto err;
603 }
604
605 list_add_tail(&obj->exec_list, objects);
606 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000607 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000608 eb_add_object(eb, obj);
609 }
610
Chris Wilson6fe4f142011-01-10 17:35:37 +0000611 ret = i915_gem_execbuffer_reserve(ring, file, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000612 if (ret)
613 goto err;
614
Chris Wilson432e58e2010-11-25 19:32:06 +0000615 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondd6864a2011-01-12 23:49:13 +0000616 int offset = obj->exec_entry - exec;
Chris Wilson67731b82010-12-08 10:38:14 +0000617 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilsondd6864a2011-01-12 23:49:13 +0000618 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000619 if (ret)
620 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000621 }
622
623 /* Leave the user relocations as are, this is the painfully slow path,
624 * and we want to avoid the complication of dropping the lock whilst
625 * having buffers reserved in the aperture and so causing spurious
626 * ENOSPC for random operations.
627 */
628
629err:
630 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000631 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000632 return ret;
633}
634
Chris Wilson54cf91d2010-11-25 18:00:26 +0000635static int
Chris Wilsonc59a3332011-03-06 13:51:29 +0000636i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
637{
638 u32 plane, flip_mask;
639 int ret;
640
641 /* Check for any pending flips. As we only maintain a flip queue depth
642 * of 1, we can simply insert a WAIT for the next display flip prior
643 * to executing the batch and avoid stalling the CPU.
644 */
645
646 for (plane = 0; flips >> plane; plane++) {
647 if (((flips >> plane) & 1) == 0)
648 continue;
649
650 if (plane)
651 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
652 else
653 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
654
655 ret = intel_ring_begin(ring, 2);
656 if (ret)
657 return ret;
658
659 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
660 intel_ring_emit(ring, MI_NOOP);
661 intel_ring_advance(ring);
662 }
663
664 return 0;
665}
666
Chris Wilsonc59a3332011-03-06 13:51:29 +0000667static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000668i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
669 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000670{
Chris Wilson432e58e2010-11-25 19:32:06 +0000671 struct drm_i915_gem_object *obj;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200672 uint32_t flush_domains = 0;
673 uint32_t flips = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000674 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000675
Chris Wilson432e58e2010-11-25 19:32:06 +0000676 list_for_each_entry(obj, objects, exec_list) {
Ben Widawsky2911a352012-04-05 14:47:36 -0700677 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000678 if (ret)
679 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200680
681 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
682 i915_gem_clflush_object(obj);
683
684 if (obj->base.pending_write_domain)
685 flips |= atomic_read(&obj->pending_flip);
686
687 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000688 }
689
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200690 if (flips) {
691 ret = i915_gem_execbuffer_wait_for_flips(ring, flips);
692 if (ret)
693 return ret;
694 }
695
696 if (flush_domains & I915_GEM_DOMAIN_CPU)
697 intel_gtt_chipset_flush();
698
699 if (flush_domains & I915_GEM_DOMAIN_GTT)
700 wmb();
701
Chris Wilson09cf7c92012-07-13 14:14:08 +0100702 /* Unconditionally invalidate gpu caches and ensure that we do flush
703 * any residual writes from the previous batch.
704 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100705 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000706}
707
Chris Wilson432e58e2010-11-25 19:32:06 +0000708static bool
709i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000710{
Chris Wilson432e58e2010-11-25 19:32:06 +0000711 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000712}
713
714static int
715validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
716 int count)
717{
718 int i;
719
720 for (i = 0; i < count; i++) {
721 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
722 int length; /* limited by fault_in_pages_readable() */
723
724 /* First check for malicious input causing overflow */
725 if (exec[i].relocation_count >
726 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
727 return -EINVAL;
728
729 length = exec[i].relocation_count *
730 sizeof(struct drm_i915_gem_relocation_entry);
731 if (!access_ok(VERIFY_READ, ptr, length))
732 return -EFAULT;
733
734 /* we may also need to update the presumed offsets */
735 if (!access_ok(VERIFY_WRITE, ptr, length))
736 return -EFAULT;
737
Daniel Vetterf56f8212012-03-25 19:47:41 +0200738 if (fault_in_multipages_readable(ptr, length))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000739 return -EFAULT;
740 }
741
742 return 0;
743}
744
Chris Wilson432e58e2010-11-25 19:32:06 +0000745static void
746i915_gem_execbuffer_move_to_active(struct list_head *objects,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000747 struct intel_ring_buffer *ring,
748 u32 seqno)
Chris Wilson432e58e2010-11-25 19:32:06 +0000749{
750 struct drm_i915_gem_object *obj;
751
752 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson69c2fc82012-07-20 12:41:03 +0100753 u32 old_read = obj->base.read_domains;
754 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000755
Chris Wilson432e58e2010-11-25 19:32:06 +0000756 obj->base.read_domains = obj->base.pending_read_domains;
757 obj->base.write_domain = obj->base.pending_write_domain;
758 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
759
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000760 i915_gem_object_move_to_active(obj, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +0000761 if (obj->base.write_domain) {
762 obj->dirty = 1;
Chris Wilson0201f1e2012-07-20 12:41:01 +0100763 obj->last_write_seqno = seqno;
Chris Wilsonacb87df2012-05-03 15:47:57 +0100764 if (obj->pin_count) /* check for potential scanout */
Chris Wilsonf047e392012-07-21 12:31:41 +0100765 intel_mark_fb_busy(obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000766 }
767
Chris Wilsondb53a302011-02-03 11:57:46 +0000768 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000769 }
770}
771
Chris Wilson54cf91d2010-11-25 18:00:26 +0000772static void
773i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000774 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000775 struct intel_ring_buffer *ring)
776{
Daniel Vettercc889e02012-06-13 20:45:19 +0200777 /* Unconditionally force add_request to emit a full flush. */
778 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000779
Chris Wilson432e58e2010-11-25 19:32:06 +0000780 /* Add a breadcrumb for the completion of the batch buffer */
Chris Wilson3bb73ab2012-07-20 12:40:59 +0100781 (void)i915_add_request(ring, file, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000782}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000783
784static int
Eric Anholtae662d32012-01-03 09:23:29 -0800785i915_reset_gen7_sol_offsets(struct drm_device *dev,
786 struct intel_ring_buffer *ring)
787{
788 drm_i915_private_t *dev_priv = dev->dev_private;
789 int ret, i;
790
791 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
792 return 0;
793
794 ret = intel_ring_begin(ring, 4 * 3);
795 if (ret)
796 return ret;
797
798 for (i = 0; i < 4; i++) {
799 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
800 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
801 intel_ring_emit(ring, 0);
802 }
803
804 intel_ring_advance(ring);
805
806 return 0;
807}
808
809static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000810i915_gem_do_execbuffer(struct drm_device *dev, void *data,
811 struct drm_file *file,
812 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +0000813 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000814{
815 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000816 struct list_head objects;
Chris Wilson67731b82010-12-08 10:38:14 +0000817 struct eb_objects *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000818 struct drm_i915_gem_object *batch_obj;
819 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000820 struct intel_ring_buffer *ring;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700821 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000822 u32 exec_start, exec_len;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000823 u32 seqno;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800824 u32 mask;
Chris Wilson72bfa192010-12-19 11:42:05 +0000825 int ret, mode, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000826
Chris Wilson432e58e2010-11-25 19:32:06 +0000827 if (!i915_gem_check_execbuffer(args)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100828 DRM_DEBUG("execbuf with invalid offset/length\n");
Chris Wilson432e58e2010-11-25 19:32:06 +0000829 return -EINVAL;
830 }
831
832 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000833 if (ret)
834 return ret;
835
Chris Wilson54cf91d2010-11-25 18:00:26 +0000836 switch (args->flags & I915_EXEC_RING_MASK) {
837 case I915_EXEC_DEFAULT:
838 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000839 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000840 break;
841 case I915_EXEC_BSD:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000842 ring = &dev_priv->ring[VCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700843 if (ctx_id != 0) {
844 DRM_DEBUG("Ring %s doesn't support contexts\n",
845 ring->name);
846 return -EPERM;
847 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000848 break;
849 case I915_EXEC_BLT:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000850 ring = &dev_priv->ring[BCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700851 if (ctx_id != 0) {
852 DRM_DEBUG("Ring %s doesn't support contexts\n",
853 ring->name);
854 return -EPERM;
855 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000856 break;
857 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100858 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000859 (int)(args->flags & I915_EXEC_RING_MASK));
860 return -EINVAL;
861 }
Chris Wilsona15817c2012-05-11 14:29:31 +0100862 if (!intel_ring_initialized(ring)) {
863 DRM_DEBUG("execbuf with invalid ring: %d\n",
864 (int)(args->flags & I915_EXEC_RING_MASK));
865 return -EINVAL;
866 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000867
Chris Wilson72bfa192010-12-19 11:42:05 +0000868 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800869 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +0000870 switch (mode) {
871 case I915_EXEC_CONSTANTS_REL_GENERAL:
872 case I915_EXEC_CONSTANTS_ABSOLUTE:
873 case I915_EXEC_CONSTANTS_REL_SURFACE:
874 if (ring == &dev_priv->ring[RCS] &&
875 mode != dev_priv->relative_constants_mode) {
876 if (INTEL_INFO(dev)->gen < 4)
877 return -EINVAL;
878
879 if (INTEL_INFO(dev)->gen > 5 &&
880 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
881 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800882
883 /* The HW changed the meaning on this bit on gen6 */
884 if (INTEL_INFO(dev)->gen >= 6)
885 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +0000886 }
887 break;
888 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100889 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +0000890 return -EINVAL;
891 }
892
Chris Wilson54cf91d2010-11-25 18:00:26 +0000893 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +0100894 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000895 return -EINVAL;
896 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000897
898 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000899 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +0100900 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000901 return -EINVAL;
902 }
903
Daniel Vetter6ebebc92012-04-26 23:28:11 +0200904 if (INTEL_INFO(dev)->gen >= 5) {
905 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
906 return -EINVAL;
907 }
908
Xi Wang44afb3a2012-04-23 04:06:42 -0400909 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
910 DRM_DEBUG("execbuf with %u cliprects\n",
911 args->num_cliprects);
912 return -EINVAL;
913 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200914
Chris Wilson432e58e2010-11-25 19:32:06 +0000915 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +0000916 GFP_KERNEL);
917 if (cliprects == NULL) {
918 ret = -ENOMEM;
919 goto pre_mutex_err;
920 }
921
Chris Wilson432e58e2010-11-25 19:32:06 +0000922 if (copy_from_user(cliprects,
923 (struct drm_clip_rect __user *)(uintptr_t)
924 args->cliprects_ptr,
925 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000926 ret = -EFAULT;
927 goto pre_mutex_err;
928 }
929 }
930
Chris Wilson54cf91d2010-11-25 18:00:26 +0000931 ret = i915_mutex_lock_interruptible(dev);
932 if (ret)
933 goto pre_mutex_err;
934
935 if (dev_priv->mm.suspended) {
936 mutex_unlock(&dev->struct_mutex);
937 ret = -EBUSY;
938 goto pre_mutex_err;
939 }
940
Chris Wilson67731b82010-12-08 10:38:14 +0000941 eb = eb_create(args->buffer_count);
942 if (eb == NULL) {
943 mutex_unlock(&dev->struct_mutex);
944 ret = -ENOMEM;
945 goto pre_mutex_err;
946 }
947
Chris Wilson54cf91d2010-11-25 18:00:26 +0000948 /* Look up object handles */
Chris Wilson432e58e2010-11-25 19:32:06 +0000949 INIT_LIST_HEAD(&objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000950 for (i = 0; i < args->buffer_count; i++) {
951 struct drm_i915_gem_object *obj;
952
Chris Wilson432e58e2010-11-25 19:32:06 +0000953 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
954 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000955 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100956 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000957 exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000958 /* prevent error path from reading uninitialized data */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000959 ret = -ENOENT;
960 goto err;
961 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000962
Chris Wilson432e58e2010-11-25 19:32:06 +0000963 if (!list_empty(&obj->exec_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100964 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000965 obj, exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000966 ret = -EINVAL;
967 goto err;
968 }
Chris Wilson432e58e2010-11-25 19:32:06 +0000969
970 list_add_tail(&obj->exec_list, &objects);
Chris Wilson67731b82010-12-08 10:38:14 +0000971 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000972 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000973 eb_add_object(eb, obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000974 }
975
Chris Wilson6fe4f142011-01-10 17:35:37 +0000976 /* take note of the batch buffer before we might reorder the lists */
977 batch_obj = list_entry(objects.prev,
978 struct drm_i915_gem_object,
979 exec_list);
980
Chris Wilson54cf91d2010-11-25 18:00:26 +0000981 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilson6fe4f142011-01-10 17:35:37 +0000982 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000983 if (ret)
984 goto err;
985
986 /* The objects are in their final locations, apply the relocations. */
Chris Wilson6fe4f142011-01-10 17:35:37 +0000987 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000988 if (ret) {
989 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +0000990 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilson67731b82010-12-08 10:38:14 +0000991 &objects, eb,
992 exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000993 args->buffer_count);
994 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
995 }
996 if (ret)
997 goto err;
998 }
999
1000 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001001 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001002 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001003 ret = -EINVAL;
1004 goto err;
1005 }
1006 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1007
Chris Wilson432e58e2010-11-25 19:32:06 +00001008 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001009 if (ret)
1010 goto err;
1011
Chris Wilsondb53a302011-02-03 11:57:46 +00001012 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson076e2c02011-01-21 10:07:18 +00001013 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001014 if (seqno < ring->sync_seqno[i]) {
1015 /* The GPU can not handle its semaphore value wrapping,
1016 * so every billion or so execbuffers, we need to stall
1017 * the GPU in order to reset the counters.
1018 */
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001019 ret = i915_gpu_idle(dev);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001020 if (ret)
1021 goto err;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001022 i915_gem_retire_requests(dev);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001023
1024 BUG_ON(ring->sync_seqno[i]);
1025 }
1026 }
1027
Eric Anholt0da5cec2012-07-23 12:33:55 -07001028 ret = i915_switch_context(ring, file, ctx_id);
1029 if (ret)
1030 goto err;
1031
Ben Widawskye2971bd2011-12-12 19:21:57 -08001032 if (ring == &dev_priv->ring[RCS] &&
1033 mode != dev_priv->relative_constants_mode) {
1034 ret = intel_ring_begin(ring, 4);
1035 if (ret)
1036 goto err;
1037
1038 intel_ring_emit(ring, MI_NOOP);
1039 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1040 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001041 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001042 intel_ring_advance(ring);
1043
1044 dev_priv->relative_constants_mode = mode;
1045 }
1046
Eric Anholtae662d32012-01-03 09:23:29 -08001047 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1048 ret = i915_reset_gen7_sol_offsets(dev, ring);
1049 if (ret)
1050 goto err;
1051 }
1052
Chris Wilsondb53a302011-02-03 11:57:46 +00001053 trace_i915_gem_ring_dispatch(ring, seqno);
1054
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001055 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1056 exec_len = args->batch_len;
1057 if (cliprects) {
1058 for (i = 0; i < args->num_cliprects; i++) {
1059 ret = i915_emit_box(dev, &cliprects[i],
1060 args->DR1, args->DR4);
1061 if (ret)
1062 goto err;
1063
1064 ret = ring->dispatch_execbuffer(ring,
1065 exec_start, exec_len);
1066 if (ret)
1067 goto err;
1068 }
1069 } else {
1070 ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
1071 if (ret)
1072 goto err;
1073 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001074
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001075 i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +00001076 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001077
1078err:
Chris Wilson67731b82010-12-08 10:38:14 +00001079 eb_destroy(eb);
Chris Wilson432e58e2010-11-25 19:32:06 +00001080 while (!list_empty(&objects)) {
1081 struct drm_i915_gem_object *obj;
1082
1083 obj = list_first_entry(&objects,
1084 struct drm_i915_gem_object,
1085 exec_list);
1086 list_del_init(&obj->exec_list);
1087 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001088 }
1089
1090 mutex_unlock(&dev->struct_mutex);
1091
1092pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001093 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001094 return ret;
1095}
1096
1097/*
1098 * Legacy execbuffer just creates an exec2 list from the original exec object
1099 * list array and passes it to the real function.
1100 */
1101int
1102i915_gem_execbuffer(struct drm_device *dev, void *data,
1103 struct drm_file *file)
1104{
1105 struct drm_i915_gem_execbuffer *args = data;
1106 struct drm_i915_gem_execbuffer2 exec2;
1107 struct drm_i915_gem_exec_object *exec_list = NULL;
1108 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1109 int ret, i;
1110
Chris Wilson54cf91d2010-11-25 18:00:26 +00001111 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001112 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001113 return -EINVAL;
1114 }
1115
1116 /* Copy in the exec list from userland */
1117 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1118 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1119 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001120 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001121 args->buffer_count);
1122 drm_free_large(exec_list);
1123 drm_free_large(exec2_list);
1124 return -ENOMEM;
1125 }
1126 ret = copy_from_user(exec_list,
1127 (struct drm_i915_relocation_entry __user *)
1128 (uintptr_t) args->buffers_ptr,
1129 sizeof(*exec_list) * args->buffer_count);
1130 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001131 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001132 args->buffer_count, ret);
1133 drm_free_large(exec_list);
1134 drm_free_large(exec2_list);
1135 return -EFAULT;
1136 }
1137
1138 for (i = 0; i < args->buffer_count; i++) {
1139 exec2_list[i].handle = exec_list[i].handle;
1140 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1141 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1142 exec2_list[i].alignment = exec_list[i].alignment;
1143 exec2_list[i].offset = exec_list[i].offset;
1144 if (INTEL_INFO(dev)->gen < 4)
1145 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1146 else
1147 exec2_list[i].flags = 0;
1148 }
1149
1150 exec2.buffers_ptr = args->buffers_ptr;
1151 exec2.buffer_count = args->buffer_count;
1152 exec2.batch_start_offset = args->batch_start_offset;
1153 exec2.batch_len = args->batch_len;
1154 exec2.DR1 = args->DR1;
1155 exec2.DR4 = args->DR4;
1156 exec2.num_cliprects = args->num_cliprects;
1157 exec2.cliprects_ptr = args->cliprects_ptr;
1158 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001159 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001160
1161 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1162 if (!ret) {
1163 /* Copy the new buffer offsets back to the user's exec list. */
1164 for (i = 0; i < args->buffer_count; i++)
1165 exec_list[i].offset = exec2_list[i].offset;
1166 /* ... and back out to userspace */
1167 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1168 (uintptr_t) args->buffers_ptr,
1169 exec_list,
1170 sizeof(*exec_list) * args->buffer_count);
1171 if (ret) {
1172 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001173 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001174 "back to user (%d)\n",
1175 args->buffer_count, ret);
1176 }
1177 }
1178
1179 drm_free_large(exec_list);
1180 drm_free_large(exec2_list);
1181 return ret;
1182}
1183
1184int
1185i915_gem_execbuffer2(struct drm_device *dev, void *data,
1186 struct drm_file *file)
1187{
1188 struct drm_i915_gem_execbuffer2 *args = data;
1189 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1190 int ret;
1191
Xi Wanged8cd3b2012-04-23 04:06:41 -04001192 if (args->buffer_count < 1 ||
1193 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001194 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001195 return -EINVAL;
1196 }
1197
Chris Wilson8408c282011-02-21 12:54:48 +00001198 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1199 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1200 if (exec2_list == NULL)
1201 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1202 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001203 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001204 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001205 args->buffer_count);
1206 return -ENOMEM;
1207 }
1208 ret = copy_from_user(exec2_list,
1209 (struct drm_i915_relocation_entry __user *)
1210 (uintptr_t) args->buffers_ptr,
1211 sizeof(*exec2_list) * args->buffer_count);
1212 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001213 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001214 args->buffer_count, ret);
1215 drm_free_large(exec2_list);
1216 return -EFAULT;
1217 }
1218
1219 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1220 if (!ret) {
1221 /* Copy the new buffer offsets back to the user's exec list. */
1222 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1223 (uintptr_t) args->buffers_ptr,
1224 exec2_list,
1225 sizeof(*exec2_list) * args->buffer_count);
1226 if (ret) {
1227 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001228 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001229 "back to user (%d)\n",
1230 args->buffer_count, ret);
1231 }
1232 }
1233
1234 drm_free_large(exec2_list);
1235 return ret;
1236}