blob: 86de3720527bcb5a23590902af728d491be380a9 [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 Wilsona415d352013-11-26 11:23:15 +000036#define __EXEC_OBJECT_HAS_PIN (1<<31)
37#define __EXEC_OBJECT_HAS_FENCE (1<<30)
Chris Wilsone6a84462014-08-11 12:00:12 +020038#define __EXEC_OBJECT_NEEDS_MAP (1<<29)
Chris Wilsond23db882014-05-23 08:48:08 +020039#define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
Brad Volkin0079a7d2014-12-11 12:13:11 -080040#define __EXEC_OBJECT_PURGEABLE (1<<27)
Chris Wilsond23db882014-05-23 08:48:08 +020041
42#define BATCH_OFFSET_BIAS (256*1024)
Chris Wilsona415d352013-11-26 11:23:15 +000043
Ben Widawsky27173f12013-08-14 11:38:36 +020044struct eb_vmas {
45 struct list_head vmas;
Chris Wilson67731b82010-12-08 10:38:14 +000046 int and;
Chris Wilsoneef90cc2013-01-08 10:53:17 +000047 union {
Ben Widawsky27173f12013-08-14 11:38:36 +020048 struct i915_vma *lut[0];
Chris Wilsoneef90cc2013-01-08 10:53:17 +000049 struct hlist_head buckets[0];
50 };
Chris Wilson67731b82010-12-08 10:38:14 +000051};
52
Ben Widawsky27173f12013-08-14 11:38:36 +020053static struct eb_vmas *
Ben Widawsky17601cbc2013-11-25 09:54:38 -080054eb_create(struct drm_i915_gem_execbuffer2 *args)
Chris Wilson67731b82010-12-08 10:38:14 +000055{
Ben Widawsky27173f12013-08-14 11:38:36 +020056 struct eb_vmas *eb = NULL;
Chris Wilson67731b82010-12-08 10:38:14 +000057
Chris Wilsoneef90cc2013-01-08 10:53:17 +000058 if (args->flags & I915_EXEC_HANDLE_LUT) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020059 unsigned size = args->buffer_count;
Ben Widawsky27173f12013-08-14 11:38:36 +020060 size *= sizeof(struct i915_vma *);
61 size += sizeof(struct eb_vmas);
Chris Wilsoneef90cc2013-01-08 10:53:17 +000062 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
63 }
64
65 if (eb == NULL) {
Daniel Vetterb205ca52013-09-19 14:00:11 +020066 unsigned size = args->buffer_count;
67 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
Lauri Kasanen27b7c632013-03-27 15:04:55 +020068 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
Chris Wilsoneef90cc2013-01-08 10:53:17 +000069 while (count > 2*size)
70 count >>= 1;
71 eb = kzalloc(count*sizeof(struct hlist_head) +
Ben Widawsky27173f12013-08-14 11:38:36 +020072 sizeof(struct eb_vmas),
Chris Wilsoneef90cc2013-01-08 10:53:17 +000073 GFP_TEMPORARY);
74 if (eb == NULL)
75 return eb;
76
77 eb->and = count - 1;
78 } else
79 eb->and = -args->buffer_count;
80
Ben Widawsky27173f12013-08-14 11:38:36 +020081 INIT_LIST_HEAD(&eb->vmas);
Chris Wilson67731b82010-12-08 10:38:14 +000082 return eb;
83}
84
85static void
Ben Widawsky27173f12013-08-14 11:38:36 +020086eb_reset(struct eb_vmas *eb)
Chris Wilson67731b82010-12-08 10:38:14 +000087{
Chris Wilsoneef90cc2013-01-08 10:53:17 +000088 if (eb->and >= 0)
89 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
Chris Wilson67731b82010-12-08 10:38:14 +000090}
91
Chris Wilson3b96eff2013-01-08 10:53:14 +000092static int
Ben Widawsky27173f12013-08-14 11:38:36 +020093eb_lookup_vmas(struct eb_vmas *eb,
94 struct drm_i915_gem_exec_object2 *exec,
95 const struct drm_i915_gem_execbuffer2 *args,
96 struct i915_address_space *vm,
97 struct drm_file *file)
Chris Wilson3b96eff2013-01-08 10:53:14 +000098{
Ben Widawsky27173f12013-08-14 11:38:36 +020099 struct drm_i915_gem_object *obj;
100 struct list_head objects;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000101 int i, ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000102
Ben Widawsky27173f12013-08-14 11:38:36 +0200103 INIT_LIST_HEAD(&objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000104 spin_lock(&file->table_lock);
Ben Widawsky27173f12013-08-14 11:38:36 +0200105 /* Grab a reference to the object and release the lock so we can lookup
106 * or create the VMA without using GFP_ATOMIC */
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000107 for (i = 0; i < args->buffer_count; i++) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000108 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
109 if (obj == NULL) {
110 spin_unlock(&file->table_lock);
111 DRM_DEBUG("Invalid object handle %d at index %d\n",
112 exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200113 ret = -ENOENT;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000114 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000115 }
116
Ben Widawsky27173f12013-08-14 11:38:36 +0200117 if (!list_empty(&obj->obj_exec_link)) {
Chris Wilson3b96eff2013-01-08 10:53:14 +0000118 spin_unlock(&file->table_lock);
119 DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
120 obj, exec[i].handle, i);
Ben Widawsky27173f12013-08-14 11:38:36 +0200121 ret = -EINVAL;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000122 goto err;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000123 }
124
Thomas Hellstrom355a7012014-11-20 09:56:25 +0100125 WARN_ONCE(obj->base.dumb,
126 "GPU use of dumb buffer is illegal.\n");
127
Chris Wilson3b96eff2013-01-08 10:53:14 +0000128 drm_gem_object_reference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200129 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000130 }
131 spin_unlock(&file->table_lock);
132
Ben Widawsky27173f12013-08-14 11:38:36 +0200133 i = 0;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000134 while (!list_empty(&objects)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200135 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800136
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000137 obj = list_first_entry(&objects,
138 struct drm_i915_gem_object,
139 obj_exec_link);
140
Daniel Vettere656a6c2013-08-14 14:14:04 +0200141 /*
142 * NOTE: We can leak any vmas created here when something fails
143 * later on. But that's no issue since vma_unbind can deal with
144 * vmas which are not actually bound. And since only
145 * lookup_or_create exists as an interface to get at the vma
146 * from the (obj, vm) we don't run the risk of creating
147 * duplicated vmas for the same vm.
148 */
Daniel Vetterda51a1e2014-08-11 12:08:58 +0200149 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200150 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200151 DRM_DEBUG("Failed to lookup VMA\n");
152 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000153 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200154 }
155
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000156 /* Transfer ownership from the objects list to the vmas list. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200157 list_add_tail(&vma->exec_list, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000158 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200159
160 vma->exec_entry = &exec[i];
161 if (eb->and < 0) {
162 eb->lut[i] = vma;
163 } else {
164 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
165 vma->exec_handle = handle;
166 hlist_add_head(&vma->exec_node,
167 &eb->buckets[handle & eb->and]);
168 }
169 ++i;
170 }
171
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000172 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200173
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000174
175err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200176 while (!list_empty(&objects)) {
177 obj = list_first_entry(&objects,
178 struct drm_i915_gem_object,
179 obj_exec_link);
180 list_del_init(&obj->obj_exec_link);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000181 drm_gem_object_unreference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200182 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000183 /*
184 * Objects already transfered to the vmas list will be unreferenced by
185 * eb_destroy.
186 */
187
Ben Widawsky27173f12013-08-14 11:38:36 +0200188 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000189}
190
Ben Widawsky27173f12013-08-14 11:38:36 +0200191static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000192{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000193 if (eb->and < 0) {
194 if (handle >= -eb->and)
195 return NULL;
196 return eb->lut[handle];
197 } else {
198 struct hlist_head *head;
199 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000200
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000201 head = &eb->buckets[handle & eb->and];
202 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200203 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000204
Ben Widawsky27173f12013-08-14 11:38:36 +0200205 vma = hlist_entry(node, struct i915_vma, exec_node);
206 if (vma->exec_handle == handle)
207 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000208 }
209 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000210 }
Chris Wilson67731b82010-12-08 10:38:14 +0000211}
212
Chris Wilsona415d352013-11-26 11:23:15 +0000213static void
214i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
215{
216 struct drm_i915_gem_exec_object2 *entry;
217 struct drm_i915_gem_object *obj = vma->obj;
218
219 if (!drm_mm_node_allocated(&vma->node))
220 return;
221
222 entry = vma->exec_entry;
223
224 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
225 i915_gem_object_unpin_fence(obj);
226
227 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Daniel Vetter3d7f0f92013-12-18 16:23:37 +0100228 vma->pin_count--;
Chris Wilsona415d352013-11-26 11:23:15 +0000229
Brad Volkin0079a7d2014-12-11 12:13:11 -0800230 if (entry->flags & __EXEC_OBJECT_PURGEABLE)
231 obj->madv = I915_MADV_DONTNEED;
232
233 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE |
234 __EXEC_OBJECT_HAS_PIN |
235 __EXEC_OBJECT_PURGEABLE);
Chris Wilsona415d352013-11-26 11:23:15 +0000236}
237
238static void eb_destroy(struct eb_vmas *eb)
239{
Ben Widawsky27173f12013-08-14 11:38:36 +0200240 while (!list_empty(&eb->vmas)) {
241 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000242
Ben Widawsky27173f12013-08-14 11:38:36 +0200243 vma = list_first_entry(&eb->vmas,
244 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000245 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200246 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000247 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200248 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000249 }
Chris Wilson67731b82010-12-08 10:38:14 +0000250 kfree(eb);
251}
252
Chris Wilsondabdfe02012-03-26 10:10:27 +0200253static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
254{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300255 return (HAS_LLC(obj->base.dev) ||
256 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100257 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200258 obj->cache_level != I915_CACHE_NONE);
259}
260
Chris Wilson54cf91d2010-11-25 18:00:26 +0000261static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100262relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700263 struct drm_i915_gem_relocation_entry *reloc,
264 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100265{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700266 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100267 uint32_t page_offset = offset_in_page(reloc->offset);
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700268 uint64_t delta = reloc->delta + target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100269 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800270 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100271
Chris Wilson2cc86b82013-08-26 19:51:00 -0300272 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100273 if (ret)
274 return ret;
275
276 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
277 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700278 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700279
280 if (INTEL_INFO(dev)->gen >= 8) {
281 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
282
283 if (page_offset == 0) {
284 kunmap_atomic(vaddr);
285 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
286 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
287 }
288
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700289 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700290 }
291
Rafael Barbalho5032d872013-08-21 17:10:51 +0100292 kunmap_atomic(vaddr);
293
294 return 0;
295}
296
297static int
298relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700299 struct drm_i915_gem_relocation_entry *reloc,
300 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100301{
302 struct drm_device *dev = obj->base.dev;
303 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700304 uint64_t delta = reloc->delta + target_offset;
Chris Wilson906843c2014-08-10 06:29:11 +0100305 uint64_t offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100306 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800307 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100308
309 ret = i915_gem_object_set_to_gtt_domain(obj, true);
310 if (ret)
311 return ret;
312
313 ret = i915_gem_object_put_fence(obj);
314 if (ret)
315 return ret;
316
317 /* Map the page containing the relocation we're going to perform. */
Chris Wilson906843c2014-08-10 06:29:11 +0100318 offset = i915_gem_obj_ggtt_offset(obj);
319 offset += reloc->offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100320 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100321 offset & PAGE_MASK);
322 iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700323
324 if (INTEL_INFO(dev)->gen >= 8) {
Chris Wilson906843c2014-08-10 06:29:11 +0100325 offset += sizeof(uint32_t);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700326
Chris Wilson906843c2014-08-10 06:29:11 +0100327 if (offset_in_page(offset) == 0) {
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700328 io_mapping_unmap_atomic(reloc_page);
Chris Wilson906843c2014-08-10 06:29:11 +0100329 reloc_page =
330 io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
331 offset);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700332 }
333
Chris Wilson906843c2014-08-10 06:29:11 +0100334 iowrite32(upper_32_bits(delta),
335 reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700336 }
337
Rafael Barbalho5032d872013-08-21 17:10:51 +0100338 io_mapping_unmap_atomic(reloc_page);
339
340 return 0;
341}
342
343static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000344i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200345 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800346 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000347{
348 struct drm_device *dev = obj->base.dev;
349 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100350 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200351 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700352 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800353 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000354
Chris Wilson67731b82010-12-08 10:38:14 +0000355 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200356 target_vma = eb_get_vma(eb, reloc->target_handle);
357 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000358 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200359 target_i915_obj = target_vma->obj;
360 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000361
Ben Widawsky5ce09722013-11-25 09:54:40 -0800362 target_offset = target_vma->node.start;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000363
Eric Anholte844b992012-07-31 15:35:01 -0700364 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
365 * pipe_control writes because the gpu doesn't properly redirect them
366 * through the ppgtt for non_secure batchbuffers. */
367 if (unlikely(IS_GEN6(dev) &&
368 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000369 !(target_vma->bound & GLOBAL_BIND))) {
370 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
371 GLOBAL_BIND);
372 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
373 return ret;
374 }
Eric Anholte844b992012-07-31 15:35:01 -0700375
Chris Wilson54cf91d2010-11-25 18:00:26 +0000376 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000377 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100378 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000379 "obj %p target %d offset %d "
380 "read %08x write %08x",
381 obj, reloc->target_handle,
382 (int) reloc->offset,
383 reloc->read_domains,
384 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800385 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000386 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100387 if (unlikely((reloc->write_domain | reloc->read_domains)
388 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100389 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000390 "obj %p target %d offset %d "
391 "read %08x write %08x",
392 obj, reloc->target_handle,
393 (int) reloc->offset,
394 reloc->read_domains,
395 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800396 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000397 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000398
399 target_obj->pending_read_domains |= reloc->read_domains;
400 target_obj->pending_write_domain |= reloc->write_domain;
401
402 /* If the relocation already has the right value in it, no
403 * more work needs to be done.
404 */
405 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000406 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000407
408 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700409 if (unlikely(reloc->offset >
410 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100411 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000412 "obj %p target %d offset %d size %d.\n",
413 obj, reloc->target_handle,
414 (int) reloc->offset,
415 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800416 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000417 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000418 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100419 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000420 "obj %p target %d offset %d.\n",
421 obj, reloc->target_handle,
422 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800423 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000424 }
425
Chris Wilsondabdfe02012-03-26 10:10:27 +0200426 /* We can't wait for rendering with pagefaults disabled */
427 if (obj->active && in_atomic())
428 return -EFAULT;
429
Rafael Barbalho5032d872013-08-21 17:10:51 +0100430 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700431 ret = relocate_entry_cpu(obj, reloc, target_offset);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100432 else
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700433 ret = relocate_entry_gtt(obj, reloc, target_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000434
Daniel Vetterd4d36012013-09-02 20:56:23 +0200435 if (ret)
436 return ret;
437
Chris Wilson54cf91d2010-11-25 18:00:26 +0000438 /* and update the user's relocation entry */
439 reloc->presumed_offset = target_offset;
440
Chris Wilson67731b82010-12-08 10:38:14 +0000441 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000442}
443
444static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200445i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
446 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000447{
Chris Wilson1d83f442012-03-24 20:12:53 +0000448#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
449 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000450 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200451 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000452 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000453
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200454 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000455
Chris Wilson1d83f442012-03-24 20:12:53 +0000456 remain = entry->relocation_count;
457 while (remain) {
458 struct drm_i915_gem_relocation_entry *r = stack_reloc;
459 int count = remain;
460 if (count > ARRAY_SIZE(stack_reloc))
461 count = ARRAY_SIZE(stack_reloc);
462 remain -= count;
463
464 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000465 return -EFAULT;
466
Chris Wilson1d83f442012-03-24 20:12:53 +0000467 do {
468 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000469
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800470 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000471 if (ret)
472 return ret;
473
474 if (r->presumed_offset != offset &&
475 __copy_to_user_inatomic(&user_relocs->presumed_offset,
476 &r->presumed_offset,
477 sizeof(r->presumed_offset))) {
478 return -EFAULT;
479 }
480
481 user_relocs++;
482 r++;
483 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000484 }
485
486 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000487#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000488}
489
490static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200491i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
492 struct eb_vmas *eb,
493 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000494{
Ben Widawsky27173f12013-08-14 11:38:36 +0200495 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000496 int i, ret;
497
498 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800499 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000500 if (ret)
501 return ret;
502 }
503
504 return 0;
505}
506
507static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800508i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000509{
Ben Widawsky27173f12013-08-14 11:38:36 +0200510 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000511 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000512
Chris Wilsond4aeee72011-03-14 15:11:24 +0000513 /* This is the fast path and we cannot handle a pagefault whilst
514 * holding the struct mutex lest the user pass in the relocations
515 * contained within a mmaped bo. For in such a case we, the page
516 * fault handler would call i915_gem_fault() and we would try to
517 * acquire the struct mutex again. Obviously this is bad and so
518 * lockdep complains vehemently.
519 */
520 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200521 list_for_each_entry(vma, &eb->vmas, exec_list) {
522 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000523 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000524 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000525 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000526 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000527
Chris Wilsond4aeee72011-03-14 15:11:24 +0000528 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000529}
530
Chris Wilson1690e1e2011-12-14 13:57:08 +0100531static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200532i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100533 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200534 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100535{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800536 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200537 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200538 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100539 int ret;
540
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100541 flags = 0;
Chris Wilsone6a84462014-08-11 12:00:12 +0200542 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
Chris Wilsonc826c442014-10-31 13:53:53 +0000543 flags |= PIN_GLOBAL | PIN_MAPPABLE;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100544 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
Daniel Vetterbf3d1492014-02-14 14:01:12 +0100545 flags |= PIN_GLOBAL;
Chris Wilsond23db882014-05-23 08:48:08 +0200546 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
547 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100548
549 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100550 if (ret)
551 return ret;
552
Chris Wilson7788a762012-08-24 19:18:18 +0100553 entry->flags |= __EXEC_OBJECT_HAS_PIN;
554
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100555 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
556 ret = i915_gem_object_get_fence(obj);
557 if (ret)
558 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100559
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100560 if (i915_gem_object_pin_fence(obj))
561 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100562 }
563
Ben Widawsky27173f12013-08-14 11:38:36 +0200564 if (entry->offset != vma->node.start) {
565 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100566 *need_reloc = true;
567 }
568
569 if (entry->flags & EXEC_OBJECT_WRITE) {
570 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
571 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
572 }
573
Chris Wilson1690e1e2011-12-14 13:57:08 +0100574 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100575}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100576
Chris Wilsond23db882014-05-23 08:48:08 +0200577static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200578need_reloc_mappable(struct i915_vma *vma)
579{
580 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
581
582 if (entry->relocation_count == 0)
583 return false;
584
585 if (!i915_is_ggtt(vma->vm))
586 return false;
587
588 /* See also use_cpu_reloc() */
589 if (HAS_LLC(vma->obj->base.dev))
590 return false;
591
592 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
593 return false;
594
595 return true;
596}
597
598static bool
599eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200600{
601 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
602 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond23db882014-05-23 08:48:08 +0200603
Chris Wilsone6a84462014-08-11 12:00:12 +0200604 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
Chris Wilsond23db882014-05-23 08:48:08 +0200605 !i915_is_ggtt(vma->vm));
606
607 if (entry->alignment &&
608 vma->node.start & (entry->alignment - 1))
609 return true;
610
Chris Wilsone6a84462014-08-11 12:00:12 +0200611 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
Chris Wilsond23db882014-05-23 08:48:08 +0200612 return true;
613
614 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
615 vma->node.start < BATCH_OFFSET_BIAS)
616 return true;
617
618 return false;
619}
620
Chris Wilson54cf91d2010-11-25 18:00:26 +0000621static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100622i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200623 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100624 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000625{
Chris Wilson432e58e2010-11-25 19:32:06 +0000626 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200627 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700628 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200629 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100630 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
631 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000632
Chris Wilson227f7822014-05-15 10:41:42 +0100633 i915_gem_retire_requests_ring(ring);
634
Ben Widawsky68c8c172013-09-11 14:57:50 -0700635 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
636
Ben Widawsky27173f12013-08-14 11:38:36 +0200637 INIT_LIST_HEAD(&ordered_vmas);
638 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000639 struct drm_i915_gem_exec_object2 *entry;
640 bool need_fence, need_mappable;
641
Ben Widawsky27173f12013-08-14 11:38:36 +0200642 vma = list_first_entry(vmas, struct i915_vma, exec_list);
643 obj = vma->obj;
644 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000645
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100646 if (!has_fenced_gpu_access)
647 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000648 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000649 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
650 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200651 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000652
Chris Wilsone6a84462014-08-11 12:00:12 +0200653 if (need_mappable) {
654 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200655 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200656 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200657 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000658
Daniel Vettered5982e2013-01-17 22:23:36 +0100659 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000660 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000661 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200662 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000663
664 /* Attempt to pin all of the buffers into the GTT.
665 * This is done in 3 phases:
666 *
667 * 1a. Unbind all objects that do not match the GTT constraints for
668 * the execbuffer (fenceable, mappable, alignment etc).
669 * 1b. Increment pin count for already bound objects.
670 * 2. Bind new objects.
671 * 3. Decrement pin count.
672 *
Chris Wilson7788a762012-08-24 19:18:18 +0100673 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000674 * room for the earlier objects *unless* we need to defragment.
675 */
676 retry = 0;
677 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100678 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000679
680 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200681 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200682 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000683 continue;
684
Chris Wilsone6a84462014-08-11 12:00:12 +0200685 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200686 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000687 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200688 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000689 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000690 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000691 }
692
693 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200694 list_for_each_entry(vma, vmas, exec_list) {
695 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100696 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000697
Ben Widawsky27173f12013-08-14 11:38:36 +0200698 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100699 if (ret)
700 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000701 }
702
Chris Wilsona415d352013-11-26 11:23:15 +0000703err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200704 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000705 return ret;
706
Chris Wilsona415d352013-11-26 11:23:15 +0000707 /* Decrement pin count for bound objects */
708 list_for_each_entry(vma, vmas, exec_list)
709 i915_gem_execbuffer_unreserve_vma(vma);
710
Ben Widawsky68c8c172013-09-11 14:57:50 -0700711 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000712 if (ret)
713 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000714 } while (1);
715}
716
717static int
718i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100719 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000720 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100721 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200722 struct eb_vmas *eb,
723 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000724{
725 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200726 struct i915_address_space *vm;
727 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100728 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000729 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000730 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200731 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000732
Ben Widawsky27173f12013-08-14 11:38:36 +0200733 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
734
Chris Wilson67731b82010-12-08 10:38:14 +0000735 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200736 while (!list_empty(&eb->vmas)) {
737 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
738 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000739 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200740 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000741 }
742
Chris Wilson54cf91d2010-11-25 18:00:26 +0000743 mutex_unlock(&dev->struct_mutex);
744
745 total = 0;
746 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000747 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000748
Chris Wilsondd6864a2011-01-12 23:49:13 +0000749 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000750 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000751 if (reloc == NULL || reloc_offset == NULL) {
752 drm_free_large(reloc);
753 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000754 mutex_lock(&dev->struct_mutex);
755 return -ENOMEM;
756 }
757
758 total = 0;
759 for (i = 0; i < count; i++) {
760 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000761 u64 invalid_offset = (u64)-1;
762 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000763
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200764 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000765
766 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000767 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000768 ret = -EFAULT;
769 mutex_lock(&dev->struct_mutex);
770 goto err;
771 }
772
Chris Wilson262b6d32013-01-15 16:17:54 +0000773 /* As we do not update the known relocation offsets after
774 * relocating (due to the complexities in lock handling),
775 * we need to mark them as invalid now so that we force the
776 * relocation processing next time. Just in case the target
777 * object is evicted and then rebound into its old
778 * presumed_offset before the next execbuffer - if that
779 * happened we would make the mistake of assuming that the
780 * relocations were valid.
781 */
782 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100783 if (__copy_to_user(&user_relocs[j].presumed_offset,
784 &invalid_offset,
785 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000786 ret = -EFAULT;
787 mutex_lock(&dev->struct_mutex);
788 goto err;
789 }
790 }
791
Chris Wilsondd6864a2011-01-12 23:49:13 +0000792 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000793 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000794 }
795
796 ret = i915_mutex_lock_interruptible(dev);
797 if (ret) {
798 mutex_lock(&dev->struct_mutex);
799 goto err;
800 }
801
Chris Wilson67731b82010-12-08 10:38:14 +0000802 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000803 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200804 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000805 if (ret)
806 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000807
Daniel Vettered5982e2013-01-17 22:23:36 +0100808 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200809 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000810 if (ret)
811 goto err;
812
Ben Widawsky27173f12013-08-14 11:38:36 +0200813 list_for_each_entry(vma, &eb->vmas, exec_list) {
814 int offset = vma->exec_entry - exec;
815 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
816 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000817 if (ret)
818 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000819 }
820
821 /* Leave the user relocations as are, this is the painfully slow path,
822 * and we want to avoid the complication of dropping the lock whilst
823 * having buffers reserved in the aperture and so causing spurious
824 * ENOSPC for random operations.
825 */
826
827err:
828 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000829 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000830 return ret;
831}
832
Chris Wilson54cf91d2010-11-25 18:00:26 +0000833static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100834i915_gem_execbuffer_move_to_gpu(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200835 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000836{
Ben Widawsky27173f12013-08-14 11:38:36 +0200837 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200838 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100839 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000840 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000841
Ben Widawsky27173f12013-08-14 11:38:36 +0200842 list_for_each_entry(vma, vmas, exec_list) {
843 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700844 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000845 if (ret)
846 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200847
848 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100849 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200850
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200851 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000852 }
853
Chris Wilson000433b2013-08-08 14:41:09 +0100854 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800855 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200856
857 if (flush_domains & I915_GEM_DOMAIN_GTT)
858 wmb();
859
Chris Wilson09cf7c92012-07-13 14:14:08 +0100860 /* Unconditionally invalidate gpu caches and ensure that we do flush
861 * any residual writes from the previous batch.
862 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100863 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000864}
865
Chris Wilson432e58e2010-11-25 19:32:06 +0000866static bool
867i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000868{
Daniel Vettered5982e2013-01-17 22:23:36 +0100869 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
870 return false;
871
Chris Wilson432e58e2010-11-25 19:32:06 +0000872 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000873}
874
875static int
Chris Wilsonad19f102014-08-10 06:29:08 +0100876validate_exec_list(struct drm_device *dev,
877 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000878 int count)
879{
Daniel Vetterb205ca52013-09-19 14:00:11 +0200880 unsigned relocs_total = 0;
881 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +0100882 unsigned invalid_flags;
883 int i;
884
885 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
886 if (USES_FULL_PPGTT(dev))
887 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000888
889 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200890 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000891 int length; /* limited by fault_in_pages_readable() */
892
Chris Wilsonad19f102014-08-10 06:29:08 +0100893 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +0100894 return -EINVAL;
895
Kees Cook3118a4f2013-03-11 17:31:45 -0700896 /* First check for malicious input causing overflow in
897 * the worst case where we need to allocate the entire
898 * relocation tree as a single array.
899 */
900 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000901 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700902 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000903
904 length = exec[i].relocation_count *
905 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700906 /*
907 * We must check that the entire relocation array is safe
908 * to read, but since we may need to update the presumed
909 * offsets during execution, check for full write access.
910 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000911 if (!access_ok(VERIFY_WRITE, ptr, length))
912 return -EFAULT;
913
Jani Nikulad330a952014-01-21 11:24:25 +0200914 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +0800915 if (fault_in_multipages_readable(ptr, length))
916 return -EFAULT;
917 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000918 }
919
920 return 0;
921}
922
Oscar Mateo273497e2014-05-22 14:13:37 +0100923static struct intel_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200924i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100925 struct intel_engine_cs *ring, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200926{
Oscar Mateo273497e2014-05-22 14:13:37 +0100927 struct intel_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200928 struct i915_ctx_hang_stats *hs;
929
Oscar Mateo821d66d2014-07-03 16:28:00 +0100930 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +0100931 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200932
Ben Widawsky41bde552013-12-06 14:11:21 -0800933 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -1000934 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -0800935 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200936
Ben Widawsky41bde552013-12-06 14:11:21 -0800937 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200938 if (hs->banned) {
939 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -0800940 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200941 }
942
Oscar Mateoec3e9962014-07-24 17:04:18 +0100943 if (i915.enable_execlists && !ctx->engine[ring->id].state) {
944 int ret = intel_lr_context_deferred_create(ctx, ring);
945 if (ret) {
946 DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
947 return ERR_PTR(ret);
948 }
949 }
950
Ben Widawsky41bde552013-12-06 14:11:21 -0800951 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200952}
953
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100954void
Ben Widawsky27173f12013-08-14 11:38:36 +0200955i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100956 struct intel_engine_cs *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000957{
John Harrison97b2a6a2014-11-24 18:49:26 +0000958 struct drm_i915_gem_request *req = intel_ring_get_request(ring);
Ben Widawsky27173f12013-08-14 11:38:36 +0200959 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000960
Ben Widawsky27173f12013-08-14 11:38:36 +0200961 list_for_each_entry(vma, vmas, exec_list) {
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100962 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Ben Widawsky27173f12013-08-14 11:38:36 +0200963 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100964 u32 old_read = obj->base.read_domains;
965 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000966
Chris Wilson432e58e2010-11-25 19:32:06 +0000967 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100968 if (obj->base.write_domain == 0)
969 obj->base.pending_read_domains |= obj->base.read_domains;
970 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000971
Ben Widawskye2d05a82013-09-24 09:57:58 -0700972 i915_vma_move_to_active(vma, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000973 if (obj->base.write_domain) {
974 obj->dirty = 1;
John Harrison97b2a6a2014-11-24 18:49:26 +0000975 i915_gem_request_assign(&obj->last_write_req, req);
Daniel Vetterf99d7062014-06-19 16:01:59 +0200976
977 intel_fb_obj_invalidate(obj, ring);
Chris Wilsonc8725f32014-03-17 12:21:55 +0000978
979 /* update for the implicit flush after a batch */
980 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson432e58e2010-11-25 19:32:06 +0000981 }
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100982 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
John Harrison97b2a6a2014-11-24 18:49:26 +0000983 i915_gem_request_assign(&obj->last_fenced_req, req);
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100984 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
985 struct drm_i915_private *dev_priv = to_i915(ring->dev);
986 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
987 &dev_priv->mm.fence_list);
988 }
989 }
Chris Wilson432e58e2010-11-25 19:32:06 +0000990
Chris Wilsondb53a302011-02-03 11:57:46 +0000991 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000992 }
993}
994
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100995void
Chris Wilson54cf91d2010-11-25 18:00:26 +0000996i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000997 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100998 struct intel_engine_cs *ring,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300999 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001000{
Daniel Vettercc889e02012-06-13 20:45:19 +02001001 /* Unconditionally force add_request to emit a full flush. */
1002 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001003
Chris Wilson432e58e2010-11-25 19:32:06 +00001004 /* Add a breadcrumb for the completion of the batch buffer */
John Harrison9400ae52014-11-24 18:49:36 +00001005 (void)__i915_add_request(ring, file, obj);
Chris Wilson432e58e2010-11-25 19:32:06 +00001006}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001007
1008static int
Eric Anholtae662d32012-01-03 09:23:29 -08001009i915_reset_gen7_sol_offsets(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001010 struct intel_engine_cs *ring)
Eric Anholtae662d32012-01-03 09:23:29 -08001011{
Jani Nikula50227e12014-03-31 14:27:21 +03001012 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtae662d32012-01-03 09:23:29 -08001013 int ret, i;
1014
Daniel Vetter9d662da2014-04-24 08:09:09 +02001015 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
1016 DRM_DEBUG("sol reset is gen7/rcs only\n");
1017 return -EINVAL;
1018 }
Eric Anholtae662d32012-01-03 09:23:29 -08001019
1020 ret = intel_ring_begin(ring, 4 * 3);
1021 if (ret)
1022 return ret;
1023
1024 for (i = 0; i < 4; i++) {
1025 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1026 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
1027 intel_ring_emit(ring, 0);
1028 }
1029
1030 intel_ring_advance(ring);
1031
1032 return 0;
1033}
1034
Chris Wilson5c6c6002014-09-06 10:28:27 +01001035static int
1036i915_emit_box(struct intel_engine_cs *ring,
1037 struct drm_clip_rect *box,
1038 int DR1, int DR4)
1039{
1040 int ret;
1041
1042 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
1043 box->y2 <= 0 || box->x2 <= 0) {
1044 DRM_ERROR("Bad box %d,%d..%d,%d\n",
1045 box->x1, box->y1, box->x2, box->y2);
1046 return -EINVAL;
1047 }
1048
1049 if (INTEL_INFO(ring->dev)->gen >= 4) {
1050 ret = intel_ring_begin(ring, 4);
1051 if (ret)
1052 return ret;
1053
1054 intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO_I965);
1055 intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
1056 intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
1057 intel_ring_emit(ring, DR4);
1058 } else {
1059 ret = intel_ring_begin(ring, 6);
1060 if (ret)
1061 return ret;
1062
1063 intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO);
1064 intel_ring_emit(ring, DR1);
1065 intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
1066 intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
1067 intel_ring_emit(ring, DR4);
1068 intel_ring_emit(ring, 0);
1069 }
1070 intel_ring_advance(ring);
1071
1072 return 0;
1073}
1074
1075
Oscar Mateoa83014d2014-07-24 17:04:21 +01001076int
1077i915_gem_ringbuffer_submission(struct drm_device *dev, struct drm_file *file,
1078 struct intel_engine_cs *ring,
1079 struct intel_context *ctx,
1080 struct drm_i915_gem_execbuffer2 *args,
1081 struct list_head *vmas,
1082 struct drm_i915_gem_object *batch_obj,
1083 u64 exec_start, u32 flags)
Oscar Mateo78382592014-07-03 16:28:05 +01001084{
1085 struct drm_clip_rect *cliprects = NULL;
1086 struct drm_i915_private *dev_priv = dev->dev_private;
1087 u64 exec_len;
1088 int instp_mode;
1089 u32 instp_mask;
1090 int i, ret = 0;
1091
1092 if (args->num_cliprects != 0) {
1093 if (ring != &dev_priv->ring[RCS]) {
1094 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
1095 return -EINVAL;
1096 }
1097
1098 if (INTEL_INFO(dev)->gen >= 5) {
1099 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1100 return -EINVAL;
1101 }
1102
1103 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1104 DRM_DEBUG("execbuf with %u cliprects\n",
1105 args->num_cliprects);
1106 return -EINVAL;
1107 }
1108
1109 cliprects = kcalloc(args->num_cliprects,
1110 sizeof(*cliprects),
1111 GFP_KERNEL);
1112 if (cliprects == NULL) {
1113 ret = -ENOMEM;
1114 goto error;
1115 }
1116
1117 if (copy_from_user(cliprects,
1118 to_user_ptr(args->cliprects_ptr),
1119 sizeof(*cliprects)*args->num_cliprects)) {
1120 ret = -EFAULT;
1121 goto error;
1122 }
1123 } else {
1124 if (args->DR4 == 0xffffffff) {
1125 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1126 args->DR4 = 0;
1127 }
1128
1129 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
1130 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
1131 return -EINVAL;
1132 }
1133 }
1134
1135 ret = i915_gem_execbuffer_move_to_gpu(ring, vmas);
1136 if (ret)
1137 goto error;
1138
1139 ret = i915_switch_context(ring, ctx);
1140 if (ret)
1141 goto error;
1142
1143 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1144 instp_mask = I915_EXEC_CONSTANTS_MASK;
1145 switch (instp_mode) {
1146 case I915_EXEC_CONSTANTS_REL_GENERAL:
1147 case I915_EXEC_CONSTANTS_ABSOLUTE:
1148 case I915_EXEC_CONSTANTS_REL_SURFACE:
1149 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
1150 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
1151 ret = -EINVAL;
1152 goto error;
1153 }
1154
1155 if (instp_mode != dev_priv->relative_constants_mode) {
1156 if (INTEL_INFO(dev)->gen < 4) {
1157 DRM_DEBUG("no rel constants on pre-gen4\n");
1158 ret = -EINVAL;
1159 goto error;
1160 }
1161
1162 if (INTEL_INFO(dev)->gen > 5 &&
1163 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1164 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
1165 ret = -EINVAL;
1166 goto error;
1167 }
1168
1169 /* The HW changed the meaning on this bit on gen6 */
1170 if (INTEL_INFO(dev)->gen >= 6)
1171 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1172 }
1173 break;
1174 default:
1175 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
1176 ret = -EINVAL;
1177 goto error;
1178 }
1179
1180 if (ring == &dev_priv->ring[RCS] &&
1181 instp_mode != dev_priv->relative_constants_mode) {
1182 ret = intel_ring_begin(ring, 4);
1183 if (ret)
1184 goto error;
1185
1186 intel_ring_emit(ring, MI_NOOP);
1187 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1188 intel_ring_emit(ring, INSTPM);
1189 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1190 intel_ring_advance(ring);
1191
1192 dev_priv->relative_constants_mode = instp_mode;
1193 }
1194
1195 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1196 ret = i915_reset_gen7_sol_offsets(dev, ring);
1197 if (ret)
1198 goto error;
1199 }
1200
1201 exec_len = args->batch_len;
1202 if (cliprects) {
1203 for (i = 0; i < args->num_cliprects; i++) {
Chris Wilson5c6c6002014-09-06 10:28:27 +01001204 ret = i915_emit_box(ring, &cliprects[i],
Oscar Mateo78382592014-07-03 16:28:05 +01001205 args->DR1, args->DR4);
1206 if (ret)
1207 goto error;
1208
1209 ret = ring->dispatch_execbuffer(ring,
1210 exec_start, exec_len,
1211 flags);
1212 if (ret)
1213 goto error;
1214 }
1215 } else {
1216 ret = ring->dispatch_execbuffer(ring,
1217 exec_start, exec_len,
1218 flags);
1219 if (ret)
1220 return ret;
1221 }
1222
John Harrison74328ee2014-11-24 18:49:38 +00001223 trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001224
1225 i915_gem_execbuffer_move_to_active(vmas, ring);
1226 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
1227
1228error:
1229 kfree(cliprects);
1230 return ret;
1231}
1232
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001233/**
1234 * Find one BSD ring to dispatch the corresponding BSD command.
1235 * The Ring ID is returned.
1236 */
1237static int gen8_dispatch_bsd_ring(struct drm_device *dev,
1238 struct drm_file *file)
1239{
1240 struct drm_i915_private *dev_priv = dev->dev_private;
1241 struct drm_i915_file_private *file_priv = file->driver_priv;
1242
1243 /* Check whether the file_priv is using one ring */
1244 if (file_priv->bsd_ring)
1245 return file_priv->bsd_ring->id;
1246 else {
1247 /* If no, use the ping-pong mechanism to select one ring */
1248 int ring_id;
1249
1250 mutex_lock(&dev->struct_mutex);
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001251 if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001252 ring_id = VCS;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001253 dev_priv->mm.bsd_ring_dispatch_index = 1;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001254 } else {
1255 ring_id = VCS2;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001256 dev_priv->mm.bsd_ring_dispatch_index = 0;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001257 }
1258 file_priv->bsd_ring = &dev_priv->ring[ring_id];
1259 mutex_unlock(&dev->struct_mutex);
1260 return ring_id;
1261 }
1262}
1263
Chris Wilsond23db882014-05-23 08:48:08 +02001264static struct drm_i915_gem_object *
1265eb_get_batch(struct eb_vmas *eb)
1266{
1267 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
1268
1269 /*
1270 * SNA is doing fancy tricks with compressing batch buffers, which leads
1271 * to negative relocation deltas. Usually that works out ok since the
1272 * relocate address is still positive, except when the batch is placed
1273 * very low in the GTT. Ensure this doesn't happen.
1274 *
1275 * Note that actual hangs have only been observed on gen7, but for
1276 * paranoia do it everywhere.
1277 */
1278 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
1279
1280 return vma->obj;
1281}
1282
Eric Anholtae662d32012-01-03 09:23:29 -08001283static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001284i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1285 struct drm_file *file,
1286 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001287 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001288{
Jani Nikula50227e12014-03-31 14:27:21 +03001289 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +02001290 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001291 struct drm_i915_gem_object *batch_obj;
Brad Volkin78a42372014-12-11 12:13:09 -08001292 struct drm_i915_gem_object *shadow_batch_obj = NULL;
1293 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001294 struct intel_engine_cs *ring;
Oscar Mateo273497e2014-05-22 14:13:37 +01001295 struct intel_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001296 struct i915_address_space *vm;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001297 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Oscar Mateo78382592014-07-03 16:28:05 +01001298 u64 exec_start = args->batch_start_offset;
1299 u32 flags;
1300 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001301 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001302
Daniel Vettered5982e2013-01-17 22:23:36 +01001303 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001304 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001305
Chris Wilsonad19f102014-08-10 06:29:08 +01001306 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001307 if (ret)
1308 return ret;
1309
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001310 flags = 0;
1311 if (args->flags & I915_EXEC_SECURE) {
1312 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1313 return -EPERM;
1314
1315 flags |= I915_DISPATCH_SECURE;
1316 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001317 if (args->flags & I915_EXEC_IS_PINNED)
1318 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001319
Zhao Yakuib1a93302014-04-17 10:37:36 +08001320 if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
Daniel Vetterff240192012-01-31 21:08:14 +01001321 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001322 (int)(args->flags & I915_EXEC_RING_MASK));
1323 return -EINVAL;
1324 }
Ben Widawskyca01b122013-12-06 14:11:00 -08001325
1326 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1327 ring = &dev_priv->ring[RCS];
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001328 else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
1329 if (HAS_BSD2(dev)) {
1330 int ring_id;
1331 ring_id = gen8_dispatch_bsd_ring(dev, file);
1332 ring = &dev_priv->ring[ring_id];
1333 } else
1334 ring = &dev_priv->ring[VCS];
1335 } else
Ben Widawskyca01b122013-12-06 14:11:00 -08001336 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1337
Chris Wilsona15817c2012-05-11 14:29:31 +01001338 if (!intel_ring_initialized(ring)) {
1339 DRM_DEBUG("execbuf with invalid ring: %d\n",
1340 (int)(args->flags & I915_EXEC_RING_MASK));
1341 return -EINVAL;
1342 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001343
1344 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001345 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001346 return -EINVAL;
1347 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001348
Paulo Zanonif65c9162013-11-27 18:20:34 -02001349 intel_runtime_pm_get(dev_priv);
1350
Chris Wilson54cf91d2010-11-25 18:00:26 +00001351 ret = i915_mutex_lock_interruptible(dev);
1352 if (ret)
1353 goto pre_mutex_err;
1354
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001355 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001356 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001357 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001358 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001359 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001360 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001361
1362 i915_gem_context_reference(ctx);
1363
Daniel Vetterae6c4802014-08-06 15:04:53 +02001364 if (ctx->ppgtt)
1365 vm = &ctx->ppgtt->base;
1366 else
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001367 vm = &dev_priv->gtt.base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001368
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001369 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001370 if (eb == NULL) {
Ben Widawsky935f38d2014-04-04 22:41:07 -07001371 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001372 mutex_unlock(&dev->struct_mutex);
1373 ret = -ENOMEM;
1374 goto pre_mutex_err;
1375 }
1376
Chris Wilson54cf91d2010-11-25 18:00:26 +00001377 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001378 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001379 if (ret)
1380 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001381
Chris Wilson6fe4f142011-01-10 17:35:37 +00001382 /* take note of the batch buffer before we might reorder the lists */
Chris Wilsond23db882014-05-23 08:48:08 +02001383 batch_obj = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001384
Chris Wilson54cf91d2010-11-25 18:00:26 +00001385 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001386 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001387 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001388 if (ret)
1389 goto err;
1390
1391 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001392 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001393 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001394 if (ret) {
1395 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001396 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001397 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001398 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1399 }
1400 if (ret)
1401 goto err;
1402 }
1403
1404 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001405 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001406 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001407 ret = -EINVAL;
1408 goto err;
1409 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001410
Brad Volkin351e3db2014-02-18 10:15:46 -08001411 if (i915_needs_cmd_parser(ring)) {
Brad Volkin78a42372014-12-11 12:13:09 -08001412 shadow_batch_obj =
1413 i915_gem_batch_pool_get(&dev_priv->mm.batch_pool,
1414 batch_obj->base.size);
1415 if (IS_ERR(shadow_batch_obj)) {
1416 ret = PTR_ERR(shadow_batch_obj);
1417 /* Don't try to clean up the obj in the error path */
1418 shadow_batch_obj = NULL;
1419 goto err;
1420 }
1421
1422 ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 4096, 0);
1423 if (ret)
1424 goto err;
1425
Brad Volkin351e3db2014-02-18 10:15:46 -08001426 ret = i915_parse_cmds(ring,
1427 batch_obj,
Brad Volkin78a42372014-12-11 12:13:09 -08001428 shadow_batch_obj,
Brad Volkin351e3db2014-02-18 10:15:46 -08001429 args->batch_start_offset,
Brad Volkinb9ffd802014-12-11 12:13:10 -08001430 args->batch_len,
Brad Volkin351e3db2014-02-18 10:15:46 -08001431 file->is_master);
Brad Volkin78a42372014-12-11 12:13:09 -08001432 i915_gem_object_ggtt_unpin(shadow_batch_obj);
1433
Brad Volkin42c71562014-10-16 12:24:42 -07001434 if (ret) {
1435 if (ret != -EACCES)
1436 goto err;
1437 } else {
Brad Volkin78a42372014-12-11 12:13:09 -08001438 struct i915_vma *vma;
1439
1440 memset(&shadow_exec_entry, 0,
1441 sizeof(shadow_exec_entry));
1442
1443 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1444 vma->exec_entry = &shadow_exec_entry;
Brad Volkin0079a7d2014-12-11 12:13:11 -08001445 vma->exec_entry->flags = __EXEC_OBJECT_PURGEABLE;
Brad Volkin78a42372014-12-11 12:13:09 -08001446 drm_gem_object_reference(&shadow_batch_obj->base);
1447 list_add_tail(&vma->exec_list, &eb->vmas);
1448
1449 shadow_batch_obj->base.pending_read_domains =
1450 batch_obj->base.pending_read_domains;
1451
1452 batch_obj = shadow_batch_obj;
1453
Brad Volkin42c71562014-10-16 12:24:42 -07001454 /*
Brad Volkin78a42372014-12-11 12:13:09 -08001455 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1456 * bit from MI_BATCH_BUFFER_START commands issued in the
1457 * dispatch_execbuffer implementations. We specifically
1458 * don't want that set when the command parser is
1459 * enabled.
Brad Volkin42c71562014-10-16 12:24:42 -07001460 *
Brad Volkin78a42372014-12-11 12:13:09 -08001461 * FIXME: with aliasing ppgtt, buffers that should only
1462 * be in ggtt still end up in the aliasing ppgtt. remove
1463 * this check when that is fixed.
Brad Volkin42c71562014-10-16 12:24:42 -07001464 */
Brad Volkin78a42372014-12-11 12:13:09 -08001465 if (USES_FULL_PPGTT(dev))
1466 flags |= I915_DISPATCH_SECURE;
Brad Volkin42c71562014-10-16 12:24:42 -07001467 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001468 }
1469
Brad Volkin78a42372014-12-11 12:13:09 -08001470 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1471
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001472 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1473 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001474 * hsw should have this fixed, but bdw mucks it up again. */
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001475 if (flags & I915_DISPATCH_SECURE) {
1476 /*
1477 * So on first glance it looks freaky that we pin the batch here
1478 * outside of the reservation loop. But:
1479 * - The batch is already pinned into the relevant ppgtt, so we
1480 * already have the backing storage fully allocated.
1481 * - No other BO uses the global gtt (well contexts, but meh),
1482 * so we don't really have issues with mutliple objects not
1483 * fitting due to fragmentation.
1484 * So this is actually safe.
1485 */
1486 ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
1487 if (ret)
1488 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001489
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001490 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001491 } else
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001492 exec_start += i915_gem_obj_offset(batch_obj, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001493
Oscar Mateoa83014d2014-07-24 17:04:21 +01001494 ret = dev_priv->gt.do_execbuf(dev, file, ring, ctx, args,
1495 &eb->vmas, batch_obj, exec_start, flags);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001496
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001497 /*
1498 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1499 * batch vma for correctness. For less ugly and less fragility this
1500 * needs to be adjusted to also track the ggtt batch vma properly as
1501 * active.
1502 */
1503 if (flags & I915_DISPATCH_SECURE)
1504 i915_gem_object_ggtt_unpin(batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001505err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001506 /* the request owns the ref now */
1507 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001508 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001509
1510 mutex_unlock(&dev->struct_mutex);
1511
1512pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001513 /* intel_gpu_busy should also get a ref, so it will free when the device
1514 * is really idle. */
1515 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001516 return ret;
1517}
1518
1519/*
1520 * Legacy execbuffer just creates an exec2 list from the original exec object
1521 * list array and passes it to the real function.
1522 */
1523int
1524i915_gem_execbuffer(struct drm_device *dev, void *data,
1525 struct drm_file *file)
1526{
1527 struct drm_i915_gem_execbuffer *args = data;
1528 struct drm_i915_gem_execbuffer2 exec2;
1529 struct drm_i915_gem_exec_object *exec_list = NULL;
1530 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1531 int ret, i;
1532
Chris Wilson54cf91d2010-11-25 18:00:26 +00001533 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001534 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001535 return -EINVAL;
1536 }
1537
1538 /* Copy in the exec list from userland */
1539 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1540 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1541 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001542 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001543 args->buffer_count);
1544 drm_free_large(exec_list);
1545 drm_free_large(exec2_list);
1546 return -ENOMEM;
1547 }
1548 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001549 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001550 sizeof(*exec_list) * args->buffer_count);
1551 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001552 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001553 args->buffer_count, ret);
1554 drm_free_large(exec_list);
1555 drm_free_large(exec2_list);
1556 return -EFAULT;
1557 }
1558
1559 for (i = 0; i < args->buffer_count; i++) {
1560 exec2_list[i].handle = exec_list[i].handle;
1561 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1562 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1563 exec2_list[i].alignment = exec_list[i].alignment;
1564 exec2_list[i].offset = exec_list[i].offset;
1565 if (INTEL_INFO(dev)->gen < 4)
1566 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1567 else
1568 exec2_list[i].flags = 0;
1569 }
1570
1571 exec2.buffers_ptr = args->buffers_ptr;
1572 exec2.buffer_count = args->buffer_count;
1573 exec2.batch_start_offset = args->batch_start_offset;
1574 exec2.batch_len = args->batch_len;
1575 exec2.DR1 = args->DR1;
1576 exec2.DR4 = args->DR4;
1577 exec2.num_cliprects = args->num_cliprects;
1578 exec2.cliprects_ptr = args->cliprects_ptr;
1579 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001580 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001581
Ben Widawsky41bde552013-12-06 14:11:21 -08001582 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001583 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001584 struct drm_i915_gem_exec_object __user *user_exec_list =
1585 to_user_ptr(args->buffers_ptr);
1586
Chris Wilson54cf91d2010-11-25 18:00:26 +00001587 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001588 for (i = 0; i < args->buffer_count; i++) {
1589 ret = __copy_to_user(&user_exec_list[i].offset,
1590 &exec2_list[i].offset,
1591 sizeof(user_exec_list[i].offset));
1592 if (ret) {
1593 ret = -EFAULT;
1594 DRM_DEBUG("failed to copy %d exec entries "
1595 "back to user (%d)\n",
1596 args->buffer_count, ret);
1597 break;
1598 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001599 }
1600 }
1601
1602 drm_free_large(exec_list);
1603 drm_free_large(exec2_list);
1604 return ret;
1605}
1606
1607int
1608i915_gem_execbuffer2(struct drm_device *dev, void *data,
1609 struct drm_file *file)
1610{
1611 struct drm_i915_gem_execbuffer2 *args = data;
1612 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1613 int ret;
1614
Xi Wanged8cd3b2012-04-23 04:06:41 -04001615 if (args->buffer_count < 1 ||
1616 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001617 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001618 return -EINVAL;
1619 }
1620
Daniel Vetter9cb34662014-04-24 08:09:11 +02001621 if (args->rsvd2 != 0) {
1622 DRM_DEBUG("dirty rvsd2 field\n");
1623 return -EINVAL;
1624 }
1625
Chris Wilson8408c282011-02-21 12:54:48 +00001626 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001627 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001628 if (exec2_list == NULL)
1629 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1630 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001631 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001632 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001633 args->buffer_count);
1634 return -ENOMEM;
1635 }
1636 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001637 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001638 sizeof(*exec2_list) * args->buffer_count);
1639 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001640 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001641 args->buffer_count, ret);
1642 drm_free_large(exec2_list);
1643 return -EFAULT;
1644 }
1645
Ben Widawsky41bde552013-12-06 14:11:21 -08001646 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001647 if (!ret) {
1648 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001649 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001650 to_user_ptr(args->buffers_ptr);
1651 int i;
1652
1653 for (i = 0; i < args->buffer_count; i++) {
1654 ret = __copy_to_user(&user_exec_list[i].offset,
1655 &exec2_list[i].offset,
1656 sizeof(user_exec_list[i].offset));
1657 if (ret) {
1658 ret = -EFAULT;
1659 DRM_DEBUG("failed to copy %d exec entries "
1660 "back to user\n",
1661 args->buffer_count);
1662 break;
1663 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001664 }
1665 }
1666
1667 drm_free_large(exec2_list);
1668 return ret;
1669}