Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sub license, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the |
| 15 | * next paragraph) shall be included in all copies or substantial portions |
| 16 | * of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | **************************************************************************/ |
| 27 | /* |
| 28 | * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> |
| 29 | */ |
| 30 | |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 31 | #define pr_fmt(fmt) "[TTM] " fmt |
| 32 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 33 | #include <drm/ttm/ttm_module.h> |
| 34 | #include <drm/ttm/ttm_bo_driver.h> |
| 35 | #include <drm/ttm/ttm_placement.h> |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 36 | #include <linux/jiffies.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/sched.h> |
| 39 | #include <linux/mm.h> |
| 40 | #include <linux/file.h> |
| 41 | #include <linux/module.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 42 | #include <linux/atomic.h> |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 43 | #include <linux/reservation.h> |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 44 | |
| 45 | #define TTM_ASSERT_LOCKED(param) |
| 46 | #define TTM_DEBUG(fmt, arg...) |
| 47 | #define TTM_BO_HASH_ORDER 13 |
| 48 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 49 | static int ttm_bo_swapout(struct ttm_mem_shrink *shrink); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 50 | static void ttm_bo_global_kobj_release(struct kobject *kobj); |
| 51 | |
| 52 | static struct attribute ttm_bo_count = { |
| 53 | .name = "bo_count", |
| 54 | .mode = S_IRUGO |
| 55 | }; |
| 56 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 57 | static inline int ttm_mem_type_from_place(const struct ttm_place *place, |
| 58 | uint32_t *mem_type) |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 59 | { |
Christian König | 5d98d0bc | 2016-09-12 13:16:16 +0200 | [diff] [blame] | 60 | int pos; |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 61 | |
Christian König | 5d98d0bc | 2016-09-12 13:16:16 +0200 | [diff] [blame] | 62 | pos = ffs(place->flags & TTM_PL_MASK_MEM); |
| 63 | if (unlikely(!pos)) |
| 64 | return -EINVAL; |
| 65 | |
| 66 | *mem_type = pos - 1; |
| 67 | return 0; |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Jerome Glisse | 5012f50 | 2009-12-10 18:07:26 +0100 | [diff] [blame] | 70 | static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type) |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 71 | { |
Jerome Glisse | 5012f50 | 2009-12-10 18:07:26 +0100 | [diff] [blame] | 72 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
Christian König | 373533f | 2017-08-07 11:13:41 +0200 | [diff] [blame] | 73 | struct drm_printer p = drm_debug_printer(TTM_PFX); |
Jerome Glisse | 5012f50 | 2009-12-10 18:07:26 +0100 | [diff] [blame] | 74 | |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 75 | pr_err(" has_type: %d\n", man->has_type); |
| 76 | pr_err(" use_type: %d\n", man->use_type); |
| 77 | pr_err(" flags: 0x%08X\n", man->flags); |
Alex Deucher | 54c4cd6 | 2015-03-04 00:18:38 -0500 | [diff] [blame] | 78 | pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset); |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 79 | pr_err(" size: %llu\n", man->size); |
| 80 | pr_err(" available_caching: 0x%08X\n", man->available_caching); |
| 81 | pr_err(" default_caching: 0x%08X\n", man->default_caching); |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 82 | if (mem_type != TTM_PL_SYSTEM) |
Christian König | 373533f | 2017-08-07 11:13:41 +0200 | [diff] [blame] | 83 | (*man->func->debug)(man, &p); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo, |
| 87 | struct ttm_placement *placement) |
| 88 | { |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 89 | int i, ret, mem_type; |
| 90 | |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 91 | pr_err("No space for %p (%lu pages, %luK, %luM)\n", |
| 92 | bo, bo->mem.num_pages, bo->mem.size >> 10, |
| 93 | bo->mem.size >> 20); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 94 | for (i = 0; i < placement->num_placement; i++) { |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 95 | ret = ttm_mem_type_from_place(&placement->placement[i], |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 96 | &mem_type); |
| 97 | if (ret) |
| 98 | return; |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 99 | pr_err(" placement[%d]=0x%08X (%d)\n", |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 100 | i, placement->placement[i].flags, mem_type); |
Jerome Glisse | 5012f50 | 2009-12-10 18:07:26 +0100 | [diff] [blame] | 101 | ttm_mem_type_debug(bo->bdev, mem_type); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 105 | static ssize_t ttm_bo_global_show(struct kobject *kobj, |
| 106 | struct attribute *attr, |
| 107 | char *buffer) |
| 108 | { |
| 109 | struct ttm_bo_global *glob = |
| 110 | container_of(kobj, struct ttm_bo_global, kobj); |
| 111 | |
| 112 | return snprintf(buffer, PAGE_SIZE, "%lu\n", |
| 113 | (unsigned long) atomic_read(&glob->bo_count)); |
| 114 | } |
| 115 | |
| 116 | static struct attribute *ttm_bo_global_attrs[] = { |
| 117 | &ttm_bo_count, |
| 118 | NULL |
| 119 | }; |
| 120 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 121 | static const struct sysfs_ops ttm_bo_global_ops = { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 122 | .show = &ttm_bo_global_show |
| 123 | }; |
| 124 | |
| 125 | static struct kobj_type ttm_bo_glob_kobj_type = { |
| 126 | .release = &ttm_bo_global_kobj_release, |
| 127 | .sysfs_ops = &ttm_bo_global_ops, |
| 128 | .default_attrs = ttm_bo_global_attrs |
| 129 | }; |
| 130 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 131 | |
| 132 | static inline uint32_t ttm_bo_type_flags(unsigned type) |
| 133 | { |
| 134 | return 1 << (type); |
| 135 | } |
| 136 | |
| 137 | static void ttm_bo_release_list(struct kref *list_kref) |
| 138 | { |
| 139 | struct ttm_buffer_object *bo = |
| 140 | container_of(list_kref, struct ttm_buffer_object, list_kref); |
| 141 | struct ttm_bo_device *bdev = bo->bdev; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 142 | size_t acc_size = bo->acc_size; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 143 | |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 144 | BUG_ON(kref_read(&bo->list_kref)); |
| 145 | BUG_ON(kref_read(&bo->kref)); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 146 | BUG_ON(atomic_read(&bo->cpu_writers)); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 147 | BUG_ON(bo->mem.mm_node != NULL); |
| 148 | BUG_ON(!list_empty(&bo->lru)); |
| 149 | BUG_ON(!list_empty(&bo->ddestroy)); |
Christian König | 4279cb1 | 2016-06-06 10:17:51 +0200 | [diff] [blame] | 150 | ttm_tt_destroy(bo->ttm); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 151 | atomic_dec(&bo->glob->bo_count); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 152 | dma_fence_put(bo->moving); |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame] | 153 | if (bo->resv == &bo->ttm_resv) |
| 154 | reservation_object_fini(&bo->ttm_resv); |
Thomas Hellstrom | c58f009 | 2013-11-14 10:49:05 -0800 | [diff] [blame] | 155 | mutex_destroy(&bo->wu_mutex); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 156 | if (bo->destroy) |
| 157 | bo->destroy(bo); |
| 158 | else { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 159 | kfree(bo); |
| 160 | } |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 161 | ttm_mem_global_free(bdev->glob->mem_glob, acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 164 | void ttm_bo_add_to_lru(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 165 | { |
| 166 | struct ttm_bo_device *bdev = bo->bdev; |
Christian König | 260498f | 2017-01-12 11:50:13 +0100 | [diff] [blame] | 167 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 168 | |
Maarten Lankhorst | 009a9da | 2013-06-27 13:48:25 +0200 | [diff] [blame] | 169 | lockdep_assert_held(&bo->resv->lock.base); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 170 | |
| 171 | if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) { |
| 172 | |
| 173 | BUG_ON(!list_empty(&bo->lru)); |
| 174 | |
Christian König | 260498f | 2017-01-12 11:50:13 +0100 | [diff] [blame] | 175 | man = &bdev->man[bo->mem.mem_type]; |
| 176 | list_add_tail(&bo->lru, &man->lru[bo->priority]); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 177 | kref_get(&bo->list_kref); |
| 178 | |
Christian König | ed704a4 | 2016-01-11 15:35:19 +0100 | [diff] [blame] | 179 | if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) { |
Christian König | 260498f | 2017-01-12 11:50:13 +0100 | [diff] [blame] | 180 | list_add_tail(&bo->swap, |
| 181 | &bo->glob->swap_lru[bo->priority]); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 182 | kref_get(&bo->list_kref); |
| 183 | } |
| 184 | } |
| 185 | } |
Maarten Lankhorst | 3482032 | 2013-06-27 13:48:24 +0200 | [diff] [blame] | 186 | EXPORT_SYMBOL(ttm_bo_add_to_lru); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 187 | |
Peter Zijlstra | bdfafc4 | 2016-11-14 17:34:19 +0100 | [diff] [blame] | 188 | static void ttm_bo_ref_bug(struct kref *list_kref) |
| 189 | { |
| 190 | BUG(); |
| 191 | } |
| 192 | |
| 193 | void ttm_bo_del_from_lru(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 194 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 195 | if (!list_empty(&bo->swap)) { |
| 196 | list_del_init(&bo->swap); |
Peter Zijlstra | bdfafc4 | 2016-11-14 17:34:19 +0100 | [diff] [blame] | 197 | kref_put(&bo->list_kref, ttm_bo_ref_bug); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 198 | } |
| 199 | if (!list_empty(&bo->lru)) { |
| 200 | list_del_init(&bo->lru); |
Peter Zijlstra | bdfafc4 | 2016-11-14 17:34:19 +0100 | [diff] [blame] | 201 | kref_put(&bo->list_kref, ttm_bo_ref_bug); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 202 | } |
| 203 | |
Christian König | 896d630 | 2017-01-12 11:54:11 +0100 | [diff] [blame] | 204 | /* |
| 205 | * TODO: Add a driver hook to delete from |
| 206 | * driver-specific LRU's here. |
| 207 | */ |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 208 | } |
| 209 | |
Maarten Lankhorst | 3482032 | 2013-06-27 13:48:24 +0200 | [diff] [blame] | 210 | void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 211 | { |
Maarten Lankhorst | 3482032 | 2013-06-27 13:48:24 +0200 | [diff] [blame] | 212 | spin_lock(&bo->glob->lru_lock); |
Peter Zijlstra | bdfafc4 | 2016-11-14 17:34:19 +0100 | [diff] [blame] | 213 | ttm_bo_del_from_lru(bo); |
Maarten Lankhorst | 3482032 | 2013-06-27 13:48:24 +0200 | [diff] [blame] | 214 | spin_unlock(&bo->glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 215 | } |
Maarten Lankhorst | 3482032 | 2013-06-27 13:48:24 +0200 | [diff] [blame] | 216 | EXPORT_SYMBOL(ttm_bo_del_sub_from_lru); |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 217 | |
Christian König | ab74961 | 2016-01-11 15:35:20 +0100 | [diff] [blame] | 218 | void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo) |
| 219 | { |
Christian König | ab74961 | 2016-01-11 15:35:20 +0100 | [diff] [blame] | 220 | lockdep_assert_held(&bo->resv->lock.base); |
| 221 | |
Peter Zijlstra | bdfafc4 | 2016-11-14 17:34:19 +0100 | [diff] [blame] | 222 | ttm_bo_del_from_lru(bo); |
Flora Cui | 56fc350 | 2016-04-20 10:23:47 +0800 | [diff] [blame] | 223 | ttm_bo_add_to_lru(bo); |
Christian König | ab74961 | 2016-01-11 15:35:20 +0100 | [diff] [blame] | 224 | } |
| 225 | EXPORT_SYMBOL(ttm_bo_move_to_lru_tail); |
| 226 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 227 | /* |
| 228 | * Call bo->mutex locked. |
| 229 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 230 | static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc) |
| 231 | { |
| 232 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 233 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 234 | int ret = 0; |
| 235 | uint32_t page_flags = 0; |
| 236 | |
| 237 | TTM_ASSERT_LOCKED(&bo->mutex); |
| 238 | bo->ttm = NULL; |
| 239 | |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 240 | if (bdev->need_dma32) |
| 241 | page_flags |= TTM_PAGE_FLAG_DMA32; |
| 242 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 243 | switch (bo->type) { |
| 244 | case ttm_bo_type_device: |
| 245 | if (zero_alloc) |
| 246 | page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC; |
| 247 | case ttm_bo_type_kernel: |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 248 | bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, |
| 249 | page_flags, glob->dummy_read_page); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 250 | if (unlikely(bo->ttm == NULL)) |
| 251 | ret = -ENOMEM; |
| 252 | break; |
Dave Airlie | 129b78b | 2012-04-02 11:46:06 +0100 | [diff] [blame] | 253 | case ttm_bo_type_sg: |
| 254 | bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, |
| 255 | page_flags | TTM_PAGE_FLAG_SG, |
| 256 | glob->dummy_read_page); |
| 257 | if (unlikely(bo->ttm == NULL)) { |
| 258 | ret = -ENOMEM; |
| 259 | break; |
| 260 | } |
| 261 | bo->ttm->sg = bo->sg; |
| 262 | break; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 263 | default: |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 264 | pr_err("Illegal buffer object type\n"); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 265 | ret = -EINVAL; |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, |
| 273 | struct ttm_mem_reg *mem, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 274 | bool evict, bool interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 275 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 276 | { |
| 277 | struct ttm_bo_device *bdev = bo->bdev; |
| 278 | bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem); |
| 279 | bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem); |
| 280 | struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type]; |
| 281 | struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type]; |
| 282 | int ret = 0; |
| 283 | |
| 284 | if (old_is_pci || new_is_pci || |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 285 | ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) { |
| 286 | ret = ttm_mem_io_lock(old_man, true); |
| 287 | if (unlikely(ret != 0)) |
| 288 | goto out_err; |
| 289 | ttm_bo_unmap_virtual_locked(bo); |
| 290 | ttm_mem_io_unlock(old_man); |
| 291 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | * Create and bind a ttm if required. |
| 295 | */ |
| 296 | |
Ben Skeggs | 8d3bb23 | 2011-08-22 03:15:05 +0000 | [diff] [blame] | 297 | if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) { |
| 298 | if (bo->ttm == NULL) { |
Ben Skeggs | ff02b13 | 2011-09-14 06:08:06 +1000 | [diff] [blame] | 299 | bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED); |
| 300 | ret = ttm_bo_add_ttm(bo, zero); |
Ben Skeggs | 8d3bb23 | 2011-08-22 03:15:05 +0000 | [diff] [blame] | 301 | if (ret) |
| 302 | goto out_err; |
| 303 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 304 | |
| 305 | ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement); |
| 306 | if (ret) |
Thomas Hellstrom | 87ef920 | 2009-06-17 12:29:57 +0200 | [diff] [blame] | 307 | goto out_err; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 308 | |
| 309 | if (mem->mem_type != TTM_PL_SYSTEM) { |
| 310 | ret = ttm_tt_bind(bo->ttm, mem); |
| 311 | if (ret) |
| 312 | goto out_err; |
| 313 | } |
| 314 | |
| 315 | if (bo->mem.mem_type == TTM_PL_SYSTEM) { |
Ben Skeggs | 82ef594 | 2011-02-02 00:27:10 +0000 | [diff] [blame] | 316 | if (bdev->driver->move_notify) |
Nicolai Hähnle | 66257db | 2016-12-15 17:23:49 +0100 | [diff] [blame] | 317 | bdev->driver->move_notify(bo, evict, mem); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 318 | bo->mem = *mem; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 319 | mem->mm_node = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 320 | goto moved; |
| 321 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 324 | if (bdev->driver->move_notify) |
Nicolai Hähnle | 66257db | 2016-12-15 17:23:49 +0100 | [diff] [blame] | 325 | bdev->driver->move_notify(bo, evict, mem); |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 326 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 327 | if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) && |
| 328 | !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) |
Michel Dänzer | 4e2f0ca | 2016-08-08 12:28:25 +0900 | [diff] [blame] | 329 | ret = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 330 | else if (bdev->driver->move) |
| 331 | ret = bdev->driver->move(bo, evict, interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 332 | no_wait_gpu, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 333 | else |
Michel Dänzer | 4499f2a | 2016-08-08 12:28:26 +0900 | [diff] [blame] | 334 | ret = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 335 | |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 336 | if (ret) { |
| 337 | if (bdev->driver->move_notify) { |
| 338 | struct ttm_mem_reg tmp_mem = *mem; |
| 339 | *mem = bo->mem; |
| 340 | bo->mem = tmp_mem; |
Nicolai Hähnle | 66257db | 2016-12-15 17:23:49 +0100 | [diff] [blame] | 341 | bdev->driver->move_notify(bo, false, mem); |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 342 | bo->mem = *mem; |
Dave Airlie | 014b344 | 2013-01-16 15:58:34 +1000 | [diff] [blame] | 343 | *mem = tmp_mem; |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 344 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 345 | |
Ben Skeggs | 9f1feed | 2012-01-25 15:34:22 +1000 | [diff] [blame] | 346 | goto out_err; |
| 347 | } |
Jerome Glisse | dc97b34 | 2011-11-18 11:47:03 -0500 | [diff] [blame] | 348 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 349 | moved: |
| 350 | if (bo->evicted) { |
Rob Clark | 9ef7506 | 2014-03-12 10:59:37 -0400 | [diff] [blame] | 351 | if (bdev->driver->invalidate_caches) { |
| 352 | ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement); |
| 353 | if (ret) |
| 354 | pr_err("Can not flush read caches\n"); |
| 355 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 356 | bo->evicted = false; |
| 357 | } |
| 358 | |
| 359 | if (bo->mem.mm_node) { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 360 | bo->offset = (bo->mem.start << PAGE_SHIFT) + |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 361 | bdev->man[bo->mem.mem_type].gpu_offset; |
| 362 | bo->cur_placement = bo->mem.placement; |
Thomas Hellstrom | 354fb52 | 2010-01-13 22:28:45 +0100 | [diff] [blame] | 363 | } else |
| 364 | bo->offset = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 365 | |
| 366 | return 0; |
| 367 | |
| 368 | out_err: |
| 369 | new_man = &bdev->man[bo->mem.mem_type]; |
Christian König | 4279cb1 | 2016-06-06 10:17:51 +0200 | [diff] [blame] | 370 | if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 371 | ttm_tt_destroy(bo->ttm); |
| 372 | bo->ttm = NULL; |
| 373 | } |
| 374 | |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | /** |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 379 | * Call bo::reserved. |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 380 | * Will release GPU memory type usage on destruction. |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 381 | * This is the place to put in driver specific hooks to release |
| 382 | * driver private resources. |
| 383 | * Will release the bo::reserved lock. |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 384 | */ |
| 385 | |
| 386 | static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo) |
| 387 | { |
Jerome Glisse | dc97b34 | 2011-11-18 11:47:03 -0500 | [diff] [blame] | 388 | if (bo->bdev->driver->move_notify) |
Nicolai Hähnle | 66257db | 2016-12-15 17:23:49 +0100 | [diff] [blame] | 389 | bo->bdev->driver->move_notify(bo, false, NULL); |
Jerome Glisse | dc97b34 | 2011-11-18 11:47:03 -0500 | [diff] [blame] | 390 | |
Christian König | 4279cb1 | 2016-06-06 10:17:51 +0200 | [diff] [blame] | 391 | ttm_tt_destroy(bo->ttm); |
| 392 | bo->ttm = NULL; |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 393 | ttm_bo_mem_put(bo, &bo->mem); |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 394 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame] | 395 | ww_mutex_unlock (&bo->resv->lock); |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Christian König | 841e763 | 2017-07-20 20:55:06 +0200 | [diff] [blame] | 398 | static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo) |
| 399 | { |
| 400 | int r; |
| 401 | |
| 402 | if (bo->resv == &bo->ttm_resv) |
| 403 | return 0; |
| 404 | |
| 405 | reservation_object_init(&bo->ttm_resv); |
Christian König | df9bcb0 | 2017-08-17 12:23:51 +0200 | [diff] [blame^] | 406 | BUG_ON(!reservation_object_trylock(&bo->ttm_resv)); |
Christian König | 841e763 | 2017-07-20 20:55:06 +0200 | [diff] [blame] | 407 | |
| 408 | r = reservation_object_copy_fences(&bo->ttm_resv, bo->resv); |
| 409 | if (r) { |
| 410 | reservation_object_unlock(&bo->ttm_resv); |
| 411 | reservation_object_fini(&bo->ttm_resv); |
| 412 | } |
| 413 | |
| 414 | return r; |
| 415 | } |
| 416 | |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 417 | static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo) |
| 418 | { |
| 419 | struct reservation_object_list *fobj; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 420 | struct dma_fence *fence; |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 421 | int i; |
| 422 | |
Christian König | 841e763 | 2017-07-20 20:55:06 +0200 | [diff] [blame] | 423 | fobj = reservation_object_get_list(&bo->ttm_resv); |
| 424 | fence = reservation_object_get_excl(&bo->ttm_resv); |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 425 | if (fence && !fence->ops->signaled) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 426 | dma_fence_enable_sw_signaling(fence); |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 427 | |
| 428 | for (i = 0; fobj && i < fobj->shared_count; ++i) { |
| 429 | fence = rcu_dereference_protected(fobj->shared[i], |
| 430 | reservation_object_held(bo->resv)); |
| 431 | |
| 432 | if (!fence->ops->signaled) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 433 | dma_fence_enable_sw_signaling(fence); |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 437 | static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 438 | { |
| 439 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 440 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 441 | int ret; |
| 442 | |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 443 | spin_lock(&glob->lru_lock); |
Christian König | dfd5e50 | 2016-04-06 11:12:03 +0200 | [diff] [blame] | 444 | ret = __ttm_bo_reserve(bo, false, true, NULL); |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 445 | |
Thomas Hellstrom | 15205fb | 2013-10-10 11:09:03 -0700 | [diff] [blame] | 446 | if (!ret) { |
Christian König | 8aa6d4f | 2016-04-06 11:12:04 +0200 | [diff] [blame] | 447 | if (!ttm_bo_wait(bo, false, true)) { |
Peter Zijlstra | bdfafc4 | 2016-11-14 17:34:19 +0100 | [diff] [blame] | 448 | ttm_bo_del_from_lru(bo); |
Maarten Lankhorst | dd7cfd6 | 2014-01-21 13:07:31 +0100 | [diff] [blame] | 449 | spin_unlock(&glob->lru_lock); |
| 450 | ttm_bo_cleanup_memtype_use(bo); |
| 451 | |
Maarten Lankhorst | dd7cfd6 | 2014-01-21 13:07:31 +0100 | [diff] [blame] | 452 | return; |
Christian König | 841e763 | 2017-07-20 20:55:06 +0200 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | ret = ttm_bo_individualize_resv(bo); |
| 456 | if (ret) { |
| 457 | /* Last resort, if we fail to allocate memory for the |
| 458 | * fences block for the BO to become idle and free it. |
| 459 | */ |
| 460 | spin_unlock(&glob->lru_lock); |
| 461 | ttm_bo_wait(bo, true, true); |
| 462 | ttm_bo_cleanup_memtype_use(bo); |
| 463 | return; |
| 464 | } |
| 465 | ttm_bo_flush_all_fences(bo); |
Thomas Hellstrom | 15205fb | 2013-10-10 11:09:03 -0700 | [diff] [blame] | 466 | |
| 467 | /* |
| 468 | * Make NO_EVICT bos immediately available to |
| 469 | * shrinkers, now that they are queued for |
| 470 | * destruction. |
| 471 | */ |
| 472 | if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) { |
| 473 | bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT; |
| 474 | ttm_bo_add_to_lru(bo); |
| 475 | } |
| 476 | |
Christian König | 841e763 | 2017-07-20 20:55:06 +0200 | [diff] [blame] | 477 | if (bo->resv != &bo->ttm_resv) |
| 478 | reservation_object_unlock(&bo->ttm_resv); |
Thomas Hellstrom | c752308 | 2014-02-20 11:36:25 +0100 | [diff] [blame] | 479 | __ttm_bo_unreserve(bo); |
Thomas Hellstrom | 15205fb | 2013-10-10 11:09:03 -0700 | [diff] [blame] | 480 | } |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 481 | |
| 482 | kref_get(&bo->list_kref); |
| 483 | list_add_tail(&bo->ddestroy, &bdev->ddestroy); |
| 484 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 485 | |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 486 | schedule_delayed_work(&bdev->wq, |
| 487 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 488 | } |
| 489 | |
| 490 | /** |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 491 | * function ttm_bo_cleanup_refs_and_unlock |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 492 | * If bo idle, remove from delayed- and lru lists, and unref. |
| 493 | * If not idle, do nothing. |
| 494 | * |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 495 | * Must be called with lru_lock and reservation held, this function |
| 496 | * will drop both before returning. |
| 497 | * |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 498 | * @interruptible Any sleeps should occur interruptibly. |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 499 | * @no_wait_gpu Never wait for gpu. Return -EBUSY instead. |
| 500 | */ |
| 501 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 502 | static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo, |
| 503 | bool interruptible, |
| 504 | bool no_wait_gpu) |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 505 | { |
| 506 | struct ttm_bo_global *glob = bo->glob; |
Christian König | 841e763 | 2017-07-20 20:55:06 +0200 | [diff] [blame] | 507 | struct reservation_object *resv; |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 508 | int ret; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 509 | |
Christian König | 841e763 | 2017-07-20 20:55:06 +0200 | [diff] [blame] | 510 | if (unlikely(list_empty(&bo->ddestroy))) |
| 511 | resv = bo->resv; |
| 512 | else |
| 513 | resv = &bo->ttm_resv; |
| 514 | |
| 515 | if (reservation_object_test_signaled_rcu(resv, true)) |
| 516 | ret = 0; |
| 517 | else |
| 518 | ret = -EBUSY; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 519 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 520 | if (ret && !no_wait_gpu) { |
Maarten Lankhorst | 472db7a | 2014-05-14 15:42:29 +0200 | [diff] [blame] | 521 | long lret; |
| 522 | ww_mutex_unlock(&bo->resv->lock); |
| 523 | spin_unlock(&glob->lru_lock); |
| 524 | |
Christian König | 841e763 | 2017-07-20 20:55:06 +0200 | [diff] [blame] | 525 | lret = reservation_object_wait_timeout_rcu(resv, true, |
Maarten Lankhorst | 472db7a | 2014-05-14 15:42:29 +0200 | [diff] [blame] | 526 | interruptible, |
| 527 | 30 * HZ); |
| 528 | |
| 529 | if (lret < 0) |
| 530 | return lret; |
| 531 | else if (lret == 0) |
| 532 | return -EBUSY; |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 533 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 534 | spin_lock(&glob->lru_lock); |
Christian König | dfd5e50 | 2016-04-06 11:12:03 +0200 | [diff] [blame] | 535 | ret = __ttm_bo_reserve(bo, false, true, NULL); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * We raced, and lost, someone else holds the reservation now, |
| 539 | * and is probably busy in ttm_bo_cleanup_memtype_use. |
| 540 | * |
| 541 | * Even if it's not the case, because we finished waiting any |
| 542 | * delayed destruction would succeed, so just return success |
| 543 | * here. |
| 544 | */ |
| 545 | if (ret) { |
| 546 | spin_unlock(&glob->lru_lock); |
| 547 | return 0; |
| 548 | } |
Maarten Lankhorst | 7040138 | 2014-01-21 13:07:01 +0100 | [diff] [blame] | 549 | } |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 550 | |
| 551 | if (ret || unlikely(list_empty(&bo->ddestroy))) { |
Thomas Hellstrom | c752308 | 2014-02-20 11:36:25 +0100 | [diff] [blame] | 552 | __ttm_bo_unreserve(bo); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 553 | spin_unlock(&glob->lru_lock); |
| 554 | return ret; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 555 | } |
| 556 | |
Peter Zijlstra | bdfafc4 | 2016-11-14 17:34:19 +0100 | [diff] [blame] | 557 | ttm_bo_del_from_lru(bo); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 558 | list_del_init(&bo->ddestroy); |
Peter Zijlstra | bdfafc4 | 2016-11-14 17:34:19 +0100 | [diff] [blame] | 559 | kref_put(&bo->list_kref, ttm_bo_ref_bug); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 560 | |
| 561 | spin_unlock(&glob->lru_lock); |
| 562 | ttm_bo_cleanup_memtype_use(bo); |
| 563 | |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 564 | return 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Traverse the delayed list, and call ttm_bo_cleanup_refs on all |
| 569 | * encountered buffers. |
| 570 | */ |
| 571 | |
| 572 | static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all) |
| 573 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 574 | struct ttm_bo_global *glob = bdev->glob; |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 575 | struct ttm_buffer_object *entry = NULL; |
| 576 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 577 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 578 | spin_lock(&glob->lru_lock); |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 579 | if (list_empty(&bdev->ddestroy)) |
| 580 | goto out_unlock; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 581 | |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 582 | entry = list_first_entry(&bdev->ddestroy, |
| 583 | struct ttm_buffer_object, ddestroy); |
| 584 | kref_get(&entry->list_kref); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 585 | |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 586 | for (;;) { |
| 587 | struct ttm_buffer_object *nentry = NULL; |
| 588 | |
| 589 | if (entry->ddestroy.next != &bdev->ddestroy) { |
| 590 | nentry = list_first_entry(&entry->ddestroy, |
| 591 | struct ttm_buffer_object, ddestroy); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 592 | kref_get(&nentry->list_kref); |
| 593 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 594 | |
Christian König | dfd5e50 | 2016-04-06 11:12:03 +0200 | [diff] [blame] | 595 | ret = __ttm_bo_reserve(entry, false, true, NULL); |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 596 | if (remove_all && ret) { |
| 597 | spin_unlock(&glob->lru_lock); |
Christian König | dfd5e50 | 2016-04-06 11:12:03 +0200 | [diff] [blame] | 598 | ret = __ttm_bo_reserve(entry, false, false, NULL); |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 599 | spin_lock(&glob->lru_lock); |
| 600 | } |
| 601 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 602 | if (!ret) |
| 603 | ret = ttm_bo_cleanup_refs_and_unlock(entry, false, |
| 604 | !remove_all); |
| 605 | else |
| 606 | spin_unlock(&glob->lru_lock); |
| 607 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 608 | kref_put(&entry->list_kref, ttm_bo_release_list); |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 609 | entry = nentry; |
| 610 | |
| 611 | if (ret || !entry) |
| 612 | goto out; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 613 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 614 | spin_lock(&glob->lru_lock); |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 615 | if (list_empty(&entry->ddestroy)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 616 | break; |
| 617 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 618 | |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 619 | out_unlock: |
| 620 | spin_unlock(&glob->lru_lock); |
| 621 | out: |
| 622 | if (entry) |
| 623 | kref_put(&entry->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | static void ttm_bo_delayed_workqueue(struct work_struct *work) |
| 628 | { |
| 629 | struct ttm_bo_device *bdev = |
| 630 | container_of(work, struct ttm_bo_device, wq.work); |
| 631 | |
| 632 | if (ttm_bo_delayed_delete(bdev, false)) { |
| 633 | schedule_delayed_work(&bdev->wq, |
| 634 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | static void ttm_bo_release(struct kref *kref) |
| 639 | { |
| 640 | struct ttm_buffer_object *bo = |
| 641 | container_of(kref, struct ttm_buffer_object, kref); |
| 642 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 643 | struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 644 | |
David Herrmann | 72525b3 | 2013-07-24 21:08:53 +0200 | [diff] [blame] | 645 | drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 646 | ttm_mem_io_lock(man, false); |
| 647 | ttm_mem_io_free_vm(bo); |
| 648 | ttm_mem_io_unlock(man); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 649 | ttm_bo_cleanup_refs_or_queue(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 650 | kref_put(&bo->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | void ttm_bo_unref(struct ttm_buffer_object **p_bo) |
| 654 | { |
| 655 | struct ttm_buffer_object *bo = *p_bo; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 656 | |
| 657 | *p_bo = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 658 | kref_put(&bo->kref, ttm_bo_release); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 659 | } |
| 660 | EXPORT_SYMBOL(ttm_bo_unref); |
| 661 | |
Matthew Garrett | 7c5ee53 | 2010-04-26 16:00:09 -0400 | [diff] [blame] | 662 | int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev) |
| 663 | { |
| 664 | return cancel_delayed_work_sync(&bdev->wq); |
| 665 | } |
| 666 | EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue); |
| 667 | |
| 668 | void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched) |
| 669 | { |
| 670 | if (resched) |
| 671 | schedule_delayed_work(&bdev->wq, |
| 672 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 673 | } |
| 674 | EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue); |
| 675 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 676 | static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 677 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 678 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 679 | struct ttm_bo_device *bdev = bo->bdev; |
| 680 | struct ttm_mem_reg evict_mem; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 681 | struct ttm_placement placement; |
| 682 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 683 | |
Maarten Lankhorst | 009a9da | 2013-06-27 13:48:25 +0200 | [diff] [blame] | 684 | lockdep_assert_held(&bo->resv->lock.base); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 685 | |
| 686 | evict_mem = bo->mem; |
| 687 | evict_mem.mm_node = NULL; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 688 | evict_mem.bus.io_reserved_vm = false; |
| 689 | evict_mem.bus.io_reserved_count = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 690 | |
Jerome Glisse | 7cb7d1d | 2009-12-09 22:14:27 +0100 | [diff] [blame] | 691 | placement.num_placement = 0; |
| 692 | placement.num_busy_placement = 0; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 693 | bdev->driver->evict_flags(bo, &placement); |
| 694 | ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 695 | no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 696 | if (ret) { |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 697 | if (ret != -ERESTARTSYS) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 698 | pr_err("Failed to find memory space for buffer 0x%p eviction\n", |
| 699 | bo); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 700 | ttm_bo_mem_space_debug(bo, &placement); |
| 701 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 702 | goto out; |
| 703 | } |
| 704 | |
| 705 | ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 706 | no_wait_gpu); |
Christian König | 17d33bc | 2016-06-06 10:17:56 +0200 | [diff] [blame] | 707 | if (unlikely(ret)) { |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 708 | if (ret != -ERESTARTSYS) |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 709 | pr_err("Buffer eviction failed\n"); |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 710 | ttm_bo_mem_put(bo, &evict_mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 711 | goto out; |
| 712 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 713 | bo->evicted = true; |
| 714 | out: |
| 715 | return ret; |
| 716 | } |
| 717 | |
Christian König | a2ab19fe | 2016-08-30 17:26:04 +0200 | [diff] [blame] | 718 | bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, |
| 719 | const struct ttm_place *place) |
| 720 | { |
| 721 | /* Don't evict this BO if it's outside of the |
| 722 | * requested placement range |
| 723 | */ |
| 724 | if (place->fpfn >= (bo->mem.start + bo->mem.size) || |
| 725 | (place->lpfn && place->lpfn <= bo->mem.start)) |
| 726 | return false; |
| 727 | |
| 728 | return true; |
| 729 | } |
| 730 | EXPORT_SYMBOL(ttm_bo_eviction_valuable); |
| 731 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 732 | static int ttm_mem_evict_first(struct ttm_bo_device *bdev, |
| 733 | uint32_t mem_type, |
Michel Dänzer | e300180 | 2014-10-09 15:02:59 +0900 | [diff] [blame] | 734 | const struct ttm_place *place, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 735 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 736 | bool no_wait_gpu) |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 737 | { |
| 738 | struct ttm_bo_global *glob = bdev->glob; |
| 739 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
| 740 | struct ttm_buffer_object *bo; |
Peter Zijlstra | bdfafc4 | 2016-11-14 17:34:19 +0100 | [diff] [blame] | 741 | int ret = -EBUSY; |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 742 | unsigned i; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 743 | |
| 744 | spin_lock(&glob->lru_lock); |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 745 | for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { |
| 746 | list_for_each_entry(bo, &man->lru[i], lru) { |
| 747 | ret = __ttm_bo_reserve(bo, false, true, NULL); |
| 748 | if (ret) |
| 749 | continue; |
Michel Dänzer | e300180 | 2014-10-09 15:02:59 +0900 | [diff] [blame] | 750 | |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 751 | if (place && !bdev->driver->eviction_valuable(bo, |
| 752 | place)) { |
| 753 | __ttm_bo_unreserve(bo); |
| 754 | ret = -EBUSY; |
| 755 | continue; |
| 756 | } |
| 757 | |
| 758 | break; |
Michel Dänzer | e300180 | 2014-10-09 15:02:59 +0900 | [diff] [blame] | 759 | } |
Christian König | a2ab19fe | 2016-08-30 17:26:04 +0200 | [diff] [blame] | 760 | |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 761 | if (!ret) |
| 762 | break; |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 763 | } |
| 764 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 765 | if (ret) { |
| 766 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | b8e902f | 2012-10-22 12:51:26 +0000 | [diff] [blame] | 767 | return ret; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 768 | } |
| 769 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 770 | kref_get(&bo->list_kref); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 771 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 772 | if (!list_empty(&bo->ddestroy)) { |
| 773 | ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible, |
| 774 | no_wait_gpu); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 775 | kref_put(&bo->list_kref, ttm_bo_release_list); |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 776 | return ret; |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 777 | } |
| 778 | |
Peter Zijlstra | bdfafc4 | 2016-11-14 17:34:19 +0100 | [diff] [blame] | 779 | ttm_bo_del_from_lru(bo); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 780 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 781 | |
| 782 | BUG_ON(ret != 0); |
| 783 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 784 | ret = ttm_bo_evict(bo, interruptible, no_wait_gpu); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 785 | ttm_bo_unreserve(bo); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 786 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 787 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 788 | return ret; |
| 789 | } |
| 790 | |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 791 | void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem) |
| 792 | { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 793 | struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type]; |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 794 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 795 | if (mem->mm_node) |
| 796 | (*man->func->put_node)(man, mem); |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 797 | } |
| 798 | EXPORT_SYMBOL(ttm_bo_mem_put); |
| 799 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 800 | /** |
Christian König | 3ddf4ad | 2016-06-15 13:44:03 +0200 | [diff] [blame] | 801 | * Add the last move fence to the BO and reserve a new shared slot. |
| 802 | */ |
| 803 | static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo, |
| 804 | struct ttm_mem_type_manager *man, |
| 805 | struct ttm_mem_reg *mem) |
| 806 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 807 | struct dma_fence *fence; |
Christian König | 3ddf4ad | 2016-06-15 13:44:03 +0200 | [diff] [blame] | 808 | int ret; |
| 809 | |
| 810 | spin_lock(&man->move_lock); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 811 | fence = dma_fence_get(man->move); |
Christian König | 3ddf4ad | 2016-06-15 13:44:03 +0200 | [diff] [blame] | 812 | spin_unlock(&man->move_lock); |
| 813 | |
| 814 | if (fence) { |
| 815 | reservation_object_add_shared_fence(bo->resv, fence); |
| 816 | |
| 817 | ret = reservation_object_reserve_shared(bo->resv); |
| 818 | if (unlikely(ret)) |
| 819 | return ret; |
| 820 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 821 | dma_fence_put(bo->moving); |
Christian König | 3ddf4ad | 2016-06-15 13:44:03 +0200 | [diff] [blame] | 822 | bo->moving = fence; |
| 823 | } |
| 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | /** |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 829 | * Repeatedly evict memory from the LRU for @mem_type until we create enough |
| 830 | * space, or we've evicted everything and there isn't enough space. |
| 831 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 832 | static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo, |
| 833 | uint32_t mem_type, |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 834 | const struct ttm_place *place, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 835 | struct ttm_mem_reg *mem, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 836 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 837 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 838 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 839 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 840 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 841 | int ret; |
| 842 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 843 | do { |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 844 | ret = (*man->func->get_node)(man, bo, place, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 845 | if (unlikely(ret != 0)) |
| 846 | return ret; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 847 | if (mem->mm_node) |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 848 | break; |
Michel Dänzer | e300180 | 2014-10-09 15:02:59 +0900 | [diff] [blame] | 849 | ret = ttm_mem_evict_first(bdev, mem_type, place, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 850 | interruptible, no_wait_gpu); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 851 | if (unlikely(ret != 0)) |
| 852 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 853 | } while (1); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 854 | mem->mem_type = mem_type; |
Christian König | 3ddf4ad | 2016-06-15 13:44:03 +0200 | [diff] [blame] | 855 | return ttm_bo_add_move_fence(bo, man, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 856 | } |
| 857 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 858 | static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man, |
| 859 | uint32_t cur_placement, |
| 860 | uint32_t proposed_placement) |
| 861 | { |
| 862 | uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING; |
| 863 | uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING; |
| 864 | |
| 865 | /** |
| 866 | * Keep current caching if possible. |
| 867 | */ |
| 868 | |
| 869 | if ((cur_placement & caching) != 0) |
| 870 | result |= (cur_placement & caching); |
| 871 | else if ((man->default_caching & caching) != 0) |
| 872 | result |= man->default_caching; |
| 873 | else if ((TTM_PL_FLAG_CACHED & caching) != 0) |
| 874 | result |= TTM_PL_FLAG_CACHED; |
| 875 | else if ((TTM_PL_FLAG_WC & caching) != 0) |
| 876 | result |= TTM_PL_FLAG_WC; |
| 877 | else if ((TTM_PL_FLAG_UNCACHED & caching) != 0) |
| 878 | result |= TTM_PL_FLAG_UNCACHED; |
| 879 | |
| 880 | return result; |
| 881 | } |
| 882 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 883 | static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 884 | uint32_t mem_type, |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 885 | const struct ttm_place *place, |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 886 | uint32_t *masked_placement) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 887 | { |
| 888 | uint32_t cur_flags = ttm_bo_type_flags(mem_type); |
| 889 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 890 | if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 891 | return false; |
| 892 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 893 | if ((place->flags & man->available_caching) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 894 | return false; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 895 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 896 | cur_flags |= (place->flags & man->available_caching); |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 897 | |
| 898 | *masked_placement = cur_flags; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 899 | return true; |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * Creates space for memory region @mem according to its type. |
| 904 | * |
| 905 | * This function first searches for free space in compatible memory types in |
| 906 | * the priority order defined by the driver. If free space isn't found, then |
| 907 | * ttm_bo_mem_force_space is attempted in priority order to evict and find |
| 908 | * space. |
| 909 | */ |
| 910 | int ttm_bo_mem_space(struct ttm_buffer_object *bo, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 911 | struct ttm_placement *placement, |
| 912 | struct ttm_mem_reg *mem, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 913 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 914 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 915 | { |
| 916 | struct ttm_bo_device *bdev = bo->bdev; |
| 917 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 918 | uint32_t mem_type = TTM_PL_SYSTEM; |
| 919 | uint32_t cur_flags = 0; |
| 920 | bool type_found = false; |
| 921 | bool type_ok = false; |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 922 | bool has_erestartsys = false; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 923 | int i, ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 924 | |
Christian König | 3ddf4ad | 2016-06-15 13:44:03 +0200 | [diff] [blame] | 925 | ret = reservation_object_reserve_shared(bo->resv); |
| 926 | if (unlikely(ret)) |
| 927 | return ret; |
| 928 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 929 | mem->mm_node = NULL; |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 930 | for (i = 0; i < placement->num_placement; ++i) { |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 931 | const struct ttm_place *place = &placement->placement[i]; |
| 932 | |
| 933 | ret = ttm_mem_type_from_place(place, &mem_type); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 934 | if (ret) |
| 935 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 936 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame] | 937 | if (!man->has_type || !man->use_type) |
| 938 | continue; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 939 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 940 | type_ok = ttm_bo_mt_compatible(man, mem_type, place, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 941 | &cur_flags); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 942 | |
| 943 | if (!type_ok) |
| 944 | continue; |
| 945 | |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame] | 946 | type_found = true; |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 947 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
| 948 | cur_flags); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 949 | /* |
| 950 | * Use the access and other non-mapping-related flag bits from |
| 951 | * the memory placement flags to the current flags |
| 952 | */ |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 953 | ttm_flag_masked(&cur_flags, place->flags, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 954 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 955 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 956 | if (mem_type == TTM_PL_SYSTEM) |
| 957 | break; |
| 958 | |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame] | 959 | ret = (*man->func->get_node)(man, bo, place, mem); |
| 960 | if (unlikely(ret)) |
| 961 | return ret; |
Christian König | 3ddf4ad | 2016-06-15 13:44:03 +0200 | [diff] [blame] | 962 | |
| 963 | if (mem->mm_node) { |
| 964 | ret = ttm_bo_add_move_fence(bo, man, mem); |
| 965 | if (unlikely(ret)) { |
| 966 | (*man->func->put_node)(man, mem); |
| 967 | return ret; |
| 968 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 969 | break; |
Christian König | 3ddf4ad | 2016-06-15 13:44:03 +0200 | [diff] [blame] | 970 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 971 | } |
| 972 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 973 | if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 974 | mem->mem_type = mem_type; |
| 975 | mem->placement = cur_flags; |
| 976 | return 0; |
| 977 | } |
| 978 | |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 979 | for (i = 0; i < placement->num_busy_placement; ++i) { |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 980 | const struct ttm_place *place = &placement->busy_placement[i]; |
| 981 | |
| 982 | ret = ttm_mem_type_from_place(place, &mem_type); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 983 | if (ret) |
| 984 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 985 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame] | 986 | if (!man->has_type || !man->use_type) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 987 | continue; |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 988 | if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 989 | continue; |
| 990 | |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame] | 991 | type_found = true; |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 992 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
| 993 | cur_flags); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 994 | /* |
| 995 | * Use the access and other non-mapping-related flag bits from |
| 996 | * the memory placement flags to the current flags |
| 997 | */ |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 998 | ttm_flag_masked(&cur_flags, place->flags, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 999 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 1000 | |
Thomas Hellstrom | 0eaddb2 | 2010-01-16 16:05:04 +0100 | [diff] [blame] | 1001 | if (mem_type == TTM_PL_SYSTEM) { |
| 1002 | mem->mem_type = mem_type; |
| 1003 | mem->placement = cur_flags; |
| 1004 | mem->mm_node = NULL; |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 1008 | ret = ttm_bo_mem_force_space(bo, mem_type, place, mem, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1009 | interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1010 | if (ret == 0 && mem->mm_node) { |
| 1011 | mem->placement = cur_flags; |
| 1012 | return 0; |
| 1013 | } |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 1014 | if (ret == -ERESTARTSYS) |
| 1015 | has_erestartsys = true; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1016 | } |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame] | 1017 | |
| 1018 | if (!type_found) { |
Joe Perches | 8dfe162 | 2017-02-28 04:55:54 -0800 | [diff] [blame] | 1019 | pr_err(TTM_PFX "No compatible memory type found\n"); |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame] | 1020 | return -EINVAL; |
| 1021 | } |
| 1022 | |
| 1023 | return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1024 | } |
| 1025 | EXPORT_SYMBOL(ttm_bo_mem_space); |
| 1026 | |
Rashika Kheria | 6e87fa4 | 2014-01-06 22:12:58 +0530 | [diff] [blame] | 1027 | static int ttm_bo_move_buffer(struct ttm_buffer_object *bo, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1028 | struct ttm_placement *placement, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1029 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 1030 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1031 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1032 | int ret = 0; |
| 1033 | struct ttm_mem_reg mem; |
| 1034 | |
Maarten Lankhorst | 009a9da | 2013-06-27 13:48:25 +0200 | [diff] [blame] | 1035 | lockdep_assert_held(&bo->resv->lock.base); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1036 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1037 | mem.num_pages = bo->num_pages; |
| 1038 | mem.size = mem.num_pages << PAGE_SHIFT; |
| 1039 | mem.page_alignment = bo->mem.page_alignment; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1040 | mem.bus.io_reserved_vm = false; |
| 1041 | mem.bus.io_reserved_count = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1042 | /* |
| 1043 | * Determine where to move the buffer. |
| 1044 | */ |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1045 | ret = ttm_bo_mem_space(bo, placement, &mem, |
| 1046 | interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1047 | if (ret) |
| 1048 | goto out_unlock; |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1049 | ret = ttm_bo_handle_move_mem(bo, &mem, false, |
| 1050 | interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1051 | out_unlock: |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1052 | if (ret && mem.mm_node) |
| 1053 | ttm_bo_mem_put(bo, &mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1054 | return ret; |
| 1055 | } |
| 1056 | |
Christian König | 018b7fc | 2017-03-29 11:47:04 +0200 | [diff] [blame] | 1057 | static bool ttm_bo_places_compat(const struct ttm_place *places, |
| 1058 | unsigned num_placement, |
| 1059 | struct ttm_mem_reg *mem, |
| 1060 | uint32_t *new_flags) |
| 1061 | { |
| 1062 | unsigned i; |
| 1063 | |
| 1064 | for (i = 0; i < num_placement; i++) { |
| 1065 | const struct ttm_place *heap = &places[i]; |
| 1066 | |
| 1067 | if (mem->mm_node && (mem->start < heap->fpfn || |
| 1068 | (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn))) |
| 1069 | continue; |
| 1070 | |
| 1071 | *new_flags = heap->flags; |
| 1072 | if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && |
Christian König | c8b26bd1 | 2017-03-29 12:13:54 +0200 | [diff] [blame] | 1073 | (*new_flags & mem->placement & TTM_PL_MASK_MEM) && |
| 1074 | (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) || |
| 1075 | (mem->placement & TTM_PL_FLAG_CONTIGUOUS))) |
Christian König | 018b7fc | 2017-03-29 11:47:04 +0200 | [diff] [blame] | 1076 | return true; |
| 1077 | } |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
Sinclair Yeh | 94477bf | 2016-06-29 12:58:49 -0700 | [diff] [blame] | 1081 | bool ttm_bo_mem_compat(struct ttm_placement *placement, |
| 1082 | struct ttm_mem_reg *mem, |
| 1083 | uint32_t *new_flags) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1084 | { |
Christian König | 018b7fc | 2017-03-29 11:47:04 +0200 | [diff] [blame] | 1085 | if (ttm_bo_places_compat(placement->placement, placement->num_placement, |
| 1086 | mem, new_flags)) |
| 1087 | return true; |
Thomas Hellstrom | e22238e | 2010-02-12 00:18:00 +0100 | [diff] [blame] | 1088 | |
Christian König | 018b7fc | 2017-03-29 11:47:04 +0200 | [diff] [blame] | 1089 | if ((placement->busy_placement != placement->placement || |
| 1090 | placement->num_busy_placement > placement->num_placement) && |
| 1091 | ttm_bo_places_compat(placement->busy_placement, |
| 1092 | placement->num_busy_placement, |
| 1093 | mem, new_flags)) |
| 1094 | return true; |
Thomas Hellstrom | 59c8e66 | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1095 | |
| 1096 | return false; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1097 | } |
Sinclair Yeh | 94477bf | 2016-06-29 12:58:49 -0700 | [diff] [blame] | 1098 | EXPORT_SYMBOL(ttm_bo_mem_compat); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1099 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1100 | int ttm_bo_validate(struct ttm_buffer_object *bo, |
| 1101 | struct ttm_placement *placement, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1102 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 1103 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1104 | { |
| 1105 | int ret; |
Thomas Hellstrom | 59c8e66 | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1106 | uint32_t new_flags; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1107 | |
Maarten Lankhorst | 009a9da | 2013-06-27 13:48:25 +0200 | [diff] [blame] | 1108 | lockdep_assert_held(&bo->resv->lock.base); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1109 | /* |
| 1110 | * Check whether we need to move buffer. |
| 1111 | */ |
Thomas Hellstrom | 59c8e66 | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1112 | if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) { |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1113 | ret = ttm_bo_move_buffer(bo, placement, interruptible, |
| 1114 | no_wait_gpu); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1115 | if (ret) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1116 | return ret; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1117 | } else { |
| 1118 | /* |
| 1119 | * Use the access and other non-mapping-related flag bits from |
| 1120 | * the compatible memory placement flags to the active flags |
| 1121 | */ |
Thomas Hellstrom | 59c8e66 | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1122 | ttm_flag_masked(&bo->mem.placement, new_flags, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1123 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1124 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1125 | /* |
| 1126 | * We might need to add a TTM. |
| 1127 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1128 | if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { |
| 1129 | ret = ttm_bo_add_ttm(bo, true); |
| 1130 | if (ret) |
| 1131 | return ret; |
| 1132 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1133 | return 0; |
| 1134 | } |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1135 | EXPORT_SYMBOL(ttm_bo_validate); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1136 | |
Nicolai Hähnle | ca9cf68d | 2017-02-16 10:56:40 +0100 | [diff] [blame] | 1137 | int ttm_bo_init_reserved(struct ttm_bo_device *bdev, |
| 1138 | struct ttm_buffer_object *bo, |
| 1139 | unsigned long size, |
| 1140 | enum ttm_bo_type type, |
| 1141 | struct ttm_placement *placement, |
| 1142 | uint32_t page_alignment, |
| 1143 | bool interruptible, |
| 1144 | struct file *persistent_swap_storage, |
| 1145 | size_t acc_size, |
| 1146 | struct sg_table *sg, |
| 1147 | struct reservation_object *resv, |
| 1148 | void (*destroy) (struct ttm_buffer_object *)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1149 | { |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1150 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1151 | unsigned long num_pages; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1152 | struct ttm_mem_global *mem_glob = bdev->glob->mem_glob; |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame] | 1153 | bool locked; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1154 | |
| 1155 | ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false); |
| 1156 | if (ret) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1157 | pr_err("Out of kernel memory\n"); |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1158 | if (destroy) |
| 1159 | (*destroy)(bo); |
| 1160 | else |
| 1161 | kfree(bo); |
| 1162 | return -ENOMEM; |
| 1163 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1164 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1165 | num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1166 | if (num_pages == 0) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1167 | pr_err("Illegal buffer object size\n"); |
Thomas Hellstrom | 7dfbbdc | 2010-11-09 21:31:44 +0100 | [diff] [blame] | 1168 | if (destroy) |
| 1169 | (*destroy)(bo); |
| 1170 | else |
| 1171 | kfree(bo); |
Thomas Hellstrom | a393c73 | 2012-06-12 13:28:42 +0200 | [diff] [blame] | 1172 | ttm_mem_global_free(mem_glob, acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1173 | return -EINVAL; |
| 1174 | } |
| 1175 | bo->destroy = destroy; |
| 1176 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1177 | kref_init(&bo->kref); |
| 1178 | kref_init(&bo->list_kref); |
| 1179 | atomic_set(&bo->cpu_writers, 0); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1180 | INIT_LIST_HEAD(&bo->lru); |
| 1181 | INIT_LIST_HEAD(&bo->ddestroy); |
| 1182 | INIT_LIST_HEAD(&bo->swap); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1183 | INIT_LIST_HEAD(&bo->io_reserve_lru); |
Thomas Hellstrom | c58f009 | 2013-11-14 10:49:05 -0800 | [diff] [blame] | 1184 | mutex_init(&bo->wu_mutex); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1185 | bo->bdev = bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1186 | bo->glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1187 | bo->type = type; |
| 1188 | bo->num_pages = num_pages; |
Jerome Glisse | eb6d2c3 | 2009-12-10 16:15:52 +0100 | [diff] [blame] | 1189 | bo->mem.size = num_pages << PAGE_SHIFT; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1190 | bo->mem.mem_type = TTM_PL_SYSTEM; |
| 1191 | bo->mem.num_pages = bo->num_pages; |
| 1192 | bo->mem.mm_node = NULL; |
| 1193 | bo->mem.page_alignment = page_alignment; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1194 | bo->mem.bus.io_reserved_vm = false; |
| 1195 | bo->mem.bus.io_reserved_count = 0; |
Christian König | 5bc7306 | 2016-06-15 13:44:01 +0200 | [diff] [blame] | 1196 | bo->moving = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1197 | bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED); |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1198 | bo->persistent_swap_storage = persistent_swap_storage; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1199 | bo->acc_size = acc_size; |
Dave Airlie | 129b78b | 2012-04-02 11:46:06 +0100 | [diff] [blame] | 1200 | bo->sg = sg; |
Maarten Lankhorst | f4f4e3e | 2014-01-09 11:03:15 +0100 | [diff] [blame] | 1201 | if (resv) { |
| 1202 | bo->resv = resv; |
| 1203 | lockdep_assert_held(&bo->resv->lock.base); |
| 1204 | } else { |
| 1205 | bo->resv = &bo->ttm_resv; |
| 1206 | reservation_object_init(&bo->ttm_resv); |
| 1207 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1208 | atomic_inc(&bo->glob->bo_count); |
David Herrmann | 72525b3 | 2013-07-24 21:08:53 +0200 | [diff] [blame] | 1209 | drm_vma_node_reset(&bo->vma_node); |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 1210 | bo->priority = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1211 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1212 | /* |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1213 | * For ttm_bo_type_device buffers, allocate |
| 1214 | * address space from the device. |
| 1215 | */ |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 1216 | if (bo->type == ttm_bo_type_device || |
| 1217 | bo->type == ttm_bo_type_sg) |
David Herrmann | abf1903 | 2013-07-25 14:08:51 +0200 | [diff] [blame] | 1218 | ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node, |
| 1219 | bo->mem.num_pages); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1220 | |
Maarten Lankhorst | f4f4e3e | 2014-01-09 11:03:15 +0100 | [diff] [blame] | 1221 | /* passed reservation objects should already be locked, |
| 1222 | * since otherwise lockdep will be angered in radeon. |
| 1223 | */ |
| 1224 | if (!resv) { |
| 1225 | locked = ww_mutex_trylock(&bo->resv->lock); |
| 1226 | WARN_ON(!locked); |
| 1227 | } |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame] | 1228 | |
| 1229 | if (likely(!ret)) |
| 1230 | ret = ttm_bo_validate(bo, placement, interruptible, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1231 | |
Nicolai Hähnle | c2c139c | 2017-02-14 09:37:12 +0100 | [diff] [blame] | 1232 | if (unlikely(ret)) { |
Nicolai Hähnle | ca9cf68d | 2017-02-16 10:56:40 +0100 | [diff] [blame] | 1233 | if (!resv) |
| 1234 | ttm_bo_unreserve(bo); |
| 1235 | |
Nicolai Hähnle | c2c139c | 2017-02-14 09:37:12 +0100 | [diff] [blame] | 1236 | ttm_bo_unref(&bo); |
| 1237 | return ret; |
| 1238 | } |
| 1239 | |
| 1240 | if (resv && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) { |
Christian König | 33d48cf | 2016-01-11 15:35:18 +0100 | [diff] [blame] | 1241 | spin_lock(&bo->glob->lru_lock); |
| 1242 | ttm_bo_add_to_lru(bo); |
| 1243 | spin_unlock(&bo->glob->lru_lock); |
| 1244 | } |
| 1245 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1246 | return ret; |
| 1247 | } |
Nicolai Hähnle | ca9cf68d | 2017-02-16 10:56:40 +0100 | [diff] [blame] | 1248 | EXPORT_SYMBOL(ttm_bo_init_reserved); |
| 1249 | |
| 1250 | int ttm_bo_init(struct ttm_bo_device *bdev, |
| 1251 | struct ttm_buffer_object *bo, |
| 1252 | unsigned long size, |
| 1253 | enum ttm_bo_type type, |
| 1254 | struct ttm_placement *placement, |
| 1255 | uint32_t page_alignment, |
| 1256 | bool interruptible, |
| 1257 | struct file *persistent_swap_storage, |
| 1258 | size_t acc_size, |
| 1259 | struct sg_table *sg, |
| 1260 | struct reservation_object *resv, |
| 1261 | void (*destroy) (struct ttm_buffer_object *)) |
| 1262 | { |
| 1263 | int ret; |
| 1264 | |
| 1265 | ret = ttm_bo_init_reserved(bdev, bo, size, type, placement, |
| 1266 | page_alignment, interruptible, |
| 1267 | persistent_swap_storage, acc_size, |
| 1268 | sg, resv, destroy); |
| 1269 | if (ret) |
| 1270 | return ret; |
| 1271 | |
| 1272 | if (!resv) |
| 1273 | ttm_bo_unreserve(bo); |
| 1274 | |
| 1275 | return 0; |
| 1276 | } |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1277 | EXPORT_SYMBOL(ttm_bo_init); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1278 | |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1279 | size_t ttm_bo_acc_size(struct ttm_bo_device *bdev, |
| 1280 | unsigned long bo_size, |
| 1281 | unsigned struct_size) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1282 | { |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1283 | unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; |
| 1284 | size_t size = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1285 | |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1286 | size += ttm_round_pot(struct_size); |
Felix Kuehling | 8562163 | 2016-04-07 21:42:17 -0400 | [diff] [blame] | 1287 | size += ttm_round_pot(npages * sizeof(void *)); |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1288 | size += ttm_round_pot(sizeof(struct ttm_tt)); |
| 1289 | return size; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1290 | } |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1291 | EXPORT_SYMBOL(ttm_bo_acc_size); |
| 1292 | |
| 1293 | size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev, |
| 1294 | unsigned long bo_size, |
| 1295 | unsigned struct_size) |
| 1296 | { |
| 1297 | unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; |
| 1298 | size_t size = 0; |
| 1299 | |
| 1300 | size += ttm_round_pot(struct_size); |
Felix Kuehling | 8562163 | 2016-04-07 21:42:17 -0400 | [diff] [blame] | 1301 | size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t))); |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1302 | size += ttm_round_pot(sizeof(struct ttm_dma_tt)); |
| 1303 | return size; |
| 1304 | } |
| 1305 | EXPORT_SYMBOL(ttm_bo_dma_acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1306 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1307 | int ttm_bo_create(struct ttm_bo_device *bdev, |
| 1308 | unsigned long size, |
| 1309 | enum ttm_bo_type type, |
| 1310 | struct ttm_placement *placement, |
| 1311 | uint32_t page_alignment, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1312 | bool interruptible, |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1313 | struct file *persistent_swap_storage, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1314 | struct ttm_buffer_object **p_bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1315 | { |
| 1316 | struct ttm_buffer_object *bo; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1317 | size_t acc_size; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1318 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1319 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1320 | bo = kzalloc(sizeof(*bo), GFP_KERNEL); |
Thomas Hellstrom | a393c73 | 2012-06-12 13:28:42 +0200 | [diff] [blame] | 1321 | if (unlikely(bo == NULL)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1322 | return -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1323 | |
Thomas Hellstrom | a393c73 | 2012-06-12 13:28:42 +0200 | [diff] [blame] | 1324 | acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object)); |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1325 | ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment, |
Marcin Slusarz | 0b91c4a | 2012-11-06 21:49:51 +0000 | [diff] [blame] | 1326 | interruptible, persistent_swap_storage, acc_size, |
Maarten Lankhorst | f4f4e3e | 2014-01-09 11:03:15 +0100 | [diff] [blame] | 1327 | NULL, NULL, NULL); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1328 | if (likely(ret == 0)) |
| 1329 | *p_bo = bo; |
| 1330 | |
| 1331 | return ret; |
| 1332 | } |
Thomas Hellstrom | 4d79893 | 2011-10-04 20:13:11 +0200 | [diff] [blame] | 1333 | EXPORT_SYMBOL(ttm_bo_create); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1334 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1335 | static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev, |
Christian König | 2ee7fc9 | 2017-01-06 19:16:07 +0100 | [diff] [blame] | 1336 | unsigned mem_type) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1337 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1338 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1339 | struct ttm_bo_global *glob = bdev->glob; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1340 | struct dma_fence *fence; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1341 | int ret; |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 1342 | unsigned i; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1343 | |
| 1344 | /* |
| 1345 | * Can't use standard list traversal since we're unlocking. |
| 1346 | */ |
| 1347 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1348 | spin_lock(&glob->lru_lock); |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 1349 | for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { |
| 1350 | while (!list_empty(&man->lru[i])) { |
| 1351 | spin_unlock(&glob->lru_lock); |
| 1352 | ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false); |
| 1353 | if (ret) |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1354 | return ret; |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 1355 | spin_lock(&glob->lru_lock); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1356 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1357 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1358 | spin_unlock(&glob->lru_lock); |
Christian König | aff98ba | 2016-06-22 14:16:28 +0200 | [diff] [blame] | 1359 | |
| 1360 | spin_lock(&man->move_lock); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1361 | fence = dma_fence_get(man->move); |
Christian König | aff98ba | 2016-06-22 14:16:28 +0200 | [diff] [blame] | 1362 | spin_unlock(&man->move_lock); |
| 1363 | |
| 1364 | if (fence) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1365 | ret = dma_fence_wait(fence, false); |
| 1366 | dma_fence_put(fence); |
Christian König | 2ee7fc9 | 2017-01-06 19:16:07 +0100 | [diff] [blame] | 1367 | if (ret) |
| 1368 | return ret; |
Christian König | aff98ba | 2016-06-22 14:16:28 +0200 | [diff] [blame] | 1369 | } |
| 1370 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1371 | return 0; |
| 1372 | } |
| 1373 | |
| 1374 | int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type) |
| 1375 | { |
Roel Kluin | c96e7c7 | 2009-08-03 14:22:53 +0200 | [diff] [blame] | 1376 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1377 | int ret = -EINVAL; |
| 1378 | |
| 1379 | if (mem_type >= TTM_NUM_MEM_TYPES) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1380 | pr_err("Illegal memory type %d\n", mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1381 | return ret; |
| 1382 | } |
Roel Kluin | c96e7c7 | 2009-08-03 14:22:53 +0200 | [diff] [blame] | 1383 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1384 | |
| 1385 | if (!man->has_type) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1386 | pr_err("Trying to take down uninitialized memory manager type %u\n", |
| 1387 | mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1388 | return ret; |
| 1389 | } |
| 1390 | |
| 1391 | man->use_type = false; |
| 1392 | man->has_type = false; |
| 1393 | |
| 1394 | ret = 0; |
| 1395 | if (mem_type > 0) { |
Christian König | 2ee7fc9 | 2017-01-06 19:16:07 +0100 | [diff] [blame] | 1396 | ret = ttm_bo_force_list_clean(bdev, mem_type); |
| 1397 | if (ret) { |
| 1398 | pr_err("Cleanup eviction failed\n"); |
| 1399 | return ret; |
| 1400 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1401 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1402 | ret = (*man->func->takedown)(man); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1403 | } |
| 1404 | |
John Brooks | 8046e19 | 2017-07-03 14:05:34 -0400 | [diff] [blame] | 1405 | dma_fence_put(man->move); |
| 1406 | man->move = NULL; |
| 1407 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1408 | return ret; |
| 1409 | } |
| 1410 | EXPORT_SYMBOL(ttm_bo_clean_mm); |
| 1411 | |
| 1412 | int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type) |
| 1413 | { |
| 1414 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
| 1415 | |
| 1416 | if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1417 | pr_err("Illegal memory manager memory type %u\n", mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1418 | return -EINVAL; |
| 1419 | } |
| 1420 | |
| 1421 | if (!man->has_type) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1422 | pr_err("Memory type %u has not been initialized\n", mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
| 1425 | |
Christian König | 2ee7fc9 | 2017-01-06 19:16:07 +0100 | [diff] [blame] | 1426 | return ttm_bo_force_list_clean(bdev, mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1427 | } |
| 1428 | EXPORT_SYMBOL(ttm_bo_evict_mm); |
| 1429 | |
| 1430 | int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1431 | unsigned long p_size) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1432 | { |
Huang Rui | aef1ba5 | 2017-04-17 15:32:52 +0800 | [diff] [blame] | 1433 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1434 | struct ttm_mem_type_manager *man; |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 1435 | unsigned i; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1436 | |
Thomas Hellstrom | dbc4a5b | 2010-10-29 10:46:47 +0200 | [diff] [blame] | 1437 | BUG_ON(type >= TTM_NUM_MEM_TYPES); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1438 | man = &bdev->man[type]; |
Thomas Hellstrom | dbc4a5b | 2010-10-29 10:46:47 +0200 | [diff] [blame] | 1439 | BUG_ON(man->has_type); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1440 | man->io_reserve_fastpath = true; |
| 1441 | man->use_io_reserve_lru = false; |
| 1442 | mutex_init(&man->io_reserve_mutex); |
Christian König | 3ddf4ad | 2016-06-15 13:44:03 +0200 | [diff] [blame] | 1443 | spin_lock_init(&man->move_lock); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1444 | INIT_LIST_HEAD(&man->io_reserve_lru); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1445 | |
| 1446 | ret = bdev->driver->init_mem_type(bdev, type, man); |
| 1447 | if (ret) |
| 1448 | return ret; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1449 | man->bdev = bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1450 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1451 | if (type != TTM_PL_SYSTEM) { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1452 | ret = (*man->func->init)(man, p_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1453 | if (ret) |
| 1454 | return ret; |
| 1455 | } |
| 1456 | man->has_type = true; |
| 1457 | man->use_type = true; |
| 1458 | man->size = p_size; |
| 1459 | |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 1460 | for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) |
| 1461 | INIT_LIST_HEAD(&man->lru[i]); |
Christian König | 3ddf4ad | 2016-06-15 13:44:03 +0200 | [diff] [blame] | 1462 | man->move = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1463 | |
| 1464 | return 0; |
| 1465 | } |
| 1466 | EXPORT_SYMBOL(ttm_bo_init_mm); |
| 1467 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1468 | static void ttm_bo_global_kobj_release(struct kobject *kobj) |
| 1469 | { |
| 1470 | struct ttm_bo_global *glob = |
| 1471 | container_of(kobj, struct ttm_bo_global, kobj); |
| 1472 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1473 | ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink); |
| 1474 | __free_page(glob->dummy_read_page); |
| 1475 | kfree(glob); |
| 1476 | } |
| 1477 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 1478 | void ttm_bo_global_release(struct drm_global_reference *ref) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1479 | { |
| 1480 | struct ttm_bo_global *glob = ref->object; |
| 1481 | |
| 1482 | kobject_del(&glob->kobj); |
| 1483 | kobject_put(&glob->kobj); |
| 1484 | } |
| 1485 | EXPORT_SYMBOL(ttm_bo_global_release); |
| 1486 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 1487 | int ttm_bo_global_init(struct drm_global_reference *ref) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1488 | { |
| 1489 | struct ttm_bo_global_ref *bo_ref = |
| 1490 | container_of(ref, struct ttm_bo_global_ref, ref); |
| 1491 | struct ttm_bo_global *glob = ref->object; |
| 1492 | int ret; |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 1493 | unsigned i; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1494 | |
| 1495 | mutex_init(&glob->device_list_mutex); |
| 1496 | spin_lock_init(&glob->lru_lock); |
| 1497 | glob->mem_glob = bo_ref->mem_glob; |
| 1498 | glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32); |
| 1499 | |
| 1500 | if (unlikely(glob->dummy_read_page == NULL)) { |
| 1501 | ret = -ENOMEM; |
| 1502 | goto out_no_drp; |
| 1503 | } |
| 1504 | |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 1505 | for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) |
| 1506 | INIT_LIST_HEAD(&glob->swap_lru[i]); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1507 | INIT_LIST_HEAD(&glob->device_list); |
| 1508 | |
| 1509 | ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout); |
| 1510 | ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink); |
| 1511 | if (unlikely(ret != 0)) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1512 | pr_err("Could not register buffer object swapout\n"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1513 | goto out_no_shrink; |
| 1514 | } |
| 1515 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1516 | atomic_set(&glob->bo_count, 0); |
| 1517 | |
Robert P. J. Day | b642ed0 | 2010-03-13 10:36:32 +0000 | [diff] [blame] | 1518 | ret = kobject_init_and_add( |
| 1519 | &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1520 | if (unlikely(ret != 0)) |
| 1521 | kobject_put(&glob->kobj); |
| 1522 | return ret; |
| 1523 | out_no_shrink: |
| 1524 | __free_page(glob->dummy_read_page); |
| 1525 | out_no_drp: |
| 1526 | kfree(glob); |
| 1527 | return ret; |
| 1528 | } |
| 1529 | EXPORT_SYMBOL(ttm_bo_global_init); |
| 1530 | |
| 1531 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1532 | int ttm_bo_device_release(struct ttm_bo_device *bdev) |
| 1533 | { |
| 1534 | int ret = 0; |
| 1535 | unsigned i = TTM_NUM_MEM_TYPES; |
| 1536 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1537 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1538 | |
| 1539 | while (i--) { |
| 1540 | man = &bdev->man[i]; |
| 1541 | if (man->has_type) { |
| 1542 | man->use_type = false; |
| 1543 | if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) { |
| 1544 | ret = -EBUSY; |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1545 | pr_err("DRM memory manager type %d is not clean\n", |
| 1546 | i); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1547 | } |
| 1548 | man->has_type = false; |
| 1549 | } |
| 1550 | } |
| 1551 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1552 | mutex_lock(&glob->device_list_mutex); |
| 1553 | list_del(&bdev->device_list); |
| 1554 | mutex_unlock(&glob->device_list_mutex); |
| 1555 | |
Tejun Heo | f094cfc | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 1556 | cancel_delayed_work_sync(&bdev->wq); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1557 | |
| 1558 | while (ttm_bo_delayed_delete(bdev, true)) |
| 1559 | ; |
| 1560 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1561 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1562 | if (list_empty(&bdev->ddestroy)) |
| 1563 | TTM_DEBUG("Delayed destroy list was clean\n"); |
| 1564 | |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 1565 | for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) |
| 1566 | if (list_empty(&bdev->man[0].lru[0])) |
| 1567 | TTM_DEBUG("Swap list %d was clean\n", i); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1568 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1569 | |
David Herrmann | 72525b3 | 2013-07-24 21:08:53 +0200 | [diff] [blame] | 1570 | drm_vma_offset_manager_destroy(&bdev->vma_manager); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1571 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1572 | return ret; |
| 1573 | } |
| 1574 | EXPORT_SYMBOL(ttm_bo_device_release); |
| 1575 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1576 | int ttm_bo_device_init(struct ttm_bo_device *bdev, |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1577 | struct ttm_bo_global *glob, |
| 1578 | struct ttm_bo_driver *driver, |
David Herrmann | 44d847b | 2013-08-13 19:10:30 +0200 | [diff] [blame] | 1579 | struct address_space *mapping, |
Dave Airlie | 51c8b40 | 2009-08-20 13:38:04 +1000 | [diff] [blame] | 1580 | uint64_t file_page_offset, |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 1581 | bool need_dma32) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1582 | { |
| 1583 | int ret = -EINVAL; |
| 1584 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1585 | bdev->driver = driver; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1586 | |
| 1587 | memset(bdev->man, 0, sizeof(bdev->man)); |
| 1588 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1589 | /* |
| 1590 | * Initialize the system memory buffer type. |
| 1591 | * Other types need to be driver / IOCTL initialized. |
| 1592 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1593 | ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1594 | if (unlikely(ret != 0)) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1595 | goto out_no_sys; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1596 | |
David Herrmann | 72525b3 | 2013-07-24 21:08:53 +0200 | [diff] [blame] | 1597 | drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset, |
| 1598 | 0x10000000); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1599 | INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1600 | INIT_LIST_HEAD(&bdev->ddestroy); |
David Herrmann | 44d847b | 2013-08-13 19:10:30 +0200 | [diff] [blame] | 1601 | bdev->dev_mapping = mapping; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1602 | bdev->glob = glob; |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 1603 | bdev->need_dma32 = need_dma32; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1604 | mutex_lock(&glob->device_list_mutex); |
| 1605 | list_add_tail(&bdev->device_list, &glob->device_list); |
| 1606 | mutex_unlock(&glob->device_list_mutex); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1607 | |
| 1608 | return 0; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1609 | out_no_sys: |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1610 | return ret; |
| 1611 | } |
| 1612 | EXPORT_SYMBOL(ttm_bo_device_init); |
| 1613 | |
| 1614 | /* |
| 1615 | * buffer object vm functions. |
| 1616 | */ |
| 1617 | |
| 1618 | bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 1619 | { |
| 1620 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
| 1621 | |
| 1622 | if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) { |
| 1623 | if (mem->mem_type == TTM_PL_SYSTEM) |
| 1624 | return false; |
| 1625 | |
| 1626 | if (man->flags & TTM_MEMTYPE_FLAG_CMA) |
| 1627 | return false; |
| 1628 | |
| 1629 | if (mem->placement & TTM_PL_FLAG_CACHED) |
| 1630 | return false; |
| 1631 | } |
| 1632 | return true; |
| 1633 | } |
| 1634 | |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1635 | void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1636 | { |
| 1637 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1638 | |
David Herrmann | 51335df | 2013-07-24 21:10:03 +0200 | [diff] [blame] | 1639 | drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1640 | ttm_mem_io_free_vm(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1641 | } |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1642 | |
| 1643 | void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) |
| 1644 | { |
| 1645 | struct ttm_bo_device *bdev = bo->bdev; |
| 1646 | struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; |
| 1647 | |
| 1648 | ttm_mem_io_lock(man, false); |
| 1649 | ttm_bo_unmap_virtual_locked(bo); |
| 1650 | ttm_mem_io_unlock(man); |
| 1651 | } |
| 1652 | |
| 1653 | |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 1654 | EXPORT_SYMBOL(ttm_bo_unmap_virtual); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1655 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1656 | int ttm_bo_wait(struct ttm_buffer_object *bo, |
Christian König | 8aa6d4f | 2016-04-06 11:12:04 +0200 | [diff] [blame] | 1657 | bool interruptible, bool no_wait) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1658 | { |
Christian König | 98a6dd9 | 2016-11-07 16:16:15 -0500 | [diff] [blame] | 1659 | long timeout = 15 * HZ; |
| 1660 | |
| 1661 | if (no_wait) { |
| 1662 | if (reservation_object_test_signaled_rcu(bo->resv, true)) |
| 1663 | return 0; |
| 1664 | else |
| 1665 | return -EBUSY; |
| 1666 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1667 | |
Christian König | f849c6d | 2016-06-15 13:44:02 +0200 | [diff] [blame] | 1668 | timeout = reservation_object_wait_timeout_rcu(bo->resv, true, |
| 1669 | interruptible, timeout); |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 1670 | if (timeout < 0) |
| 1671 | return timeout; |
| 1672 | |
| 1673 | if (timeout == 0) |
| 1674 | return -EBUSY; |
| 1675 | |
Christian König | f849c6d | 2016-06-15 13:44:02 +0200 | [diff] [blame] | 1676 | reservation_object_add_excl_fence(bo->resv, NULL); |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 1677 | return 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1678 | } |
| 1679 | EXPORT_SYMBOL(ttm_bo_wait); |
| 1680 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1681 | int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait) |
| 1682 | { |
| 1683 | int ret = 0; |
| 1684 | |
| 1685 | /* |
Thomas Hellstrom | 8cfe92d | 2010-04-28 11:33:25 +0200 | [diff] [blame] | 1686 | * Using ttm_bo_reserve makes sure the lru lists are updated. |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1687 | */ |
| 1688 | |
Christian König | dfd5e50 | 2016-04-06 11:12:03 +0200 | [diff] [blame] | 1689 | ret = ttm_bo_reserve(bo, true, no_wait, NULL); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1690 | if (unlikely(ret != 0)) |
| 1691 | return ret; |
Christian König | 8aa6d4f | 2016-04-06 11:12:04 +0200 | [diff] [blame] | 1692 | ret = ttm_bo_wait(bo, true, no_wait); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1693 | if (likely(ret == 0)) |
| 1694 | atomic_inc(&bo->cpu_writers); |
| 1695 | ttm_bo_unreserve(bo); |
| 1696 | return ret; |
| 1697 | } |
Ben Skeggs | d1ede14 | 2009-12-11 15:13:00 +1000 | [diff] [blame] | 1698 | EXPORT_SYMBOL(ttm_bo_synccpu_write_grab); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1699 | |
| 1700 | void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo) |
| 1701 | { |
Maarten Lankhorst | 654aa792 | 2012-11-06 14:39:43 +0100 | [diff] [blame] | 1702 | atomic_dec(&bo->cpu_writers); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1703 | } |
Ben Skeggs | d1ede14 | 2009-12-11 15:13:00 +1000 | [diff] [blame] | 1704 | EXPORT_SYMBOL(ttm_bo_synccpu_write_release); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1705 | |
| 1706 | /** |
| 1707 | * A buffer object shrink method that tries to swap out the first |
| 1708 | * buffer object on the bo_global::swap_lru list. |
| 1709 | */ |
| 1710 | |
| 1711 | static int ttm_bo_swapout(struct ttm_mem_shrink *shrink) |
| 1712 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1713 | struct ttm_bo_global *glob = |
| 1714 | container_of(shrink, struct ttm_bo_global, shrink); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1715 | struct ttm_buffer_object *bo; |
| 1716 | int ret = -EBUSY; |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 1717 | unsigned i; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1718 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1719 | spin_lock(&glob->lru_lock); |
Christian König | cf6c467 | 2017-01-10 14:08:28 +0100 | [diff] [blame] | 1720 | for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { |
| 1721 | list_for_each_entry(bo, &glob->swap_lru[i], swap) { |
| 1722 | ret = __ttm_bo_reserve(bo, false, true, NULL); |
| 1723 | if (!ret) |
| 1724 | break; |
| 1725 | } |
Maarten Lankhorst | 2b7b3ad | 2012-11-28 11:25:42 +0000 | [diff] [blame] | 1726 | if (!ret) |
| 1727 | break; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1728 | } |
| 1729 | |
Maarten Lankhorst | 2b7b3ad | 2012-11-28 11:25:42 +0000 | [diff] [blame] | 1730 | if (ret) { |
| 1731 | spin_unlock(&glob->lru_lock); |
| 1732 | return ret; |
| 1733 | } |
| 1734 | |
| 1735 | kref_get(&bo->list_kref); |
| 1736 | |
| 1737 | if (!list_empty(&bo->ddestroy)) { |
| 1738 | ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false); |
| 1739 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 1740 | return ret; |
| 1741 | } |
| 1742 | |
Peter Zijlstra | bdfafc4 | 2016-11-14 17:34:19 +0100 | [diff] [blame] | 1743 | ttm_bo_del_from_lru(bo); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1744 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1745 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1746 | /** |
Christian König | 61ede07 | 2016-06-06 10:17:57 +0200 | [diff] [blame] | 1747 | * Move to system cached |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1748 | */ |
| 1749 | |
Michel Dänzer | 239ac65 | 2017-01-25 17:21:31 +0900 | [diff] [blame] | 1750 | if (bo->mem.mem_type != TTM_PL_SYSTEM || |
| 1751 | bo->ttm->caching_state != tt_cached) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1752 | struct ttm_mem_reg evict_mem; |
| 1753 | |
| 1754 | evict_mem = bo->mem; |
| 1755 | evict_mem.mm_node = NULL; |
| 1756 | evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED; |
| 1757 | evict_mem.mem_type = TTM_PL_SYSTEM; |
| 1758 | |
| 1759 | ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1760 | false, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1761 | if (unlikely(ret != 0)) |
| 1762 | goto out; |
| 1763 | } |
| 1764 | |
Christian König | 61ede07 | 2016-06-06 10:17:57 +0200 | [diff] [blame] | 1765 | /** |
| 1766 | * Make sure BO is idle. |
| 1767 | */ |
| 1768 | |
| 1769 | ret = ttm_bo_wait(bo, false, false); |
| 1770 | if (unlikely(ret != 0)) |
| 1771 | goto out; |
| 1772 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1773 | ttm_bo_unmap_virtual(bo); |
| 1774 | |
| 1775 | /** |
| 1776 | * Swap out. Buffer will be swapped in again as soon as |
| 1777 | * anyone tries to access a ttm page. |
| 1778 | */ |
| 1779 | |
Thomas Hellstrom | 3f09ea4 | 2010-01-13 22:28:40 +0100 | [diff] [blame] | 1780 | if (bo->bdev->driver->swap_notify) |
| 1781 | bo->bdev->driver->swap_notify(bo); |
| 1782 | |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1783 | ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1784 | out: |
| 1785 | |
| 1786 | /** |
| 1787 | * |
| 1788 | * Unreserve without putting on LRU to avoid swapping out an |
| 1789 | * already swapped buffer. |
| 1790 | */ |
| 1791 | |
Thomas Hellstrom | c752308 | 2014-02-20 11:36:25 +0100 | [diff] [blame] | 1792 | __ttm_bo_unreserve(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1793 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 1794 | return ret; |
| 1795 | } |
| 1796 | |
| 1797 | void ttm_bo_swapout_all(struct ttm_bo_device *bdev) |
| 1798 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1799 | while (ttm_bo_swapout(&bdev->glob->shrink) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1800 | ; |
| 1801 | } |
Thomas Hellstrom | e99e1e7 | 2010-01-13 22:28:42 +0100 | [diff] [blame] | 1802 | EXPORT_SYMBOL(ttm_bo_swapout_all); |
Thomas Hellstrom | c58f009 | 2013-11-14 10:49:05 -0800 | [diff] [blame] | 1803 | |
| 1804 | /** |
| 1805 | * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become |
| 1806 | * unreserved |
| 1807 | * |
| 1808 | * @bo: Pointer to buffer |
| 1809 | */ |
| 1810 | int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo) |
| 1811 | { |
| 1812 | int ret; |
| 1813 | |
| 1814 | /* |
| 1815 | * In the absense of a wait_unlocked API, |
| 1816 | * Use the bo::wu_mutex to avoid triggering livelocks due to |
| 1817 | * concurrent use of this function. Note that this use of |
| 1818 | * bo::wu_mutex can go away if we change locking order to |
| 1819 | * mmap_sem -> bo::reserve. |
| 1820 | */ |
| 1821 | ret = mutex_lock_interruptible(&bo->wu_mutex); |
| 1822 | if (unlikely(ret != 0)) |
| 1823 | return -ERESTARTSYS; |
| 1824 | if (!ww_mutex_is_locked(&bo->resv->lock)) |
| 1825 | goto out_unlock; |
Christian König | dfd5e50 | 2016-04-06 11:12:03 +0200 | [diff] [blame] | 1826 | ret = __ttm_bo_reserve(bo, true, false, NULL); |
Thomas Hellstrom | c58f009 | 2013-11-14 10:49:05 -0800 | [diff] [blame] | 1827 | if (unlikely(ret != 0)) |
| 1828 | goto out_unlock; |
Thomas Hellstrom | c752308 | 2014-02-20 11:36:25 +0100 | [diff] [blame] | 1829 | __ttm_bo_unreserve(bo); |
Thomas Hellstrom | c58f009 | 2013-11-14 10:49:05 -0800 | [diff] [blame] | 1830 | |
| 1831 | out_unlock: |
| 1832 | mutex_unlock(&bo->wu_mutex); |
| 1833 | return ret; |
| 1834 | } |