blob: b773368fc62c8ac67717f6770ce20fd642a1bc8c [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
125 drm_gem_object_reference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200126 list_add_tail(&obj->obj_exec_link, &objects);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000127 }
128 spin_unlock(&file->table_lock);
129
Ben Widawsky27173f12013-08-14 11:38:36 +0200130 i = 0;
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000131 while (!list_empty(&objects)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200132 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -0800133
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000134 obj = list_first_entry(&objects,
135 struct drm_i915_gem_object,
136 obj_exec_link);
137
Daniel Vettere656a6c2013-08-14 14:14:04 +0200138 /*
139 * NOTE: We can leak any vmas created here when something fails
140 * later on. But that's no issue since vma_unbind can deal with
141 * vmas which are not actually bound. And since only
142 * lookup_or_create exists as an interface to get at the vma
143 * from the (obj, vm) we don't run the risk of creating
144 * duplicated vmas for the same vm.
145 */
Daniel Vetterda51a1e2014-08-11 12:08:58 +0200146 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
Ben Widawsky27173f12013-08-14 11:38:36 +0200147 if (IS_ERR(vma)) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200148 DRM_DEBUG("Failed to lookup VMA\n");
149 ret = PTR_ERR(vma);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000150 goto err;
Ben Widawsky27173f12013-08-14 11:38:36 +0200151 }
152
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000153 /* Transfer ownership from the objects list to the vmas list. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200154 list_add_tail(&vma->exec_list, &eb->vmas);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000155 list_del_init(&obj->obj_exec_link);
Ben Widawsky27173f12013-08-14 11:38:36 +0200156
157 vma->exec_entry = &exec[i];
158 if (eb->and < 0) {
159 eb->lut[i] = vma;
160 } else {
161 uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
162 vma->exec_handle = handle;
163 hlist_add_head(&vma->exec_node,
164 &eb->buckets[handle & eb->and]);
165 }
166 ++i;
167 }
168
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000169 return 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200170
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000171
172err:
Ben Widawsky27173f12013-08-14 11:38:36 +0200173 while (!list_empty(&objects)) {
174 obj = list_first_entry(&objects,
175 struct drm_i915_gem_object,
176 obj_exec_link);
177 list_del_init(&obj->obj_exec_link);
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000178 drm_gem_object_unreference(&obj->base);
Ben Widawsky27173f12013-08-14 11:38:36 +0200179 }
Chris Wilson9ae9ab52013-12-04 09:52:58 +0000180 /*
181 * Objects already transfered to the vmas list will be unreferenced by
182 * eb_destroy.
183 */
184
Ben Widawsky27173f12013-08-14 11:38:36 +0200185 return ret;
Chris Wilson3b96eff2013-01-08 10:53:14 +0000186}
187
Ben Widawsky27173f12013-08-14 11:38:36 +0200188static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
Chris Wilson67731b82010-12-08 10:38:14 +0000189{
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000190 if (eb->and < 0) {
191 if (handle >= -eb->and)
192 return NULL;
193 return eb->lut[handle];
194 } else {
195 struct hlist_head *head;
196 struct hlist_node *node;
Chris Wilson67731b82010-12-08 10:38:14 +0000197
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000198 head = &eb->buckets[handle & eb->and];
199 hlist_for_each(node, head) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200200 struct i915_vma *vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000201
Ben Widawsky27173f12013-08-14 11:38:36 +0200202 vma = hlist_entry(node, struct i915_vma, exec_node);
203 if (vma->exec_handle == handle)
204 return vma;
Chris Wilsoneef90cc2013-01-08 10:53:17 +0000205 }
206 return NULL;
Chris Wilson67731b82010-12-08 10:38:14 +0000207 }
Chris Wilson67731b82010-12-08 10:38:14 +0000208}
209
Chris Wilsona415d352013-11-26 11:23:15 +0000210static void
211i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
212{
213 struct drm_i915_gem_exec_object2 *entry;
214 struct drm_i915_gem_object *obj = vma->obj;
215
216 if (!drm_mm_node_allocated(&vma->node))
217 return;
218
219 entry = vma->exec_entry;
220
221 if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
222 i915_gem_object_unpin_fence(obj);
223
224 if (entry->flags & __EXEC_OBJECT_HAS_PIN)
Daniel Vetter3d7f0f92013-12-18 16:23:37 +0100225 vma->pin_count--;
Chris Wilsona415d352013-11-26 11:23:15 +0000226
Brad Volkin0079a7d2014-12-11 12:13:11 -0800227 if (entry->flags & __EXEC_OBJECT_PURGEABLE)
228 obj->madv = I915_MADV_DONTNEED;
229
230 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE |
231 __EXEC_OBJECT_HAS_PIN |
232 __EXEC_OBJECT_PURGEABLE);
Chris Wilsona415d352013-11-26 11:23:15 +0000233}
234
235static void eb_destroy(struct eb_vmas *eb)
236{
Ben Widawsky27173f12013-08-14 11:38:36 +0200237 while (!list_empty(&eb->vmas)) {
238 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000239
Ben Widawsky27173f12013-08-14 11:38:36 +0200240 vma = list_first_entry(&eb->vmas,
241 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000242 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200243 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000244 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200245 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000246 }
Chris Wilson67731b82010-12-08 10:38:14 +0000247 kfree(eb);
248}
249
Chris Wilsondabdfe02012-03-26 10:10:27 +0200250static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
251{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300252 return (HAS_LLC(obj->base.dev) ||
253 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilson504c7262012-08-23 13:12:52 +0100254 !obj->map_and_fenceable ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200255 obj->cache_level != I915_CACHE_NONE);
256}
257
Chris Wilson54cf91d2010-11-25 18:00:26 +0000258static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100259relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700260 struct drm_i915_gem_relocation_entry *reloc,
261 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100262{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700263 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100264 uint32_t page_offset = offset_in_page(reloc->offset);
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700265 uint64_t delta = reloc->delta + target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100266 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800267 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100268
Chris Wilson2cc86b82013-08-26 19:51:00 -0300269 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100270 if (ret)
271 return ret;
272
273 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
274 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700275 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700276
277 if (INTEL_INFO(dev)->gen >= 8) {
278 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
279
280 if (page_offset == 0) {
281 kunmap_atomic(vaddr);
282 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
283 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
284 }
285
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700286 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700287 }
288
Rafael Barbalho5032d872013-08-21 17:10:51 +0100289 kunmap_atomic(vaddr);
290
291 return 0;
292}
293
294static int
295relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700296 struct drm_i915_gem_relocation_entry *reloc,
297 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100298{
299 struct drm_device *dev = obj->base.dev;
300 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700301 uint64_t delta = reloc->delta + target_offset;
Chris Wilson906843c2014-08-10 06:29:11 +0100302 uint64_t offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100303 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800304 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100305
306 ret = i915_gem_object_set_to_gtt_domain(obj, true);
307 if (ret)
308 return ret;
309
310 ret = i915_gem_object_put_fence(obj);
311 if (ret)
312 return ret;
313
314 /* Map the page containing the relocation we're going to perform. */
Chris Wilson906843c2014-08-10 06:29:11 +0100315 offset = i915_gem_obj_ggtt_offset(obj);
316 offset += reloc->offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100317 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100318 offset & PAGE_MASK);
319 iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700320
321 if (INTEL_INFO(dev)->gen >= 8) {
Chris Wilson906843c2014-08-10 06:29:11 +0100322 offset += sizeof(uint32_t);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700323
Chris Wilson906843c2014-08-10 06:29:11 +0100324 if (offset_in_page(offset) == 0) {
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700325 io_mapping_unmap_atomic(reloc_page);
Chris Wilson906843c2014-08-10 06:29:11 +0100326 reloc_page =
327 io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
328 offset);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700329 }
330
Chris Wilson906843c2014-08-10 06:29:11 +0100331 iowrite32(upper_32_bits(delta),
332 reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700333 }
334
Rafael Barbalho5032d872013-08-21 17:10:51 +0100335 io_mapping_unmap_atomic(reloc_page);
336
337 return 0;
338}
339
340static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000341i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200342 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800343 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000344{
345 struct drm_device *dev = obj->base.dev;
346 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100347 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200348 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700349 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800350 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000351
Chris Wilson67731b82010-12-08 10:38:14 +0000352 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200353 target_vma = eb_get_vma(eb, reloc->target_handle);
354 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000355 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200356 target_i915_obj = target_vma->obj;
357 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000358
Ben Widawsky5ce09722013-11-25 09:54:40 -0800359 target_offset = target_vma->node.start;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000360
Eric Anholte844b992012-07-31 15:35:01 -0700361 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
362 * pipe_control writes because the gpu doesn't properly redirect them
363 * through the ppgtt for non_secure batchbuffers. */
364 if (unlikely(IS_GEN6(dev) &&
365 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000366 !(target_vma->bound & GLOBAL_BIND))) {
367 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
368 GLOBAL_BIND);
369 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
370 return ret;
371 }
Eric Anholte844b992012-07-31 15:35:01 -0700372
Chris Wilson54cf91d2010-11-25 18:00:26 +0000373 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000374 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100375 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000376 "obj %p target %d offset %d "
377 "read %08x write %08x",
378 obj, reloc->target_handle,
379 (int) reloc->offset,
380 reloc->read_domains,
381 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800382 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000383 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100384 if (unlikely((reloc->write_domain | reloc->read_domains)
385 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100386 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000387 "obj %p target %d offset %d "
388 "read %08x write %08x",
389 obj, reloc->target_handle,
390 (int) reloc->offset,
391 reloc->read_domains,
392 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800393 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000394 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000395
396 target_obj->pending_read_domains |= reloc->read_domains;
397 target_obj->pending_write_domain |= reloc->write_domain;
398
399 /* If the relocation already has the right value in it, no
400 * more work needs to be done.
401 */
402 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000403 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000404
405 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700406 if (unlikely(reloc->offset >
407 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100408 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000409 "obj %p target %d offset %d size %d.\n",
410 obj, reloc->target_handle,
411 (int) reloc->offset,
412 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800413 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000414 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000415 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100416 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000417 "obj %p target %d offset %d.\n",
418 obj, reloc->target_handle,
419 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800420 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000421 }
422
Chris Wilsondabdfe02012-03-26 10:10:27 +0200423 /* We can't wait for rendering with pagefaults disabled */
424 if (obj->active && in_atomic())
425 return -EFAULT;
426
Rafael Barbalho5032d872013-08-21 17:10:51 +0100427 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700428 ret = relocate_entry_cpu(obj, reloc, target_offset);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100429 else
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700430 ret = relocate_entry_gtt(obj, reloc, target_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000431
Daniel Vetterd4d36012013-09-02 20:56:23 +0200432 if (ret)
433 return ret;
434
Chris Wilson54cf91d2010-11-25 18:00:26 +0000435 /* and update the user's relocation entry */
436 reloc->presumed_offset = target_offset;
437
Chris Wilson67731b82010-12-08 10:38:14 +0000438 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000439}
440
441static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200442i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
443 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000444{
Chris Wilson1d83f442012-03-24 20:12:53 +0000445#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
446 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000447 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200448 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000449 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000450
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200451 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000452
Chris Wilson1d83f442012-03-24 20:12:53 +0000453 remain = entry->relocation_count;
454 while (remain) {
455 struct drm_i915_gem_relocation_entry *r = stack_reloc;
456 int count = remain;
457 if (count > ARRAY_SIZE(stack_reloc))
458 count = ARRAY_SIZE(stack_reloc);
459 remain -= count;
460
461 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000462 return -EFAULT;
463
Chris Wilson1d83f442012-03-24 20:12:53 +0000464 do {
465 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000466
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800467 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000468 if (ret)
469 return ret;
470
471 if (r->presumed_offset != offset &&
472 __copy_to_user_inatomic(&user_relocs->presumed_offset,
473 &r->presumed_offset,
474 sizeof(r->presumed_offset))) {
475 return -EFAULT;
476 }
477
478 user_relocs++;
479 r++;
480 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000481 }
482
483 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000484#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000485}
486
487static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200488i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
489 struct eb_vmas *eb,
490 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000491{
Ben Widawsky27173f12013-08-14 11:38:36 +0200492 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000493 int i, ret;
494
495 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800496 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000497 if (ret)
498 return ret;
499 }
500
501 return 0;
502}
503
504static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800505i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000506{
Ben Widawsky27173f12013-08-14 11:38:36 +0200507 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000508 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000509
Chris Wilsond4aeee72011-03-14 15:11:24 +0000510 /* This is the fast path and we cannot handle a pagefault whilst
511 * holding the struct mutex lest the user pass in the relocations
512 * contained within a mmaped bo. For in such a case we, the page
513 * fault handler would call i915_gem_fault() and we would try to
514 * acquire the struct mutex again. Obviously this is bad and so
515 * lockdep complains vehemently.
516 */
517 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200518 list_for_each_entry(vma, &eb->vmas, exec_list) {
519 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000520 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000521 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000522 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000523 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000524
Chris Wilsond4aeee72011-03-14 15:11:24 +0000525 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000526}
527
Chris Wilson1690e1e2011-12-14 13:57:08 +0100528static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200529i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100530 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200531 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100532{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800533 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200534 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200535 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100536 int ret;
537
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100538 flags = 0;
Chris Wilsone6a84462014-08-11 12:00:12 +0200539 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
Chris Wilsonc826c442014-10-31 13:53:53 +0000540 flags |= PIN_GLOBAL | PIN_MAPPABLE;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100541 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
Daniel Vetterbf3d1492014-02-14 14:01:12 +0100542 flags |= PIN_GLOBAL;
Chris Wilsond23db882014-05-23 08:48:08 +0200543 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
544 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100545
546 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100547 if (ret)
548 return ret;
549
Chris Wilson7788a762012-08-24 19:18:18 +0100550 entry->flags |= __EXEC_OBJECT_HAS_PIN;
551
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100552 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
553 ret = i915_gem_object_get_fence(obj);
554 if (ret)
555 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100556
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100557 if (i915_gem_object_pin_fence(obj))
558 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100559 }
560
Ben Widawsky27173f12013-08-14 11:38:36 +0200561 if (entry->offset != vma->node.start) {
562 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100563 *need_reloc = true;
564 }
565
566 if (entry->flags & EXEC_OBJECT_WRITE) {
567 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
568 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
569 }
570
Chris Wilson1690e1e2011-12-14 13:57:08 +0100571 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100572}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100573
Chris Wilsond23db882014-05-23 08:48:08 +0200574static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200575need_reloc_mappable(struct i915_vma *vma)
576{
577 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
578
579 if (entry->relocation_count == 0)
580 return false;
581
582 if (!i915_is_ggtt(vma->vm))
583 return false;
584
585 /* See also use_cpu_reloc() */
586 if (HAS_LLC(vma->obj->base.dev))
587 return false;
588
589 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
590 return false;
591
592 return true;
593}
594
595static bool
596eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200597{
598 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
599 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond23db882014-05-23 08:48:08 +0200600
Chris Wilsone6a84462014-08-11 12:00:12 +0200601 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
Chris Wilsond23db882014-05-23 08:48:08 +0200602 !i915_is_ggtt(vma->vm));
603
604 if (entry->alignment &&
605 vma->node.start & (entry->alignment - 1))
606 return true;
607
Chris Wilsone6a84462014-08-11 12:00:12 +0200608 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
Chris Wilsond23db882014-05-23 08:48:08 +0200609 return true;
610
611 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
612 vma->node.start < BATCH_OFFSET_BIAS)
613 return true;
614
615 return false;
616}
617
Chris Wilson54cf91d2010-11-25 18:00:26 +0000618static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100619i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200620 struct list_head *vmas,
Daniel Vettered5982e2013-01-17 22:23:36 +0100621 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000622{
Chris Wilson432e58e2010-11-25 19:32:06 +0000623 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200624 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700625 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200626 struct list_head ordered_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100627 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
628 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000629
Chris Wilson227f7822014-05-15 10:41:42 +0100630 i915_gem_retire_requests_ring(ring);
631
Ben Widawsky68c8c172013-09-11 14:57:50 -0700632 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
633
Ben Widawsky27173f12013-08-14 11:38:36 +0200634 INIT_LIST_HEAD(&ordered_vmas);
635 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000636 struct drm_i915_gem_exec_object2 *entry;
637 bool need_fence, need_mappable;
638
Ben Widawsky27173f12013-08-14 11:38:36 +0200639 vma = list_first_entry(vmas, struct i915_vma, exec_list);
640 obj = vma->obj;
641 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000642
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100643 if (!has_fenced_gpu_access)
644 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000645 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000646 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
647 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200648 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000649
Chris Wilsone6a84462014-08-11 12:00:12 +0200650 if (need_mappable) {
651 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200652 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200653 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200654 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000655
Daniel Vettered5982e2013-01-17 22:23:36 +0100656 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000657 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000658 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200659 list_splice(&ordered_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000660
661 /* Attempt to pin all of the buffers into the GTT.
662 * This is done in 3 phases:
663 *
664 * 1a. Unbind all objects that do not match the GTT constraints for
665 * the execbuffer (fenceable, mappable, alignment etc).
666 * 1b. Increment pin count for already bound objects.
667 * 2. Bind new objects.
668 * 3. Decrement pin count.
669 *
Chris Wilson7788a762012-08-24 19:18:18 +0100670 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000671 * room for the earlier objects *unless* we need to defragment.
672 */
673 retry = 0;
674 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100675 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000676
677 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200678 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200679 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000680 continue;
681
Chris Wilsone6a84462014-08-11 12:00:12 +0200682 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200683 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000684 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200685 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000686 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000687 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000688 }
689
690 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200691 list_for_each_entry(vma, vmas, exec_list) {
692 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100693 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000694
Ben Widawsky27173f12013-08-14 11:38:36 +0200695 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100696 if (ret)
697 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000698 }
699
Chris Wilsona415d352013-11-26 11:23:15 +0000700err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200701 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000702 return ret;
703
Chris Wilsona415d352013-11-26 11:23:15 +0000704 /* Decrement pin count for bound objects */
705 list_for_each_entry(vma, vmas, exec_list)
706 i915_gem_execbuffer_unreserve_vma(vma);
707
Ben Widawsky68c8c172013-09-11 14:57:50 -0700708 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000709 if (ret)
710 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000711 } while (1);
712}
713
714static int
715i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100716 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000717 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100718 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200719 struct eb_vmas *eb,
720 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000721{
722 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200723 struct i915_address_space *vm;
724 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100725 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000726 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000727 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200728 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000729
Ben Widawsky27173f12013-08-14 11:38:36 +0200730 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
731
Chris Wilson67731b82010-12-08 10:38:14 +0000732 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200733 while (!list_empty(&eb->vmas)) {
734 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
735 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000736 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200737 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000738 }
739
Chris Wilson54cf91d2010-11-25 18:00:26 +0000740 mutex_unlock(&dev->struct_mutex);
741
742 total = 0;
743 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000744 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000745
Chris Wilsondd6864a2011-01-12 23:49:13 +0000746 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000747 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000748 if (reloc == NULL || reloc_offset == NULL) {
749 drm_free_large(reloc);
750 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000751 mutex_lock(&dev->struct_mutex);
752 return -ENOMEM;
753 }
754
755 total = 0;
756 for (i = 0; i < count; i++) {
757 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000758 u64 invalid_offset = (u64)-1;
759 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000760
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200761 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000762
763 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000764 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000765 ret = -EFAULT;
766 mutex_lock(&dev->struct_mutex);
767 goto err;
768 }
769
Chris Wilson262b6d32013-01-15 16:17:54 +0000770 /* As we do not update the known relocation offsets after
771 * relocating (due to the complexities in lock handling),
772 * we need to mark them as invalid now so that we force the
773 * relocation processing next time. Just in case the target
774 * object is evicted and then rebound into its old
775 * presumed_offset before the next execbuffer - if that
776 * happened we would make the mistake of assuming that the
777 * relocations were valid.
778 */
779 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100780 if (__copy_to_user(&user_relocs[j].presumed_offset,
781 &invalid_offset,
782 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000783 ret = -EFAULT;
784 mutex_lock(&dev->struct_mutex);
785 goto err;
786 }
787 }
788
Chris Wilsondd6864a2011-01-12 23:49:13 +0000789 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000790 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000791 }
792
793 ret = i915_mutex_lock_interruptible(dev);
794 if (ret) {
795 mutex_lock(&dev->struct_mutex);
796 goto err;
797 }
798
Chris Wilson67731b82010-12-08 10:38:14 +0000799 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000800 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200801 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000802 if (ret)
803 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000804
Daniel Vettered5982e2013-01-17 22:23:36 +0100805 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +0200806 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000807 if (ret)
808 goto err;
809
Ben Widawsky27173f12013-08-14 11:38:36 +0200810 list_for_each_entry(vma, &eb->vmas, exec_list) {
811 int offset = vma->exec_entry - exec;
812 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
813 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000814 if (ret)
815 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000816 }
817
818 /* Leave the user relocations as are, this is the painfully slow path,
819 * and we want to avoid the complication of dropping the lock whilst
820 * having buffers reserved in the aperture and so causing spurious
821 * ENOSPC for random operations.
822 */
823
824err:
825 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000826 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000827 return ret;
828}
829
Chris Wilson54cf91d2010-11-25 18:00:26 +0000830static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100831i915_gem_execbuffer_move_to_gpu(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200832 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000833{
Ben Widawsky27173f12013-08-14 11:38:36 +0200834 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200835 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100836 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000837 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000838
Ben Widawsky27173f12013-08-14 11:38:36 +0200839 list_for_each_entry(vma, vmas, exec_list) {
840 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky2911a352012-04-05 14:47:36 -0700841 ret = i915_gem_object_sync(obj, ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000842 if (ret)
843 return ret;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200844
845 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100846 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200847
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200848 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000849 }
850
Chris Wilson000433b2013-08-08 14:41:09 +0100851 if (flush_chipset)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800852 i915_gem_chipset_flush(ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200853
854 if (flush_domains & I915_GEM_DOMAIN_GTT)
855 wmb();
856
Chris Wilson09cf7c92012-07-13 14:14:08 +0100857 /* Unconditionally invalidate gpu caches and ensure that we do flush
858 * any residual writes from the previous batch.
859 */
Chris Wilsona7b97612012-07-20 12:41:08 +0100860 return intel_ring_invalidate_all_caches(ring);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000861}
862
Chris Wilson432e58e2010-11-25 19:32:06 +0000863static bool
864i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000865{
Daniel Vettered5982e2013-01-17 22:23:36 +0100866 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
867 return false;
868
Chris Wilson432e58e2010-11-25 19:32:06 +0000869 return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000870}
871
872static int
Chris Wilsonad19f102014-08-10 06:29:08 +0100873validate_exec_list(struct drm_device *dev,
874 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000875 int count)
876{
Daniel Vetterb205ca52013-09-19 14:00:11 +0200877 unsigned relocs_total = 0;
878 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +0100879 unsigned invalid_flags;
880 int i;
881
882 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
883 if (USES_FULL_PPGTT(dev))
884 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000885
886 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200887 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000888 int length; /* limited by fault_in_pages_readable() */
889
Chris Wilsonad19f102014-08-10 06:29:08 +0100890 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +0100891 return -EINVAL;
892
Kees Cook3118a4f2013-03-11 17:31:45 -0700893 /* First check for malicious input causing overflow in
894 * the worst case where we need to allocate the entire
895 * relocation tree as a single array.
896 */
897 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000898 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -0700899 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000900
901 length = exec[i].relocation_count *
902 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -0700903 /*
904 * We must check that the entire relocation array is safe
905 * to read, but since we may need to update the presumed
906 * offsets during execution, check for full write access.
907 */
Chris Wilson54cf91d2010-11-25 18:00:26 +0000908 if (!access_ok(VERIFY_WRITE, ptr, length))
909 return -EFAULT;
910
Jani Nikulad330a952014-01-21 11:24:25 +0200911 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +0800912 if (fault_in_multipages_readable(ptr, length))
913 return -EFAULT;
914 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000915 }
916
917 return 0;
918}
919
Oscar Mateo273497e2014-05-22 14:13:37 +0100920static struct intel_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200921i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100922 struct intel_engine_cs *ring, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200923{
Oscar Mateo273497e2014-05-22 14:13:37 +0100924 struct intel_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200925 struct i915_ctx_hang_stats *hs;
926
Oscar Mateo821d66d2014-07-03 16:28:00 +0100927 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +0100928 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200929
Ben Widawsky41bde552013-12-06 14:11:21 -0800930 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -1000931 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -0800932 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200933
Ben Widawsky41bde552013-12-06 14:11:21 -0800934 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200935 if (hs->banned) {
936 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -0800937 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200938 }
939
Oscar Mateoec3e9962014-07-24 17:04:18 +0100940 if (i915.enable_execlists && !ctx->engine[ring->id].state) {
941 int ret = intel_lr_context_deferred_create(ctx, ring);
942 if (ret) {
943 DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
944 return ERR_PTR(ret);
945 }
946 }
947
Ben Widawsky41bde552013-12-06 14:11:21 -0800948 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +0200949}
950
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100951void
Ben Widawsky27173f12013-08-14 11:38:36 +0200952i915_gem_execbuffer_move_to_active(struct list_head *vmas,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100953 struct intel_engine_cs *ring)
Chris Wilson432e58e2010-11-25 19:32:06 +0000954{
John Harrison97b2a6a2014-11-24 18:49:26 +0000955 struct drm_i915_gem_request *req = intel_ring_get_request(ring);
Ben Widawsky27173f12013-08-14 11:38:36 +0200956 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +0000957
Ben Widawsky27173f12013-08-14 11:38:36 +0200958 list_for_each_entry(vma, vmas, exec_list) {
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100959 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Ben Widawsky27173f12013-08-14 11:38:36 +0200960 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +0100961 u32 old_read = obj->base.read_domains;
962 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +0000963
Chris Wilson432e58e2010-11-25 19:32:06 +0000964 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +0100965 if (obj->base.write_domain == 0)
966 obj->base.pending_read_domains |= obj->base.read_domains;
967 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +0000968
Ben Widawskye2d05a82013-09-24 09:57:58 -0700969 i915_vma_move_to_active(vma, ring);
Chris Wilson432e58e2010-11-25 19:32:06 +0000970 if (obj->base.write_domain) {
971 obj->dirty = 1;
John Harrison97b2a6a2014-11-24 18:49:26 +0000972 i915_gem_request_assign(&obj->last_write_req, req);
Daniel Vetterf99d7062014-06-19 16:01:59 +0200973
974 intel_fb_obj_invalidate(obj, ring);
Chris Wilsonc8725f32014-03-17 12:21:55 +0000975
976 /* update for the implicit flush after a batch */
977 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson432e58e2010-11-25 19:32:06 +0000978 }
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100979 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
John Harrison97b2a6a2014-11-24 18:49:26 +0000980 i915_gem_request_assign(&obj->last_fenced_req, req);
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100981 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
982 struct drm_i915_private *dev_priv = to_i915(ring->dev);
983 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
984 &dev_priv->mm.fence_list);
985 }
986 }
Chris Wilson432e58e2010-11-25 19:32:06 +0000987
Chris Wilsondb53a302011-02-03 11:57:46 +0000988 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +0000989 }
990}
991
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100992void
Chris Wilson54cf91d2010-11-25 18:00:26 +0000993i915_gem_execbuffer_retire_commands(struct drm_device *dev,
Chris Wilson432e58e2010-11-25 19:32:06 +0000994 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100995 struct intel_engine_cs *ring,
Mika Kuoppala7d736f42013-06-12 15:01:39 +0300996 struct drm_i915_gem_object *obj)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000997{
Daniel Vettercc889e02012-06-13 20:45:19 +0200998 /* Unconditionally force add_request to emit a full flush. */
999 ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001000
Chris Wilson432e58e2010-11-25 19:32:06 +00001001 /* Add a breadcrumb for the completion of the batch buffer */
John Harrison9400ae52014-11-24 18:49:36 +00001002 (void)__i915_add_request(ring, file, obj);
Chris Wilson432e58e2010-11-25 19:32:06 +00001003}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001004
1005static int
Eric Anholtae662d32012-01-03 09:23:29 -08001006i915_reset_gen7_sol_offsets(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001007 struct intel_engine_cs *ring)
Eric Anholtae662d32012-01-03 09:23:29 -08001008{
Jani Nikula50227e12014-03-31 14:27:21 +03001009 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtae662d32012-01-03 09:23:29 -08001010 int ret, i;
1011
Daniel Vetter9d662da2014-04-24 08:09:09 +02001012 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
1013 DRM_DEBUG("sol reset is gen7/rcs only\n");
1014 return -EINVAL;
1015 }
Eric Anholtae662d32012-01-03 09:23:29 -08001016
1017 ret = intel_ring_begin(ring, 4 * 3);
1018 if (ret)
1019 return ret;
1020
1021 for (i = 0; i < 4; i++) {
1022 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1023 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
1024 intel_ring_emit(ring, 0);
1025 }
1026
1027 intel_ring_advance(ring);
1028
1029 return 0;
1030}
1031
Chris Wilson5c6c6002014-09-06 10:28:27 +01001032static int
1033i915_emit_box(struct intel_engine_cs *ring,
1034 struct drm_clip_rect *box,
1035 int DR1, int DR4)
1036{
1037 int ret;
1038
1039 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
1040 box->y2 <= 0 || box->x2 <= 0) {
1041 DRM_ERROR("Bad box %d,%d..%d,%d\n",
1042 box->x1, box->y1, box->x2, box->y2);
1043 return -EINVAL;
1044 }
1045
1046 if (INTEL_INFO(ring->dev)->gen >= 4) {
1047 ret = intel_ring_begin(ring, 4);
1048 if (ret)
1049 return ret;
1050
1051 intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO_I965);
1052 intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
1053 intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
1054 intel_ring_emit(ring, DR4);
1055 } else {
1056 ret = intel_ring_begin(ring, 6);
1057 if (ret)
1058 return ret;
1059
1060 intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO);
1061 intel_ring_emit(ring, DR1);
1062 intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
1063 intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
1064 intel_ring_emit(ring, DR4);
1065 intel_ring_emit(ring, 0);
1066 }
1067 intel_ring_advance(ring);
1068
1069 return 0;
1070}
1071
Brad Volkin71745372014-12-11 12:13:12 -08001072static struct drm_i915_gem_object*
1073i915_gem_execbuffer_parse(struct intel_engine_cs *ring,
1074 struct drm_i915_gem_exec_object2 *shadow_exec_entry,
1075 struct eb_vmas *eb,
1076 struct drm_i915_gem_object *batch_obj,
1077 u32 batch_start_offset,
1078 u32 batch_len,
1079 bool is_master,
1080 u32 *flags)
1081{
1082 struct drm_i915_private *dev_priv = to_i915(batch_obj->base.dev);
1083 struct drm_i915_gem_object *shadow_batch_obj;
Tvrtko Ursulin72265722015-01-07 17:32:39 +00001084 bool need_reloc = false;
Brad Volkin71745372014-12-11 12:13:12 -08001085 int ret;
1086
1087 shadow_batch_obj = i915_gem_batch_pool_get(&dev_priv->mm.batch_pool,
1088 batch_obj->base.size);
1089 if (IS_ERR(shadow_batch_obj))
1090 return shadow_batch_obj;
1091
1092 ret = i915_parse_cmds(ring,
1093 batch_obj,
1094 shadow_batch_obj,
1095 batch_start_offset,
1096 batch_len,
1097 is_master);
1098 if (ret) {
1099 if (ret == -EACCES)
1100 return batch_obj;
1101 } else {
1102 struct i915_vma *vma;
1103
1104 memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
1105
1106 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1107 vma->exec_entry = shadow_exec_entry;
1108 vma->exec_entry->flags = __EXEC_OBJECT_PURGEABLE;
1109 drm_gem_object_reference(&shadow_batch_obj->base);
Tvrtko Ursulin72265722015-01-07 17:32:39 +00001110 i915_gem_execbuffer_reserve_vma(vma, ring, &need_reloc);
Brad Volkin71745372014-12-11 12:13:12 -08001111 list_add_tail(&vma->exec_list, &eb->vmas);
1112
1113 shadow_batch_obj->base.pending_read_domains =
1114 batch_obj->base.pending_read_domains;
1115
1116 /*
1117 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1118 * bit from MI_BATCH_BUFFER_START commands issued in the
1119 * dispatch_execbuffer implementations. We specifically
1120 * don't want that set when the command parser is
1121 * enabled.
1122 *
1123 * FIXME: with aliasing ppgtt, buffers that should only
1124 * be in ggtt still end up in the aliasing ppgtt. remove
1125 * this check when that is fixed.
1126 */
1127 if (USES_FULL_PPGTT(dev))
1128 *flags |= I915_DISPATCH_SECURE;
1129 }
1130
1131 return ret ? ERR_PTR(ret) : shadow_batch_obj;
1132}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001133
Oscar Mateoa83014d2014-07-24 17:04:21 +01001134int
1135i915_gem_ringbuffer_submission(struct drm_device *dev, struct drm_file *file,
1136 struct intel_engine_cs *ring,
1137 struct intel_context *ctx,
1138 struct drm_i915_gem_execbuffer2 *args,
1139 struct list_head *vmas,
1140 struct drm_i915_gem_object *batch_obj,
1141 u64 exec_start, u32 flags)
Oscar Mateo78382592014-07-03 16:28:05 +01001142{
1143 struct drm_clip_rect *cliprects = NULL;
1144 struct drm_i915_private *dev_priv = dev->dev_private;
1145 u64 exec_len;
1146 int instp_mode;
1147 u32 instp_mask;
1148 int i, ret = 0;
1149
1150 if (args->num_cliprects != 0) {
1151 if (ring != &dev_priv->ring[RCS]) {
1152 DRM_DEBUG("clip rectangles are only valid with the render ring\n");
1153 return -EINVAL;
1154 }
1155
1156 if (INTEL_INFO(dev)->gen >= 5) {
1157 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
1158 return -EINVAL;
1159 }
1160
1161 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1162 DRM_DEBUG("execbuf with %u cliprects\n",
1163 args->num_cliprects);
1164 return -EINVAL;
1165 }
1166
1167 cliprects = kcalloc(args->num_cliprects,
1168 sizeof(*cliprects),
1169 GFP_KERNEL);
1170 if (cliprects == NULL) {
1171 ret = -ENOMEM;
1172 goto error;
1173 }
1174
1175 if (copy_from_user(cliprects,
1176 to_user_ptr(args->cliprects_ptr),
1177 sizeof(*cliprects)*args->num_cliprects)) {
1178 ret = -EFAULT;
1179 goto error;
1180 }
1181 } else {
1182 if (args->DR4 == 0xffffffff) {
1183 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
1184 args->DR4 = 0;
1185 }
1186
1187 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
1188 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
1189 return -EINVAL;
1190 }
1191 }
1192
1193 ret = i915_gem_execbuffer_move_to_gpu(ring, vmas);
1194 if (ret)
1195 goto error;
1196
1197 ret = i915_switch_context(ring, ctx);
1198 if (ret)
1199 goto error;
1200
1201 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1202 instp_mask = I915_EXEC_CONSTANTS_MASK;
1203 switch (instp_mode) {
1204 case I915_EXEC_CONSTANTS_REL_GENERAL:
1205 case I915_EXEC_CONSTANTS_ABSOLUTE:
1206 case I915_EXEC_CONSTANTS_REL_SURFACE:
1207 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
1208 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
1209 ret = -EINVAL;
1210 goto error;
1211 }
1212
1213 if (instp_mode != dev_priv->relative_constants_mode) {
1214 if (INTEL_INFO(dev)->gen < 4) {
1215 DRM_DEBUG("no rel constants on pre-gen4\n");
1216 ret = -EINVAL;
1217 goto error;
1218 }
1219
1220 if (INTEL_INFO(dev)->gen > 5 &&
1221 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1222 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
1223 ret = -EINVAL;
1224 goto error;
1225 }
1226
1227 /* The HW changed the meaning on this bit on gen6 */
1228 if (INTEL_INFO(dev)->gen >= 6)
1229 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1230 }
1231 break;
1232 default:
1233 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
1234 ret = -EINVAL;
1235 goto error;
1236 }
1237
1238 if (ring == &dev_priv->ring[RCS] &&
1239 instp_mode != dev_priv->relative_constants_mode) {
1240 ret = intel_ring_begin(ring, 4);
1241 if (ret)
1242 goto error;
1243
1244 intel_ring_emit(ring, MI_NOOP);
1245 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1246 intel_ring_emit(ring, INSTPM);
1247 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1248 intel_ring_advance(ring);
1249
1250 dev_priv->relative_constants_mode = instp_mode;
1251 }
1252
1253 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1254 ret = i915_reset_gen7_sol_offsets(dev, ring);
1255 if (ret)
1256 goto error;
1257 }
1258
1259 exec_len = args->batch_len;
1260 if (cliprects) {
1261 for (i = 0; i < args->num_cliprects; i++) {
Chris Wilson5c6c6002014-09-06 10:28:27 +01001262 ret = i915_emit_box(ring, &cliprects[i],
Oscar Mateo78382592014-07-03 16:28:05 +01001263 args->DR1, args->DR4);
1264 if (ret)
1265 goto error;
1266
1267 ret = ring->dispatch_execbuffer(ring,
1268 exec_start, exec_len,
1269 flags);
1270 if (ret)
1271 goto error;
1272 }
1273 } else {
1274 ret = ring->dispatch_execbuffer(ring,
1275 exec_start, exec_len,
1276 flags);
1277 if (ret)
1278 return ret;
1279 }
1280
John Harrison74328ee2014-11-24 18:49:38 +00001281 trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001282
1283 i915_gem_execbuffer_move_to_active(vmas, ring);
1284 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
1285
1286error:
1287 kfree(cliprects);
1288 return ret;
1289}
1290
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001291/**
1292 * Find one BSD ring to dispatch the corresponding BSD command.
1293 * The Ring ID is returned.
1294 */
1295static int gen8_dispatch_bsd_ring(struct drm_device *dev,
1296 struct drm_file *file)
1297{
1298 struct drm_i915_private *dev_priv = dev->dev_private;
1299 struct drm_i915_file_private *file_priv = file->driver_priv;
1300
1301 /* Check whether the file_priv is using one ring */
1302 if (file_priv->bsd_ring)
1303 return file_priv->bsd_ring->id;
1304 else {
1305 /* If no, use the ping-pong mechanism to select one ring */
1306 int ring_id;
1307
1308 mutex_lock(&dev->struct_mutex);
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001309 if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001310 ring_id = VCS;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001311 dev_priv->mm.bsd_ring_dispatch_index = 1;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001312 } else {
1313 ring_id = VCS2;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001314 dev_priv->mm.bsd_ring_dispatch_index = 0;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001315 }
1316 file_priv->bsd_ring = &dev_priv->ring[ring_id];
1317 mutex_unlock(&dev->struct_mutex);
1318 return ring_id;
1319 }
1320}
1321
Chris Wilsond23db882014-05-23 08:48:08 +02001322static struct drm_i915_gem_object *
1323eb_get_batch(struct eb_vmas *eb)
1324{
1325 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
1326
1327 /*
1328 * SNA is doing fancy tricks with compressing batch buffers, which leads
1329 * to negative relocation deltas. Usually that works out ok since the
1330 * relocate address is still positive, except when the batch is placed
1331 * very low in the GTT. Ensure this doesn't happen.
1332 *
1333 * Note that actual hangs have only been observed on gen7, but for
1334 * paranoia do it everywhere.
1335 */
1336 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
1337
1338 return vma->obj;
1339}
1340
Eric Anholtae662d32012-01-03 09:23:29 -08001341static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001342i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1343 struct drm_file *file,
1344 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001345 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001346{
Jani Nikula50227e12014-03-31 14:27:21 +03001347 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +02001348 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001349 struct drm_i915_gem_object *batch_obj;
Brad Volkin78a42372014-12-11 12:13:09 -08001350 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001351 struct intel_engine_cs *ring;
Oscar Mateo273497e2014-05-22 14:13:37 +01001352 struct intel_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001353 struct i915_address_space *vm;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001354 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
Oscar Mateo78382592014-07-03 16:28:05 +01001355 u64 exec_start = args->batch_start_offset;
1356 u32 flags;
1357 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001358 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001359
Daniel Vettered5982e2013-01-17 22:23:36 +01001360 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001361 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001362
Chris Wilsonad19f102014-08-10 06:29:08 +01001363 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001364 if (ret)
1365 return ret;
1366
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001367 flags = 0;
1368 if (args->flags & I915_EXEC_SECURE) {
1369 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1370 return -EPERM;
1371
1372 flags |= I915_DISPATCH_SECURE;
1373 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001374 if (args->flags & I915_EXEC_IS_PINNED)
1375 flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001376
Zhao Yakuib1a93302014-04-17 10:37:36 +08001377 if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
Daniel Vetterff240192012-01-31 21:08:14 +01001378 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001379 (int)(args->flags & I915_EXEC_RING_MASK));
1380 return -EINVAL;
1381 }
Ben Widawskyca01b122013-12-06 14:11:00 -08001382
Zhipeng Gong8d360df2015-01-13 08:48:24 +08001383 if (((args->flags & I915_EXEC_RING_MASK) != I915_EXEC_BSD) &&
1384 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1385 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1386 "bsd dispatch flags: %d\n", (int)(args->flags));
1387 return -EINVAL;
1388 }
1389
Ben Widawskyca01b122013-12-06 14:11:00 -08001390 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1391 ring = &dev_priv->ring[RCS];
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001392 else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
1393 if (HAS_BSD2(dev)) {
1394 int ring_id;
Zhipeng Gong8d360df2015-01-13 08:48:24 +08001395
1396 switch (args->flags & I915_EXEC_BSD_MASK) {
1397 case I915_EXEC_BSD_DEFAULT:
1398 ring_id = gen8_dispatch_bsd_ring(dev, file);
1399 ring = &dev_priv->ring[ring_id];
1400 break;
1401 case I915_EXEC_BSD_RING1:
1402 ring = &dev_priv->ring[VCS];
1403 break;
1404 case I915_EXEC_BSD_RING2:
1405 ring = &dev_priv->ring[VCS2];
1406 break;
1407 default:
1408 DRM_DEBUG("execbuf with unknown bsd ring: %d\n",
1409 (int)(args->flags & I915_EXEC_BSD_MASK));
1410 return -EINVAL;
1411 }
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001412 } else
1413 ring = &dev_priv->ring[VCS];
1414 } else
Ben Widawskyca01b122013-12-06 14:11:00 -08001415 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1416
Chris Wilsona15817c2012-05-11 14:29:31 +01001417 if (!intel_ring_initialized(ring)) {
1418 DRM_DEBUG("execbuf with invalid ring: %d\n",
1419 (int)(args->flags & I915_EXEC_RING_MASK));
1420 return -EINVAL;
1421 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001422
1423 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001424 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001425 return -EINVAL;
1426 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001427
Paulo Zanonif65c9162013-11-27 18:20:34 -02001428 intel_runtime_pm_get(dev_priv);
1429
Chris Wilson54cf91d2010-11-25 18:00:26 +00001430 ret = i915_mutex_lock_interruptible(dev);
1431 if (ret)
1432 goto pre_mutex_err;
1433
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001434 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001435 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001436 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001437 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001438 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001439 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001440
1441 i915_gem_context_reference(ctx);
1442
Daniel Vetterae6c4802014-08-06 15:04:53 +02001443 if (ctx->ppgtt)
1444 vm = &ctx->ppgtt->base;
1445 else
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001446 vm = &dev_priv->gtt.base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001447
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001448 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001449 if (eb == NULL) {
Ben Widawsky935f38d2014-04-04 22:41:07 -07001450 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001451 mutex_unlock(&dev->struct_mutex);
1452 ret = -ENOMEM;
1453 goto pre_mutex_err;
1454 }
1455
Chris Wilson54cf91d2010-11-25 18:00:26 +00001456 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001457 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001458 if (ret)
1459 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001460
Chris Wilson6fe4f142011-01-10 17:35:37 +00001461 /* take note of the batch buffer before we might reorder the lists */
Chris Wilsond23db882014-05-23 08:48:08 +02001462 batch_obj = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001463
Chris Wilson54cf91d2010-11-25 18:00:26 +00001464 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001465 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
Ben Widawsky27173f12013-08-14 11:38:36 +02001466 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001467 if (ret)
1468 goto err;
1469
1470 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001471 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001472 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001473 if (ret) {
1474 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001475 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
Ben Widawsky27173f12013-08-14 11:38:36 +02001476 eb, exec);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001477 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1478 }
1479 if (ret)
1480 goto err;
1481 }
1482
1483 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001484 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001485 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001486 ret = -EINVAL;
1487 goto err;
1488 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001489
Brad Volkin351e3db2014-02-18 10:15:46 -08001490 if (i915_needs_cmd_parser(ring)) {
Brad Volkin71745372014-12-11 12:13:12 -08001491 batch_obj = i915_gem_execbuffer_parse(ring,
1492 &shadow_exec_entry,
1493 eb,
1494 batch_obj,
1495 args->batch_start_offset,
1496 args->batch_len,
1497 file->is_master,
1498 &flags);
1499 if (IS_ERR(batch_obj)) {
1500 ret = PTR_ERR(batch_obj);
Brad Volkin78a42372014-12-11 12:13:09 -08001501 goto err;
1502 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001503 }
1504
Brad Volkin78a42372014-12-11 12:13:09 -08001505 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1506
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001507 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1508 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001509 * hsw should have this fixed, but bdw mucks it up again. */
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001510 if (flags & I915_DISPATCH_SECURE) {
1511 /*
1512 * So on first glance it looks freaky that we pin the batch here
1513 * outside of the reservation loop. But:
1514 * - The batch is already pinned into the relevant ppgtt, so we
1515 * already have the backing storage fully allocated.
1516 * - No other BO uses the global gtt (well contexts, but meh),
1517 * so we don't really have issues with mutliple objects not
1518 * fitting due to fragmentation.
1519 * So this is actually safe.
1520 */
1521 ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
1522 if (ret)
1523 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001524
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001525 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001526 } else
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001527 exec_start += i915_gem_obj_offset(batch_obj, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001528
Oscar Mateoa83014d2014-07-24 17:04:21 +01001529 ret = dev_priv->gt.do_execbuf(dev, file, ring, ctx, args,
1530 &eb->vmas, batch_obj, exec_start, flags);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001531
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001532 /*
1533 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1534 * batch vma for correctness. For less ugly and less fragility this
1535 * needs to be adjusted to also track the ggtt batch vma properly as
1536 * active.
1537 */
1538 if (flags & I915_DISPATCH_SECURE)
1539 i915_gem_object_ggtt_unpin(batch_obj);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001540err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001541 /* the request owns the ref now */
1542 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001543 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001544
1545 mutex_unlock(&dev->struct_mutex);
1546
1547pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001548 /* intel_gpu_busy should also get a ref, so it will free when the device
1549 * is really idle. */
1550 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001551 return ret;
1552}
1553
1554/*
1555 * Legacy execbuffer just creates an exec2 list from the original exec object
1556 * list array and passes it to the real function.
1557 */
1558int
1559i915_gem_execbuffer(struct drm_device *dev, void *data,
1560 struct drm_file *file)
1561{
1562 struct drm_i915_gem_execbuffer *args = data;
1563 struct drm_i915_gem_execbuffer2 exec2;
1564 struct drm_i915_gem_exec_object *exec_list = NULL;
1565 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1566 int ret, i;
1567
Chris Wilson54cf91d2010-11-25 18:00:26 +00001568 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001569 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001570 return -EINVAL;
1571 }
1572
1573 /* Copy in the exec list from userland */
1574 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1575 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1576 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001577 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001578 args->buffer_count);
1579 drm_free_large(exec_list);
1580 drm_free_large(exec2_list);
1581 return -ENOMEM;
1582 }
1583 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001584 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001585 sizeof(*exec_list) * args->buffer_count);
1586 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001587 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001588 args->buffer_count, ret);
1589 drm_free_large(exec_list);
1590 drm_free_large(exec2_list);
1591 return -EFAULT;
1592 }
1593
1594 for (i = 0; i < args->buffer_count; i++) {
1595 exec2_list[i].handle = exec_list[i].handle;
1596 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1597 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1598 exec2_list[i].alignment = exec_list[i].alignment;
1599 exec2_list[i].offset = exec_list[i].offset;
1600 if (INTEL_INFO(dev)->gen < 4)
1601 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1602 else
1603 exec2_list[i].flags = 0;
1604 }
1605
1606 exec2.buffers_ptr = args->buffers_ptr;
1607 exec2.buffer_count = args->buffer_count;
1608 exec2.batch_start_offset = args->batch_start_offset;
1609 exec2.batch_len = args->batch_len;
1610 exec2.DR1 = args->DR1;
1611 exec2.DR4 = args->DR4;
1612 exec2.num_cliprects = args->num_cliprects;
1613 exec2.cliprects_ptr = args->cliprects_ptr;
1614 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001615 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001616
Ben Widawsky41bde552013-12-06 14:11:21 -08001617 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001618 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001619 struct drm_i915_gem_exec_object __user *user_exec_list =
1620 to_user_ptr(args->buffers_ptr);
1621
Chris Wilson54cf91d2010-11-25 18:00:26 +00001622 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001623 for (i = 0; i < args->buffer_count; i++) {
1624 ret = __copy_to_user(&user_exec_list[i].offset,
1625 &exec2_list[i].offset,
1626 sizeof(user_exec_list[i].offset));
1627 if (ret) {
1628 ret = -EFAULT;
1629 DRM_DEBUG("failed to copy %d exec entries "
1630 "back to user (%d)\n",
1631 args->buffer_count, ret);
1632 break;
1633 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001634 }
1635 }
1636
1637 drm_free_large(exec_list);
1638 drm_free_large(exec2_list);
1639 return ret;
1640}
1641
1642int
1643i915_gem_execbuffer2(struct drm_device *dev, void *data,
1644 struct drm_file *file)
1645{
1646 struct drm_i915_gem_execbuffer2 *args = data;
1647 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1648 int ret;
1649
Xi Wanged8cd3b2012-04-23 04:06:41 -04001650 if (args->buffer_count < 1 ||
1651 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001652 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001653 return -EINVAL;
1654 }
1655
Daniel Vetter9cb34662014-04-24 08:09:11 +02001656 if (args->rsvd2 != 0) {
1657 DRM_DEBUG("dirty rvsd2 field\n");
1658 return -EINVAL;
1659 }
1660
Chris Wilson8408c282011-02-21 12:54:48 +00001661 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001662 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001663 if (exec2_list == NULL)
1664 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1665 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001666 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001667 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001668 args->buffer_count);
1669 return -ENOMEM;
1670 }
1671 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001672 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001673 sizeof(*exec2_list) * args->buffer_count);
1674 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001675 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001676 args->buffer_count, ret);
1677 drm_free_large(exec2_list);
1678 return -EFAULT;
1679 }
1680
Ben Widawsky41bde552013-12-06 14:11:21 -08001681 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001682 if (!ret) {
1683 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001684 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001685 to_user_ptr(args->buffers_ptr);
1686 int i;
1687
1688 for (i = 0; i < args->buffer_count; i++) {
1689 ret = __copy_to_user(&user_exec_list[i].offset,
1690 &exec2_list[i].offset,
1691 sizeof(user_exec_list[i].offset));
1692 if (ret) {
1693 ret = -EFAULT;
1694 DRM_DEBUG("failed to copy %d exec entries "
1695 "back to user\n",
1696 args->buffer_count);
1697 break;
1698 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001699 }
1700 }
1701
1702 drm_free_large(exec2_list);
1703 return ret;
1704}