blob: 48ec4846e6f29b064a8b47bae8f3e1997699d3cd [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>
David Hildenbrand32d82062015-05-11 17:52:12 +020035#include <linux/uaccess.h>
Chris Wilson54cf91d2010-11-25 18:00:26 +000036
Chris Wilsona415d352013-11-26 11:23:15 +000037#define __EXEC_OBJECT_HAS_PIN (1<<31)
38#define __EXEC_OBJECT_HAS_FENCE (1<<30)
Chris Wilsone6a84462014-08-11 12:00:12 +020039#define __EXEC_OBJECT_NEEDS_MAP (1<<29)
Chris Wilsond23db882014-05-23 08:48:08 +020040#define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
41
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
Chris Wilsonde4e7832015-04-07 16:20:35 +0100227 entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
Chris Wilsona415d352013-11-26 11:23:15 +0000228}
229
230static void eb_destroy(struct eb_vmas *eb)
231{
Ben Widawsky27173f12013-08-14 11:38:36 +0200232 while (!list_empty(&eb->vmas)) {
233 struct i915_vma *vma;
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000234
Ben Widawsky27173f12013-08-14 11:38:36 +0200235 vma = list_first_entry(&eb->vmas,
236 struct i915_vma,
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000237 exec_list);
Ben Widawsky27173f12013-08-14 11:38:36 +0200238 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000239 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200240 drm_gem_object_unreference(&vma->obj->base);
Chris Wilsonbcffc3f2013-01-08 10:53:15 +0000241 }
Chris Wilson67731b82010-12-08 10:38:14 +0000242 kfree(eb);
243}
244
Chris Wilsondabdfe02012-03-26 10:10:27 +0200245static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
246{
Chris Wilson2cc86b82013-08-26 19:51:00 -0300247 return (HAS_LLC(obj->base.dev) ||
248 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
Chris Wilsondabdfe02012-03-26 10:10:27 +0200249 obj->cache_level != I915_CACHE_NONE);
250}
251
Chris Wilson54cf91d2010-11-25 18:00:26 +0000252static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100253relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700254 struct drm_i915_gem_relocation_entry *reloc,
255 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100256{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700257 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100258 uint32_t page_offset = offset_in_page(reloc->offset);
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700259 uint64_t delta = reloc->delta + target_offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100260 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800261 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100262
Chris Wilson2cc86b82013-08-26 19:51:00 -0300263 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100264 if (ret)
265 return ret;
266
267 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
268 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700269 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700270
271 if (INTEL_INFO(dev)->gen >= 8) {
272 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
273
274 if (page_offset == 0) {
275 kunmap_atomic(vaddr);
276 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
277 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
278 }
279
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700280 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700281 }
282
Rafael Barbalho5032d872013-08-21 17:10:51 +0100283 kunmap_atomic(vaddr);
284
285 return 0;
286}
287
288static int
289relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700290 struct drm_i915_gem_relocation_entry *reloc,
291 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100292{
293 struct drm_device *dev = obj->base.dev;
294 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700295 uint64_t delta = reloc->delta + target_offset;
Chris Wilson906843c2014-08-10 06:29:11 +0100296 uint64_t offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100297 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800298 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100299
300 ret = i915_gem_object_set_to_gtt_domain(obj, true);
301 if (ret)
302 return ret;
303
304 ret = i915_gem_object_put_fence(obj);
305 if (ret)
306 return ret;
307
308 /* Map the page containing the relocation we're going to perform. */
Chris Wilson906843c2014-08-10 06:29:11 +0100309 offset = i915_gem_obj_ggtt_offset(obj);
310 offset += reloc->offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100311 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100312 offset & PAGE_MASK);
313 iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700314
315 if (INTEL_INFO(dev)->gen >= 8) {
Chris Wilson906843c2014-08-10 06:29:11 +0100316 offset += sizeof(uint32_t);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700317
Chris Wilson906843c2014-08-10 06:29:11 +0100318 if (offset_in_page(offset) == 0) {
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700319 io_mapping_unmap_atomic(reloc_page);
Chris Wilson906843c2014-08-10 06:29:11 +0100320 reloc_page =
321 io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
322 offset);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700323 }
324
Chris Wilson906843c2014-08-10 06:29:11 +0100325 iowrite32(upper_32_bits(delta),
326 reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700327 }
328
Rafael Barbalho5032d872013-08-21 17:10:51 +0100329 io_mapping_unmap_atomic(reloc_page);
330
331 return 0;
332}
333
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000334static void
335clflush_write32(void *addr, uint32_t value)
336{
337 /* This is not a fast path, so KISS. */
338 drm_clflush_virt_range(addr, sizeof(uint32_t));
339 *(uint32_t *)addr = value;
340 drm_clflush_virt_range(addr, sizeof(uint32_t));
341}
342
343static int
344relocate_entry_clflush(struct drm_i915_gem_object *obj,
345 struct drm_i915_gem_relocation_entry *reloc,
346 uint64_t target_offset)
347{
348 struct drm_device *dev = obj->base.dev;
349 uint32_t page_offset = offset_in_page(reloc->offset);
350 uint64_t delta = (int)reloc->delta + target_offset;
351 char *vaddr;
352 int ret;
353
354 ret = i915_gem_object_set_to_gtt_domain(obj, true);
355 if (ret)
356 return ret;
357
358 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
359 reloc->offset >> PAGE_SHIFT));
360 clflush_write32(vaddr + page_offset, lower_32_bits(delta));
361
362 if (INTEL_INFO(dev)->gen >= 8) {
363 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
364
365 if (page_offset == 0) {
366 kunmap_atomic(vaddr);
367 vaddr = kmap_atomic(i915_gem_object_get_page(obj,
368 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
369 }
370
371 clflush_write32(vaddr + page_offset, upper_32_bits(delta));
372 }
373
374 kunmap_atomic(vaddr);
375
376 return 0;
377}
378
Rafael Barbalho5032d872013-08-21 17:10:51 +0100379static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000380i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200381 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800382 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000383{
384 struct drm_device *dev = obj->base.dev;
385 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100386 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200387 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700388 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800389 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000390
Chris Wilson67731b82010-12-08 10:38:14 +0000391 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200392 target_vma = eb_get_vma(eb, reloc->target_handle);
393 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000394 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200395 target_i915_obj = target_vma->obj;
396 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000397
Ben Widawsky5ce09722013-11-25 09:54:40 -0800398 target_offset = target_vma->node.start;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000399
Eric Anholte844b992012-07-31 15:35:01 -0700400 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
401 * pipe_control writes because the gpu doesn't properly redirect them
402 * through the ppgtt for non_secure batchbuffers. */
403 if (unlikely(IS_GEN6(dev) &&
Daniel Vetter08755462015-04-20 09:04:05 -0700404 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000405 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
Daniel Vetter08755462015-04-20 09:04:05 -0700406 PIN_GLOBAL);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000407 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
408 return ret;
409 }
Eric Anholte844b992012-07-31 15:35:01 -0700410
Chris Wilson54cf91d2010-11-25 18:00:26 +0000411 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000412 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100413 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000414 "obj %p target %d offset %d "
415 "read %08x write %08x",
416 obj, reloc->target_handle,
417 (int) reloc->offset,
418 reloc->read_domains,
419 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800420 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000421 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100422 if (unlikely((reloc->write_domain | reloc->read_domains)
423 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100424 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000425 "obj %p target %d offset %d "
426 "read %08x write %08x",
427 obj, reloc->target_handle,
428 (int) reloc->offset,
429 reloc->read_domains,
430 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800431 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000432 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000433
434 target_obj->pending_read_domains |= reloc->read_domains;
435 target_obj->pending_write_domain |= reloc->write_domain;
436
437 /* If the relocation already has the right value in it, no
438 * more work needs to be done.
439 */
440 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000441 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000442
443 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700444 if (unlikely(reloc->offset >
445 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100446 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000447 "obj %p target %d offset %d size %d.\n",
448 obj, reloc->target_handle,
449 (int) reloc->offset,
450 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800451 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000452 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000453 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100454 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000455 "obj %p target %d offset %d.\n",
456 obj, reloc->target_handle,
457 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800458 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000459 }
460
Chris Wilsondabdfe02012-03-26 10:10:27 +0200461 /* We can't wait for rendering with pagefaults disabled */
David Hildenbrand32d82062015-05-11 17:52:12 +0200462 if (obj->active && pagefault_disabled())
Chris Wilsondabdfe02012-03-26 10:10:27 +0200463 return -EFAULT;
464
Rafael Barbalho5032d872013-08-21 17:10:51 +0100465 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700466 ret = relocate_entry_cpu(obj, reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000467 else if (obj->map_and_fenceable)
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700468 ret = relocate_entry_gtt(obj, reloc, target_offset);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000469 else if (cpu_has_clflush)
470 ret = relocate_entry_clflush(obj, reloc, target_offset);
471 else {
472 WARN_ONCE(1, "Impossible case in relocation handling\n");
473 ret = -ENODEV;
474 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000475
Daniel Vetterd4d36012013-09-02 20:56:23 +0200476 if (ret)
477 return ret;
478
Chris Wilson54cf91d2010-11-25 18:00:26 +0000479 /* and update the user's relocation entry */
480 reloc->presumed_offset = target_offset;
481
Chris Wilson67731b82010-12-08 10:38:14 +0000482 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000483}
484
485static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200486i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
487 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000488{
Chris Wilson1d83f442012-03-24 20:12:53 +0000489#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
490 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000491 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200492 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000493 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000494
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200495 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000496
Chris Wilson1d83f442012-03-24 20:12:53 +0000497 remain = entry->relocation_count;
498 while (remain) {
499 struct drm_i915_gem_relocation_entry *r = stack_reloc;
500 int count = remain;
501 if (count > ARRAY_SIZE(stack_reloc))
502 count = ARRAY_SIZE(stack_reloc);
503 remain -= count;
504
505 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000506 return -EFAULT;
507
Chris Wilson1d83f442012-03-24 20:12:53 +0000508 do {
509 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000510
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800511 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000512 if (ret)
513 return ret;
514
515 if (r->presumed_offset != offset &&
516 __copy_to_user_inatomic(&user_relocs->presumed_offset,
517 &r->presumed_offset,
518 sizeof(r->presumed_offset))) {
519 return -EFAULT;
520 }
521
522 user_relocs++;
523 r++;
524 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000525 }
526
527 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000528#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000529}
530
531static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200532i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
533 struct eb_vmas *eb,
534 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000535{
Ben Widawsky27173f12013-08-14 11:38:36 +0200536 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000537 int i, ret;
538
539 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800540 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000541 if (ret)
542 return ret;
543 }
544
545 return 0;
546}
547
548static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800549i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000550{
Ben Widawsky27173f12013-08-14 11:38:36 +0200551 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000552 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000553
Chris Wilsond4aeee72011-03-14 15:11:24 +0000554 /* This is the fast path and we cannot handle a pagefault whilst
555 * holding the struct mutex lest the user pass in the relocations
556 * contained within a mmaped bo. For in such a case we, the page
557 * fault handler would call i915_gem_fault() and we would try to
558 * acquire the struct mutex again. Obviously this is bad and so
559 * lockdep complains vehemently.
560 */
561 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200562 list_for_each_entry(vma, &eb->vmas, exec_list) {
563 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000564 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000565 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000566 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000567 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000568
Chris Wilsond4aeee72011-03-14 15:11:24 +0000569 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000570}
571
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000572static bool only_mappable_for_reloc(unsigned int flags)
573{
574 return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
575 __EXEC_OBJECT_NEEDS_MAP;
576}
577
Chris Wilson1690e1e2011-12-14 13:57:08 +0100578static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200579i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100580 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200581 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100582{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800583 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200584 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200585 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100586 int ret;
587
Daniel Vetter08755462015-04-20 09:04:05 -0700588 flags = PIN_USER;
Daniel Vetter0229da32015-04-14 19:01:54 +0200589 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
590 flags |= PIN_GLOBAL;
591
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000592 if (!drm_mm_node_allocated(&vma->node)) {
Michel Thierry101b5062015-10-01 13:33:57 +0100593 /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
594 * limit address to the first 4GBs for unflagged objects.
595 */
596 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
597 flags |= PIN_ZONE_4G;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000598 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
599 flags |= PIN_GLOBAL | PIN_MAPPABLE;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000600 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
601 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson506a8e82015-12-08 11:55:07 +0000602 if (entry->flags & EXEC_OBJECT_PINNED)
603 flags |= entry->offset | PIN_OFFSET_FIXED;
Michel Thierry101b5062015-10-01 13:33:57 +0100604 if ((flags & PIN_MAPPABLE) == 0)
605 flags |= PIN_HIGH;
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000606 }
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100607
608 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000609 if ((ret == -ENOSPC || ret == -E2BIG) &&
610 only_mappable_for_reloc(entry->flags))
611 ret = i915_gem_object_pin(obj, vma->vm,
612 entry->alignment,
Daniel Vetter0229da32015-04-14 19:01:54 +0200613 flags & ~PIN_MAPPABLE);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100614 if (ret)
615 return ret;
616
Chris Wilson7788a762012-08-24 19:18:18 +0100617 entry->flags |= __EXEC_OBJECT_HAS_PIN;
618
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100619 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
620 ret = i915_gem_object_get_fence(obj);
621 if (ret)
622 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100623
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100624 if (i915_gem_object_pin_fence(obj))
625 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100626 }
627
Ben Widawsky27173f12013-08-14 11:38:36 +0200628 if (entry->offset != vma->node.start) {
629 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100630 *need_reloc = true;
631 }
632
633 if (entry->flags & EXEC_OBJECT_WRITE) {
634 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
635 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
636 }
637
Chris Wilson1690e1e2011-12-14 13:57:08 +0100638 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100639}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100640
Chris Wilsond23db882014-05-23 08:48:08 +0200641static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200642need_reloc_mappable(struct i915_vma *vma)
643{
644 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
645
646 if (entry->relocation_count == 0)
647 return false;
648
649 if (!i915_is_ggtt(vma->vm))
650 return false;
651
652 /* See also use_cpu_reloc() */
653 if (HAS_LLC(vma->obj->base.dev))
654 return false;
655
656 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
657 return false;
658
659 return true;
660}
661
662static bool
663eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200664{
665 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
666 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond23db882014-05-23 08:48:08 +0200667
Chris Wilsone6a84462014-08-11 12:00:12 +0200668 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
Chris Wilsond23db882014-05-23 08:48:08 +0200669 !i915_is_ggtt(vma->vm));
670
671 if (entry->alignment &&
672 vma->node.start & (entry->alignment - 1))
673 return true;
674
Chris Wilson506a8e82015-12-08 11:55:07 +0000675 if (entry->flags & EXEC_OBJECT_PINNED &&
676 vma->node.start != entry->offset)
677 return true;
678
Chris Wilsond23db882014-05-23 08:48:08 +0200679 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
680 vma->node.start < BATCH_OFFSET_BIAS)
681 return true;
682
Chris Wilsonedf4427b2015-01-14 11:20:56 +0000683 /* avoid costly ping-pong once a batch bo ended up non-mappable */
684 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
685 return !only_mappable_for_reloc(entry->flags);
686
Michel Thierry101b5062015-10-01 13:33:57 +0100687 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
688 (vma->node.start + vma->node.size - 1) >> 32)
689 return true;
690
Chris Wilsond23db882014-05-23 08:48:08 +0200691 return false;
692}
693
Chris Wilson54cf91d2010-11-25 18:00:26 +0000694static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100695i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200696 struct list_head *vmas,
David Weinehallb1b38272015-05-20 17:00:13 +0300697 struct intel_context *ctx,
Daniel Vettered5982e2013-01-17 22:23:36 +0100698 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000699{
Chris Wilson432e58e2010-11-25 19:32:06 +0000700 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200701 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700702 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200703 struct list_head ordered_vmas;
Chris Wilson506a8e82015-12-08 11:55:07 +0000704 struct list_head pinned_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100705 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
706 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000707
Chris Wilson227f7822014-05-15 10:41:42 +0100708 i915_gem_retire_requests_ring(ring);
709
Ben Widawsky68c8c172013-09-11 14:57:50 -0700710 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
711
Ben Widawsky27173f12013-08-14 11:38:36 +0200712 INIT_LIST_HEAD(&ordered_vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000713 INIT_LIST_HEAD(&pinned_vmas);
Ben Widawsky27173f12013-08-14 11:38:36 +0200714 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000715 struct drm_i915_gem_exec_object2 *entry;
716 bool need_fence, need_mappable;
717
Ben Widawsky27173f12013-08-14 11:38:36 +0200718 vma = list_first_entry(vmas, struct i915_vma, exec_list);
719 obj = vma->obj;
720 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000721
David Weinehallb1b38272015-05-20 17:00:13 +0300722 if (ctx->flags & CONTEXT_NO_ZEROMAP)
723 entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
724
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100725 if (!has_fenced_gpu_access)
726 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000727 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000728 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
729 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200730 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000731
Chris Wilson506a8e82015-12-08 11:55:07 +0000732 if (entry->flags & EXEC_OBJECT_PINNED)
733 list_move_tail(&vma->exec_list, &pinned_vmas);
734 else if (need_mappable) {
Chris Wilsone6a84462014-08-11 12:00:12 +0200735 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200736 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200737 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200738 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000739
Daniel Vettered5982e2013-01-17 22:23:36 +0100740 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000741 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000742 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200743 list_splice(&ordered_vmas, vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000744 list_splice(&pinned_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000745
746 /* Attempt to pin all of the buffers into the GTT.
747 * This is done in 3 phases:
748 *
749 * 1a. Unbind all objects that do not match the GTT constraints for
750 * the execbuffer (fenceable, mappable, alignment etc).
751 * 1b. Increment pin count for already bound objects.
752 * 2. Bind new objects.
753 * 3. Decrement pin count.
754 *
Chris Wilson7788a762012-08-24 19:18:18 +0100755 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000756 * room for the earlier objects *unless* we need to defragment.
757 */
758 retry = 0;
759 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100760 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000761
762 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200763 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200764 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000765 continue;
766
Chris Wilsone6a84462014-08-11 12:00:12 +0200767 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200768 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000769 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200770 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000771 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000772 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000773 }
774
775 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200776 list_for_each_entry(vma, vmas, exec_list) {
777 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100778 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000779
Ben Widawsky27173f12013-08-14 11:38:36 +0200780 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100781 if (ret)
782 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000783 }
784
Chris Wilsona415d352013-11-26 11:23:15 +0000785err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200786 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000787 return ret;
788
Chris Wilsona415d352013-11-26 11:23:15 +0000789 /* Decrement pin count for bound objects */
790 list_for_each_entry(vma, vmas, exec_list)
791 i915_gem_execbuffer_unreserve_vma(vma);
792
Ben Widawsky68c8c172013-09-11 14:57:50 -0700793 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000794 if (ret)
795 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000796 } while (1);
797}
798
799static int
800i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100801 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000802 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100803 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200804 struct eb_vmas *eb,
David Weinehallb1b38272015-05-20 17:00:13 +0300805 struct drm_i915_gem_exec_object2 *exec,
806 struct intel_context *ctx)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000807{
808 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200809 struct i915_address_space *vm;
810 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100811 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000812 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000813 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200814 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000815
Ben Widawsky27173f12013-08-14 11:38:36 +0200816 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
817
Chris Wilson67731b82010-12-08 10:38:14 +0000818 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200819 while (!list_empty(&eb->vmas)) {
820 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
821 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000822 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200823 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000824 }
825
Chris Wilson54cf91d2010-11-25 18:00:26 +0000826 mutex_unlock(&dev->struct_mutex);
827
828 total = 0;
829 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000830 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000831
Chris Wilsondd6864a2011-01-12 23:49:13 +0000832 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000833 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000834 if (reloc == NULL || reloc_offset == NULL) {
835 drm_free_large(reloc);
836 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000837 mutex_lock(&dev->struct_mutex);
838 return -ENOMEM;
839 }
840
841 total = 0;
842 for (i = 0; i < count; i++) {
843 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000844 u64 invalid_offset = (u64)-1;
845 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000846
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200847 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000848
849 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000850 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000851 ret = -EFAULT;
852 mutex_lock(&dev->struct_mutex);
853 goto err;
854 }
855
Chris Wilson262b6d32013-01-15 16:17:54 +0000856 /* As we do not update the known relocation offsets after
857 * relocating (due to the complexities in lock handling),
858 * we need to mark them as invalid now so that we force the
859 * relocation processing next time. Just in case the target
860 * object is evicted and then rebound into its old
861 * presumed_offset before the next execbuffer - if that
862 * happened we would make the mistake of assuming that the
863 * relocations were valid.
864 */
865 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100866 if (__copy_to_user(&user_relocs[j].presumed_offset,
867 &invalid_offset,
868 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000869 ret = -EFAULT;
870 mutex_lock(&dev->struct_mutex);
871 goto err;
872 }
873 }
874
Chris Wilsondd6864a2011-01-12 23:49:13 +0000875 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000876 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000877 }
878
879 ret = i915_mutex_lock_interruptible(dev);
880 if (ret) {
881 mutex_lock(&dev->struct_mutex);
882 goto err;
883 }
884
Chris Wilson67731b82010-12-08 10:38:14 +0000885 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000886 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200887 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000888 if (ret)
889 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000890
Daniel Vettered5982e2013-01-17 22:23:36 +0100891 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
David Weinehallb1b38272015-05-20 17:00:13 +0300892 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, ctx, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000893 if (ret)
894 goto err;
895
Ben Widawsky27173f12013-08-14 11:38:36 +0200896 list_for_each_entry(vma, &eb->vmas, exec_list) {
897 int offset = vma->exec_entry - exec;
898 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
899 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000900 if (ret)
901 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000902 }
903
904 /* Leave the user relocations as are, this is the painfully slow path,
905 * and we want to avoid the complication of dropping the lock whilst
906 * having buffers reserved in the aperture and so causing spurious
907 * ENOSPC for random operations.
908 */
909
910err:
911 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000912 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000913 return ret;
914}
915
Chris Wilson54cf91d2010-11-25 18:00:26 +0000916static int
John Harrison535fbe82015-05-29 17:43:32 +0100917i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
Ben Widawsky27173f12013-08-14 11:38:36 +0200918 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000919{
John Harrison535fbe82015-05-29 17:43:32 +0100920 const unsigned other_rings = ~intel_ring_flag(req->ring);
Ben Widawsky27173f12013-08-14 11:38:36 +0200921 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200922 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100923 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000924 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000925
Ben Widawsky27173f12013-08-14 11:38:36 +0200926 list_for_each_entry(vma, vmas, exec_list) {
927 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +0100928
929 if (obj->active & other_rings) {
John Harrison91af1272015-06-18 13:14:56 +0100930 ret = i915_gem_object_sync(obj, req->ring, &req);
Chris Wilson03ade512015-04-27 13:41:18 +0100931 if (ret)
932 return ret;
933 }
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200934
935 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100936 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200937
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200938 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000939 }
940
Chris Wilson000433b2013-08-08 14:41:09 +0100941 if (flush_chipset)
John Harrison535fbe82015-05-29 17:43:32 +0100942 i915_gem_chipset_flush(req->ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200943
944 if (flush_domains & I915_GEM_DOMAIN_GTT)
945 wmb();
946
Chris Wilson09cf7c92012-07-13 14:14:08 +0100947 /* Unconditionally invalidate gpu caches and ensure that we do flush
948 * any residual writes from the previous batch.
949 */
John Harrison2f200552015-05-29 17:43:53 +0100950 return intel_ring_invalidate_all_caches(req);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000951}
952
Chris Wilson432e58e2010-11-25 19:32:06 +0000953static bool
954i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000955{
Daniel Vettered5982e2013-01-17 22:23:36 +0100956 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
957 return false;
958
Chris Wilson2f5945b2015-10-06 11:39:55 +0100959 /* Kernel clipping was a DRI1 misfeature */
960 if (exec->num_cliprects || exec->cliprects_ptr)
961 return false;
962
963 if (exec->DR4 == 0xffffffff) {
964 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
965 exec->DR4 = 0;
966 }
967 if (exec->DR1 || exec->DR4)
968 return false;
969
970 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
971 return false;
972
973 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000974}
975
976static int
Chris Wilsonad19f102014-08-10 06:29:08 +0100977validate_exec_list(struct drm_device *dev,
978 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000979 int count)
980{
Daniel Vetterb205ca52013-09-19 14:00:11 +0200981 unsigned relocs_total = 0;
982 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +0100983 unsigned invalid_flags;
984 int i;
985
986 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
987 if (USES_FULL_PPGTT(dev))
988 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000989
990 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200991 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000992 int length; /* limited by fault_in_pages_readable() */
993
Chris Wilsonad19f102014-08-10 06:29:08 +0100994 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +0100995 return -EINVAL;
996
Chris Wilson55a97852015-06-19 13:59:46 +0100997 if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
998 return -EINVAL;
999
Kees Cook3118a4f2013-03-11 17:31:45 -07001000 /* First check for malicious input causing overflow in
1001 * the worst case where we need to allocate the entire
1002 * relocation tree as a single array.
1003 */
1004 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001005 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -07001006 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001007
1008 length = exec[i].relocation_count *
1009 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -07001010 /*
1011 * We must check that the entire relocation array is safe
1012 * to read, but since we may need to update the presumed
1013 * offsets during execution, check for full write access.
1014 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001015 if (!access_ok(VERIFY_WRITE, ptr, length))
1016 return -EFAULT;
1017
Jani Nikulad330a952014-01-21 11:24:25 +02001018 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +08001019 if (fault_in_multipages_readable(ptr, length))
1020 return -EFAULT;
1021 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001022 }
1023
1024 return 0;
1025}
1026
Oscar Mateo273497e2014-05-22 14:13:37 +01001027static struct intel_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001028i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001029 struct intel_engine_cs *ring, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001030{
Oscar Mateo273497e2014-05-22 14:13:37 +01001031 struct intel_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001032 struct i915_ctx_hang_stats *hs;
1033
Oscar Mateo821d66d2014-07-03 16:28:00 +01001034 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001035 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001036
Ben Widawsky41bde552013-12-06 14:11:21 -08001037 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001038 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -08001039 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001040
Ben Widawsky41bde552013-12-06 14:11:21 -08001041 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001042 if (hs->banned) {
1043 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -08001044 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001045 }
1046
Oscar Mateoec3e9962014-07-24 17:04:18 +01001047 if (i915.enable_execlists && !ctx->engine[ring->id].state) {
Nick Hoathe84fe802015-09-11 12:53:46 +01001048 int ret = intel_lr_context_deferred_alloc(ctx, ring);
Oscar Mateoec3e9962014-07-24 17:04:18 +01001049 if (ret) {
1050 DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
1051 return ERR_PTR(ret);
1052 }
1053 }
1054
Ben Widawsky41bde552013-12-06 14:11:21 -08001055 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001056}
1057
Oscar Mateoba8b7cc2014-07-24 17:04:33 +01001058void
Ben Widawsky27173f12013-08-14 11:38:36 +02001059i915_gem_execbuffer_move_to_active(struct list_head *vmas,
John Harrison8a8edb52015-05-29 17:43:33 +01001060 struct drm_i915_gem_request *req)
Chris Wilson432e58e2010-11-25 19:32:06 +00001061{
John Harrison8a8edb52015-05-29 17:43:33 +01001062 struct intel_engine_cs *ring = i915_gem_request_get_ring(req);
Ben Widawsky27173f12013-08-14 11:38:36 +02001063 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +00001064
Ben Widawsky27173f12013-08-14 11:38:36 +02001065 list_for_each_entry(vma, vmas, exec_list) {
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001066 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Ben Widawsky27173f12013-08-14 11:38:36 +02001067 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +01001068 u32 old_read = obj->base.read_domains;
1069 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +00001070
Chris Wilson51bc1402015-08-31 15:10:39 +01001071 obj->dirty = 1; /* be paranoid */
Chris Wilson432e58e2010-11-25 19:32:06 +00001072 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +01001073 if (obj->base.write_domain == 0)
1074 obj->base.pending_read_domains |= obj->base.read_domains;
1075 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +00001076
John Harrisonb2af0372015-05-29 17:43:50 +01001077 i915_vma_move_to_active(vma, req);
Chris Wilson432e58e2010-11-25 19:32:06 +00001078 if (obj->base.write_domain) {
John Harrison97b2a6a2014-11-24 18:49:26 +00001079 i915_gem_request_assign(&obj->last_write_req, req);
Daniel Vetterf99d7062014-06-19 16:01:59 +02001080
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -07001081 intel_fb_obj_invalidate(obj, ORIGIN_CS);
Chris Wilsonc8725f32014-03-17 12:21:55 +00001082
1083 /* update for the implicit flush after a batch */
1084 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson432e58e2010-11-25 19:32:06 +00001085 }
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001086 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
John Harrison97b2a6a2014-11-24 18:49:26 +00001087 i915_gem_request_assign(&obj->last_fenced_req, req);
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001088 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
1089 struct drm_i915_private *dev_priv = to_i915(ring->dev);
1090 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
1091 &dev_priv->mm.fence_list);
1092 }
1093 }
Chris Wilson432e58e2010-11-25 19:32:06 +00001094
Chris Wilsondb53a302011-02-03 11:57:46 +00001095 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +00001096 }
1097}
1098
Oscar Mateoba8b7cc2014-07-24 17:04:33 +01001099void
John Harrisonadeca762015-05-29 17:43:28 +01001100i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params *params)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001101{
Daniel Vettercc889e02012-06-13 20:45:19 +02001102 /* Unconditionally force add_request to emit a full flush. */
John Harrisonadeca762015-05-29 17:43:28 +01001103 params->ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001104
Chris Wilson432e58e2010-11-25 19:32:06 +00001105 /* Add a breadcrumb for the completion of the batch buffer */
John Harrisonfcfa423c2015-05-29 17:44:12 +01001106 __i915_add_request(params->request, params->batch_obj, true);
Chris Wilson432e58e2010-11-25 19:32:06 +00001107}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001108
1109static int
Eric Anholtae662d32012-01-03 09:23:29 -08001110i915_reset_gen7_sol_offsets(struct drm_device *dev,
John Harrison2f200552015-05-29 17:43:53 +01001111 struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001112{
John Harrison2f200552015-05-29 17:43:53 +01001113 struct intel_engine_cs *ring = req->ring;
Jani Nikula50227e12014-03-31 14:27:21 +03001114 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtae662d32012-01-03 09:23:29 -08001115 int ret, i;
1116
Daniel Vetter9d662da2014-04-24 08:09:09 +02001117 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
1118 DRM_DEBUG("sol reset is gen7/rcs only\n");
1119 return -EINVAL;
1120 }
Eric Anholtae662d32012-01-03 09:23:29 -08001121
John Harrison5fb9de12015-05-29 17:44:07 +01001122 ret = intel_ring_begin(req, 4 * 3);
Eric Anholtae662d32012-01-03 09:23:29 -08001123 if (ret)
1124 return ret;
1125
1126 for (i = 0; i < 4; i++) {
1127 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +02001128 intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
Eric Anholtae662d32012-01-03 09:23:29 -08001129 intel_ring_emit(ring, 0);
1130 }
1131
1132 intel_ring_advance(ring);
1133
1134 return 0;
1135}
1136
Brad Volkin71745372014-12-11 12:13:12 -08001137static struct drm_i915_gem_object*
1138i915_gem_execbuffer_parse(struct intel_engine_cs *ring,
1139 struct drm_i915_gem_exec_object2 *shadow_exec_entry,
1140 struct eb_vmas *eb,
1141 struct drm_i915_gem_object *batch_obj,
1142 u32 batch_start_offset,
1143 u32 batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001144 bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001145{
Brad Volkin71745372014-12-11 12:13:12 -08001146 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001147 struct i915_vma *vma;
Brad Volkin71745372014-12-11 12:13:12 -08001148 int ret;
1149
Chris Wilson06fbca72015-04-07 16:20:36 +01001150 shadow_batch_obj = i915_gem_batch_pool_get(&ring->batch_pool,
Chris Wilson17cabf52015-01-14 11:20:57 +00001151 PAGE_ALIGN(batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001152 if (IS_ERR(shadow_batch_obj))
1153 return shadow_batch_obj;
1154
1155 ret = i915_parse_cmds(ring,
1156 batch_obj,
1157 shadow_batch_obj,
1158 batch_start_offset,
1159 batch_len,
1160 is_master);
Chris Wilson17cabf52015-01-14 11:20:57 +00001161 if (ret)
1162 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001163
Chris Wilson17cabf52015-01-14 11:20:57 +00001164 ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
1165 if (ret)
1166 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001167
Chris Wilsonde4e7832015-04-07 16:20:35 +01001168 i915_gem_object_unpin_pages(shadow_batch_obj);
1169
Chris Wilson17cabf52015-01-14 11:20:57 +00001170 memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
Brad Volkin71745372014-12-11 12:13:12 -08001171
Chris Wilson17cabf52015-01-14 11:20:57 +00001172 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1173 vma->exec_entry = shadow_exec_entry;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001174 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
Chris Wilson17cabf52015-01-14 11:20:57 +00001175 drm_gem_object_reference(&shadow_batch_obj->base);
1176 list_add_tail(&vma->exec_list, &eb->vmas);
Brad Volkin71745372014-12-11 12:13:12 -08001177
Chris Wilson17cabf52015-01-14 11:20:57 +00001178 shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
Brad Volkin71745372014-12-11 12:13:12 -08001179
Chris Wilson17cabf52015-01-14 11:20:57 +00001180 return shadow_batch_obj;
1181
1182err:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001183 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001184 if (ret == -EACCES) /* unhandled chained batch */
1185 return batch_obj;
1186 else
1187 return ERR_PTR(ret);
Brad Volkin71745372014-12-11 12:13:12 -08001188}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001189
Oscar Mateoa83014d2014-07-24 17:04:21 +01001190int
John Harrison5f19e2b2015-05-29 17:43:27 +01001191i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
Oscar Mateoa83014d2014-07-24 17:04:21 +01001192 struct drm_i915_gem_execbuffer2 *args,
John Harrison5f19e2b2015-05-29 17:43:27 +01001193 struct list_head *vmas)
Oscar Mateo78382592014-07-03 16:28:05 +01001194{
John Harrison5f19e2b2015-05-29 17:43:27 +01001195 struct drm_device *dev = params->dev;
1196 struct intel_engine_cs *ring = params->ring;
Oscar Mateo78382592014-07-03 16:28:05 +01001197 struct drm_i915_private *dev_priv = dev->dev_private;
John Harrison5f19e2b2015-05-29 17:43:27 +01001198 u64 exec_start, exec_len;
Oscar Mateo78382592014-07-03 16:28:05 +01001199 int instp_mode;
1200 u32 instp_mask;
Chris Wilson2f5945b2015-10-06 11:39:55 +01001201 int ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001202
John Harrison535fbe82015-05-29 17:43:32 +01001203 ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
Oscar Mateo78382592014-07-03 16:28:05 +01001204 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001205 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001206
John Harrisonba01cc92015-05-29 17:43:41 +01001207 ret = i915_switch_context(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001208 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001209 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001210
John Harrison5f19e2b2015-05-29 17:43:27 +01001211 WARN(params->ctx->ppgtt && params->ctx->ppgtt->pd_dirty_rings & (1<<ring->id),
Daniel Vetter92588112015-04-14 17:35:19 +02001212 "%s didn't clear reload\n", ring->name);
Ben Widawsky563222a2015-03-19 12:53:28 +00001213
Oscar Mateo78382592014-07-03 16:28:05 +01001214 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1215 instp_mask = I915_EXEC_CONSTANTS_MASK;
1216 switch (instp_mode) {
1217 case I915_EXEC_CONSTANTS_REL_GENERAL:
1218 case I915_EXEC_CONSTANTS_ABSOLUTE:
1219 case I915_EXEC_CONSTANTS_REL_SURFACE:
1220 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
1221 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001222 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001223 }
1224
1225 if (instp_mode != dev_priv->relative_constants_mode) {
1226 if (INTEL_INFO(dev)->gen < 4) {
1227 DRM_DEBUG("no rel constants on pre-gen4\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001228 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001229 }
1230
1231 if (INTEL_INFO(dev)->gen > 5 &&
1232 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1233 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001234 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001235 }
1236
1237 /* The HW changed the meaning on this bit on gen6 */
1238 if (INTEL_INFO(dev)->gen >= 6)
1239 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1240 }
1241 break;
1242 default:
1243 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001244 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001245 }
1246
1247 if (ring == &dev_priv->ring[RCS] &&
Chris Wilson2f5945b2015-10-06 11:39:55 +01001248 instp_mode != dev_priv->relative_constants_mode) {
John Harrison5fb9de12015-05-29 17:44:07 +01001249 ret = intel_ring_begin(params->request, 4);
Oscar Mateo78382592014-07-03 16:28:05 +01001250 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001251 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001252
1253 intel_ring_emit(ring, MI_NOOP);
1254 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +02001255 intel_ring_emit_reg(ring, INSTPM);
Oscar Mateo78382592014-07-03 16:28:05 +01001256 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1257 intel_ring_advance(ring);
1258
1259 dev_priv->relative_constants_mode = instp_mode;
1260 }
1261
1262 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
John Harrison2f200552015-05-29 17:43:53 +01001263 ret = i915_reset_gen7_sol_offsets(dev, params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001264 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001265 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001266 }
1267
John Harrison5f19e2b2015-05-29 17:43:27 +01001268 exec_len = args->batch_len;
1269 exec_start = params->batch_obj_vm_offset +
1270 params->args_batch_start_offset;
1271
Chris Wilson2f5945b2015-10-06 11:39:55 +01001272 ret = ring->dispatch_execbuffer(params->request,
1273 exec_start, exec_len,
1274 params->dispatch_flags);
1275 if (ret)
1276 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001277
John Harrison95c24162015-05-29 17:43:31 +01001278 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001279
John Harrison8a8edb52015-05-29 17:43:33 +01001280 i915_gem_execbuffer_move_to_active(vmas, params->request);
John Harrisonadeca762015-05-29 17:43:28 +01001281 i915_gem_execbuffer_retire_commands(params);
Oscar Mateo78382592014-07-03 16:28:05 +01001282
Chris Wilson2f5945b2015-10-06 11:39:55 +01001283 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001284}
1285
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001286/**
1287 * Find one BSD ring to dispatch the corresponding BSD command.
1288 * The Ring ID is returned.
1289 */
1290static int gen8_dispatch_bsd_ring(struct drm_device *dev,
1291 struct drm_file *file)
1292{
1293 struct drm_i915_private *dev_priv = dev->dev_private;
1294 struct drm_i915_file_private *file_priv = file->driver_priv;
1295
1296 /* Check whether the file_priv is using one ring */
1297 if (file_priv->bsd_ring)
1298 return file_priv->bsd_ring->id;
1299 else {
1300 /* If no, use the ping-pong mechanism to select one ring */
1301 int ring_id;
1302
1303 mutex_lock(&dev->struct_mutex);
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001304 if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001305 ring_id = VCS;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001306 dev_priv->mm.bsd_ring_dispatch_index = 1;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001307 } else {
1308 ring_id = VCS2;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001309 dev_priv->mm.bsd_ring_dispatch_index = 0;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001310 }
1311 file_priv->bsd_ring = &dev_priv->ring[ring_id];
1312 mutex_unlock(&dev->struct_mutex);
1313 return ring_id;
1314 }
1315}
1316
Chris Wilsond23db882014-05-23 08:48:08 +02001317static struct drm_i915_gem_object *
1318eb_get_batch(struct eb_vmas *eb)
1319{
1320 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
1321
1322 /*
1323 * SNA is doing fancy tricks with compressing batch buffers, which leads
1324 * to negative relocation deltas. Usually that works out ok since the
1325 * relocate address is still positive, except when the batch is placed
1326 * very low in the GTT. Ensure this doesn't happen.
1327 *
1328 * Note that actual hangs have only been observed on gen7, but for
1329 * paranoia do it everywhere.
1330 */
Chris Wilson506a8e82015-12-08 11:55:07 +00001331 if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
1332 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
Chris Wilsond23db882014-05-23 08:48:08 +02001333
1334 return vma->obj;
1335}
1336
Eric Anholtae662d32012-01-03 09:23:29 -08001337static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001338i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1339 struct drm_file *file,
1340 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001341 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001342{
Jani Nikula50227e12014-03-31 14:27:21 +03001343 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +02001344 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001345 struct drm_i915_gem_object *batch_obj;
Brad Volkin78a42372014-12-11 12:13:09 -08001346 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001347 struct intel_engine_cs *ring;
Oscar Mateo273497e2014-05-22 14:13:37 +01001348 struct intel_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001349 struct i915_address_space *vm;
John Harrison5f19e2b2015-05-29 17:43:27 +01001350 struct i915_execbuffer_params params_master; /* XXX: will be removed later */
1351 struct i915_execbuffer_params *params = &params_master;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001352 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
John Harrison8e004ef2015-02-13 11:48:10 +00001353 u32 dispatch_flags;
Oscar Mateo78382592014-07-03 16:28:05 +01001354 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001355 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001356
Daniel Vettered5982e2013-01-17 22:23:36 +01001357 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001358 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001359
Chris Wilsonad19f102014-08-10 06:29:08 +01001360 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001361 if (ret)
1362 return ret;
1363
John Harrison8e004ef2015-02-13 11:48:10 +00001364 dispatch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001365 if (args->flags & I915_EXEC_SECURE) {
1366 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1367 return -EPERM;
1368
John Harrison8e004ef2015-02-13 11:48:10 +00001369 dispatch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001370 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001371 if (args->flags & I915_EXEC_IS_PINNED)
John Harrison8e004ef2015-02-13 11:48:10 +00001372 dispatch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001373
Zhao Yakuib1a93302014-04-17 10:37:36 +08001374 if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
Daniel Vetterff240192012-01-31 21:08:14 +01001375 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001376 (int)(args->flags & I915_EXEC_RING_MASK));
1377 return -EINVAL;
1378 }
Ben Widawskyca01b122013-12-06 14:11:00 -08001379
Zhipeng Gong8d360df2015-01-13 08:48:24 +08001380 if (((args->flags & I915_EXEC_RING_MASK) != I915_EXEC_BSD) &&
1381 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1382 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1383 "bsd dispatch flags: %d\n", (int)(args->flags));
1384 return -EINVAL;
1385 }
1386
Ben Widawskyca01b122013-12-06 14:11:00 -08001387 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1388 ring = &dev_priv->ring[RCS];
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001389 else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
1390 if (HAS_BSD2(dev)) {
1391 int ring_id;
Zhipeng Gong8d360df2015-01-13 08:48:24 +08001392
1393 switch (args->flags & I915_EXEC_BSD_MASK) {
1394 case I915_EXEC_BSD_DEFAULT:
1395 ring_id = gen8_dispatch_bsd_ring(dev, file);
1396 ring = &dev_priv->ring[ring_id];
1397 break;
1398 case I915_EXEC_BSD_RING1:
1399 ring = &dev_priv->ring[VCS];
1400 break;
1401 case I915_EXEC_BSD_RING2:
1402 ring = &dev_priv->ring[VCS2];
1403 break;
1404 default:
1405 DRM_DEBUG("execbuf with unknown bsd ring: %d\n",
1406 (int)(args->flags & I915_EXEC_BSD_MASK));
1407 return -EINVAL;
1408 }
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001409 } else
1410 ring = &dev_priv->ring[VCS];
1411 } else
Ben Widawskyca01b122013-12-06 14:11:00 -08001412 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1413
Chris Wilsona15817c2012-05-11 14:29:31 +01001414 if (!intel_ring_initialized(ring)) {
1415 DRM_DEBUG("execbuf with invalid ring: %d\n",
1416 (int)(args->flags & I915_EXEC_RING_MASK));
1417 return -EINVAL;
1418 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001419
1420 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001421 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001422 return -EINVAL;
1423 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001424
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001425 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
1426 if (!HAS_RESOURCE_STREAMER(dev)) {
1427 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
1428 return -EINVAL;
1429 }
1430 if (ring->id != RCS) {
1431 DRM_DEBUG("RS is not available on %s\n",
1432 ring->name);
1433 return -EINVAL;
1434 }
1435
1436 dispatch_flags |= I915_DISPATCH_RS;
1437 }
1438
Paulo Zanonif65c9162013-11-27 18:20:34 -02001439 intel_runtime_pm_get(dev_priv);
1440
Chris Wilson54cf91d2010-11-25 18:00:26 +00001441 ret = i915_mutex_lock_interruptible(dev);
1442 if (ret)
1443 goto pre_mutex_err;
1444
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001445 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001446 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001447 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001448 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001449 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001450 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001451
1452 i915_gem_context_reference(ctx);
1453
Daniel Vetterae6c4802014-08-06 15:04:53 +02001454 if (ctx->ppgtt)
1455 vm = &ctx->ppgtt->base;
1456 else
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001457 vm = &dev_priv->gtt.base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001458
John Harrison5f19e2b2015-05-29 17:43:27 +01001459 memset(&params_master, 0x00, sizeof(params_master));
1460
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001461 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001462 if (eb == NULL) {
Ben Widawsky935f38d2014-04-04 22:41:07 -07001463 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001464 mutex_unlock(&dev->struct_mutex);
1465 ret = -ENOMEM;
1466 goto pre_mutex_err;
1467 }
1468
Chris Wilson54cf91d2010-11-25 18:00:26 +00001469 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001470 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001471 if (ret)
1472 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001473
Chris Wilson6fe4f142011-01-10 17:35:37 +00001474 /* take note of the batch buffer before we might reorder the lists */
Chris Wilsond23db882014-05-23 08:48:08 +02001475 batch_obj = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001476
Chris Wilson54cf91d2010-11-25 18:00:26 +00001477 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001478 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
David Weinehallb1b38272015-05-20 17:00:13 +03001479 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, ctx, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001480 if (ret)
1481 goto err;
1482
1483 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001484 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001485 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001486 if (ret) {
1487 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001488 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
David Weinehallb1b38272015-05-20 17:00:13 +03001489 eb, exec, ctx);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001490 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1491 }
1492 if (ret)
1493 goto err;
1494 }
1495
1496 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001497 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001498 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001499 ret = -EINVAL;
1500 goto err;
1501 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001502
John Harrison5f19e2b2015-05-29 17:43:27 +01001503 params->args_batch_start_offset = args->batch_start_offset;
Chris Wilson743e78c2015-03-27 11:02:10 +00001504 if (i915_needs_cmd_parser(ring) && args->batch_len) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001505 struct drm_i915_gem_object *parsed_batch_obj;
1506
1507 parsed_batch_obj = i915_gem_execbuffer_parse(ring,
Brad Volkin71745372014-12-11 12:13:12 -08001508 &shadow_exec_entry,
1509 eb,
1510 batch_obj,
1511 args->batch_start_offset,
1512 args->batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001513 file->is_master);
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001514 if (IS_ERR(parsed_batch_obj)) {
1515 ret = PTR_ERR(parsed_batch_obj);
Brad Volkin78a42372014-12-11 12:13:09 -08001516 goto err;
1517 }
Chris Wilson17cabf52015-01-14 11:20:57 +00001518
1519 /*
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001520 * parsed_batch_obj == batch_obj means batch not fully parsed:
1521 * Accept, but don't promote to secure.
Chris Wilson17cabf52015-01-14 11:20:57 +00001522 */
Chris Wilson17cabf52015-01-14 11:20:57 +00001523
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001524 if (parsed_batch_obj != batch_obj) {
1525 /*
1526 * Batch parsed and accepted:
1527 *
1528 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1529 * bit from MI_BATCH_BUFFER_START commands issued in
1530 * the dispatch_execbuffer implementations. We
1531 * specifically don't want that set on batches the
1532 * command parser has accepted.
1533 */
1534 dispatch_flags |= I915_DISPATCH_SECURE;
John Harrison5f19e2b2015-05-29 17:43:27 +01001535 params->args_batch_start_offset = 0;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001536 batch_obj = parsed_batch_obj;
1537 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001538 }
1539
Brad Volkin78a42372014-12-11 12:13:09 -08001540 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1541
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001542 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1543 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001544 * hsw should have this fixed, but bdw mucks it up again. */
John Harrison8e004ef2015-02-13 11:48:10 +00001545 if (dispatch_flags & I915_DISPATCH_SECURE) {
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001546 /*
1547 * So on first glance it looks freaky that we pin the batch here
1548 * outside of the reservation loop. But:
1549 * - The batch is already pinned into the relevant ppgtt, so we
1550 * already have the backing storage fully allocated.
1551 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01001552 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001553 * fitting due to fragmentation.
1554 * So this is actually safe.
1555 */
1556 ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
1557 if (ret)
1558 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001559
John Harrison5f19e2b2015-05-29 17:43:27 +01001560 params->batch_obj_vm_offset = i915_gem_obj_ggtt_offset(batch_obj);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001561 } else
John Harrison5f19e2b2015-05-29 17:43:27 +01001562 params->batch_obj_vm_offset = i915_gem_obj_offset(batch_obj, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001563
John Harrison0c8dac82015-05-29 17:43:25 +01001564 /* Allocate a request for this batch buffer nice and early. */
John Harrison6a6ae792015-05-29 17:43:30 +01001565 ret = i915_gem_request_alloc(ring, ctx, &params->request);
John Harrison0c8dac82015-05-29 17:43:25 +01001566 if (ret)
1567 goto err_batch_unpin;
1568
John Harrisonfcfa423c2015-05-29 17:44:12 +01001569 ret = i915_gem_request_add_to_client(params->request, file);
1570 if (ret)
1571 goto err_batch_unpin;
1572
John Harrison5f19e2b2015-05-29 17:43:27 +01001573 /*
1574 * Save assorted stuff away to pass through to *_submission().
1575 * NB: This data should be 'persistent' and not local as it will
1576 * kept around beyond the duration of the IOCTL once the GPU
1577 * scheduler arrives.
1578 */
1579 params->dev = dev;
1580 params->file = file;
1581 params->ring = ring;
1582 params->dispatch_flags = dispatch_flags;
1583 params->batch_obj = batch_obj;
1584 params->ctx = ctx;
1585
1586 ret = dev_priv->gt.execbuf_submit(params, args, &eb->vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001587
John Harrison0c8dac82015-05-29 17:43:25 +01001588err_batch_unpin:
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001589 /*
1590 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1591 * batch vma for correctness. For less ugly and less fragility this
1592 * needs to be adjusted to also track the ggtt batch vma properly as
1593 * active.
1594 */
John Harrison8e004ef2015-02-13 11:48:10 +00001595 if (dispatch_flags & I915_DISPATCH_SECURE)
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001596 i915_gem_object_ggtt_unpin(batch_obj);
John Harrison0c8dac82015-05-29 17:43:25 +01001597
Chris Wilson54cf91d2010-11-25 18:00:26 +00001598err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001599 /* the request owns the ref now */
1600 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001601 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001602
John Harrison6a6ae792015-05-29 17:43:30 +01001603 /*
1604 * If the request was created but not successfully submitted then it
1605 * must be freed again. If it was submitted then it is being tracked
1606 * on the active request list and no clean up is required here.
1607 */
John Harrisonbccca492015-05-29 17:44:11 +01001608 if (ret && params->request)
John Harrison6a6ae792015-05-29 17:43:30 +01001609 i915_gem_request_cancel(params->request);
John Harrison6a6ae792015-05-29 17:43:30 +01001610
Chris Wilson54cf91d2010-11-25 18:00:26 +00001611 mutex_unlock(&dev->struct_mutex);
1612
1613pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001614 /* intel_gpu_busy should also get a ref, so it will free when the device
1615 * is really idle. */
1616 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001617 return ret;
1618}
1619
1620/*
1621 * Legacy execbuffer just creates an exec2 list from the original exec object
1622 * list array and passes it to the real function.
1623 */
1624int
1625i915_gem_execbuffer(struct drm_device *dev, void *data,
1626 struct drm_file *file)
1627{
1628 struct drm_i915_gem_execbuffer *args = data;
1629 struct drm_i915_gem_execbuffer2 exec2;
1630 struct drm_i915_gem_exec_object *exec_list = NULL;
1631 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1632 int ret, i;
1633
Chris Wilson54cf91d2010-11-25 18:00:26 +00001634 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001635 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001636 return -EINVAL;
1637 }
1638
1639 /* Copy in the exec list from userland */
1640 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1641 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1642 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001643 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001644 args->buffer_count);
1645 drm_free_large(exec_list);
1646 drm_free_large(exec2_list);
1647 return -ENOMEM;
1648 }
1649 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001650 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001651 sizeof(*exec_list) * args->buffer_count);
1652 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001653 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001654 args->buffer_count, ret);
1655 drm_free_large(exec_list);
1656 drm_free_large(exec2_list);
1657 return -EFAULT;
1658 }
1659
1660 for (i = 0; i < args->buffer_count; i++) {
1661 exec2_list[i].handle = exec_list[i].handle;
1662 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1663 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1664 exec2_list[i].alignment = exec_list[i].alignment;
1665 exec2_list[i].offset = exec_list[i].offset;
1666 if (INTEL_INFO(dev)->gen < 4)
1667 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1668 else
1669 exec2_list[i].flags = 0;
1670 }
1671
1672 exec2.buffers_ptr = args->buffers_ptr;
1673 exec2.buffer_count = args->buffer_count;
1674 exec2.batch_start_offset = args->batch_start_offset;
1675 exec2.batch_len = args->batch_len;
1676 exec2.DR1 = args->DR1;
1677 exec2.DR4 = args->DR4;
1678 exec2.num_cliprects = args->num_cliprects;
1679 exec2.cliprects_ptr = args->cliprects_ptr;
1680 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001681 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001682
Ben Widawsky41bde552013-12-06 14:11:21 -08001683 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001684 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001685 struct drm_i915_gem_exec_object __user *user_exec_list =
1686 to_user_ptr(args->buffers_ptr);
1687
Chris Wilson54cf91d2010-11-25 18:00:26 +00001688 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001689 for (i = 0; i < args->buffer_count; i++) {
1690 ret = __copy_to_user(&user_exec_list[i].offset,
1691 &exec2_list[i].offset,
1692 sizeof(user_exec_list[i].offset));
1693 if (ret) {
1694 ret = -EFAULT;
1695 DRM_DEBUG("failed to copy %d exec entries "
1696 "back to user (%d)\n",
1697 args->buffer_count, ret);
1698 break;
1699 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001700 }
1701 }
1702
1703 drm_free_large(exec_list);
1704 drm_free_large(exec2_list);
1705 return ret;
1706}
1707
1708int
1709i915_gem_execbuffer2(struct drm_device *dev, void *data,
1710 struct drm_file *file)
1711{
1712 struct drm_i915_gem_execbuffer2 *args = data;
1713 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1714 int ret;
1715
Xi Wanged8cd3b2012-04-23 04:06:41 -04001716 if (args->buffer_count < 1 ||
1717 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001718 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001719 return -EINVAL;
1720 }
1721
Daniel Vetter9cb34662014-04-24 08:09:11 +02001722 if (args->rsvd2 != 0) {
1723 DRM_DEBUG("dirty rvsd2 field\n");
1724 return -EINVAL;
1725 }
1726
Chris Wilson8408c282011-02-21 12:54:48 +00001727 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001728 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001729 if (exec2_list == NULL)
1730 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1731 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001732 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001733 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001734 args->buffer_count);
1735 return -ENOMEM;
1736 }
1737 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001738 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001739 sizeof(*exec2_list) * args->buffer_count);
1740 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001741 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001742 args->buffer_count, ret);
1743 drm_free_large(exec2_list);
1744 return -EFAULT;
1745 }
1746
Ben Widawsky41bde552013-12-06 14:11:21 -08001747 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001748 if (!ret) {
1749 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001750 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001751 to_user_ptr(args->buffers_ptr);
1752 int i;
1753
1754 for (i = 0; i < args->buffer_count; i++) {
1755 ret = __copy_to_user(&user_exec_list[i].offset,
1756 &exec2_list[i].offset,
1757 sizeof(user_exec_list[i].offset));
1758 if (ret) {
1759 ret = -EFAULT;
1760 DRM_DEBUG("failed to copy %d exec entries "
1761 "back to user\n",
1762 args->buffer_count);
1763 break;
1764 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001765 }
1766 }
1767
1768 drm_free_large(exec2_list);
1769 return ret;
1770}