blob: dc87563440f92c8149130bdb030cf63b52ec8bfa [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 ||
Chris Wilson504c7262012-08-23 13:12:52 +010098 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +020099 obj->cache_level != I915_CACHE_NONE);
100}
101
Chris Wilson54cf91d2010-11-25 18:00:26 +0000102static int
103i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000104 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000105 struct drm_i915_gem_relocation_entry *reloc)
106{
107 struct drm_device *dev = obj->base.dev;
108 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100109 struct drm_i915_gem_object *target_i915_obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000110 uint32_t target_offset;
111 int ret = -EINVAL;
112
Chris Wilson67731b82010-12-08 10:38:14 +0000113 /* we've already hold a reference to all valid objects */
114 target_obj = &eb_get_object(eb, reloc->target_handle)->base;
115 if (unlikely(target_obj == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000116 return -ENOENT;
117
Daniel Vetter149c8402012-02-15 23:50:23 +0100118 target_i915_obj = to_intel_bo(target_obj);
119 target_offset = target_i915_obj->gtt_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000120
Eric Anholte844b992012-07-31 15:35:01 -0700121 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
122 * pipe_control writes because the gpu doesn't properly redirect them
123 * through the ppgtt for non_secure batchbuffers. */
124 if (unlikely(IS_GEN6(dev) &&
125 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
126 !target_i915_obj->has_global_gtt_mapping)) {
127 i915_gem_gtt_bind_object(target_i915_obj,
128 target_i915_obj->cache_level);
129 }
130
Chris Wilson54cf91d2010-11-25 18:00:26 +0000131 /* The target buffer should have appeared before us in the
132 * exec_object list, so it should have a GTT space bound by now.
133 */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000134 if (unlikely(target_offset == 0)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100135 DRM_DEBUG("No GTT space found for object %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000136 reloc->target_handle);
Chris Wilson67731b82010-12-08 10:38:14 +0000137 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000138 }
139
140 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000141 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100142 DRM_DEBUG("reloc with multiple write 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 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100151 if (unlikely((reloc->write_domain | reloc->read_domains)
152 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100153 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000154 "obj %p target %d offset %d "
155 "read %08x write %08x",
156 obj, reloc->target_handle,
157 (int) reloc->offset,
158 reloc->read_domains,
159 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000160 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000161 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000162 if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
163 reloc->write_domain != target_obj->pending_write_domain)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100164 DRM_DEBUG("Write domain conflict: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000165 "obj %p target %d offset %d "
166 "new %08x old %08x\n",
167 obj, reloc->target_handle,
168 (int) reloc->offset,
169 reloc->write_domain,
170 target_obj->pending_write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000171 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000172 }
173
174 target_obj->pending_read_domains |= reloc->read_domains;
175 target_obj->pending_write_domain |= reloc->write_domain;
176
177 /* If the relocation already has the right value in it, no
178 * more work needs to be done.
179 */
180 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000181 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000182
183 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000184 if (unlikely(reloc->offset > obj->base.size - 4)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100185 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000186 "obj %p target %d offset %d size %d.\n",
187 obj, reloc->target_handle,
188 (int) reloc->offset,
189 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000190 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000191 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000192 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100193 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000194 "obj %p target %d offset %d.\n",
195 obj, reloc->target_handle,
196 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000197 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000198 }
199
Chris Wilsondabdfe02012-03-26 10:10:27 +0200200 /* We can't wait for rendering with pagefaults disabled */
201 if (obj->active && in_atomic())
202 return -EFAULT;
203
Chris Wilson54cf91d2010-11-25 18:00:26 +0000204 reloc->delta += target_offset;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200205 if (use_cpu_reloc(obj)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000206 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
207 char *vaddr;
208
Chris Wilsondabdfe02012-03-26 10:10:27 +0200209 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
210 if (ret)
211 return ret;
212
Chris Wilson54cf91d2010-11-25 18:00:26 +0000213 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
214 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
215 kunmap_atomic(vaddr);
216 } else {
217 struct drm_i915_private *dev_priv = dev->dev_private;
218 uint32_t __iomem *reloc_entry;
219 void __iomem *reloc_page;
220
Chris Wilson7b096382012-04-14 09:55:51 +0100221 ret = i915_gem_object_set_to_gtt_domain(obj, true);
222 if (ret)
223 return ret;
224
225 ret = i915_gem_object_put_fence(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000226 if (ret)
Chris Wilson67731b82010-12-08 10:38:14 +0000227 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000228
229 /* Map the page containing the relocation we're going to perform. */
230 reloc->offset += obj->gtt_offset;
231 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
232 reloc->offset & PAGE_MASK);
233 reloc_entry = (uint32_t __iomem *)
234 (reloc_page + (reloc->offset & ~PAGE_MASK));
235 iowrite32(reloc->delta, reloc_entry);
236 io_mapping_unmap_atomic(reloc_page);
237 }
238
239 /* and update the user's relocation entry */
240 reloc->presumed_offset = target_offset;
241
Chris Wilson67731b82010-12-08 10:38:14 +0000242 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000243}
244
245static int
246i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000247 struct eb_objects *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000248{
Chris Wilson1d83f442012-03-24 20:12:53 +0000249#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
250 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000251 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000252 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000253 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000254
255 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000256
Chris Wilson1d83f442012-03-24 20:12:53 +0000257 remain = entry->relocation_count;
258 while (remain) {
259 struct drm_i915_gem_relocation_entry *r = stack_reloc;
260 int count = remain;
261 if (count > ARRAY_SIZE(stack_reloc))
262 count = ARRAY_SIZE(stack_reloc);
263 remain -= count;
264
265 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000266 return -EFAULT;
267
Chris Wilson1d83f442012-03-24 20:12:53 +0000268 do {
269 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000270
Chris Wilson1d83f442012-03-24 20:12:53 +0000271 ret = i915_gem_execbuffer_relocate_entry(obj, eb, r);
272 if (ret)
273 return ret;
274
275 if (r->presumed_offset != offset &&
276 __copy_to_user_inatomic(&user_relocs->presumed_offset,
277 &r->presumed_offset,
278 sizeof(r->presumed_offset))) {
279 return -EFAULT;
280 }
281
282 user_relocs++;
283 r++;
284 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000285 }
286
287 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000288#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000289}
290
291static int
292i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000293 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000294 struct drm_i915_gem_relocation_entry *relocs)
295{
Chris Wilson6fe4f142011-01-10 17:35:37 +0000296 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000297 int i, ret;
298
299 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000300 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000301 if (ret)
302 return ret;
303 }
304
305 return 0;
306}
307
308static int
309i915_gem_execbuffer_relocate(struct drm_device *dev,
Chris Wilson67731b82010-12-08 10:38:14 +0000310 struct eb_objects *eb,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000311 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000312{
Chris Wilson432e58e2010-11-25 19:32:06 +0000313 struct drm_i915_gem_object *obj;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000314 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000315
Chris Wilsond4aeee72011-03-14 15:11:24 +0000316 /* This is the fast path and we cannot handle a pagefault whilst
317 * holding the struct mutex lest the user pass in the relocations
318 * contained within a mmaped bo. For in such a case we, the page
319 * fault handler would call i915_gem_fault() and we would try to
320 * acquire the struct mutex again. Obviously this is bad and so
321 * lockdep complains vehemently.
322 */
323 pagefault_disable();
Chris Wilson432e58e2010-11-25 19:32:06 +0000324 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000325 ret = i915_gem_execbuffer_relocate_object(obj, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000326 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000327 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000328 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000329 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000330
Chris Wilsond4aeee72011-03-14 15:11:24 +0000331 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000332}
333
Chris Wilson1690e1e2011-12-14 13:57:08 +0100334#define __EXEC_OBJECT_HAS_FENCE (1<<31)
335
336static int
Chris Wilsondabdfe02012-03-26 10:10:27 +0200337need_reloc_mappable(struct drm_i915_gem_object *obj)
338{
339 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
340 return entry->relocation_count && !use_cpu_reloc(obj);
341}
342
343static int
Chris Wilson1690e1e2011-12-14 13:57:08 +0100344pin_and_fence_object(struct drm_i915_gem_object *obj,
345 struct intel_ring_buffer *ring)
346{
347 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
348 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
349 bool need_fence, need_mappable;
350 int ret;
351
352 need_fence =
353 has_fenced_gpu_access &&
354 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
355 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200356 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100357
Chris Wilson86a1ee22012-08-11 15:41:04 +0100358 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable, false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100359 if (ret)
360 return ret;
361
362 if (has_fenced_gpu_access) {
363 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100364 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000365 if (ret)
366 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100367
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000368 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100369 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000370
Chris Wilson7dd49062012-03-21 10:48:18 +0000371 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100372 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100373 }
374
375 entry->offset = obj->gtt_offset;
376 return 0;
377
378err_unpin:
379 i915_gem_object_unpin(obj);
380 return ret;
381}
382
Chris Wilson54cf91d2010-11-25 18:00:26 +0000383static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000384i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000385 struct drm_file *file,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000386 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000387{
Daniel Vetter7bddb012012-02-09 17:15:47 +0100388 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000389 struct drm_i915_gem_object *obj;
Chris Wilson432e58e2010-11-25 19:32:06 +0000390 int ret, retry;
Chris Wilson9b3826b2010-12-05 17:11:54 +0000391 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000392 struct list_head ordered_objects;
393
394 INIT_LIST_HEAD(&ordered_objects);
395 while (!list_empty(objects)) {
396 struct drm_i915_gem_exec_object2 *entry;
397 bool need_fence, need_mappable;
398
399 obj = list_first_entry(objects,
400 struct drm_i915_gem_object,
401 exec_list);
402 entry = obj->exec_entry;
403
404 need_fence =
405 has_fenced_gpu_access &&
406 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
407 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200408 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000409
410 if (need_mappable)
411 list_move(&obj->exec_list, &ordered_objects);
412 else
413 list_move_tail(&obj->exec_list, &ordered_objects);
Chris Wilson595dad72011-01-13 11:03:48 +0000414
415 obj->base.pending_read_domains = 0;
416 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100417 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000418 }
419 list_splice(&ordered_objects, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000420
421 /* Attempt to pin all of the buffers into the GTT.
422 * This is done in 3 phases:
423 *
424 * 1a. Unbind all objects that do not match the GTT constraints for
425 * the execbuffer (fenceable, mappable, alignment etc).
426 * 1b. Increment pin count for already bound objects.
427 * 2. Bind new objects.
428 * 3. Decrement pin count.
429 *
430 * This avoid unnecessary unbinding of later objects in order to makr
431 * room for the earlier objects *unless* we need to defragment.
432 */
433 retry = 0;
434 do {
435 ret = 0;
436
437 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000438 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000439 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000440 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100441
Chris Wilson6fe4f142011-01-10 17:35:37 +0000442 if (!obj->gtt_space)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000443 continue;
444
445 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000446 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000447 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
448 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200449 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000450
451 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
452 (need_mappable && !obj->map_and_fenceable))
453 ret = i915_gem_object_unbind(obj);
454 else
Chris Wilson1690e1e2011-12-14 13:57:08 +0100455 ret = pin_and_fence_object(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000456 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000457 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000458 }
459
460 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000461 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100462 if (obj->gtt_space)
463 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000464
Chris Wilson1690e1e2011-12-14 13:57:08 +0100465 ret = pin_and_fence_object(obj, ring);
466 if (ret) {
467 int ret_ignore;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000468
Chris Wilson1690e1e2011-12-14 13:57:08 +0100469 /* This can potentially raise a harmless
470 * -EINVAL if we failed to bind in the above
471 * call. It cannot raise -EINTR since we know
472 * that the bo is freshly bound and so will
473 * not need to be flushed or waited upon.
474 */
475 ret_ignore = i915_gem_object_unbind(obj);
476 (void)ret_ignore;
477 WARN_ON(obj->gtt_space);
478 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000479 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000480 }
481
Chris Wilson432e58e2010-11-25 19:32:06 +0000482 /* Decrement pin count for bound objects */
483 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100484 struct drm_i915_gem_exec_object2 *entry;
485
486 if (!obj->gtt_space)
487 continue;
488
489 entry = obj->exec_entry;
490 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
491 i915_gem_object_unpin_fence(obj);
492 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
493 }
494
495 i915_gem_object_unpin(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100496
497 /* ... and ensure ppgtt mapping exist if needed. */
498 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
499 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
500 obj, obj->cache_level);
501
502 obj->has_aliasing_ppgtt_mapping = 1;
503 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000504 }
505
Chris Wilson6c085a72012-08-20 11:40:46 +0200506 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000507 return ret;
508
Chris Wilson6c085a72012-08-20 11:40:46 +0200509 ret = i915_gem_evict_everything(ring->dev);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000510 if (ret)
511 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000512 } while (1);
Chris Wilson432e58e2010-11-25 19:32:06 +0000513
514err:
Chris Wilson1690e1e2011-12-14 13:57:08 +0100515 list_for_each_entry_continue_reverse(obj, objects, exec_list) {
516 struct drm_i915_gem_exec_object2 *entry;
Chris Wilson432e58e2010-11-25 19:32:06 +0000517
Chris Wilson1690e1e2011-12-14 13:57:08 +0100518 if (!obj->gtt_space)
519 continue;
520
521 entry = obj->exec_entry;
522 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
523 i915_gem_object_unpin_fence(obj);
524 entry->flags &= ~__EXEC_OBJECT_HAS_FENCE;
525 }
526
527 i915_gem_object_unpin(obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000528 }
529
530 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000531}
532
533static int
534i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
535 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000536 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000537 struct list_head *objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000538 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000539 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000540 int count)
541{
542 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000543 struct drm_i915_gem_object *obj;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000544 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000545 int i, total, ret;
546
Chris Wilson67731b82010-12-08 10:38:14 +0000547 /* We may process another execbuffer during the unlock... */
Chris Wilson36cf1742011-01-10 12:09:12 +0000548 while (!list_empty(objects)) {
Chris Wilson67731b82010-12-08 10:38:14 +0000549 obj = list_first_entry(objects,
550 struct drm_i915_gem_object,
551 exec_list);
552 list_del_init(&obj->exec_list);
553 drm_gem_object_unreference(&obj->base);
554 }
555
Chris Wilson54cf91d2010-11-25 18:00:26 +0000556 mutex_unlock(&dev->struct_mutex);
557
558 total = 0;
559 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000560 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000561
Chris Wilsondd6864a2011-01-12 23:49:13 +0000562 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000563 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000564 if (reloc == NULL || reloc_offset == NULL) {
565 drm_free_large(reloc);
566 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000567 mutex_lock(&dev->struct_mutex);
568 return -ENOMEM;
569 }
570
571 total = 0;
572 for (i = 0; i < count; i++) {
573 struct drm_i915_gem_relocation_entry __user *user_relocs;
574
Chris Wilson432e58e2010-11-25 19:32:06 +0000575 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000576
577 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000578 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000579 ret = -EFAULT;
580 mutex_lock(&dev->struct_mutex);
581 goto err;
582 }
583
Chris Wilsondd6864a2011-01-12 23:49:13 +0000584 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000585 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000586 }
587
588 ret = i915_mutex_lock_interruptible(dev);
589 if (ret) {
590 mutex_lock(&dev->struct_mutex);
591 goto err;
592 }
593
Chris Wilson67731b82010-12-08 10:38:14 +0000594 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000595 eb_reset(eb);
596 for (i = 0; i < count; i++) {
Chris Wilson67731b82010-12-08 10:38:14 +0000597 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
598 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000599 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100600 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson67731b82010-12-08 10:38:14 +0000601 exec[i].handle, i);
602 ret = -ENOENT;
603 goto err;
604 }
605
606 list_add_tail(&obj->exec_list, objects);
607 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000608 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000609 eb_add_object(eb, obj);
610 }
611
Chris Wilson6fe4f142011-01-10 17:35:37 +0000612 ret = i915_gem_execbuffer_reserve(ring, file, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000613 if (ret)
614 goto err;
615
Chris Wilson432e58e2010-11-25 19:32:06 +0000616 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondd6864a2011-01-12 23:49:13 +0000617 int offset = obj->exec_entry - exec;
Chris Wilson67731b82010-12-08 10:38:14 +0000618 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilsondd6864a2011-01-12 23:49:13 +0000619 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000620 if (ret)
621 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000622 }
623
624 /* Leave the user relocations as are, this is the painfully slow path,
625 * and we want to avoid the complication of dropping the lock whilst
626 * having buffers reserved in the aperture and so causing spurious
627 * ENOSPC for random operations.
628 */
629
630err:
631 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000632 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000633 return ret;
634}
635
Chris Wilson54cf91d2010-11-25 18:00:26 +0000636static int
Chris Wilsonc59a3332011-03-06 13:51:29 +0000637i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
638{
639 u32 plane, flip_mask;
640 int ret;
641
642 /* Check for any pending flips. As we only maintain a flip queue depth
643 * of 1, we can simply insert a WAIT for the next display flip prior
644 * to executing the batch and avoid stalling the CPU.
645 */
646
647 for (plane = 0; flips >> plane; plane++) {
648 if (((flips >> plane) & 1) == 0)
649 continue;
650
651 if (plane)
652 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
653 else
654 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
655
656 ret = intel_ring_begin(ring, 2);
657 if (ret)
658 return ret;
659
660 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
661 intel_ring_emit(ring, MI_NOOP);
662 intel_ring_advance(ring);
663 }
664
665 return 0;
666}
667
Chris Wilsonc59a3332011-03-06 13:51:29 +0000668static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000669i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
670 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000671{
Chris Wilson432e58e2010-11-25 19:32:06 +0000672 struct drm_i915_gem_object *obj;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200673 uint32_t flush_domains = 0;
674 uint32_t flips = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000675 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000676
Chris Wilson432e58e2010-11-25 19:32:06 +0000677 list_for_each_entry(obj, objects, exec_list) {
Ben Widawsky2911a352012-04-05 14:47:36 -0700678 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000679 if (ret)
680 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200681
682 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
683 i915_gem_clflush_object(obj);
684
685 if (obj->base.pending_write_domain)
686 flips |= atomic_read(&obj->pending_flip);
687
688 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000689 }
690
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200691 if (flips) {
692 ret = i915_gem_execbuffer_wait_for_flips(ring, flips);
693 if (ret)
694 return ret;
695 }
696
697 if (flush_domains & I915_GEM_DOMAIN_CPU)
698 intel_gtt_chipset_flush();
699
700 if (flush_domains & I915_GEM_DOMAIN_GTT)
701 wmb();
702
Chris Wilson09cf7c92012-07-13 14:14:08 +0100703 /* Unconditionally invalidate gpu caches and ensure that we do flush
704 * any residual writes from the previous batch.
705 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100706 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000707}
708
Chris Wilson432e58e2010-11-25 19:32:06 +0000709static bool
710i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000711{
Chris Wilson432e58e2010-11-25 19:32:06 +0000712 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000713}
714
715static int
716validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
717 int count)
718{
719 int i;
720
721 for (i = 0; i < count; i++) {
722 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
723 int length; /* limited by fault_in_pages_readable() */
724
725 /* First check for malicious input causing overflow */
726 if (exec[i].relocation_count >
727 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
728 return -EINVAL;
729
730 length = exec[i].relocation_count *
731 sizeof(struct drm_i915_gem_relocation_entry);
732 if (!access_ok(VERIFY_READ, ptr, length))
733 return -EFAULT;
734
735 /* we may also need to update the presumed offsets */
736 if (!access_ok(VERIFY_WRITE, ptr, length))
737 return -EFAULT;
738
Daniel Vetterf56f8212012-03-25 19:47:41 +0200739 if (fault_in_multipages_readable(ptr, length))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000740 return -EFAULT;
741 }
742
743 return 0;
744}
745
Chris Wilson432e58e2010-11-25 19:32:06 +0000746static void
747i915_gem_execbuffer_move_to_active(struct list_head *objects,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000748 struct intel_ring_buffer *ring,
749 u32 seqno)
Chris Wilson432e58e2010-11-25 19:32:06 +0000750{
751 struct drm_i915_gem_object *obj;
752
753 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson69c2fc82012-07-20 12:41:03 +0100754 u32 old_read = obj->base.read_domains;
755 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000756
Chris Wilson432e58e2010-11-25 19:32:06 +0000757 obj->base.read_domains = obj->base.pending_read_domains;
758 obj->base.write_domain = obj->base.pending_write_domain;
759 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
760
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000761 i915_gem_object_move_to_active(obj, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +0000762 if (obj->base.write_domain) {
763 obj->dirty = 1;
Chris Wilson0201f1e2012-07-20 12:41:01 +0100764 obj->last_write_seqno = seqno;
Chris Wilsonacb87df2012-05-03 15:47:57 +0100765 if (obj->pin_count) /* check for potential scanout */
Chris Wilsonf047e392012-07-21 12:31:41 +0100766 intel_mark_fb_busy(obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000767 }
768
Chris Wilsondb53a302011-02-03 11:57:46 +0000769 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000770 }
771}
772
Chris Wilson54cf91d2010-11-25 18:00:26 +0000773static void
774i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000775 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000776 struct intel_ring_buffer *ring)
777{
Daniel Vettercc889e02012-06-13 20:45:19 +0200778 /* Unconditionally force add_request to emit a full flush. */
779 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000780
Chris Wilson432e58e2010-11-25 19:32:06 +0000781 /* Add a breadcrumb for the completion of the batch buffer */
Chris Wilson3bb73ab2012-07-20 12:40:59 +0100782 (void)i915_add_request(ring, file, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000783}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000784
785static int
Eric Anholtae662d32012-01-03 09:23:29 -0800786i915_reset_gen7_sol_offsets(struct drm_device *dev,
787 struct intel_ring_buffer *ring)
788{
789 drm_i915_private_t *dev_priv = dev->dev_private;
790 int ret, i;
791
792 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
793 return 0;
794
795 ret = intel_ring_begin(ring, 4 * 3);
796 if (ret)
797 return ret;
798
799 for (i = 0; i < 4; i++) {
800 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
801 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
802 intel_ring_emit(ring, 0);
803 }
804
805 intel_ring_advance(ring);
806
807 return 0;
808}
809
810static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000811i915_gem_do_execbuffer(struct drm_device *dev, void *data,
812 struct drm_file *file,
813 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +0000814 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000815{
816 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000817 struct list_head objects;
Chris Wilson67731b82010-12-08 10:38:14 +0000818 struct eb_objects *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000819 struct drm_i915_gem_object *batch_obj;
820 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000821 struct intel_ring_buffer *ring;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700822 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000823 u32 exec_start, exec_len;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000824 u32 seqno;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800825 u32 mask;
Chris Wilson72bfa192010-12-19 11:42:05 +0000826 int ret, mode, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000827
Chris Wilson432e58e2010-11-25 19:32:06 +0000828 if (!i915_gem_check_execbuffer(args)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100829 DRM_DEBUG("execbuf with invalid offset/length\n");
Chris Wilson432e58e2010-11-25 19:32:06 +0000830 return -EINVAL;
831 }
832
833 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000834 if (ret)
835 return ret;
836
Chris Wilson54cf91d2010-11-25 18:00:26 +0000837 switch (args->flags & I915_EXEC_RING_MASK) {
838 case I915_EXEC_DEFAULT:
839 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000840 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000841 break;
842 case I915_EXEC_BSD:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000843 ring = &dev_priv->ring[VCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700844 if (ctx_id != 0) {
845 DRM_DEBUG("Ring %s doesn't support contexts\n",
846 ring->name);
847 return -EPERM;
848 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000849 break;
850 case I915_EXEC_BLT:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000851 ring = &dev_priv->ring[BCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700852 if (ctx_id != 0) {
853 DRM_DEBUG("Ring %s doesn't support contexts\n",
854 ring->name);
855 return -EPERM;
856 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000857 break;
858 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100859 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000860 (int)(args->flags & I915_EXEC_RING_MASK));
861 return -EINVAL;
862 }
Chris Wilsona15817c2012-05-11 14:29:31 +0100863 if (!intel_ring_initialized(ring)) {
864 DRM_DEBUG("execbuf with invalid ring: %d\n",
865 (int)(args->flags & I915_EXEC_RING_MASK));
866 return -EINVAL;
867 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000868
Chris Wilson72bfa192010-12-19 11:42:05 +0000869 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800870 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +0000871 switch (mode) {
872 case I915_EXEC_CONSTANTS_REL_GENERAL:
873 case I915_EXEC_CONSTANTS_ABSOLUTE:
874 case I915_EXEC_CONSTANTS_REL_SURFACE:
875 if (ring == &dev_priv->ring[RCS] &&
876 mode != dev_priv->relative_constants_mode) {
877 if (INTEL_INFO(dev)->gen < 4)
878 return -EINVAL;
879
880 if (INTEL_INFO(dev)->gen > 5 &&
881 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
882 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800883
884 /* The HW changed the meaning on this bit on gen6 */
885 if (INTEL_INFO(dev)->gen >= 6)
886 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +0000887 }
888 break;
889 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100890 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +0000891 return -EINVAL;
892 }
893
Chris Wilson54cf91d2010-11-25 18:00:26 +0000894 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +0100895 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000896 return -EINVAL;
897 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000898
899 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000900 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +0100901 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000902 return -EINVAL;
903 }
904
Daniel Vetter6ebebc92012-04-26 23:28:11 +0200905 if (INTEL_INFO(dev)->gen >= 5) {
906 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
907 return -EINVAL;
908 }
909
Xi Wang44afb3a2012-04-23 04:06:42 -0400910 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
911 DRM_DEBUG("execbuf with %u cliprects\n",
912 args->num_cliprects);
913 return -EINVAL;
914 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200915
Chris Wilson432e58e2010-11-25 19:32:06 +0000916 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +0000917 GFP_KERNEL);
918 if (cliprects == NULL) {
919 ret = -ENOMEM;
920 goto pre_mutex_err;
921 }
922
Chris Wilson432e58e2010-11-25 19:32:06 +0000923 if (copy_from_user(cliprects,
924 (struct drm_clip_rect __user *)(uintptr_t)
925 args->cliprects_ptr,
926 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000927 ret = -EFAULT;
928 goto pre_mutex_err;
929 }
930 }
931
Chris Wilson54cf91d2010-11-25 18:00:26 +0000932 ret = i915_mutex_lock_interruptible(dev);
933 if (ret)
934 goto pre_mutex_err;
935
936 if (dev_priv->mm.suspended) {
937 mutex_unlock(&dev->struct_mutex);
938 ret = -EBUSY;
939 goto pre_mutex_err;
940 }
941
Chris Wilson67731b82010-12-08 10:38:14 +0000942 eb = eb_create(args->buffer_count);
943 if (eb == NULL) {
944 mutex_unlock(&dev->struct_mutex);
945 ret = -ENOMEM;
946 goto pre_mutex_err;
947 }
948
Chris Wilson54cf91d2010-11-25 18:00:26 +0000949 /* Look up object handles */
Chris Wilson432e58e2010-11-25 19:32:06 +0000950 INIT_LIST_HEAD(&objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000951 for (i = 0; i < args->buffer_count; i++) {
952 struct drm_i915_gem_object *obj;
953
Chris Wilson432e58e2010-11-25 19:32:06 +0000954 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
955 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000956 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100957 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000958 exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000959 /* prevent error path from reading uninitialized data */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000960 ret = -ENOENT;
961 goto err;
962 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000963
Chris Wilson432e58e2010-11-25 19:32:06 +0000964 if (!list_empty(&obj->exec_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100965 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000966 obj, exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000967 ret = -EINVAL;
968 goto err;
969 }
Chris Wilson432e58e2010-11-25 19:32:06 +0000970
971 list_add_tail(&obj->exec_list, &objects);
Chris Wilson67731b82010-12-08 10:38:14 +0000972 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000973 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000974 eb_add_object(eb, obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000975 }
976
Chris Wilson6fe4f142011-01-10 17:35:37 +0000977 /* take note of the batch buffer before we might reorder the lists */
978 batch_obj = list_entry(objects.prev,
979 struct drm_i915_gem_object,
980 exec_list);
981
Chris Wilson54cf91d2010-11-25 18:00:26 +0000982 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilson6fe4f142011-01-10 17:35:37 +0000983 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000984 if (ret)
985 goto err;
986
987 /* The objects are in their final locations, apply the relocations. */
Chris Wilson6fe4f142011-01-10 17:35:37 +0000988 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000989 if (ret) {
990 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +0000991 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilson67731b82010-12-08 10:38:14 +0000992 &objects, eb,
993 exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000994 args->buffer_count);
995 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
996 }
997 if (ret)
998 goto err;
999 }
1000
1001 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001002 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001003 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001004 ret = -EINVAL;
1005 goto err;
1006 }
1007 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1008
Chris Wilson432e58e2010-11-25 19:32:06 +00001009 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001010 if (ret)
1011 goto err;
1012
Chris Wilsondb53a302011-02-03 11:57:46 +00001013 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson076e2c02011-01-21 10:07:18 +00001014 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001015 if (seqno < ring->sync_seqno[i]) {
1016 /* The GPU can not handle its semaphore value wrapping,
1017 * so every billion or so execbuffers, we need to stall
1018 * the GPU in order to reset the counters.
1019 */
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001020 ret = i915_gpu_idle(dev);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001021 if (ret)
1022 goto err;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001023 i915_gem_retire_requests(dev);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001024
1025 BUG_ON(ring->sync_seqno[i]);
1026 }
1027 }
1028
Eric Anholt0da5cec2012-07-23 12:33:55 -07001029 ret = i915_switch_context(ring, file, ctx_id);
1030 if (ret)
1031 goto err;
1032
Ben Widawskye2971bd2011-12-12 19:21:57 -08001033 if (ring == &dev_priv->ring[RCS] &&
1034 mode != dev_priv->relative_constants_mode) {
1035 ret = intel_ring_begin(ring, 4);
1036 if (ret)
1037 goto err;
1038
1039 intel_ring_emit(ring, MI_NOOP);
1040 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1041 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001042 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001043 intel_ring_advance(ring);
1044
1045 dev_priv->relative_constants_mode = mode;
1046 }
1047
Eric Anholtae662d32012-01-03 09:23:29 -08001048 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1049 ret = i915_reset_gen7_sol_offsets(dev, ring);
1050 if (ret)
1051 goto err;
1052 }
1053
Chris Wilsondb53a302011-02-03 11:57:46 +00001054 trace_i915_gem_ring_dispatch(ring, seqno);
1055
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001056 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1057 exec_len = args->batch_len;
1058 if (cliprects) {
1059 for (i = 0; i < args->num_cliprects; i++) {
1060 ret = i915_emit_box(dev, &cliprects[i],
1061 args->DR1, args->DR4);
1062 if (ret)
1063 goto err;
1064
1065 ret = ring->dispatch_execbuffer(ring,
1066 exec_start, exec_len);
1067 if (ret)
1068 goto err;
1069 }
1070 } else {
1071 ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
1072 if (ret)
1073 goto err;
1074 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001075
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001076 i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +00001077 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001078
1079err:
Chris Wilson67731b82010-12-08 10:38:14 +00001080 eb_destroy(eb);
Chris Wilson432e58e2010-11-25 19:32:06 +00001081 while (!list_empty(&objects)) {
1082 struct drm_i915_gem_object *obj;
1083
1084 obj = list_first_entry(&objects,
1085 struct drm_i915_gem_object,
1086 exec_list);
1087 list_del_init(&obj->exec_list);
1088 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001089 }
1090
1091 mutex_unlock(&dev->struct_mutex);
1092
1093pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001094 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001095 return ret;
1096}
1097
1098/*
1099 * Legacy execbuffer just creates an exec2 list from the original exec object
1100 * list array and passes it to the real function.
1101 */
1102int
1103i915_gem_execbuffer(struct drm_device *dev, void *data,
1104 struct drm_file *file)
1105{
1106 struct drm_i915_gem_execbuffer *args = data;
1107 struct drm_i915_gem_execbuffer2 exec2;
1108 struct drm_i915_gem_exec_object *exec_list = NULL;
1109 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1110 int ret, i;
1111
Chris Wilson54cf91d2010-11-25 18:00:26 +00001112 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001113 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001114 return -EINVAL;
1115 }
1116
1117 /* Copy in the exec list from userland */
1118 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1119 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1120 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001121 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001122 args->buffer_count);
1123 drm_free_large(exec_list);
1124 drm_free_large(exec2_list);
1125 return -ENOMEM;
1126 }
1127 ret = copy_from_user(exec_list,
1128 (struct drm_i915_relocation_entry __user *)
1129 (uintptr_t) args->buffers_ptr,
1130 sizeof(*exec_list) * args->buffer_count);
1131 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001132 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001133 args->buffer_count, ret);
1134 drm_free_large(exec_list);
1135 drm_free_large(exec2_list);
1136 return -EFAULT;
1137 }
1138
1139 for (i = 0; i < args->buffer_count; i++) {
1140 exec2_list[i].handle = exec_list[i].handle;
1141 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1142 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1143 exec2_list[i].alignment = exec_list[i].alignment;
1144 exec2_list[i].offset = exec_list[i].offset;
1145 if (INTEL_INFO(dev)->gen < 4)
1146 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1147 else
1148 exec2_list[i].flags = 0;
1149 }
1150
1151 exec2.buffers_ptr = args->buffers_ptr;
1152 exec2.buffer_count = args->buffer_count;
1153 exec2.batch_start_offset = args->batch_start_offset;
1154 exec2.batch_len = args->batch_len;
1155 exec2.DR1 = args->DR1;
1156 exec2.DR4 = args->DR4;
1157 exec2.num_cliprects = args->num_cliprects;
1158 exec2.cliprects_ptr = args->cliprects_ptr;
1159 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001160 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001161
1162 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1163 if (!ret) {
1164 /* Copy the new buffer offsets back to the user's exec list. */
1165 for (i = 0; i < args->buffer_count; i++)
1166 exec_list[i].offset = exec2_list[i].offset;
1167 /* ... and back out to userspace */
1168 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1169 (uintptr_t) args->buffers_ptr,
1170 exec_list,
1171 sizeof(*exec_list) * args->buffer_count);
1172 if (ret) {
1173 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001174 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001175 "back to user (%d)\n",
1176 args->buffer_count, ret);
1177 }
1178 }
1179
1180 drm_free_large(exec_list);
1181 drm_free_large(exec2_list);
1182 return ret;
1183}
1184
1185int
1186i915_gem_execbuffer2(struct drm_device *dev, void *data,
1187 struct drm_file *file)
1188{
1189 struct drm_i915_gem_execbuffer2 *args = data;
1190 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1191 int ret;
1192
Xi Wanged8cd3b2012-04-23 04:06:41 -04001193 if (args->buffer_count < 1 ||
1194 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001195 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001196 return -EINVAL;
1197 }
1198
Chris Wilson8408c282011-02-21 12:54:48 +00001199 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1200 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1201 if (exec2_list == NULL)
1202 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1203 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001204 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001205 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001206 args->buffer_count);
1207 return -ENOMEM;
1208 }
1209 ret = copy_from_user(exec2_list,
1210 (struct drm_i915_relocation_entry __user *)
1211 (uintptr_t) args->buffers_ptr,
1212 sizeof(*exec2_list) * args->buffer_count);
1213 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001214 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001215 args->buffer_count, ret);
1216 drm_free_large(exec2_list);
1217 return -EFAULT;
1218 }
1219
1220 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1221 if (!ret) {
1222 /* Copy the new buffer offsets back to the user's exec list. */
1223 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1224 (uintptr_t) args->buffers_ptr,
1225 exec2_list,
1226 sizeof(*exec2_list) * args->buffer_count);
1227 if (ret) {
1228 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001229 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001230 "back to user (%d)\n",
1231 args->buffer_count, ret);
1232 }
1233 }
1234
1235 drm_free_large(exec2_list);
1236 return ret;
1237}