blob: ee8f97f0539ec6fb8a8392378219ebaa1f91d3bb [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
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
30#include <drm/i915_drm.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000031#include "i915_drv.h"
32#include "i915_trace.h"
33#include "intel_drv.h"
Eugeni Dodonovf45b5552011-12-09 17:16:37 -080034#include <linux/dma_remapping.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000035
Chris Wilson67731b82010-12-08 10:38:14 +000036struct eb_objects {
37 int and;
38 struct hlist_head buckets[0];
39};
40
41static struct eb_objects *
42eb_create(int size)
43{
44 struct eb_objects *eb;
45 int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Chris Wilson41783ee2012-09-18 10:04:02 +010046 BUILD_BUG_ON(!is_power_of_2(PAGE_SIZE / sizeof(struct hlist_head)));
Chris Wilson67731b82010-12-08 10:38:14 +000047 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 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000132 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100133 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000134 "obj %p target %d offset %d "
135 "read %08x write %08x",
136 obj, reloc->target_handle,
137 (int) reloc->offset,
138 reloc->read_domains,
139 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000140 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000141 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100142 if (unlikely((reloc->write_domain | reloc->read_domains)
143 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100144 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000145 "obj %p target %d offset %d "
146 "read %08x write %08x",
147 obj, reloc->target_handle,
148 (int) reloc->offset,
149 reloc->read_domains,
150 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000151 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000152 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000153 if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
154 reloc->write_domain != target_obj->pending_write_domain)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100155 DRM_DEBUG("Write domain conflict: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000156 "obj %p target %d offset %d "
157 "new %08x old %08x\n",
158 obj, reloc->target_handle,
159 (int) reloc->offset,
160 reloc->write_domain,
161 target_obj->pending_write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000162 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000163 }
164
165 target_obj->pending_read_domains |= reloc->read_domains;
166 target_obj->pending_write_domain |= reloc->write_domain;
167
168 /* If the relocation already has the right value in it, no
169 * more work needs to be done.
170 */
171 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000172 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000173
174 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000175 if (unlikely(reloc->offset > obj->base.size - 4)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100176 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000177 "obj %p target %d offset %d size %d.\n",
178 obj, reloc->target_handle,
179 (int) reloc->offset,
180 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000181 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000182 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000183 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100184 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000185 "obj %p target %d offset %d.\n",
186 obj, reloc->target_handle,
187 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000188 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000189 }
190
Chris Wilsondabdfe02012-03-26 10:10:27 +0200191 /* We can't wait for rendering with pagefaults disabled */
192 if (obj->active && in_atomic())
193 return -EFAULT;
194
Chris Wilson54cf91d2010-11-25 18:00:26 +0000195 reloc->delta += target_offset;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200196 if (use_cpu_reloc(obj)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000197 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
198 char *vaddr;
199
Chris Wilsondabdfe02012-03-26 10:10:27 +0200200 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
201 if (ret)
202 return ret;
203
Chris Wilson9da3da62012-06-01 15:20:22 +0100204 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
205 reloc->offset >> PAGE_SHIFT));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000206 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
207 kunmap_atomic(vaddr);
208 } else {
209 struct drm_i915_private *dev_priv = dev->dev_private;
210 uint32_t __iomem *reloc_entry;
211 void __iomem *reloc_page;
212
Chris Wilson7b096382012-04-14 09:55:51 +0100213 ret = i915_gem_object_set_to_gtt_domain(obj, true);
214 if (ret)
215 return ret;
216
217 ret = i915_gem_object_put_fence(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000218 if (ret)
Chris Wilson67731b82010-12-08 10:38:14 +0000219 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000220
221 /* Map the page containing the relocation we're going to perform. */
222 reloc->offset += obj->gtt_offset;
223 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
224 reloc->offset & PAGE_MASK);
225 reloc_entry = (uint32_t __iomem *)
226 (reloc_page + (reloc->offset & ~PAGE_MASK));
227 iowrite32(reloc->delta, reloc_entry);
228 io_mapping_unmap_atomic(reloc_page);
229 }
230
231 /* and update the user's relocation entry */
232 reloc->presumed_offset = target_offset;
233
Chris Wilson67731b82010-12-08 10:38:14 +0000234 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000235}
236
237static int
238i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000239 struct eb_objects *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000240{
Chris Wilson1d83f442012-03-24 20:12:53 +0000241#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
242 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000243 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000244 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000245 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000246
247 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000248
Chris Wilson1d83f442012-03-24 20:12:53 +0000249 remain = entry->relocation_count;
250 while (remain) {
251 struct drm_i915_gem_relocation_entry *r = stack_reloc;
252 int count = remain;
253 if (count > ARRAY_SIZE(stack_reloc))
254 count = ARRAY_SIZE(stack_reloc);
255 remain -= count;
256
257 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000258 return -EFAULT;
259
Chris Wilson1d83f442012-03-24 20:12:53 +0000260 do {
261 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000262
Chris Wilson1d83f442012-03-24 20:12:53 +0000263 ret = i915_gem_execbuffer_relocate_entry(obj, eb, r);
264 if (ret)
265 return ret;
266
267 if (r->presumed_offset != offset &&
268 __copy_to_user_inatomic(&user_relocs->presumed_offset,
269 &r->presumed_offset,
270 sizeof(r->presumed_offset))) {
271 return -EFAULT;
272 }
273
274 user_relocs++;
275 r++;
276 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000277 }
278
279 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000280#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000281}
282
283static int
284i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000285 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000286 struct drm_i915_gem_relocation_entry *relocs)
287{
Chris Wilson6fe4f142011-01-10 17:35:37 +0000288 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000289 int i, ret;
290
291 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000292 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000293 if (ret)
294 return ret;
295 }
296
297 return 0;
298}
299
300static int
301i915_gem_execbuffer_relocate(struct drm_device *dev,
Chris Wilson67731b82010-12-08 10:38:14 +0000302 struct eb_objects *eb,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000303 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000304{
Chris Wilson432e58e2010-11-25 19:32:06 +0000305 struct drm_i915_gem_object *obj;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000306 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000307
Chris Wilsond4aeee72011-03-14 15:11:24 +0000308 /* This is the fast path and we cannot handle a pagefault whilst
309 * holding the struct mutex lest the user pass in the relocations
310 * contained within a mmaped bo. For in such a case we, the page
311 * fault handler would call i915_gem_fault() and we would try to
312 * acquire the struct mutex again. Obviously this is bad and so
313 * lockdep complains vehemently.
314 */
315 pagefault_disable();
Chris Wilson432e58e2010-11-25 19:32:06 +0000316 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000317 ret = i915_gem_execbuffer_relocate_object(obj, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000318 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000319 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000320 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000321 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000322
Chris Wilsond4aeee72011-03-14 15:11:24 +0000323 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000324}
325
Chris Wilson7788a762012-08-24 19:18:18 +0100326#define __EXEC_OBJECT_HAS_PIN (1<<31)
327#define __EXEC_OBJECT_HAS_FENCE (1<<30)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100328
329static int
Chris Wilsondabdfe02012-03-26 10:10:27 +0200330need_reloc_mappable(struct drm_i915_gem_object *obj)
331{
332 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
333 return entry->relocation_count && !use_cpu_reloc(obj);
334}
335
336static int
Chris Wilson7788a762012-08-24 19:18:18 +0100337i915_gem_execbuffer_reserve_object(struct drm_i915_gem_object *obj,
338 struct intel_ring_buffer *ring)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100339{
Chris Wilson7788a762012-08-24 19:18:18 +0100340 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100341 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
342 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
343 bool need_fence, need_mappable;
344 int ret;
345
346 need_fence =
347 has_fenced_gpu_access &&
348 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
349 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200350 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100351
Chris Wilson86a1ee22012-08-11 15:41:04 +0100352 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable, false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100353 if (ret)
354 return ret;
355
Chris Wilson7788a762012-08-24 19:18:18 +0100356 entry->flags |= __EXEC_OBJECT_HAS_PIN;
357
Chris Wilson1690e1e2011-12-14 13:57:08 +0100358 if (has_fenced_gpu_access) {
359 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100360 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000361 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100362 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100363
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000364 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100365 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000366
Chris Wilson7dd49062012-03-21 10:48:18 +0000367 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100368 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100369 }
370
Chris Wilson7788a762012-08-24 19:18:18 +0100371 /* Ensure ppgtt mapping exists if needed */
372 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
373 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
374 obj, obj->cache_level);
375
376 obj->has_aliasing_ppgtt_mapping = 1;
377 }
378
Chris Wilson1690e1e2011-12-14 13:57:08 +0100379 entry->offset = obj->gtt_offset;
380 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100381}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100382
Chris Wilson7788a762012-08-24 19:18:18 +0100383static void
384i915_gem_execbuffer_unreserve_object(struct drm_i915_gem_object *obj)
385{
386 struct drm_i915_gem_exec_object2 *entry;
387
388 if (!obj->gtt_space)
389 return;
390
391 entry = obj->exec_entry;
392
393 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
394 i915_gem_object_unpin_fence(obj);
395
396 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
397 i915_gem_object_unpin(obj);
398
399 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100400}
401
Chris Wilson54cf91d2010-11-25 18:00:26 +0000402static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000403i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000404 struct drm_file *file,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000405 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000406{
Chris Wilson432e58e2010-11-25 19:32:06 +0000407 struct drm_i915_gem_object *obj;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000408 struct list_head ordered_objects;
Chris Wilson7788a762012-08-24 19:18:18 +0100409 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
410 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000411
412 INIT_LIST_HEAD(&ordered_objects);
413 while (!list_empty(objects)) {
414 struct drm_i915_gem_exec_object2 *entry;
415 bool need_fence, need_mappable;
416
417 obj = list_first_entry(objects,
418 struct drm_i915_gem_object,
419 exec_list);
420 entry = obj->exec_entry;
421
422 need_fence =
423 has_fenced_gpu_access &&
424 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
425 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200426 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000427
428 if (need_mappable)
429 list_move(&obj->exec_list, &ordered_objects);
430 else
431 list_move_tail(&obj->exec_list, &ordered_objects);
Chris Wilson595dad72011-01-13 11:03:48 +0000432
433 obj->base.pending_read_domains = 0;
434 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100435 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000436 }
437 list_splice(&ordered_objects, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000438
439 /* Attempt to pin all of the buffers into the GTT.
440 * This is done in 3 phases:
441 *
442 * 1a. Unbind all objects that do not match the GTT constraints for
443 * the execbuffer (fenceable, mappable, alignment etc).
444 * 1b. Increment pin count for already bound objects.
445 * 2. Bind new objects.
446 * 3. Decrement pin count.
447 *
Chris Wilson7788a762012-08-24 19:18:18 +0100448 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000449 * room for the earlier objects *unless* we need to defragment.
450 */
451 retry = 0;
452 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100453 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000454
455 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000456 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000457 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000458 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100459
Chris Wilson6fe4f142011-01-10 17:35:37 +0000460 if (!obj->gtt_space)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000461 continue;
462
463 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000464 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000465 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
466 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200467 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000468
469 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
470 (need_mappable && !obj->map_and_fenceable))
471 ret = i915_gem_object_unbind(obj);
472 else
Chris Wilson7788a762012-08-24 19:18:18 +0100473 ret = i915_gem_execbuffer_reserve_object(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000474 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000475 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000476 }
477
478 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000479 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100480 if (obj->gtt_space)
481 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000482
Chris Wilson7788a762012-08-24 19:18:18 +0100483 ret = i915_gem_execbuffer_reserve_object(obj, ring);
484 if (ret)
485 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000486 }
487
Chris Wilson7788a762012-08-24 19:18:18 +0100488err: /* Decrement pin count for bound objects */
489 list_for_each_entry(obj, objects, exec_list)
490 i915_gem_execbuffer_unreserve_object(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000491
Chris Wilson6c085a72012-08-20 11:40:46 +0200492 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000493 return ret;
494
Chris Wilson6c085a72012-08-20 11:40:46 +0200495 ret = i915_gem_evict_everything(ring->dev);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000496 if (ret)
497 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000498 } while (1);
499}
500
501static int
502i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
503 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000504 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000505 struct list_head *objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000506 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000507 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000508 int count)
509{
510 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000511 struct drm_i915_gem_object *obj;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000512 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000513 int i, total, ret;
514
Chris Wilson67731b82010-12-08 10:38:14 +0000515 /* We may process another execbuffer during the unlock... */
Chris Wilson36cf1742011-01-10 12:09:12 +0000516 while (!list_empty(objects)) {
Chris Wilson67731b82010-12-08 10:38:14 +0000517 obj = list_first_entry(objects,
518 struct drm_i915_gem_object,
519 exec_list);
520 list_del_init(&obj->exec_list);
521 drm_gem_object_unreference(&obj->base);
522 }
523
Chris Wilson54cf91d2010-11-25 18:00:26 +0000524 mutex_unlock(&dev->struct_mutex);
525
526 total = 0;
527 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000528 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000529
Chris Wilsondd6864a2011-01-12 23:49:13 +0000530 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000531 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000532 if (reloc == NULL || reloc_offset == NULL) {
533 drm_free_large(reloc);
534 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000535 mutex_lock(&dev->struct_mutex);
536 return -ENOMEM;
537 }
538
539 total = 0;
540 for (i = 0; i < count; i++) {
541 struct drm_i915_gem_relocation_entry __user *user_relocs;
542
Chris Wilson432e58e2010-11-25 19:32:06 +0000543 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000544
545 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000546 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000547 ret = -EFAULT;
548 mutex_lock(&dev->struct_mutex);
549 goto err;
550 }
551
Chris Wilsondd6864a2011-01-12 23:49:13 +0000552 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000553 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000554 }
555
556 ret = i915_mutex_lock_interruptible(dev);
557 if (ret) {
558 mutex_lock(&dev->struct_mutex);
559 goto err;
560 }
561
Chris Wilson67731b82010-12-08 10:38:14 +0000562 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000563 eb_reset(eb);
564 for (i = 0; i < count; i++) {
Chris Wilson67731b82010-12-08 10:38:14 +0000565 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
566 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000567 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100568 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson67731b82010-12-08 10:38:14 +0000569 exec[i].handle, i);
570 ret = -ENOENT;
571 goto err;
572 }
573
574 list_add_tail(&obj->exec_list, objects);
575 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000576 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000577 eb_add_object(eb, obj);
578 }
579
Chris Wilson6fe4f142011-01-10 17:35:37 +0000580 ret = i915_gem_execbuffer_reserve(ring, file, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000581 if (ret)
582 goto err;
583
Chris Wilson432e58e2010-11-25 19:32:06 +0000584 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondd6864a2011-01-12 23:49:13 +0000585 int offset = obj->exec_entry - exec;
Chris Wilson67731b82010-12-08 10:38:14 +0000586 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilsondd6864a2011-01-12 23:49:13 +0000587 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000588 if (ret)
589 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000590 }
591
592 /* Leave the user relocations as are, this is the painfully slow path,
593 * and we want to avoid the complication of dropping the lock whilst
594 * having buffers reserved in the aperture and so causing spurious
595 * ENOSPC for random operations.
596 */
597
598err:
599 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000600 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000601 return ret;
602}
603
Chris Wilson54cf91d2010-11-25 18:00:26 +0000604static int
Chris Wilsonc59a3332011-03-06 13:51:29 +0000605i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
606{
607 u32 plane, flip_mask;
608 int ret;
609
610 /* Check for any pending flips. As we only maintain a flip queue depth
611 * of 1, we can simply insert a WAIT for the next display flip prior
612 * to executing the batch and avoid stalling the CPU.
613 */
614
615 for (plane = 0; flips >> plane; plane++) {
616 if (((flips >> plane) & 1) == 0)
617 continue;
618
619 if (plane)
620 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
621 else
622 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
623
624 ret = intel_ring_begin(ring, 2);
625 if (ret)
626 return ret;
627
628 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
629 intel_ring_emit(ring, MI_NOOP);
630 intel_ring_advance(ring);
631 }
632
633 return 0;
634}
635
Chris Wilsonc59a3332011-03-06 13:51:29 +0000636static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000637i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
638 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000639{
Chris Wilson432e58e2010-11-25 19:32:06 +0000640 struct drm_i915_gem_object *obj;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200641 uint32_t flush_domains = 0;
642 uint32_t flips = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000643 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000644
Chris Wilson432e58e2010-11-25 19:32:06 +0000645 list_for_each_entry(obj, objects, exec_list) {
Ben Widawsky2911a352012-04-05 14:47:36 -0700646 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000647 if (ret)
648 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200649
650 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
651 i915_gem_clflush_object(obj);
652
653 if (obj->base.pending_write_domain)
654 flips |= atomic_read(&obj->pending_flip);
655
656 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000657 }
658
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200659 if (flips) {
660 ret = i915_gem_execbuffer_wait_for_flips(ring, flips);
661 if (ret)
662 return ret;
663 }
664
665 if (flush_domains & I915_GEM_DOMAIN_CPU)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800666 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200667
668 if (flush_domains & I915_GEM_DOMAIN_GTT)
669 wmb();
670
Chris Wilson09cf7c92012-07-13 14:14:08 +0100671 /* Unconditionally invalidate gpu caches and ensure that we do flush
672 * any residual writes from the previous batch.
673 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100674 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000675}
676
Chris Wilson432e58e2010-11-25 19:32:06 +0000677static bool
678i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000679{
Chris Wilson432e58e2010-11-25 19:32:06 +0000680 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000681}
682
683static int
684validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
685 int count)
686{
687 int i;
688
689 for (i = 0; i < count; i++) {
690 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
691 int length; /* limited by fault_in_pages_readable() */
692
693 /* First check for malicious input causing overflow */
694 if (exec[i].relocation_count >
695 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
696 return -EINVAL;
697
698 length = exec[i].relocation_count *
699 sizeof(struct drm_i915_gem_relocation_entry);
700 if (!access_ok(VERIFY_READ, ptr, length))
701 return -EFAULT;
702
703 /* we may also need to update the presumed offsets */
704 if (!access_ok(VERIFY_WRITE, ptr, length))
705 return -EFAULT;
706
Daniel Vetterf56f8212012-03-25 19:47:41 +0200707 if (fault_in_multipages_readable(ptr, length))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000708 return -EFAULT;
709 }
710
711 return 0;
712}
713
Chris Wilson432e58e2010-11-25 19:32:06 +0000714static void
715i915_gem_execbuffer_move_to_active(struct list_head *objects,
Chris Wilson9d7730912012-11-27 16:22:52 +0000716 struct intel_ring_buffer *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000717{
718 struct drm_i915_gem_object *obj;
719
720 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson69c2fc82012-07-20 12:41:03 +0100721 u32 old_read = obj->base.read_domains;
722 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000723
Chris Wilson432e58e2010-11-25 19:32:06 +0000724 obj->base.read_domains = obj->base.pending_read_domains;
725 obj->base.write_domain = obj->base.pending_write_domain;
726 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
727
Chris Wilson9d7730912012-11-27 16:22:52 +0000728 i915_gem_object_move_to_active(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000729 if (obj->base.write_domain) {
730 obj->dirty = 1;
Chris Wilson9d7730912012-11-27 16:22:52 +0000731 obj->last_write_seqno = intel_ring_get_seqno(ring);
Chris Wilsonacb87df2012-05-03 15:47:57 +0100732 if (obj->pin_count) /* check for potential scanout */
Chris Wilsonf047e392012-07-21 12:31:41 +0100733 intel_mark_fb_busy(obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000734 }
735
Chris Wilsondb53a302011-02-03 11:57:46 +0000736 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000737 }
738}
739
Chris Wilson54cf91d2010-11-25 18:00:26 +0000740static void
741i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000742 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000743 struct intel_ring_buffer *ring)
744{
Daniel Vettercc889e02012-06-13 20:45:19 +0200745 /* Unconditionally force add_request to emit a full flush. */
746 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000747
Chris Wilson432e58e2010-11-25 19:32:06 +0000748 /* Add a breadcrumb for the completion of the batch buffer */
Chris Wilson3bb73ab2012-07-20 12:40:59 +0100749 (void)i915_add_request(ring, file, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000750}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000751
752static int
Eric Anholtae662d32012-01-03 09:23:29 -0800753i915_reset_gen7_sol_offsets(struct drm_device *dev,
754 struct intel_ring_buffer *ring)
755{
756 drm_i915_private_t *dev_priv = dev->dev_private;
757 int ret, i;
758
759 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
760 return 0;
761
762 ret = intel_ring_begin(ring, 4 * 3);
763 if (ret)
764 return ret;
765
766 for (i = 0; i < 4; i++) {
767 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
768 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
769 intel_ring_emit(ring, 0);
770 }
771
772 intel_ring_advance(ring);
773
774 return 0;
775}
776
777static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000778i915_gem_do_execbuffer(struct drm_device *dev, void *data,
779 struct drm_file *file,
780 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +0000781 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000782{
783 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000784 struct list_head objects;
Chris Wilson67731b82010-12-08 10:38:14 +0000785 struct eb_objects *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000786 struct drm_i915_gem_object *batch_obj;
787 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000788 struct intel_ring_buffer *ring;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700789 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000790 u32 exec_start, exec_len;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800791 u32 mask;
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100792 u32 flags;
Chris Wilson72bfa192010-12-19 11:42:05 +0000793 int ret, mode, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000794
Chris Wilson432e58e2010-11-25 19:32:06 +0000795 if (!i915_gem_check_execbuffer(args)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100796 DRM_DEBUG("execbuf with invalid offset/length\n");
Chris Wilson432e58e2010-11-25 19:32:06 +0000797 return -EINVAL;
798 }
799
800 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000801 if (ret)
802 return ret;
803
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100804 flags = 0;
805 if (args->flags & I915_EXEC_SECURE) {
806 if (!file->is_master || !capable(CAP_SYS_ADMIN))
807 return -EPERM;
808
809 flags |= I915_DISPATCH_SECURE;
810 }
811
Chris Wilson54cf91d2010-11-25 18:00:26 +0000812 switch (args->flags & I915_EXEC_RING_MASK) {
813 case I915_EXEC_DEFAULT:
814 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000815 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000816 break;
817 case I915_EXEC_BSD:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000818 ring = &dev_priv->ring[VCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700819 if (ctx_id != 0) {
820 DRM_DEBUG("Ring %s doesn't support contexts\n",
821 ring->name);
822 return -EPERM;
823 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000824 break;
825 case I915_EXEC_BLT:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000826 ring = &dev_priv->ring[BCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700827 if (ctx_id != 0) {
828 DRM_DEBUG("Ring %s doesn't support contexts\n",
829 ring->name);
830 return -EPERM;
831 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000832 break;
833 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100834 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000835 (int)(args->flags & I915_EXEC_RING_MASK));
836 return -EINVAL;
837 }
Chris Wilsona15817c2012-05-11 14:29:31 +0100838 if (!intel_ring_initialized(ring)) {
839 DRM_DEBUG("execbuf with invalid ring: %d\n",
840 (int)(args->flags & I915_EXEC_RING_MASK));
841 return -EINVAL;
842 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000843
Chris Wilson72bfa192010-12-19 11:42:05 +0000844 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800845 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +0000846 switch (mode) {
847 case I915_EXEC_CONSTANTS_REL_GENERAL:
848 case I915_EXEC_CONSTANTS_ABSOLUTE:
849 case I915_EXEC_CONSTANTS_REL_SURFACE:
850 if (ring == &dev_priv->ring[RCS] &&
851 mode != dev_priv->relative_constants_mode) {
852 if (INTEL_INFO(dev)->gen < 4)
853 return -EINVAL;
854
855 if (INTEL_INFO(dev)->gen > 5 &&
856 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
857 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800858
859 /* The HW changed the meaning on this bit on gen6 */
860 if (INTEL_INFO(dev)->gen >= 6)
861 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +0000862 }
863 break;
864 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100865 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +0000866 return -EINVAL;
867 }
868
Chris Wilson54cf91d2010-11-25 18:00:26 +0000869 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +0100870 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000871 return -EINVAL;
872 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000873
874 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000875 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +0100876 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000877 return -EINVAL;
878 }
879
Daniel Vetter6ebebc92012-04-26 23:28:11 +0200880 if (INTEL_INFO(dev)->gen >= 5) {
881 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
882 return -EINVAL;
883 }
884
Xi Wang44afb3a2012-04-23 04:06:42 -0400885 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
886 DRM_DEBUG("execbuf with %u cliprects\n",
887 args->num_cliprects);
888 return -EINVAL;
889 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200890
Chris Wilson432e58e2010-11-25 19:32:06 +0000891 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +0000892 GFP_KERNEL);
893 if (cliprects == NULL) {
894 ret = -ENOMEM;
895 goto pre_mutex_err;
896 }
897
Chris Wilson432e58e2010-11-25 19:32:06 +0000898 if (copy_from_user(cliprects,
899 (struct drm_clip_rect __user *)(uintptr_t)
900 args->cliprects_ptr,
901 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000902 ret = -EFAULT;
903 goto pre_mutex_err;
904 }
905 }
906
Chris Wilson54cf91d2010-11-25 18:00:26 +0000907 ret = i915_mutex_lock_interruptible(dev);
908 if (ret)
909 goto pre_mutex_err;
910
911 if (dev_priv->mm.suspended) {
912 mutex_unlock(&dev->struct_mutex);
913 ret = -EBUSY;
914 goto pre_mutex_err;
915 }
916
Chris Wilson67731b82010-12-08 10:38:14 +0000917 eb = eb_create(args->buffer_count);
918 if (eb == NULL) {
919 mutex_unlock(&dev->struct_mutex);
920 ret = -ENOMEM;
921 goto pre_mutex_err;
922 }
923
Chris Wilson54cf91d2010-11-25 18:00:26 +0000924 /* Look up object handles */
Chris Wilson432e58e2010-11-25 19:32:06 +0000925 INIT_LIST_HEAD(&objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000926 for (i = 0; i < args->buffer_count; i++) {
927 struct drm_i915_gem_object *obj;
928
Chris Wilson432e58e2010-11-25 19:32:06 +0000929 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
930 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000931 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100932 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000933 exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000934 /* prevent error path from reading uninitialized data */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000935 ret = -ENOENT;
936 goto err;
937 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000938
Chris Wilson432e58e2010-11-25 19:32:06 +0000939 if (!list_empty(&obj->exec_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100940 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000941 obj, exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000942 ret = -EINVAL;
943 goto err;
944 }
Chris Wilson432e58e2010-11-25 19:32:06 +0000945
946 list_add_tail(&obj->exec_list, &objects);
Chris Wilson67731b82010-12-08 10:38:14 +0000947 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000948 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000949 eb_add_object(eb, obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000950 }
951
Chris Wilson6fe4f142011-01-10 17:35:37 +0000952 /* take note of the batch buffer before we might reorder the lists */
953 batch_obj = list_entry(objects.prev,
954 struct drm_i915_gem_object,
955 exec_list);
956
Chris Wilson54cf91d2010-11-25 18:00:26 +0000957 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilson6fe4f142011-01-10 17:35:37 +0000958 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000959 if (ret)
960 goto err;
961
962 /* The objects are in their final locations, apply the relocations. */
Chris Wilson6fe4f142011-01-10 17:35:37 +0000963 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000964 if (ret) {
965 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +0000966 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilson67731b82010-12-08 10:38:14 +0000967 &objects, eb,
968 exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000969 args->buffer_count);
970 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
971 }
972 if (ret)
973 goto err;
974 }
975
976 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000977 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +0100978 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +0000979 ret = -EINVAL;
980 goto err;
981 }
982 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
983
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100984 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
985 * batch" bit. Hence we need to pin secure batches into the global gtt.
986 * hsw should have this fixed, but let's be paranoid and do it
987 * unconditionally for now. */
988 if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
989 i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
990
Chris Wilson432e58e2010-11-25 19:32:06 +0000991 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000992 if (ret)
993 goto err;
994
Eric Anholt0da5cec2012-07-23 12:33:55 -0700995 ret = i915_switch_context(ring, file, ctx_id);
996 if (ret)
997 goto err;
998
Ben Widawskye2971bd2011-12-12 19:21:57 -0800999 if (ring == &dev_priv->ring[RCS] &&
1000 mode != dev_priv->relative_constants_mode) {
1001 ret = intel_ring_begin(ring, 4);
1002 if (ret)
1003 goto err;
1004
1005 intel_ring_emit(ring, MI_NOOP);
1006 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1007 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -08001008 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -08001009 intel_ring_advance(ring);
1010
1011 dev_priv->relative_constants_mode = mode;
1012 }
1013
Eric Anholtae662d32012-01-03 09:23:29 -08001014 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1015 ret = i915_reset_gen7_sol_offsets(dev, ring);
1016 if (ret)
1017 goto err;
1018 }
1019
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001020 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
1021 exec_len = args->batch_len;
1022 if (cliprects) {
1023 for (i = 0; i < args->num_cliprects; i++) {
1024 ret = i915_emit_box(dev, &cliprects[i],
1025 args->DR1, args->DR4);
1026 if (ret)
1027 goto err;
1028
1029 ret = ring->dispatch_execbuffer(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001030 exec_start, exec_len,
1031 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001032 if (ret)
1033 goto err;
1034 }
1035 } else {
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001036 ret = ring->dispatch_execbuffer(ring,
1037 exec_start, exec_len,
1038 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001039 if (ret)
1040 goto err;
1041 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001042
Chris Wilson9d7730912012-11-27 16:22:52 +00001043 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1044
1045 i915_gem_execbuffer_move_to_active(&objects, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +00001046 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001047
1048err:
Chris Wilson67731b82010-12-08 10:38:14 +00001049 eb_destroy(eb);
Chris Wilson432e58e2010-11-25 19:32:06 +00001050 while (!list_empty(&objects)) {
1051 struct drm_i915_gem_object *obj;
1052
1053 obj = list_first_entry(&objects,
1054 struct drm_i915_gem_object,
1055 exec_list);
1056 list_del_init(&obj->exec_list);
1057 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001058 }
1059
1060 mutex_unlock(&dev->struct_mutex);
1061
1062pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001063 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001064 return ret;
1065}
1066
1067/*
1068 * Legacy execbuffer just creates an exec2 list from the original exec object
1069 * list array and passes it to the real function.
1070 */
1071int
1072i915_gem_execbuffer(struct drm_device *dev, void *data,
1073 struct drm_file *file)
1074{
1075 struct drm_i915_gem_execbuffer *args = data;
1076 struct drm_i915_gem_execbuffer2 exec2;
1077 struct drm_i915_gem_exec_object *exec_list = NULL;
1078 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1079 int ret, i;
1080
Chris Wilson54cf91d2010-11-25 18:00:26 +00001081 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001082 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001083 return -EINVAL;
1084 }
1085
1086 /* Copy in the exec list from userland */
1087 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1088 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1089 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001090 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001091 args->buffer_count);
1092 drm_free_large(exec_list);
1093 drm_free_large(exec2_list);
1094 return -ENOMEM;
1095 }
1096 ret = copy_from_user(exec_list,
Chris Wilsonba7a6452012-09-14 11:46:00 +01001097 (void __user *)(uintptr_t)args->buffers_ptr,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001098 sizeof(*exec_list) * args->buffer_count);
1099 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001100 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001101 args->buffer_count, ret);
1102 drm_free_large(exec_list);
1103 drm_free_large(exec2_list);
1104 return -EFAULT;
1105 }
1106
1107 for (i = 0; i < args->buffer_count; i++) {
1108 exec2_list[i].handle = exec_list[i].handle;
1109 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1110 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1111 exec2_list[i].alignment = exec_list[i].alignment;
1112 exec2_list[i].offset = exec_list[i].offset;
1113 if (INTEL_INFO(dev)->gen < 4)
1114 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1115 else
1116 exec2_list[i].flags = 0;
1117 }
1118
1119 exec2.buffers_ptr = args->buffers_ptr;
1120 exec2.buffer_count = args->buffer_count;
1121 exec2.batch_start_offset = args->batch_start_offset;
1122 exec2.batch_len = args->batch_len;
1123 exec2.DR1 = args->DR1;
1124 exec2.DR4 = args->DR4;
1125 exec2.num_cliprects = args->num_cliprects;
1126 exec2.cliprects_ptr = args->cliprects_ptr;
1127 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001128 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001129
1130 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1131 if (!ret) {
1132 /* Copy the new buffer offsets back to the user's exec list. */
1133 for (i = 0; i < args->buffer_count; i++)
1134 exec_list[i].offset = exec2_list[i].offset;
1135 /* ... and back out to userspace */
Chris Wilsonba7a6452012-09-14 11:46:00 +01001136 ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001137 exec_list,
1138 sizeof(*exec_list) * args->buffer_count);
1139 if (ret) {
1140 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001141 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001142 "back to user (%d)\n",
1143 args->buffer_count, ret);
1144 }
1145 }
1146
1147 drm_free_large(exec_list);
1148 drm_free_large(exec2_list);
1149 return ret;
1150}
1151
1152int
1153i915_gem_execbuffer2(struct drm_device *dev, void *data,
1154 struct drm_file *file)
1155{
1156 struct drm_i915_gem_execbuffer2 *args = data;
1157 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1158 int ret;
1159
Xi Wanged8cd3b2012-04-23 04:06:41 -04001160 if (args->buffer_count < 1 ||
1161 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001162 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001163 return -EINVAL;
1164 }
1165
Chris Wilson8408c282011-02-21 12:54:48 +00001166 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1167 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1168 if (exec2_list == NULL)
1169 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1170 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001171 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001172 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001173 args->buffer_count);
1174 return -ENOMEM;
1175 }
1176 ret = copy_from_user(exec2_list,
1177 (struct drm_i915_relocation_entry __user *)
1178 (uintptr_t) args->buffers_ptr,
1179 sizeof(*exec2_list) * args->buffer_count);
1180 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001181 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001182 args->buffer_count, ret);
1183 drm_free_large(exec2_list);
1184 return -EFAULT;
1185 }
1186
1187 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1188 if (!ret) {
1189 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilsonba7a6452012-09-14 11:46:00 +01001190 ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001191 exec2_list,
1192 sizeof(*exec2_list) * args->buffer_count);
1193 if (ret) {
1194 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001195 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001196 "back to user (%d)\n",
1197 args->buffer_count, ret);
1198 }
1199 }
1200
1201 drm_free_large(exec2_list);
1202 return ret;
1203}