blob: 386677f8fd384669f741b9f9c2a61a42d21a824c [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 {
Chris Wilsonbcffc3f2013-01-08 10:53:15 +000037 struct list_head objects;
Chris Wilson67731b82010-12-08 10:38:14 +000038 int and;
39 struct hlist_head buckets[0];
40};
41
42static struct eb_objects *
43eb_create(int size)
44{
45 struct eb_objects *eb;
46 int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Chris Wilson41783ee2012-09-18 10:04:02 +010047 BUILD_BUG_ON(!is_power_of_2(PAGE_SIZE / sizeof(struct hlist_head)));
Chris Wilson67731b82010-12-08 10:38:14 +000048 while (count > size)
49 count >>= 1;
50 eb = kzalloc(count*sizeof(struct hlist_head) +
51 sizeof(struct eb_objects),
52 GFP_KERNEL);
53 if (eb == NULL)
54 return eb;
55
56 eb->and = count - 1;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +000057 INIT_LIST_HEAD(&eb->objects);
Chris Wilson67731b82010-12-08 10:38:14 +000058 return eb;
59}
60
61static void
62eb_reset(struct eb_objects *eb)
63{
64 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
65}
66
67static void
68eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj)
69{
70 hlist_add_head(&obj->exec_node,
71 &eb->buckets[obj->exec_handle & eb->and]);
72}
73
Chris Wilson3b96eff2013-01-08 10:53:14 +000074static int
75eb_lookup_objects(struct eb_objects *eb,
76 struct drm_i915_gem_exec_object2 *exec,
77 int count,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +000078 struct drm_file *file)
Chris Wilson3b96eff2013-01-08 10:53:14 +000079{
80 int i;
81
82 spin_lock(&file->table_lock);
83 for (i = 0; i < count; i++) {
84 struct drm_i915_gem_object *obj;
85
86 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
87 if (obj == NULL) {
88 spin_unlock(&file->table_lock);
89 DRM_DEBUG("Invalid object handle %d at index %d\n",
90 exec[i].handle, i);
91 return -ENOENT;
92 }
93
94 if (!list_empty(&obj->exec_list)) {
95 spin_unlock(&file->table_lock);
96 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
97 obj, exec[i].handle, i);
98 return -EINVAL;
99 }
100
101 drm_gem_object_reference(&obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000102 list_add_tail(&obj->exec_list, &eb->objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000103
104 obj->exec_handle = exec[i].handle;
105 obj->exec_entry = &exec[i];
106 eb_add_object(eb, obj);
107 }
108 spin_unlock(&file->table_lock);
109
110 return 0;
111}
112
Chris Wilson67731b82010-12-08 10:38:14 +0000113static struct drm_i915_gem_object *
114eb_get_object(struct eb_objects *eb, unsigned long handle)
115{
116 struct hlist_head *head;
117 struct hlist_node *node;
118 struct drm_i915_gem_object *obj;
119
120 head = &eb->buckets[handle & eb->and];
121 hlist_for_each(node, head) {
122 obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
123 if (obj->exec_handle == handle)
124 return obj;
125 }
126
127 return NULL;
128}
129
130static void
131eb_destroy(struct eb_objects *eb)
132{
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000133 while (!list_empty(&eb->objects)) {
134 struct drm_i915_gem_object *obj;
135
136 obj = list_first_entry(&eb->objects,
137 struct drm_i915_gem_object,
138 exec_list);
139 list_del_init(&obj->exec_list);
140 drm_gem_object_unreference(&obj->base);
141 }
Chris Wilson67731b82010-12-08 10:38:14 +0000142 kfree(eb);
143}
144
Chris Wilsondabdfe02012-03-26 10:10:27 +0200145static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
146{
147 return (obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100148 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200149 obj->cache_level != I915_CACHE_NONE);
150}
151
Chris Wilson54cf91d2010-11-25 18:00:26 +0000152static int
153i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000154 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000155 struct drm_i915_gem_relocation_entry *reloc)
156{
157 struct drm_device *dev = obj->base.dev;
158 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100159 struct drm_i915_gem_object *target_i915_obj;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000160 uint32_t target_offset;
161 int ret = -EINVAL;
162
Chris Wilson67731b82010-12-08 10:38:14 +0000163 /* we've already hold a reference to all valid objects */
164 target_obj = &eb_get_object(eb, reloc->target_handle)->base;
165 if (unlikely(target_obj == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000166 return -ENOENT;
167
Daniel Vetter149c8402012-02-15 23:50:23 +0100168 target_i915_obj = to_intel_bo(target_obj);
169 target_offset = target_i915_obj->gtt_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000170
Eric Anholte844b992012-07-31 15:35:01 -0700171 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
172 * pipe_control writes because the gpu doesn't properly redirect them
173 * through the ppgtt for non_secure batchbuffers. */
174 if (unlikely(IS_GEN6(dev) &&
175 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
176 !target_i915_obj->has_global_gtt_mapping)) {
177 i915_gem_gtt_bind_object(target_i915_obj,
178 target_i915_obj->cache_level);
179 }
180
Chris Wilson54cf91d2010-11-25 18:00:26 +0000181 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000182 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100183 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000184 "obj %p target %d offset %d "
185 "read %08x write %08x",
186 obj, reloc->target_handle,
187 (int) reloc->offset,
188 reloc->read_domains,
189 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000190 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000191 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100192 if (unlikely((reloc->write_domain | reloc->read_domains)
193 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100194 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000195 "obj %p target %d offset %d "
196 "read %08x write %08x",
197 obj, reloc->target_handle,
198 (int) reloc->offset,
199 reloc->read_domains,
200 reloc->write_domain);
Chris Wilson67731b82010-12-08 10:38:14 +0000201 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000202 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000203
204 target_obj->pending_read_domains |= reloc->read_domains;
205 target_obj->pending_write_domain |= reloc->write_domain;
206
207 /* If the relocation already has the right value in it, no
208 * more work needs to be done.
209 */
210 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000211 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000212
213 /* Check that the relocation address is valid... */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000214 if (unlikely(reloc->offset > obj->base.size - 4)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100215 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000216 "obj %p target %d offset %d size %d.\n",
217 obj, reloc->target_handle,
218 (int) reloc->offset,
219 (int) obj->base.size);
Chris Wilson67731b82010-12-08 10:38:14 +0000220 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000221 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000222 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100223 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000224 "obj %p target %d offset %d.\n",
225 obj, reloc->target_handle,
226 (int) reloc->offset);
Chris Wilson67731b82010-12-08 10:38:14 +0000227 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000228 }
229
Chris Wilsondabdfe02012-03-26 10:10:27 +0200230 /* We can't wait for rendering with pagefaults disabled */
231 if (obj->active && in_atomic())
232 return -EFAULT;
233
Chris Wilson54cf91d2010-11-25 18:00:26 +0000234 reloc->delta += target_offset;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200235 if (use_cpu_reloc(obj)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000236 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
237 char *vaddr;
238
Chris Wilsondabdfe02012-03-26 10:10:27 +0200239 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
240 if (ret)
241 return ret;
242
Chris Wilson9da3da62012-06-01 15:20:22 +0100243 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
244 reloc->offset >> PAGE_SHIFT));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000245 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
246 kunmap_atomic(vaddr);
247 } else {
248 struct drm_i915_private *dev_priv = dev->dev_private;
249 uint32_t __iomem *reloc_entry;
250 void __iomem *reloc_page;
251
Chris Wilson7b096382012-04-14 09:55:51 +0100252 ret = i915_gem_object_set_to_gtt_domain(obj, true);
253 if (ret)
254 return ret;
255
256 ret = i915_gem_object_put_fence(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000257 if (ret)
Chris Wilson67731b82010-12-08 10:38:14 +0000258 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000259
260 /* Map the page containing the relocation we're going to perform. */
261 reloc->offset += obj->gtt_offset;
262 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
263 reloc->offset & PAGE_MASK);
264 reloc_entry = (uint32_t __iomem *)
265 (reloc_page + (reloc->offset & ~PAGE_MASK));
266 iowrite32(reloc->delta, reloc_entry);
267 io_mapping_unmap_atomic(reloc_page);
268 }
269
270 /* and update the user's relocation entry */
271 reloc->presumed_offset = target_offset;
272
Chris Wilson67731b82010-12-08 10:38:14 +0000273 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000274}
275
276static int
277i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000278 struct eb_objects *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000279{
Chris Wilson1d83f442012-03-24 20:12:53 +0000280#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
281 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000282 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000283 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000284 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000285
286 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000287
Chris Wilson1d83f442012-03-24 20:12:53 +0000288 remain = entry->relocation_count;
289 while (remain) {
290 struct drm_i915_gem_relocation_entry *r = stack_reloc;
291 int count = remain;
292 if (count > ARRAY_SIZE(stack_reloc))
293 count = ARRAY_SIZE(stack_reloc);
294 remain -= count;
295
296 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000297 return -EFAULT;
298
Chris Wilson1d83f442012-03-24 20:12:53 +0000299 do {
300 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000301
Chris Wilson1d83f442012-03-24 20:12:53 +0000302 ret = i915_gem_execbuffer_relocate_entry(obj, eb, r);
303 if (ret)
304 return ret;
305
306 if (r->presumed_offset != offset &&
307 __copy_to_user_inatomic(&user_relocs->presumed_offset,
308 &r->presumed_offset,
309 sizeof(r->presumed_offset))) {
310 return -EFAULT;
311 }
312
313 user_relocs++;
314 r++;
315 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000316 }
317
318 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000319#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000320}
321
322static int
323i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
Chris Wilson67731b82010-12-08 10:38:14 +0000324 struct eb_objects *eb,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000325 struct drm_i915_gem_relocation_entry *relocs)
326{
Chris Wilson6fe4f142011-01-10 17:35:37 +0000327 const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000328 int i, ret;
329
330 for (i = 0; i < entry->relocation_count; i++) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000331 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000332 if (ret)
333 return ret;
334 }
335
336 return 0;
337}
338
339static int
340i915_gem_execbuffer_relocate(struct drm_device *dev,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000341 struct eb_objects *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000342{
Chris Wilson432e58e2010-11-25 19:32:06 +0000343 struct drm_i915_gem_object *obj;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000344 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000345
Chris Wilsond4aeee72011-03-14 15:11:24 +0000346 /* This is the fast path and we cannot handle a pagefault whilst
347 * holding the struct mutex lest the user pass in the relocations
348 * contained within a mmaped bo. For in such a case we, the page
349 * fault handler would call i915_gem_fault() and we would try to
350 * acquire the struct mutex again. Obviously this is bad and so
351 * lockdep complains vehemently.
352 */
353 pagefault_disable();
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000354 list_for_each_entry(obj, &eb->objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000355 ret = i915_gem_execbuffer_relocate_object(obj, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000356 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000357 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000358 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000359 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000360
Chris Wilsond4aeee72011-03-14 15:11:24 +0000361 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000362}
363
Chris Wilson7788a762012-08-24 19:18:18 +0100364#define __EXEC_OBJECT_HAS_PIN (1<<31)
365#define __EXEC_OBJECT_HAS_FENCE (1<<30)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100366
367static int
Chris Wilsondabdfe02012-03-26 10:10:27 +0200368need_reloc_mappable(struct drm_i915_gem_object *obj)
369{
370 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
371 return entry->relocation_count && !use_cpu_reloc(obj);
372}
373
374static int
Chris Wilson7788a762012-08-24 19:18:18 +0100375i915_gem_execbuffer_reserve_object(struct drm_i915_gem_object *obj,
376 struct intel_ring_buffer *ring)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100377{
Chris Wilson7788a762012-08-24 19:18:18 +0100378 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100379 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
380 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
381 bool need_fence, need_mappable;
382 int ret;
383
384 need_fence =
385 has_fenced_gpu_access &&
386 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
387 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200388 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100389
Chris Wilson86a1ee22012-08-11 15:41:04 +0100390 ret = i915_gem_object_pin(obj, entry->alignment, need_mappable, false);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100391 if (ret)
392 return ret;
393
Chris Wilson7788a762012-08-24 19:18:18 +0100394 entry->flags |= __EXEC_OBJECT_HAS_PIN;
395
Chris Wilson1690e1e2011-12-14 13:57:08 +0100396 if (has_fenced_gpu_access) {
397 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
Chris Wilson06d98132012-04-17 15:31:24 +0100398 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000399 if (ret)
Chris Wilson7788a762012-08-24 19:18:18 +0100400 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100401
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000402 if (i915_gem_object_pin_fence(obj))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100403 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson9a5a53b2012-03-22 15:10:00 +0000404
Chris Wilson7dd49062012-03-21 10:48:18 +0000405 obj->pending_fenced_gpu_access = true;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100406 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100407 }
408
Chris Wilson7788a762012-08-24 19:18:18 +0100409 /* Ensure ppgtt mapping exists if needed */
410 if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
411 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
412 obj, obj->cache_level);
413
414 obj->has_aliasing_ppgtt_mapping = 1;
415 }
416
Chris Wilson1690e1e2011-12-14 13:57:08 +0100417 entry->offset = obj->gtt_offset;
418 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100419}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100420
Chris Wilson7788a762012-08-24 19:18:18 +0100421static void
422i915_gem_execbuffer_unreserve_object(struct drm_i915_gem_object *obj)
423{
424 struct drm_i915_gem_exec_object2 *entry;
425
426 if (!obj->gtt_space)
427 return;
428
429 entry = obj->exec_entry;
430
431 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
432 i915_gem_object_unpin_fence(obj);
433
434 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
435 i915_gem_object_unpin(obj);
436
437 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100438}
439
Chris Wilson54cf91d2010-11-25 18:00:26 +0000440static int
Chris Wilsond9e86c02010-11-10 16:40:20 +0000441i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000442 struct drm_file *file,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000443 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000444{
Chris Wilson432e58e2010-11-25 19:32:06 +0000445 struct drm_i915_gem_object *obj;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000446 struct list_head ordered_objects;
Chris Wilson7788a762012-08-24 19:18:18 +0100447 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
448 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000449
450 INIT_LIST_HEAD(&ordered_objects);
451 while (!list_empty(objects)) {
452 struct drm_i915_gem_exec_object2 *entry;
453 bool need_fence, need_mappable;
454
455 obj = list_first_entry(objects,
456 struct drm_i915_gem_object,
457 exec_list);
458 entry = obj->exec_entry;
459
460 need_fence =
461 has_fenced_gpu_access &&
462 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
463 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200464 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000465
466 if (need_mappable)
467 list_move(&obj->exec_list, &ordered_objects);
468 else
469 list_move_tail(&obj->exec_list, &ordered_objects);
Chris Wilson595dad72011-01-13 11:03:48 +0000470
471 obj->base.pending_read_domains = 0;
472 obj->base.pending_write_domain = 0;
Chris Wilson016fd0c2012-07-20 12:41:07 +0100473 obj->pending_fenced_gpu_access = false;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000474 }
475 list_splice(&ordered_objects, objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000476
477 /* Attempt to pin all of the buffers into the GTT.
478 * This is done in 3 phases:
479 *
480 * 1a. Unbind all objects that do not match the GTT constraints for
481 * the execbuffer (fenceable, mappable, alignment etc).
482 * 1b. Increment pin count for already bound objects.
483 * 2. Bind new objects.
484 * 3. Decrement pin count.
485 *
Chris Wilson7788a762012-08-24 19:18:18 +0100486 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000487 * room for the earlier objects *unless* we need to defragment.
488 */
489 retry = 0;
490 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100491 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000492
493 /* Unbind any ill-fitting objects or pin. */
Chris Wilson432e58e2010-11-25 19:32:06 +0000494 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000495 struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000496 bool need_fence, need_mappable;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100497
Chris Wilson6fe4f142011-01-10 17:35:37 +0000498 if (!obj->gtt_space)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000499 continue;
500
501 need_fence =
Chris Wilson9b3826b2010-12-05 17:11:54 +0000502 has_fenced_gpu_access &&
Chris Wilson54cf91d2010-11-25 18:00:26 +0000503 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
504 obj->tiling_mode != I915_TILING_NONE;
Chris Wilsondabdfe02012-03-26 10:10:27 +0200505 need_mappable = need_fence || need_reloc_mappable(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000506
507 if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
508 (need_mappable && !obj->map_and_fenceable))
509 ret = i915_gem_object_unbind(obj);
510 else
Chris Wilson7788a762012-08-24 19:18:18 +0100511 ret = i915_gem_execbuffer_reserve_object(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000512 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000513 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000514 }
515
516 /* Bind fresh objects */
Chris Wilson432e58e2010-11-25 19:32:06 +0000517 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +0100518 if (obj->gtt_space)
519 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000520
Chris Wilson7788a762012-08-24 19:18:18 +0100521 ret = i915_gem_execbuffer_reserve_object(obj, ring);
522 if (ret)
523 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000524 }
525
Chris Wilson7788a762012-08-24 19:18:18 +0100526err: /* Decrement pin count for bound objects */
527 list_for_each_entry(obj, objects, exec_list)
528 i915_gem_execbuffer_unreserve_object(obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000529
Chris Wilson6c085a72012-08-20 11:40:46 +0200530 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000531 return ret;
532
Chris Wilson6c085a72012-08-20 11:40:46 +0200533 ret = i915_gem_evict_everything(ring->dev);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000534 if (ret)
535 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000536 } while (1);
537}
538
539static int
540i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
541 struct drm_file *file,
Chris Wilsond9e86c02010-11-10 16:40:20 +0000542 struct intel_ring_buffer *ring,
Chris Wilson67731b82010-12-08 10:38:14 +0000543 struct eb_objects *eb,
Chris Wilson432e58e2010-11-25 19:32:06 +0000544 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000545 int count)
546{
547 struct drm_i915_gem_relocation_entry *reloc;
Chris Wilson432e58e2010-11-25 19:32:06 +0000548 struct drm_i915_gem_object *obj;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000549 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000550 int i, total, ret;
551
Chris Wilson67731b82010-12-08 10:38:14 +0000552 /* We may process another execbuffer during the unlock... */
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000553 while (!list_empty(&eb->objects)) {
554 obj = list_first_entry(&eb->objects,
Chris Wilson67731b82010-12-08 10:38:14 +0000555 struct drm_i915_gem_object,
556 exec_list);
557 list_del_init(&obj->exec_list);
558 drm_gem_object_unreference(&obj->base);
559 }
560
Chris Wilson54cf91d2010-11-25 18:00:26 +0000561 mutex_unlock(&dev->struct_mutex);
562
563 total = 0;
564 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000565 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000566
Chris Wilsondd6864a2011-01-12 23:49:13 +0000567 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000568 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000569 if (reloc == NULL || reloc_offset == NULL) {
570 drm_free_large(reloc);
571 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000572 mutex_lock(&dev->struct_mutex);
573 return -ENOMEM;
574 }
575
576 total = 0;
577 for (i = 0; i < count; i++) {
578 struct drm_i915_gem_relocation_entry __user *user_relocs;
579
Chris Wilson432e58e2010-11-25 19:32:06 +0000580 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000581
582 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000583 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000584 ret = -EFAULT;
585 mutex_lock(&dev->struct_mutex);
586 goto err;
587 }
588
Chris Wilsondd6864a2011-01-12 23:49:13 +0000589 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000590 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000591 }
592
593 ret = i915_mutex_lock_interruptible(dev);
594 if (ret) {
595 mutex_lock(&dev->struct_mutex);
596 goto err;
597 }
598
Chris Wilson67731b82010-12-08 10:38:14 +0000599 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000600 eb_reset(eb);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000601 ret = eb_lookup_objects(eb, exec, count, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000602 if (ret)
603 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000604
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000605 ret = i915_gem_execbuffer_reserve(ring, file, &eb->objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000606 if (ret)
607 goto err;
608
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000609 list_for_each_entry(obj, &eb->objects, exec_list) {
Chris Wilsondd6864a2011-01-12 23:49:13 +0000610 int offset = obj->exec_entry - exec;
Chris Wilson67731b82010-12-08 10:38:14 +0000611 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
Chris Wilsondd6864a2011-01-12 23:49:13 +0000612 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000613 if (ret)
614 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000615 }
616
617 /* Leave the user relocations as are, this is the painfully slow path,
618 * and we want to avoid the complication of dropping the lock whilst
619 * having buffers reserved in the aperture and so causing spurious
620 * ENOSPC for random operations.
621 */
622
623err:
624 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000625 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000626 return ret;
627}
628
Chris Wilson54cf91d2010-11-25 18:00:26 +0000629static int
Chris Wilson432e58e2010-11-25 19:32:06 +0000630i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
631 struct list_head *objects)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000632{
Chris Wilson432e58e2010-11-25 19:32:06 +0000633 struct drm_i915_gem_object *obj;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200634 uint32_t flush_domains = 0;
Chris Wilson432e58e2010-11-25 19:32:06 +0000635 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000636
Chris Wilson432e58e2010-11-25 19:32:06 +0000637 list_for_each_entry(obj, objects, exec_list) {
Ben Widawsky2911a352012-04-05 14:47:36 -0700638 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000639 if (ret)
640 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200641
642 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
643 i915_gem_clflush_object(obj);
644
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200645 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000646 }
647
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200648 if (flush_domains & I915_GEM_DOMAIN_CPU)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800649 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200650
651 if (flush_domains & I915_GEM_DOMAIN_GTT)
652 wmb();
653
Chris Wilson09cf7c92012-07-13 14:14:08 +0100654 /* Unconditionally invalidate gpu caches and ensure that we do flush
655 * any residual writes from the previous batch.
656 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100657 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000658}
659
Chris Wilson432e58e2010-11-25 19:32:06 +0000660static bool
661i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000662{
Chris Wilson432e58e2010-11-25 19:32:06 +0000663 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000664}
665
666static int
667validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
668 int count)
669{
670 int i;
671
672 for (i = 0; i < count; i++) {
673 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
674 int length; /* limited by fault_in_pages_readable() */
675
676 /* First check for malicious input causing overflow */
677 if (exec[i].relocation_count >
678 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
679 return -EINVAL;
680
681 length = exec[i].relocation_count *
682 sizeof(struct drm_i915_gem_relocation_entry);
683 if (!access_ok(VERIFY_READ, ptr, length))
684 return -EFAULT;
685
686 /* we may also need to update the presumed offsets */
687 if (!access_ok(VERIFY_WRITE, ptr, length))
688 return -EFAULT;
689
Daniel Vetterf56f8212012-03-25 19:47:41 +0200690 if (fault_in_multipages_readable(ptr, length))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000691 return -EFAULT;
692 }
693
694 return 0;
695}
696
Chris Wilson432e58e2010-11-25 19:32:06 +0000697static void
698i915_gem_execbuffer_move_to_active(struct list_head *objects,
Chris Wilson9d7730912012-11-27 16:22:52 +0000699 struct intel_ring_buffer *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000700{
701 struct drm_i915_gem_object *obj;
702
703 list_for_each_entry(obj, objects, exec_list) {
Chris Wilson69c2fc82012-07-20 12:41:03 +0100704 u32 old_read = obj->base.read_domains;
705 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000706
Chris Wilson432e58e2010-11-25 19:32:06 +0000707 obj->base.read_domains = obj->base.pending_read_domains;
708 obj->base.write_domain = obj->base.pending_write_domain;
709 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
710
Chris Wilson9d7730912012-11-27 16:22:52 +0000711 i915_gem_object_move_to_active(obj, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000712 if (obj->base.write_domain) {
713 obj->dirty = 1;
Chris Wilson9d7730912012-11-27 16:22:52 +0000714 obj->last_write_seqno = intel_ring_get_seqno(ring);
Chris Wilsonacb87df2012-05-03 15:47:57 +0100715 if (obj->pin_count) /* check for potential scanout */
Chris Wilsonf047e392012-07-21 12:31:41 +0100716 intel_mark_fb_busy(obj);
Chris Wilson432e58e2010-11-25 19:32:06 +0000717 }
718
Chris Wilsondb53a302011-02-03 11:57:46 +0000719 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000720 }
721}
722
Chris Wilson54cf91d2010-11-25 18:00:26 +0000723static void
724i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000725 struct drm_file *file,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000726 struct intel_ring_buffer *ring)
727{
Daniel Vettercc889e02012-06-13 20:45:19 +0200728 /* Unconditionally force add_request to emit a full flush. */
729 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000730
Chris Wilson432e58e2010-11-25 19:32:06 +0000731 /* Add a breadcrumb for the completion of the batch buffer */
Chris Wilson3bb73ab2012-07-20 12:40:59 +0100732 (void)i915_add_request(ring, file, NULL);
Chris Wilson432e58e2010-11-25 19:32:06 +0000733}
Chris Wilson54cf91d2010-11-25 18:00:26 +0000734
735static int
Eric Anholtae662d32012-01-03 09:23:29 -0800736i915_reset_gen7_sol_offsets(struct drm_device *dev,
737 struct intel_ring_buffer *ring)
738{
739 drm_i915_private_t *dev_priv = dev->dev_private;
740 int ret, i;
741
742 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
743 return 0;
744
745 ret = intel_ring_begin(ring, 4 * 3);
746 if (ret)
747 return ret;
748
749 for (i = 0; i < 4; i++) {
750 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
751 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
752 intel_ring_emit(ring, 0);
753 }
754
755 intel_ring_advance(ring);
756
757 return 0;
758}
759
760static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000761i915_gem_do_execbuffer(struct drm_device *dev, void *data,
762 struct drm_file *file,
763 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson432e58e2010-11-25 19:32:06 +0000764 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000765{
766 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson67731b82010-12-08 10:38:14 +0000767 struct eb_objects *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000768 struct drm_i915_gem_object *batch_obj;
769 struct drm_clip_rect *cliprects = NULL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000770 struct intel_ring_buffer *ring;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700771 u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000772 u32 exec_start, exec_len;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800773 u32 mask;
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100774 u32 flags;
Chris Wilson72bfa192010-12-19 11:42:05 +0000775 int ret, mode, i;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000776
Chris Wilson432e58e2010-11-25 19:32:06 +0000777 if (!i915_gem_check_execbuffer(args)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100778 DRM_DEBUG("execbuf with invalid offset/length\n");
Chris Wilson432e58e2010-11-25 19:32:06 +0000779 return -EINVAL;
780 }
781
782 ret = validate_exec_list(exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000783 if (ret)
784 return ret;
785
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100786 flags = 0;
787 if (args->flags & I915_EXEC_SECURE) {
788 if (!file->is_master || !capable(CAP_SYS_ADMIN))
789 return -EPERM;
790
791 flags |= I915_DISPATCH_SECURE;
792 }
Daniel Vetterb45305f2012-12-17 16:21:27 +0100793 if (args->flags & I915_EXEC_IS_PINNED)
794 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100795
Chris Wilson54cf91d2010-11-25 18:00:26 +0000796 switch (args->flags & I915_EXEC_RING_MASK) {
797 case I915_EXEC_DEFAULT:
798 case I915_EXEC_RENDER:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000799 ring = &dev_priv->ring[RCS];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000800 break;
801 case I915_EXEC_BSD:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000802 ring = &dev_priv->ring[VCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700803 if (ctx_id != 0) {
804 DRM_DEBUG("Ring %s doesn't support contexts\n",
805 ring->name);
806 return -EPERM;
807 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000808 break;
809 case I915_EXEC_BLT:
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000810 ring = &dev_priv->ring[BCS];
Ben Widawsky6e0a69d2012-06-04 14:42:55 -0700811 if (ctx_id != 0) {
812 DRM_DEBUG("Ring %s doesn't support contexts\n",
813 ring->name);
814 return -EPERM;
815 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000816 break;
817 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100818 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +0000819 (int)(args->flags & I915_EXEC_RING_MASK));
820 return -EINVAL;
821 }
Chris Wilsona15817c2012-05-11 14:29:31 +0100822 if (!intel_ring_initialized(ring)) {
823 DRM_DEBUG("execbuf with invalid ring: %d\n",
824 (int)(args->flags & I915_EXEC_RING_MASK));
825 return -EINVAL;
826 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000827
Chris Wilson72bfa192010-12-19 11:42:05 +0000828 mode = args->flags & I915_EXEC_CONSTANTS_MASK;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800829 mask = I915_EXEC_CONSTANTS_MASK;
Chris Wilson72bfa192010-12-19 11:42:05 +0000830 switch (mode) {
831 case I915_EXEC_CONSTANTS_REL_GENERAL:
832 case I915_EXEC_CONSTANTS_ABSOLUTE:
833 case I915_EXEC_CONSTANTS_REL_SURFACE:
834 if (ring == &dev_priv->ring[RCS] &&
835 mode != dev_priv->relative_constants_mode) {
836 if (INTEL_INFO(dev)->gen < 4)
837 return -EINVAL;
838
839 if (INTEL_INFO(dev)->gen > 5 &&
840 mode == I915_EXEC_CONSTANTS_REL_SURFACE)
841 return -EINVAL;
Ben Widawsky84f9f932011-12-12 19:21:58 -0800842
843 /* The HW changed the meaning on this bit on gen6 */
844 if (INTEL_INFO(dev)->gen >= 6)
845 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
Chris Wilson72bfa192010-12-19 11:42:05 +0000846 }
847 break;
848 default:
Daniel Vetterff240192012-01-31 21:08:14 +0100849 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
Chris Wilson72bfa192010-12-19 11:42:05 +0000850 return -EINVAL;
851 }
852
Chris Wilson54cf91d2010-11-25 18:00:26 +0000853 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +0100854 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000855 return -EINVAL;
856 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000857
858 if (args->num_cliprects != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000859 if (ring != &dev_priv->ring[RCS]) {
Daniel Vetterff240192012-01-31 21:08:14 +0100860 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000861 return -EINVAL;
862 }
863
Daniel Vetter6ebebc92012-04-26 23:28:11 +0200864 if (INTEL_INFO(dev)->gen >= 5) {
865 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
866 return -EINVAL;
867 }
868
Xi Wang44afb3a2012-04-23 04:06:42 -0400869 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
870 DRM_DEBUG("execbuf with %u cliprects\n",
871 args->num_cliprects);
872 return -EINVAL;
873 }
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200874
Chris Wilson432e58e2010-11-25 19:32:06 +0000875 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
Chris Wilson54cf91d2010-11-25 18:00:26 +0000876 GFP_KERNEL);
877 if (cliprects == NULL) {
878 ret = -ENOMEM;
879 goto pre_mutex_err;
880 }
881
Chris Wilson432e58e2010-11-25 19:32:06 +0000882 if (copy_from_user(cliprects,
883 (struct drm_clip_rect __user *)(uintptr_t)
884 args->cliprects_ptr,
885 sizeof(*cliprects)*args->num_cliprects)) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000886 ret = -EFAULT;
887 goto pre_mutex_err;
888 }
889 }
890
Chris Wilson54cf91d2010-11-25 18:00:26 +0000891 ret = i915_mutex_lock_interruptible(dev);
892 if (ret)
893 goto pre_mutex_err;
894
895 if (dev_priv->mm.suspended) {
896 mutex_unlock(&dev->struct_mutex);
897 ret = -EBUSY;
898 goto pre_mutex_err;
899 }
900
Chris Wilson67731b82010-12-08 10:38:14 +0000901 eb = eb_create(args->buffer_count);
902 if (eb == NULL) {
903 mutex_unlock(&dev->struct_mutex);
904 ret = -ENOMEM;
905 goto pre_mutex_err;
906 }
907
Chris Wilson54cf91d2010-11-25 18:00:26 +0000908 /* Look up object handles */
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000909 ret = eb_lookup_objects(eb, exec, args->buffer_count, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000910 if (ret)
911 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000912
Chris Wilson6fe4f142011-01-10 17:35:37 +0000913 /* take note of the batch buffer before we might reorder the lists */
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000914 batch_obj = list_entry(eb->objects.prev,
Chris Wilson6fe4f142011-01-10 17:35:37 +0000915 struct drm_i915_gem_object,
916 exec_list);
917
Chris Wilson54cf91d2010-11-25 18:00:26 +0000918 /* Move the objects en-masse into the GTT, evicting if necessary. */
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000919 ret = i915_gem_execbuffer_reserve(ring, file, &eb->objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000920 if (ret)
921 goto err;
922
923 /* The objects are in their final locations, apply the relocations. */
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000924 ret = i915_gem_execbuffer_relocate(dev, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000925 if (ret) {
926 if (ret == -EFAULT) {
Chris Wilsond9e86c02010-11-10 16:40:20 +0000927 ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000928 eb, exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000929 args->buffer_count);
930 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
931 }
932 if (ret)
933 goto err;
934 }
935
936 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000937 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +0100938 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +0000939 ret = -EINVAL;
940 goto err;
941 }
942 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
943
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100944 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
945 * batch" bit. Hence we need to pin secure batches into the global gtt.
946 * hsw should have this fixed, but let's be paranoid and do it
947 * unconditionally for now. */
948 if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
949 i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
950
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000951 ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->objects);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000952 if (ret)
953 goto err;
954
Eric Anholt0da5cec2012-07-23 12:33:55 -0700955 ret = i915_switch_context(ring, file, ctx_id);
956 if (ret)
957 goto err;
958
Ben Widawskye2971bd2011-12-12 19:21:57 -0800959 if (ring == &dev_priv->ring[RCS] &&
960 mode != dev_priv->relative_constants_mode) {
961 ret = intel_ring_begin(ring, 4);
962 if (ret)
963 goto err;
964
965 intel_ring_emit(ring, MI_NOOP);
966 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
967 intel_ring_emit(ring, INSTPM);
Ben Widawsky84f9f932011-12-12 19:21:58 -0800968 intel_ring_emit(ring, mask << 16 | mode);
Ben Widawskye2971bd2011-12-12 19:21:57 -0800969 intel_ring_advance(ring);
970
971 dev_priv->relative_constants_mode = mode;
972 }
973
Eric Anholtae662d32012-01-03 09:23:29 -0800974 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
975 ret = i915_reset_gen7_sol_offsets(dev, ring);
976 if (ret)
977 goto err;
978 }
979
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000980 exec_start = batch_obj->gtt_offset + args->batch_start_offset;
981 exec_len = args->batch_len;
982 if (cliprects) {
983 for (i = 0; i < args->num_cliprects; i++) {
984 ret = i915_emit_box(dev, &cliprects[i],
985 args->DR1, args->DR4);
986 if (ret)
987 goto err;
988
989 ret = ring->dispatch_execbuffer(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100990 exec_start, exec_len,
991 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000992 if (ret)
993 goto err;
994 }
995 } else {
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100996 ret = ring->dispatch_execbuffer(ring,
997 exec_start, exec_len,
998 flags);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000999 if (ret)
1000 goto err;
1001 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001002
Chris Wilson9d7730912012-11-27 16:22:52 +00001003 trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
1004
Chris Wilsonbcffc3f2013-01-08 10:53:15 +00001005 i915_gem_execbuffer_move_to_active(&eb->objects, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +00001006 i915_gem_execbuffer_retire_commands(dev, file, ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001007
1008err:
Chris Wilson67731b82010-12-08 10:38:14 +00001009 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001010
1011 mutex_unlock(&dev->struct_mutex);
1012
1013pre_mutex_err:
Chris Wilson54cf91d2010-11-25 18:00:26 +00001014 kfree(cliprects);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001015 return ret;
1016}
1017
1018/*
1019 * Legacy execbuffer just creates an exec2 list from the original exec object
1020 * list array and passes it to the real function.
1021 */
1022int
1023i915_gem_execbuffer(struct drm_device *dev, void *data,
1024 struct drm_file *file)
1025{
1026 struct drm_i915_gem_execbuffer *args = data;
1027 struct drm_i915_gem_execbuffer2 exec2;
1028 struct drm_i915_gem_exec_object *exec_list = NULL;
1029 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1030 int ret, i;
1031
Chris Wilson54cf91d2010-11-25 18:00:26 +00001032 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001033 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001034 return -EINVAL;
1035 }
1036
1037 /* Copy in the exec list from userland */
1038 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1039 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1040 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001041 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001042 args->buffer_count);
1043 drm_free_large(exec_list);
1044 drm_free_large(exec2_list);
1045 return -ENOMEM;
1046 }
1047 ret = copy_from_user(exec_list,
Chris Wilsonba7a6452012-09-14 11:46:00 +01001048 (void __user *)(uintptr_t)args->buffers_ptr,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001049 sizeof(*exec_list) * args->buffer_count);
1050 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001051 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001052 args->buffer_count, ret);
1053 drm_free_large(exec_list);
1054 drm_free_large(exec2_list);
1055 return -EFAULT;
1056 }
1057
1058 for (i = 0; i < args->buffer_count; i++) {
1059 exec2_list[i].handle = exec_list[i].handle;
1060 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1061 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1062 exec2_list[i].alignment = exec_list[i].alignment;
1063 exec2_list[i].offset = exec_list[i].offset;
1064 if (INTEL_INFO(dev)->gen < 4)
1065 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1066 else
1067 exec2_list[i].flags = 0;
1068 }
1069
1070 exec2.buffers_ptr = args->buffers_ptr;
1071 exec2.buffer_count = args->buffer_count;
1072 exec2.batch_start_offset = args->batch_start_offset;
1073 exec2.batch_len = args->batch_len;
1074 exec2.DR1 = args->DR1;
1075 exec2.DR4 = args->DR4;
1076 exec2.num_cliprects = args->num_cliprects;
1077 exec2.cliprects_ptr = args->cliprects_ptr;
1078 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001079 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001080
1081 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
1082 if (!ret) {
1083 /* Copy the new buffer offsets back to the user's exec list. */
1084 for (i = 0; i < args->buffer_count; i++)
1085 exec_list[i].offset = exec2_list[i].offset;
1086 /* ... and back out to userspace */
Chris Wilsonba7a6452012-09-14 11:46:00 +01001087 ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001088 exec_list,
1089 sizeof(*exec_list) * args->buffer_count);
1090 if (ret) {
1091 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001092 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001093 "back to user (%d)\n",
1094 args->buffer_count, ret);
1095 }
1096 }
1097
1098 drm_free_large(exec_list);
1099 drm_free_large(exec2_list);
1100 return ret;
1101}
1102
1103int
1104i915_gem_execbuffer2(struct drm_device *dev, void *data,
1105 struct drm_file *file)
1106{
1107 struct drm_i915_gem_execbuffer2 *args = data;
1108 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1109 int ret;
1110
Xi Wanged8cd3b2012-04-23 04:06:41 -04001111 if (args->buffer_count < 1 ||
1112 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001113 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001114 return -EINVAL;
1115 }
1116
Chris Wilson8408c282011-02-21 12:54:48 +00001117 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001118 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001119 if (exec2_list == NULL)
1120 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1121 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001122 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001123 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001124 args->buffer_count);
1125 return -ENOMEM;
1126 }
1127 ret = copy_from_user(exec2_list,
1128 (struct drm_i915_relocation_entry __user *)
1129 (uintptr_t) args->buffers_ptr,
1130 sizeof(*exec2_list) * args->buffer_count);
1131 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001132 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001133 args->buffer_count, ret);
1134 drm_free_large(exec2_list);
1135 return -EFAULT;
1136 }
1137
1138 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
1139 if (!ret) {
1140 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilsonba7a6452012-09-14 11:46:00 +01001141 ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001142 exec2_list,
1143 sizeof(*exec2_list) * args->buffer_count);
1144 if (ret) {
1145 ret = -EFAULT;
Daniel Vetterff240192012-01-31 21:08:14 +01001146 DRM_DEBUG("failed to copy %d exec entries "
Chris Wilson54cf91d2010-11-25 18:00:26 +00001147 "back to user (%d)\n",
1148 args->buffer_count, ret);
1149 }
1150 }
1151
1152 drm_free_large(exec2_list);
1153 return ret;
1154}