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