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" |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 35 | #include "i915_sw_fence_work.h" |
Jani Nikula | a09d9a8 | 2019-08-06 13:07:28 +0300 | [diff] [blame] | 36 | #include "i915_trace.h" |
Jani Nikula | df0566a | 2019-06-13 11:44:16 +0300 | [diff] [blame] | 37 | #include "i915_vma.h" |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 38 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 39 | static struct i915_global_vma { |
Chris Wilson | 103b76ee | 2019-03-05 21:38:30 +0000 | [diff] [blame] | 40 | struct i915_global base; |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 41 | struct kmem_cache *slab_vmas; |
| 42 | } global; |
| 43 | |
| 44 | struct i915_vma *i915_vma_alloc(void) |
| 45 | { |
| 46 | return kmem_cache_zalloc(global.slab_vmas, GFP_KERNEL); |
| 47 | } |
| 48 | |
| 49 | void i915_vma_free(struct i915_vma *vma) |
| 50 | { |
| 51 | return kmem_cache_free(global.slab_vmas, vma); |
| 52 | } |
| 53 | |
Chris Wilson | 1eca65d | 2018-07-06 07:53:06 +0100 | [diff] [blame] | 54 | #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] | 55 | |
| 56 | #include <linux/stackdepot.h> |
| 57 | |
| 58 | static void vma_print_allocator(struct i915_vma *vma, const char *reason) |
| 59 | { |
Thomas Gleixner | 487f3c7 | 2019-04-25 11:45:09 +0200 | [diff] [blame] | 60 | unsigned long *entries; |
| 61 | unsigned int nr_entries; |
Chris Wilson | 10195b1 | 2018-06-28 14:22:06 +0100 | [diff] [blame] | 62 | char buf[512]; |
| 63 | |
| 64 | if (!vma->node.stack) { |
| 65 | DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: unknown owner\n", |
| 66 | vma->node.start, vma->node.size, reason); |
| 67 | return; |
| 68 | } |
| 69 | |
Thomas Gleixner | 487f3c7 | 2019-04-25 11:45:09 +0200 | [diff] [blame] | 70 | nr_entries = stack_depot_fetch(vma->node.stack, &entries); |
| 71 | stack_trace_snprint(buf, sizeof(buf), entries, nr_entries, 0); |
Chris Wilson | 10195b1 | 2018-06-28 14:22:06 +0100 | [diff] [blame] | 72 | DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: inserted at %s\n", |
| 73 | vma->node.start, vma->node.size, reason, buf); |
| 74 | } |
| 75 | |
| 76 | #else |
| 77 | |
| 78 | static void vma_print_allocator(struct i915_vma *vma, const char *reason) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | #endif |
| 83 | |
Chris Wilson | 12c255b | 2019-06-21 19:38:00 +0100 | [diff] [blame] | 84 | static inline struct i915_vma *active_to_vma(struct i915_active *ref) |
| 85 | { |
| 86 | return container_of(ref, typeof(struct i915_vma), active); |
| 87 | } |
| 88 | |
| 89 | static int __i915_vma_active(struct i915_active *ref) |
| 90 | { |
Chris Wilson | 2833ddc | 2019-08-20 11:05:31 +0100 | [diff] [blame] | 91 | return i915_vma_tryget(active_to_vma(ref)) ? 0 : -ENOENT; |
Chris Wilson | 12c255b | 2019-06-21 19:38:00 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Chris Wilson | 274cbf2 | 2019-10-04 14:39:59 +0100 | [diff] [blame] | 94 | __i915_active_call |
Chris Wilson | 64d6c50 | 2019-02-05 13:00:02 +0000 | [diff] [blame] | 95 | static void __i915_vma_retire(struct i915_active *ref) |
| 96 | { |
Chris Wilson | 12c255b | 2019-06-21 19:38:00 +0100 | [diff] [blame] | 97 | i915_vma_put(active_to_vma(ref)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 100 | static struct i915_vma * |
Chris Wilson | a01cb37a | 2017-01-16 15:21:30 +0000 | [diff] [blame] | 101 | vma_create(struct drm_i915_gem_object *obj, |
| 102 | struct i915_address_space *vm, |
| 103 | const struct i915_ggtt_view *view) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 104 | { |
| 105 | struct i915_vma *vma; |
| 106 | struct rb_node *rb, **p; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 107 | |
Chris Wilson | e1cc3db | 2017-02-09 11:19:33 +0000 | [diff] [blame] | 108 | /* The aliasing_ppgtt should never be used directly! */ |
Chris Wilson | 71e51ca | 2019-10-21 19:32:35 +0100 | [diff] [blame] | 109 | GEM_BUG_ON(vm == &vm->gt->ggtt->alias->vm); |
Chris Wilson | e1cc3db | 2017-02-09 11:19:33 +0000 | [diff] [blame] | 110 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 111 | vma = i915_vma_alloc(); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 112 | if (vma == NULL) |
| 113 | return ERR_PTR(-ENOMEM); |
| 114 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 115 | mutex_init(&vma->pages_mutex); |
| 116 | vma->vm = i915_vm_get(vm); |
Chris Wilson | 93f2cde | 2018-06-07 16:40:46 +0100 | [diff] [blame] | 117 | vma->ops = &vm->vma_ops; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 118 | vma->obj = obj; |
Chris Wilson | ef78f7b | 2019-06-18 13:58:58 +0100 | [diff] [blame] | 119 | vma->resv = obj->base.resv; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 120 | vma->size = obj->base.size; |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 121 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 122 | |
Chris Wilson | b1e3177 | 2019-10-04 14:40:00 +0100 | [diff] [blame] | 123 | i915_active_init(&vma->active, __i915_vma_active, __i915_vma_retire); |
Chris Wilson | 155ab88 | 2019-06-06 12:23:20 +0100 | [diff] [blame] | 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 | 4dd2fbb | 2019-09-11 10:02:43 +0100 | [diff] [blame] | 176 | __set_bit(I915_VMA_GGTT_BIT, __i915_vma_flags(vma)); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 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 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 223 | return vma; |
Chris Wilson | 1fcdaa7 | 2017-01-19 19:26:56 +0000 | [diff] [blame] | 224 | |
| 225 | err_vma: |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 226 | i915_vma_free(vma); |
Chris Wilson | 1fcdaa7 | 2017-01-19 19:26:56 +0000 | [diff] [blame] | 227 | return ERR_PTR(-E2BIG); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Chris Wilson | 481a6f7 | 2017-01-16 15:21:31 +0000 | [diff] [blame] | 230 | static struct i915_vma * |
| 231 | vma_lookup(struct drm_i915_gem_object *obj, |
| 232 | struct i915_address_space *vm, |
| 233 | const struct i915_ggtt_view *view) |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 234 | { |
| 235 | struct rb_node *rb; |
| 236 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 237 | rb = obj->vma.tree.rb_node; |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 238 | while (rb) { |
| 239 | struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node); |
| 240 | long cmp; |
| 241 | |
| 242 | cmp = i915_vma_compare(vma, vm, view); |
| 243 | if (cmp == 0) |
| 244 | return vma; |
| 245 | |
| 246 | if (cmp < 0) |
| 247 | rb = rb->rb_right; |
| 248 | else |
| 249 | rb = rb->rb_left; |
| 250 | } |
| 251 | |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | /** |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 256 | * i915_vma_instance - return the singleton instance of the VMA |
| 257 | * @obj: parent &struct drm_i915_gem_object to be mapped |
| 258 | * @vm: address space in which the mapping is located |
| 259 | * @view: additional mapping requirements |
| 260 | * |
| 261 | * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with |
| 262 | * the same @view characteristics. If a match is not found, one is created. |
| 263 | * Once created, the VMA is kept until either the object is freed, or the |
| 264 | * address space is closed. |
| 265 | * |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 266 | * Returns the vma, or an error pointer. |
| 267 | */ |
| 268 | struct i915_vma * |
| 269 | i915_vma_instance(struct drm_i915_gem_object *obj, |
| 270 | struct i915_address_space *vm, |
| 271 | const struct i915_ggtt_view *view) |
| 272 | { |
| 273 | struct i915_vma *vma; |
| 274 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 275 | GEM_BUG_ON(view && !i915_is_ggtt(vm)); |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 276 | GEM_BUG_ON(!atomic_read(&vm->open)); |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 277 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 278 | spin_lock(&obj->vma.lock); |
Chris Wilson | 481a6f7 | 2017-01-16 15:21:31 +0000 | [diff] [blame] | 279 | vma = vma_lookup(obj, vm, view); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 280 | spin_unlock(&obj->vma.lock); |
| 281 | |
| 282 | /* vma_create() will resolve the race if another creates the vma */ |
| 283 | if (unlikely(!vma)) |
Chris Wilson | a01cb37a | 2017-01-16 15:21:30 +0000 | [diff] [blame] | 284 | vma = vma_create(obj, vm, view); |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 285 | |
Chris Wilson | 4ea9527 | 2017-01-16 15:21:29 +0000 | [diff] [blame] | 286 | GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view)); |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 287 | return vma; |
| 288 | } |
| 289 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 290 | struct i915_vma_work { |
| 291 | struct dma_fence_work base; |
| 292 | struct i915_vma *vma; |
| 293 | enum i915_cache_level cache_level; |
| 294 | unsigned int flags; |
| 295 | }; |
| 296 | |
| 297 | static int __vma_bind(struct dma_fence_work *work) |
| 298 | { |
| 299 | struct i915_vma_work *vw = container_of(work, typeof(*vw), base); |
| 300 | struct i915_vma *vma = vw->vma; |
| 301 | int err; |
| 302 | |
| 303 | err = vma->ops->bind_vma(vma, vw->cache_level, vw->flags); |
| 304 | if (err) |
| 305 | atomic_or(I915_VMA_ERROR, &vma->flags); |
| 306 | |
| 307 | if (vma->obj) |
| 308 | __i915_gem_object_unpin_pages(vma->obj); |
| 309 | |
| 310 | return err; |
| 311 | } |
| 312 | |
| 313 | static const struct dma_fence_work_ops bind_ops = { |
| 314 | .name = "bind", |
| 315 | .work = __vma_bind, |
| 316 | }; |
| 317 | |
| 318 | struct i915_vma_work *i915_vma_work(void) |
| 319 | { |
| 320 | struct i915_vma_work *vw; |
| 321 | |
| 322 | vw = kzalloc(sizeof(*vw), GFP_KERNEL); |
| 323 | if (!vw) |
| 324 | return NULL; |
| 325 | |
| 326 | dma_fence_work_init(&vw->base, &bind_ops); |
| 327 | vw->base.dma.error = -EAGAIN; /* disable the worker by default */ |
| 328 | |
| 329 | return vw; |
| 330 | } |
| 331 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 332 | /** |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 333 | * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space. |
| 334 | * @vma: VMA to map |
| 335 | * @cache_level: mapping cache level |
| 336 | * @flags: flags like global or local mapping |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 337 | * @work: preallocated worker for allocating and binding the PTE |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 338 | * |
| 339 | * DMA addresses are taken from the scatter-gather table of this object (or of |
| 340 | * this VMA in case of non-default GGTT views) and PTE entries set up. |
| 341 | * Note that DMA addresses are also the only part of the SG table we care about. |
| 342 | */ |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 343 | int i915_vma_bind(struct i915_vma *vma, |
| 344 | enum i915_cache_level cache_level, |
| 345 | u32 flags, |
| 346 | struct i915_vma_work *work) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 347 | { |
| 348 | u32 bind_flags; |
| 349 | u32 vma_flags; |
| 350 | int ret; |
| 351 | |
Chris Wilson | aa14943 | 2017-02-25 18:11:21 +0000 | [diff] [blame] | 352 | GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); |
| 353 | GEM_BUG_ON(vma->size > vma->node.size); |
| 354 | |
Tvrtko Ursulin | bbb8a9d | 2018-10-12 07:31:42 +0100 | [diff] [blame] | 355 | if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start, |
| 356 | vma->node.size, |
| 357 | vma->vm->total))) |
Chris Wilson | aa14943 | 2017-02-25 18:11:21 +0000 | [diff] [blame] | 358 | return -ENODEV; |
| 359 | |
Tvrtko Ursulin | bbb8a9d | 2018-10-12 07:31:42 +0100 | [diff] [blame] | 360 | if (GEM_DEBUG_WARN_ON(!flags)) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 361 | return -EINVAL; |
| 362 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 363 | bind_flags = flags; |
| 364 | bind_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 365 | |
Chris Wilson | 4dd2fbb | 2019-09-11 10:02:43 +0100 | [diff] [blame] | 366 | vma_flags = atomic_read(&vma->flags); |
| 367 | vma_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 368 | if (flags & PIN_UPDATE) |
| 369 | bind_flags |= vma_flags; |
| 370 | else |
| 371 | bind_flags &= ~vma_flags; |
| 372 | if (bind_flags == 0) |
| 373 | return 0; |
| 374 | |
Matthew Auld | fa3f46a | 2017-10-06 23:18:19 +0100 | [diff] [blame] | 375 | GEM_BUG_ON(!vma->pages); |
| 376 | |
Daniele Ceraolo Spurio | 6146e6d | 2017-01-20 13:51:23 -0800 | [diff] [blame] | 377 | trace_i915_vma_bind(vma, bind_flags); |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 378 | if (work && (bind_flags & ~vma_flags) & vma->vm->bind_async_flags) { |
| 379 | work->vma = vma; |
| 380 | work->cache_level = cache_level; |
| 381 | work->flags = bind_flags | I915_VMA_ALLOC; |
| 382 | |
| 383 | /* |
| 384 | * Note we only want to chain up to the migration fence on |
| 385 | * the pages (not the object itself). As we don't track that, |
| 386 | * yet, we have to use the exclusive fence instead. |
| 387 | * |
| 388 | * Also note that we do not want to track the async vma as |
| 389 | * part of the obj->resv->excl_fence as it only affects |
| 390 | * execution and not content or object's backing store lifetime. |
| 391 | */ |
| 392 | GEM_BUG_ON(i915_active_has_exclusive(&vma->active)); |
| 393 | i915_active_set_exclusive(&vma->active, &work->base.dma); |
| 394 | work->base.dma.error = 0; /* enable the queue_work() */ |
| 395 | |
| 396 | if (vma->obj) |
| 397 | __i915_gem_object_pin_pages(vma->obj); |
| 398 | } else { |
| 399 | GEM_BUG_ON((bind_flags & ~vma_flags) & vma->vm->bind_async_flags); |
| 400 | ret = vma->ops->bind_vma(vma, cache_level, bind_flags); |
| 401 | if (ret) |
| 402 | return ret; |
| 403 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 404 | |
Chris Wilson | 4dd2fbb | 2019-09-11 10:02:43 +0100 | [diff] [blame] | 405 | atomic_or(bind_flags, &vma->flags); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | void __iomem *i915_vma_pin_iomap(struct i915_vma *vma) |
| 410 | { |
| 411 | void __iomem *ptr; |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 412 | int err; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 413 | |
| 414 | /* Access through the GTT requires the device to be awake. */ |
Chris Wilson | 71e51ca | 2019-10-21 19:32:35 +0100 | [diff] [blame] | 415 | assert_rpm_wakelock_held(vma->vm->gt->uncore->rpm); |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 416 | if (GEM_WARN_ON(!i915_vma_is_map_and_fenceable(vma))) { |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 417 | err = -ENODEV; |
| 418 | goto err; |
| 419 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 420 | |
| 421 | GEM_BUG_ON(!i915_vma_is_ggtt(vma)); |
Chris Wilson | 4dd2fbb | 2019-09-11 10:02:43 +0100 | [diff] [blame] | 422 | GEM_BUG_ON(!i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 423 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 424 | ptr = READ_ONCE(vma->iomap); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 425 | if (ptr == NULL) { |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 426 | ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap, |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 427 | vma->node.start, |
| 428 | vma->node.size); |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 429 | if (ptr == NULL) { |
| 430 | err = -ENOMEM; |
| 431 | goto err; |
| 432 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 433 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 434 | if (unlikely(cmpxchg(&vma->iomap, NULL, ptr))) { |
| 435 | io_mapping_unmap(ptr); |
| 436 | ptr = vma->iomap; |
| 437 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | __i915_vma_pin(vma); |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 441 | |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 442 | err = i915_vma_pin_fence(vma); |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 443 | if (err) |
| 444 | goto err_unpin; |
| 445 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 446 | i915_vma_set_ggtt_write(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 447 | return ptr; |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 448 | |
| 449 | err_unpin: |
| 450 | __i915_vma_unpin(vma); |
| 451 | err: |
| 452 | return IO_ERR_PTR(err); |
| 453 | } |
| 454 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 455 | void i915_vma_flush_writes(struct i915_vma *vma) |
| 456 | { |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 457 | if (i915_vma_unset_ggtt_write(vma)) |
| 458 | intel_gt_flush_ggtt_writes(vma->vm->gt); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 461 | void i915_vma_unpin_iomap(struct i915_vma *vma) |
| 462 | { |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 463 | GEM_BUG_ON(vma->iomap == NULL); |
| 464 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 465 | i915_vma_flush_writes(vma); |
| 466 | |
Chris Wilson | b4563f5 | 2017-10-09 09:43:55 +0100 | [diff] [blame] | 467 | i915_vma_unpin_fence(vma); |
| 468 | i915_vma_unpin(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Chris Wilson | 6a2f59e | 2018-07-21 13:50:37 +0100 | [diff] [blame] | 471 | 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] | 472 | { |
| 473 | struct i915_vma *vma; |
| 474 | struct drm_i915_gem_object *obj; |
| 475 | |
| 476 | vma = fetch_and_zero(p_vma); |
| 477 | if (!vma) |
| 478 | return; |
| 479 | |
| 480 | obj = vma->obj; |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 481 | GEM_BUG_ON(!obj); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 482 | |
| 483 | i915_vma_unpin(vma); |
| 484 | i915_vma_close(vma); |
| 485 | |
Chris Wilson | 6a2f59e | 2018-07-21 13:50:37 +0100 | [diff] [blame] | 486 | if (flags & I915_VMA_RELEASE_MAP) |
| 487 | i915_gem_object_unpin_map(obj); |
| 488 | |
Chris Wilson | c017cf6 | 2019-05-28 10:29:56 +0100 | [diff] [blame] | 489 | i915_gem_object_put(obj); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Chris Wilson | 782a3e9 | 2017-02-13 17:15:46 +0000 | [diff] [blame] | 492 | bool i915_vma_misplaced(const struct i915_vma *vma, |
| 493 | u64 size, u64 alignment, u64 flags) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 494 | { |
| 495 | if (!drm_mm_node_allocated(&vma->node)) |
| 496 | return false; |
| 497 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 498 | if (test_bit(I915_VMA_ERROR_BIT, __i915_vma_flags(vma))) |
| 499 | return true; |
| 500 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 501 | if (vma->node.size < size) |
| 502 | return true; |
| 503 | |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 504 | GEM_BUG_ON(alignment && !is_power_of_2(alignment)); |
| 505 | if (alignment && !IS_ALIGNED(vma->node.start, alignment)) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 506 | return true; |
| 507 | |
| 508 | if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma)) |
| 509 | return true; |
| 510 | |
| 511 | if (flags & PIN_OFFSET_BIAS && |
| 512 | vma->node.start < (flags & PIN_OFFSET_MASK)) |
| 513 | return true; |
| 514 | |
| 515 | if (flags & PIN_OFFSET_FIXED && |
| 516 | vma->node.start != (flags & PIN_OFFSET_MASK)) |
| 517 | return true; |
| 518 | |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | void __i915_vma_set_map_and_fenceable(struct i915_vma *vma) |
| 523 | { |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 524 | bool mappable, fenceable; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 525 | |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 526 | GEM_BUG_ON(!i915_vma_is_ggtt(vma)); |
| 527 | GEM_BUG_ON(!vma->fence_size); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 528 | |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 529 | fenceable = (vma->node.size >= vma->fence_size && |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 530 | IS_ALIGNED(vma->node.start, vma->fence_alignment)); |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 531 | |
| 532 | mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end; |
| 533 | |
| 534 | if (mappable && fenceable) |
Chris Wilson | 4dd2fbb | 2019-09-11 10:02:43 +0100 | [diff] [blame] | 535 | set_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 536 | else |
Chris Wilson | 4dd2fbb | 2019-09-11 10:02:43 +0100 | [diff] [blame] | 537 | clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Matthew Auld | 33dd889 | 2019-09-09 13:40:52 +0100 | [diff] [blame] | 540 | 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] | 541 | { |
| 542 | struct drm_mm_node *node = &vma->node; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 543 | struct drm_mm_node *other; |
| 544 | |
| 545 | /* |
| 546 | * On some machines we have to be careful when putting differing types |
| 547 | * of snoopable memory together to avoid the prefetcher crossing memory |
| 548 | * domains and dying. During vm initialisation, we decide whether or not |
| 549 | * these constraints apply and set the drm_mm.color_adjust |
| 550 | * appropriately. |
| 551 | */ |
Matthew Auld | 33dd889 | 2019-09-09 13:40:52 +0100 | [diff] [blame] | 552 | if (!i915_vm_has_cache_coloring(vma->vm)) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 553 | return true; |
| 554 | |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 555 | /* Only valid to be called on an already inserted vma */ |
| 556 | GEM_BUG_ON(!drm_mm_node_allocated(node)); |
| 557 | GEM_BUG_ON(list_empty(&node->node_list)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 558 | |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 559 | other = list_prev_entry(node, node_list); |
Matthew Auld | 33dd889 | 2019-09-09 13:40:52 +0100 | [diff] [blame] | 560 | if (i915_node_color_differs(other, color) && |
Matthew Auld | 1e0a96e | 2019-09-09 13:40:50 +0100 | [diff] [blame] | 561 | !drm_mm_hole_follows(other)) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 562 | return false; |
| 563 | |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 564 | other = list_next_entry(node, node_list); |
Matthew Auld | 33dd889 | 2019-09-09 13:40:52 +0100 | [diff] [blame] | 565 | if (i915_node_color_differs(other, color) && |
Matthew Auld | 1e0a96e | 2019-09-09 13:40:50 +0100 | [diff] [blame] | 566 | !drm_mm_hole_follows(node)) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 567 | return false; |
| 568 | |
| 569 | return true; |
| 570 | } |
| 571 | |
Chris Wilson | 83d317a | 2018-06-05 10:41:07 +0100 | [diff] [blame] | 572 | static void assert_bind_count(const struct drm_i915_gem_object *obj) |
| 573 | { |
| 574 | /* |
| 575 | * Combine the assertion that the object is bound and that we have |
| 576 | * pinned its pages. But we should never have bound the object |
| 577 | * more than we have pinned its pages. (For complete accuracy, we |
| 578 | * assume that no else is pinning the pages, but as a rough assertion |
| 579 | * that we will not run into problems later, this will do!) |
| 580 | */ |
Chris Wilson | ecab9be | 2019-06-12 11:57:20 +0100 | [diff] [blame] | 581 | 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] | 582 | } |
| 583 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 584 | /** |
| 585 | * i915_vma_insert - finds a slot for the vma in its address space |
| 586 | * @vma: the vma |
| 587 | * @size: requested size in bytes (can be larger than the VMA) |
| 588 | * @alignment: required alignment |
| 589 | * @flags: mask of PIN_* flags to use |
| 590 | * |
| 591 | * First we try to allocate some free space that meets the requirements for |
| 592 | * the VMA. Failiing that, if the flags permit, it will evict an old VMA, |
| 593 | * preferrably the oldest idle entry to make room for the new VMA. |
| 594 | * |
| 595 | * Returns: |
| 596 | * 0 on success, negative error code otherwise. |
| 597 | */ |
| 598 | static int |
| 599 | i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags) |
| 600 | { |
Matthew Auld | 33dd889 | 2019-09-09 13:40:52 +0100 | [diff] [blame] | 601 | unsigned long color; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 602 | u64 start, end; |
| 603 | int ret; |
| 604 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 605 | GEM_BUG_ON(i915_vma_is_closed(vma)); |
Chris Wilson | 4dd2fbb | 2019-09-11 10:02:43 +0100 | [diff] [blame] | 606 | GEM_BUG_ON(i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 607 | GEM_BUG_ON(drm_mm_node_allocated(&vma->node)); |
| 608 | |
| 609 | size = max(size, vma->size); |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 610 | alignment = max(alignment, vma->display_alignment); |
| 611 | if (flags & PIN_MAPPABLE) { |
| 612 | size = max_t(typeof(size), size, vma->fence_size); |
| 613 | alignment = max_t(typeof(alignment), |
| 614 | alignment, vma->fence_alignment); |
| 615 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 616 | |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 617 | GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE)); |
| 618 | GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT)); |
| 619 | GEM_BUG_ON(!is_power_of_2(alignment)); |
| 620 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 621 | start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0; |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 622 | GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 623 | |
| 624 | end = vma->vm->total; |
| 625 | if (flags & PIN_MAPPABLE) |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 626 | end = min_t(u64, end, i915_vm_to_ggtt(vma->vm)->mappable_end); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 627 | if (flags & PIN_ZONE_4G) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 628 | end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE); |
| 629 | GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 630 | |
| 631 | /* If binding the object/GGTT view requires more space than the entire |
| 632 | * aperture has, reject it early before evicting everything in a vain |
| 633 | * attempt to find space. |
| 634 | */ |
| 635 | if (size > end) { |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 636 | DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n", |
| 637 | size, flags & PIN_MAPPABLE ? "mappable" : "total", |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 638 | end); |
Chris Wilson | 2889caa | 2017-06-16 15:05:19 +0100 | [diff] [blame] | 639 | return -ENOSPC; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 640 | } |
| 641 | |
Matthew Auld | 33dd889 | 2019-09-09 13:40:52 +0100 | [diff] [blame] | 642 | color = 0; |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 643 | if (vma->obj && i915_vm_has_cache_coloring(vma->vm)) |
| 644 | color = vma->obj->cache_level; |
Matthew Auld | fa3f46a | 2017-10-06 23:18:19 +0100 | [diff] [blame] | 645 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 646 | if (flags & PIN_OFFSET_FIXED) { |
| 647 | u64 offset = flags & PIN_OFFSET_MASK; |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 648 | if (!IS_ALIGNED(offset, alignment) || |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 649 | range_overflows(offset, size, end)) |
| 650 | return -EINVAL; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 651 | |
Chris Wilson | 625d988 | 2017-01-11 11:23:11 +0000 | [diff] [blame] | 652 | ret = i915_gem_gtt_reserve(vma->vm, &vma->node, |
Matthew Auld | 33dd889 | 2019-09-09 13:40:52 +0100 | [diff] [blame] | 653 | size, offset, color, |
Chris Wilson | 625d988 | 2017-01-11 11:23:11 +0000 | [diff] [blame] | 654 | flags); |
| 655 | if (ret) |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 656 | return ret; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 657 | } else { |
Matthew Auld | 7464284 | 2017-10-06 23:18:20 +0100 | [diff] [blame] | 658 | /* |
| 659 | * We only support huge gtt pages through the 48b PPGTT, |
| 660 | * however we also don't want to force any alignment for |
| 661 | * objects which need to be tightly packed into the low 32bits. |
| 662 | * |
| 663 | * Note that we assume that GGTT are limited to 4GiB for the |
| 664 | * forseeable future. See also i915_ggtt_offset(). |
| 665 | */ |
| 666 | if (upper_32_bits(end - 1) && |
| 667 | vma->page_sizes.sg > I915_GTT_PAGE_SIZE) { |
Matthew Auld | 855822b | 2017-10-06 23:18:21 +0100 | [diff] [blame] | 668 | /* |
| 669 | * We can't mix 64K and 4K PTEs in the same page-table |
| 670 | * (2M block), and so to avoid the ugliness and |
| 671 | * complexity of coloring we opt for just aligning 64K |
| 672 | * objects to 2M. |
| 673 | */ |
Matthew Auld | 7464284 | 2017-10-06 23:18:20 +0100 | [diff] [blame] | 674 | u64 page_alignment = |
Matthew Auld | 855822b | 2017-10-06 23:18:21 +0100 | [diff] [blame] | 675 | rounddown_pow_of_two(vma->page_sizes.sg | |
| 676 | I915_GTT_PAGE_SIZE_2M); |
Matthew Auld | 7464284 | 2017-10-06 23:18:20 +0100 | [diff] [blame] | 677 | |
Chris Wilson | bef27bdb | 2017-10-09 10:20:19 +0100 | [diff] [blame] | 678 | /* |
| 679 | * Check we don't expand for the limited Global GTT |
| 680 | * (mappable aperture is even more precious!). This |
| 681 | * also checks that we exclude the aliasing-ppgtt. |
| 682 | */ |
| 683 | GEM_BUG_ON(i915_vma_is_ggtt(vma)); |
| 684 | |
Matthew Auld | 7464284 | 2017-10-06 23:18:20 +0100 | [diff] [blame] | 685 | alignment = max(alignment, page_alignment); |
Matthew Auld | 855822b | 2017-10-06 23:18:21 +0100 | [diff] [blame] | 686 | |
| 687 | if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K) |
| 688 | size = round_up(size, I915_GTT_PAGE_SIZE_2M); |
Matthew Auld | 7464284 | 2017-10-06 23:18:20 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Chris Wilson | e007b19 | 2017-01-11 11:23:10 +0000 | [diff] [blame] | 691 | ret = i915_gem_gtt_insert(vma->vm, &vma->node, |
Matthew Auld | 33dd889 | 2019-09-09 13:40:52 +0100 | [diff] [blame] | 692 | size, alignment, color, |
Chris Wilson | e007b19 | 2017-01-11 11:23:10 +0000 | [diff] [blame] | 693 | start, end, flags); |
| 694 | if (ret) |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 695 | return ret; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 696 | |
| 697 | GEM_BUG_ON(vma->node.start < start); |
| 698 | GEM_BUG_ON(vma->node.start + vma->node.size > end); |
| 699 | } |
Chris Wilson | 44a0ec0 | 2017-01-19 19:26:58 +0000 | [diff] [blame] | 700 | GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); |
Matthew Auld | 33dd889 | 2019-09-09 13:40:52 +0100 | [diff] [blame] | 701 | GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, color)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 702 | |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 703 | if (vma->obj) { |
Chris Wilson | dde01d9 | 2019-10-30 19:21:49 +0000 | [diff] [blame] | 704 | struct drm_i915_gem_object *obj = vma->obj; |
| 705 | |
| 706 | atomic_inc(&obj->bind_count); |
| 707 | assert_bind_count(obj); |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 708 | } |
Chris Wilson | dde01d9 | 2019-10-30 19:21:49 +0000 | [diff] [blame] | 709 | list_add_tail(&vma->vm_link, &vma->vm->bound_list); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 710 | |
| 711 | return 0; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 714 | static void |
Chris Wilson | dde01d9 | 2019-10-30 19:21:49 +0000 | [diff] [blame] | 715 | i915_vma_detach(struct i915_vma *vma) |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 716 | { |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 717 | GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); |
Chris Wilson | 4dd2fbb | 2019-09-11 10:02:43 +0100 | [diff] [blame] | 718 | GEM_BUG_ON(i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND)); |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 719 | |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 720 | /* |
Chris Wilson | dde01d9 | 2019-10-30 19:21:49 +0000 | [diff] [blame] | 721 | * And finally now the object is completely decoupled from this |
| 722 | * vma, we can drop its hold on the backing storage and allow |
| 723 | * it to be reaped by the shrinker. |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 724 | */ |
Chris Wilson | dde01d9 | 2019-10-30 19:21:49 +0000 | [diff] [blame] | 725 | list_del(&vma->vm_link); |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 726 | if (vma->obj) { |
| 727 | struct drm_i915_gem_object *obj = vma->obj; |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 728 | |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 729 | assert_bind_count(obj); |
Chris Wilson | dde01d9 | 2019-10-30 19:21:49 +0000 | [diff] [blame] | 730 | atomic_dec(&obj->bind_count); |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 731 | } |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 734 | static bool try_qad_pin(struct i915_vma *vma, unsigned int flags) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 735 | { |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 736 | unsigned int bound; |
| 737 | bool pinned = true; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 738 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 739 | bound = atomic_read(&vma->flags); |
| 740 | do { |
| 741 | if (unlikely(flags & ~bound)) |
| 742 | return false; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 743 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 744 | if (unlikely(bound & (I915_VMA_OVERFLOW | I915_VMA_ERROR))) |
| 745 | return false; |
| 746 | |
| 747 | if (!(bound & I915_VMA_PIN_MASK)) |
| 748 | goto unpinned; |
| 749 | |
| 750 | GEM_BUG_ON(((bound + 1) & I915_VMA_PIN_MASK) == 0); |
| 751 | } while (!atomic_try_cmpxchg(&vma->flags, &bound, bound + 1)); |
| 752 | |
| 753 | return true; |
| 754 | |
| 755 | unpinned: |
| 756 | /* |
| 757 | * If pin_count==0, but we are bound, check under the lock to avoid |
| 758 | * racing with a concurrent i915_vma_unbind(). |
| 759 | */ |
| 760 | mutex_lock(&vma->vm->mutex); |
| 761 | do { |
| 762 | if (unlikely(bound & (I915_VMA_OVERFLOW | I915_VMA_ERROR))) { |
| 763 | pinned = false; |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | if (unlikely(flags & ~bound)) { |
| 768 | pinned = false; |
| 769 | break; |
| 770 | } |
| 771 | } while (!atomic_try_cmpxchg(&vma->flags, &bound, bound + 1)); |
| 772 | mutex_unlock(&vma->vm->mutex); |
| 773 | |
| 774 | return pinned; |
| 775 | } |
| 776 | |
| 777 | static int vma_get_pages(struct i915_vma *vma) |
| 778 | { |
| 779 | int err = 0; |
| 780 | |
| 781 | if (atomic_add_unless(&vma->pages_count, 1, 0)) |
| 782 | return 0; |
| 783 | |
| 784 | /* Allocations ahoy! */ |
| 785 | if (mutex_lock_interruptible(&vma->pages_mutex)) |
| 786 | return -EINTR; |
| 787 | |
| 788 | if (!atomic_read(&vma->pages_count)) { |
| 789 | if (vma->obj) { |
| 790 | err = i915_gem_object_pin_pages(vma->obj); |
| 791 | if (err) |
| 792 | goto unlock; |
| 793 | } |
| 794 | |
| 795 | err = vma->ops->set_pages(vma); |
Chris Wilson | 56184a2 | 2019-10-15 10:39:15 +0100 | [diff] [blame] | 796 | if (err) { |
| 797 | if (vma->obj) |
| 798 | i915_gem_object_unpin_pages(vma->obj); |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 799 | goto unlock; |
Chris Wilson | 56184a2 | 2019-10-15 10:39:15 +0100 | [diff] [blame] | 800 | } |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 801 | } |
| 802 | atomic_inc(&vma->pages_count); |
| 803 | |
| 804 | unlock: |
| 805 | mutex_unlock(&vma->pages_mutex); |
| 806 | |
| 807 | return err; |
| 808 | } |
| 809 | |
| 810 | static void __vma_put_pages(struct i915_vma *vma, unsigned int count) |
| 811 | { |
| 812 | /* We allocate under vma_get_pages, so beware the shrinker */ |
| 813 | mutex_lock_nested(&vma->pages_mutex, SINGLE_DEPTH_NESTING); |
| 814 | GEM_BUG_ON(atomic_read(&vma->pages_count) < count); |
| 815 | if (atomic_sub_return(count, &vma->pages_count) == 0) { |
| 816 | vma->ops->clear_pages(vma); |
| 817 | GEM_BUG_ON(vma->pages); |
| 818 | if (vma->obj) |
| 819 | i915_gem_object_unpin_pages(vma->obj); |
| 820 | } |
| 821 | mutex_unlock(&vma->pages_mutex); |
| 822 | } |
| 823 | |
| 824 | static void vma_put_pages(struct i915_vma *vma) |
| 825 | { |
| 826 | if (atomic_add_unless(&vma->pages_count, -1, 1)) |
| 827 | return; |
| 828 | |
| 829 | __vma_put_pages(vma, 1); |
| 830 | } |
| 831 | |
| 832 | static void vma_unbind_pages(struct i915_vma *vma) |
| 833 | { |
| 834 | unsigned int count; |
| 835 | |
| 836 | lockdep_assert_held(&vma->vm->mutex); |
| 837 | |
| 838 | /* The upper portion of pages_count is the number of bindings */ |
| 839 | count = atomic_read(&vma->pages_count); |
| 840 | count >>= I915_VMA_PAGES_BIAS; |
| 841 | GEM_BUG_ON(!count); |
| 842 | |
| 843 | __vma_put_pages(vma, count | count << I915_VMA_PAGES_BIAS); |
| 844 | } |
| 845 | |
| 846 | int i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags) |
| 847 | { |
| 848 | struct i915_vma_work *work = NULL; |
| 849 | unsigned int bound; |
| 850 | int err; |
| 851 | |
| 852 | BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND); |
| 853 | BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND); |
| 854 | |
| 855 | GEM_BUG_ON(flags & PIN_UPDATE); |
| 856 | GEM_BUG_ON(!(flags & (PIN_USER | PIN_GLOBAL))); |
| 857 | |
| 858 | /* First try and grab the pin without rebinding the vma */ |
| 859 | if (try_qad_pin(vma, flags & I915_VMA_BIND_MASK)) |
| 860 | return 0; |
| 861 | |
| 862 | err = vma_get_pages(vma); |
| 863 | if (err) |
| 864 | return err; |
| 865 | |
| 866 | if (flags & vma->vm->bind_async_flags) { |
| 867 | work = i915_vma_work(); |
| 868 | if (!work) { |
| 869 | err = -ENOMEM; |
| 870 | goto err_pages; |
| 871 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 872 | } |
| 873 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 874 | /* No more allocations allowed once we hold vm->mutex */ |
| 875 | err = mutex_lock_interruptible(&vma->vm->mutex); |
| 876 | if (err) |
| 877 | goto err_fence; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 878 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 879 | bound = atomic_read(&vma->flags); |
| 880 | if (unlikely(bound & I915_VMA_ERROR)) { |
| 881 | err = -ENOMEM; |
| 882 | goto err_unlock; |
| 883 | } |
| 884 | |
| 885 | if (unlikely(!((bound + 1) & I915_VMA_PIN_MASK))) { |
| 886 | err = -EAGAIN; /* pins are meant to be fairly temporary */ |
| 887 | goto err_unlock; |
| 888 | } |
| 889 | |
| 890 | if (unlikely(!(flags & ~bound & I915_VMA_BIND_MASK))) { |
| 891 | __i915_vma_pin(vma); |
| 892 | goto err_unlock; |
| 893 | } |
| 894 | |
| 895 | err = i915_active_acquire(&vma->active); |
| 896 | if (err) |
| 897 | goto err_unlock; |
| 898 | |
| 899 | if (!(bound & I915_VMA_BIND_MASK)) { |
| 900 | err = i915_vma_insert(vma, size, alignment, flags); |
| 901 | if (err) |
| 902 | goto err_active; |
| 903 | |
| 904 | if (i915_is_ggtt(vma->vm)) |
| 905 | __i915_vma_set_map_and_fenceable(vma); |
| 906 | } |
| 907 | |
| 908 | GEM_BUG_ON(!vma->pages); |
| 909 | err = i915_vma_bind(vma, |
| 910 | vma->obj ? vma->obj->cache_level : 0, |
| 911 | flags, work); |
| 912 | if (err) |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 913 | goto err_remove; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 914 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 915 | /* There should only be at most 2 active bindings (user, global) */ |
| 916 | GEM_BUG_ON(bound + I915_VMA_PAGES_ACTIVE < bound); |
| 917 | atomic_add(I915_VMA_PAGES_ACTIVE, &vma->pages_count); |
| 918 | list_move_tail(&vma->vm_link, &vma->vm->bound_list); |
Chris Wilson | d36caee | 2017-11-05 12:45:50 +0000 | [diff] [blame] | 919 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 920 | __i915_vma_pin(vma); |
| 921 | GEM_BUG_ON(!i915_vma_is_pinned(vma)); |
| 922 | GEM_BUG_ON(!i915_vma_is_bound(vma, flags)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 923 | GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 924 | |
Chris Wilson | 31c7eff | 2017-02-27 12:26:54 +0000 | [diff] [blame] | 925 | err_remove: |
Chris Wilson | dde01d9 | 2019-10-30 19:21:49 +0000 | [diff] [blame] | 926 | if (!i915_vma_is_bound(vma, I915_VMA_BIND_MASK)) { |
| 927 | i915_vma_detach(vma); |
| 928 | drm_mm_remove_node(&vma->node); |
| 929 | } |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 930 | err_active: |
| 931 | i915_active_release(&vma->active); |
| 932 | err_unlock: |
| 933 | mutex_unlock(&vma->vm->mutex); |
| 934 | err_fence: |
| 935 | if (work) |
| 936 | dma_fence_work_commit(&work->base); |
| 937 | err_pages: |
| 938 | vma_put_pages(vma); |
| 939 | return err; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 940 | } |
| 941 | |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 942 | void i915_vma_close(struct i915_vma *vma) |
| 943 | { |
Chris Wilson | 71e51ca | 2019-10-21 19:32:35 +0100 | [diff] [blame] | 944 | struct intel_gt *gt = vma->vm->gt; |
Chris Wilson | 155ab88 | 2019-06-06 12:23:20 +0100 | [diff] [blame] | 945 | unsigned long flags; |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 946 | |
| 947 | GEM_BUG_ON(i915_vma_is_closed(vma)); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 948 | |
| 949 | /* |
| 950 | * We defer actually closing, unbinding and destroying the VMA until |
| 951 | * the next idle point, or if the object is freed in the meantime. By |
| 952 | * postponing the unbind, we allow for it to be resurrected by the |
| 953 | * client, avoiding the work required to rebind the VMA. This is |
| 954 | * advantageous for DRI, where the client/server pass objects |
| 955 | * between themselves, temporarily opening a local VMA to the |
| 956 | * object, and then closing it again. The same object is then reused |
| 957 | * on the next frame (or two, depending on the depth of the swap queue) |
| 958 | * causing us to rebind the VMA once more. This ends up being a lot |
| 959 | * of wasted work for the steady state. |
| 960 | */ |
Chris Wilson | 71e51ca | 2019-10-21 19:32:35 +0100 | [diff] [blame] | 961 | spin_lock_irqsave(>->closed_lock, flags); |
| 962 | list_add(&vma->closed_link, >->closed_vma); |
| 963 | spin_unlock_irqrestore(>->closed_lock, flags); |
Chris Wilson | 155ab88 | 2019-06-06 12:23:20 +0100 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | static void __i915_vma_remove_closed(struct i915_vma *vma) |
| 967 | { |
Chris Wilson | 71e51ca | 2019-10-21 19:32:35 +0100 | [diff] [blame] | 968 | struct intel_gt *gt = vma->vm->gt; |
Chris Wilson | 155ab88 | 2019-06-06 12:23:20 +0100 | [diff] [blame] | 969 | |
Chris Wilson | 71e51ca | 2019-10-21 19:32:35 +0100 | [diff] [blame] | 970 | spin_lock_irq(>->closed_lock); |
Chris Wilson | 155ab88 | 2019-06-06 12:23:20 +0100 | [diff] [blame] | 971 | list_del_init(&vma->closed_link); |
Chris Wilson | 71e51ca | 2019-10-21 19:32:35 +0100 | [diff] [blame] | 972 | spin_unlock_irq(>->closed_lock); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | void i915_vma_reopen(struct i915_vma *vma) |
| 976 | { |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 977 | if (i915_vma_is_closed(vma)) |
| 978 | __i915_vma_remove_closed(vma); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 979 | } |
| 980 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 981 | void i915_vma_destroy(struct i915_vma *vma) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 982 | { |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 983 | if (drm_mm_node_allocated(&vma->node)) { |
| 984 | mutex_lock(&vma->vm->mutex); |
| 985 | atomic_and(~I915_VMA_PIN_MASK, &vma->flags); |
| 986 | WARN_ON(__i915_vma_unbind(vma)); |
| 987 | mutex_unlock(&vma->vm->mutex); |
| 988 | GEM_BUG_ON(drm_mm_node_allocated(&vma->node)); |
| 989 | } |
| 990 | GEM_BUG_ON(i915_vma_is_active(vma)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 991 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 992 | if (vma->obj) { |
| 993 | struct drm_i915_gem_object *obj = vma->obj; |
| 994 | |
| 995 | spin_lock(&obj->vma.lock); |
| 996 | list_del(&vma->obj_link); |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 997 | rb_erase(&vma->obj_node, &obj->vma.tree); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 998 | spin_unlock(&obj->vma.lock); |
| 999 | } |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 1000 | |
Chris Wilson | 155ab88 | 2019-06-06 12:23:20 +0100 | [diff] [blame] | 1001 | __i915_vma_remove_closed(vma); |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1002 | i915_vm_put(vma->vm); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 1003 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1004 | i915_active_fini(&vma->active); |
| 1005 | i915_vma_free(vma); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 1006 | } |
| 1007 | |
Chris Wilson | 71e51ca | 2019-10-21 19:32:35 +0100 | [diff] [blame] | 1008 | void i915_vma_parked(struct intel_gt *gt) |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 1009 | { |
| 1010 | struct i915_vma *vma, *next; |
| 1011 | |
Chris Wilson | 71e51ca | 2019-10-21 19:32:35 +0100 | [diff] [blame] | 1012 | spin_lock_irq(>->closed_lock); |
| 1013 | list_for_each_entry_safe(vma, next, >->closed_vma, closed_link) { |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1014 | struct drm_i915_gem_object *obj = vma->obj; |
| 1015 | struct i915_address_space *vm = vma->vm; |
| 1016 | |
| 1017 | /* XXX All to avoid keeping a reference on i915_vma itself */ |
| 1018 | |
| 1019 | if (!kref_get_unless_zero(&obj->base.refcount)) |
| 1020 | continue; |
| 1021 | |
| 1022 | if (!i915_vm_tryopen(vm)) { |
| 1023 | i915_gem_object_put(obj); |
| 1024 | obj = NULL; |
| 1025 | } |
| 1026 | |
Chris Wilson | 71e51ca | 2019-10-21 19:32:35 +0100 | [diff] [blame] | 1027 | spin_unlock_irq(>->closed_lock); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 1028 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1029 | if (obj) { |
| 1030 | i915_vma_destroy(vma); |
| 1031 | i915_gem_object_put(obj); |
| 1032 | } |
Chris Wilson | 155ab88 | 2019-06-06 12:23:20 +0100 | [diff] [blame] | 1033 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1034 | i915_vm_close(vm); |
| 1035 | |
| 1036 | /* Restart after dropping lock */ |
Chris Wilson | 71e51ca | 2019-10-21 19:32:35 +0100 | [diff] [blame] | 1037 | spin_lock_irq(>->closed_lock); |
| 1038 | next = list_first_entry(>->closed_vma, |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1039 | typeof(*next), closed_link); |
Chris Wilson | 155ab88 | 2019-06-06 12:23:20 +0100 | [diff] [blame] | 1040 | } |
Chris Wilson | 71e51ca | 2019-10-21 19:32:35 +0100 | [diff] [blame] | 1041 | spin_unlock_irq(>->closed_lock); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | static void __i915_vma_iounmap(struct i915_vma *vma) |
| 1045 | { |
| 1046 | GEM_BUG_ON(i915_vma_is_pinned(vma)); |
| 1047 | |
| 1048 | if (vma->iomap == NULL) |
| 1049 | return; |
| 1050 | |
| 1051 | io_mapping_unmap(vma->iomap); |
| 1052 | vma->iomap = NULL; |
| 1053 | } |
| 1054 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1055 | void i915_vma_revoke_mmap(struct i915_vma *vma) |
| 1056 | { |
| 1057 | struct drm_vma_offset_node *node = &vma->obj->base.vma_node; |
| 1058 | u64 vma_offset; |
| 1059 | |
Chris Wilson | b7d151b | 2019-08-22 07:09:13 +0100 | [diff] [blame] | 1060 | lockdep_assert_held(&vma->vm->mutex); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1061 | |
| 1062 | if (!i915_vma_has_userfault(vma)) |
| 1063 | return; |
| 1064 | |
| 1065 | GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma)); |
| 1066 | GEM_BUG_ON(!vma->obj->userfault_count); |
| 1067 | |
| 1068 | vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT; |
| 1069 | unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping, |
| 1070 | drm_vma_node_offset_addr(node) + vma_offset, |
| 1071 | vma->size, |
| 1072 | 1); |
| 1073 | |
| 1074 | i915_vma_unset_userfault(vma); |
| 1075 | if (!--vma->obj->userfault_count) |
| 1076 | list_del(&vma->obj->userfault_link); |
| 1077 | } |
| 1078 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1079 | int __i915_vma_move_to_active(struct i915_vma *vma, struct i915_request *rq) |
| 1080 | { |
| 1081 | int err; |
| 1082 | |
| 1083 | GEM_BUG_ON(!i915_vma_is_pinned(vma)); |
| 1084 | |
| 1085 | /* Wait for the vma to be bound before we start! */ |
| 1086 | err = i915_request_await_active(rq, &vma->active); |
| 1087 | if (err) |
| 1088 | return err; |
| 1089 | |
| 1090 | return i915_active_add_request(&vma->active, rq); |
| 1091 | } |
| 1092 | |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 1093 | int i915_vma_move_to_active(struct i915_vma *vma, |
| 1094 | struct i915_request *rq, |
| 1095 | unsigned int flags) |
| 1096 | { |
| 1097 | struct drm_i915_gem_object *obj = vma->obj; |
Chris Wilson | a93615f | 2019-06-21 19:37:59 +0100 | [diff] [blame] | 1098 | int err; |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 1099 | |
Chris Wilson | 6951e58 | 2019-05-28 10:29:51 +0100 | [diff] [blame] | 1100 | assert_object_held(obj); |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 1101 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1102 | err = __i915_vma_move_to_active(vma, rq); |
Chris Wilson | a93615f | 2019-06-21 19:37:59 +0100 | [diff] [blame] | 1103 | if (unlikely(err)) |
| 1104 | return err; |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 1105 | |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 1106 | if (flags & EXEC_OBJECT_WRITE) { |
Chris Wilson | 8e7cb17 | 2019-08-16 08:46:35 +0100 | [diff] [blame] | 1107 | if (intel_frontbuffer_invalidate(obj->frontbuffer, ORIGIN_CS)) |
Chris Wilson | d19d71f | 2019-09-19 12:19:10 +0100 | [diff] [blame] | 1108 | i915_active_add_request(&obj->frontbuffer->write, rq); |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 1109 | |
Rodrigo Vivi | 829e8de | 2019-08-21 22:47:35 -0700 | [diff] [blame] | 1110 | dma_resv_add_excl_fence(vma->resv, &rq->fence); |
Chris Wilson | cd2a4ea | 2019-07-30 21:58:05 +0100 | [diff] [blame] | 1111 | obj->write_domain = I915_GEM_DOMAIN_RENDER; |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 1112 | obj->read_domains = 0; |
Chris Wilson | cd2a4ea | 2019-07-30 21:58:05 +0100 | [diff] [blame] | 1113 | } else { |
Rodrigo Vivi | 829e8de | 2019-08-21 22:47:35 -0700 | [diff] [blame] | 1114 | err = dma_resv_reserve_shared(vma->resv, 1); |
Chris Wilson | cd2a4ea | 2019-07-30 21:58:05 +0100 | [diff] [blame] | 1115 | if (unlikely(err)) |
| 1116 | return err; |
| 1117 | |
Rodrigo Vivi | 829e8de | 2019-08-21 22:47:35 -0700 | [diff] [blame] | 1118 | dma_resv_add_shared_fence(vma->resv, &rq->fence); |
Chris Wilson | cd2a4ea | 2019-07-30 21:58:05 +0100 | [diff] [blame] | 1119 | obj->write_domain = 0; |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 1120 | } |
| 1121 | obj->read_domains |= I915_GEM_GPU_DOMAINS; |
Chris Wilson | a93615f | 2019-06-21 19:37:59 +0100 | [diff] [blame] | 1122 | obj->mm.dirty = true; |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 1123 | |
Chris Wilson | a93615f | 2019-06-21 19:37:59 +0100 | [diff] [blame] | 1124 | GEM_BUG_ON(!i915_vma_is_active(vma)); |
Chris Wilson | e6bb1d7 | 2018-07-06 11:39:45 +0100 | [diff] [blame] | 1125 | return 0; |
| 1126 | } |
| 1127 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1128 | int __i915_vma_unbind(struct i915_vma *vma) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1129 | { |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1130 | int ret; |
| 1131 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1132 | lockdep_assert_held(&vma->vm->mutex); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1133 | |
Chris Wilson | 520ea7c | 2018-06-07 16:40:45 +0100 | [diff] [blame] | 1134 | /* |
| 1135 | * First wait upon any activity as retiring the request may |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1136 | * have side-effects such as unpinning or even unbinding this vma. |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1137 | * |
| 1138 | * XXX Actually waiting under the vm->mutex is a hinderance and |
| 1139 | * should be pipelined wherever possible. In cases where that is |
| 1140 | * unavoidable, we should lift the wait to before the mutex. |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1141 | */ |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1142 | ret = i915_vma_sync(vma); |
| 1143 | if (ret) |
| 1144 | return ret; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1145 | |
Chris Wilson | b1e3177 | 2019-10-04 14:40:00 +0100 | [diff] [blame] | 1146 | GEM_BUG_ON(i915_vma_is_active(vma)); |
Chris Wilson | 10195b1 | 2018-06-28 14:22:06 +0100 | [diff] [blame] | 1147 | if (i915_vma_is_pinned(vma)) { |
| 1148 | vma_print_allocator(vma, "is pinned"); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1149 | return -EBUSY; |
Chris Wilson | 10195b1 | 2018-06-28 14:22:06 +0100 | [diff] [blame] | 1150 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1151 | |
Chris Wilson | 274cbf2 | 2019-10-04 14:39:59 +0100 | [diff] [blame] | 1152 | GEM_BUG_ON(i915_vma_is_active(vma)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1153 | if (!drm_mm_node_allocated(&vma->node)) |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 1154 | return 0; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1155 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1156 | if (i915_vma_is_map_and_fenceable(vma)) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1157 | /* |
| 1158 | * Check that we have flushed all writes through the GGTT |
| 1159 | * before the unbind, other due to non-strict nature of those |
| 1160 | * indirect writes they may end up referencing the GGTT PTE |
| 1161 | * after the unbind. |
| 1162 | */ |
| 1163 | i915_vma_flush_writes(vma); |
| 1164 | GEM_BUG_ON(i915_vma_has_ggtt_write(vma)); |
| 1165 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1166 | /* release the fence reg _after_ flushing */ |
Chris Wilson | 1f7fd48 | 2019-08-22 07:15:57 +0100 | [diff] [blame] | 1167 | ret = i915_vma_revoke_fence(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1168 | if (ret) |
| 1169 | return ret; |
| 1170 | |
| 1171 | /* Force a pagefault for domain tracking on next user access */ |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1172 | i915_vma_revoke_mmap(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1173 | |
| 1174 | __i915_vma_iounmap(vma); |
Chris Wilson | 4dd2fbb | 2019-09-11 10:02:43 +0100 | [diff] [blame] | 1175 | clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1176 | } |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1177 | GEM_BUG_ON(vma->fence); |
| 1178 | GEM_BUG_ON(i915_vma_has_userfault(vma)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1179 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1180 | if (likely(atomic_read(&vma->vm->open))) { |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1181 | trace_i915_vma_unbind(vma); |
Chris Wilson | 93f2cde | 2018-06-07 16:40:46 +0100 | [diff] [blame] | 1182 | vma->ops->unbind_vma(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1183 | } |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1184 | atomic_and(~(I915_VMA_BIND_MASK | I915_VMA_ERROR), &vma->flags); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1185 | |
Chris Wilson | dde01d9 | 2019-10-30 19:21:49 +0000 | [diff] [blame] | 1186 | i915_vma_detach(vma); |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1187 | vma_unbind_pages(vma); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1188 | |
Chris Wilson | dde01d9 | 2019-10-30 19:21:49 +0000 | [diff] [blame] | 1189 | drm_mm_remove_node(&vma->node); /* pairs with i915_vma_destroy() */ |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |
| 1192 | |
Chris Wilson | 2850748 | 2019-10-04 14:39:58 +0100 | [diff] [blame] | 1193 | int i915_vma_unbind(struct i915_vma *vma) |
| 1194 | { |
| 1195 | struct i915_address_space *vm = vma->vm; |
| 1196 | int err; |
| 1197 | |
| 1198 | err = mutex_lock_interruptible(&vm->mutex); |
| 1199 | if (err) |
| 1200 | return err; |
| 1201 | |
| 1202 | err = __i915_vma_unbind(vma); |
| 1203 | mutex_unlock(&vm->mutex); |
| 1204 | |
| 1205 | return err; |
| 1206 | } |
| 1207 | |
Chris Wilson | 1aff190 | 2019-08-02 22:21:36 +0100 | [diff] [blame] | 1208 | struct i915_vma *i915_vma_make_unshrinkable(struct i915_vma *vma) |
| 1209 | { |
| 1210 | i915_gem_object_make_unshrinkable(vma->obj); |
| 1211 | return vma; |
| 1212 | } |
| 1213 | |
| 1214 | void i915_vma_make_shrinkable(struct i915_vma *vma) |
| 1215 | { |
| 1216 | i915_gem_object_make_shrinkable(vma->obj); |
| 1217 | } |
| 1218 | |
| 1219 | void i915_vma_make_purgeable(struct i915_vma *vma) |
| 1220 | { |
| 1221 | i915_gem_object_make_purgeable(vma->obj); |
| 1222 | } |
| 1223 | |
Chris Wilson | e3c7a1c | 2017-02-13 17:15:45 +0000 | [diff] [blame] | 1224 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 1225 | #include "selftests/i915_vma.c" |
| 1226 | #endif |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 1227 | |
Chris Wilson | 103b76ee | 2019-03-05 21:38:30 +0000 | [diff] [blame] | 1228 | static void i915_global_vma_shrink(void) |
| 1229 | { |
| 1230 | kmem_cache_shrink(global.slab_vmas); |
| 1231 | } |
| 1232 | |
| 1233 | static void i915_global_vma_exit(void) |
| 1234 | { |
| 1235 | kmem_cache_destroy(global.slab_vmas); |
| 1236 | } |
| 1237 | |
| 1238 | static struct i915_global_vma global = { { |
| 1239 | .shrink = i915_global_vma_shrink, |
| 1240 | .exit = i915_global_vma_exit, |
| 1241 | } }; |
| 1242 | |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 1243 | int __init i915_global_vma_init(void) |
| 1244 | { |
| 1245 | global.slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 1246 | if (!global.slab_vmas) |
| 1247 | return -ENOMEM; |
| 1248 | |
Chris Wilson | 103b76ee | 2019-03-05 21:38:30 +0000 | [diff] [blame] | 1249 | i915_global_register(&global.base); |
Chris Wilson | 13f1bfd | 2019-02-28 10:20:34 +0000 | [diff] [blame] | 1250 | return 0; |
| 1251 | } |