Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016 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 | */ |
Chris Wilson | 10195b1 | 2018-06-28 14:22:06 +0100 | [diff] [blame] | 24 | |
Chris Wilson | 112ed2d | 2019-04-24 18:48:39 +0100 | [diff] [blame] | 25 | #include "gt/intel_engine.h" |
| 26 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 27 | #include "i915_vma.h" |
| 28 | |
| 29 | #include "i915_drv.h" |
Chris Wilson | 103b76ee | 2019-03-05 21:38:30 +0000 | [diff] [blame] | 30 | #include "i915_globals.h" |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 31 | #include "intel_frontbuffer.h" |
| 32 | |
| 33 | #include <drm/drm_gem.h> |
| 34 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 35 | static struct i915_global_vma { |
Chris Wilson | 103b76ee | 2019-03-05 21:38:30 +0000 | [diff] [blame] | 36 | struct i915_global base; |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 37 | struct kmem_cache *slab_vmas; |
| 38 | } global; |
| 39 | |
| 40 | struct i915_vma *i915_vma_alloc(void) |
| 41 | { |
| 42 | return kmem_cache_zalloc(global.slab_vmas, GFP_KERNEL); |
| 43 | } |
| 44 | |
| 45 | void i915_vma_free(struct i915_vma *vma) |
| 46 | { |
| 47 | return kmem_cache_free(global.slab_vmas, vma); |
| 48 | } |
| 49 | |
Chris Wilson | 1eca65d | 2018-07-06 07:53:06 +0100 | [diff] [blame] | 50 | #if IS_ENABLED(CONFIG_DRM_I915_ERRLOG_GEM) && IS_ENABLED(CONFIG_DRM_DEBUG_MM) |
Chris Wilson | 10195b1 | 2018-06-28 14:22:06 +0100 | [diff] [blame] | 51 | |
| 52 | #include <linux/stackdepot.h> |
| 53 | |
| 54 | static void vma_print_allocator(struct i915_vma *vma, const char *reason) |
| 55 | { |
| 56 | unsigned long entries[12]; |
| 57 | struct stack_trace trace = { |
| 58 | .entries = entries, |
| 59 | .max_entries = ARRAY_SIZE(entries), |
| 60 | }; |
| 61 | char buf[512]; |
| 62 | |
| 63 | if (!vma->node.stack) { |
| 64 | DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: unknown owner\n", |
| 65 | vma->node.start, vma->node.size, reason); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | depot_fetch_stack(vma->node.stack, &trace); |
| 70 | snprint_stack_trace(buf, sizeof(buf), &trace, 0); |
| 71 | DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: inserted at %s\n", |
| 72 | vma->node.start, vma->node.size, reason, buf); |
| 73 | } |
| 74 | |
| 75 | #else |
| 76 | |
| 77 | static void vma_print_allocator(struct i915_vma *vma, const char *reason) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | #endif |
| 82 | |
Chris Wilson | 64d6c50 | 2019-02-05 13:00:02 +0000 | [diff] [blame] | 83 | static void obj_bump_mru(struct drm_i915_gem_object *obj) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 84 | { |
Chris Wilson | 64d6c50 | 2019-02-05 13:00:02 +0000 | [diff] [blame] | 85 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 86 | |
Chris Wilson | 64d6c50 | 2019-02-05 13:00:02 +0000 | [diff] [blame] | 87 | spin_lock(&i915->mm.obj_lock); |
| 88 | if (obj->bind_count) |
| 89 | list_move_tail(&obj->mm.link, &i915->mm.bound_list); |
| 90 | spin_unlock(&i915->mm.obj_lock); |
| 91 | |
| 92 | obj->mm.dirty = true; /* be paranoid */ |
| 93 | } |
| 94 | |
| 95 | static void __i915_vma_retire(struct i915_active *ref) |
| 96 | { |
| 97 | struct i915_vma *vma = container_of(ref, typeof(*vma), active); |
| 98 | struct drm_i915_gem_object *obj = vma->obj; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 99 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 100 | GEM_BUG_ON(!i915_gem_object_is_active(obj)); |
| 101 | if (--obj->active_count) |
| 102 | return; |
| 103 | |
Chris Wilson | 1ab2235 | 2017-11-07 22:06:56 +0000 | [diff] [blame] | 104 | /* Prune the shared fence arrays iff completely idle (inc. external) */ |
| 105 | if (reservation_object_trylock(obj->resv)) { |
| 106 | if (reservation_object_test_signaled_rcu(obj->resv, true)) |
| 107 | reservation_object_add_excl_fence(obj->resv, NULL); |
| 108 | reservation_object_unlock(obj->resv); |
| 109 | } |
| 110 | |
Chris Wilson | 64d6c50 | 2019-02-05 13:00:02 +0000 | [diff] [blame] | 111 | /* |
| 112 | * Bump our place on the bound list to keep it roughly in LRU order |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 113 | * so that we don't steal from recently used but inactive objects |
| 114 | * (unless we are forced to ofc!) |
| 115 | */ |
Chris Wilson | 64d6c50 | 2019-02-05 13:00:02 +0000 | [diff] [blame] | 116 | obj_bump_mru(obj); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 117 | |
| 118 | if (i915_gem_object_has_active_reference(obj)) { |
| 119 | i915_gem_object_clear_active_reference(obj); |
| 120 | i915_gem_object_put(obj); |
| 121 | } |
| 122 | } |
| 123 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 124 | static struct i915_vma * |
Chris Wilson | a01cb37a | 2017-01-16 15:21:30 +0000 | [diff] [blame] | 125 | vma_create(struct drm_i915_gem_object *obj, |
| 126 | struct i915_address_space *vm, |
| 127 | const struct i915_ggtt_view *view) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 128 | { |
| 129 | struct i915_vma *vma; |
| 130 | struct rb_node *rb, **p; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 131 | |
Chris Wilson | e1cc3db | 2017-02-09 11:19:33 +0000 | [diff] [blame] | 132 | /* The aliasing_ppgtt should never be used directly! */ |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 133 | GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->vm); |
Chris Wilson | e1cc3db | 2017-02-09 11:19:33 +0000 | [diff] [blame] | 134 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 135 | vma = i915_vma_alloc(); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 136 | if (vma == NULL) |
| 137 | return ERR_PTR(-ENOMEM); |
| 138 | |
Chris Wilson | 64d6c50 | 2019-02-05 13:00:02 +0000 | [diff] [blame] | 139 | i915_active_init(vm->i915, &vma->active, __i915_vma_retire); |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 140 | INIT_ACTIVE_REQUEST(&vma->last_fence); |
Chris Wilson | 64d6c50 | 2019-02-05 13:00:02 +0000 | [diff] [blame] | 141 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 142 | vma->vm = vm; |
Chris Wilson | 93f2cde | 2018-06-07 16:40:46 +0100 | [diff] [blame] | 143 | vma->ops = &vm->vma_ops; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 144 | vma->obj = obj; |
Chris Wilson | 95ff7c7 | 2017-06-16 15:05:25 +0100 | [diff] [blame] | 145 | vma->resv = obj->resv; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 146 | vma->size = obj->base.size; |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 147 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 148 | |
Chris Wilson | 7c51846 | 2017-01-23 14:52:45 +0000 | [diff] [blame] | 149 | if (view && view->type != I915_GGTT_VIEW_NORMAL) { |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 150 | vma->ggtt_view = *view; |
| 151 | if (view->type == I915_GGTT_VIEW_PARTIAL) { |
Chris Wilson | 07e19ea | 2016-12-23 14:57:59 +0000 | [diff] [blame] | 152 | GEM_BUG_ON(range_overflows_t(u64, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 153 | view->partial.offset, |
| 154 | view->partial.size, |
Chris Wilson | 07e19ea | 2016-12-23 14:57:59 +0000 | [diff] [blame] | 155 | obj->base.size >> PAGE_SHIFT)); |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 156 | vma->size = view->partial.size; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 157 | vma->size <<= PAGE_SHIFT; |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 158 | GEM_BUG_ON(vma->size > obj->base.size); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 159 | } else if (view->type == I915_GGTT_VIEW_ROTATED) { |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 160 | vma->size = intel_rotation_info_size(&view->rotated); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 161 | vma->size <<= PAGE_SHIFT; |
Ville Syrjälä | 1a74fc0 | 2019-05-09 15:21:52 +0300 | [diff] [blame^] | 162 | } else if (view->type == I915_GGTT_VIEW_REMAPPED) { |
| 163 | vma->size = intel_remapped_info_size(&view->remapped); |
| 164 | vma->size <<= PAGE_SHIFT; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Chris Wilson | 1fcdaa7 | 2017-01-19 19:26:56 +0000 | [diff] [blame] | 168 | if (unlikely(vma->size > vm->total)) |
| 169 | goto err_vma; |
| 170 | |
Chris Wilson | b00ddb2 | 2017-01-19 19:26:59 +0000 | [diff] [blame] | 171 | GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE)); |
| 172 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 173 | if (i915_is_ggtt(vm)) { |
Chris Wilson | 1fcdaa7 | 2017-01-19 19:26:56 +0000 | [diff] [blame] | 174 | if (unlikely(overflows_type(vma->size, u32))) |
| 175 | goto err_vma; |
| 176 | |
Chris Wilson | 91d4e0aa | 2017-01-09 16:16:13 +0000 | [diff] [blame] | 177 | vma->fence_size = i915_gem_fence_size(vm->i915, vma->size, |
| 178 | i915_gem_object_get_tiling(obj), |
| 179 | i915_gem_object_get_stride(obj)); |
Chris Wilson | 1fcdaa7 | 2017-01-19 19:26:56 +0000 | [diff] [blame] | 180 | if (unlikely(vma->fence_size < vma->size || /* overflow */ |
| 181 | vma->fence_size > vm->total)) |
| 182 | goto err_vma; |
| 183 | |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 184 | GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT)); |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 185 | |
Chris Wilson | 91d4e0aa | 2017-01-09 16:16:13 +0000 | [diff] [blame] | 186 | vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size, |
| 187 | i915_gem_object_get_tiling(obj), |
| 188 | i915_gem_object_get_stride(obj)); |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 189 | GEM_BUG_ON(!is_power_of_2(vma->fence_alignment)); |
| 190 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 191 | vma->flags |= I915_VMA_GGTT; |
| 192 | } |
| 193 | |
| 194 | spin_lock(&obj->vma.lock); |
| 195 | |
| 196 | rb = NULL; |
| 197 | p = &obj->vma.tree.rb_node; |
| 198 | while (*p) { |
| 199 | struct i915_vma *pos; |
| 200 | long cmp; |
| 201 | |
| 202 | rb = *p; |
| 203 | pos = rb_entry(rb, struct i915_vma, obj_node); |
| 204 | |
| 205 | /* |
| 206 | * If the view already exists in the tree, another thread |
| 207 | * already created a matching vma, so return the older instance |
| 208 | * and dispose of ours. |
| 209 | */ |
| 210 | cmp = i915_vma_compare(pos, vm, view); |
| 211 | if (cmp == 0) { |
| 212 | spin_unlock(&obj->vma.lock); |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 213 | i915_vma_free(vma); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 214 | return pos; |
| 215 | } |
| 216 | |
| 217 | if (cmp < 0) |
| 218 | p = &rb->rb_right; |
| 219 | else |
| 220 | p = &rb->rb_left; |
| 221 | } |
| 222 | rb_link_node(&vma->obj_node, rb, p); |
| 223 | rb_insert_color(&vma->obj_node, &obj->vma.tree); |
| 224 | |
| 225 | if (i915_vma_is_ggtt(vma)) |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 226 | /* |
| 227 | * We put the GGTT vma at the start of the vma-list, followed |
| 228 | * by the ppGGTT vma. This allows us to break early when |
| 229 | * iterating over only the GGTT vma for an object, see |
| 230 | * for_each_ggtt_vma() |
| 231 | */ |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 232 | list_add(&vma->obj_link, &obj->vma.list); |
| 233 | else |
| 234 | list_add_tail(&vma->obj_link, &obj->vma.list); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 235 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 236 | spin_unlock(&obj->vma.lock); |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 237 | |
| 238 | mutex_lock(&vm->mutex); |
Chris Wilson | 1fcdaa7 | 2017-01-19 19:26:56 +0000 | [diff] [blame] | 239 | list_add(&vma->vm_link, &vm->unbound_list); |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 240 | mutex_unlock(&vm->mutex); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 241 | |
| 242 | return vma; |
Chris Wilson | 1fcdaa7 | 2017-01-19 19:26:56 +0000 | [diff] [blame] | 243 | |
| 244 | err_vma: |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 245 | i915_vma_free(vma); |
Chris Wilson | 1fcdaa7 | 2017-01-19 19:26:56 +0000 | [diff] [blame] | 246 | return ERR_PTR(-E2BIG); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Chris Wilson | 481a6f7 | 2017-01-16 15:21:31 +0000 | [diff] [blame] | 249 | static struct i915_vma * |
| 250 | vma_lookup(struct drm_i915_gem_object *obj, |
| 251 | struct i915_address_space *vm, |
| 252 | const struct i915_ggtt_view *view) |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 253 | { |
| 254 | struct rb_node *rb; |
| 255 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 256 | rb = obj->vma.tree.rb_node; |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 257 | while (rb) { |
| 258 | struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node); |
| 259 | long cmp; |
| 260 | |
| 261 | cmp = i915_vma_compare(vma, vm, view); |
| 262 | if (cmp == 0) |
| 263 | return vma; |
| 264 | |
| 265 | if (cmp < 0) |
| 266 | rb = rb->rb_right; |
| 267 | else |
| 268 | rb = rb->rb_left; |
| 269 | } |
| 270 | |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | /** |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 275 | * i915_vma_instance - return the singleton instance of the VMA |
| 276 | * @obj: parent &struct drm_i915_gem_object to be mapped |
| 277 | * @vm: address space in which the mapping is located |
| 278 | * @view: additional mapping requirements |
| 279 | * |
| 280 | * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with |
| 281 | * the same @view characteristics. If a match is not found, one is created. |
| 282 | * Once created, the VMA is kept until either the object is freed, or the |
| 283 | * address space is closed. |
| 284 | * |
| 285 | * Must be called with struct_mutex held. |
| 286 | * |
| 287 | * Returns the vma, or an error pointer. |
| 288 | */ |
| 289 | struct i915_vma * |
| 290 | i915_vma_instance(struct drm_i915_gem_object *obj, |
| 291 | struct i915_address_space *vm, |
| 292 | const struct i915_ggtt_view *view) |
| 293 | { |
| 294 | struct i915_vma *vma; |
| 295 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 296 | GEM_BUG_ON(view && !i915_is_ggtt(vm)); |
| 297 | GEM_BUG_ON(vm->closed); |
| 298 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 299 | spin_lock(&obj->vma.lock); |
Chris Wilson | 481a6f7 | 2017-01-16 15:21:31 +0000 | [diff] [blame] | 300 | vma = vma_lookup(obj, vm, view); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 301 | spin_unlock(&obj->vma.lock); |
| 302 | |
| 303 | /* vma_create() will resolve the race if another creates the vma */ |
| 304 | if (unlikely(!vma)) |
Chris Wilson | a01cb37a | 2017-01-16 15:21:30 +0000 | [diff] [blame] | 305 | vma = vma_create(obj, vm, view); |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 306 | |
Chris Wilson | 4ea9527 | 2017-01-16 15:21:29 +0000 | [diff] [blame] | 307 | GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view)); |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 308 | return vma; |
| 309 | } |
| 310 | |
| 311 | /** |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 312 | * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space. |
| 313 | * @vma: VMA to map |
| 314 | * @cache_level: mapping cache level |
| 315 | * @flags: flags like global or local mapping |
| 316 | * |
| 317 | * DMA addresses are taken from the scatter-gather table of this object (or of |
| 318 | * this VMA in case of non-default GGTT views) and PTE entries set up. |
| 319 | * Note that DMA addresses are also the only part of the SG table we care about. |
| 320 | */ |
| 321 | int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level, |
| 322 | u32 flags) |
| 323 | { |
| 324 | u32 bind_flags; |
| 325 | u32 vma_flags; |
| 326 | int ret; |
| 327 | |
Chris Wilson | aa14943 | 2017-02-25 18:11:21 +0000 | [diff] [blame] | 328 | GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); |
| 329 | GEM_BUG_ON(vma->size > vma->node.size); |
| 330 | |
Tvrtko Ursulin | bbb8a9d | 2018-10-12 07:31:42 +0100 | [diff] [blame] | 331 | if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start, |
| 332 | vma->node.size, |
| 333 | vma->vm->total))) |
Chris Wilson | aa14943 | 2017-02-25 18:11:21 +0000 | [diff] [blame] | 334 | return -ENODEV; |
| 335 | |
Tvrtko Ursulin | bbb8a9d | 2018-10-12 07:31:42 +0100 | [diff] [blame] | 336 | if (GEM_DEBUG_WARN_ON(!flags)) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 337 | return -EINVAL; |
| 338 | |
| 339 | bind_flags = 0; |
| 340 | if (flags & PIN_GLOBAL) |
| 341 | bind_flags |= I915_VMA_GLOBAL_BIND; |
| 342 | if (flags & PIN_USER) |
| 343 | bind_flags |= I915_VMA_LOCAL_BIND; |
| 344 | |
| 345 | vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND); |
| 346 | if (flags & PIN_UPDATE) |
| 347 | bind_flags |= vma_flags; |
| 348 | else |
| 349 | bind_flags &= ~vma_flags; |
| 350 | if (bind_flags == 0) |
| 351 | return 0; |
| 352 | |
Matthew Auld | fa3f46a | 2017-10-06 23:18:19 +0100 | [diff] [blame] | 353 | GEM_BUG_ON(!vma->pages); |
| 354 | |
Daniele Ceraolo Spurio | 6146e6d | 2017-01-20 13:51:23 -0800 | [diff] [blame] | 355 | trace_i915_vma_bind(vma, bind_flags); |
Chris Wilson | 93f2cde | 2018-06-07 16:40:46 +0100 | [diff] [blame] | 356 | ret = vma->ops->bind_vma(vma, cache_level, bind_flags); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 357 | if (ret) |
| 358 | return ret; |
| 359 | |
| 360 | vma->flags |= bind_flags; |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | void __iomem *i915_vma_pin_iomap(struct i915_vma *vma) |
| 365 | { |
| 366 | void __iomem *ptr; |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 367 | int err; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 368 | |
| 369 | /* Access through the GTT requires the device to be awake. */ |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 370 | assert_rpm_wakelock_held(vma->vm->i915); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 371 | |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 372 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 373 | if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) { |
| 374 | err = -ENODEV; |
| 375 | goto err; |
| 376 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 377 | |
| 378 | GEM_BUG_ON(!i915_vma_is_ggtt(vma)); |
| 379 | GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0); |
| 380 | |
| 381 | ptr = vma->iomap; |
| 382 | if (ptr == NULL) { |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 383 | ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap, |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 384 | vma->node.start, |
| 385 | vma->node.size); |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 386 | if (ptr == NULL) { |
| 387 | err = -ENOMEM; |
| 388 | goto err; |
| 389 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 390 | |
| 391 | vma->iomap = ptr; |
| 392 | } |
| 393 | |
| 394 | __i915_vma_pin(vma); |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 395 | |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 396 | err = i915_vma_pin_fence(vma); |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 397 | if (err) |
| 398 | goto err_unpin; |
| 399 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 400 | i915_vma_set_ggtt_write(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 401 | return ptr; |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 402 | |
| 403 | err_unpin: |
| 404 | __i915_vma_unpin(vma); |
| 405 | err: |
| 406 | return IO_ERR_PTR(err); |
| 407 | } |
| 408 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 409 | void i915_vma_flush_writes(struct i915_vma *vma) |
| 410 | { |
| 411 | if (!i915_vma_has_ggtt_write(vma)) |
| 412 | return; |
| 413 | |
| 414 | i915_gem_flush_ggtt_writes(vma->vm->i915); |
| 415 | |
| 416 | i915_vma_unset_ggtt_write(vma); |
| 417 | } |
| 418 | |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 419 | void i915_vma_unpin_iomap(struct i915_vma *vma) |
| 420 | { |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 421 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 422 | |
| 423 | GEM_BUG_ON(vma->iomap == NULL); |
| 424 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 425 | i915_vma_flush_writes(vma); |
| 426 | |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 427 | i915_vma_unpin_fence(vma); |
| 428 | i915_vma_unpin(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Chris Wilson | 6a2f59e | 2018-07-21 13:50:37 +0100 | [diff] [blame] | 431 | void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 432 | { |
| 433 | struct i915_vma *vma; |
| 434 | struct drm_i915_gem_object *obj; |
| 435 | |
| 436 | vma = fetch_and_zero(p_vma); |
| 437 | if (!vma) |
| 438 | return; |
| 439 | |
| 440 | obj = vma->obj; |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 441 | GEM_BUG_ON(!obj); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 442 | |
| 443 | i915_vma_unpin(vma); |
| 444 | i915_vma_close(vma); |
| 445 | |
Chris Wilson | 6a2f59e | 2018-07-21 13:50:37 +0100 | [diff] [blame] | 446 | if (flags & I915_VMA_RELEASE_MAP) |
| 447 | i915_gem_object_unpin_map(obj); |
| 448 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 449 | __i915_gem_object_release_unless_active(obj); |
| 450 | } |
| 451 | |
Chris Wilson | 782a3e9 | 2017-02-13 17:15:46 +0000 | [diff] [blame] | 452 | bool i915_vma_misplaced(const struct i915_vma *vma, |
| 453 | u64 size, u64 alignment, u64 flags) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 454 | { |
| 455 | if (!drm_mm_node_allocated(&vma->node)) |
| 456 | return false; |
| 457 | |
| 458 | if (vma->node.size < size) |
| 459 | return true; |
| 460 | |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 461 | GEM_BUG_ON(alignment && !is_power_of_2(alignment)); |
| 462 | if (alignment && !IS_ALIGNED(vma->node.start, alignment)) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 463 | return true; |
| 464 | |
| 465 | if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma)) |
| 466 | return true; |
| 467 | |
| 468 | if (flags & PIN_OFFSET_BIAS && |
| 469 | vma->node.start < (flags & PIN_OFFSET_MASK)) |
| 470 | return true; |
| 471 | |
| 472 | if (flags & PIN_OFFSET_FIXED && |
| 473 | vma->node.start != (flags & PIN_OFFSET_MASK)) |
| 474 | return true; |
| 475 | |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | void __i915_vma_set_map_and_fenceable(struct i915_vma *vma) |
| 480 | { |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 481 | bool mappable, fenceable; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 482 | |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 483 | GEM_BUG_ON(!i915_vma_is_ggtt(vma)); |
| 484 | GEM_BUG_ON(!vma->fence_size); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 485 | |
| 486 | /* |
| 487 | * Explicitly disable for rotated VMA since the display does not |
| 488 | * need the fence and the VMA is not accessible to other users. |
| 489 | */ |
Ville Syrjälä | 1a74fc0 | 2019-05-09 15:21:52 +0300 | [diff] [blame^] | 490 | if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED || |
| 491 | vma->ggtt_view.type == I915_GGTT_VIEW_REMAPPED) |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 492 | return; |
| 493 | |
| 494 | fenceable = (vma->node.size >= vma->fence_size && |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 495 | IS_ALIGNED(vma->node.start, vma->fence_alignment)); |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 496 | |
| 497 | mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end; |
| 498 | |
| 499 | if (mappable && fenceable) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 500 | vma->flags |= I915_VMA_CAN_FENCE; |
| 501 | else |
| 502 | vma->flags &= ~I915_VMA_CAN_FENCE; |
| 503 | } |
| 504 | |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 505 | static bool color_differs(struct drm_mm_node *node, unsigned long color) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 506 | { |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 507 | return node->allocated && node->color != color; |
| 508 | } |
| 509 | |
| 510 | bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level) |
| 511 | { |
| 512 | struct drm_mm_node *node = &vma->node; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 513 | struct drm_mm_node *other; |
| 514 | |
| 515 | /* |
| 516 | * On some machines we have to be careful when putting differing types |
| 517 | * of snoopable memory together to avoid the prefetcher crossing memory |
| 518 | * domains and dying. During vm initialisation, we decide whether or not |
| 519 | * these constraints apply and set the drm_mm.color_adjust |
| 520 | * appropriately. |
| 521 | */ |
| 522 | if (vma->vm->mm.color_adjust == NULL) |
| 523 | return true; |
| 524 | |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 525 | /* Only valid to be called on an already inserted vma */ |
| 526 | GEM_BUG_ON(!drm_mm_node_allocated(node)); |
| 527 | GEM_BUG_ON(list_empty(&node->node_list)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 528 | |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 529 | other = list_prev_entry(node, node_list); |
Daniel Vetter | ef426c1 | 2017-01-04 11:41:10 +0100 | [diff] [blame] | 530 | if (color_differs(other, cache_level) && !drm_mm_hole_follows(other)) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 531 | return false; |
| 532 | |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 533 | other = list_next_entry(node, node_list); |
Daniel Vetter | ef426c1 | 2017-01-04 11:41:10 +0100 | [diff] [blame] | 534 | if (color_differs(other, cache_level) && !drm_mm_hole_follows(node)) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 535 | return false; |
| 536 | |
| 537 | return true; |
| 538 | } |
| 539 | |
Chris Wilson | 83d317a | 2018-06-05 10:41:07 +0100 | [diff] [blame] | 540 | static void assert_bind_count(const struct drm_i915_gem_object *obj) |
| 541 | { |
| 542 | /* |
| 543 | * Combine the assertion that the object is bound and that we have |
| 544 | * pinned its pages. But we should never have bound the object |
| 545 | * more than we have pinned its pages. (For complete accuracy, we |
| 546 | * assume that no else is pinning the pages, but as a rough assertion |
| 547 | * that we will not run into problems later, this will do!) |
| 548 | */ |
| 549 | GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count); |
| 550 | } |
| 551 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 552 | /** |
| 553 | * i915_vma_insert - finds a slot for the vma in its address space |
| 554 | * @vma: the vma |
| 555 | * @size: requested size in bytes (can be larger than the VMA) |
| 556 | * @alignment: required alignment |
| 557 | * @flags: mask of PIN_* flags to use |
| 558 | * |
| 559 | * First we try to allocate some free space that meets the requirements for |
| 560 | * the VMA. Failiing that, if the flags permit, it will evict an old VMA, |
| 561 | * preferrably the oldest idle entry to make room for the new VMA. |
| 562 | * |
| 563 | * Returns: |
| 564 | * 0 on success, negative error code otherwise. |
| 565 | */ |
| 566 | static int |
| 567 | i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags) |
| 568 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 569 | struct drm_i915_private *dev_priv = vma->vm->i915; |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 570 | unsigned int cache_level; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 571 | u64 start, end; |
| 572 | int ret; |
| 573 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 574 | GEM_BUG_ON(i915_vma_is_closed(vma)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 575 | GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND)); |
| 576 | GEM_BUG_ON(drm_mm_node_allocated(&vma->node)); |
| 577 | |
| 578 | size = max(size, vma->size); |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 579 | alignment = max(alignment, vma->display_alignment); |
| 580 | if (flags & PIN_MAPPABLE) { |
| 581 | size = max_t(typeof(size), size, vma->fence_size); |
| 582 | alignment = max_t(typeof(alignment), |
| 583 | alignment, vma->fence_alignment); |
| 584 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 585 | |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 586 | GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE)); |
| 587 | GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT)); |
| 588 | GEM_BUG_ON(!is_power_of_2(alignment)); |
| 589 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 590 | start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0; |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 591 | GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 592 | |
| 593 | end = vma->vm->total; |
| 594 | if (flags & PIN_MAPPABLE) |
| 595 | end = min_t(u64, end, dev_priv->ggtt.mappable_end); |
| 596 | if (flags & PIN_ZONE_4G) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 597 | end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE); |
| 598 | GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 599 | |
| 600 | /* If binding the object/GGTT view requires more space than the entire |
| 601 | * aperture has, reject it early before evicting everything in a vain |
| 602 | * attempt to find space. |
| 603 | */ |
| 604 | if (size > end) { |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 605 | DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n", |
| 606 | size, flags & PIN_MAPPABLE ? "mappable" : "total", |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 607 | end); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 608 | return -ENOSPC; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 609 | } |
| 610 | |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 611 | if (vma->obj) { |
| 612 | ret = i915_gem_object_pin_pages(vma->obj); |
| 613 | if (ret) |
| 614 | return ret; |
| 615 | |
| 616 | cache_level = vma->obj->cache_level; |
| 617 | } else { |
| 618 | cache_level = 0; |
| 619 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 620 | |
Matthew Auld | fa3f46a | 2017-10-06 23:18:19 +0100 | [diff] [blame] | 621 | GEM_BUG_ON(vma->pages); |
| 622 | |
Chris Wilson | 93f2cde | 2018-06-07 16:40:46 +0100 | [diff] [blame] | 623 | ret = vma->ops->set_pages(vma); |
Matthew Auld | fa3f46a | 2017-10-06 23:18:19 +0100 | [diff] [blame] | 624 | if (ret) |
| 625 | goto err_unpin; |
| 626 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 627 | if (flags & PIN_OFFSET_FIXED) { |
| 628 | u64 offset = flags & PIN_OFFSET_MASK; |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 629 | if (!IS_ALIGNED(offset, alignment) || |
Chris Wilson | e8f9ae9 | 2017-01-06 15:20:12 +0000 | [diff] [blame] | 630 | range_overflows(offset, size, end)) { |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 631 | ret = -EINVAL; |
Matthew Auld | fa3f46a | 2017-10-06 23:18:19 +0100 | [diff] [blame] | 632 | goto err_clear; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 633 | } |
| 634 | |
Chris Wilson | 625d988 | 2017-01-11 11:23:11 +0000 | [diff] [blame] | 635 | ret = i915_gem_gtt_reserve(vma->vm, &vma->node, |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 636 | size, offset, cache_level, |
Chris Wilson | 625d988 | 2017-01-11 11:23:11 +0000 | [diff] [blame] | 637 | flags); |
| 638 | if (ret) |
Matthew Auld | fa3f46a | 2017-10-06 23:18:19 +0100 | [diff] [blame] | 639 | goto err_clear; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 640 | } else { |
Matthew Auld | 7464284 | 2017-10-06 23:18:20 +0100 | [diff] [blame] | 641 | /* |
| 642 | * We only support huge gtt pages through the 48b PPGTT, |
| 643 | * however we also don't want to force any alignment for |
| 644 | * objects which need to be tightly packed into the low 32bits. |
| 645 | * |
| 646 | * Note that we assume that GGTT are limited to 4GiB for the |
| 647 | * forseeable future. See also i915_ggtt_offset(). |
| 648 | */ |
| 649 | if (upper_32_bits(end - 1) && |
| 650 | vma->page_sizes.sg > I915_GTT_PAGE_SIZE) { |
Matthew Auld | 855822b | 2017-10-06 23:18:21 +0100 | [diff] [blame] | 651 | /* |
| 652 | * We can't mix 64K and 4K PTEs in the same page-table |
| 653 | * (2M block), and so to avoid the ugliness and |
| 654 | * complexity of coloring we opt for just aligning 64K |
| 655 | * objects to 2M. |
| 656 | */ |
Matthew Auld | 7464284 | 2017-10-06 23:18:20 +0100 | [diff] [blame] | 657 | u64 page_alignment = |
Matthew Auld | 855822b | 2017-10-06 23:18:21 +0100 | [diff] [blame] | 658 | rounddown_pow_of_two(vma->page_sizes.sg | |
| 659 | I915_GTT_PAGE_SIZE_2M); |
Matthew Auld | 7464284 | 2017-10-06 23:18:20 +0100 | [diff] [blame] | 660 | |
Chris Wilson | bef27bdb | 2017-10-09 10:20:19 +0100 | [diff] [blame] | 661 | /* |
| 662 | * Check we don't expand for the limited Global GTT |
| 663 | * (mappable aperture is even more precious!). This |
| 664 | * also checks that we exclude the aliasing-ppgtt. |
| 665 | */ |
| 666 | GEM_BUG_ON(i915_vma_is_ggtt(vma)); |
| 667 | |
Matthew Auld | 7464284 | 2017-10-06 23:18:20 +0100 | [diff] [blame] | 668 | alignment = max(alignment, page_alignment); |
Matthew Auld | 855822b | 2017-10-06 23:18:21 +0100 | [diff] [blame] | 669 | |
| 670 | if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K) |
| 671 | size = round_up(size, I915_GTT_PAGE_SIZE_2M); |
Matthew Auld | 7464284 | 2017-10-06 23:18:20 +0100 | [diff] [blame] | 672 | } |
| 673 | |
Chris Wilson | e007b19 | 2017-01-11 11:23:10 +0000 | [diff] [blame] | 674 | ret = i915_gem_gtt_insert(vma->vm, &vma->node, |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 675 | size, alignment, cache_level, |
Chris Wilson | e007b19 | 2017-01-11 11:23:10 +0000 | [diff] [blame] | 676 | start, end, flags); |
| 677 | if (ret) |
Matthew Auld | fa3f46a | 2017-10-06 23:18:19 +0100 | [diff] [blame] | 678 | goto err_clear; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 679 | |
| 680 | GEM_BUG_ON(vma->node.start < start); |
| 681 | GEM_BUG_ON(vma->node.start + vma->node.size > end); |
| 682 | } |
Chris Wilson | 44a0ec0 | 2017-01-19 19:26:58 +0000 | [diff] [blame] | 683 | GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 684 | GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, cache_level)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 685 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 686 | mutex_lock(&vma->vm->mutex); |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 687 | list_move_tail(&vma->vm_link, &vma->vm->bound_list); |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 688 | mutex_unlock(&vma->vm->mutex); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 689 | |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 690 | if (vma->obj) { |
| 691 | struct drm_i915_gem_object *obj = vma->obj; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 692 | |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 693 | spin_lock(&dev_priv->mm.obj_lock); |
| 694 | list_move_tail(&obj->mm.link, &dev_priv->mm.bound_list); |
| 695 | obj->bind_count++; |
| 696 | spin_unlock(&dev_priv->mm.obj_lock); |
| 697 | |
| 698 | assert_bind_count(obj); |
| 699 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 700 | |
| 701 | return 0; |
| 702 | |
Matthew Auld | fa3f46a | 2017-10-06 23:18:19 +0100 | [diff] [blame] | 703 | err_clear: |
Chris Wilson | 93f2cde | 2018-06-07 16:40:46 +0100 | [diff] [blame] | 704 | vma->ops->clear_pages(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 705 | err_unpin: |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 706 | if (vma->obj) |
| 707 | i915_gem_object_unpin_pages(vma->obj); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 708 | return ret; |
| 709 | } |
| 710 | |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 711 | static void |
| 712 | i915_vma_remove(struct i915_vma *vma) |
| 713 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 714 | struct drm_i915_private *i915 = vma->vm->i915; |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 715 | |
| 716 | GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); |
| 717 | GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND)); |
| 718 | |
Chris Wilson | 93f2cde | 2018-06-07 16:40:46 +0100 | [diff] [blame] | 719 | vma->ops->clear_pages(vma); |
Matthew Auld | fa3f46a | 2017-10-06 23:18:19 +0100 | [diff] [blame] | 720 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 721 | mutex_lock(&vma->vm->mutex); |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 722 | drm_mm_remove_node(&vma->node); |
| 723 | list_move_tail(&vma->vm_link, &vma->vm->unbound_list); |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 724 | mutex_unlock(&vma->vm->mutex); |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 725 | |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 726 | /* |
| 727 | * Since the unbound list is global, only move to that list if |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 728 | * no more VMAs exist. |
| 729 | */ |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 730 | if (vma->obj) { |
| 731 | struct drm_i915_gem_object *obj = vma->obj; |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 732 | |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 733 | spin_lock(&i915->mm.obj_lock); |
| 734 | if (--obj->bind_count == 0) |
| 735 | list_move_tail(&obj->mm.link, &i915->mm.unbound_list); |
| 736 | spin_unlock(&i915->mm.obj_lock); |
| 737 | |
| 738 | /* |
| 739 | * And finally now the object is completely decoupled from this |
| 740 | * vma, we can drop its hold on the backing storage and allow |
| 741 | * it to be reaped by the shrinker. |
| 742 | */ |
| 743 | i915_gem_object_unpin_pages(obj); |
| 744 | assert_bind_count(obj); |
| 745 | } |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 748 | int __i915_vma_do_pin(struct i915_vma *vma, |
| 749 | u64 size, u64 alignment, u64 flags) |
| 750 | { |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 751 | const unsigned int bound = vma->flags; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 752 | int ret; |
| 753 | |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 754 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 755 | GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0); |
| 756 | GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma)); |
| 757 | |
| 758 | if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) { |
| 759 | ret = -EBUSY; |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 760 | goto err_unpin; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | if ((bound & I915_VMA_BIND_MASK) == 0) { |
| 764 | ret = i915_vma_insert(vma, size, alignment, flags); |
| 765 | if (ret) |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 766 | goto err_unpin; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 767 | } |
Chris Wilson | d36caee | 2017-11-05 12:45:50 +0000 | [diff] [blame] | 768 | GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 769 | |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 770 | ret = i915_vma_bind(vma, vma->obj ? vma->obj->cache_level : 0, flags); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 771 | if (ret) |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 772 | goto err_remove; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 773 | |
Chris Wilson | d36caee | 2017-11-05 12:45:50 +0000 | [diff] [blame] | 774 | GEM_BUG_ON((vma->flags & I915_VMA_BIND_MASK) == 0); |
| 775 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 776 | if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND) |
| 777 | __i915_vma_set_map_and_fenceable(vma); |
| 778 | |
| 779 | GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags)); |
| 780 | return 0; |
| 781 | |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 782 | err_remove: |
| 783 | if ((bound & I915_VMA_BIND_MASK) == 0) { |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 784 | i915_vma_remove(vma); |
Matthew Auld | fa3f46a | 2017-10-06 23:18:19 +0100 | [diff] [blame] | 785 | GEM_BUG_ON(vma->pages); |
Chris Wilson | d36caee | 2017-11-05 12:45:50 +0000 | [diff] [blame] | 786 | GEM_BUG_ON(vma->flags & I915_VMA_BIND_MASK); |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 787 | } |
| 788 | err_unpin: |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 789 | __i915_vma_unpin(vma); |
| 790 | return ret; |
| 791 | } |
| 792 | |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 793 | void i915_vma_close(struct i915_vma *vma) |
| 794 | { |
| 795 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
| 796 | |
| 797 | GEM_BUG_ON(i915_vma_is_closed(vma)); |
| 798 | vma->flags |= I915_VMA_CLOSED; |
| 799 | |
| 800 | /* |
| 801 | * We defer actually closing, unbinding and destroying the VMA until |
| 802 | * the next idle point, or if the object is freed in the meantime. By |
| 803 | * postponing the unbind, we allow for it to be resurrected by the |
| 804 | * client, avoiding the work required to rebind the VMA. This is |
| 805 | * advantageous for DRI, where the client/server pass objects |
| 806 | * between themselves, temporarily opening a local VMA to the |
| 807 | * object, and then closing it again. The same object is then reused |
| 808 | * on the next frame (or two, depending on the depth of the swap queue) |
| 809 | * causing us to rebind the VMA once more. This ends up being a lot |
| 810 | * of wasted work for the steady state. |
| 811 | */ |
| 812 | list_add_tail(&vma->closed_link, &vma->vm->i915->gt.closed_vma); |
| 813 | } |
| 814 | |
| 815 | void i915_vma_reopen(struct i915_vma *vma) |
| 816 | { |
| 817 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
| 818 | |
| 819 | if (vma->flags & I915_VMA_CLOSED) { |
| 820 | vma->flags &= ~I915_VMA_CLOSED; |
| 821 | list_del(&vma->closed_link); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | static void __i915_vma_destroy(struct i915_vma *vma) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 826 | { |
| 827 | GEM_BUG_ON(vma->node.allocated); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 828 | GEM_BUG_ON(vma->fence); |
| 829 | |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 830 | GEM_BUG_ON(i915_active_request_isset(&vma->last_fence)); |
Chris Wilson | 7a3bc03 | 2017-06-20 13:43:21 +0100 | [diff] [blame] | 831 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 832 | mutex_lock(&vma->vm->mutex); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 833 | list_del(&vma->vm_link); |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 834 | mutex_unlock(&vma->vm->mutex); |
| 835 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 836 | if (vma->obj) { |
| 837 | struct drm_i915_gem_object *obj = vma->obj; |
| 838 | |
| 839 | spin_lock(&obj->vma.lock); |
| 840 | list_del(&vma->obj_link); |
| 841 | rb_erase(&vma->obj_node, &vma->obj->vma.tree); |
| 842 | spin_unlock(&obj->vma.lock); |
| 843 | } |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 844 | |
Chris Wilson | 64d6c50 | 2019-02-05 13:00:02 +0000 | [diff] [blame] | 845 | i915_active_fini(&vma->active); |
Chris Wilson | 5c3f8c2 | 2018-07-06 11:39:46 +0100 | [diff] [blame] | 846 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 847 | i915_vma_free(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 848 | } |
| 849 | |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 850 | void i915_vma_destroy(struct i915_vma *vma) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 851 | { |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 852 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 853 | |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 854 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 855 | GEM_BUG_ON(i915_vma_is_pinned(vma)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 856 | |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 857 | if (i915_vma_is_closed(vma)) |
| 858 | list_del(&vma->closed_link); |
| 859 | |
| 860 | WARN_ON(i915_vma_unbind(vma)); |
| 861 | __i915_vma_destroy(vma); |
| 862 | } |
| 863 | |
| 864 | void i915_vma_parked(struct drm_i915_private *i915) |
| 865 | { |
| 866 | struct i915_vma *vma, *next; |
| 867 | |
| 868 | list_for_each_entry_safe(vma, next, &i915->gt.closed_vma, closed_link) { |
| 869 | GEM_BUG_ON(!i915_vma_is_closed(vma)); |
| 870 | i915_vma_destroy(vma); |
| 871 | } |
| 872 | |
| 873 | GEM_BUG_ON(!list_empty(&i915->gt.closed_vma)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | static void __i915_vma_iounmap(struct i915_vma *vma) |
| 877 | { |
| 878 | GEM_BUG_ON(i915_vma_is_pinned(vma)); |
| 879 | |
| 880 | if (vma->iomap == NULL) |
| 881 | return; |
| 882 | |
| 883 | io_mapping_unmap(vma->iomap); |
| 884 | vma->iomap = NULL; |
| 885 | } |
| 886 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 887 | void i915_vma_revoke_mmap(struct i915_vma *vma) |
| 888 | { |
| 889 | struct drm_vma_offset_node *node = &vma->obj->base.vma_node; |
| 890 | u64 vma_offset; |
| 891 | |
| 892 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
| 893 | |
| 894 | if (!i915_vma_has_userfault(vma)) |
| 895 | return; |
| 896 | |
| 897 | GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma)); |
| 898 | GEM_BUG_ON(!vma->obj->userfault_count); |
| 899 | |
| 900 | vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT; |
| 901 | unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping, |
| 902 | drm_vma_node_offset_addr(node) + vma_offset, |
| 903 | vma->size, |
| 904 | 1); |
| 905 | |
| 906 | i915_vma_unset_userfault(vma); |
| 907 | if (!--vma->obj->userfault_count) |
| 908 | list_del(&vma->obj->userfault_link); |
| 909 | } |
| 910 | |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 911 | static void export_fence(struct i915_vma *vma, |
| 912 | struct i915_request *rq, |
| 913 | unsigned int flags) |
| 914 | { |
| 915 | struct reservation_object *resv = vma->resv; |
| 916 | |
| 917 | /* |
| 918 | * Ignore errors from failing to allocate the new fence, we can't |
| 919 | * handle an error right now. Worst case should be missed |
| 920 | * synchronisation leading to rendering corruption. |
| 921 | */ |
| 922 | reservation_object_lock(resv, NULL); |
| 923 | if (flags & EXEC_OBJECT_WRITE) |
| 924 | reservation_object_add_excl_fence(resv, &rq->fence); |
Christian König | ca05359 | 2018-09-19 16:12:25 +0200 | [diff] [blame] | 925 | else if (reservation_object_reserve_shared(resv, 1) == 0) |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 926 | reservation_object_add_shared_fence(resv, &rq->fence); |
| 927 | reservation_object_unlock(resv); |
| 928 | } |
| 929 | |
| 930 | int i915_vma_move_to_active(struct i915_vma *vma, |
| 931 | struct i915_request *rq, |
| 932 | unsigned int flags) |
| 933 | { |
| 934 | struct drm_i915_gem_object *obj = vma->obj; |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 935 | |
| 936 | lockdep_assert_held(&rq->i915->drm.struct_mutex); |
| 937 | GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); |
| 938 | |
| 939 | /* |
| 940 | * Add a reference if we're newly entering the active list. |
| 941 | * The order in which we add operations to the retirement queue is |
| 942 | * vital here: mark_active adds to the start of the callback list, |
| 943 | * such that subsequent callbacks are called first. Therefore we |
| 944 | * add the active reference first and queue for it to be dropped |
| 945 | * *last*. |
| 946 | */ |
Chris Wilson | 64d6c50 | 2019-02-05 13:00:02 +0000 | [diff] [blame] | 947 | if (!vma->active.count) |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 948 | obj->active_count++; |
Chris Wilson | 64d6c50 | 2019-02-05 13:00:02 +0000 | [diff] [blame] | 949 | |
| 950 | if (unlikely(i915_active_ref(&vma->active, rq->fence.context, rq))) { |
| 951 | if (!vma->active.count) |
| 952 | obj->active_count--; |
| 953 | return -ENOMEM; |
| 954 | } |
| 955 | |
Chris Wilson | 5c3f8c2 | 2018-07-06 11:39:46 +0100 | [diff] [blame] | 956 | GEM_BUG_ON(!i915_vma_is_active(vma)); |
| 957 | GEM_BUG_ON(!obj->active_count); |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 958 | |
| 959 | obj->write_domain = 0; |
| 960 | if (flags & EXEC_OBJECT_WRITE) { |
| 961 | obj->write_domain = I915_GEM_DOMAIN_RENDER; |
| 962 | |
| 963 | if (intel_fb_obj_invalidate(obj, ORIGIN_CS)) |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 964 | __i915_active_request_set(&obj->frontbuffer_write, rq); |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 965 | |
| 966 | obj->read_domains = 0; |
| 967 | } |
| 968 | obj->read_domains |= I915_GEM_GPU_DOMAINS; |
| 969 | |
| 970 | if (flags & EXEC_OBJECT_NEEDS_FENCE) |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 971 | __i915_active_request_set(&vma->last_fence, rq); |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 972 | |
| 973 | export_fence(vma, rq, flags); |
| 974 | return 0; |
| 975 | } |
| 976 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 977 | int i915_vma_unbind(struct i915_vma *vma) |
| 978 | { |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 979 | int ret; |
| 980 | |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 981 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 982 | |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 983 | /* |
| 984 | * First wait upon any activity as retiring the request may |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 985 | * have side-effects such as unpinning or even unbinding this vma. |
| 986 | */ |
Chris Wilson | 7f017b1 | 2017-11-09 21:34:50 +0000 | [diff] [blame] | 987 | might_sleep(); |
Chris Wilson | 5c3f8c2 | 2018-07-06 11:39:46 +0100 | [diff] [blame] | 988 | if (i915_vma_is_active(vma)) { |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 989 | /* |
| 990 | * When a closed VMA is retired, it is unbound - eek. |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 991 | * In order to prevent it from being recursively closed, |
| 992 | * take a pin on the vma so that the second unbind is |
| 993 | * aborted. |
| 994 | * |
| 995 | * Even more scary is that the retire callback may free |
| 996 | * the object (last active vma). To prevent the explosion |
| 997 | * we defer the actual object free to a worker that can |
| 998 | * only proceed once it acquires the struct_mutex (which |
| 999 | * we currently hold, therefore it cannot free this object |
| 1000 | * before we are finished). |
| 1001 | */ |
| 1002 | __i915_vma_pin(vma); |
| 1003 | |
Chris Wilson | 64d6c50 | 2019-02-05 13:00:02 +0000 | [diff] [blame] | 1004 | ret = i915_active_wait(&vma->active); |
Chris Wilson | 8b293eb | 2018-07-06 13:31:57 +0100 | [diff] [blame] | 1005 | if (ret) |
| 1006 | goto unpin; |
| 1007 | |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 1008 | ret = i915_active_request_retire(&vma->last_fence, |
| 1009 | &vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 5c3f8c2 | 2018-07-06 11:39:46 +0100 | [diff] [blame] | 1010 | unpin: |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1011 | __i915_vma_unpin(vma); |
| 1012 | if (ret) |
| 1013 | return ret; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1014 | } |
Chris Wilson | 7a3bc03 | 2017-06-20 13:43:21 +0100 | [diff] [blame] | 1015 | GEM_BUG_ON(i915_vma_is_active(vma)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1016 | |
Chris Wilson | 10195b1 | 2018-06-28 14:22:06 +0100 | [diff] [blame] | 1017 | if (i915_vma_is_pinned(vma)) { |
| 1018 | vma_print_allocator(vma, "is pinned"); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1019 | return -EBUSY; |
Chris Wilson | 10195b1 | 2018-06-28 14:22:06 +0100 | [diff] [blame] | 1020 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1021 | |
| 1022 | if (!drm_mm_node_allocated(&vma->node)) |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 1023 | return 0; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1024 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1025 | if (i915_vma_is_map_and_fenceable(vma)) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1026 | /* |
| 1027 | * Check that we have flushed all writes through the GGTT |
| 1028 | * before the unbind, other due to non-strict nature of those |
| 1029 | * indirect writes they may end up referencing the GGTT PTE |
| 1030 | * after the unbind. |
| 1031 | */ |
| 1032 | i915_vma_flush_writes(vma); |
| 1033 | GEM_BUG_ON(i915_vma_has_ggtt_write(vma)); |
| 1034 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1035 | /* release the fence reg _after_ flushing */ |
| 1036 | ret = i915_vma_put_fence(vma); |
| 1037 | if (ret) |
| 1038 | return ret; |
| 1039 | |
| 1040 | /* Force a pagefault for domain tracking on next user access */ |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1041 | i915_vma_revoke_mmap(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1042 | |
| 1043 | __i915_vma_iounmap(vma); |
| 1044 | vma->flags &= ~I915_VMA_CAN_FENCE; |
| 1045 | } |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1046 | GEM_BUG_ON(vma->fence); |
| 1047 | GEM_BUG_ON(i915_vma_has_userfault(vma)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1048 | |
| 1049 | if (likely(!vma->vm->closed)) { |
| 1050 | trace_i915_vma_unbind(vma); |
Chris Wilson | 93f2cde | 2018-06-07 16:40:46 +0100 | [diff] [blame] | 1051 | vma->ops->unbind_vma(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1052 | } |
| 1053 | vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND); |
| 1054 | |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 1055 | i915_vma_remove(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1056 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1057 | return 0; |
| 1058 | } |
| 1059 | |
Chris Wilson | e3c7a1c | 2017-02-13 17:15:45 +0000 | [diff] [blame] | 1060 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 1061 | #include "selftests/i915_vma.c" |
| 1062 | #endif |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 1063 | |
Chris Wilson | 103b76ee | 2019-03-05 21:38:30 +0000 | [diff] [blame] | 1064 | static void i915_global_vma_shrink(void) |
| 1065 | { |
| 1066 | kmem_cache_shrink(global.slab_vmas); |
| 1067 | } |
| 1068 | |
| 1069 | static void i915_global_vma_exit(void) |
| 1070 | { |
| 1071 | kmem_cache_destroy(global.slab_vmas); |
| 1072 | } |
| 1073 | |
| 1074 | static struct i915_global_vma global = { { |
| 1075 | .shrink = i915_global_vma_shrink, |
| 1076 | .exit = i915_global_vma_exit, |
| 1077 | } }; |
| 1078 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 1079 | int __init i915_global_vma_init(void) |
| 1080 | { |
| 1081 | global.slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 1082 | if (!global.slab_vmas) |
| 1083 | return -ENOMEM; |
| 1084 | |
Chris Wilson | 103b76ee | 2019-03-05 21:38:30 +0000 | [diff] [blame] | 1085 | i915_global_register(&global.base); |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 1086 | return 0; |
| 1087 | } |