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