Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 29 | #include <drm/drmP.h> |
| 30 | #include <drm/i915_drm.h> |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 31 | #include "i915_drv.h" |
| 32 | #include "i915_trace.h" |
| 33 | #include "intel_drv.h" |
Eugeni Dodonov | f45b555 | 2011-12-09 17:16:37 -0800 | [diff] [blame] | 34 | #include <linux/dma_remapping.h> |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 35 | |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 36 | #define __EXEC_OBJECT_HAS_PIN (1<<31) |
| 37 | #define __EXEC_OBJECT_HAS_FENCE (1<<30) |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 38 | #define __EXEC_OBJECT_NEEDS_MAP (1<<29) |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 39 | #define __EXEC_OBJECT_NEEDS_BIAS (1<<28) |
Brad Volkin | 0079a7d | 2014-12-11 12:13:11 -0800 | [diff] [blame] | 40 | #define __EXEC_OBJECT_PURGEABLE (1<<27) |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 41 | |
| 42 | #define BATCH_OFFSET_BIAS (256*1024) |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 43 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 44 | struct eb_vmas { |
| 45 | struct list_head vmas; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 46 | int and; |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 47 | union { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 48 | struct i915_vma *lut[0]; |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 49 | struct hlist_head buckets[0]; |
| 50 | }; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 53 | static struct eb_vmas * |
Ben Widawsky | 17601cbc | 2013-11-25 09:54:38 -0800 | [diff] [blame] | 54 | eb_create(struct drm_i915_gem_execbuffer2 *args) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 55 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 56 | struct eb_vmas *eb = NULL; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 57 | |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 58 | if (args->flags & I915_EXEC_HANDLE_LUT) { |
Daniel Vetter | b205ca5 | 2013-09-19 14:00:11 +0200 | [diff] [blame] | 59 | unsigned size = args->buffer_count; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 60 | size *= sizeof(struct i915_vma *); |
| 61 | size += sizeof(struct eb_vmas); |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 62 | eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY); |
| 63 | } |
| 64 | |
| 65 | if (eb == NULL) { |
Daniel Vetter | b205ca5 | 2013-09-19 14:00:11 +0200 | [diff] [blame] | 66 | unsigned size = args->buffer_count; |
| 67 | unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2; |
Lauri Kasanen | 27b7c63 | 2013-03-27 15:04:55 +0200 | [diff] [blame] | 68 | BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head)); |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 69 | while (count > 2*size) |
| 70 | count >>= 1; |
| 71 | eb = kzalloc(count*sizeof(struct hlist_head) + |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 72 | sizeof(struct eb_vmas), |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 73 | 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 Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 81 | INIT_LIST_HEAD(&eb->vmas); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 82 | return eb; |
| 83 | } |
| 84 | |
| 85 | static void |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 86 | eb_reset(struct eb_vmas *eb) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 87 | { |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 88 | if (eb->and >= 0) |
| 89 | memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head)); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 92 | static int |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 93 | eb_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 Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 98 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 99 | struct drm_i915_gem_object *obj; |
| 100 | struct list_head objects; |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 101 | int i, ret; |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 102 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 103 | INIT_LIST_HEAD(&objects); |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 104 | spin_lock(&file->table_lock); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 105 | /* Grab a reference to the object and release the lock so we can lookup |
| 106 | * or create the VMA without using GFP_ATOMIC */ |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 107 | for (i = 0; i < args->buffer_count; i++) { |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 108 | 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 Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 113 | ret = -ENOENT; |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 114 | goto err; |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 117 | if (!list_empty(&obj->obj_exec_link)) { |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 118 | 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 Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 121 | ret = -EINVAL; |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 122 | goto err; |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | drm_gem_object_reference(&obj->base); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 126 | list_add_tail(&obj->obj_exec_link, &objects); |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 127 | } |
| 128 | spin_unlock(&file->table_lock); |
| 129 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 130 | i = 0; |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 131 | while (!list_empty(&objects)) { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 132 | struct i915_vma *vma; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 133 | |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 134 | obj = list_first_entry(&objects, |
| 135 | struct drm_i915_gem_object, |
| 136 | obj_exec_link); |
| 137 | |
Daniel Vetter | e656a6c | 2013-08-14 14:14:04 +0200 | [diff] [blame] | 138 | /* |
| 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 Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame] | 146 | vma = i915_gem_obj_lookup_or_create_vma(obj, vm); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 147 | if (IS_ERR(vma)) { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 148 | DRM_DEBUG("Failed to lookup VMA\n"); |
| 149 | ret = PTR_ERR(vma); |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 150 | goto err; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 153 | /* Transfer ownership from the objects list to the vmas list. */ |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 154 | list_add_tail(&vma->exec_list, &eb->vmas); |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 155 | list_del_init(&obj->obj_exec_link); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 156 | |
| 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 Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 169 | return 0; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 170 | |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 171 | |
| 172 | err: |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 173 | 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 Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 178 | drm_gem_object_unreference(&obj->base); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 179 | } |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 180 | /* |
| 181 | * Objects already transfered to the vmas list will be unreferenced by |
| 182 | * eb_destroy. |
| 183 | */ |
| 184 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 185 | return ret; |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 188 | static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 189 | { |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 190 | 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 Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 197 | |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 198 | head = &eb->buckets[handle & eb->and]; |
| 199 | hlist_for_each(node, head) { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 200 | struct i915_vma *vma; |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 201 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 202 | vma = hlist_entry(node, struct i915_vma, exec_node); |
| 203 | if (vma->exec_handle == handle) |
| 204 | return vma; |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 205 | } |
| 206 | return NULL; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 207 | } |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 210 | static void |
| 211 | i915_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 Vetter | 3d7f0f9 | 2013-12-18 16:23:37 +0100 | [diff] [blame] | 225 | vma->pin_count--; |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 226 | |
Brad Volkin | 0079a7d | 2014-12-11 12:13:11 -0800 | [diff] [blame] | 227 | if (entry->flags & __EXEC_OBJECT_PURGEABLE) |
| 228 | obj->madv = I915_MADV_DONTNEED; |
| 229 | |
| 230 | entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | |
| 231 | __EXEC_OBJECT_HAS_PIN | |
| 232 | __EXEC_OBJECT_PURGEABLE); |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static void eb_destroy(struct eb_vmas *eb) |
| 236 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 237 | while (!list_empty(&eb->vmas)) { |
| 238 | struct i915_vma *vma; |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 239 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 240 | vma = list_first_entry(&eb->vmas, |
| 241 | struct i915_vma, |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 242 | exec_list); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 243 | list_del_init(&vma->exec_list); |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 244 | i915_gem_execbuffer_unreserve_vma(vma); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 245 | drm_gem_object_unreference(&vma->obj->base); |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 246 | } |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 247 | kfree(eb); |
| 248 | } |
| 249 | |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 250 | static inline int use_cpu_reloc(struct drm_i915_gem_object *obj) |
| 251 | { |
Chris Wilson | 2cc86b8 | 2013-08-26 19:51:00 -0300 | [diff] [blame] | 252 | return (HAS_LLC(obj->base.dev) || |
| 253 | obj->base.write_domain == I915_GEM_DOMAIN_CPU || |
Chris Wilson | 504c726 | 2012-08-23 13:12:52 +0100 | [diff] [blame] | 254 | !obj->map_and_fenceable || |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 255 | obj->cache_level != I915_CACHE_NONE); |
| 256 | } |
| 257 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 258 | static int |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 259 | relocate_entry_cpu(struct drm_i915_gem_object *obj, |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 260 | struct drm_i915_gem_relocation_entry *reloc, |
| 261 | uint64_t target_offset) |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 262 | { |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 263 | struct drm_device *dev = obj->base.dev; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 264 | uint32_t page_offset = offset_in_page(reloc->offset); |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 265 | uint64_t delta = reloc->delta + target_offset; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 266 | char *vaddr; |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 267 | int ret; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 268 | |
Chris Wilson | 2cc86b8 | 2013-08-26 19:51:00 -0300 | [diff] [blame] | 269 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 270 | if (ret) |
| 271 | return ret; |
| 272 | |
| 273 | vaddr = kmap_atomic(i915_gem_object_get_page(obj, |
| 274 | reloc->offset >> PAGE_SHIFT)); |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 275 | *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta); |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 276 | |
| 277 | if (INTEL_INFO(dev)->gen >= 8) { |
| 278 | page_offset = offset_in_page(page_offset + sizeof(uint32_t)); |
| 279 | |
| 280 | if (page_offset == 0) { |
| 281 | kunmap_atomic(vaddr); |
| 282 | vaddr = kmap_atomic(i915_gem_object_get_page(obj, |
| 283 | (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT)); |
| 284 | } |
| 285 | |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 286 | *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta); |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 289 | kunmap_atomic(vaddr); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int |
| 295 | relocate_entry_gtt(struct drm_i915_gem_object *obj, |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 296 | struct drm_i915_gem_relocation_entry *reloc, |
| 297 | uint64_t target_offset) |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 298 | { |
| 299 | struct drm_device *dev = obj->base.dev; |
| 300 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 301 | uint64_t delta = reloc->delta + target_offset; |
Chris Wilson | 906843c | 2014-08-10 06:29:11 +0100 | [diff] [blame] | 302 | uint64_t offset; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 303 | void __iomem *reloc_page; |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 304 | int ret; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 305 | |
| 306 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 307 | if (ret) |
| 308 | return ret; |
| 309 | |
| 310 | ret = i915_gem_object_put_fence(obj); |
| 311 | if (ret) |
| 312 | return ret; |
| 313 | |
| 314 | /* Map the page containing the relocation we're going to perform. */ |
Chris Wilson | 906843c | 2014-08-10 06:29:11 +0100 | [diff] [blame] | 315 | offset = i915_gem_obj_ggtt_offset(obj); |
| 316 | offset += reloc->offset; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 317 | reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable, |
Chris Wilson | 906843c | 2014-08-10 06:29:11 +0100 | [diff] [blame] | 318 | offset & PAGE_MASK); |
| 319 | iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset)); |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 320 | |
| 321 | if (INTEL_INFO(dev)->gen >= 8) { |
Chris Wilson | 906843c | 2014-08-10 06:29:11 +0100 | [diff] [blame] | 322 | offset += sizeof(uint32_t); |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 323 | |
Chris Wilson | 906843c | 2014-08-10 06:29:11 +0100 | [diff] [blame] | 324 | if (offset_in_page(offset) == 0) { |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 325 | io_mapping_unmap_atomic(reloc_page); |
Chris Wilson | 906843c | 2014-08-10 06:29:11 +0100 | [diff] [blame] | 326 | reloc_page = |
| 327 | io_mapping_map_atomic_wc(dev_priv->gtt.mappable, |
| 328 | offset); |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Chris Wilson | 906843c | 2014-08-10 06:29:11 +0100 | [diff] [blame] | 331 | iowrite32(upper_32_bits(delta), |
| 332 | reloc_page + offset_in_page(offset)); |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 335 | io_mapping_unmap_atomic(reloc_page); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 341 | i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj, |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 342 | struct eb_vmas *eb, |
Ben Widawsky | 3e7a032 | 2013-12-06 14:10:57 -0800 | [diff] [blame] | 343 | struct drm_i915_gem_relocation_entry *reloc) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 344 | { |
| 345 | struct drm_device *dev = obj->base.dev; |
| 346 | struct drm_gem_object *target_obj; |
Daniel Vetter | 149c840 | 2012-02-15 23:50:23 +0100 | [diff] [blame] | 347 | struct drm_i915_gem_object *target_i915_obj; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 348 | struct i915_vma *target_vma; |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 349 | uint64_t target_offset; |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 350 | int ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 351 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 352 | /* we've already hold a reference to all valid objects */ |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 353 | target_vma = eb_get_vma(eb, reloc->target_handle); |
| 354 | if (unlikely(target_vma == NULL)) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 355 | return -ENOENT; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 356 | target_i915_obj = target_vma->obj; |
| 357 | target_obj = &target_vma->obj->base; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 358 | |
Ben Widawsky | 5ce0972 | 2013-11-25 09:54:40 -0800 | [diff] [blame] | 359 | target_offset = target_vma->node.start; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 360 | |
Eric Anholt | e844b99 | 2012-07-31 15:35:01 -0700 | [diff] [blame] | 361 | /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and |
| 362 | * pipe_control writes because the gpu doesn't properly redirect them |
| 363 | * through the ppgtt for non_secure batchbuffers. */ |
| 364 | if (unlikely(IS_GEN6(dev) && |
| 365 | reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION && |
Tvrtko Ursulin | fe14d5f | 2014-12-10 17:27:58 +0000 | [diff] [blame] | 366 | !(target_vma->bound & GLOBAL_BIND))) { |
| 367 | ret = i915_vma_bind(target_vma, target_i915_obj->cache_level, |
| 368 | GLOBAL_BIND); |
| 369 | if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!")) |
| 370 | return ret; |
| 371 | } |
Eric Anholt | e844b99 | 2012-07-31 15:35:01 -0700 | [diff] [blame] | 372 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 373 | /* Validate that the target is in a valid r/w GPU domain */ |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 374 | if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 375 | DRM_DEBUG("reloc with multiple write domains: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 376 | "obj %p target %d offset %d " |
| 377 | "read %08x write %08x", |
| 378 | obj, reloc->target_handle, |
| 379 | (int) reloc->offset, |
| 380 | reloc->read_domains, |
| 381 | reloc->write_domain); |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 382 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 383 | } |
Daniel Vetter | 4ca4a25 | 2011-12-14 13:57:27 +0100 | [diff] [blame] | 384 | if (unlikely((reloc->write_domain | reloc->read_domains) |
| 385 | & ~I915_GEM_GPU_DOMAINS)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 386 | DRM_DEBUG("reloc with read/write non-GPU domains: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 387 | "obj %p target %d offset %d " |
| 388 | "read %08x write %08x", |
| 389 | obj, reloc->target_handle, |
| 390 | (int) reloc->offset, |
| 391 | reloc->read_domains, |
| 392 | reloc->write_domain); |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 393 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 394 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 395 | |
| 396 | target_obj->pending_read_domains |= reloc->read_domains; |
| 397 | target_obj->pending_write_domain |= reloc->write_domain; |
| 398 | |
| 399 | /* If the relocation already has the right value in it, no |
| 400 | * more work needs to be done. |
| 401 | */ |
| 402 | if (target_offset == reloc->presumed_offset) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 403 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 404 | |
| 405 | /* Check that the relocation address is valid... */ |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 406 | if (unlikely(reloc->offset > |
| 407 | obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 408 | DRM_DEBUG("Relocation beyond object bounds: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 409 | "obj %p target %d offset %d size %d.\n", |
| 410 | obj, reloc->target_handle, |
| 411 | (int) reloc->offset, |
| 412 | (int) obj->base.size); |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 413 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 414 | } |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 415 | if (unlikely(reloc->offset & 3)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 416 | DRM_DEBUG("Relocation not 4-byte aligned: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 417 | "obj %p target %d offset %d.\n", |
| 418 | obj, reloc->target_handle, |
| 419 | (int) reloc->offset); |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 420 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 423 | /* We can't wait for rendering with pagefaults disabled */ |
| 424 | if (obj->active && in_atomic()) |
| 425 | return -EFAULT; |
| 426 | |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 427 | if (use_cpu_reloc(obj)) |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 428 | ret = relocate_entry_cpu(obj, reloc, target_offset); |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 429 | else |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 430 | ret = relocate_entry_gtt(obj, reloc, target_offset); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 431 | |
Daniel Vetter | d4d3601 | 2013-09-02 20:56:23 +0200 | [diff] [blame] | 432 | if (ret) |
| 433 | return ret; |
| 434 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 435 | /* and update the user's relocation entry */ |
| 436 | reloc->presumed_offset = target_offset; |
| 437 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 438 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static int |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 442 | i915_gem_execbuffer_relocate_vma(struct i915_vma *vma, |
| 443 | struct eb_vmas *eb) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 444 | { |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 445 | #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry)) |
| 446 | struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)]; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 447 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 448 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 449 | int remain, ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 450 | |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 451 | user_relocs = to_user_ptr(entry->relocs_ptr); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 452 | |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 453 | remain = entry->relocation_count; |
| 454 | while (remain) { |
| 455 | struct drm_i915_gem_relocation_entry *r = stack_reloc; |
| 456 | int count = remain; |
| 457 | if (count > ARRAY_SIZE(stack_reloc)) |
| 458 | count = ARRAY_SIZE(stack_reloc); |
| 459 | remain -= count; |
| 460 | |
| 461 | if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0]))) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 462 | return -EFAULT; |
| 463 | |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 464 | do { |
| 465 | u64 offset = r->presumed_offset; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 466 | |
Ben Widawsky | 3e7a032 | 2013-12-06 14:10:57 -0800 | [diff] [blame] | 467 | ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r); |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 468 | if (ret) |
| 469 | return ret; |
| 470 | |
| 471 | if (r->presumed_offset != offset && |
| 472 | __copy_to_user_inatomic(&user_relocs->presumed_offset, |
| 473 | &r->presumed_offset, |
| 474 | sizeof(r->presumed_offset))) { |
| 475 | return -EFAULT; |
| 476 | } |
| 477 | |
| 478 | user_relocs++; |
| 479 | r++; |
| 480 | } while (--count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | return 0; |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 484 | #undef N_RELOC |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static int |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 488 | i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma, |
| 489 | struct eb_vmas *eb, |
| 490 | struct drm_i915_gem_relocation_entry *relocs) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 491 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 492 | const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 493 | int i, ret; |
| 494 | |
| 495 | for (i = 0; i < entry->relocation_count; i++) { |
Ben Widawsky | 3e7a032 | 2013-12-06 14:10:57 -0800 | [diff] [blame] | 496 | ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 497 | if (ret) |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | static int |
Ben Widawsky | 17601cbc | 2013-11-25 09:54:38 -0800 | [diff] [blame] | 505 | i915_gem_execbuffer_relocate(struct eb_vmas *eb) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 506 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 507 | struct i915_vma *vma; |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 508 | int ret = 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 509 | |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 510 | /* This is the fast path and we cannot handle a pagefault whilst |
| 511 | * holding the struct mutex lest the user pass in the relocations |
| 512 | * contained within a mmaped bo. For in such a case we, the page |
| 513 | * fault handler would call i915_gem_fault() and we would try to |
| 514 | * acquire the struct mutex again. Obviously this is bad and so |
| 515 | * lockdep complains vehemently. |
| 516 | */ |
| 517 | pagefault_disable(); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 518 | list_for_each_entry(vma, &eb->vmas, exec_list) { |
| 519 | ret = i915_gem_execbuffer_relocate_vma(vma, eb); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 520 | if (ret) |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 521 | break; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 522 | } |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 523 | pagefault_enable(); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 524 | |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 525 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 528 | static int |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 529 | i915_gem_execbuffer_reserve_vma(struct i915_vma *vma, |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 530 | struct intel_engine_cs *ring, |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 531 | bool *need_reloc) |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 532 | { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 533 | struct drm_i915_gem_object *obj = vma->obj; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 534 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 535 | uint64_t flags; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 536 | int ret; |
| 537 | |
Daniel Vetter | 1ec9e26 | 2014-02-14 14:01:11 +0100 | [diff] [blame] | 538 | flags = 0; |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 539 | if (entry->flags & __EXEC_OBJECT_NEEDS_MAP) |
Chris Wilson | c826c44 | 2014-10-31 13:53:53 +0000 | [diff] [blame] | 540 | flags |= PIN_GLOBAL | PIN_MAPPABLE; |
Daniel Vetter | 1ec9e26 | 2014-02-14 14:01:11 +0100 | [diff] [blame] | 541 | if (entry->flags & EXEC_OBJECT_NEEDS_GTT) |
Daniel Vetter | bf3d149 | 2014-02-14 14:01:12 +0100 | [diff] [blame] | 542 | flags |= PIN_GLOBAL; |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 543 | if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS) |
| 544 | flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS; |
Daniel Vetter | 1ec9e26 | 2014-02-14 14:01:11 +0100 | [diff] [blame] | 545 | |
| 546 | ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags); |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 547 | if (ret) |
| 548 | return ret; |
| 549 | |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 550 | entry->flags |= __EXEC_OBJECT_HAS_PIN; |
| 551 | |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 552 | if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) { |
| 553 | ret = i915_gem_object_get_fence(obj); |
| 554 | if (ret) |
| 555 | return ret; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 556 | |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 557 | if (i915_gem_object_pin_fence(obj)) |
| 558 | entry->flags |= __EXEC_OBJECT_HAS_FENCE; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 559 | } |
| 560 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 561 | if (entry->offset != vma->node.start) { |
| 562 | entry->offset = vma->node.start; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 563 | *need_reloc = true; |
| 564 | } |
| 565 | |
| 566 | if (entry->flags & EXEC_OBJECT_WRITE) { |
| 567 | obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER; |
| 568 | obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER; |
| 569 | } |
| 570 | |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 571 | return 0; |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 572 | } |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 573 | |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 574 | static bool |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 575 | need_reloc_mappable(struct i915_vma *vma) |
| 576 | { |
| 577 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
| 578 | |
| 579 | if (entry->relocation_count == 0) |
| 580 | return false; |
| 581 | |
| 582 | if (!i915_is_ggtt(vma->vm)) |
| 583 | return false; |
| 584 | |
| 585 | /* See also use_cpu_reloc() */ |
| 586 | if (HAS_LLC(vma->obj->base.dev)) |
| 587 | return false; |
| 588 | |
| 589 | if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU) |
| 590 | return false; |
| 591 | |
| 592 | return true; |
| 593 | } |
| 594 | |
| 595 | static bool |
| 596 | eb_vma_misplaced(struct i915_vma *vma) |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 597 | { |
| 598 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
| 599 | struct drm_i915_gem_object *obj = vma->obj; |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 600 | |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 601 | WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP && |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 602 | !i915_is_ggtt(vma->vm)); |
| 603 | |
| 604 | if (entry->alignment && |
| 605 | vma->node.start & (entry->alignment - 1)) |
| 606 | return true; |
| 607 | |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 608 | if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable) |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 609 | return true; |
| 610 | |
| 611 | if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS && |
| 612 | vma->node.start < BATCH_OFFSET_BIAS) |
| 613 | return true; |
| 614 | |
| 615 | return false; |
| 616 | } |
| 617 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 618 | static int |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 619 | i915_gem_execbuffer_reserve(struct intel_engine_cs *ring, |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 620 | struct list_head *vmas, |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 621 | bool *need_relocs) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 622 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 623 | struct drm_i915_gem_object *obj; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 624 | struct i915_vma *vma; |
Ben Widawsky | 68c8c17 | 2013-09-11 14:57:50 -0700 | [diff] [blame] | 625 | struct i915_address_space *vm; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 626 | struct list_head ordered_vmas; |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 627 | bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4; |
| 628 | int retry; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 629 | |
Chris Wilson | 227f782 | 2014-05-15 10:41:42 +0100 | [diff] [blame] | 630 | i915_gem_retire_requests_ring(ring); |
| 631 | |
Ben Widawsky | 68c8c17 | 2013-09-11 14:57:50 -0700 | [diff] [blame] | 632 | vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm; |
| 633 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 634 | INIT_LIST_HEAD(&ordered_vmas); |
| 635 | while (!list_empty(vmas)) { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 636 | struct drm_i915_gem_exec_object2 *entry; |
| 637 | bool need_fence, need_mappable; |
| 638 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 639 | vma = list_first_entry(vmas, struct i915_vma, exec_list); |
| 640 | obj = vma->obj; |
| 641 | entry = vma->exec_entry; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 642 | |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 643 | if (!has_fenced_gpu_access) |
| 644 | entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 645 | need_fence = |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 646 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 647 | obj->tiling_mode != I915_TILING_NONE; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 648 | need_mappable = need_fence || need_reloc_mappable(vma); |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 649 | |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 650 | if (need_mappable) { |
| 651 | entry->flags |= __EXEC_OBJECT_NEEDS_MAP; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 652 | list_move(&vma->exec_list, &ordered_vmas); |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 653 | } else |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 654 | list_move_tail(&vma->exec_list, &ordered_vmas); |
Chris Wilson | 595dad7 | 2011-01-13 11:03:48 +0000 | [diff] [blame] | 655 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 656 | obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND; |
Chris Wilson | 595dad7 | 2011-01-13 11:03:48 +0000 | [diff] [blame] | 657 | obj->base.pending_write_domain = 0; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 658 | } |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 659 | list_splice(&ordered_vmas, vmas); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 660 | |
| 661 | /* Attempt to pin all of the buffers into the GTT. |
| 662 | * This is done in 3 phases: |
| 663 | * |
| 664 | * 1a. Unbind all objects that do not match the GTT constraints for |
| 665 | * the execbuffer (fenceable, mappable, alignment etc). |
| 666 | * 1b. Increment pin count for already bound objects. |
| 667 | * 2. Bind new objects. |
| 668 | * 3. Decrement pin count. |
| 669 | * |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 670 | * This avoid unnecessary unbinding of later objects in order to make |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 671 | * room for the earlier objects *unless* we need to defragment. |
| 672 | */ |
| 673 | retry = 0; |
| 674 | do { |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 675 | int ret = 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 676 | |
| 677 | /* Unbind any ill-fitting objects or pin. */ |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 678 | list_for_each_entry(vma, vmas, exec_list) { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 679 | if (!drm_mm_node_allocated(&vma->node)) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 680 | continue; |
| 681 | |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 682 | if (eb_vma_misplaced(vma)) |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 683 | ret = i915_vma_unbind(vma); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 684 | else |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 685 | ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 686 | if (ret) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 687 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /* Bind fresh objects */ |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 691 | list_for_each_entry(vma, vmas, exec_list) { |
| 692 | if (drm_mm_node_allocated(&vma->node)) |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 693 | continue; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 694 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 695 | ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs); |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 696 | if (ret) |
| 697 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 700 | err: |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 701 | if (ret != -ENOSPC || retry++) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 702 | return ret; |
| 703 | |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 704 | /* Decrement pin count for bound objects */ |
| 705 | list_for_each_entry(vma, vmas, exec_list) |
| 706 | i915_gem_execbuffer_unreserve_vma(vma); |
| 707 | |
Ben Widawsky | 68c8c17 | 2013-09-11 14:57:50 -0700 | [diff] [blame] | 708 | ret = i915_gem_evict_vm(vm, true); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 709 | if (ret) |
| 710 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 711 | } while (1); |
| 712 | } |
| 713 | |
| 714 | static int |
| 715 | i915_gem_execbuffer_relocate_slow(struct drm_device *dev, |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 716 | struct drm_i915_gem_execbuffer2 *args, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 717 | struct drm_file *file, |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 718 | struct intel_engine_cs *ring, |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 719 | struct eb_vmas *eb, |
| 720 | struct drm_i915_gem_exec_object2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 721 | { |
| 722 | struct drm_i915_gem_relocation_entry *reloc; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 723 | struct i915_address_space *vm; |
| 724 | struct i915_vma *vma; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 725 | bool need_relocs; |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 726 | int *reloc_offset; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 727 | int i, total, ret; |
Daniel Vetter | b205ca5 | 2013-09-19 14:00:11 +0200 | [diff] [blame] | 728 | unsigned count = args->buffer_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 729 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 730 | vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm; |
| 731 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 732 | /* We may process another execbuffer during the unlock... */ |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 733 | while (!list_empty(&eb->vmas)) { |
| 734 | vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list); |
| 735 | list_del_init(&vma->exec_list); |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 736 | i915_gem_execbuffer_unreserve_vma(vma); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 737 | drm_gem_object_unreference(&vma->obj->base); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 740 | mutex_unlock(&dev->struct_mutex); |
| 741 | |
| 742 | total = 0; |
| 743 | for (i = 0; i < count; i++) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 744 | total += exec[i].relocation_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 745 | |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 746 | reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset)); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 747 | reloc = drm_malloc_ab(total, sizeof(*reloc)); |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 748 | if (reloc == NULL || reloc_offset == NULL) { |
| 749 | drm_free_large(reloc); |
| 750 | drm_free_large(reloc_offset); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 751 | mutex_lock(&dev->struct_mutex); |
| 752 | return -ENOMEM; |
| 753 | } |
| 754 | |
| 755 | total = 0; |
| 756 | for (i = 0; i < count; i++) { |
| 757 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
Chris Wilson | 262b6d3 | 2013-01-15 16:17:54 +0000 | [diff] [blame] | 758 | u64 invalid_offset = (u64)-1; |
| 759 | int j; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 760 | |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 761 | user_relocs = to_user_ptr(exec[i].relocs_ptr); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 762 | |
| 763 | if (copy_from_user(reloc+total, user_relocs, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 764 | exec[i].relocation_count * sizeof(*reloc))) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 765 | ret = -EFAULT; |
| 766 | mutex_lock(&dev->struct_mutex); |
| 767 | goto err; |
| 768 | } |
| 769 | |
Chris Wilson | 262b6d3 | 2013-01-15 16:17:54 +0000 | [diff] [blame] | 770 | /* As we do not update the known relocation offsets after |
| 771 | * relocating (due to the complexities in lock handling), |
| 772 | * we need to mark them as invalid now so that we force the |
| 773 | * relocation processing next time. Just in case the target |
| 774 | * object is evicted and then rebound into its old |
| 775 | * presumed_offset before the next execbuffer - if that |
| 776 | * happened we would make the mistake of assuming that the |
| 777 | * relocations were valid. |
| 778 | */ |
| 779 | for (j = 0; j < exec[i].relocation_count; j++) { |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 780 | if (__copy_to_user(&user_relocs[j].presumed_offset, |
| 781 | &invalid_offset, |
| 782 | sizeof(invalid_offset))) { |
Chris Wilson | 262b6d3 | 2013-01-15 16:17:54 +0000 | [diff] [blame] | 783 | ret = -EFAULT; |
| 784 | mutex_lock(&dev->struct_mutex); |
| 785 | goto err; |
| 786 | } |
| 787 | } |
| 788 | |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 789 | reloc_offset[i] = total; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 790 | total += exec[i].relocation_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | ret = i915_mutex_lock_interruptible(dev); |
| 794 | if (ret) { |
| 795 | mutex_lock(&dev->struct_mutex); |
| 796 | goto err; |
| 797 | } |
| 798 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 799 | /* reacquire the objects */ |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 800 | eb_reset(eb); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 801 | ret = eb_lookup_vmas(eb, exec, args, vm, file); |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 802 | if (ret) |
| 803 | goto err; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 804 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 805 | need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 806 | ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 807 | if (ret) |
| 808 | goto err; |
| 809 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 810 | list_for_each_entry(vma, &eb->vmas, exec_list) { |
| 811 | int offset = vma->exec_entry - exec; |
| 812 | ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb, |
| 813 | reloc + reloc_offset[offset]); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 814 | if (ret) |
| 815 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | /* Leave the user relocations as are, this is the painfully slow path, |
| 819 | * and we want to avoid the complication of dropping the lock whilst |
| 820 | * having buffers reserved in the aperture and so causing spurious |
| 821 | * ENOSPC for random operations. |
| 822 | */ |
| 823 | |
| 824 | err: |
| 825 | drm_free_large(reloc); |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 826 | drm_free_large(reloc_offset); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 827 | return ret; |
| 828 | } |
| 829 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 830 | static int |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 831 | i915_gem_execbuffer_move_to_gpu(struct intel_engine_cs *ring, |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 832 | struct list_head *vmas) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 833 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 834 | struct i915_vma *vma; |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 835 | uint32_t flush_domains = 0; |
Chris Wilson | 000433b | 2013-08-08 14:41:09 +0100 | [diff] [blame] | 836 | bool flush_chipset = false; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 837 | int ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 838 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 839 | list_for_each_entry(vma, vmas, exec_list) { |
| 840 | struct drm_i915_gem_object *obj = vma->obj; |
Ben Widawsky | 2911a35 | 2012-04-05 14:47:36 -0700 | [diff] [blame] | 841 | ret = i915_gem_object_sync(obj, ring); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 842 | if (ret) |
| 843 | return ret; |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 844 | |
| 845 | if (obj->base.write_domain & I915_GEM_DOMAIN_CPU) |
Chris Wilson | 000433b | 2013-08-08 14:41:09 +0100 | [diff] [blame] | 846 | flush_chipset |= i915_gem_clflush_object(obj, false); |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 847 | |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 848 | flush_domains |= obj->base.write_domain; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Chris Wilson | 000433b | 2013-08-08 14:41:09 +0100 | [diff] [blame] | 851 | if (flush_chipset) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 852 | i915_gem_chipset_flush(ring->dev); |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 853 | |
| 854 | if (flush_domains & I915_GEM_DOMAIN_GTT) |
| 855 | wmb(); |
| 856 | |
Chris Wilson | 09cf7c9 | 2012-07-13 14:14:08 +0100 | [diff] [blame] | 857 | /* Unconditionally invalidate gpu caches and ensure that we do flush |
| 858 | * any residual writes from the previous batch. |
| 859 | */ |
Chris Wilson | a7b9761 | 2012-07-20 12:41:08 +0100 | [diff] [blame] | 860 | return intel_ring_invalidate_all_caches(ring); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 863 | static bool |
| 864 | i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 865 | { |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 866 | if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS) |
| 867 | return false; |
| 868 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 869 | return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | static int |
Chris Wilson | ad19f10 | 2014-08-10 06:29:08 +0100 | [diff] [blame] | 873 | validate_exec_list(struct drm_device *dev, |
| 874 | struct drm_i915_gem_exec_object2 *exec, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 875 | int count) |
| 876 | { |
Daniel Vetter | b205ca5 | 2013-09-19 14:00:11 +0200 | [diff] [blame] | 877 | unsigned relocs_total = 0; |
| 878 | unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry); |
Chris Wilson | ad19f10 | 2014-08-10 06:29:08 +0100 | [diff] [blame] | 879 | unsigned invalid_flags; |
| 880 | int i; |
| 881 | |
| 882 | invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS; |
| 883 | if (USES_FULL_PPGTT(dev)) |
| 884 | invalid_flags |= EXEC_OBJECT_NEEDS_GTT; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 885 | |
| 886 | for (i = 0; i < count; i++) { |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 887 | char __user *ptr = to_user_ptr(exec[i].relocs_ptr); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 888 | int length; /* limited by fault_in_pages_readable() */ |
| 889 | |
Chris Wilson | ad19f10 | 2014-08-10 06:29:08 +0100 | [diff] [blame] | 890 | if (exec[i].flags & invalid_flags) |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 891 | return -EINVAL; |
| 892 | |
Kees Cook | 3118a4f | 2013-03-11 17:31:45 -0700 | [diff] [blame] | 893 | /* First check for malicious input causing overflow in |
| 894 | * the worst case where we need to allocate the entire |
| 895 | * relocation tree as a single array. |
| 896 | */ |
| 897 | if (exec[i].relocation_count > relocs_max - relocs_total) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 898 | return -EINVAL; |
Kees Cook | 3118a4f | 2013-03-11 17:31:45 -0700 | [diff] [blame] | 899 | relocs_total += exec[i].relocation_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 900 | |
| 901 | length = exec[i].relocation_count * |
| 902 | sizeof(struct drm_i915_gem_relocation_entry); |
Kees Cook | 3058753 | 2013-03-11 14:37:35 -0700 | [diff] [blame] | 903 | /* |
| 904 | * We must check that the entire relocation array is safe |
| 905 | * to read, but since we may need to update the presumed |
| 906 | * offsets during execution, check for full write access. |
| 907 | */ |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 908 | if (!access_ok(VERIFY_WRITE, ptr, length)) |
| 909 | return -EFAULT; |
| 910 | |
Jani Nikula | d330a95 | 2014-01-21 11:24:25 +0200 | [diff] [blame] | 911 | if (likely(!i915.prefault_disable)) { |
Xiong Zhang | 0b74b50 | 2013-07-19 13:51:24 +0800 | [diff] [blame] | 912 | if (fault_in_multipages_readable(ptr, length)) |
| 913 | return -EFAULT; |
| 914 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | return 0; |
| 918 | } |
| 919 | |
Oscar Mateo | 273497e | 2014-05-22 14:13:37 +0100 | [diff] [blame] | 920 | static struct intel_context * |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 921 | i915_gem_validate_context(struct drm_device *dev, struct drm_file *file, |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 922 | struct intel_engine_cs *ring, const u32 ctx_id) |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 923 | { |
Oscar Mateo | 273497e | 2014-05-22 14:13:37 +0100 | [diff] [blame] | 924 | struct intel_context *ctx = NULL; |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 925 | struct i915_ctx_hang_stats *hs; |
| 926 | |
Oscar Mateo | 821d66d | 2014-07-03 16:28:00 +0100 | [diff] [blame] | 927 | if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE) |
Daniel Vetter | 7c9c4b8 | 2013-12-18 16:37:49 +0100 | [diff] [blame] | 928 | return ERR_PTR(-EINVAL); |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 929 | |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 930 | ctx = i915_gem_context_get(file->driver_priv, ctx_id); |
Ben Widawsky | 72ad5c4 | 2014-01-02 19:50:27 -1000 | [diff] [blame] | 931 | if (IS_ERR(ctx)) |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 932 | return ctx; |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 933 | |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 934 | hs = &ctx->hang_stats; |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 935 | if (hs->banned) { |
| 936 | DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id); |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 937 | return ERR_PTR(-EIO); |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 938 | } |
| 939 | |
Oscar Mateo | ec3e996 | 2014-07-24 17:04:18 +0100 | [diff] [blame] | 940 | if (i915.enable_execlists && !ctx->engine[ring->id].state) { |
| 941 | int ret = intel_lr_context_deferred_create(ctx, ring); |
| 942 | if (ret) { |
| 943 | DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret); |
| 944 | return ERR_PTR(ret); |
| 945 | } |
| 946 | } |
| 947 | |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 948 | return ctx; |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 949 | } |
| 950 | |
Oscar Mateo | ba8b7cc | 2014-07-24 17:04:33 +0100 | [diff] [blame] | 951 | void |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 952 | i915_gem_execbuffer_move_to_active(struct list_head *vmas, |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 953 | struct intel_engine_cs *ring) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 954 | { |
John Harrison | 97b2a6a | 2014-11-24 18:49:26 +0000 | [diff] [blame] | 955 | struct drm_i915_gem_request *req = intel_ring_get_request(ring); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 956 | struct i915_vma *vma; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 957 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 958 | list_for_each_entry(vma, vmas, exec_list) { |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 959 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 960 | struct drm_i915_gem_object *obj = vma->obj; |
Chris Wilson | 69c2fc8 | 2012-07-20 12:41:03 +0100 | [diff] [blame] | 961 | u32 old_read = obj->base.read_domains; |
| 962 | u32 old_write = obj->base.write_domain; |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 963 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 964 | obj->base.write_domain = obj->base.pending_write_domain; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 965 | if (obj->base.write_domain == 0) |
| 966 | obj->base.pending_read_domains |= obj->base.read_domains; |
| 967 | obj->base.read_domains = obj->base.pending_read_domains; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 968 | |
Ben Widawsky | e2d05a8 | 2013-09-24 09:57:58 -0700 | [diff] [blame] | 969 | i915_vma_move_to_active(vma, ring); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 970 | if (obj->base.write_domain) { |
| 971 | obj->dirty = 1; |
John Harrison | 97b2a6a | 2014-11-24 18:49:26 +0000 | [diff] [blame] | 972 | i915_gem_request_assign(&obj->last_write_req, req); |
Daniel Vetter | f99d706 | 2014-06-19 16:01:59 +0200 | [diff] [blame] | 973 | |
| 974 | intel_fb_obj_invalidate(obj, ring); |
Chris Wilson | c8725f3 | 2014-03-17 12:21:55 +0000 | [diff] [blame] | 975 | |
| 976 | /* update for the implicit flush after a batch */ |
| 977 | obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 978 | } |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 979 | if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) { |
John Harrison | 97b2a6a | 2014-11-24 18:49:26 +0000 | [diff] [blame] | 980 | i915_gem_request_assign(&obj->last_fenced_req, req); |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 981 | if (entry->flags & __EXEC_OBJECT_HAS_FENCE) { |
| 982 | struct drm_i915_private *dev_priv = to_i915(ring->dev); |
| 983 | list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list, |
| 984 | &dev_priv->mm.fence_list); |
| 985 | } |
| 986 | } |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 987 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 988 | trace_i915_gem_object_change_domain(obj, old_read, old_write); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
Oscar Mateo | ba8b7cc | 2014-07-24 17:04:33 +0100 | [diff] [blame] | 992 | void |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 993 | i915_gem_execbuffer_retire_commands(struct drm_device *dev, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 994 | struct drm_file *file, |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 995 | struct intel_engine_cs *ring, |
Mika Kuoppala | 7d736f4 | 2013-06-12 15:01:39 +0300 | [diff] [blame] | 996 | struct drm_i915_gem_object *obj) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 997 | { |
Daniel Vetter | cc889e0 | 2012-06-13 20:45:19 +0200 | [diff] [blame] | 998 | /* Unconditionally force add_request to emit a full flush. */ |
| 999 | ring->gpu_caches_dirty = true; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1000 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1001 | /* Add a breadcrumb for the completion of the batch buffer */ |
John Harrison | 9400ae5 | 2014-11-24 18:49:36 +0000 | [diff] [blame] | 1002 | (void)__i915_add_request(ring, file, obj); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1003 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1004 | |
| 1005 | static int |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1006 | i915_reset_gen7_sol_offsets(struct drm_device *dev, |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 1007 | struct intel_engine_cs *ring) |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1008 | { |
Jani Nikula | 50227e1 | 2014-03-31 14:27:21 +0300 | [diff] [blame] | 1009 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1010 | int ret, i; |
| 1011 | |
Daniel Vetter | 9d662da | 2014-04-24 08:09:09 +0200 | [diff] [blame] | 1012 | if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) { |
| 1013 | DRM_DEBUG("sol reset is gen7/rcs only\n"); |
| 1014 | return -EINVAL; |
| 1015 | } |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1016 | |
| 1017 | ret = intel_ring_begin(ring, 4 * 3); |
| 1018 | if (ret) |
| 1019 | return ret; |
| 1020 | |
| 1021 | for (i = 0; i < 4; i++) { |
| 1022 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 1023 | intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i)); |
| 1024 | intel_ring_emit(ring, 0); |
| 1025 | } |
| 1026 | |
| 1027 | intel_ring_advance(ring); |
| 1028 | |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
Chris Wilson | 5c6c600 | 2014-09-06 10:28:27 +0100 | [diff] [blame] | 1032 | static int |
| 1033 | i915_emit_box(struct intel_engine_cs *ring, |
| 1034 | struct drm_clip_rect *box, |
| 1035 | int DR1, int DR4) |
| 1036 | { |
| 1037 | int ret; |
| 1038 | |
| 1039 | if (box->y2 <= box->y1 || box->x2 <= box->x1 || |
| 1040 | box->y2 <= 0 || box->x2 <= 0) { |
| 1041 | DRM_ERROR("Bad box %d,%d..%d,%d\n", |
| 1042 | box->x1, box->y1, box->x2, box->y2); |
| 1043 | return -EINVAL; |
| 1044 | } |
| 1045 | |
| 1046 | if (INTEL_INFO(ring->dev)->gen >= 4) { |
| 1047 | ret = intel_ring_begin(ring, 4); |
| 1048 | if (ret) |
| 1049 | return ret; |
| 1050 | |
| 1051 | intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO_I965); |
| 1052 | intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16); |
| 1053 | intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16); |
| 1054 | intel_ring_emit(ring, DR4); |
| 1055 | } else { |
| 1056 | ret = intel_ring_begin(ring, 6); |
| 1057 | if (ret) |
| 1058 | return ret; |
| 1059 | |
| 1060 | intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO); |
| 1061 | intel_ring_emit(ring, DR1); |
| 1062 | intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16); |
| 1063 | intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16); |
| 1064 | intel_ring_emit(ring, DR4); |
| 1065 | intel_ring_emit(ring, 0); |
| 1066 | } |
| 1067 | intel_ring_advance(ring); |
| 1068 | |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1072 | static struct drm_i915_gem_object* |
| 1073 | i915_gem_execbuffer_parse(struct intel_engine_cs *ring, |
| 1074 | struct drm_i915_gem_exec_object2 *shadow_exec_entry, |
| 1075 | struct eb_vmas *eb, |
| 1076 | struct drm_i915_gem_object *batch_obj, |
| 1077 | u32 batch_start_offset, |
| 1078 | u32 batch_len, |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1079 | bool is_master) |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1080 | { |
| 1081 | struct drm_i915_private *dev_priv = to_i915(batch_obj->base.dev); |
| 1082 | struct drm_i915_gem_object *shadow_batch_obj; |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1083 | struct i915_vma *vma; |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1084 | int ret; |
| 1085 | |
| 1086 | shadow_batch_obj = i915_gem_batch_pool_get(&dev_priv->mm.batch_pool, |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1087 | PAGE_ALIGN(batch_len)); |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1088 | if (IS_ERR(shadow_batch_obj)) |
| 1089 | return shadow_batch_obj; |
| 1090 | |
| 1091 | ret = i915_parse_cmds(ring, |
| 1092 | batch_obj, |
| 1093 | shadow_batch_obj, |
| 1094 | batch_start_offset, |
| 1095 | batch_len, |
| 1096 | is_master); |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1097 | if (ret) |
| 1098 | goto err; |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1099 | |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1100 | ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0); |
| 1101 | if (ret) |
| 1102 | goto err; |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1103 | |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1104 | memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry)); |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1105 | |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1106 | vma = i915_gem_obj_to_ggtt(shadow_batch_obj); |
| 1107 | vma->exec_entry = shadow_exec_entry; |
| 1108 | vma->exec_entry->flags = __EXEC_OBJECT_PURGEABLE | __EXEC_OBJECT_HAS_PIN; |
| 1109 | drm_gem_object_reference(&shadow_batch_obj->base); |
| 1110 | list_add_tail(&vma->exec_list, &eb->vmas); |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1111 | |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1112 | shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND; |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1113 | |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1114 | return shadow_batch_obj; |
| 1115 | |
| 1116 | err: |
| 1117 | if (ret == -EACCES) /* unhandled chained batch */ |
| 1118 | return batch_obj; |
| 1119 | else |
| 1120 | return ERR_PTR(ret); |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1121 | } |
Chris Wilson | 5c6c600 | 2014-09-06 10:28:27 +0100 | [diff] [blame] | 1122 | |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 1123 | int |
| 1124 | i915_gem_ringbuffer_submission(struct drm_device *dev, struct drm_file *file, |
| 1125 | struct intel_engine_cs *ring, |
| 1126 | struct intel_context *ctx, |
| 1127 | struct drm_i915_gem_execbuffer2 *args, |
| 1128 | struct list_head *vmas, |
| 1129 | struct drm_i915_gem_object *batch_obj, |
John Harrison | 8e004ef | 2015-02-13 11:48:10 +0000 | [diff] [blame] | 1130 | u64 exec_start, u32 dispatch_flags) |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1131 | { |
| 1132 | struct drm_clip_rect *cliprects = NULL; |
| 1133 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1134 | u64 exec_len; |
| 1135 | int instp_mode; |
| 1136 | u32 instp_mask; |
| 1137 | int i, ret = 0; |
| 1138 | |
| 1139 | if (args->num_cliprects != 0) { |
| 1140 | if (ring != &dev_priv->ring[RCS]) { |
| 1141 | DRM_DEBUG("clip rectangles are only valid with the render ring\n"); |
| 1142 | return -EINVAL; |
| 1143 | } |
| 1144 | |
| 1145 | if (INTEL_INFO(dev)->gen >= 5) { |
| 1146 | DRM_DEBUG("clip rectangles are only valid on pre-gen5\n"); |
| 1147 | return -EINVAL; |
| 1148 | } |
| 1149 | |
| 1150 | if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) { |
| 1151 | DRM_DEBUG("execbuf with %u cliprects\n", |
| 1152 | args->num_cliprects); |
| 1153 | return -EINVAL; |
| 1154 | } |
| 1155 | |
| 1156 | cliprects = kcalloc(args->num_cliprects, |
| 1157 | sizeof(*cliprects), |
| 1158 | GFP_KERNEL); |
| 1159 | if (cliprects == NULL) { |
| 1160 | ret = -ENOMEM; |
| 1161 | goto error; |
| 1162 | } |
| 1163 | |
| 1164 | if (copy_from_user(cliprects, |
| 1165 | to_user_ptr(args->cliprects_ptr), |
| 1166 | sizeof(*cliprects)*args->num_cliprects)) { |
| 1167 | ret = -EFAULT; |
| 1168 | goto error; |
| 1169 | } |
| 1170 | } else { |
| 1171 | if (args->DR4 == 0xffffffff) { |
| 1172 | DRM_DEBUG("UXA submitting garbage DR4, fixing up\n"); |
| 1173 | args->DR4 = 0; |
| 1174 | } |
| 1175 | |
| 1176 | if (args->DR1 || args->DR4 || args->cliprects_ptr) { |
| 1177 | DRM_DEBUG("0 cliprects but dirt in cliprects fields\n"); |
| 1178 | return -EINVAL; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | ret = i915_gem_execbuffer_move_to_gpu(ring, vmas); |
| 1183 | if (ret) |
| 1184 | goto error; |
| 1185 | |
| 1186 | ret = i915_switch_context(ring, ctx); |
| 1187 | if (ret) |
| 1188 | goto error; |
| 1189 | |
| 1190 | instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK; |
| 1191 | instp_mask = I915_EXEC_CONSTANTS_MASK; |
| 1192 | switch (instp_mode) { |
| 1193 | case I915_EXEC_CONSTANTS_REL_GENERAL: |
| 1194 | case I915_EXEC_CONSTANTS_ABSOLUTE: |
| 1195 | case I915_EXEC_CONSTANTS_REL_SURFACE: |
| 1196 | if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) { |
| 1197 | DRM_DEBUG("non-0 rel constants mode on non-RCS\n"); |
| 1198 | ret = -EINVAL; |
| 1199 | goto error; |
| 1200 | } |
| 1201 | |
| 1202 | if (instp_mode != dev_priv->relative_constants_mode) { |
| 1203 | if (INTEL_INFO(dev)->gen < 4) { |
| 1204 | DRM_DEBUG("no rel constants on pre-gen4\n"); |
| 1205 | ret = -EINVAL; |
| 1206 | goto error; |
| 1207 | } |
| 1208 | |
| 1209 | if (INTEL_INFO(dev)->gen > 5 && |
| 1210 | instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) { |
| 1211 | DRM_DEBUG("rel surface constants mode invalid on gen5+\n"); |
| 1212 | ret = -EINVAL; |
| 1213 | goto error; |
| 1214 | } |
| 1215 | |
| 1216 | /* The HW changed the meaning on this bit on gen6 */ |
| 1217 | if (INTEL_INFO(dev)->gen >= 6) |
| 1218 | instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE; |
| 1219 | } |
| 1220 | break; |
| 1221 | default: |
| 1222 | DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode); |
| 1223 | ret = -EINVAL; |
| 1224 | goto error; |
| 1225 | } |
| 1226 | |
| 1227 | if (ring == &dev_priv->ring[RCS] && |
| 1228 | instp_mode != dev_priv->relative_constants_mode) { |
| 1229 | ret = intel_ring_begin(ring, 4); |
| 1230 | if (ret) |
| 1231 | goto error; |
| 1232 | |
| 1233 | intel_ring_emit(ring, MI_NOOP); |
| 1234 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 1235 | intel_ring_emit(ring, INSTPM); |
| 1236 | intel_ring_emit(ring, instp_mask << 16 | instp_mode); |
| 1237 | intel_ring_advance(ring); |
| 1238 | |
| 1239 | dev_priv->relative_constants_mode = instp_mode; |
| 1240 | } |
| 1241 | |
| 1242 | if (args->flags & I915_EXEC_GEN7_SOL_RESET) { |
| 1243 | ret = i915_reset_gen7_sol_offsets(dev, ring); |
| 1244 | if (ret) |
| 1245 | goto error; |
| 1246 | } |
| 1247 | |
| 1248 | exec_len = args->batch_len; |
| 1249 | if (cliprects) { |
| 1250 | for (i = 0; i < args->num_cliprects; i++) { |
Chris Wilson | 5c6c600 | 2014-09-06 10:28:27 +0100 | [diff] [blame] | 1251 | ret = i915_emit_box(ring, &cliprects[i], |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1252 | args->DR1, args->DR4); |
| 1253 | if (ret) |
| 1254 | goto error; |
| 1255 | |
| 1256 | ret = ring->dispatch_execbuffer(ring, |
| 1257 | exec_start, exec_len, |
John Harrison | 8e004ef | 2015-02-13 11:48:10 +0000 | [diff] [blame] | 1258 | dispatch_flags); |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1259 | if (ret) |
| 1260 | goto error; |
| 1261 | } |
| 1262 | } else { |
| 1263 | ret = ring->dispatch_execbuffer(ring, |
| 1264 | exec_start, exec_len, |
John Harrison | 8e004ef | 2015-02-13 11:48:10 +0000 | [diff] [blame] | 1265 | dispatch_flags); |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1266 | if (ret) |
| 1267 | return ret; |
| 1268 | } |
| 1269 | |
John Harrison | 8e004ef | 2015-02-13 11:48:10 +0000 | [diff] [blame] | 1270 | trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), dispatch_flags); |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1271 | |
| 1272 | i915_gem_execbuffer_move_to_active(vmas, ring); |
| 1273 | i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj); |
| 1274 | |
| 1275 | error: |
| 1276 | kfree(cliprects); |
| 1277 | return ret; |
| 1278 | } |
| 1279 | |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1280 | /** |
| 1281 | * Find one BSD ring to dispatch the corresponding BSD command. |
| 1282 | * The Ring ID is returned. |
| 1283 | */ |
| 1284 | static int gen8_dispatch_bsd_ring(struct drm_device *dev, |
| 1285 | struct drm_file *file) |
| 1286 | { |
| 1287 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1288 | struct drm_i915_file_private *file_priv = file->driver_priv; |
| 1289 | |
| 1290 | /* Check whether the file_priv is using one ring */ |
| 1291 | if (file_priv->bsd_ring) |
| 1292 | return file_priv->bsd_ring->id; |
| 1293 | else { |
| 1294 | /* If no, use the ping-pong mechanism to select one ring */ |
| 1295 | int ring_id; |
| 1296 | |
| 1297 | mutex_lock(&dev->struct_mutex); |
Daniel Vetter | bdf1e7e | 2014-05-21 17:37:52 +0200 | [diff] [blame] | 1298 | if (dev_priv->mm.bsd_ring_dispatch_index == 0) { |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1299 | ring_id = VCS; |
Daniel Vetter | bdf1e7e | 2014-05-21 17:37:52 +0200 | [diff] [blame] | 1300 | dev_priv->mm.bsd_ring_dispatch_index = 1; |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1301 | } else { |
| 1302 | ring_id = VCS2; |
Daniel Vetter | bdf1e7e | 2014-05-21 17:37:52 +0200 | [diff] [blame] | 1303 | dev_priv->mm.bsd_ring_dispatch_index = 0; |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1304 | } |
| 1305 | file_priv->bsd_ring = &dev_priv->ring[ring_id]; |
| 1306 | mutex_unlock(&dev->struct_mutex); |
| 1307 | return ring_id; |
| 1308 | } |
| 1309 | } |
| 1310 | |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 1311 | static struct drm_i915_gem_object * |
| 1312 | eb_get_batch(struct eb_vmas *eb) |
| 1313 | { |
| 1314 | struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list); |
| 1315 | |
| 1316 | /* |
| 1317 | * SNA is doing fancy tricks with compressing batch buffers, which leads |
| 1318 | * to negative relocation deltas. Usually that works out ok since the |
| 1319 | * relocate address is still positive, except when the batch is placed |
| 1320 | * very low in the GTT. Ensure this doesn't happen. |
| 1321 | * |
| 1322 | * Note that actual hangs have only been observed on gen7, but for |
| 1323 | * paranoia do it everywhere. |
| 1324 | */ |
| 1325 | vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS; |
| 1326 | |
| 1327 | return vma->obj; |
| 1328 | } |
| 1329 | |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1330 | static int |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1331 | i915_gem_do_execbuffer(struct drm_device *dev, void *data, |
| 1332 | struct drm_file *file, |
| 1333 | struct drm_i915_gem_execbuffer2 *args, |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1334 | struct drm_i915_gem_exec_object2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1335 | { |
Jani Nikula | 50227e1 | 2014-03-31 14:27:21 +0300 | [diff] [blame] | 1336 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 1337 | struct eb_vmas *eb; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1338 | struct drm_i915_gem_object *batch_obj; |
Brad Volkin | 78a4237 | 2014-12-11 12:13:09 -0800 | [diff] [blame] | 1339 | struct drm_i915_gem_exec_object2 shadow_exec_entry; |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 1340 | struct intel_engine_cs *ring; |
Oscar Mateo | 273497e | 2014-05-22 14:13:37 +0100 | [diff] [blame] | 1341 | struct intel_context *ctx; |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1342 | struct i915_address_space *vm; |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 1343 | const u32 ctx_id = i915_execbuffer2_get_context_id(*args); |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1344 | u64 exec_start = args->batch_start_offset; |
John Harrison | 8e004ef | 2015-02-13 11:48:10 +0000 | [diff] [blame] | 1345 | u32 dispatch_flags; |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1346 | int ret; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 1347 | bool need_relocs; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1348 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 1349 | if (!i915_gem_check_execbuffer(args)) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1350 | return -EINVAL; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1351 | |
Chris Wilson | ad19f10 | 2014-08-10 06:29:08 +0100 | [diff] [blame] | 1352 | ret = validate_exec_list(dev, exec, args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1353 | if (ret) |
| 1354 | return ret; |
| 1355 | |
John Harrison | 8e004ef | 2015-02-13 11:48:10 +0000 | [diff] [blame] | 1356 | dispatch_flags = 0; |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1357 | if (args->flags & I915_EXEC_SECURE) { |
| 1358 | if (!file->is_master || !capable(CAP_SYS_ADMIN)) |
| 1359 | return -EPERM; |
| 1360 | |
John Harrison | 8e004ef | 2015-02-13 11:48:10 +0000 | [diff] [blame] | 1361 | dispatch_flags |= I915_DISPATCH_SECURE; |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1362 | } |
Daniel Vetter | b45305f | 2012-12-17 16:21:27 +0100 | [diff] [blame] | 1363 | if (args->flags & I915_EXEC_IS_PINNED) |
John Harrison | 8e004ef | 2015-02-13 11:48:10 +0000 | [diff] [blame] | 1364 | dispatch_flags |= I915_DISPATCH_PINNED; |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1365 | |
Zhao Yakui | b1a9330 | 2014-04-17 10:37:36 +0800 | [diff] [blame] | 1366 | if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1367 | DRM_DEBUG("execbuf with unknown ring: %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1368 | (int)(args->flags & I915_EXEC_RING_MASK)); |
| 1369 | return -EINVAL; |
| 1370 | } |
Ben Widawsky | ca01b12 | 2013-12-06 14:11:00 -0800 | [diff] [blame] | 1371 | |
Zhipeng Gong | 8d360df | 2015-01-13 08:48:24 +0800 | [diff] [blame] | 1372 | if (((args->flags & I915_EXEC_RING_MASK) != I915_EXEC_BSD) && |
| 1373 | ((args->flags & I915_EXEC_BSD_MASK) != 0)) { |
| 1374 | DRM_DEBUG("execbuf with non bsd ring but with invalid " |
| 1375 | "bsd dispatch flags: %d\n", (int)(args->flags)); |
| 1376 | return -EINVAL; |
| 1377 | } |
| 1378 | |
Ben Widawsky | ca01b12 | 2013-12-06 14:11:00 -0800 | [diff] [blame] | 1379 | if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT) |
| 1380 | ring = &dev_priv->ring[RCS]; |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1381 | else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) { |
| 1382 | if (HAS_BSD2(dev)) { |
| 1383 | int ring_id; |
Zhipeng Gong | 8d360df | 2015-01-13 08:48:24 +0800 | [diff] [blame] | 1384 | |
| 1385 | switch (args->flags & I915_EXEC_BSD_MASK) { |
| 1386 | case I915_EXEC_BSD_DEFAULT: |
| 1387 | ring_id = gen8_dispatch_bsd_ring(dev, file); |
| 1388 | ring = &dev_priv->ring[ring_id]; |
| 1389 | break; |
| 1390 | case I915_EXEC_BSD_RING1: |
| 1391 | ring = &dev_priv->ring[VCS]; |
| 1392 | break; |
| 1393 | case I915_EXEC_BSD_RING2: |
| 1394 | ring = &dev_priv->ring[VCS2]; |
| 1395 | break; |
| 1396 | default: |
| 1397 | DRM_DEBUG("execbuf with unknown bsd ring: %d\n", |
| 1398 | (int)(args->flags & I915_EXEC_BSD_MASK)); |
| 1399 | return -EINVAL; |
| 1400 | } |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1401 | } else |
| 1402 | ring = &dev_priv->ring[VCS]; |
| 1403 | } else |
Ben Widawsky | ca01b12 | 2013-12-06 14:11:00 -0800 | [diff] [blame] | 1404 | ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1]; |
| 1405 | |
Chris Wilson | a15817c | 2012-05-11 14:29:31 +0100 | [diff] [blame] | 1406 | if (!intel_ring_initialized(ring)) { |
| 1407 | DRM_DEBUG("execbuf with invalid ring: %d\n", |
| 1408 | (int)(args->flags & I915_EXEC_RING_MASK)); |
| 1409 | return -EINVAL; |
| 1410 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1411 | |
| 1412 | if (args->buffer_count < 1) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1413 | DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1414 | return -EINVAL; |
| 1415 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1416 | |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1417 | intel_runtime_pm_get(dev_priv); |
| 1418 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1419 | ret = i915_mutex_lock_interruptible(dev); |
| 1420 | if (ret) |
| 1421 | goto pre_mutex_err; |
| 1422 | |
Daniel Vetter | 7c9c4b8 | 2013-12-18 16:37:49 +0100 | [diff] [blame] | 1423 | ctx = i915_gem_validate_context(dev, file, ring, ctx_id); |
Ben Widawsky | 72ad5c4 | 2014-01-02 19:50:27 -1000 | [diff] [blame] | 1424 | if (IS_ERR(ctx)) { |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 1425 | mutex_unlock(&dev->struct_mutex); |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1426 | ret = PTR_ERR(ctx); |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 1427 | goto pre_mutex_err; |
Ben Widawsky | 935f38d | 2014-04-04 22:41:07 -0700 | [diff] [blame] | 1428 | } |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1429 | |
| 1430 | i915_gem_context_reference(ctx); |
| 1431 | |
Daniel Vetter | ae6c480 | 2014-08-06 15:04:53 +0200 | [diff] [blame] | 1432 | if (ctx->ppgtt) |
| 1433 | vm = &ctx->ppgtt->base; |
| 1434 | else |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 1435 | vm = &dev_priv->gtt.base; |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 1436 | |
Ben Widawsky | 17601cbc | 2013-11-25 09:54:38 -0800 | [diff] [blame] | 1437 | eb = eb_create(args); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1438 | if (eb == NULL) { |
Ben Widawsky | 935f38d | 2014-04-04 22:41:07 -0700 | [diff] [blame] | 1439 | i915_gem_context_unreference(ctx); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1440 | mutex_unlock(&dev->struct_mutex); |
| 1441 | ret = -ENOMEM; |
| 1442 | goto pre_mutex_err; |
| 1443 | } |
| 1444 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1445 | /* Look up object handles */ |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 1446 | ret = eb_lookup_vmas(eb, exec, args, vm, file); |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 1447 | if (ret) |
| 1448 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1449 | |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 1450 | /* take note of the batch buffer before we might reorder the lists */ |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 1451 | batch_obj = eb_get_batch(eb); |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 1452 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1453 | /* Move the objects en-masse into the GTT, evicting if necessary. */ |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 1454 | need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 1455 | ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1456 | if (ret) |
| 1457 | goto err; |
| 1458 | |
| 1459 | /* The objects are in their final locations, apply the relocations. */ |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 1460 | if (need_relocs) |
Ben Widawsky | 17601cbc | 2013-11-25 09:54:38 -0800 | [diff] [blame] | 1461 | ret = i915_gem_execbuffer_relocate(eb); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1462 | if (ret) { |
| 1463 | if (ret == -EFAULT) { |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 1464 | ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring, |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 1465 | eb, exec); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1466 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 1467 | } |
| 1468 | if (ret) |
| 1469 | goto err; |
| 1470 | } |
| 1471 | |
| 1472 | /* Set the pending read domains for the batch buffer to COMMAND */ |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1473 | if (batch_obj->base.pending_write_domain) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1474 | DRM_DEBUG("Attempting to use self-modifying batch buffer\n"); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1475 | ret = -EINVAL; |
| 1476 | goto err; |
| 1477 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1478 | |
Brad Volkin | 351e3db | 2014-02-18 10:15:46 -0800 | [diff] [blame] | 1479 | if (i915_needs_cmd_parser(ring)) { |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1480 | batch_obj = i915_gem_execbuffer_parse(ring, |
| 1481 | &shadow_exec_entry, |
| 1482 | eb, |
| 1483 | batch_obj, |
| 1484 | args->batch_start_offset, |
| 1485 | args->batch_len, |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1486 | file->is_master); |
Brad Volkin | 7174537 | 2014-12-11 12:13:12 -0800 | [diff] [blame] | 1487 | if (IS_ERR(batch_obj)) { |
| 1488 | ret = PTR_ERR(batch_obj); |
Brad Volkin | 78a4237 | 2014-12-11 12:13:09 -0800 | [diff] [blame] | 1489 | goto err; |
| 1490 | } |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1491 | |
| 1492 | /* |
| 1493 | * Set the DISPATCH_SECURE bit to remove the NON_SECURE |
| 1494 | * bit from MI_BATCH_BUFFER_START commands issued in the |
| 1495 | * dispatch_execbuffer implementations. We specifically |
| 1496 | * don't want that set when the command parser is |
| 1497 | * enabled. |
| 1498 | * |
| 1499 | * FIXME: with aliasing ppgtt, buffers that should only |
| 1500 | * be in ggtt still end up in the aliasing ppgtt. remove |
| 1501 | * this check when that is fixed. |
| 1502 | */ |
| 1503 | if (USES_FULL_PPGTT(dev)) |
John Harrison | 8e004ef | 2015-02-13 11:48:10 +0000 | [diff] [blame] | 1504 | dispatch_flags |= I915_DISPATCH_SECURE; |
Chris Wilson | 17cabf5 | 2015-01-14 11:20:57 +0000 | [diff] [blame] | 1505 | |
| 1506 | exec_start = 0; |
Brad Volkin | 351e3db | 2014-02-18 10:15:46 -0800 | [diff] [blame] | 1507 | } |
| 1508 | |
Brad Volkin | 78a4237 | 2014-12-11 12:13:09 -0800 | [diff] [blame] | 1509 | batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND; |
| 1510 | |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1511 | /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure |
| 1512 | * batch" bit. Hence we need to pin secure batches into the global gtt. |
Ben Widawsky | 28cf541 | 2013-11-02 21:07:26 -0700 | [diff] [blame] | 1513 | * hsw should have this fixed, but bdw mucks it up again. */ |
John Harrison | 8e004ef | 2015-02-13 11:48:10 +0000 | [diff] [blame] | 1514 | if (dispatch_flags & I915_DISPATCH_SECURE) { |
Daniel Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame] | 1515 | /* |
| 1516 | * So on first glance it looks freaky that we pin the batch here |
| 1517 | * outside of the reservation loop. But: |
| 1518 | * - The batch is already pinned into the relevant ppgtt, so we |
| 1519 | * already have the backing storage fully allocated. |
| 1520 | * - No other BO uses the global gtt (well contexts, but meh), |
Yannick Guerrini | fd0753c | 2015-02-28 17:20:41 +0100 | [diff] [blame] | 1521 | * so we don't really have issues with multiple objects not |
Daniel Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame] | 1522 | * fitting due to fragmentation. |
| 1523 | * So this is actually safe. |
| 1524 | */ |
| 1525 | ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0); |
| 1526 | if (ret) |
| 1527 | goto err; |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1528 | |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 1529 | exec_start += i915_gem_obj_ggtt_offset(batch_obj); |
Daniel Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame] | 1530 | } else |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 1531 | exec_start += i915_gem_obj_offset(batch_obj, vm); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1532 | |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 1533 | ret = dev_priv->gt.do_execbuf(dev, file, ring, ctx, args, |
John Harrison | 8e004ef | 2015-02-13 11:48:10 +0000 | [diff] [blame] | 1534 | &eb->vmas, batch_obj, exec_start, |
| 1535 | dispatch_flags); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1536 | |
Daniel Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame] | 1537 | /* |
| 1538 | * FIXME: We crucially rely upon the active tracking for the (ppgtt) |
| 1539 | * batch vma for correctness. For less ugly and less fragility this |
| 1540 | * needs to be adjusted to also track the ggtt batch vma properly as |
| 1541 | * active. |
| 1542 | */ |
John Harrison | 8e004ef | 2015-02-13 11:48:10 +0000 | [diff] [blame] | 1543 | if (dispatch_flags & I915_DISPATCH_SECURE) |
Daniel Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame] | 1544 | i915_gem_object_ggtt_unpin(batch_obj); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1545 | err: |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1546 | /* the request owns the ref now */ |
| 1547 | i915_gem_context_unreference(ctx); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1548 | eb_destroy(eb); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1549 | |
| 1550 | mutex_unlock(&dev->struct_mutex); |
| 1551 | |
| 1552 | pre_mutex_err: |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1553 | /* intel_gpu_busy should also get a ref, so it will free when the device |
| 1554 | * is really idle. */ |
| 1555 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1556 | return ret; |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * Legacy execbuffer just creates an exec2 list from the original exec object |
| 1561 | * list array and passes it to the real function. |
| 1562 | */ |
| 1563 | int |
| 1564 | i915_gem_execbuffer(struct drm_device *dev, void *data, |
| 1565 | struct drm_file *file) |
| 1566 | { |
| 1567 | struct drm_i915_gem_execbuffer *args = data; |
| 1568 | struct drm_i915_gem_execbuffer2 exec2; |
| 1569 | struct drm_i915_gem_exec_object *exec_list = NULL; |
| 1570 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 1571 | int ret, i; |
| 1572 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1573 | if (args->buffer_count < 1) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1574 | DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1575 | return -EINVAL; |
| 1576 | } |
| 1577 | |
| 1578 | /* Copy in the exec list from userland */ |
| 1579 | exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count); |
| 1580 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count); |
| 1581 | if (exec_list == NULL || exec2_list == NULL) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1582 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1583 | args->buffer_count); |
| 1584 | drm_free_large(exec_list); |
| 1585 | drm_free_large(exec2_list); |
| 1586 | return -ENOMEM; |
| 1587 | } |
| 1588 | ret = copy_from_user(exec_list, |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 1589 | to_user_ptr(args->buffers_ptr), |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1590 | sizeof(*exec_list) * args->buffer_count); |
| 1591 | if (ret != 0) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1592 | DRM_DEBUG("copy %d exec entries failed %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1593 | args->buffer_count, ret); |
| 1594 | drm_free_large(exec_list); |
| 1595 | drm_free_large(exec2_list); |
| 1596 | return -EFAULT; |
| 1597 | } |
| 1598 | |
| 1599 | for (i = 0; i < args->buffer_count; i++) { |
| 1600 | exec2_list[i].handle = exec_list[i].handle; |
| 1601 | exec2_list[i].relocation_count = exec_list[i].relocation_count; |
| 1602 | exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; |
| 1603 | exec2_list[i].alignment = exec_list[i].alignment; |
| 1604 | exec2_list[i].offset = exec_list[i].offset; |
| 1605 | if (INTEL_INFO(dev)->gen < 4) |
| 1606 | exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; |
| 1607 | else |
| 1608 | exec2_list[i].flags = 0; |
| 1609 | } |
| 1610 | |
| 1611 | exec2.buffers_ptr = args->buffers_ptr; |
| 1612 | exec2.buffer_count = args->buffer_count; |
| 1613 | exec2.batch_start_offset = args->batch_start_offset; |
| 1614 | exec2.batch_len = args->batch_len; |
| 1615 | exec2.DR1 = args->DR1; |
| 1616 | exec2.DR4 = args->DR4; |
| 1617 | exec2.num_cliprects = args->num_cliprects; |
| 1618 | exec2.cliprects_ptr = args->cliprects_ptr; |
| 1619 | exec2.flags = I915_EXEC_RENDER; |
Ben Widawsky | 6e0a69d | 2012-06-04 14:42:55 -0700 | [diff] [blame] | 1620 | i915_execbuffer2_set_context_id(exec2, 0); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1621 | |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1622 | ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1623 | if (!ret) { |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 1624 | struct drm_i915_gem_exec_object __user *user_exec_list = |
| 1625 | to_user_ptr(args->buffers_ptr); |
| 1626 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1627 | /* Copy the new buffer offsets back to the user's exec list. */ |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 1628 | for (i = 0; i < args->buffer_count; i++) { |
| 1629 | ret = __copy_to_user(&user_exec_list[i].offset, |
| 1630 | &exec2_list[i].offset, |
| 1631 | sizeof(user_exec_list[i].offset)); |
| 1632 | if (ret) { |
| 1633 | ret = -EFAULT; |
| 1634 | DRM_DEBUG("failed to copy %d exec entries " |
| 1635 | "back to user (%d)\n", |
| 1636 | args->buffer_count, ret); |
| 1637 | break; |
| 1638 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | drm_free_large(exec_list); |
| 1643 | drm_free_large(exec2_list); |
| 1644 | return ret; |
| 1645 | } |
| 1646 | |
| 1647 | int |
| 1648 | i915_gem_execbuffer2(struct drm_device *dev, void *data, |
| 1649 | struct drm_file *file) |
| 1650 | { |
| 1651 | struct drm_i915_gem_execbuffer2 *args = data; |
| 1652 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 1653 | int ret; |
| 1654 | |
Xi Wang | ed8cd3b | 2012-04-23 04:06:41 -0400 | [diff] [blame] | 1655 | if (args->buffer_count < 1 || |
| 1656 | args->buffer_count > UINT_MAX / sizeof(*exec2_list)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1657 | DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1658 | return -EINVAL; |
| 1659 | } |
| 1660 | |
Daniel Vetter | 9cb3466 | 2014-04-24 08:09:11 +0200 | [diff] [blame] | 1661 | if (args->rsvd2 != 0) { |
| 1662 | DRM_DEBUG("dirty rvsd2 field\n"); |
| 1663 | return -EINVAL; |
| 1664 | } |
| 1665 | |
Chris Wilson | 8408c28 | 2011-02-21 12:54:48 +0000 | [diff] [blame] | 1666 | exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count, |
Chris Wilson | 419fa72 | 2013-01-08 10:53:13 +0000 | [diff] [blame] | 1667 | GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY); |
Chris Wilson | 8408c28 | 2011-02-21 12:54:48 +0000 | [diff] [blame] | 1668 | if (exec2_list == NULL) |
| 1669 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), |
| 1670 | args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1671 | if (exec2_list == NULL) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1672 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1673 | args->buffer_count); |
| 1674 | return -ENOMEM; |
| 1675 | } |
| 1676 | ret = copy_from_user(exec2_list, |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 1677 | to_user_ptr(args->buffers_ptr), |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1678 | sizeof(*exec2_list) * args->buffer_count); |
| 1679 | if (ret != 0) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1680 | DRM_DEBUG("copy %d exec entries failed %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1681 | args->buffer_count, ret); |
| 1682 | drm_free_large(exec2_list); |
| 1683 | return -EFAULT; |
| 1684 | } |
| 1685 | |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1686 | ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1687 | if (!ret) { |
| 1688 | /* Copy the new buffer offsets back to the user's exec list. */ |
Ville Syrjälä | d593d99 | 2014-06-13 16:42:51 +0300 | [diff] [blame] | 1689 | struct drm_i915_gem_exec_object2 __user *user_exec_list = |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 1690 | to_user_ptr(args->buffers_ptr); |
| 1691 | int i; |
| 1692 | |
| 1693 | for (i = 0; i < args->buffer_count; i++) { |
| 1694 | ret = __copy_to_user(&user_exec_list[i].offset, |
| 1695 | &exec2_list[i].offset, |
| 1696 | sizeof(user_exec_list[i].offset)); |
| 1697 | if (ret) { |
| 1698 | ret = -EFAULT; |
| 1699 | DRM_DEBUG("failed to copy %d exec entries " |
| 1700 | "back to user\n", |
| 1701 | args->buffer_count); |
| 1702 | break; |
| 1703 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | drm_free_large(exec2_list); |
| 1708 | return ret; |
| 1709 | } |