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 | */ |
| 24 | |
| 25 | #include "i915_vma.h" |
| 26 | |
| 27 | #include "i915_drv.h" |
| 28 | #include "intel_ringbuffer.h" |
| 29 | #include "intel_frontbuffer.h" |
| 30 | |
| 31 | #include <drm/drm_gem.h> |
| 32 | |
| 33 | static void |
| 34 | i915_vma_retire(struct i915_gem_active *active, |
| 35 | struct drm_i915_gem_request *rq) |
| 36 | { |
| 37 | const unsigned int idx = rq->engine->id; |
| 38 | struct i915_vma *vma = |
| 39 | container_of(active, struct i915_vma, last_read[idx]); |
| 40 | struct drm_i915_gem_object *obj = vma->obj; |
| 41 | |
| 42 | GEM_BUG_ON(!i915_vma_has_active_engine(vma, idx)); |
| 43 | |
| 44 | i915_vma_clear_active(vma, idx); |
| 45 | if (i915_vma_is_active(vma)) |
| 46 | return; |
| 47 | |
| 48 | list_move_tail(&vma->vm_link, &vma->vm->inactive_list); |
| 49 | if (unlikely(i915_vma_is_closed(vma) && !i915_vma_is_pinned(vma))) |
| 50 | WARN_ON(i915_vma_unbind(vma)); |
| 51 | |
| 52 | GEM_BUG_ON(!i915_gem_object_is_active(obj)); |
| 53 | if (--obj->active_count) |
| 54 | return; |
| 55 | |
| 56 | /* Bump our place on the bound list to keep it roughly in LRU order |
| 57 | * so that we don't steal from recently used but inactive objects |
| 58 | * (unless we are forced to ofc!) |
| 59 | */ |
| 60 | if (obj->bind_count) |
| 61 | list_move_tail(&obj->global_link, &rq->i915->mm.bound_list); |
| 62 | |
| 63 | obj->mm.dirty = true; /* be paranoid */ |
| 64 | |
| 65 | if (i915_gem_object_has_active_reference(obj)) { |
| 66 | i915_gem_object_clear_active_reference(obj); |
| 67 | i915_gem_object_put(obj); |
| 68 | } |
| 69 | } |
| 70 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 71 | static struct i915_vma * |
Chris Wilson | a01cb37a | 2017-01-16 15:21:30 +0000 | [diff] [blame^] | 72 | vma_create(struct drm_i915_gem_object *obj, |
| 73 | struct i915_address_space *vm, |
| 74 | const struct i915_ggtt_view *view) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 75 | { |
| 76 | struct i915_vma *vma; |
| 77 | struct rb_node *rb, **p; |
| 78 | int i; |
| 79 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 80 | vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL); |
| 81 | if (vma == NULL) |
| 82 | return ERR_PTR(-ENOMEM); |
| 83 | |
| 84 | INIT_LIST_HEAD(&vma->exec_list); |
| 85 | for (i = 0; i < ARRAY_SIZE(vma->last_read); i++) |
| 86 | init_request_active(&vma->last_read[i], i915_vma_retire); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 87 | init_request_active(&vma->last_fence, NULL); |
| 88 | list_add(&vma->vm_link, &vm->unbound_list); |
| 89 | vma->vm = vm; |
| 90 | vma->obj = obj; |
| 91 | vma->size = obj->base.size; |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 92 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 93 | |
| 94 | if (view) { |
| 95 | vma->ggtt_view = *view; |
| 96 | if (view->type == I915_GGTT_VIEW_PARTIAL) { |
Chris Wilson | 07e19ea | 2016-12-23 14:57:59 +0000 | [diff] [blame] | 97 | GEM_BUG_ON(range_overflows_t(u64, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 98 | view->partial.offset, |
| 99 | view->partial.size, |
Chris Wilson | 07e19ea | 2016-12-23 14:57:59 +0000 | [diff] [blame] | 100 | obj->base.size >> PAGE_SHIFT)); |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 101 | vma->size = view->partial.size; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 102 | vma->size <<= PAGE_SHIFT; |
Chris Wilson | 07e19ea | 2016-12-23 14:57:59 +0000 | [diff] [blame] | 103 | GEM_BUG_ON(vma->size >= obj->base.size); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 104 | } else if (view->type == I915_GGTT_VIEW_ROTATED) { |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 105 | vma->size = intel_rotation_info_size(&view->rotated); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 106 | vma->size <<= PAGE_SHIFT; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (i915_is_ggtt(vm)) { |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 111 | GEM_BUG_ON(overflows_type(vma->size, u32)); |
Chris Wilson | 91d4e0aa | 2017-01-09 16:16:13 +0000 | [diff] [blame] | 112 | vma->fence_size = i915_gem_fence_size(vm->i915, vma->size, |
| 113 | i915_gem_object_get_tiling(obj), |
| 114 | i915_gem_object_get_stride(obj)); |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 115 | GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT)); |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 116 | |
Chris Wilson | 91d4e0aa | 2017-01-09 16:16:13 +0000 | [diff] [blame] | 117 | vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size, |
| 118 | i915_gem_object_get_tiling(obj), |
| 119 | i915_gem_object_get_stride(obj)); |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 120 | GEM_BUG_ON(!is_power_of_2(vma->fence_alignment)); |
| 121 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 122 | vma->flags |= I915_VMA_GGTT; |
| 123 | list_add(&vma->obj_link, &obj->vma_list); |
| 124 | } else { |
| 125 | i915_ppgtt_get(i915_vm_to_ppgtt(vm)); |
| 126 | list_add_tail(&vma->obj_link, &obj->vma_list); |
| 127 | } |
| 128 | |
| 129 | rb = NULL; |
| 130 | p = &obj->vma_tree.rb_node; |
| 131 | while (*p) { |
| 132 | struct i915_vma *pos; |
| 133 | |
| 134 | rb = *p; |
| 135 | pos = rb_entry(rb, struct i915_vma, obj_node); |
| 136 | if (i915_vma_compare(pos, vm, view) < 0) |
| 137 | p = &rb->rb_right; |
| 138 | else |
| 139 | p = &rb->rb_left; |
| 140 | } |
| 141 | rb_link_node(&vma->obj_node, rb, p); |
| 142 | rb_insert_color(&vma->obj_node, &obj->vma_tree); |
| 143 | |
| 144 | return vma; |
| 145 | } |
| 146 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 147 | /** |
| 148 | * i915_vma_lookup - finds a matching VMA |
| 149 | * @obj: parent &struct drm_i915_gem_object to be mapped |
| 150 | * @vm: address space in which the mapping is located |
| 151 | * @view: additional mapping requirements |
| 152 | * |
| 153 | * i915_vma_lookup() looks up an existing VMA of the @obj in the @vm with |
| 154 | * the same @view characteristics. |
| 155 | * |
| 156 | * Must be called with struct_mutex held. |
| 157 | * |
| 158 | * Returns the vma if found, or NULL. |
| 159 | */ |
| 160 | struct i915_vma * |
| 161 | i915_vma_lookup(struct drm_i915_gem_object *obj, |
| 162 | struct i915_address_space *vm, |
| 163 | const struct i915_ggtt_view *view) |
| 164 | { |
| 165 | struct rb_node *rb; |
| 166 | |
| 167 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 168 | |
| 169 | rb = obj->vma_tree.rb_node; |
| 170 | while (rb) { |
| 171 | struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node); |
| 172 | long cmp; |
| 173 | |
| 174 | cmp = i915_vma_compare(vma, vm, view); |
| 175 | if (cmp == 0) |
| 176 | return vma; |
| 177 | |
| 178 | if (cmp < 0) |
| 179 | rb = rb->rb_right; |
| 180 | else |
| 181 | rb = rb->rb_left; |
| 182 | } |
| 183 | |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | /** |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 188 | * i915_vma_instance - return the singleton instance of the VMA |
| 189 | * @obj: parent &struct drm_i915_gem_object to be mapped |
| 190 | * @vm: address space in which the mapping is located |
| 191 | * @view: additional mapping requirements |
| 192 | * |
| 193 | * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with |
| 194 | * the same @view characteristics. If a match is not found, one is created. |
| 195 | * Once created, the VMA is kept until either the object is freed, or the |
| 196 | * address space is closed. |
| 197 | * |
| 198 | * Must be called with struct_mutex held. |
| 199 | * |
| 200 | * Returns the vma, or an error pointer. |
| 201 | */ |
| 202 | struct i915_vma * |
| 203 | i915_vma_instance(struct drm_i915_gem_object *obj, |
| 204 | struct i915_address_space *vm, |
| 205 | const struct i915_ggtt_view *view) |
| 206 | { |
| 207 | struct i915_vma *vma; |
| 208 | |
| 209 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 210 | GEM_BUG_ON(view && !i915_is_ggtt(vm)); |
| 211 | GEM_BUG_ON(vm->closed); |
| 212 | |
| 213 | vma = i915_vma_lookup(obj, vm, view); |
| 214 | if (!vma) |
Chris Wilson | a01cb37a | 2017-01-16 15:21:30 +0000 | [diff] [blame^] | 215 | vma = vma_create(obj, vm, view); |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 216 | |
| 217 | GEM_BUG_ON(!IS_ERR(vma) && i915_vma_is_closed(vma)); |
Chris Wilson | 4ea9527 | 2017-01-16 15:21:29 +0000 | [diff] [blame] | 218 | GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view)); |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 219 | GEM_BUG_ON(!IS_ERR(vma) && i915_vma_lookup(obj, vm, view) != vma); |
| 220 | return vma; |
| 221 | } |
| 222 | |
| 223 | /** |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 224 | * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space. |
| 225 | * @vma: VMA to map |
| 226 | * @cache_level: mapping cache level |
| 227 | * @flags: flags like global or local mapping |
| 228 | * |
| 229 | * DMA addresses are taken from the scatter-gather table of this object (or of |
| 230 | * this VMA in case of non-default GGTT views) and PTE entries set up. |
| 231 | * Note that DMA addresses are also the only part of the SG table we care about. |
| 232 | */ |
| 233 | int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level, |
| 234 | u32 flags) |
| 235 | { |
| 236 | u32 bind_flags; |
| 237 | u32 vma_flags; |
| 238 | int ret; |
| 239 | |
| 240 | if (WARN_ON(flags == 0)) |
| 241 | return -EINVAL; |
| 242 | |
| 243 | bind_flags = 0; |
| 244 | if (flags & PIN_GLOBAL) |
| 245 | bind_flags |= I915_VMA_GLOBAL_BIND; |
| 246 | if (flags & PIN_USER) |
| 247 | bind_flags |= I915_VMA_LOCAL_BIND; |
| 248 | |
| 249 | vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND); |
| 250 | if (flags & PIN_UPDATE) |
| 251 | bind_flags |= vma_flags; |
| 252 | else |
| 253 | bind_flags &= ~vma_flags; |
| 254 | if (bind_flags == 0) |
| 255 | return 0; |
| 256 | |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 257 | if (GEM_WARN_ON(range_overflows(vma->node.start, |
| 258 | vma->node.size, |
| 259 | vma->vm->total))) |
Matthew Auld | 7a0499a | 2016-12-13 20:32:20 +0000 | [diff] [blame] | 260 | return -ENODEV; |
| 261 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 262 | if (vma_flags == 0 && vma->vm->allocate_va_range) { |
| 263 | trace_i915_va_alloc(vma); |
| 264 | ret = vma->vm->allocate_va_range(vma->vm, |
| 265 | vma->node.start, |
| 266 | vma->node.size); |
| 267 | if (ret) |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | ret = vma->vm->bind_vma(vma, cache_level, bind_flags); |
| 272 | if (ret) |
| 273 | return ret; |
| 274 | |
| 275 | vma->flags |= bind_flags; |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | void __iomem *i915_vma_pin_iomap(struct i915_vma *vma) |
| 280 | { |
| 281 | void __iomem *ptr; |
| 282 | |
| 283 | /* Access through the GTT requires the device to be awake. */ |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 284 | assert_rpm_wakelock_held(vma->vm->i915); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 285 | |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 286 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 287 | if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) |
| 288 | return IO_ERR_PTR(-ENODEV); |
| 289 | |
| 290 | GEM_BUG_ON(!i915_vma_is_ggtt(vma)); |
| 291 | GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0); |
| 292 | |
| 293 | ptr = vma->iomap; |
| 294 | if (ptr == NULL) { |
| 295 | ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->mappable, |
| 296 | vma->node.start, |
| 297 | vma->node.size); |
| 298 | if (ptr == NULL) |
| 299 | return IO_ERR_PTR(-ENOMEM); |
| 300 | |
| 301 | vma->iomap = ptr; |
| 302 | } |
| 303 | |
| 304 | __i915_vma_pin(vma); |
| 305 | return ptr; |
| 306 | } |
| 307 | |
| 308 | void i915_vma_unpin_and_release(struct i915_vma **p_vma) |
| 309 | { |
| 310 | struct i915_vma *vma; |
| 311 | struct drm_i915_gem_object *obj; |
| 312 | |
| 313 | vma = fetch_and_zero(p_vma); |
| 314 | if (!vma) |
| 315 | return; |
| 316 | |
| 317 | obj = vma->obj; |
| 318 | |
| 319 | i915_vma_unpin(vma); |
| 320 | i915_vma_close(vma); |
| 321 | |
| 322 | __i915_gem_object_release_unless_active(obj); |
| 323 | } |
| 324 | |
| 325 | bool |
| 326 | i915_vma_misplaced(struct i915_vma *vma, u64 size, u64 alignment, u64 flags) |
| 327 | { |
| 328 | if (!drm_mm_node_allocated(&vma->node)) |
| 329 | return false; |
| 330 | |
| 331 | if (vma->node.size < size) |
| 332 | return true; |
| 333 | |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 334 | GEM_BUG_ON(alignment && !is_power_of_2(alignment)); |
| 335 | if (alignment && !IS_ALIGNED(vma->node.start, alignment)) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 336 | return true; |
| 337 | |
| 338 | if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma)) |
| 339 | return true; |
| 340 | |
| 341 | if (flags & PIN_OFFSET_BIAS && |
| 342 | vma->node.start < (flags & PIN_OFFSET_MASK)) |
| 343 | return true; |
| 344 | |
| 345 | if (flags & PIN_OFFSET_FIXED && |
| 346 | vma->node.start != (flags & PIN_OFFSET_MASK)) |
| 347 | return true; |
| 348 | |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | void __i915_vma_set_map_and_fenceable(struct i915_vma *vma) |
| 353 | { |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 354 | bool mappable, fenceable; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 355 | |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 356 | GEM_BUG_ON(!i915_vma_is_ggtt(vma)); |
| 357 | GEM_BUG_ON(!vma->fence_size); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 358 | |
| 359 | /* |
| 360 | * Explicitly disable for rotated VMA since the display does not |
| 361 | * need the fence and the VMA is not accessible to other users. |
| 362 | */ |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 363 | if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED) |
| 364 | return; |
| 365 | |
| 366 | fenceable = (vma->node.size >= vma->fence_size && |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 367 | IS_ALIGNED(vma->node.start, vma->fence_alignment)); |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 368 | |
| 369 | mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end; |
| 370 | |
| 371 | if (mappable && fenceable) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 372 | vma->flags |= I915_VMA_CAN_FENCE; |
| 373 | else |
| 374 | vma->flags &= ~I915_VMA_CAN_FENCE; |
| 375 | } |
| 376 | |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 377 | static bool color_differs(struct drm_mm_node *node, unsigned long color) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 378 | { |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 379 | return node->allocated && node->color != color; |
| 380 | } |
| 381 | |
| 382 | bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level) |
| 383 | { |
| 384 | struct drm_mm_node *node = &vma->node; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 385 | struct drm_mm_node *other; |
| 386 | |
| 387 | /* |
| 388 | * On some machines we have to be careful when putting differing types |
| 389 | * of snoopable memory together to avoid the prefetcher crossing memory |
| 390 | * domains and dying. During vm initialisation, we decide whether or not |
| 391 | * these constraints apply and set the drm_mm.color_adjust |
| 392 | * appropriately. |
| 393 | */ |
| 394 | if (vma->vm->mm.color_adjust == NULL) |
| 395 | return true; |
| 396 | |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 397 | /* Only valid to be called on an already inserted vma */ |
| 398 | GEM_BUG_ON(!drm_mm_node_allocated(node)); |
| 399 | GEM_BUG_ON(list_empty(&node->node_list)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 400 | |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 401 | other = list_prev_entry(node, node_list); |
Daniel Vetter | ef426c1 | 2017-01-04 11:41:10 +0100 | [diff] [blame] | 402 | if (color_differs(other, cache_level) && !drm_mm_hole_follows(other)) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 403 | return false; |
| 404 | |
Chris Wilson | 7d1d9ae | 2016-12-05 14:29:38 +0000 | [diff] [blame] | 405 | other = list_next_entry(node, node_list); |
Daniel Vetter | ef426c1 | 2017-01-04 11:41:10 +0100 | [diff] [blame] | 406 | if (color_differs(other, cache_level) && !drm_mm_hole_follows(node)) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 407 | return false; |
| 408 | |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * i915_vma_insert - finds a slot for the vma in its address space |
| 414 | * @vma: the vma |
| 415 | * @size: requested size in bytes (can be larger than the VMA) |
| 416 | * @alignment: required alignment |
| 417 | * @flags: mask of PIN_* flags to use |
| 418 | * |
| 419 | * First we try to allocate some free space that meets the requirements for |
| 420 | * the VMA. Failiing that, if the flags permit, it will evict an old VMA, |
| 421 | * preferrably the oldest idle entry to make room for the new VMA. |
| 422 | * |
| 423 | * Returns: |
| 424 | * 0 on success, negative error code otherwise. |
| 425 | */ |
| 426 | static int |
| 427 | i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags) |
| 428 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 429 | struct drm_i915_private *dev_priv = vma->vm->i915; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 430 | struct drm_i915_gem_object *obj = vma->obj; |
| 431 | u64 start, end; |
| 432 | int ret; |
| 433 | |
| 434 | GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND)); |
| 435 | GEM_BUG_ON(drm_mm_node_allocated(&vma->node)); |
| 436 | |
| 437 | size = max(size, vma->size); |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 438 | alignment = max(alignment, vma->display_alignment); |
| 439 | if (flags & PIN_MAPPABLE) { |
| 440 | size = max_t(typeof(size), size, vma->fence_size); |
| 441 | alignment = max_t(typeof(alignment), |
| 442 | alignment, vma->fence_alignment); |
| 443 | } |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 444 | |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 445 | GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE)); |
| 446 | GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT)); |
| 447 | GEM_BUG_ON(!is_power_of_2(alignment)); |
| 448 | |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 449 | start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0; |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 450 | GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 451 | |
| 452 | end = vma->vm->total; |
| 453 | if (flags & PIN_MAPPABLE) |
| 454 | end = min_t(u64, end, dev_priv->ggtt.mappable_end); |
| 455 | if (flags & PIN_ZONE_4G) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 456 | end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE); |
| 457 | GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 458 | |
| 459 | /* If binding the object/GGTT view requires more space than the entire |
| 460 | * aperture has, reject it early before evicting everything in a vain |
| 461 | * attempt to find space. |
| 462 | */ |
| 463 | if (size > end) { |
| 464 | DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n", |
| 465 | size, obj->base.size, |
| 466 | flags & PIN_MAPPABLE ? "mappable" : "total", |
| 467 | end); |
| 468 | return -E2BIG; |
| 469 | } |
| 470 | |
| 471 | ret = i915_gem_object_pin_pages(obj); |
| 472 | if (ret) |
| 473 | return ret; |
| 474 | |
| 475 | if (flags & PIN_OFFSET_FIXED) { |
| 476 | u64 offset = flags & PIN_OFFSET_MASK; |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 477 | if (!IS_ALIGNED(offset, alignment) || |
Chris Wilson | e8f9ae9 | 2017-01-06 15:20:12 +0000 | [diff] [blame] | 478 | range_overflows(offset, size, end)) { |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 479 | ret = -EINVAL; |
| 480 | goto err_unpin; |
| 481 | } |
| 482 | |
Chris Wilson | 625d988 | 2017-01-11 11:23:11 +0000 | [diff] [blame] | 483 | ret = i915_gem_gtt_reserve(vma->vm, &vma->node, |
| 484 | size, offset, obj->cache_level, |
| 485 | flags); |
| 486 | if (ret) |
| 487 | goto err_unpin; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 488 | } else { |
Chris Wilson | e007b19 | 2017-01-11 11:23:10 +0000 | [diff] [blame] | 489 | ret = i915_gem_gtt_insert(vma->vm, &vma->node, |
| 490 | size, alignment, obj->cache_level, |
| 491 | start, end, flags); |
| 492 | if (ret) |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 493 | goto err_unpin; |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 494 | |
| 495 | GEM_BUG_ON(vma->node.start < start); |
| 496 | GEM_BUG_ON(vma->node.start + vma->node.size > end); |
| 497 | } |
| 498 | GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level)); |
| 499 | |
| 500 | list_move_tail(&obj->global_link, &dev_priv->mm.bound_list); |
| 501 | list_move_tail(&vma->vm_link, &vma->vm->inactive_list); |
| 502 | obj->bind_count++; |
| 503 | GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count); |
| 504 | |
| 505 | return 0; |
| 506 | |
| 507 | err_unpin: |
| 508 | i915_gem_object_unpin_pages(obj); |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | int __i915_vma_do_pin(struct i915_vma *vma, |
| 513 | u64 size, u64 alignment, u64 flags) |
| 514 | { |
| 515 | unsigned int bound = vma->flags; |
| 516 | int ret; |
| 517 | |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 518 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 519 | GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0); |
| 520 | GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma)); |
| 521 | |
| 522 | if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) { |
| 523 | ret = -EBUSY; |
| 524 | goto err; |
| 525 | } |
| 526 | |
| 527 | if ((bound & I915_VMA_BIND_MASK) == 0) { |
| 528 | ret = i915_vma_insert(vma, size, alignment, flags); |
| 529 | if (ret) |
| 530 | goto err; |
| 531 | } |
| 532 | |
| 533 | ret = i915_vma_bind(vma, vma->obj->cache_level, flags); |
| 534 | if (ret) |
| 535 | goto err; |
| 536 | |
| 537 | if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND) |
| 538 | __i915_vma_set_map_and_fenceable(vma); |
| 539 | |
Chris Wilson | 0325701 | 2017-01-11 21:09:26 +0000 | [diff] [blame] | 540 | GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 541 | GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags)); |
| 542 | return 0; |
| 543 | |
| 544 | err: |
| 545 | __i915_vma_unpin(vma); |
| 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | void i915_vma_destroy(struct i915_vma *vma) |
| 550 | { |
| 551 | GEM_BUG_ON(vma->node.allocated); |
| 552 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 553 | GEM_BUG_ON(!i915_vma_is_closed(vma)); |
| 554 | GEM_BUG_ON(vma->fence); |
| 555 | |
| 556 | list_del(&vma->vm_link); |
| 557 | if (!i915_vma_is_ggtt(vma)) |
| 558 | i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm)); |
| 559 | |
| 560 | kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma); |
| 561 | } |
| 562 | |
| 563 | void i915_vma_close(struct i915_vma *vma) |
| 564 | { |
| 565 | GEM_BUG_ON(i915_vma_is_closed(vma)); |
| 566 | vma->flags |= I915_VMA_CLOSED; |
| 567 | |
| 568 | list_del(&vma->obj_link); |
| 569 | rb_erase(&vma->obj_node, &vma->obj->vma_tree); |
| 570 | |
| 571 | if (!i915_vma_is_active(vma) && !i915_vma_is_pinned(vma)) |
| 572 | WARN_ON(i915_vma_unbind(vma)); |
| 573 | } |
| 574 | |
| 575 | static void __i915_vma_iounmap(struct i915_vma *vma) |
| 576 | { |
| 577 | GEM_BUG_ON(i915_vma_is_pinned(vma)); |
| 578 | |
| 579 | if (vma->iomap == NULL) |
| 580 | return; |
| 581 | |
| 582 | io_mapping_unmap(vma->iomap); |
| 583 | vma->iomap = NULL; |
| 584 | } |
| 585 | |
| 586 | int i915_vma_unbind(struct i915_vma *vma) |
| 587 | { |
| 588 | struct drm_i915_gem_object *obj = vma->obj; |
| 589 | unsigned long active; |
| 590 | int ret; |
| 591 | |
| 592 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 593 | |
| 594 | /* First wait upon any activity as retiring the request may |
| 595 | * have side-effects such as unpinning or even unbinding this vma. |
| 596 | */ |
| 597 | active = i915_vma_get_active(vma); |
| 598 | if (active) { |
| 599 | int idx; |
| 600 | |
| 601 | /* When a closed VMA is retired, it is unbound - eek. |
| 602 | * In order to prevent it from being recursively closed, |
| 603 | * take a pin on the vma so that the second unbind is |
| 604 | * aborted. |
| 605 | * |
| 606 | * Even more scary is that the retire callback may free |
| 607 | * the object (last active vma). To prevent the explosion |
| 608 | * we defer the actual object free to a worker that can |
| 609 | * only proceed once it acquires the struct_mutex (which |
| 610 | * we currently hold, therefore it cannot free this object |
| 611 | * before we are finished). |
| 612 | */ |
| 613 | __i915_vma_pin(vma); |
| 614 | |
| 615 | for_each_active(active, idx) { |
| 616 | ret = i915_gem_active_retire(&vma->last_read[idx], |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 617 | &vma->vm->i915->drm.struct_mutex); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 618 | if (ret) |
| 619 | break; |
| 620 | } |
| 621 | |
| 622 | __i915_vma_unpin(vma); |
| 623 | if (ret) |
| 624 | return ret; |
| 625 | |
| 626 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 627 | } |
| 628 | |
| 629 | if (i915_vma_is_pinned(vma)) |
| 630 | return -EBUSY; |
| 631 | |
| 632 | if (!drm_mm_node_allocated(&vma->node)) |
| 633 | goto destroy; |
| 634 | |
| 635 | GEM_BUG_ON(obj->bind_count == 0); |
| 636 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 637 | |
| 638 | if (i915_vma_is_map_and_fenceable(vma)) { |
| 639 | /* release the fence reg _after_ flushing */ |
| 640 | ret = i915_vma_put_fence(vma); |
| 641 | if (ret) |
| 642 | return ret; |
| 643 | |
| 644 | /* Force a pagefault for domain tracking on next user access */ |
| 645 | i915_gem_release_mmap(obj); |
| 646 | |
| 647 | __i915_vma_iounmap(vma); |
| 648 | vma->flags &= ~I915_VMA_CAN_FENCE; |
| 649 | } |
| 650 | |
| 651 | if (likely(!vma->vm->closed)) { |
| 652 | trace_i915_vma_unbind(vma); |
| 653 | vma->vm->unbind_vma(vma); |
| 654 | } |
| 655 | vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND); |
| 656 | |
| 657 | drm_mm_remove_node(&vma->node); |
| 658 | list_move_tail(&vma->vm_link, &vma->vm->unbound_list); |
| 659 | |
| 660 | if (vma->pages != obj->mm.pages) { |
| 661 | GEM_BUG_ON(!vma->pages); |
| 662 | sg_free_table(vma->pages); |
| 663 | kfree(vma->pages); |
| 664 | } |
| 665 | vma->pages = NULL; |
| 666 | |
| 667 | /* Since the unbound list is global, only move to that list if |
| 668 | * no more VMAs exist. */ |
| 669 | if (--obj->bind_count == 0) |
| 670 | list_move_tail(&obj->global_link, |
| 671 | &to_i915(obj->base.dev)->mm.unbound_list); |
| 672 | |
| 673 | /* And finally now the object is completely decoupled from this vma, |
| 674 | * we can drop its hold on the backing storage and allow it to be |
| 675 | * reaped by the shrinker. |
| 676 | */ |
| 677 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 7a5580a | 2016-12-31 11:20:09 +0000 | [diff] [blame] | 678 | GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count); |
Joonas Lahtinen | b42fe9c | 2016-11-11 12:43:54 +0200 | [diff] [blame] | 679 | |
| 680 | destroy: |
| 681 | if (unlikely(i915_vma_is_closed(vma))) |
| 682 | i915_vma_destroy(vma); |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |