blob: e6b2205ecf6d4b87e6f78574f3de1bd997dcdb72 [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 Wilson7788a762012-08-24 19:18:18 +0100334#define __EXEC_OBJECT_HAS_PIN (1<<31)
335#define __EXEC_OBJECT_HAS_FENCE (1<<30)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100336
337static int
Chris Wilsondabdfe02012-03-26 10:10:27 +0200338need_reloc_mappable(struct drm_i915_gem_object *obj)
339{
340 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
341 return entry->relocation_count && !use_cpu_reloc(obj);
342}
343
344static int
Chris Wilson7788a762012-08-24 19:18:18 +0100345i915_gem_execbuffer_reserve_object(struct drm_i915_gem_object *obj,
346 struct intel_ring_buffer *ring)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100347{
Chris Wilson7788a762012-08-24 19:18:18 +0100348 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100349 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
350 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
351 bool need_fence, need_mappable;
352 int ret;
353
354 need_fence =
355 has_fenced_gpu_access &&
356 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
357 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200358 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100359
Chris Wilson86a1ee22012-08-11 15:41:04 +0100360 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable, false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100361 if (ret)
362 return ret;
363
Chris Wilson7788a762012-08-24 19:18:18 +0100364 entry->flags |= __EXEC_OBJECT_HAS_PIN;
365
Chris Wilson1690e1e2011-12-14 13:57:08 +0100366 if (has_fenced_gpu_access) {
367 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100368 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000369 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100370 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100371
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000372 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100373 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000374
Chris Wilson7dd49062012-03-21 10:48:18 +0000375 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100376 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100377 }
378
Chris Wilson7788a762012-08-24 19:18:18 +0100379 /* Ensure ppgtt mapping exists if needed */
380 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
381 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
382 obj, obj->cache_level);
383
384 obj->has_aliasing_ppgtt_mapping = 1;
385 }
386
Chris Wilson1690e1e2011-12-14 13:57:08 +0100387 entry->offset = obj->gtt_offset;
388 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100389}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100390
Chris Wilson7788a762012-08-24 19:18:18 +0100391static void
392i915_gem_execbuffer_unreserve_object(struct drm_i915_gem_object *obj)
393{
394 struct drm_i915_gem_exec_object2 *entry;
395
396 if (!obj->gtt_space)
397 return;
398
399 entry = obj->exec_entry;
400
401 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
402 i915_gem_object_unpin_fence(obj);
403
404 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
405 i915_gem_object_unpin(obj);
406
407 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100408}
409
Chris Wilson54cf91d2010-11-25 18:00:26 +0000410static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000411i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000412 struct drm_file *file,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000413 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000414{
Chris Wilson432e58e2010-11-25 19:32:06 +0000415 struct drm_i915_gem_object *obj;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000416 struct list_head ordered_objects;
Chris Wilson7788a762012-08-24 19:18:18 +0100417 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
418 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000419
420 INIT_LIST_HEAD(&ordered_objects);
421 while (!list_empty(objects)) {
422 struct drm_i915_gem_exec_object2 *entry;
423 bool need_fence, need_mappable;
424
425 obj = list_first_entry(objects,
426 struct drm_i915_gem_object,
427 exec_list);
428 entry = obj->exec_entry;
429
430 need_fence =
431 has_fenced_gpu_access &&
432 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
433 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200434 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000435
436 if (need_mappable)
437 list_move(&obj->exec_list, &ordered_objects);
438 else
439 list_move_tail(&obj->exec_list, &ordered_objects);
Chris Wilson595dad72011-01-13 11:03:48 +0000440
441 obj->base.pending_read_domains = 0;
442 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100443 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000444 }
445 list_splice(&ordered_objects, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000446
447 /* Attempt to pin all of the buffers into the GTT.
448 * This is done in 3 phases:
449 *
450 * 1a. Unbind all objects that do not match the GTT constraints for
451 * the execbuffer (fenceable, mappable, alignment etc).
452 * 1b. Increment pin count for already bound objects.
453 * 2. Bind new objects.
454 * 3. Decrement pin count.
455 *
Chris Wilson7788a762012-08-24 19:18:18 +0100456 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000457 * room for the earlier objects *unless* we need to defragment.
458 */
459 retry = 0;
460 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100461 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000462
463 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000464 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000465 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000466 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100467
Chris Wilson6fe4f142011-01-10 17:35:37 +0000468 if (!obj->gtt_space)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000469 continue;
470
471 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000472 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000473 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
474 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200475 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000476
477 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
478 (need_mappable && !obj->map_and_fenceable))
479 ret = i915_gem_object_unbind(obj);
480 else
Chris Wilson7788a762012-08-24 19:18:18 +0100481 ret = i915_gem_execbuffer_reserve_object(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000482 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000483 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000484 }
485
486 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000487 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100488 if (obj->gtt_space)
489 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000490
Chris Wilson7788a762012-08-24 19:18:18 +0100491 ret = i915_gem_execbuffer_reserve_object(obj, ring);
492 if (ret)
493 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000494 }
495
Chris Wilson7788a762012-08-24 19:18:18 +0100496err: /* Decrement pin count for bound objects */
497 list_for_each_entry(obj, objects, exec_list)
498 i915_gem_execbuffer_unreserve_object(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000499
Chris Wilson6c085a72012-08-20 11:40:46 +0200500 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000501 return ret;
502
Chris Wilson6c085a72012-08-20 11:40:46 +0200503 ret = i915_gem_evict_everything(ring->dev);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000504 if (ret)
505 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000506 } while (1);
507}
508
509static int
510i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
511 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000512 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000513 struct list_head *objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000514 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000515 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000516 int count)
517{
518 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000519 struct drm_i915_gem_object *obj;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000520 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000521 int i, total, ret;
522
Chris Wilson67731b82010-12-08 10:38:14 +0000523 /* We may process another execbuffer during the unlock... */
Chris Wilson36cf1742011-01-10 12:09:12 +0000524 while (!list_empty(objects)) {
Chris Wilson67731b82010-12-08 10:38:14 +0000525 obj = list_first_entry(objects,
526 struct drm_i915_gem_object,
527 exec_list);
528 list_del_init(&obj->exec_list);
529 drm_gem_object_unreference(&obj->base);
530 }
531
Chris Wilson54cf91d2010-11-25 18:00:26 +0000532 mutex_unlock(&dev->struct_mutex);
533
534 total = 0;
535 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000536 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000537
Chris Wilsondd6864a2011-01-12 23:49:13 +0000538 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000539 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000540 if (reloc == NULL || reloc_offset == NULL) {
541 drm_free_large(reloc);
542 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000543 mutex_lock(&dev->struct_mutex);
544 return -ENOMEM;
545 }
546
547 total = 0;
548 for (i = 0; i < count; i++) {
549 struct drm_i915_gem_relocation_entry __user *user_relocs;
550
Chris Wilson432e58e2010-11-25 19:32:06 +0000551 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000552
553 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000554 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000555 ret = -EFAULT;
556 mutex_lock(&dev->struct_mutex);
557 goto err;
558 }
559
Chris Wilsondd6864a2011-01-12 23:49:13 +0000560 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000561 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000562 }
563
564 ret = i915_mutex_lock_interruptible(dev);
565 if (ret) {
566 mutex_lock(&dev->struct_mutex);
567 goto err;
568 }
569
Chris Wilson67731b82010-12-08 10:38:14 +0000570 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000571 eb_reset(eb);
572 for (i = 0; i < count; i++) {
Chris Wilson67731b82010-12-08 10:38:14 +0000573 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
574 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000575 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100576 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson67731b82010-12-08 10:38:14 +0000577 exec[i].handle, i);
578 ret = -ENOENT;
579 goto err;
580 }
581
582 list_add_tail(&obj->exec_list, objects);
583 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000584 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000585 eb_add_object(eb, obj);
586 }
587
Chris Wilson6fe4f142011-01-10 17:35:37 +0000588 ret = i915_gem_execbuffer_reserve(ring, file, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000589 if (ret)
590 goto err;
591
Chris Wilson432e58e2010-11-25 19:32:06 +0000592 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondd6864a2011-01-12 23:49:13 +0000593 int offset = obj->exec_entry - exec;
Chris Wilson67731b82010-12-08 10:38:14 +0000594 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilsondd6864a2011-01-12 23:49:13 +0000595 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000596 if (ret)
597 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000598 }
599
600 /* Leave the user relocations as are, this is the painfully slow path,
601 * and we want to avoid the complication of dropping the lock whilst
602 * having buffers reserved in the aperture and so causing spurious
603 * ENOSPC for random operations.
604 */
605
606err:
607 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000608 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000609 return ret;
610}
611
Chris Wilson54cf91d2010-11-25 18:00:26 +0000612static int
Chris Wilsonc59a3332011-03-06 13:51:29 +0000613i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
614{
615 u32 plane, flip_mask;
616 int ret;
617
618 /* Check for any pending flips. As we only maintain a flip queue depth
619 * of 1, we can simply insert a WAIT for the next display flip prior
620 * to executing the batch and avoid stalling the CPU.
621 */
622
623 for (plane = 0; flips >> plane; plane++) {
624 if (((flips >> plane) & 1) == 0)
625 continue;
626
627 if (plane)
628 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
629 else
630 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
631
632 ret = intel_ring_begin(ring, 2);
633 if (ret)
634 return ret;
635
636 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
637 intel_ring_emit(ring, MI_NOOP);
638 intel_ring_advance(ring);
639 }
640
641 return 0;
642}
643
Chris Wilsonc59a3332011-03-06 13:51:29 +0000644static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000645i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
646 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000647{
Chris Wilson432e58e2010-11-25 19:32:06 +0000648 struct drm_i915_gem_object *obj;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200649 uint32_t flush_domains = 0;
650 uint32_t flips = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000651 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000652
Chris Wilson432e58e2010-11-25 19:32:06 +0000653 list_for_each_entry(obj, objects, exec_list) {
Ben Widawsky2911a352012-04-05 14:47:36 -0700654 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000655 if (ret)
656 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200657
658 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
659 i915_gem_clflush_object(obj);
660
661 if (obj->base.pending_write_domain)
662 flips |= atomic_read(&obj->pending_flip);
663
664 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000665 }
666
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200667 if (flips) {
668 ret = i915_gem_execbuffer_wait_for_flips(ring, flips);
669 if (ret)
670 return ret;
671 }
672
673 if (flush_domains & I915_GEM_DOMAIN_CPU)
674 intel_gtt_chipset_flush();
675
676 if (flush_domains & I915_GEM_DOMAIN_GTT)
677 wmb();
678
Chris Wilson09cf7c92012-07-13 14:14:08 +0100679 /* Unconditionally invalidate gpu caches and ensure that we do flush
680 * any residual writes from the previous batch.
681 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100682 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000683}
684
Chris Wilson432e58e2010-11-25 19:32:06 +0000685static bool
686i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000687{
Chris Wilson432e58e2010-11-25 19:32:06 +0000688 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000689}
690
691static int
692validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
693 int count)
694{
695 int i;
696
697 for (i = 0; i < count; i++) {
698 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
699 int length; /* limited by fault_in_pages_readable() */
700
701 /* First check for malicious input causing overflow */
702 if (exec[i].relocation_count >
703 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
704 return -EINVAL;
705
706 length = exec[i].relocation_count *
707 sizeof(struct drm_i915_gem_relocation_entry);
708 if (!access_ok(VERIFY_READ, ptr, length))
709 return -EFAULT;
710
711 /* we may also need to update the presumed offsets */
712 if (!access_ok(VERIFY_WRITE, ptr, length))
713 return -EFAULT;
714
Daniel Vetterf56f8212012-03-25 19:47:41 +0200715 if (fault_in_multipages_readable(ptr, length))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000716 return -EFAULT;
717 }
718
719 return 0;
720}
721
Chris Wilson432e58e2010-11-25 19:32:06 +0000722static void
723i915_gem_execbuffer_move_to_active(struct list_head *objects,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000724 struct intel_ring_buffer *ring,
725 u32 seqno)
Chris Wilson432e58e2010-11-25 19:32:06 +0000726{
727 struct drm_i915_gem_object *obj;
728
729 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson69c2fc82012-07-20 12:41:03 +0100730 u32 old_read = obj->base.read_domains;
731 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000732
Chris Wilson432e58e2010-11-25 19:32:06 +0000733 obj->base.read_domains = obj->base.pending_read_domains;
734 obj->base.write_domain = obj->base.pending_write_domain;
735 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
736
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000737 i915_gem_object_move_to_active(obj, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +0000738 if (obj->base.write_domain) {
739 obj->dirty = 1;
Chris Wilson0201f1e2012-07-20 12:41:01 +0100740 obj->last_write_seqno = seqno;
Chris Wilsonacb87df2012-05-03 15:47:57 +0100741 if (obj->pin_count) /* check for potential scanout */
Chris Wilsonf047e392012-07-21 12:31:41 +0100742 intel_mark_fb_busy(obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000743 }
744
Chris Wilsondb53a302011-02-03 11:57:46 +0000745 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000746 }
747}
748
Chris Wilson54cf91d2010-11-25 18:00:26 +0000749static void
750i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000751 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000752 struct intel_ring_buffer *ring)
753{
Daniel Vettercc889e02012-06-13 20:45:19 +0200754 /* Unconditionally force add_request to emit a full flush. */
755 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000756
Chris Wilson432e58e2010-11-25 19:32:06 +0000757 /* Add a breadcrumb for the completion of the batch buffer */
Chris Wilson3bb73ab2012-07-20 12:40:59 +0100758 (void)i915_add_request(ring, file, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000759}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000760
761static int
Eric Anholtae662d32012-01-03 09:23:29 -0800762i915_reset_gen7_sol_offsets(struct drm_device *dev,
763 struct intel_ring_buffer *ring)
764{
765 drm_i915_private_t *dev_priv = dev->dev_private;
766 int ret, i;
767
768 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
769 return 0;
770
771 ret = intel_ring_begin(ring, 4 * 3);
772 if (ret)
773 return ret;
774
775 for (i = 0; i < 4; i++) {
776 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
777 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
778 intel_ring_emit(ring, 0);
779 }
780
781 intel_ring_advance(ring);
782
783 return 0;
784}
785
786static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000787i915_gem_do_execbuffer(struct drm_device *dev, void *data,
788 struct drm_file *file,
789 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +0000790 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000791{
792 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000793 struct list_head objects;
Chris Wilson67731b82010-12-08 10:38:14 +0000794 struct eb_objects *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000795 struct drm_i915_gem_object *batch_obj;
796 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000797 struct intel_ring_buffer *ring;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700798 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000799 u32 exec_start, exec_len;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000800 u32 seqno;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800801 u32 mask;
Chris Wilson72bfa192010-12-19 11:42:05 +0000802 int ret, mode, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000803
Chris Wilson432e58e2010-11-25 19:32:06 +0000804 if (!i915_gem_check_execbuffer(args)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100805 DRM_DEBUG("execbuf with invalid offset/length\n");
Chris Wilson432e58e2010-11-25 19:32:06 +0000806 return -EINVAL;
807 }
808
809 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000810 if (ret)
811 return ret;
812
Chris Wilson54cf91d2010-11-25 18:00:26 +0000813 switch (args->flags & I915_EXEC_RING_MASK) {
814 case I915_EXEC_DEFAULT:
815 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000816 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000817 break;
818 case I915_EXEC_BSD:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000819 ring = &dev_priv->ring[VCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700820 if (ctx_id != 0) {
821 DRM_DEBUG("Ring %s doesn't support contexts\n",
822 ring->name);
823 return -EPERM;
824 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000825 break;
826 case I915_EXEC_BLT:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000827 ring = &dev_priv->ring[BCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700828 if (ctx_id != 0) {
829 DRM_DEBUG("Ring %s doesn't support contexts\n",
830 ring->name);
831 return -EPERM;
832 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000833 break;
834 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100835 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000836 (int)(args->flags & I915_EXEC_RING_MASK));
837 return -EINVAL;
838 }
Chris Wilsona15817c2012-05-11 14:29:31 +0100839 if (!intel_ring_initialized(ring)) {
840 DRM_DEBUG("execbuf with invalid ring: %d\n",
841 (int)(args->flags & I915_EXEC_RING_MASK));
842 return -EINVAL;
843 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000844
Chris Wilson72bfa192010-12-19 11:42:05 +0000845 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800846 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +0000847 switch (mode) {
848 case I915_EXEC_CONSTANTS_REL_GENERAL:
849 case I915_EXEC_CONSTANTS_ABSOLUTE:
850 case I915_EXEC_CONSTANTS_REL_SURFACE:
851 if (ring == &dev_priv->ring[RCS] &&
852 mode != dev_priv->relative_constants_mode) {
853 if (INTEL_INFO(dev)->gen < 4)
854 return -EINVAL;
855
856 if (INTEL_INFO(dev)->gen > 5 &&
857 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
858 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800859
860 /* The HW changed the meaning on this bit on gen6 */
861 if (INTEL_INFO(dev)->gen >= 6)
862 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +0000863 }
864 break;
865 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100866 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +0000867 return -EINVAL;
868 }
869
Chris Wilson54cf91d2010-11-25 18:00:26 +0000870 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +0100871 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000872 return -EINVAL;
873 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000874
875 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000876 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +0100877 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000878 return -EINVAL;
879 }
880
Daniel Vetter6ebebc92012-04-26 23:28:11 +0200881 if (INTEL_INFO(dev)->gen >= 5) {
882 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
883 return -EINVAL;
884 }
885
Xi Wang44afb3a2012-04-23 04:06:42 -0400886 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
887 DRM_DEBUG("execbuf with %u cliprects\n",
888 args->num_cliprects);
889 return -EINVAL;
890 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200891
Chris Wilson432e58e2010-11-25 19:32:06 +0000892 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +0000893 GFP_KERNEL);
894 if (cliprects == NULL) {
895 ret = -ENOMEM;
896 goto pre_mutex_err;
897 }
898
Chris Wilson432e58e2010-11-25 19:32:06 +0000899 if (copy_from_user(cliprects,
900 (struct drm_clip_rect __user *)(uintptr_t)
901 args->cliprects_ptr,
902 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000903 ret = -EFAULT;
904 goto pre_mutex_err;
905 }
906 }
907
Chris Wilson54cf91d2010-11-25 18:00:26 +0000908 ret = i915_mutex_lock_interruptible(dev);
909 if (ret)
910 goto pre_mutex_err;
911
912 if (dev_priv->mm.suspended) {
913 mutex_unlock(&dev->struct_mutex);
914 ret = -EBUSY;
915 goto pre_mutex_err;
916 }
917
Chris Wilson67731b82010-12-08 10:38:14 +0000918 eb = eb_create(args->buffer_count);
919 if (eb == NULL) {
920 mutex_unlock(&dev->struct_mutex);
921 ret = -ENOMEM;
922 goto pre_mutex_err;
923 }
924
Chris Wilson54cf91d2010-11-25 18:00:26 +0000925 /* Look up object handles */
Chris Wilson432e58e2010-11-25 19:32:06 +0000926 INIT_LIST_HEAD(&objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000927 for (i = 0; i < args->buffer_count; i++) {
928 struct drm_i915_gem_object *obj;
929
Chris Wilson432e58e2010-11-25 19:32:06 +0000930 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
931 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000932 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100933 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000934 exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000935 /* prevent error path from reading uninitialized data */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000936 ret = -ENOENT;
937 goto err;
938 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000939
Chris Wilson432e58e2010-11-25 19:32:06 +0000940 if (!list_empty(&obj->exec_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100941 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000942 obj, exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000943 ret = -EINVAL;
944 goto err;
945 }
Chris Wilson432e58e2010-11-25 19:32:06 +0000946
947 list_add_tail(&obj->exec_list, &objects);
Chris Wilson67731b82010-12-08 10:38:14 +0000948 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000949 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000950 eb_add_object(eb, obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000951 }
952
Chris Wilson6fe4f142011-01-10 17:35:37 +0000953 /* take note of the batch buffer before we might reorder the lists */
954 batch_obj = list_entry(objects.prev,
955 struct drm_i915_gem_object,
956 exec_list);
957
Chris Wilson54cf91d2010-11-25 18:00:26 +0000958 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilson6fe4f142011-01-10 17:35:37 +0000959 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000960 if (ret)
961 goto err;
962
963 /* The objects are in their final locations, apply the relocations. */
Chris Wilson6fe4f142011-01-10 17:35:37 +0000964 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000965 if (ret) {
966 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +0000967 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilson67731b82010-12-08 10:38:14 +0000968 &objects, eb,
969 exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000970 args->buffer_count);
971 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
972 }
973 if (ret)
974 goto err;
975 }
976
977 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000978 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +0100979 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +0000980 ret = -EINVAL;
981 goto err;
982 }
983 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
984
Chris Wilson432e58e2010-11-25 19:32:06 +0000985 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000986 if (ret)
987 goto err;
988
Chris Wilsondb53a302011-02-03 11:57:46 +0000989 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson076e2c02011-01-21 10:07:18 +0000990 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000991 if (seqno < ring->sync_seqno[i]) {
992 /* The GPU can not handle its semaphore value wrapping,
993 * so every billion or so execbuffers, we need to stall
994 * the GPU in order to reset the counters.
995 */
Ben Widawskyb2da9fe2012-04-26 16:02:58 -0700996 ret = i915_gpu_idle(dev);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000997 if (ret)
998 goto err;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -0700999 i915_gem_retire_requests(dev);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001000
1001 BUG_ON(ring->sync_seqno[i]);
1002 }
1003 }
1004
Eric Anholt0da5cec2012-07-23 12:33:55 -07001005 ret = i915_switch_context(ring, file, ctx_id);
1006 if (ret)
1007 goto err;
1008
Ben Widawskye2971bd2011-12-12 19:21:57 -08001009 if (ring == &dev_priv->ring[RCS] &&
1010 mode != dev_priv->relative_constants_mode) {
1011 ret = intel_ring_begin(ring, 4);
1012 if (ret)
1013 goto err;
1014
1015 intel_ring_emit(ring, MI_NOOP);
1016 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1017 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001018 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001019 intel_ring_advance(ring);
1020
1021 dev_priv->relative_constants_mode = mode;
1022 }
1023
Eric Anholtae662d32012-01-03 09:23:29 -08001024 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1025 ret = i915_reset_gen7_sol_offsets(dev, ring);
1026 if (ret)
1027 goto err;
1028 }
1029
Chris Wilsondb53a302011-02-03 11:57:46 +00001030 trace_i915_gem_ring_dispatch(ring, seqno);
1031
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001032 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1033 exec_len = args->batch_len;
1034 if (cliprects) {
1035 for (i = 0; i < args->num_cliprects; i++) {
1036 ret = i915_emit_box(dev, &cliprects[i],
1037 args->DR1, args->DR4);
1038 if (ret)
1039 goto err;
1040
1041 ret = ring->dispatch_execbuffer(ring,
1042 exec_start, exec_len);
1043 if (ret)
1044 goto err;
1045 }
1046 } else {
1047 ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
1048 if (ret)
1049 goto err;
1050 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001051
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001052 i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
Chris Wilson432e58e2010-11-25 19:32:06 +00001053 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001054
1055err:
Chris Wilson67731b82010-12-08 10:38:14 +00001056 eb_destroy(eb);
Chris Wilson432e58e2010-11-25 19:32:06 +00001057 while (!list_empty(&objects)) {
1058 struct drm_i915_gem_object *obj;
1059
1060 obj = list_first_entry(&objects,
1061 struct drm_i915_gem_object,
1062 exec_list);
1063 list_del_init(&obj->exec_list);
1064 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001065 }
1066
1067 mutex_unlock(&dev->struct_mutex);
1068
1069pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001070 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001071 return ret;
1072}
1073
1074/*
1075 * Legacy execbuffer just creates an exec2 list from the original exec object
1076 * list array and passes it to the real function.
1077 */
1078int
1079i915_gem_execbuffer(struct drm_device *dev, void *data,
1080 struct drm_file *file)
1081{
1082 struct drm_i915_gem_execbuffer *args = data;
1083 struct drm_i915_gem_execbuffer2 exec2;
1084 struct drm_i915_gem_exec_object *exec_list = NULL;
1085 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1086 int ret, i;
1087
Chris Wilson54cf91d2010-11-25 18:00:26 +00001088 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001089 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001090 return -EINVAL;
1091 }
1092
1093 /* Copy in the exec list from userland */
1094 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1095 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1096 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001097 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001098 args->buffer_count);
1099 drm_free_large(exec_list);
1100 drm_free_large(exec2_list);
1101 return -ENOMEM;
1102 }
1103 ret = copy_from_user(exec_list,
1104 (struct drm_i915_relocation_entry __user *)
1105 (uintptr_t) args->buffers_ptr,
1106 sizeof(*exec_list) * args->buffer_count);
1107 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001108 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001109 args->buffer_count, ret);
1110 drm_free_large(exec_list);
1111 drm_free_large(exec2_list);
1112 return -EFAULT;
1113 }
1114
1115 for (i = 0; i < args->buffer_count; i++) {
1116 exec2_list[i].handle = exec_list[i].handle;
1117 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1118 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1119 exec2_list[i].alignment = exec_list[i].alignment;
1120 exec2_list[i].offset = exec_list[i].offset;
1121 if (INTEL_INFO(dev)->gen < 4)
1122 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1123 else
1124 exec2_list[i].flags = 0;
1125 }
1126
1127 exec2.buffers_ptr = args->buffers_ptr;
1128 exec2.buffer_count = args->buffer_count;
1129 exec2.batch_start_offset = args->batch_start_offset;
1130 exec2.batch_len = args->batch_len;
1131 exec2.DR1 = args->DR1;
1132 exec2.DR4 = args->DR4;
1133 exec2.num_cliprects = args->num_cliprects;
1134 exec2.cliprects_ptr = args->cliprects_ptr;
1135 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001136 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001137
1138 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1139 if (!ret) {
1140 /* Copy the new buffer offsets back to the user's exec list. */
1141 for (i = 0; i < args->buffer_count; i++)
1142 exec_list[i].offset = exec2_list[i].offset;
1143 /* ... and back out to userspace */
1144 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1145 (uintptr_t) args->buffers_ptr,
1146 exec_list,
1147 sizeof(*exec_list) * args->buffer_count);
1148 if (ret) {
1149 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001150 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001151 "back to user (%d)\n",
1152 args->buffer_count, ret);
1153 }
1154 }
1155
1156 drm_free_large(exec_list);
1157 drm_free_large(exec2_list);
1158 return ret;
1159}
1160
1161int
1162i915_gem_execbuffer2(struct drm_device *dev, void *data,
1163 struct drm_file *file)
1164{
1165 struct drm_i915_gem_execbuffer2 *args = data;
1166 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1167 int ret;
1168
Xi Wanged8cd3b2012-04-23 04:06:41 -04001169 if (args->buffer_count < 1 ||
1170 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001171 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001172 return -EINVAL;
1173 }
1174
Chris Wilson8408c282011-02-21 12:54:48 +00001175 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1176 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1177 if (exec2_list == NULL)
1178 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1179 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001180 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001181 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001182 args->buffer_count);
1183 return -ENOMEM;
1184 }
1185 ret = copy_from_user(exec2_list,
1186 (struct drm_i915_relocation_entry __user *)
1187 (uintptr_t) args->buffers_ptr,
1188 sizeof(*exec2_list) * args->buffer_count);
1189 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001190 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001191 args->buffer_count, ret);
1192 drm_free_large(exec2_list);
1193 return -EFAULT;
1194 }
1195
1196 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1197 if (!ret) {
1198 /* Copy the new buffer offsets back to the user's exec list. */
1199 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1200 (uintptr_t) args->buffers_ptr,
1201 exec2_list,
1202 sizeof(*exec2_list) * args->buffer_count);
1203 if (ret) {
1204 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001205 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001206 "back to user (%d)\n",
1207 args->buffer_count, ret);
1208 }
1209 }
1210
1211 drm_free_large(exec2_list);
1212 return ret;
1213}