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