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