1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 2 3 /* 4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA 5 * Copyright 2020 Advanced Micro Devices, Inc. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 * OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * Authors: Christian König 26 */ 27 28 #define pr_fmt(fmt) "[TTM DEVICE] " fmt 29 30 #include <linux/debugfs.h> 31 #include <linux/mm.h> 32 33 #include <drm/ttm/ttm_bo.h> 34 #include <drm/ttm/ttm_device.h> 35 #include <drm/ttm/ttm_tt.h> 36 #include <drm/ttm/ttm_placement.h> 37 38 #include "ttm_module.h" 39 #include "ttm_bo_internal.h" 40 41 /* 42 * ttm_global_mutex - protecting the global state 43 */ 44 static DEFINE_MUTEX(ttm_global_mutex); 45 static unsigned ttm_glob_use_count; 46 struct ttm_global ttm_glob; 47 EXPORT_SYMBOL(ttm_glob); 48 49 struct dentry *ttm_debugfs_root; 50 51 static void ttm_global_release(void) 52 { 53 struct ttm_global *glob = &ttm_glob; 54 55 mutex_lock(&ttm_global_mutex); 56 if (--ttm_glob_use_count > 0) 57 goto out; 58 59 ttm_pool_mgr_fini(); 60 debugfs_remove(ttm_debugfs_root); 61 62 __free_page(glob->dummy_read_page); 63 memset(glob, 0, sizeof(*glob)); 64 out: 65 mutex_unlock(&ttm_global_mutex); 66 } 67 68 static int ttm_global_init(void) 69 { 70 struct ttm_global *glob = &ttm_glob; 71 unsigned long num_pages, num_dma32; 72 struct sysinfo si; 73 int ret = 0; 74 75 mutex_lock(&ttm_global_mutex); 76 if (++ttm_glob_use_count > 1) 77 goto out; 78 79 si_meminfo(&si); 80 81 ttm_debugfs_root = debugfs_create_dir("ttm", NULL); 82 if (IS_ERR(ttm_debugfs_root)) { 83 ttm_debugfs_root = NULL; 84 } 85 86 /* Limit the number of pages in the pool to about 50% of the total 87 * system memory. 88 */ 89 num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT; 90 num_pages /= 2; 91 92 /* But for DMA32 we limit ourself to only use 2GiB maximum. */ 93 num_dma32 = (u64)(si.totalram - si.totalhigh) * si.mem_unit 94 >> PAGE_SHIFT; 95 num_dma32 = min(num_dma32, 2UL << (30 - PAGE_SHIFT)); 96 97 ttm_pool_mgr_init(num_pages); 98 ttm_tt_mgr_init(num_pages, num_dma32); 99 100 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32 | 101 __GFP_NOWARN); 102 103 /* Retry without GFP_DMA32 for platforms DMA32 is not available */ 104 if (unlikely(glob->dummy_read_page == NULL)) { 105 glob->dummy_read_page = alloc_page(__GFP_ZERO); 106 if (unlikely(glob->dummy_read_page == NULL)) { 107 ret = -ENOMEM; 108 goto out; 109 } 110 pr_warn("Using GFP_DMA32 fallback for dummy_read_page\n"); 111 } 112 113 INIT_LIST_HEAD(&glob->device_list); 114 atomic_set(&glob->bo_count, 0); 115 116 debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root, 117 &glob->bo_count); 118 out: 119 if (ret && ttm_debugfs_root) 120 debugfs_remove(ttm_debugfs_root); 121 if (ret) 122 --ttm_glob_use_count; 123 mutex_unlock(&ttm_global_mutex); 124 return ret; 125 } 126 127 /* 128 * A buffer object shrink method that tries to swap out the first 129 * buffer object on the global::swap_lru list. 130 */ 131 int ttm_global_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags) 132 { 133 struct ttm_global *glob = &ttm_glob; 134 struct ttm_device *bdev; 135 int ret = 0; 136 137 mutex_lock(&ttm_global_mutex); 138 list_for_each_entry(bdev, &glob->device_list, device_list) { 139 ret = ttm_device_swapout(bdev, ctx, gfp_flags); 140 if (ret > 0) { 141 list_move_tail(&bdev->device_list, &glob->device_list); 142 break; 143 } 144 } 145 mutex_unlock(&ttm_global_mutex); 146 return ret; 147 } 148 149 int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx, 150 gfp_t gfp_flags) 151 { 152 struct ttm_resource_manager *man; 153 unsigned i; 154 s64 lret; 155 156 for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) { 157 man = ttm_manager_type(bdev, i); 158 if (!man || !man->use_tt) 159 continue; 160 161 lret = ttm_bo_swapout(bdev, ctx, man, gfp_flags, 1); 162 /* Can be both positive (num_pages) and negative (error) */ 163 if (lret) 164 return lret; 165 } 166 return 0; 167 } 168 EXPORT_SYMBOL(ttm_device_swapout); 169 170 /** 171 * ttm_device_init 172 * 173 * @bdev: A pointer to a struct ttm_device to initialize. 174 * @funcs: Function table for the device. 175 * @dev: The core kernel device pointer for DMA mappings and allocations. 176 * @mapping: The address space to use for this bo. 177 * @vma_manager: A pointer to a vma manager. 178 * @use_dma_alloc: If coherent DMA allocation API should be used. 179 * @use_dma32: If we should use GFP_DMA32 for device memory allocations. 180 * 181 * Initializes a struct ttm_device: 182 * Returns: 183 * !0: Failure. 184 */ 185 int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs, 186 struct device *dev, struct address_space *mapping, 187 struct drm_vma_offset_manager *vma_manager, 188 bool use_dma_alloc, bool use_dma32) 189 { 190 struct ttm_global *glob = &ttm_glob; 191 int ret, nid; 192 193 if (WARN_ON(vma_manager == NULL)) 194 return -EINVAL; 195 196 ret = ttm_global_init(); 197 if (ret) 198 return ret; 199 200 bdev->wq = alloc_workqueue("ttm", 201 WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 16); 202 if (!bdev->wq) { 203 ttm_global_release(); 204 return -ENOMEM; 205 } 206 207 bdev->funcs = funcs; 208 209 ttm_sys_man_init(bdev); 210 211 if (dev) 212 nid = dev_to_node(dev); 213 else 214 nid = NUMA_NO_NODE; 215 216 ttm_pool_init(&bdev->pool, dev, nid, use_dma_alloc, use_dma32); 217 218 bdev->vma_manager = vma_manager; 219 spin_lock_init(&bdev->lru_lock); 220 INIT_LIST_HEAD(&bdev->unevictable); 221 bdev->dev_mapping = mapping; 222 mutex_lock(&ttm_global_mutex); 223 list_add_tail(&bdev->device_list, &glob->device_list); 224 mutex_unlock(&ttm_global_mutex); 225 226 return 0; 227 } 228 EXPORT_SYMBOL(ttm_device_init); 229 230 void ttm_device_fini(struct ttm_device *bdev) 231 { 232 struct ttm_resource_manager *man; 233 unsigned i; 234 235 mutex_lock(&ttm_global_mutex); 236 list_del(&bdev->device_list); 237 mutex_unlock(&ttm_global_mutex); 238 239 drain_workqueue(bdev->wq); 240 destroy_workqueue(bdev->wq); 241 242 man = ttm_manager_type(bdev, TTM_PL_SYSTEM); 243 ttm_resource_manager_set_used(man, false); 244 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL); 245 246 spin_lock(&bdev->lru_lock); 247 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) 248 if (list_empty(&man->lru[0])) 249 pr_debug("Swap list %d was clean\n", i); 250 spin_unlock(&bdev->lru_lock); 251 252 ttm_pool_fini(&bdev->pool); 253 ttm_global_release(); 254 } 255 EXPORT_SYMBOL(ttm_device_fini); 256 257 static void ttm_device_clear_lru_dma_mappings(struct ttm_device *bdev, 258 struct list_head *list) 259 { 260 struct ttm_resource *res; 261 262 spin_lock(&bdev->lru_lock); 263 while ((res = ttm_lru_first_res_or_null(list))) { 264 struct ttm_buffer_object *bo = res->bo; 265 266 /* Take ref against racing releases once lru_lock is unlocked */ 267 if (!ttm_bo_get_unless_zero(bo)) 268 continue; 269 270 list_del_init(&bo->resource->lru.link); 271 spin_unlock(&bdev->lru_lock); 272 273 if (bo->ttm) 274 ttm_tt_unpopulate(bo->bdev, bo->ttm); 275 276 ttm_bo_put(bo); 277 spin_lock(&bdev->lru_lock); 278 } 279 spin_unlock(&bdev->lru_lock); 280 } 281 282 void ttm_device_clear_dma_mappings(struct ttm_device *bdev) 283 { 284 struct ttm_resource_manager *man; 285 unsigned int i, j; 286 287 ttm_device_clear_lru_dma_mappings(bdev, &bdev->unevictable); 288 289 for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) { 290 man = ttm_manager_type(bdev, i); 291 if (!man || !man->use_tt) 292 continue; 293 294 for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j) 295 ttm_device_clear_lru_dma_mappings(bdev, &man->lru[j]); 296 } 297 } 298 EXPORT_SYMBOL(ttm_device_clear_dma_mappings); 299