blob: 986eb98f0d253c8f13d23a236e50b323a7e90bc0 [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 Wilson54cf91d2010-11-25 18:00:26 +0000153
154 target_obj->pending_read_domains |= reloc->read_domains;
155 target_obj->pending_write_domain |= reloc->write_domain;
156
157 /* If the relocation already has the right value in it, no
158 * more work needs to be done.
159 */
160 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000161 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000162
163 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000164 if (unlikely(reloc->offset > obj->base.size - 4)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100165 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000166 "obj %p target %d offset %d size %d.\n",
167 obj, reloc->target_handle,
168 (int) reloc->offset,
169 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000170 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000171 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000172 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100173 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000174 "obj %p target %d offset %d.\n",
175 obj, reloc->target_handle,
176 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000177 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000178 }
179
Chris Wilsondabdfe02012-03-26 10:10:27 +0200180 /* We can't wait for rendering with pagefaults disabled */
181 if (obj->active && in_atomic())
182 return -EFAULT;
183
Chris Wilson54cf91d2010-11-25 18:00:26 +0000184 reloc->delta += target_offset;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200185 if (use_cpu_reloc(obj)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000186 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
187 char *vaddr;
188
Chris Wilsondabdfe02012-03-26 10:10:27 +0200189 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
190 if (ret)
191 return ret;
192
Chris Wilson9da3da62012-06-01 15:20:22 +0100193 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
194 reloc->offset >> PAGE_SHIFT));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000195 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
196 kunmap_atomic(vaddr);
197 } else {
198 struct drm_i915_private *dev_priv = dev->dev_private;
199 uint32_t __iomem *reloc_entry;
200 void __iomem *reloc_page;
201
Chris Wilson7b096382012-04-14 09:55:51 +0100202 ret = i915_gem_object_set_to_gtt_domain(obj, true);
203 if (ret)
204 return ret;
205
206 ret = i915_gem_object_put_fence(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000207 if (ret)
Chris Wilson67731b82010-12-08 10:38:14 +0000208 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000209
210 /* Map the page containing the relocation we're going to perform. */
211 reloc->offset += obj->gtt_offset;
212 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
213 reloc->offset & PAGE_MASK);
214 reloc_entry = (uint32_t __iomem *)
215 (reloc_page + (reloc->offset & ~PAGE_MASK));
216 iowrite32(reloc->delta, reloc_entry);
217 io_mapping_unmap_atomic(reloc_page);
218 }
219
220 /* and update the user's relocation entry */
221 reloc->presumed_offset = target_offset;
222
Chris Wilson67731b82010-12-08 10:38:14 +0000223 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000224}
225
226static int
227i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000228 struct eb_objects *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000229{
Chris Wilson1d83f442012-03-24 20:12:53 +0000230#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
231 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000232 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000233 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000234 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000235
236 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000237
Chris Wilson1d83f442012-03-24 20:12:53 +0000238 remain = entry->relocation_count;
239 while (remain) {
240 struct drm_i915_gem_relocation_entry *r = stack_reloc;
241 int count = remain;
242 if (count > ARRAY_SIZE(stack_reloc))
243 count = ARRAY_SIZE(stack_reloc);
244 remain -= count;
245
246 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000247 return -EFAULT;
248
Chris Wilson1d83f442012-03-24 20:12:53 +0000249 do {
250 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000251
Chris Wilson1d83f442012-03-24 20:12:53 +0000252 ret = i915_gem_execbuffer_relocate_entry(obj, eb, r);
253 if (ret)
254 return ret;
255
256 if (r->presumed_offset != offset &&
257 __copy_to_user_inatomic(&user_relocs->presumed_offset,
258 &r->presumed_offset,
259 sizeof(r->presumed_offset))) {
260 return -EFAULT;
261 }
262
263 user_relocs++;
264 r++;
265 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000266 }
267
268 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000269#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000270}
271
272static int
273i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000274 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000275 struct drm_i915_gem_relocation_entry *relocs)
276{
Chris Wilson6fe4f142011-01-10 17:35:37 +0000277 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000278 int i, ret;
279
280 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000281 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000282 if (ret)
283 return ret;
284 }
285
286 return 0;
287}
288
289static int
290i915_gem_execbuffer_relocate(struct drm_device *dev,
Chris Wilson67731b82010-12-08 10:38:14 +0000291 struct eb_objects *eb,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000292 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000293{
Chris Wilson432e58e2010-11-25 19:32:06 +0000294 struct drm_i915_gem_object *obj;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000295 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000296
Chris Wilsond4aeee72011-03-14 15:11:24 +0000297 /* This is the fast path and we cannot handle a pagefault whilst
298 * holding the struct mutex lest the user pass in the relocations
299 * contained within a mmaped bo. For in such a case we, the page
300 * fault handler would call i915_gem_fault() and we would try to
301 * acquire the struct mutex again. Obviously this is bad and so
302 * lockdep complains vehemently.
303 */
304 pagefault_disable();
Chris Wilson432e58e2010-11-25 19:32:06 +0000305 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000306 ret = i915_gem_execbuffer_relocate_object(obj, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000307 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000308 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000309 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000310 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000311
Chris Wilsond4aeee72011-03-14 15:11:24 +0000312 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000313}
314
Chris Wilson7788a762012-08-24 19:18:18 +0100315#define __EXEC_OBJECT_HAS_PIN (1<<31)
316#define __EXEC_OBJECT_HAS_FENCE (1<<30)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100317
318static int
Chris Wilsondabdfe02012-03-26 10:10:27 +0200319need_reloc_mappable(struct drm_i915_gem_object *obj)
320{
321 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
322 return entry->relocation_count && !use_cpu_reloc(obj);
323}
324
325static int
Chris Wilson7788a762012-08-24 19:18:18 +0100326i915_gem_execbuffer_reserve_object(struct drm_i915_gem_object *obj,
327 struct intel_ring_buffer *ring)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100328{
Chris Wilson7788a762012-08-24 19:18:18 +0100329 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100330 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
331 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
332 bool need_fence, need_mappable;
333 int ret;
334
335 need_fence =
336 has_fenced_gpu_access &&
337 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
338 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200339 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100340
Chris Wilson86a1ee22012-08-11 15:41:04 +0100341 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable, false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100342 if (ret)
343 return ret;
344
Chris Wilson7788a762012-08-24 19:18:18 +0100345 entry->flags |= __EXEC_OBJECT_HAS_PIN;
346
Chris Wilson1690e1e2011-12-14 13:57:08 +0100347 if (has_fenced_gpu_access) {
348 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100349 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000350 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100351 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100352
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000353 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100354 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000355
Chris Wilson7dd49062012-03-21 10:48:18 +0000356 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100357 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100358 }
359
Chris Wilson7788a762012-08-24 19:18:18 +0100360 /* Ensure ppgtt mapping exists if needed */
361 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
362 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
363 obj, obj->cache_level);
364
365 obj->has_aliasing_ppgtt_mapping = 1;
366 }
367
Chris Wilson1690e1e2011-12-14 13:57:08 +0100368 entry->offset = obj->gtt_offset;
369 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100370}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100371
Chris Wilson7788a762012-08-24 19:18:18 +0100372static void
373i915_gem_execbuffer_unreserve_object(struct drm_i915_gem_object *obj)
374{
375 struct drm_i915_gem_exec_object2 *entry;
376
377 if (!obj->gtt_space)
378 return;
379
380 entry = obj->exec_entry;
381
382 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
383 i915_gem_object_unpin_fence(obj);
384
385 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
386 i915_gem_object_unpin(obj);
387
388 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100389}
390
Chris Wilson54cf91d2010-11-25 18:00:26 +0000391static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000392i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000393 struct drm_file *file,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000394 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000395{
Chris Wilson432e58e2010-11-25 19:32:06 +0000396 struct drm_i915_gem_object *obj;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000397 struct list_head ordered_objects;
Chris Wilson7788a762012-08-24 19:18:18 +0100398 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
399 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000400
401 INIT_LIST_HEAD(&ordered_objects);
402 while (!list_empty(objects)) {
403 struct drm_i915_gem_exec_object2 *entry;
404 bool need_fence, need_mappable;
405
406 obj = list_first_entry(objects,
407 struct drm_i915_gem_object,
408 exec_list);
409 entry = obj->exec_entry;
410
411 need_fence =
412 has_fenced_gpu_access &&
413 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
414 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200415 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000416
417 if (need_mappable)
418 list_move(&obj->exec_list, &ordered_objects);
419 else
420 list_move_tail(&obj->exec_list, &ordered_objects);
Chris Wilson595dad72011-01-13 11:03:48 +0000421
422 obj->base.pending_read_domains = 0;
423 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100424 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000425 }
426 list_splice(&ordered_objects, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000427
428 /* Attempt to pin all of the buffers into the GTT.
429 * This is done in 3 phases:
430 *
431 * 1a. Unbind all objects that do not match the GTT constraints for
432 * the execbuffer (fenceable, mappable, alignment etc).
433 * 1b. Increment pin count for already bound objects.
434 * 2. Bind new objects.
435 * 3. Decrement pin count.
436 *
Chris Wilson7788a762012-08-24 19:18:18 +0100437 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000438 * room for the earlier objects *unless* we need to defragment.
439 */
440 retry = 0;
441 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100442 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000443
444 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000445 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000446 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000447 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100448
Chris Wilson6fe4f142011-01-10 17:35:37 +0000449 if (!obj->gtt_space)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000450 continue;
451
452 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000453 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000454 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
455 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200456 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000457
458 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
459 (need_mappable && !obj->map_and_fenceable))
460 ret = i915_gem_object_unbind(obj);
461 else
Chris Wilson7788a762012-08-24 19:18:18 +0100462 ret = i915_gem_execbuffer_reserve_object(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000463 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000464 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000465 }
466
467 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000468 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100469 if (obj->gtt_space)
470 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000471
Chris Wilson7788a762012-08-24 19:18:18 +0100472 ret = i915_gem_execbuffer_reserve_object(obj, ring);
473 if (ret)
474 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000475 }
476
Chris Wilson7788a762012-08-24 19:18:18 +0100477err: /* Decrement pin count for bound objects */
478 list_for_each_entry(obj, objects, exec_list)
479 i915_gem_execbuffer_unreserve_object(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000480
Chris Wilson6c085a72012-08-20 11:40:46 +0200481 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000482 return ret;
483
Chris Wilson6c085a72012-08-20 11:40:46 +0200484 ret = i915_gem_evict_everything(ring->dev);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000485 if (ret)
486 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000487 } while (1);
488}
489
490static int
491i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
492 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000493 struct intel_ring_buffer *ring,
Chris Wilson432e58e2010-11-25 19:32:06 +0000494 struct list_head *objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000495 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000496 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000497 int count)
498{
499 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000500 struct drm_i915_gem_object *obj;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000501 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000502 int i, total, ret;
503
Chris Wilson67731b82010-12-08 10:38:14 +0000504 /* We may process another execbuffer during the unlock... */
Chris Wilson36cf1742011-01-10 12:09:12 +0000505 while (!list_empty(objects)) {
Chris Wilson67731b82010-12-08 10:38:14 +0000506 obj = list_first_entry(objects,
507 struct drm_i915_gem_object,
508 exec_list);
509 list_del_init(&obj->exec_list);
510 drm_gem_object_unreference(&obj->base);
511 }
512
Chris Wilson54cf91d2010-11-25 18:00:26 +0000513 mutex_unlock(&dev->struct_mutex);
514
515 total = 0;
516 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000517 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000518
Chris Wilsondd6864a2011-01-12 23:49:13 +0000519 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000520 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000521 if (reloc == NULL || reloc_offset == NULL) {
522 drm_free_large(reloc);
523 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000524 mutex_lock(&dev->struct_mutex);
525 return -ENOMEM;
526 }
527
528 total = 0;
529 for (i = 0; i < count; i++) {
530 struct drm_i915_gem_relocation_entry __user *user_relocs;
531
Chris Wilson432e58e2010-11-25 19:32:06 +0000532 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000533
534 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000535 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000536 ret = -EFAULT;
537 mutex_lock(&dev->struct_mutex);
538 goto err;
539 }
540
Chris Wilsondd6864a2011-01-12 23:49:13 +0000541 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000542 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000543 }
544
545 ret = i915_mutex_lock_interruptible(dev);
546 if (ret) {
547 mutex_lock(&dev->struct_mutex);
548 goto err;
549 }
550
Chris Wilson67731b82010-12-08 10:38:14 +0000551 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000552 eb_reset(eb);
553 for (i = 0; i < count; i++) {
Chris Wilson67731b82010-12-08 10:38:14 +0000554 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
555 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000556 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100557 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson67731b82010-12-08 10:38:14 +0000558 exec[i].handle, i);
559 ret = -ENOENT;
560 goto err;
561 }
562
563 list_add_tail(&obj->exec_list, objects);
564 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000565 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000566 eb_add_object(eb, obj);
567 }
568
Chris Wilson6fe4f142011-01-10 17:35:37 +0000569 ret = i915_gem_execbuffer_reserve(ring, file, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000570 if (ret)
571 goto err;
572
Chris Wilson432e58e2010-11-25 19:32:06 +0000573 list_for_each_entry(obj, objects, exec_list) {
Chris Wilsondd6864a2011-01-12 23:49:13 +0000574 int offset = obj->exec_entry - exec;
Chris Wilson67731b82010-12-08 10:38:14 +0000575 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilsondd6864a2011-01-12 23:49:13 +0000576 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000577 if (ret)
578 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000579 }
580
581 /* Leave the user relocations as are, this is the painfully slow path,
582 * and we want to avoid the complication of dropping the lock whilst
583 * having buffers reserved in the aperture and so causing spurious
584 * ENOSPC for random operations.
585 */
586
587err:
588 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000589 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000590 return ret;
591}
592
Chris Wilson54cf91d2010-11-25 18:00:26 +0000593static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000594i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
595 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000596{
Chris Wilson432e58e2010-11-25 19:32:06 +0000597 struct drm_i915_gem_object *obj;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200598 uint32_t flush_domains = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000599 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000600
Chris Wilson432e58e2010-11-25 19:32:06 +0000601 list_for_each_entry(obj, objects, exec_list) {
Ben Widawsky2911a352012-04-05 14:47:36 -0700602 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000603 if (ret)
604 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200605
606 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
607 i915_gem_clflush_object(obj);
608
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200609 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000610 }
611
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200612 if (flush_domains & I915_GEM_DOMAIN_CPU)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800613 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200614
615 if (flush_domains & I915_GEM_DOMAIN_GTT)
616 wmb();
617
Chris Wilson09cf7c92012-07-13 14:14:08 +0100618 /* Unconditionally invalidate gpu caches and ensure that we do flush
619 * any residual writes from the previous batch.
620 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100621 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000622}
623
Chris Wilson432e58e2010-11-25 19:32:06 +0000624static bool
625i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000626{
Chris Wilson432e58e2010-11-25 19:32:06 +0000627 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000628}
629
630static int
631validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
632 int count)
633{
634 int i;
635
636 for (i = 0; i < count; i++) {
637 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
638 int length; /* limited by fault_in_pages_readable() */
639
640 /* First check for malicious input causing overflow */
641 if (exec[i].relocation_count >
642 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
643 return -EINVAL;
644
645 length = exec[i].relocation_count *
646 sizeof(struct drm_i915_gem_relocation_entry);
647 if (!access_ok(VERIFY_READ, ptr, length))
648 return -EFAULT;
649
650 /* we may also need to update the presumed offsets */
651 if (!access_ok(VERIFY_WRITE, ptr, length))
652 return -EFAULT;
653
Daniel Vetterf56f8212012-03-25 19:47:41 +0200654 if (fault_in_multipages_readable(ptr, length))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000655 return -EFAULT;
656 }
657
658 return 0;
659}
660
Chris Wilson432e58e2010-11-25 19:32:06 +0000661static void
662i915_gem_execbuffer_move_to_active(struct list_head *objects,
Chris Wilson9d7730912012-11-27 16:22:52 +0000663 struct intel_ring_buffer *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000664{
665 struct drm_i915_gem_object *obj;
666
667 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson69c2fc82012-07-20 12:41:03 +0100668 u32 old_read = obj->base.read_domains;
669 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000670
Chris Wilson432e58e2010-11-25 19:32:06 +0000671 obj->base.read_domains = obj->base.pending_read_domains;
672 obj->base.write_domain = obj->base.pending_write_domain;
673 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
674
Chris Wilson9d7730912012-11-27 16:22:52 +0000675 i915_gem_object_move_to_active(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000676 if (obj->base.write_domain) {
677 obj->dirty = 1;
Chris Wilson9d7730912012-11-27 16:22:52 +0000678 obj->last_write_seqno = intel_ring_get_seqno(ring);
Chris Wilsonacb87df2012-05-03 15:47:57 +0100679 if (obj->pin_count) /* check for potential scanout */
Chris Wilsonf047e392012-07-21 12:31:41 +0100680 intel_mark_fb_busy(obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000681 }
682
Chris Wilsondb53a302011-02-03 11:57:46 +0000683 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000684 }
685}
686
Chris Wilson54cf91d2010-11-25 18:00:26 +0000687static void
688i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000689 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000690 struct intel_ring_buffer *ring)
691{
Daniel Vettercc889e02012-06-13 20:45:19 +0200692 /* Unconditionally force add_request to emit a full flush. */
693 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000694
Chris Wilson432e58e2010-11-25 19:32:06 +0000695 /* Add a breadcrumb for the completion of the batch buffer */
Chris Wilson3bb73ab2012-07-20 12:40:59 +0100696 (void)i915_add_request(ring, file, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000697}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000698
699static int
Eric Anholtae662d32012-01-03 09:23:29 -0800700i915_reset_gen7_sol_offsets(struct drm_device *dev,
701 struct intel_ring_buffer *ring)
702{
703 drm_i915_private_t *dev_priv = dev->dev_private;
704 int ret, i;
705
706 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
707 return 0;
708
709 ret = intel_ring_begin(ring, 4 * 3);
710 if (ret)
711 return ret;
712
713 for (i = 0; i < 4; i++) {
714 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
715 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
716 intel_ring_emit(ring, 0);
717 }
718
719 intel_ring_advance(ring);
720
721 return 0;
722}
723
724static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000725i915_gem_do_execbuffer(struct drm_device *dev, void *data,
726 struct drm_file *file,
727 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +0000728 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000729{
730 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson432e58e2010-11-25 19:32:06 +0000731 struct list_head objects;
Chris Wilson67731b82010-12-08 10:38:14 +0000732 struct eb_objects *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000733 struct drm_i915_gem_object *batch_obj;
734 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000735 struct intel_ring_buffer *ring;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700736 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000737 u32 exec_start, exec_len;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800738 u32 mask;
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100739 u32 flags;
Chris Wilson72bfa192010-12-19 11:42:05 +0000740 int ret, mode, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000741
Chris Wilson432e58e2010-11-25 19:32:06 +0000742 if (!i915_gem_check_execbuffer(args)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100743 DRM_DEBUG("execbuf with invalid offset/length\n");
Chris Wilson432e58e2010-11-25 19:32:06 +0000744 return -EINVAL;
745 }
746
747 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000748 if (ret)
749 return ret;
750
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100751 flags = 0;
752 if (args->flags & I915_EXEC_SECURE) {
753 if (!file->is_master || !capable(CAP_SYS_ADMIN))
754 return -EPERM;
755
756 flags |= I915_DISPATCH_SECURE;
757 }
Daniel Vetterb45305f2012-12-17 16:21:27 +0100758 if (args->flags & I915_EXEC_IS_PINNED)
759 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100760
Chris Wilson54cf91d2010-11-25 18:00:26 +0000761 switch (args->flags & I915_EXEC_RING_MASK) {
762 case I915_EXEC_DEFAULT:
763 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000764 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000765 break;
766 case I915_EXEC_BSD:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000767 ring = &dev_priv->ring[VCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700768 if (ctx_id != 0) {
769 DRM_DEBUG("Ring %s doesn't support contexts\n",
770 ring->name);
771 return -EPERM;
772 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000773 break;
774 case I915_EXEC_BLT:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000775 ring = &dev_priv->ring[BCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700776 if (ctx_id != 0) {
777 DRM_DEBUG("Ring %s doesn't support contexts\n",
778 ring->name);
779 return -EPERM;
780 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000781 break;
782 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100783 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000784 (int)(args->flags & I915_EXEC_RING_MASK));
785 return -EINVAL;
786 }
Chris Wilsona15817c2012-05-11 14:29:31 +0100787 if (!intel_ring_initialized(ring)) {
788 DRM_DEBUG("execbuf with invalid ring: %d\n",
789 (int)(args->flags & I915_EXEC_RING_MASK));
790 return -EINVAL;
791 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000792
Chris Wilson72bfa192010-12-19 11:42:05 +0000793 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800794 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +0000795 switch (mode) {
796 case I915_EXEC_CONSTANTS_REL_GENERAL:
797 case I915_EXEC_CONSTANTS_ABSOLUTE:
798 case I915_EXEC_CONSTANTS_REL_SURFACE:
799 if (ring == &dev_priv->ring[RCS] &&
800 mode != dev_priv->relative_constants_mode) {
801 if (INTEL_INFO(dev)->gen < 4)
802 return -EINVAL;
803
804 if (INTEL_INFO(dev)->gen > 5 &&
805 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
806 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800807
808 /* The HW changed the meaning on this bit on gen6 */
809 if (INTEL_INFO(dev)->gen >= 6)
810 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +0000811 }
812 break;
813 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100814 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +0000815 return -EINVAL;
816 }
817
Chris Wilson54cf91d2010-11-25 18:00:26 +0000818 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +0100819 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000820 return -EINVAL;
821 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000822
823 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000824 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +0100825 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000826 return -EINVAL;
827 }
828
Daniel Vetter6ebebc92012-04-26 23:28:11 +0200829 if (INTEL_INFO(dev)->gen >= 5) {
830 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
831 return -EINVAL;
832 }
833
Xi Wang44afb3a2012-04-23 04:06:42 -0400834 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
835 DRM_DEBUG("execbuf with %u cliprects\n",
836 args->num_cliprects);
837 return -EINVAL;
838 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200839
Chris Wilson432e58e2010-11-25 19:32:06 +0000840 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +0000841 GFP_KERNEL);
842 if (cliprects == NULL) {
843 ret = -ENOMEM;
844 goto pre_mutex_err;
845 }
846
Chris Wilson432e58e2010-11-25 19:32:06 +0000847 if (copy_from_user(cliprects,
848 (struct drm_clip_rect __user *)(uintptr_t)
849 args->cliprects_ptr,
850 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000851 ret = -EFAULT;
852 goto pre_mutex_err;
853 }
854 }
855
Chris Wilson54cf91d2010-11-25 18:00:26 +0000856 ret = i915_mutex_lock_interruptible(dev);
857 if (ret)
858 goto pre_mutex_err;
859
860 if (dev_priv->mm.suspended) {
861 mutex_unlock(&dev->struct_mutex);
862 ret = -EBUSY;
863 goto pre_mutex_err;
864 }
865
Chris Wilson67731b82010-12-08 10:38:14 +0000866 eb = eb_create(args->buffer_count);
867 if (eb == NULL) {
868 mutex_unlock(&dev->struct_mutex);
869 ret = -ENOMEM;
870 goto pre_mutex_err;
871 }
872
Chris Wilson54cf91d2010-11-25 18:00:26 +0000873 /* Look up object handles */
Chris Wilson432e58e2010-11-25 19:32:06 +0000874 INIT_LIST_HEAD(&objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000875 for (i = 0; i < args->buffer_count; i++) {
876 struct drm_i915_gem_object *obj;
877
Chris Wilson432e58e2010-11-25 19:32:06 +0000878 obj = to_intel_bo(drm_gem_object_lookup(dev, file,
879 exec[i].handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000880 if (&obj->base == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +0100881 DRM_DEBUG("Invalid object handle %d at index %d\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000882 exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000883 /* prevent error path from reading uninitialized data */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000884 ret = -ENOENT;
885 goto err;
886 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000887
Chris Wilson432e58e2010-11-25 19:32:06 +0000888 if (!list_empty(&obj->exec_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100889 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
Chris Wilson432e58e2010-11-25 19:32:06 +0000890 obj, exec[i].handle, i);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000891 ret = -EINVAL;
892 goto err;
893 }
Chris Wilson432e58e2010-11-25 19:32:06 +0000894
895 list_add_tail(&obj->exec_list, &objects);
Chris Wilson67731b82010-12-08 10:38:14 +0000896 obj->exec_handle = exec[i].handle;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000897 obj->exec_entry = &exec[i];
Chris Wilson67731b82010-12-08 10:38:14 +0000898 eb_add_object(eb, obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000899 }
900
Chris Wilson6fe4f142011-01-10 17:35:37 +0000901 /* take note of the batch buffer before we might reorder the lists */
902 batch_obj = list_entry(objects.prev,
903 struct drm_i915_gem_object,
904 exec_list);
905
Chris Wilson54cf91d2010-11-25 18:00:26 +0000906 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilson6fe4f142011-01-10 17:35:37 +0000907 ret = i915_gem_execbuffer_reserve(ring, file, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000908 if (ret)
909 goto err;
910
911 /* The objects are in their final locations, apply the relocations. */
Chris Wilson6fe4f142011-01-10 17:35:37 +0000912 ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000913 if (ret) {
914 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +0000915 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilson67731b82010-12-08 10:38:14 +0000916 &objects, eb,
917 exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000918 args->buffer_count);
919 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
920 }
921 if (ret)
922 goto err;
923 }
924
925 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000926 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +0100927 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +0000928 ret = -EINVAL;
929 goto err;
930 }
931 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
932
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100933 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
934 * batch" bit. Hence we need to pin secure batches into the global gtt.
935 * hsw should have this fixed, but let's be paranoid and do it
936 * unconditionally for now. */
937 if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
938 i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
939
Chris Wilson432e58e2010-11-25 19:32:06 +0000940 ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000941 if (ret)
942 goto err;
943
Eric Anholt0da5cec2012-07-23 12:33:55 -0700944 ret = i915_switch_context(ring, file, ctx_id);
945 if (ret)
946 goto err;
947
Ben Widawskye2971bd2011-12-12 19:21:57 -0800948 if (ring == &dev_priv->ring[RCS] &&
949 mode != dev_priv->relative_constants_mode) {
950 ret = intel_ring_begin(ring, 4);
951 if (ret)
952 goto err;
953
954 intel_ring_emit(ring, MI_NOOP);
955 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
956 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -0800957 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -0800958 intel_ring_advance(ring);
959
960 dev_priv->relative_constants_mode = mode;
961 }
962
Eric Anholtae662d32012-01-03 09:23:29 -0800963 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
964 ret = i915_reset_gen7_sol_offsets(dev, ring);
965 if (ret)
966 goto err;
967 }
968
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000969 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
970 exec_len = args->batch_len;
971 if (cliprects) {
972 for (i = 0; i < args->num_cliprects; i++) {
973 ret = i915_emit_box(dev, &cliprects[i],
974 args->DR1, args->DR4);
975 if (ret)
976 goto err;
977
978 ret = ring->dispatch_execbuffer(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100979 exec_start, exec_len,
980 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000981 if (ret)
982 goto err;
983 }
984 } else {
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100985 ret = ring->dispatch_execbuffer(ring,
986 exec_start, exec_len,
987 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000988 if (ret)
989 goto err;
990 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000991
Chris Wilson9d7730912012-11-27 16:22:52 +0000992 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
993
994 i915_gem_execbuffer_move_to_active(&objects, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000995 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000996
997err:
Chris Wilson67731b82010-12-08 10:38:14 +0000998 eb_destroy(eb);
Chris Wilson432e58e2010-11-25 19:32:06 +0000999 while (!list_empty(&objects)) {
1000 struct drm_i915_gem_object *obj;
1001
1002 obj = list_first_entry(&objects,
1003 struct drm_i915_gem_object,
1004 exec_list);
1005 list_del_init(&obj->exec_list);
1006 drm_gem_object_unreference(&obj->base);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001007 }
1008
1009 mutex_unlock(&dev->struct_mutex);
1010
1011pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001012 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001013 return ret;
1014}
1015
1016/*
1017 * Legacy execbuffer just creates an exec2 list from the original exec object
1018 * list array and passes it to the real function.
1019 */
1020int
1021i915_gem_execbuffer(struct drm_device *dev, void *data,
1022 struct drm_file *file)
1023{
1024 struct drm_i915_gem_execbuffer *args = data;
1025 struct drm_i915_gem_execbuffer2 exec2;
1026 struct drm_i915_gem_exec_object *exec_list = NULL;
1027 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1028 int ret, i;
1029
Chris Wilson54cf91d2010-11-25 18:00:26 +00001030 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001031 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001032 return -EINVAL;
1033 }
1034
1035 /* Copy in the exec list from userland */
1036 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1037 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1038 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001039 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001040 args->buffer_count);
1041 drm_free_large(exec_list);
1042 drm_free_large(exec2_list);
1043 return -ENOMEM;
1044 }
1045 ret = copy_from_user(exec_list,
Chris Wilsonba7a6452012-09-14 11:46:00 +01001046 (void __user *)(uintptr_t)args->buffers_ptr,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001047 sizeof(*exec_list) * args->buffer_count);
1048 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001049 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001050 args->buffer_count, ret);
1051 drm_free_large(exec_list);
1052 drm_free_large(exec2_list);
1053 return -EFAULT;
1054 }
1055
1056 for (i = 0; i < args->buffer_count; i++) {
1057 exec2_list[i].handle = exec_list[i].handle;
1058 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1059 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1060 exec2_list[i].alignment = exec_list[i].alignment;
1061 exec2_list[i].offset = exec_list[i].offset;
1062 if (INTEL_INFO(dev)->gen < 4)
1063 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1064 else
1065 exec2_list[i].flags = 0;
1066 }
1067
1068 exec2.buffers_ptr = args->buffers_ptr;
1069 exec2.buffer_count = args->buffer_count;
1070 exec2.batch_start_offset = args->batch_start_offset;
1071 exec2.batch_len = args->batch_len;
1072 exec2.DR1 = args->DR1;
1073 exec2.DR4 = args->DR4;
1074 exec2.num_cliprects = args->num_cliprects;
1075 exec2.cliprects_ptr = args->cliprects_ptr;
1076 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001077 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001078
1079 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1080 if (!ret) {
1081 /* Copy the new buffer offsets back to the user's exec list. */
1082 for (i = 0; i < args->buffer_count; i++)
1083 exec_list[i].offset = exec2_list[i].offset;
1084 /* ... and back out to userspace */
Chris Wilsonba7a6452012-09-14 11:46:00 +01001085 ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001086 exec_list,
1087 sizeof(*exec_list) * args->buffer_count);
1088 if (ret) {
1089 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001090 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001091 "back to user (%d)\n",
1092 args->buffer_count, ret);
1093 }
1094 }
1095
1096 drm_free_large(exec_list);
1097 drm_free_large(exec2_list);
1098 return ret;
1099}
1100
1101int
1102i915_gem_execbuffer2(struct drm_device *dev, void *data,
1103 struct drm_file *file)
1104{
1105 struct drm_i915_gem_execbuffer2 *args = data;
1106 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1107 int ret;
1108
Xi Wanged8cd3b2012-04-23 04:06:41 -04001109 if (args->buffer_count < 1 ||
1110 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001111 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001112 return -EINVAL;
1113 }
1114
Chris Wilson8408c282011-02-21 12:54:48 +00001115 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001116 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001117 if (exec2_list == NULL)
1118 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1119 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001120 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001121 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001122 args->buffer_count);
1123 return -ENOMEM;
1124 }
1125 ret = copy_from_user(exec2_list,
1126 (struct drm_i915_relocation_entry __user *)
1127 (uintptr_t) args->buffers_ptr,
1128 sizeof(*exec2_list) * args->buffer_count);
1129 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001130 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001131 args->buffer_count, ret);
1132 drm_free_large(exec2_list);
1133 return -EFAULT;
1134 }
1135
1136 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1137 if (!ret) {
1138 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilsonba7a6452012-09-14 11:46:00 +01001139 ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001140 exec2_list,
1141 sizeof(*exec2_list) * args->buffer_count);
1142 if (ret) {
1143 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001144 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001145 "back to user (%d)\n",
1146 args->buffer_count, ret);
1147 }
1148 }
1149
1150 drm_free_large(exec2_list);
1151 return ret;
1152}