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