blob: dccb517361b3f1763e3241f9c87f04c58ec4d7fc [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
Michał Winiarskia5f0edf2015-12-29 18:24:52 +0100252/* Used to convert any address to canonical form.
253 * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
254 * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
255 * addresses to be in a canonical form:
256 * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
257 * canonical form [63:48] == [47]."
258 */
259#define GEN8_HIGH_ADDRESS_BIT 47
260static inline uint64_t gen8_canonical_addr(uint64_t address)
261{
262 return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
263}
264
265static inline uint64_t gen8_noncanonical_addr(uint64_t address)
266{
267 return address & ((1ULL << (GEN8_HIGH_ADDRESS_BIT + 1)) - 1);
268}
269
270static inline uint64_t
271relocation_target(struct drm_i915_gem_relocation_entry *reloc,
272 uint64_t target_offset)
273{
274 return gen8_canonical_addr((int)reloc->delta + target_offset);
275}
276
Chris Wilson54cf91d2010-11-25 18:00:26 +0000277static int
Rafael Barbalho5032d872013-08-21 17:10:51 +0100278relocate_entry_cpu(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700279 struct drm_i915_gem_relocation_entry *reloc,
280 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100281{
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700282 struct drm_device *dev = obj->base.dev;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100283 uint32_t page_offset = offset_in_page(reloc->offset);
Michał Winiarskia5f0edf2015-12-29 18:24:52 +0100284 uint64_t delta = relocation_target(reloc, target_offset);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100285 char *vaddr;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800286 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100287
Chris Wilson2cc86b82013-08-26 19:51:00 -0300288 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Rafael Barbalho5032d872013-08-21 17:10:51 +0100289 if (ret)
290 return ret;
291
Dave Gordon033908a2015-12-10 18:51:23 +0000292 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Rafael Barbalho5032d872013-08-21 17:10:51 +0100293 reloc->offset >> PAGE_SHIFT));
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700294 *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700295
296 if (INTEL_INFO(dev)->gen >= 8) {
297 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
298
299 if (page_offset == 0) {
300 kunmap_atomic(vaddr);
Dave Gordon033908a2015-12-10 18:51:23 +0000301 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700302 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
303 }
304
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700305 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700306 }
307
Rafael Barbalho5032d872013-08-21 17:10:51 +0100308 kunmap_atomic(vaddr);
309
310 return 0;
311}
312
313static int
314relocate_entry_gtt(struct drm_i915_gem_object *obj,
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700315 struct drm_i915_gem_relocation_entry *reloc,
316 uint64_t target_offset)
Rafael Barbalho5032d872013-08-21 17:10:51 +0100317{
318 struct drm_device *dev = obj->base.dev;
319 struct drm_i915_private *dev_priv = dev->dev_private;
Michał Winiarskia5f0edf2015-12-29 18:24:52 +0100320 uint64_t delta = relocation_target(reloc, target_offset);
Chris Wilson906843c2014-08-10 06:29:11 +0100321 uint64_t offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100322 void __iomem *reloc_page;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800323 int ret;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100324
325 ret = i915_gem_object_set_to_gtt_domain(obj, true);
326 if (ret)
327 return ret;
328
329 ret = i915_gem_object_put_fence(obj);
330 if (ret)
331 return ret;
332
333 /* Map the page containing the relocation we're going to perform. */
Chris Wilson906843c2014-08-10 06:29:11 +0100334 offset = i915_gem_obj_ggtt_offset(obj);
335 offset += reloc->offset;
Rafael Barbalho5032d872013-08-21 17:10:51 +0100336 reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
Chris Wilson906843c2014-08-10 06:29:11 +0100337 offset & PAGE_MASK);
338 iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700339
340 if (INTEL_INFO(dev)->gen >= 8) {
Chris Wilson906843c2014-08-10 06:29:11 +0100341 offset += sizeof(uint32_t);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700342
Chris Wilson906843c2014-08-10 06:29:11 +0100343 if (offset_in_page(offset) == 0) {
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700344 io_mapping_unmap_atomic(reloc_page);
Chris Wilson906843c2014-08-10 06:29:11 +0100345 reloc_page =
346 io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
347 offset);
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700348 }
349
Chris Wilson906843c2014-08-10 06:29:11 +0100350 iowrite32(upper_32_bits(delta),
351 reloc_page + offset_in_page(offset));
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700352 }
353
Rafael Barbalho5032d872013-08-21 17:10:51 +0100354 io_mapping_unmap_atomic(reloc_page);
355
356 return 0;
357}
358
Chris Wilsonedf44272015-01-14 11:20:56 +0000359static void
360clflush_write32(void *addr, uint32_t value)
361{
362 /* This is not a fast path, so KISS. */
363 drm_clflush_virt_range(addr, sizeof(uint32_t));
364 *(uint32_t *)addr = value;
365 drm_clflush_virt_range(addr, sizeof(uint32_t));
366}
367
368static int
369relocate_entry_clflush(struct drm_i915_gem_object *obj,
370 struct drm_i915_gem_relocation_entry *reloc,
371 uint64_t target_offset)
372{
373 struct drm_device *dev = obj->base.dev;
374 uint32_t page_offset = offset_in_page(reloc->offset);
Michał Winiarskia5f0edf2015-12-29 18:24:52 +0100375 uint64_t delta = relocation_target(reloc, target_offset);
Chris Wilsonedf44272015-01-14 11:20:56 +0000376 char *vaddr;
377 int ret;
378
379 ret = i915_gem_object_set_to_gtt_domain(obj, true);
380 if (ret)
381 return ret;
382
Dave Gordon033908a2015-12-10 18:51:23 +0000383 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Chris Wilsonedf44272015-01-14 11:20:56 +0000384 reloc->offset >> PAGE_SHIFT));
385 clflush_write32(vaddr + page_offset, lower_32_bits(delta));
386
387 if (INTEL_INFO(dev)->gen >= 8) {
388 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
389
390 if (page_offset == 0) {
391 kunmap_atomic(vaddr);
Dave Gordon033908a2015-12-10 18:51:23 +0000392 vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
Chris Wilsonedf44272015-01-14 11:20:56 +0000393 (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
394 }
395
396 clflush_write32(vaddr + page_offset, upper_32_bits(delta));
397 }
398
399 kunmap_atomic(vaddr);
400
401 return 0;
402}
403
Rafael Barbalho5032d872013-08-21 17:10:51 +0100404static int
Chris Wilson54cf91d2010-11-25 18:00:26 +0000405i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
Ben Widawsky27173f12013-08-14 11:38:36 +0200406 struct eb_vmas *eb,
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800407 struct drm_i915_gem_relocation_entry *reloc)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000408{
409 struct drm_device *dev = obj->base.dev;
410 struct drm_gem_object *target_obj;
Daniel Vetter149c8402012-02-15 23:50:23 +0100411 struct drm_i915_gem_object *target_i915_obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200412 struct i915_vma *target_vma;
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700413 uint64_t target_offset;
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800414 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000415
Chris Wilson67731b82010-12-08 10:38:14 +0000416 /* we've already hold a reference to all valid objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200417 target_vma = eb_get_vma(eb, reloc->target_handle);
418 if (unlikely(target_vma == NULL))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000419 return -ENOENT;
Ben Widawsky27173f12013-08-14 11:38:36 +0200420 target_i915_obj = target_vma->obj;
421 target_obj = &target_vma->obj->base;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000422
Michał Winiarskia5f0edf2015-12-29 18:24:52 +0100423 target_offset = gen8_canonical_addr(target_vma->node.start);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000424
Eric Anholte844b992012-07-31 15:35:01 -0700425 /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
426 * pipe_control writes because the gpu doesn't properly redirect them
427 * through the ppgtt for non_secure batchbuffers. */
428 if (unlikely(IS_GEN6(dev) &&
Daniel Vetter08755462015-04-20 09:04:05 -0700429 reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000430 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
Daniel Vetter08755462015-04-20 09:04:05 -0700431 PIN_GLOBAL);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000432 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
433 return ret;
434 }
Eric Anholte844b992012-07-31 15:35:01 -0700435
Chris Wilson54cf91d2010-11-25 18:00:26 +0000436 /* Validate that the target is in a valid r/w GPU domain */
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000437 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100438 DRM_DEBUG("reloc with multiple write domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000439 "obj %p target %d offset %d "
440 "read %08x write %08x",
441 obj, reloc->target_handle,
442 (int) reloc->offset,
443 reloc->read_domains,
444 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800445 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000446 }
Daniel Vetter4ca4a252011-12-14 13:57:27 +0100447 if (unlikely((reloc->write_domain | reloc->read_domains)
448 & ~I915_GEM_GPU_DOMAINS)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100449 DRM_DEBUG("reloc with read/write non-GPU domains: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000450 "obj %p target %d offset %d "
451 "read %08x write %08x",
452 obj, reloc->target_handle,
453 (int) reloc->offset,
454 reloc->read_domains,
455 reloc->write_domain);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800456 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000457 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000458
459 target_obj->pending_read_domains |= reloc->read_domains;
460 target_obj->pending_write_domain |= reloc->write_domain;
461
462 /* If the relocation already has the right value in it, no
463 * more work needs to be done.
464 */
465 if (target_offset == reloc->presumed_offset)
Chris Wilson67731b82010-12-08 10:38:14 +0000466 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000467
468 /* Check that the relocation address is valid... */
Ben Widawsky3c94cee2013-11-02 21:07:11 -0700469 if (unlikely(reloc->offset >
470 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
Daniel Vetterff240192012-01-31 21:08:14 +0100471 DRM_DEBUG("Relocation beyond object bounds: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000472 "obj %p target %d offset %d size %d.\n",
473 obj, reloc->target_handle,
474 (int) reloc->offset,
475 (int) obj->base.size);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800476 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000477 }
Chris Wilsonb8f7ab12010-12-08 10:43:06 +0000478 if (unlikely(reloc->offset & 3)) {
Daniel Vetterff240192012-01-31 21:08:14 +0100479 DRM_DEBUG("Relocation not 4-byte aligned: "
Chris Wilson54cf91d2010-11-25 18:00:26 +0000480 "obj %p target %d offset %d.\n",
481 obj, reloc->target_handle,
482 (int) reloc->offset);
Ben Widawsky8b78f0e2013-12-26 13:39:50 -0800483 return -EINVAL;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000484 }
485
Chris Wilsondabdfe02012-03-26 10:10:27 +0200486 /* We can't wait for rendering with pagefaults disabled */
David Hildenbrand32d82062015-05-11 17:52:12 +0200487 if (obj->active && pagefault_disabled())
Chris Wilsondabdfe02012-03-26 10:10:27 +0200488 return -EFAULT;
489
Rafael Barbalho5032d872013-08-21 17:10:51 +0100490 if (use_cpu_reloc(obj))
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700491 ret = relocate_entry_cpu(obj, reloc, target_offset);
Chris Wilsonedf44272015-01-14 11:20:56 +0000492 else if (obj->map_and_fenceable)
Ben Widawskyd9ceb952014-04-28 17:18:28 -0700493 ret = relocate_entry_gtt(obj, reloc, target_offset);
Chris Wilsonedf44272015-01-14 11:20:56 +0000494 else if (cpu_has_clflush)
495 ret = relocate_entry_clflush(obj, reloc, target_offset);
496 else {
497 WARN_ONCE(1, "Impossible case in relocation handling\n");
498 ret = -ENODEV;
499 }
Chris Wilson54cf91d2010-11-25 18:00:26 +0000500
Daniel Vetterd4d36012013-09-02 20:56:23 +0200501 if (ret)
502 return ret;
503
Chris Wilson54cf91d2010-11-25 18:00:26 +0000504 /* and update the user's relocation entry */
505 reloc->presumed_offset = target_offset;
506
Chris Wilson67731b82010-12-08 10:38:14 +0000507 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000508}
509
510static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200511i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
512 struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000513{
Chris Wilson1d83f442012-03-24 20:12:53 +0000514#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
515 struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
Chris Wilson54cf91d2010-11-25 18:00:26 +0000516 struct drm_i915_gem_relocation_entry __user *user_relocs;
Ben Widawsky27173f12013-08-14 11:38:36 +0200517 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson1d83f442012-03-24 20:12:53 +0000518 int remain, ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000519
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200520 user_relocs = to_user_ptr(entry->relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000521
Chris Wilson1d83f442012-03-24 20:12:53 +0000522 remain = entry->relocation_count;
523 while (remain) {
524 struct drm_i915_gem_relocation_entry *r = stack_reloc;
525 int count = remain;
526 if (count > ARRAY_SIZE(stack_reloc))
527 count = ARRAY_SIZE(stack_reloc);
528 remain -= count;
529
530 if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000531 return -EFAULT;
532
Chris Wilson1d83f442012-03-24 20:12:53 +0000533 do {
534 u64 offset = r->presumed_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000535
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800536 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
Chris Wilson1d83f442012-03-24 20:12:53 +0000537 if (ret)
538 return ret;
539
540 if (r->presumed_offset != offset &&
541 __copy_to_user_inatomic(&user_relocs->presumed_offset,
542 &r->presumed_offset,
543 sizeof(r->presumed_offset))) {
544 return -EFAULT;
545 }
546
547 user_relocs++;
548 r++;
549 } while (--count);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000550 }
551
552 return 0;
Chris Wilson1d83f442012-03-24 20:12:53 +0000553#undef N_RELOC
Chris Wilson54cf91d2010-11-25 18:00:26 +0000554}
555
556static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200557i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
558 struct eb_vmas *eb,
559 struct drm_i915_gem_relocation_entry *relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000560{
Ben Widawsky27173f12013-08-14 11:38:36 +0200561 const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000562 int i, ret;
563
564 for (i = 0; i < entry->relocation_count; i++) {
Ben Widawsky3e7a0322013-12-06 14:10:57 -0800565 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000566 if (ret)
567 return ret;
568 }
569
570 return 0;
571}
572
573static int
Ben Widawsky17601cbc2013-11-25 09:54:38 -0800574i915_gem_execbuffer_relocate(struct eb_vmas *eb)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000575{
Ben Widawsky27173f12013-08-14 11:38:36 +0200576 struct i915_vma *vma;
Chris Wilsond4aeee72011-03-14 15:11:24 +0000577 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000578
Chris Wilsond4aeee72011-03-14 15:11:24 +0000579 /* This is the fast path and we cannot handle a pagefault whilst
580 * holding the struct mutex lest the user pass in the relocations
581 * contained within a mmaped bo. For in such a case we, the page
582 * fault handler would call i915_gem_fault() and we would try to
583 * acquire the struct mutex again. Obviously this is bad and so
584 * lockdep complains vehemently.
585 */
586 pagefault_disable();
Ben Widawsky27173f12013-08-14 11:38:36 +0200587 list_for_each_entry(vma, &eb->vmas, exec_list) {
588 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000589 if (ret)
Chris Wilsond4aeee72011-03-14 15:11:24 +0000590 break;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000591 }
Chris Wilsond4aeee72011-03-14 15:11:24 +0000592 pagefault_enable();
Chris Wilson54cf91d2010-11-25 18:00:26 +0000593
Chris Wilsond4aeee72011-03-14 15:11:24 +0000594 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000595}
596
Chris Wilsonedf44272015-01-14 11:20:56 +0000597static bool only_mappable_for_reloc(unsigned int flags)
598{
599 return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
600 __EXEC_OBJECT_NEEDS_MAP;
601}
602
Chris Wilson1690e1e2011-12-14 13:57:08 +0100603static int
Ben Widawsky27173f12013-08-14 11:38:36 +0200604i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100605 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200606 bool *need_reloc)
Chris Wilson1690e1e2011-12-14 13:57:08 +0100607{
Ben Widawsky6f65e292013-12-06 14:10:56 -0800608 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200609 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Chris Wilsond23db882014-05-23 08:48:08 +0200610 uint64_t flags;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100611 int ret;
612
Daniel Vetter08755462015-04-20 09:04:05 -0700613 flags = PIN_USER;
Daniel Vetter0229da32015-04-14 19:01:54 +0200614 if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
615 flags |= PIN_GLOBAL;
616
Chris Wilsonedf44272015-01-14 11:20:56 +0000617 if (!drm_mm_node_allocated(&vma->node)) {
Michel Thierry101b5062015-10-01 13:33:57 +0100618 /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
619 * limit address to the first 4GBs for unflagged objects.
620 */
621 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
622 flags |= PIN_ZONE_4G;
Chris Wilsonedf44272015-01-14 11:20:56 +0000623 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
624 flags |= PIN_GLOBAL | PIN_MAPPABLE;
Chris Wilsonedf44272015-01-14 11:20:56 +0000625 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
626 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
Chris Wilson506a8e82015-12-08 11:55:07 +0000627 if (entry->flags & EXEC_OBJECT_PINNED)
628 flags |= entry->offset | PIN_OFFSET_FIXED;
Michel Thierry101b5062015-10-01 13:33:57 +0100629 if ((flags & PIN_MAPPABLE) == 0)
630 flags |= PIN_HIGH;
Chris Wilsonedf44272015-01-14 11:20:56 +0000631 }
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100632
633 ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
Chris Wilsonedf44272015-01-14 11:20:56 +0000634 if ((ret == -ENOSPC || ret == -E2BIG) &&
635 only_mappable_for_reloc(entry->flags))
636 ret = i915_gem_object_pin(obj, vma->vm,
637 entry->alignment,
Daniel Vetter0229da32015-04-14 19:01:54 +0200638 flags & ~PIN_MAPPABLE);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100639 if (ret)
640 return ret;
641
Chris Wilson7788a762012-08-24 19:18:18 +0100642 entry->flags |= __EXEC_OBJECT_HAS_PIN;
643
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100644 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
645 ret = i915_gem_object_get_fence(obj);
646 if (ret)
647 return ret;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100648
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100649 if (i915_gem_object_pin_fence(obj))
650 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
Chris Wilson1690e1e2011-12-14 13:57:08 +0100651 }
652
Ben Widawsky27173f12013-08-14 11:38:36 +0200653 if (entry->offset != vma->node.start) {
654 entry->offset = vma->node.start;
Daniel Vettered5982e2013-01-17 22:23:36 +0100655 *need_reloc = true;
656 }
657
658 if (entry->flags & EXEC_OBJECT_WRITE) {
659 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
660 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
661 }
662
Chris Wilson1690e1e2011-12-14 13:57:08 +0100663 return 0;
Chris Wilson7788a762012-08-24 19:18:18 +0100664}
Chris Wilson1690e1e2011-12-14 13:57:08 +0100665
Chris Wilsond23db882014-05-23 08:48:08 +0200666static bool
Chris Wilsone6a84462014-08-11 12:00:12 +0200667need_reloc_mappable(struct i915_vma *vma)
668{
669 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
670
671 if (entry->relocation_count == 0)
672 return false;
673
674 if (!i915_is_ggtt(vma->vm))
675 return false;
676
677 /* See also use_cpu_reloc() */
678 if (HAS_LLC(vma->obj->base.dev))
679 return false;
680
681 if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
682 return false;
683
684 return true;
685}
686
687static bool
688eb_vma_misplaced(struct i915_vma *vma)
Chris Wilsond23db882014-05-23 08:48:08 +0200689{
690 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
691 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsond23db882014-05-23 08:48:08 +0200692
Chris Wilsone6a84462014-08-11 12:00:12 +0200693 WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
Chris Wilsond23db882014-05-23 08:48:08 +0200694 !i915_is_ggtt(vma->vm));
695
696 if (entry->alignment &&
697 vma->node.start & (entry->alignment - 1))
698 return true;
699
Chris Wilson506a8e82015-12-08 11:55:07 +0000700 if (entry->flags & EXEC_OBJECT_PINNED &&
701 vma->node.start != entry->offset)
702 return true;
703
Chris Wilsond23db882014-05-23 08:48:08 +0200704 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
705 vma->node.start < BATCH_OFFSET_BIAS)
706 return true;
707
Chris Wilsonedf44272015-01-14 11:20:56 +0000708 /* avoid costly ping-pong once a batch bo ended up non-mappable */
709 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
710 return !only_mappable_for_reloc(entry->flags);
711
Michel Thierry101b5062015-10-01 13:33:57 +0100712 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
713 (vma->node.start + vma->node.size - 1) >> 32)
714 return true;
715
Chris Wilsond23db882014-05-23 08:48:08 +0200716 return false;
717}
718
Chris Wilson54cf91d2010-11-25 18:00:26 +0000719static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100720i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200721 struct list_head *vmas,
David Weinehallb1b38272015-05-20 17:00:13 +0300722 struct intel_context *ctx,
Daniel Vettered5982e2013-01-17 22:23:36 +0100723 bool *need_relocs)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000724{
Chris Wilson432e58e2010-11-25 19:32:06 +0000725 struct drm_i915_gem_object *obj;
Ben Widawsky27173f12013-08-14 11:38:36 +0200726 struct i915_vma *vma;
Ben Widawsky68c8c172013-09-11 14:57:50 -0700727 struct i915_address_space *vm;
Ben Widawsky27173f12013-08-14 11:38:36 +0200728 struct list_head ordered_vmas;
Chris Wilson506a8e82015-12-08 11:55:07 +0000729 struct list_head pinned_vmas;
Chris Wilson7788a762012-08-24 19:18:18 +0100730 bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
731 int retry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000732
Chris Wilson227f7822014-05-15 10:41:42 +0100733 i915_gem_retire_requests_ring(ring);
734
Ben Widawsky68c8c172013-09-11 14:57:50 -0700735 vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
736
Ben Widawsky27173f12013-08-14 11:38:36 +0200737 INIT_LIST_HEAD(&ordered_vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000738 INIT_LIST_HEAD(&pinned_vmas);
Ben Widawsky27173f12013-08-14 11:38:36 +0200739 while (!list_empty(vmas)) {
Chris Wilson6fe4f142011-01-10 17:35:37 +0000740 struct drm_i915_gem_exec_object2 *entry;
741 bool need_fence, need_mappable;
742
Ben Widawsky27173f12013-08-14 11:38:36 +0200743 vma = list_first_entry(vmas, struct i915_vma, exec_list);
744 obj = vma->obj;
745 entry = vma->exec_entry;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000746
David Weinehallb1b38272015-05-20 17:00:13 +0300747 if (ctx->flags & CONTEXT_NO_ZEROMAP)
748 entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
749
Chris Wilson82b6b6d2014-08-09 17:37:24 +0100750 if (!has_fenced_gpu_access)
751 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000752 need_fence =
Chris Wilson6fe4f142011-01-10 17:35:37 +0000753 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
754 obj->tiling_mode != I915_TILING_NONE;
Ben Widawsky27173f12013-08-14 11:38:36 +0200755 need_mappable = need_fence || need_reloc_mappable(vma);
Chris Wilson6fe4f142011-01-10 17:35:37 +0000756
Chris Wilson506a8e82015-12-08 11:55:07 +0000757 if (entry->flags & EXEC_OBJECT_PINNED)
758 list_move_tail(&vma->exec_list, &pinned_vmas);
759 else if (need_mappable) {
Chris Wilsone6a84462014-08-11 12:00:12 +0200760 entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
Ben Widawsky27173f12013-08-14 11:38:36 +0200761 list_move(&vma->exec_list, &ordered_vmas);
Chris Wilsone6a84462014-08-11 12:00:12 +0200762 } else
Ben Widawsky27173f12013-08-14 11:38:36 +0200763 list_move_tail(&vma->exec_list, &ordered_vmas);
Chris Wilson595dad72011-01-13 11:03:48 +0000764
Daniel Vettered5982e2013-01-17 22:23:36 +0100765 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
Chris Wilson595dad72011-01-13 11:03:48 +0000766 obj->base.pending_write_domain = 0;
Chris Wilson6fe4f142011-01-10 17:35:37 +0000767 }
Ben Widawsky27173f12013-08-14 11:38:36 +0200768 list_splice(&ordered_vmas, vmas);
Chris Wilson506a8e82015-12-08 11:55:07 +0000769 list_splice(&pinned_vmas, vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000770
771 /* Attempt to pin all of the buffers into the GTT.
772 * This is done in 3 phases:
773 *
774 * 1a. Unbind all objects that do not match the GTT constraints for
775 * the execbuffer (fenceable, mappable, alignment etc).
776 * 1b. Increment pin count for already bound objects.
777 * 2. Bind new objects.
778 * 3. Decrement pin count.
779 *
Chris Wilson7788a762012-08-24 19:18:18 +0100780 * This avoid unnecessary unbinding of later objects in order to make
Chris Wilson54cf91d2010-11-25 18:00:26 +0000781 * room for the earlier objects *unless* we need to defragment.
782 */
783 retry = 0;
784 do {
Chris Wilson7788a762012-08-24 19:18:18 +0100785 int ret = 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000786
787 /* Unbind any ill-fitting objects or pin. */
Ben Widawsky27173f12013-08-14 11:38:36 +0200788 list_for_each_entry(vma, vmas, exec_list) {
Ben Widawsky27173f12013-08-14 11:38:36 +0200789 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson54cf91d2010-11-25 18:00:26 +0000790 continue;
791
Chris Wilsone6a84462014-08-11 12:00:12 +0200792 if (eb_vma_misplaced(vma))
Ben Widawsky27173f12013-08-14 11:38:36 +0200793 ret = i915_vma_unbind(vma);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000794 else
Ben Widawsky27173f12013-08-14 11:38:36 +0200795 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson432e58e2010-11-25 19:32:06 +0000796 if (ret)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000797 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000798 }
799
800 /* Bind fresh objects */
Ben Widawsky27173f12013-08-14 11:38:36 +0200801 list_for_each_entry(vma, vmas, exec_list) {
802 if (drm_mm_node_allocated(&vma->node))
Chris Wilson1690e1e2011-12-14 13:57:08 +0100803 continue;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000804
Ben Widawsky27173f12013-08-14 11:38:36 +0200805 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
Chris Wilson7788a762012-08-24 19:18:18 +0100806 if (ret)
807 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000808 }
809
Chris Wilsona415d352013-11-26 11:23:15 +0000810err:
Chris Wilson6c085a72012-08-20 11:40:46 +0200811 if (ret != -ENOSPC || retry++)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000812 return ret;
813
Chris Wilsona415d352013-11-26 11:23:15 +0000814 /* Decrement pin count for bound objects */
815 list_for_each_entry(vma, vmas, exec_list)
816 i915_gem_execbuffer_unreserve_vma(vma);
817
Ben Widawsky68c8c172013-09-11 14:57:50 -0700818 ret = i915_gem_evict_vm(vm, true);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000819 if (ret)
820 return ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000821 } while (1);
822}
823
824static int
825i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
Daniel Vettered5982e2013-01-17 22:23:36 +0100826 struct drm_i915_gem_execbuffer2 *args,
Chris Wilson54cf91d2010-11-25 18:00:26 +0000827 struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100828 struct intel_engine_cs *ring,
Ben Widawsky27173f12013-08-14 11:38:36 +0200829 struct eb_vmas *eb,
David Weinehallb1b38272015-05-20 17:00:13 +0300830 struct drm_i915_gem_exec_object2 *exec,
831 struct intel_context *ctx)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000832{
833 struct drm_i915_gem_relocation_entry *reloc;
Ben Widawsky27173f12013-08-14 11:38:36 +0200834 struct i915_address_space *vm;
835 struct i915_vma *vma;
Daniel Vettered5982e2013-01-17 22:23:36 +0100836 bool need_relocs;
Chris Wilsondd6864a2011-01-12 23:49:13 +0000837 int *reloc_offset;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000838 int i, total, ret;
Daniel Vetterb205ca52013-09-19 14:00:11 +0200839 unsigned count = args->buffer_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000840
Ben Widawsky27173f12013-08-14 11:38:36 +0200841 vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
842
Chris Wilson67731b82010-12-08 10:38:14 +0000843 /* We may process another execbuffer during the unlock... */
Ben Widawsky27173f12013-08-14 11:38:36 +0200844 while (!list_empty(&eb->vmas)) {
845 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
846 list_del_init(&vma->exec_list);
Chris Wilsona415d352013-11-26 11:23:15 +0000847 i915_gem_execbuffer_unreserve_vma(vma);
Ben Widawsky27173f12013-08-14 11:38:36 +0200848 drm_gem_object_unreference(&vma->obj->base);
Chris Wilson67731b82010-12-08 10:38:14 +0000849 }
850
Chris Wilson54cf91d2010-11-25 18:00:26 +0000851 mutex_unlock(&dev->struct_mutex);
852
853 total = 0;
854 for (i = 0; i < count; i++)
Chris Wilson432e58e2010-11-25 19:32:06 +0000855 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000856
Chris Wilsondd6864a2011-01-12 23:49:13 +0000857 reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
Chris Wilson54cf91d2010-11-25 18:00:26 +0000858 reloc = drm_malloc_ab(total, sizeof(*reloc));
Chris Wilsondd6864a2011-01-12 23:49:13 +0000859 if (reloc == NULL || reloc_offset == NULL) {
860 drm_free_large(reloc);
861 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000862 mutex_lock(&dev->struct_mutex);
863 return -ENOMEM;
864 }
865
866 total = 0;
867 for (i = 0; i < count; i++) {
868 struct drm_i915_gem_relocation_entry __user *user_relocs;
Chris Wilson262b6d32013-01-15 16:17:54 +0000869 u64 invalid_offset = (u64)-1;
870 int j;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000871
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200872 user_relocs = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000873
874 if (copy_from_user(reloc+total, user_relocs,
Chris Wilson432e58e2010-11-25 19:32:06 +0000875 exec[i].relocation_count * sizeof(*reloc))) {
Chris Wilson54cf91d2010-11-25 18:00:26 +0000876 ret = -EFAULT;
877 mutex_lock(&dev->struct_mutex);
878 goto err;
879 }
880
Chris Wilson262b6d32013-01-15 16:17:54 +0000881 /* As we do not update the known relocation offsets after
882 * relocating (due to the complexities in lock handling),
883 * we need to mark them as invalid now so that we force the
884 * relocation processing next time. Just in case the target
885 * object is evicted and then rebound into its old
886 * presumed_offset before the next execbuffer - if that
887 * happened we would make the mistake of assuming that the
888 * relocations were valid.
889 */
890 for (j = 0; j < exec[i].relocation_count; j++) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +0100891 if (__copy_to_user(&user_relocs[j].presumed_offset,
892 &invalid_offset,
893 sizeof(invalid_offset))) {
Chris Wilson262b6d32013-01-15 16:17:54 +0000894 ret = -EFAULT;
895 mutex_lock(&dev->struct_mutex);
896 goto err;
897 }
898 }
899
Chris Wilsondd6864a2011-01-12 23:49:13 +0000900 reloc_offset[i] = total;
Chris Wilson432e58e2010-11-25 19:32:06 +0000901 total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000902 }
903
904 ret = i915_mutex_lock_interruptible(dev);
905 if (ret) {
906 mutex_lock(&dev->struct_mutex);
907 goto err;
908 }
909
Chris Wilson67731b82010-12-08 10:38:14 +0000910 /* reacquire the objects */
Chris Wilson67731b82010-12-08 10:38:14 +0000911 eb_reset(eb);
Ben Widawsky27173f12013-08-14 11:38:36 +0200912 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +0000913 if (ret)
914 goto err;
Chris Wilson67731b82010-12-08 10:38:14 +0000915
Daniel Vettered5982e2013-01-17 22:23:36 +0100916 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
David Weinehallb1b38272015-05-20 17:00:13 +0300917 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, ctx, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000918 if (ret)
919 goto err;
920
Ben Widawsky27173f12013-08-14 11:38:36 +0200921 list_for_each_entry(vma, &eb->vmas, exec_list) {
922 int offset = vma->exec_entry - exec;
923 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
924 reloc + reloc_offset[offset]);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000925 if (ret)
926 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000927 }
928
929 /* Leave the user relocations as are, this is the painfully slow path,
930 * and we want to avoid the complication of dropping the lock whilst
931 * having buffers reserved in the aperture and so causing spurious
932 * ENOSPC for random operations.
933 */
934
935err:
936 drm_free_large(reloc);
Chris Wilsondd6864a2011-01-12 23:49:13 +0000937 drm_free_large(reloc_offset);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000938 return ret;
939}
940
Chris Wilson54cf91d2010-11-25 18:00:26 +0000941static int
John Harrison535fbe82015-05-29 17:43:32 +0100942i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
Ben Widawsky27173f12013-08-14 11:38:36 +0200943 struct list_head *vmas)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000944{
John Harrison535fbe82015-05-29 17:43:32 +0100945 const unsigned other_rings = ~intel_ring_flag(req->ring);
Ben Widawsky27173f12013-08-14 11:38:36 +0200946 struct i915_vma *vma;
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200947 uint32_t flush_domains = 0;
Chris Wilson000433b2013-08-08 14:41:09 +0100948 bool flush_chipset = false;
Chris Wilson432e58e2010-11-25 19:32:06 +0000949 int ret;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000950
Ben Widawsky27173f12013-08-14 11:38:36 +0200951 list_for_each_entry(vma, vmas, exec_list) {
952 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson03ade512015-04-27 13:41:18 +0100953
954 if (obj->active & other_rings) {
John Harrison91af1272015-06-18 13:14:56 +0100955 ret = i915_gem_object_sync(obj, req->ring, &req);
Chris Wilson03ade512015-04-27 13:41:18 +0100956 if (ret)
957 return ret;
958 }
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200959
960 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
Chris Wilson000433b2013-08-08 14:41:09 +0100961 flush_chipset |= i915_gem_clflush_object(obj, false);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200962
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200963 flush_domains |= obj->base.write_domain;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000964 }
965
Chris Wilson000433b2013-08-08 14:41:09 +0100966 if (flush_chipset)
John Harrison535fbe82015-05-29 17:43:32 +0100967 i915_gem_chipset_flush(req->ring->dev);
Daniel Vetter6ac42f42012-07-21 12:25:01 +0200968
969 if (flush_domains & I915_GEM_DOMAIN_GTT)
970 wmb();
971
Chris Wilson09cf7c92012-07-13 14:14:08 +0100972 /* Unconditionally invalidate gpu caches and ensure that we do flush
973 * any residual writes from the previous batch.
974 */
John Harrison2f200552015-05-29 17:43:53 +0100975 return intel_ring_invalidate_all_caches(req);
Chris Wilson54cf91d2010-11-25 18:00:26 +0000976}
977
Chris Wilson432e58e2010-11-25 19:32:06 +0000978static bool
979i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +0000980{
Daniel Vettered5982e2013-01-17 22:23:36 +0100981 if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
982 return false;
983
Chris Wilson2f5945b2015-10-06 11:39:55 +0100984 /* Kernel clipping was a DRI1 misfeature */
985 if (exec->num_cliprects || exec->cliprects_ptr)
986 return false;
987
988 if (exec->DR4 == 0xffffffff) {
989 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
990 exec->DR4 = 0;
991 }
992 if (exec->DR1 || exec->DR4)
993 return false;
994
995 if ((exec->batch_start_offset | exec->batch_len) & 0x7)
996 return false;
997
998 return true;
Chris Wilson54cf91d2010-11-25 18:00:26 +0000999}
1000
1001static int
Chris Wilsonad19f102014-08-10 06:29:08 +01001002validate_exec_list(struct drm_device *dev,
1003 struct drm_i915_gem_exec_object2 *exec,
Chris Wilson54cf91d2010-11-25 18:00:26 +00001004 int count)
1005{
Daniel Vetterb205ca52013-09-19 14:00:11 +02001006 unsigned relocs_total = 0;
1007 unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
Chris Wilsonad19f102014-08-10 06:29:08 +01001008 unsigned invalid_flags;
1009 int i;
1010
1011 invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
1012 if (USES_FULL_PPGTT(dev))
1013 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001014
1015 for (i = 0; i < count; i++) {
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001016 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001017 int length; /* limited by fault_in_pages_readable() */
1018
Chris Wilsonad19f102014-08-10 06:29:08 +01001019 if (exec[i].flags & invalid_flags)
Daniel Vettered5982e2013-01-17 22:23:36 +01001020 return -EINVAL;
1021
Michał Winiarskia5f0edf2015-12-29 18:24:52 +01001022 /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
1023 * any non-page-aligned or non-canonical addresses.
1024 */
1025 if (exec[i].flags & EXEC_OBJECT_PINNED) {
1026 if (exec[i].offset !=
1027 gen8_canonical_addr(exec[i].offset & PAGE_MASK))
1028 return -EINVAL;
1029
1030 /* From drm_mm perspective address space is continuous,
1031 * so from this point we're always using non-canonical
1032 * form internally.
1033 */
1034 exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
1035 }
1036
Chris Wilson55a97852015-06-19 13:59:46 +01001037 if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
1038 return -EINVAL;
1039
Kees Cook3118a4f2013-03-11 17:31:45 -07001040 /* First check for malicious input causing overflow in
1041 * the worst case where we need to allocate the entire
1042 * relocation tree as a single array.
1043 */
1044 if (exec[i].relocation_count > relocs_max - relocs_total)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001045 return -EINVAL;
Kees Cook3118a4f2013-03-11 17:31:45 -07001046 relocs_total += exec[i].relocation_count;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001047
1048 length = exec[i].relocation_count *
1049 sizeof(struct drm_i915_gem_relocation_entry);
Kees Cook30587532013-03-11 14:37:35 -07001050 /*
1051 * We must check that the entire relocation array is safe
1052 * to read, but since we may need to update the presumed
1053 * offsets during execution, check for full write access.
1054 */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001055 if (!access_ok(VERIFY_WRITE, ptr, length))
1056 return -EFAULT;
1057
Jani Nikulad330a952014-01-21 11:24:25 +02001058 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +08001059 if (fault_in_multipages_readable(ptr, length))
1060 return -EFAULT;
1061 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001062 }
1063
1064 return 0;
1065}
1066
Oscar Mateo273497e2014-05-22 14:13:37 +01001067static struct intel_context *
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001068i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001069 struct intel_engine_cs *ring, const u32 ctx_id)
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001070{
Oscar Mateo273497e2014-05-22 14:13:37 +01001071 struct intel_context *ctx = NULL;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001072 struct i915_ctx_hang_stats *hs;
1073
Oscar Mateo821d66d2014-07-03 16:28:00 +01001074 if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001075 return ERR_PTR(-EINVAL);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001076
Ben Widawsky41bde552013-12-06 14:11:21 -08001077 ctx = i915_gem_context_get(file->driver_priv, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001078 if (IS_ERR(ctx))
Ben Widawsky41bde552013-12-06 14:11:21 -08001079 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001080
Ben Widawsky41bde552013-12-06 14:11:21 -08001081 hs = &ctx->hang_stats;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001082 if (hs->banned) {
1083 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
Ben Widawsky41bde552013-12-06 14:11:21 -08001084 return ERR_PTR(-EIO);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001085 }
1086
Oscar Mateoec3e9962014-07-24 17:04:18 +01001087 if (i915.enable_execlists && !ctx->engine[ring->id].state) {
Nick Hoathe84fe802015-09-11 12:53:46 +01001088 int ret = intel_lr_context_deferred_alloc(ctx, ring);
Oscar Mateoec3e9962014-07-24 17:04:18 +01001089 if (ret) {
1090 DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
1091 return ERR_PTR(ret);
1092 }
1093 }
1094
Ben Widawsky41bde552013-12-06 14:11:21 -08001095 return ctx;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001096}
1097
Oscar Mateoba8b7cc2014-07-24 17:04:33 +01001098void
Ben Widawsky27173f12013-08-14 11:38:36 +02001099i915_gem_execbuffer_move_to_active(struct list_head *vmas,
John Harrison8a8edb52015-05-29 17:43:33 +01001100 struct drm_i915_gem_request *req)
Chris Wilson432e58e2010-11-25 19:32:06 +00001101{
John Harrison8a8edb52015-05-29 17:43:33 +01001102 struct intel_engine_cs *ring = i915_gem_request_get_ring(req);
Ben Widawsky27173f12013-08-14 11:38:36 +02001103 struct i915_vma *vma;
Chris Wilson432e58e2010-11-25 19:32:06 +00001104
Ben Widawsky27173f12013-08-14 11:38:36 +02001105 list_for_each_entry(vma, vmas, exec_list) {
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001106 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
Ben Widawsky27173f12013-08-14 11:38:36 +02001107 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson69c2fc82012-07-20 12:41:03 +01001108 u32 old_read = obj->base.read_domains;
1109 u32 old_write = obj->base.write_domain;
Chris Wilsondb53a302011-02-03 11:57:46 +00001110
Chris Wilson51bc1402015-08-31 15:10:39 +01001111 obj->dirty = 1; /* be paranoid */
Chris Wilson432e58e2010-11-25 19:32:06 +00001112 obj->base.write_domain = obj->base.pending_write_domain;
Daniel Vettered5982e2013-01-17 22:23:36 +01001113 if (obj->base.write_domain == 0)
1114 obj->base.pending_read_domains |= obj->base.read_domains;
1115 obj->base.read_domains = obj->base.pending_read_domains;
Chris Wilson432e58e2010-11-25 19:32:06 +00001116
John Harrisonb2af0372015-05-29 17:43:50 +01001117 i915_vma_move_to_active(vma, req);
Chris Wilson432e58e2010-11-25 19:32:06 +00001118 if (obj->base.write_domain) {
John Harrison97b2a6a2014-11-24 18:49:26 +00001119 i915_gem_request_assign(&obj->last_write_req, req);
Daniel Vetterf99d7062014-06-19 16:01:59 +02001120
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -07001121 intel_fb_obj_invalidate(obj, ORIGIN_CS);
Chris Wilsonc8725f32014-03-17 12:21:55 +00001122
1123 /* update for the implicit flush after a batch */
1124 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson432e58e2010-11-25 19:32:06 +00001125 }
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001126 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
John Harrison97b2a6a2014-11-24 18:49:26 +00001127 i915_gem_request_assign(&obj->last_fenced_req, req);
Chris Wilson82b6b6d2014-08-09 17:37:24 +01001128 if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
1129 struct drm_i915_private *dev_priv = to_i915(ring->dev);
1130 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
1131 &dev_priv->mm.fence_list);
1132 }
1133 }
Chris Wilson432e58e2010-11-25 19:32:06 +00001134
Chris Wilsondb53a302011-02-03 11:57:46 +00001135 trace_i915_gem_object_change_domain(obj, old_read, old_write);
Chris Wilson432e58e2010-11-25 19:32:06 +00001136 }
1137}
1138
Oscar Mateoba8b7cc2014-07-24 17:04:33 +01001139void
John Harrisonadeca762015-05-29 17:43:28 +01001140i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params *params)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001141{
Daniel Vettercc889e02012-06-13 20:45:19 +02001142 /* Unconditionally force add_request to emit a full flush. */
John Harrisonadeca762015-05-29 17:43:28 +01001143 params->ring->gpu_caches_dirty = true;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001144
Chris Wilson432e58e2010-11-25 19:32:06 +00001145 /* Add a breadcrumb for the completion of the batch buffer */
John Harrisonfcfa423c2015-05-29 17:44:12 +01001146 __i915_add_request(params->request, params->batch_obj, true);
Chris Wilson432e58e2010-11-25 19:32:06 +00001147}
Chris Wilson54cf91d2010-11-25 18:00:26 +00001148
1149static int
Eric Anholtae662d32012-01-03 09:23:29 -08001150i915_reset_gen7_sol_offsets(struct drm_device *dev,
John Harrison2f200552015-05-29 17:43:53 +01001151 struct drm_i915_gem_request *req)
Eric Anholtae662d32012-01-03 09:23:29 -08001152{
John Harrison2f200552015-05-29 17:43:53 +01001153 struct intel_engine_cs *ring = req->ring;
Jani Nikula50227e12014-03-31 14:27:21 +03001154 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtae662d32012-01-03 09:23:29 -08001155 int ret, i;
1156
Daniel Vetter9d662da2014-04-24 08:09:09 +02001157 if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
1158 DRM_DEBUG("sol reset is gen7/rcs only\n");
1159 return -EINVAL;
1160 }
Eric Anholtae662d32012-01-03 09:23:29 -08001161
John Harrison5fb9de12015-05-29 17:44:07 +01001162 ret = intel_ring_begin(req, 4 * 3);
Eric Anholtae662d32012-01-03 09:23:29 -08001163 if (ret)
1164 return ret;
1165
1166 for (i = 0; i < 4; i++) {
1167 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +02001168 intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
Eric Anholtae662d32012-01-03 09:23:29 -08001169 intel_ring_emit(ring, 0);
1170 }
1171
1172 intel_ring_advance(ring);
1173
1174 return 0;
1175}
1176
Brad Volkin71745372014-12-11 12:13:12 -08001177static struct drm_i915_gem_object*
1178i915_gem_execbuffer_parse(struct intel_engine_cs *ring,
1179 struct drm_i915_gem_exec_object2 *shadow_exec_entry,
1180 struct eb_vmas *eb,
1181 struct drm_i915_gem_object *batch_obj,
1182 u32 batch_start_offset,
1183 u32 batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001184 bool is_master)
Brad Volkin71745372014-12-11 12:13:12 -08001185{
Brad Volkin71745372014-12-11 12:13:12 -08001186 struct drm_i915_gem_object *shadow_batch_obj;
Chris Wilson17cabf52015-01-14 11:20:57 +00001187 struct i915_vma *vma;
Brad Volkin71745372014-12-11 12:13:12 -08001188 int ret;
1189
Chris Wilson06fbca72015-04-07 16:20:36 +01001190 shadow_batch_obj = i915_gem_batch_pool_get(&ring->batch_pool,
Chris Wilson17cabf52015-01-14 11:20:57 +00001191 PAGE_ALIGN(batch_len));
Brad Volkin71745372014-12-11 12:13:12 -08001192 if (IS_ERR(shadow_batch_obj))
1193 return shadow_batch_obj;
1194
1195 ret = i915_parse_cmds(ring,
1196 batch_obj,
1197 shadow_batch_obj,
1198 batch_start_offset,
1199 batch_len,
1200 is_master);
Chris Wilson17cabf52015-01-14 11:20:57 +00001201 if (ret)
1202 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001203
Chris Wilson17cabf52015-01-14 11:20:57 +00001204 ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
1205 if (ret)
1206 goto err;
Brad Volkin71745372014-12-11 12:13:12 -08001207
Chris Wilsonde4e7832015-04-07 16:20:35 +01001208 i915_gem_object_unpin_pages(shadow_batch_obj);
1209
Chris Wilson17cabf52015-01-14 11:20:57 +00001210 memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
Brad Volkin71745372014-12-11 12:13:12 -08001211
Chris Wilson17cabf52015-01-14 11:20:57 +00001212 vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
1213 vma->exec_entry = shadow_exec_entry;
Chris Wilsonde4e7832015-04-07 16:20:35 +01001214 vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
Chris Wilson17cabf52015-01-14 11:20:57 +00001215 drm_gem_object_reference(&shadow_batch_obj->base);
1216 list_add_tail(&vma->exec_list, &eb->vmas);
Brad Volkin71745372014-12-11 12:13:12 -08001217
Chris Wilson17cabf52015-01-14 11:20:57 +00001218 shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
Brad Volkin71745372014-12-11 12:13:12 -08001219
Chris Wilson17cabf52015-01-14 11:20:57 +00001220 return shadow_batch_obj;
1221
1222err:
Chris Wilsonde4e7832015-04-07 16:20:35 +01001223 i915_gem_object_unpin_pages(shadow_batch_obj);
Chris Wilson17cabf52015-01-14 11:20:57 +00001224 if (ret == -EACCES) /* unhandled chained batch */
1225 return batch_obj;
1226 else
1227 return ERR_PTR(ret);
Brad Volkin71745372014-12-11 12:13:12 -08001228}
Chris Wilson5c6c6002014-09-06 10:28:27 +01001229
Oscar Mateoa83014d2014-07-24 17:04:21 +01001230int
John Harrison5f19e2b2015-05-29 17:43:27 +01001231i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
Oscar Mateoa83014d2014-07-24 17:04:21 +01001232 struct drm_i915_gem_execbuffer2 *args,
John Harrison5f19e2b2015-05-29 17:43:27 +01001233 struct list_head *vmas)
Oscar Mateo78382592014-07-03 16:28:05 +01001234{
John Harrison5f19e2b2015-05-29 17:43:27 +01001235 struct drm_device *dev = params->dev;
1236 struct intel_engine_cs *ring = params->ring;
Oscar Mateo78382592014-07-03 16:28:05 +01001237 struct drm_i915_private *dev_priv = dev->dev_private;
John Harrison5f19e2b2015-05-29 17:43:27 +01001238 u64 exec_start, exec_len;
Oscar Mateo78382592014-07-03 16:28:05 +01001239 int instp_mode;
1240 u32 instp_mask;
Chris Wilson2f5945b2015-10-06 11:39:55 +01001241 int ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001242
John Harrison535fbe82015-05-29 17:43:32 +01001243 ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
Oscar Mateo78382592014-07-03 16:28:05 +01001244 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001245 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001246
John Harrisonba01cc92015-05-29 17:43:41 +01001247 ret = i915_switch_context(params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001248 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001249 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001250
John Harrison5f19e2b2015-05-29 17:43:27 +01001251 WARN(params->ctx->ppgtt && params->ctx->ppgtt->pd_dirty_rings & (1<<ring->id),
Daniel Vetter92588112015-04-14 17:35:19 +02001252 "%s didn't clear reload\n", ring->name);
Ben Widawsky563222a2015-03-19 12:53:28 +00001253
Oscar Mateo78382592014-07-03 16:28:05 +01001254 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
1255 instp_mask = I915_EXEC_CONSTANTS_MASK;
1256 switch (instp_mode) {
1257 case I915_EXEC_CONSTANTS_REL_GENERAL:
1258 case I915_EXEC_CONSTANTS_ABSOLUTE:
1259 case I915_EXEC_CONSTANTS_REL_SURFACE:
1260 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
1261 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001262 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001263 }
1264
1265 if (instp_mode != dev_priv->relative_constants_mode) {
1266 if (INTEL_INFO(dev)->gen < 4) {
1267 DRM_DEBUG("no rel constants on pre-gen4\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001268 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001269 }
1270
1271 if (INTEL_INFO(dev)->gen > 5 &&
1272 instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
1273 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
Chris Wilson2f5945b2015-10-06 11:39:55 +01001274 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001275 }
1276
1277 /* The HW changed the meaning on this bit on gen6 */
1278 if (INTEL_INFO(dev)->gen >= 6)
1279 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
1280 }
1281 break;
1282 default:
1283 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
Chris Wilson2f5945b2015-10-06 11:39:55 +01001284 return -EINVAL;
Oscar Mateo78382592014-07-03 16:28:05 +01001285 }
1286
1287 if (ring == &dev_priv->ring[RCS] &&
Chris Wilson2f5945b2015-10-06 11:39:55 +01001288 instp_mode != dev_priv->relative_constants_mode) {
John Harrison5fb9de12015-05-29 17:44:07 +01001289 ret = intel_ring_begin(params->request, 4);
Oscar Mateo78382592014-07-03 16:28:05 +01001290 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001291 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001292
1293 intel_ring_emit(ring, MI_NOOP);
1294 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +02001295 intel_ring_emit_reg(ring, INSTPM);
Oscar Mateo78382592014-07-03 16:28:05 +01001296 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
1297 intel_ring_advance(ring);
1298
1299 dev_priv->relative_constants_mode = instp_mode;
1300 }
1301
1302 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
John Harrison2f200552015-05-29 17:43:53 +01001303 ret = i915_reset_gen7_sol_offsets(dev, params->request);
Oscar Mateo78382592014-07-03 16:28:05 +01001304 if (ret)
Chris Wilson2f5945b2015-10-06 11:39:55 +01001305 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001306 }
1307
John Harrison5f19e2b2015-05-29 17:43:27 +01001308 exec_len = args->batch_len;
1309 exec_start = params->batch_obj_vm_offset +
1310 params->args_batch_start_offset;
1311
Chris Wilson2f5945b2015-10-06 11:39:55 +01001312 ret = ring->dispatch_execbuffer(params->request,
1313 exec_start, exec_len,
1314 params->dispatch_flags);
1315 if (ret)
1316 return ret;
Oscar Mateo78382592014-07-03 16:28:05 +01001317
John Harrison95c24162015-05-29 17:43:31 +01001318 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
Oscar Mateo78382592014-07-03 16:28:05 +01001319
John Harrison8a8edb52015-05-29 17:43:33 +01001320 i915_gem_execbuffer_move_to_active(vmas, params->request);
John Harrisonadeca762015-05-29 17:43:28 +01001321 i915_gem_execbuffer_retire_commands(params);
Oscar Mateo78382592014-07-03 16:28:05 +01001322
Chris Wilson2f5945b2015-10-06 11:39:55 +01001323 return 0;
Oscar Mateo78382592014-07-03 16:28:05 +01001324}
1325
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001326/**
1327 * Find one BSD ring to dispatch the corresponding BSD command.
1328 * The Ring ID is returned.
1329 */
1330static int gen8_dispatch_bsd_ring(struct drm_device *dev,
1331 struct drm_file *file)
1332{
1333 struct drm_i915_private *dev_priv = dev->dev_private;
1334 struct drm_i915_file_private *file_priv = file->driver_priv;
1335
1336 /* Check whether the file_priv is using one ring */
1337 if (file_priv->bsd_ring)
1338 return file_priv->bsd_ring->id;
1339 else {
1340 /* If no, use the ping-pong mechanism to select one ring */
1341 int ring_id;
1342
1343 mutex_lock(&dev->struct_mutex);
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001344 if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001345 ring_id = VCS;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001346 dev_priv->mm.bsd_ring_dispatch_index = 1;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001347 } else {
1348 ring_id = VCS2;
Daniel Vetterbdf1e7e2014-05-21 17:37:52 +02001349 dev_priv->mm.bsd_ring_dispatch_index = 0;
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001350 }
1351 file_priv->bsd_ring = &dev_priv->ring[ring_id];
1352 mutex_unlock(&dev->struct_mutex);
1353 return ring_id;
1354 }
1355}
1356
Chris Wilsond23db882014-05-23 08:48:08 +02001357static struct drm_i915_gem_object *
1358eb_get_batch(struct eb_vmas *eb)
1359{
1360 struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
1361
1362 /*
1363 * SNA is doing fancy tricks with compressing batch buffers, which leads
1364 * to negative relocation deltas. Usually that works out ok since the
1365 * relocate address is still positive, except when the batch is placed
1366 * very low in the GTT. Ensure this doesn't happen.
1367 *
1368 * Note that actual hangs have only been observed on gen7, but for
1369 * paranoia do it everywhere.
1370 */
Chris Wilson506a8e82015-12-08 11:55:07 +00001371 if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
1372 vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
Chris Wilsond23db882014-05-23 08:48:08 +02001373
1374 return vma->obj;
1375}
1376
Eric Anholtae662d32012-01-03 09:23:29 -08001377static int
Chris Wilson54cf91d2010-11-25 18:00:26 +00001378i915_gem_do_execbuffer(struct drm_device *dev, void *data,
1379 struct drm_file *file,
1380 struct drm_i915_gem_execbuffer2 *args,
Ben Widawsky41bde552013-12-06 14:11:21 -08001381 struct drm_i915_gem_exec_object2 *exec)
Chris Wilson54cf91d2010-11-25 18:00:26 +00001382{
Jani Nikula50227e12014-03-31 14:27:21 +03001383 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky27173f12013-08-14 11:38:36 +02001384 struct eb_vmas *eb;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001385 struct drm_i915_gem_object *batch_obj;
Brad Volkin78a42372014-12-11 12:13:09 -08001386 struct drm_i915_gem_exec_object2 shadow_exec_entry;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001387 struct intel_engine_cs *ring;
Oscar Mateo273497e2014-05-22 14:13:37 +01001388 struct intel_context *ctx;
Ben Widawsky41bde552013-12-06 14:11:21 -08001389 struct i915_address_space *vm;
John Harrison5f19e2b2015-05-29 17:43:27 +01001390 struct i915_execbuffer_params params_master; /* XXX: will be removed later */
1391 struct i915_execbuffer_params *params = &params_master;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001392 const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
John Harrison8e004ef2015-02-13 11:48:10 +00001393 u32 dispatch_flags;
Oscar Mateo78382592014-07-03 16:28:05 +01001394 int ret;
Daniel Vettered5982e2013-01-17 22:23:36 +01001395 bool need_relocs;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001396
Daniel Vettered5982e2013-01-17 22:23:36 +01001397 if (!i915_gem_check_execbuffer(args))
Chris Wilson432e58e2010-11-25 19:32:06 +00001398 return -EINVAL;
Chris Wilson432e58e2010-11-25 19:32:06 +00001399
Chris Wilsonad19f102014-08-10 06:29:08 +01001400 ret = validate_exec_list(dev, exec, args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001401 if (ret)
1402 return ret;
1403
John Harrison8e004ef2015-02-13 11:48:10 +00001404 dispatch_flags = 0;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001405 if (args->flags & I915_EXEC_SECURE) {
1406 if (!file->is_master || !capable(CAP_SYS_ADMIN))
1407 return -EPERM;
1408
John Harrison8e004ef2015-02-13 11:48:10 +00001409 dispatch_flags |= I915_DISPATCH_SECURE;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001410 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001411 if (args->flags & I915_EXEC_IS_PINNED)
John Harrison8e004ef2015-02-13 11:48:10 +00001412 dispatch_flags |= I915_DISPATCH_PINNED;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001413
Zhao Yakuib1a93302014-04-17 10:37:36 +08001414 if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
Daniel Vetterff240192012-01-31 21:08:14 +01001415 DRM_DEBUG("execbuf with unknown ring: %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001416 (int)(args->flags & I915_EXEC_RING_MASK));
1417 return -EINVAL;
1418 }
Ben Widawskyca01b122013-12-06 14:11:00 -08001419
Zhipeng Gong8d360df2015-01-13 08:48:24 +08001420 if (((args->flags & I915_EXEC_RING_MASK) != I915_EXEC_BSD) &&
1421 ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
1422 DRM_DEBUG("execbuf with non bsd ring but with invalid "
1423 "bsd dispatch flags: %d\n", (int)(args->flags));
1424 return -EINVAL;
1425 }
1426
Ben Widawskyca01b122013-12-06 14:11:00 -08001427 if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
1428 ring = &dev_priv->ring[RCS];
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001429 else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
1430 if (HAS_BSD2(dev)) {
1431 int ring_id;
Zhipeng Gong8d360df2015-01-13 08:48:24 +08001432
1433 switch (args->flags & I915_EXEC_BSD_MASK) {
1434 case I915_EXEC_BSD_DEFAULT:
1435 ring_id = gen8_dispatch_bsd_ring(dev, file);
1436 ring = &dev_priv->ring[ring_id];
1437 break;
1438 case I915_EXEC_BSD_RING1:
1439 ring = &dev_priv->ring[VCS];
1440 break;
1441 case I915_EXEC_BSD_RING2:
1442 ring = &dev_priv->ring[VCS2];
1443 break;
1444 default:
1445 DRM_DEBUG("execbuf with unknown bsd ring: %d\n",
1446 (int)(args->flags & I915_EXEC_BSD_MASK));
1447 return -EINVAL;
1448 }
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001449 } else
1450 ring = &dev_priv->ring[VCS];
1451 } else
Ben Widawskyca01b122013-12-06 14:11:00 -08001452 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
1453
Chris Wilsona15817c2012-05-11 14:29:31 +01001454 if (!intel_ring_initialized(ring)) {
1455 DRM_DEBUG("execbuf with invalid ring: %d\n",
1456 (int)(args->flags & I915_EXEC_RING_MASK));
1457 return -EINVAL;
1458 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001459
1460 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001461 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001462 return -EINVAL;
1463 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001464
Abdiel Janulguea9ed33c2015-07-01 10:12:23 +03001465 if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
1466 if (!HAS_RESOURCE_STREAMER(dev)) {
1467 DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
1468 return -EINVAL;
1469 }
1470 if (ring->id != RCS) {
1471 DRM_DEBUG("RS is not available on %s\n",
1472 ring->name);
1473 return -EINVAL;
1474 }
1475
1476 dispatch_flags |= I915_DISPATCH_RS;
1477 }
1478
Paulo Zanonif65c9162013-11-27 18:20:34 -02001479 intel_runtime_pm_get(dev_priv);
1480
Chris Wilson54cf91d2010-11-25 18:00:26 +00001481 ret = i915_mutex_lock_interruptible(dev);
1482 if (ret)
1483 goto pre_mutex_err;
1484
Daniel Vetter7c9c4b82013-12-18 16:37:49 +01001485 ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
Ben Widawsky72ad5c42014-01-02 19:50:27 -10001486 if (IS_ERR(ctx)) {
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001487 mutex_unlock(&dev->struct_mutex);
Ben Widawsky41bde552013-12-06 14:11:21 -08001488 ret = PTR_ERR(ctx);
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001489 goto pre_mutex_err;
Ben Widawsky935f38d2014-04-04 22:41:07 -07001490 }
Ben Widawsky41bde552013-12-06 14:11:21 -08001491
1492 i915_gem_context_reference(ctx);
1493
Daniel Vetterae6c4802014-08-06 15:04:53 +02001494 if (ctx->ppgtt)
1495 vm = &ctx->ppgtt->base;
1496 else
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001497 vm = &dev_priv->gtt.base;
Mika Kuoppalad299cce2013-11-26 16:14:33 +02001498
John Harrison5f19e2b2015-05-29 17:43:27 +01001499 memset(&params_master, 0x00, sizeof(params_master));
1500
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001501 eb = eb_create(args);
Chris Wilson67731b82010-12-08 10:38:14 +00001502 if (eb == NULL) {
Ben Widawsky935f38d2014-04-04 22:41:07 -07001503 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001504 mutex_unlock(&dev->struct_mutex);
1505 ret = -ENOMEM;
1506 goto pre_mutex_err;
1507 }
1508
Chris Wilson54cf91d2010-11-25 18:00:26 +00001509 /* Look up object handles */
Ben Widawsky27173f12013-08-14 11:38:36 +02001510 ret = eb_lookup_vmas(eb, exec, args, vm, file);
Chris Wilson3b96eff2013-01-08 10:53:14 +00001511 if (ret)
1512 goto err;
Chris Wilson54cf91d2010-11-25 18:00:26 +00001513
Chris Wilson6fe4f142011-01-10 17:35:37 +00001514 /* take note of the batch buffer before we might reorder the lists */
Chris Wilsond23db882014-05-23 08:48:08 +02001515 batch_obj = eb_get_batch(eb);
Chris Wilson6fe4f142011-01-10 17:35:37 +00001516
Chris Wilson54cf91d2010-11-25 18:00:26 +00001517 /* Move the objects en-masse into the GTT, evicting if necessary. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001518 need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
David Weinehallb1b38272015-05-20 17:00:13 +03001519 ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, ctx, &need_relocs);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001520 if (ret)
1521 goto err;
1522
1523 /* The objects are in their final locations, apply the relocations. */
Daniel Vettered5982e2013-01-17 22:23:36 +01001524 if (need_relocs)
Ben Widawsky17601cbc2013-11-25 09:54:38 -08001525 ret = i915_gem_execbuffer_relocate(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001526 if (ret) {
1527 if (ret == -EFAULT) {
Daniel Vettered5982e2013-01-17 22:23:36 +01001528 ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
David Weinehallb1b38272015-05-20 17:00:13 +03001529 eb, exec, ctx);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001530 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1531 }
1532 if (ret)
1533 goto err;
1534 }
1535
1536 /* Set the pending read domains for the batch buffer to COMMAND */
Chris Wilson54cf91d2010-11-25 18:00:26 +00001537 if (batch_obj->base.pending_write_domain) {
Daniel Vetterff240192012-01-31 21:08:14 +01001538 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
Chris Wilson54cf91d2010-11-25 18:00:26 +00001539 ret = -EINVAL;
1540 goto err;
1541 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001542
John Harrison5f19e2b2015-05-29 17:43:27 +01001543 params->args_batch_start_offset = args->batch_start_offset;
Chris Wilson743e78c2015-03-27 11:02:10 +00001544 if (i915_needs_cmd_parser(ring) && args->batch_len) {
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001545 struct drm_i915_gem_object *parsed_batch_obj;
1546
1547 parsed_batch_obj = i915_gem_execbuffer_parse(ring,
Brad Volkin71745372014-12-11 12:13:12 -08001548 &shadow_exec_entry,
1549 eb,
1550 batch_obj,
1551 args->batch_start_offset,
1552 args->batch_len,
Chris Wilson17cabf52015-01-14 11:20:57 +00001553 file->is_master);
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001554 if (IS_ERR(parsed_batch_obj)) {
1555 ret = PTR_ERR(parsed_batch_obj);
Brad Volkin78a42372014-12-11 12:13:09 -08001556 goto err;
1557 }
Chris Wilson17cabf52015-01-14 11:20:57 +00001558
1559 /*
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001560 * parsed_batch_obj == batch_obj means batch not fully parsed:
1561 * Accept, but don't promote to secure.
Chris Wilson17cabf52015-01-14 11:20:57 +00001562 */
Chris Wilson17cabf52015-01-14 11:20:57 +00001563
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001564 if (parsed_batch_obj != batch_obj) {
1565 /*
1566 * Batch parsed and accepted:
1567 *
1568 * Set the DISPATCH_SECURE bit to remove the NON_SECURE
1569 * bit from MI_BATCH_BUFFER_START commands issued in
1570 * the dispatch_execbuffer implementations. We
1571 * specifically don't want that set on batches the
1572 * command parser has accepted.
1573 */
1574 dispatch_flags |= I915_DISPATCH_SECURE;
John Harrison5f19e2b2015-05-29 17:43:27 +01001575 params->args_batch_start_offset = 0;
Rebecca N. Palmerc7c73722015-05-08 14:26:50 +01001576 batch_obj = parsed_batch_obj;
1577 }
Brad Volkin351e3db2014-02-18 10:15:46 -08001578 }
1579
Brad Volkin78a42372014-12-11 12:13:09 -08001580 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1581
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001582 /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1583 * batch" bit. Hence we need to pin secure batches into the global gtt.
Ben Widawsky28cf5412013-11-02 21:07:26 -07001584 * hsw should have this fixed, but bdw mucks it up again. */
John Harrison8e004ef2015-02-13 11:48:10 +00001585 if (dispatch_flags & I915_DISPATCH_SECURE) {
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001586 /*
1587 * So on first glance it looks freaky that we pin the batch here
1588 * outside of the reservation loop. But:
1589 * - The batch is already pinned into the relevant ppgtt, so we
1590 * already have the backing storage fully allocated.
1591 * - No other BO uses the global gtt (well contexts, but meh),
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01001592 * so we don't really have issues with multiple objects not
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001593 * fitting due to fragmentation.
1594 * So this is actually safe.
1595 */
1596 ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
1597 if (ret)
1598 goto err;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001599
John Harrison5f19e2b2015-05-29 17:43:27 +01001600 params->batch_obj_vm_offset = i915_gem_obj_ggtt_offset(batch_obj);
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001601 } else
John Harrison5f19e2b2015-05-29 17:43:27 +01001602 params->batch_obj_vm_offset = i915_gem_obj_offset(batch_obj, vm);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001603
John Harrison0c8dac82015-05-29 17:43:25 +01001604 /* Allocate a request for this batch buffer nice and early. */
John Harrison6a6ae792015-05-29 17:43:30 +01001605 ret = i915_gem_request_alloc(ring, ctx, &params->request);
John Harrison0c8dac82015-05-29 17:43:25 +01001606 if (ret)
1607 goto err_batch_unpin;
1608
John Harrisonfcfa423c2015-05-29 17:44:12 +01001609 ret = i915_gem_request_add_to_client(params->request, file);
1610 if (ret)
1611 goto err_batch_unpin;
1612
John Harrison5f19e2b2015-05-29 17:43:27 +01001613 /*
1614 * Save assorted stuff away to pass through to *_submission().
1615 * NB: This data should be 'persistent' and not local as it will
1616 * kept around beyond the duration of the IOCTL once the GPU
1617 * scheduler arrives.
1618 */
1619 params->dev = dev;
1620 params->file = file;
1621 params->ring = ring;
1622 params->dispatch_flags = dispatch_flags;
1623 params->batch_obj = batch_obj;
1624 params->ctx = ctx;
1625
1626 ret = dev_priv->gt.execbuf_submit(params, args, &eb->vmas);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001627
John Harrison0c8dac82015-05-29 17:43:25 +01001628err_batch_unpin:
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001629 /*
1630 * FIXME: We crucially rely upon the active tracking for the (ppgtt)
1631 * batch vma for correctness. For less ugly and less fragility this
1632 * needs to be adjusted to also track the ggtt batch vma properly as
1633 * active.
1634 */
John Harrison8e004ef2015-02-13 11:48:10 +00001635 if (dispatch_flags & I915_DISPATCH_SECURE)
Daniel Vetterda51a1e2014-08-11 12:08:58 +02001636 i915_gem_object_ggtt_unpin(batch_obj);
John Harrison0c8dac82015-05-29 17:43:25 +01001637
Chris Wilson54cf91d2010-11-25 18:00:26 +00001638err:
Ben Widawsky41bde552013-12-06 14:11:21 -08001639 /* the request owns the ref now */
1640 i915_gem_context_unreference(ctx);
Chris Wilson67731b82010-12-08 10:38:14 +00001641 eb_destroy(eb);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001642
John Harrison6a6ae792015-05-29 17:43:30 +01001643 /*
1644 * If the request was created but not successfully submitted then it
1645 * must be freed again. If it was submitted then it is being tracked
1646 * on the active request list and no clean up is required here.
1647 */
John Harrisonbccca492015-05-29 17:44:11 +01001648 if (ret && params->request)
John Harrison6a6ae792015-05-29 17:43:30 +01001649 i915_gem_request_cancel(params->request);
John Harrison6a6ae792015-05-29 17:43:30 +01001650
Chris Wilson54cf91d2010-11-25 18:00:26 +00001651 mutex_unlock(&dev->struct_mutex);
1652
1653pre_mutex_err:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001654 /* intel_gpu_busy should also get a ref, so it will free when the device
1655 * is really idle. */
1656 intel_runtime_pm_put(dev_priv);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001657 return ret;
1658}
1659
1660/*
1661 * Legacy execbuffer just creates an exec2 list from the original exec object
1662 * list array and passes it to the real function.
1663 */
1664int
1665i915_gem_execbuffer(struct drm_device *dev, void *data,
1666 struct drm_file *file)
1667{
1668 struct drm_i915_gem_execbuffer *args = data;
1669 struct drm_i915_gem_execbuffer2 exec2;
1670 struct drm_i915_gem_exec_object *exec_list = NULL;
1671 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1672 int ret, i;
1673
Chris Wilson54cf91d2010-11-25 18:00:26 +00001674 if (args->buffer_count < 1) {
Daniel Vetterff240192012-01-31 21:08:14 +01001675 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001676 return -EINVAL;
1677 }
1678
1679 /* Copy in the exec list from userland */
1680 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1681 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1682 if (exec_list == NULL || exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001683 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001684 args->buffer_count);
1685 drm_free_large(exec_list);
1686 drm_free_large(exec2_list);
1687 return -ENOMEM;
1688 }
1689 ret = copy_from_user(exec_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001690 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001691 sizeof(*exec_list) * args->buffer_count);
1692 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001693 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001694 args->buffer_count, ret);
1695 drm_free_large(exec_list);
1696 drm_free_large(exec2_list);
1697 return -EFAULT;
1698 }
1699
1700 for (i = 0; i < args->buffer_count; i++) {
1701 exec2_list[i].handle = exec_list[i].handle;
1702 exec2_list[i].relocation_count = exec_list[i].relocation_count;
1703 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1704 exec2_list[i].alignment = exec_list[i].alignment;
1705 exec2_list[i].offset = exec_list[i].offset;
1706 if (INTEL_INFO(dev)->gen < 4)
1707 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1708 else
1709 exec2_list[i].flags = 0;
1710 }
1711
1712 exec2.buffers_ptr = args->buffers_ptr;
1713 exec2.buffer_count = args->buffer_count;
1714 exec2.batch_start_offset = args->batch_start_offset;
1715 exec2.batch_len = args->batch_len;
1716 exec2.DR1 = args->DR1;
1717 exec2.DR4 = args->DR4;
1718 exec2.num_cliprects = args->num_cliprects;
1719 exec2.cliprects_ptr = args->cliprects_ptr;
1720 exec2.flags = I915_EXEC_RENDER;
Ben Widawsky6e0a69d2012-06-04 14:42:55 -07001721 i915_execbuffer2_set_context_id(exec2, 0);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001722
Ben Widawsky41bde552013-12-06 14:11:21 -08001723 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001724 if (!ret) {
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001725 struct drm_i915_gem_exec_object __user *user_exec_list =
1726 to_user_ptr(args->buffers_ptr);
1727
Chris Wilson54cf91d2010-11-25 18:00:26 +00001728 /* Copy the new buffer offsets back to the user's exec list. */
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001729 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarskia5f0edf2015-12-29 18:24:52 +01001730 exec2_list[i].offset =
1731 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001732 ret = __copy_to_user(&user_exec_list[i].offset,
1733 &exec2_list[i].offset,
1734 sizeof(user_exec_list[i].offset));
1735 if (ret) {
1736 ret = -EFAULT;
1737 DRM_DEBUG("failed to copy %d exec entries "
1738 "back to user (%d)\n",
1739 args->buffer_count, ret);
1740 break;
1741 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001742 }
1743 }
1744
1745 drm_free_large(exec_list);
1746 drm_free_large(exec2_list);
1747 return ret;
1748}
1749
1750int
1751i915_gem_execbuffer2(struct drm_device *dev, void *data,
1752 struct drm_file *file)
1753{
1754 struct drm_i915_gem_execbuffer2 *args = data;
1755 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1756 int ret;
1757
Xi Wanged8cd3b2012-04-23 04:06:41 -04001758 if (args->buffer_count < 1 ||
1759 args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
Daniel Vetterff240192012-01-31 21:08:14 +01001760 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001761 return -EINVAL;
1762 }
1763
Daniel Vetter9cb34662014-04-24 08:09:11 +02001764 if (args->rsvd2 != 0) {
1765 DRM_DEBUG("dirty rvsd2 field\n");
1766 return -EINVAL;
1767 }
1768
Chris Wilson8408c282011-02-21 12:54:48 +00001769 exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
Chris Wilson419fa722013-01-08 10:53:13 +00001770 GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
Chris Wilson8408c282011-02-21 12:54:48 +00001771 if (exec2_list == NULL)
1772 exec2_list = drm_malloc_ab(sizeof(*exec2_list),
1773 args->buffer_count);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001774 if (exec2_list == NULL) {
Daniel Vetterff240192012-01-31 21:08:14 +01001775 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001776 args->buffer_count);
1777 return -ENOMEM;
1778 }
1779 ret = copy_from_user(exec2_list,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001780 to_user_ptr(args->buffers_ptr),
Chris Wilson54cf91d2010-11-25 18:00:26 +00001781 sizeof(*exec2_list) * args->buffer_count);
1782 if (ret != 0) {
Daniel Vetterff240192012-01-31 21:08:14 +01001783 DRM_DEBUG("copy %d exec entries failed %d\n",
Chris Wilson54cf91d2010-11-25 18:00:26 +00001784 args->buffer_count, ret);
1785 drm_free_large(exec2_list);
1786 return -EFAULT;
1787 }
1788
Ben Widawsky41bde552013-12-06 14:11:21 -08001789 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
Chris Wilson54cf91d2010-11-25 18:00:26 +00001790 if (!ret) {
1791 /* Copy the new buffer offsets back to the user's exec list. */
Ville Syrjäläd593d992014-06-13 16:42:51 +03001792 struct drm_i915_gem_exec_object2 __user *user_exec_list =
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001793 to_user_ptr(args->buffers_ptr);
1794 int i;
1795
1796 for (i = 0; i < args->buffer_count; i++) {
Michał Winiarskia5f0edf2015-12-29 18:24:52 +01001797 exec2_list[i].offset =
1798 gen8_canonical_addr(exec2_list[i].offset);
Chris Wilson9aab8bf2014-05-23 10:45:52 +01001799 ret = __copy_to_user(&user_exec_list[i].offset,
1800 &exec2_list[i].offset,
1801 sizeof(user_exec_list[i].offset));
1802 if (ret) {
1803 ret = -EFAULT;
1804 DRM_DEBUG("failed to copy %d exec entries "
1805 "back to user\n",
1806 args->buffer_count);
1807 break;
1808 }
Chris Wilson54cf91d2010-11-25 18:00:26 +00001809 }
1810 }
1811
1812 drm_free_large(exec2_list);
1813 return ret;
1814}