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/export.h> 32 #include <linux/mm.h> 33 34 #include <drm/ttm/ttm_bo.h> 35 #include <drm/ttm/ttm_device.h> 36 #include <drm/ttm/ttm_tt.h> 37 #include <drm/ttm/ttm_placement.h> 38 39 #include "ttm_module.h" 40 #include "ttm_bo_internal.h" 41 42 /* 43 * ttm_global_mutex - protecting the global state 44 */ 45 static DEFINE_MUTEX(ttm_global_mutex); 46 static unsigned ttm_glob_use_count; 47 struct ttm_global ttm_glob; 48 EXPORT_SYMBOL(ttm_glob); 49 50 struct dentry *ttm_debugfs_root; 51 52 static void ttm_global_release(void) 53 { 54 struct ttm_global *glob = &ttm_glob; 55 56 mutex_lock(&ttm_global_mutex); 57 if (--ttm_glob_use_count > 0) 58 goto out; 59 60 ttm_pool_mgr_fini(); 61 debugfs_remove(ttm_debugfs_root); 62 63 __free_page(glob->dummy_read_page); 64 memset(glob, 0, sizeof(*glob)); 65 out: 66 mutex_unlock(&ttm_global_mutex); 67 } 68 69 static int ttm_global_init(void) 70 { 71 struct ttm_global *glob = &ttm_glob; 72 unsigned long num_pages, num_dma32; 73 struct sysinfo si; 74 int ret = 0; 75 76 mutex_lock(&ttm_global_mutex); 77 if (++ttm_glob_use_count > 1) 78 goto out; 79 80 si_meminfo(&si); 81 82 ttm_debugfs_root = debugfs_create_dir("ttm", NULL); 83 if (IS_ERR(ttm_debugfs_root)) { 84 ttm_debugfs_root = NULL; 85 } 86 87 /* Limit the number of pages in the pool to about 50% of the total 88 * system memory. 89 */ 90 num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT; 91 num_pages /= 2; 92 93 /* But for DMA32 we limit ourself to only use 2GiB maximum. */ 94 num_dma32 = (u64)(si.totalram - si.totalhigh) * si.mem_unit 95 >> PAGE_SHIFT; 96 num_dma32 = min(num_dma32, 2UL << (30 - PAGE_SHIFT)); 97 98 ttm_pool_mgr_init(num_pages); 99 ttm_tt_mgr_init(num_pages, num_dma32); 100 101 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32 | 102 __GFP_NOWARN); 103 104 /* Retry without GFP_DMA32 for platforms DMA32 is not available */ 105 if (unlikely(glob->dummy_read_page == NULL)) { 106 glob->dummy_read_page = alloc_page(__GFP_ZERO); 107 if (unlikely(glob->dummy_read_page == NULL)) { 108 ret = -ENOMEM; 109 goto out; 110 } 111 pr_warn("Using GFP_DMA32 fallback for dummy_read_page\n"); 112 } 113 114 INIT_LIST_HEAD(&glob->device_list); 115 atomic_set(&glob->bo_count, 0); 116 117 debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root, 118 &glob->bo_count); 119 out: 120 if (ret && ttm_debugfs_root) 121 debugfs_remove(ttm_debugfs_root); 122 if (ret) 123 --ttm_glob_use_count; 124 mutex_unlock(&ttm_global_mutex); 125 return ret; 126 } 127 128 /* 129 * A buffer object shrink method that tries to swap out the first 130 * buffer object on the global::swap_lru list. 131 */ 132 int ttm_global_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags) 133 { 134 struct ttm_global *glob = &ttm_glob; 135 struct ttm_device *bdev; 136 int ret = 0; 137 138 mutex_lock(&ttm_global_mutex); 139 list_for_each_entry(bdev, &glob->device_list, device_list) { 140 ret = ttm_device_swapout(bdev, ctx, gfp_flags); 141 if (ret > 0) { 142 list_move_tail(&bdev->device_list, &glob->device_list); 143 break; 144 } 145 } 146 mutex_unlock(&ttm_global_mutex); 147 return ret; 148 } 149 150 int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx, 151 gfp_t gfp_flags) 152 { 153 struct ttm_resource_manager *man; 154 unsigned i; 155 s64 lret; 156 157 for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) { 158 man = ttm_manager_type(bdev, i); 159 if (!man || !man->use_tt) 160 continue; 161 162 lret = ttm_bo_swapout(bdev, ctx, man, gfp_flags, 1); 163 /* Can be both positive (num_pages) and negative (error) */ 164 if (lret) 165 return lret; 166 } 167 return 0; 168 } 169 EXPORT_SYMBOL(ttm_device_swapout); 170 171 /** 172 * ttm_device_init 173 * 174 * @bdev: A pointer to a struct ttm_device to initialize. 175 * @funcs: Function table for the device. 176 * @dev: The core kernel device pointer for DMA mappings and allocations. 177 * @mapping: The address space to use for this bo. 178 * @vma_manager: A pointer to a vma manager. 179 * @use_dma_alloc: If coherent DMA allocation API should be used. 180 * @use_dma32: If we should use GFP_DMA32 for device memory allocations. 181 * 182 * Initializes a struct ttm_device: 183 * Returns: 184 * !0: Failure. 185 */ 186 int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs, 187 struct device *dev, struct address_space *mapping, 188 struct drm_vma_offset_manager *vma_manager, 189 bool use_dma_alloc, bool use_dma32) 190 { 191 struct ttm_global *glob = &ttm_glob; 192 int ret, nid; 193 194 if (WARN_ON(vma_manager == NULL)) 195 return -EINVAL; 196 197 ret = ttm_global_init(); 198 if (ret) 199 return ret; 200 201 bdev->wq = alloc_workqueue("ttm", 202 WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_UNBOUND, 16); 203 if (!bdev->wq) { 204 ttm_global_release(); 205 return -ENOMEM; 206 } 207 208 bdev->funcs = funcs; 209 210 ttm_sys_man_init(bdev); 211 212 if (dev) 213 nid = dev_to_node(dev); 214 else 215 nid = NUMA_NO_NODE; 216 217 ttm_pool_init(&bdev->pool, dev, nid, use_dma_alloc, use_dma32); 218 219 bdev->vma_manager = vma_manager; 220 spin_lock_init(&bdev->lru_lock); 221 INIT_LIST_HEAD(&bdev->unevictable); 222 bdev->dev_mapping = mapping; 223 mutex_lock(&ttm_global_mutex); 224 list_add_tail(&bdev->device_list, &glob->device_list); 225 mutex_unlock(&ttm_global_mutex); 226 227 return 0; 228 } 229 EXPORT_SYMBOL(ttm_device_init); 230 231 void ttm_device_fini(struct ttm_device *bdev) 232 { 233 struct ttm_resource_manager *man; 234 unsigned i; 235 236 mutex_lock(&ttm_global_mutex); 237 list_del(&bdev->device_list); 238 mutex_unlock(&ttm_global_mutex); 239 240 drain_workqueue(bdev->wq); 241 destroy_workqueue(bdev->wq); 242 243 man = ttm_manager_type(bdev, TTM_PL_SYSTEM); 244 ttm_resource_manager_set_used(man, false); 245 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL); 246 247 spin_lock(&bdev->lru_lock); 248 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) 249 if (list_empty(&man->lru[0])) 250 pr_debug("Swap list %d was clean\n", i); 251 spin_unlock(&bdev->lru_lock); 252 253 ttm_pool_fini(&bdev->pool); 254 ttm_global_release(); 255 } 256 EXPORT_SYMBOL(ttm_device_fini); 257 258 static void ttm_device_clear_lru_dma_mappings(struct ttm_device *bdev, 259 struct list_head *list) 260 { 261 struct ttm_resource *res; 262 263 spin_lock(&bdev->lru_lock); 264 while ((res = ttm_lru_first_res_or_null(list))) { 265 struct ttm_buffer_object *bo = res->bo; 266 267 /* Take ref against racing releases once lru_lock is unlocked */ 268 if (!ttm_bo_get_unless_zero(bo)) 269 continue; 270 271 list_del_init(&bo->resource->lru.link); 272 spin_unlock(&bdev->lru_lock); 273 274 if (bo->ttm) 275 ttm_tt_unpopulate(bo->bdev, bo->ttm); 276 277 ttm_bo_put(bo); 278 spin_lock(&bdev->lru_lock); 279 } 280 spin_unlock(&bdev->lru_lock); 281 } 282 283 void ttm_device_clear_dma_mappings(struct ttm_device *bdev) 284 { 285 struct ttm_resource_manager *man; 286 unsigned int i, j; 287 288 ttm_device_clear_lru_dma_mappings(bdev, &bdev->unevictable); 289 290 for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) { 291 man = ttm_manager_type(bdev, i); 292 if (!man || !man->use_tt) 293 continue; 294 295 for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j) 296 ttm_device_clear_lru_dma_mappings(bdev, &man->lru[j]); 297 } 298 } 299 EXPORT_SYMBOL(ttm_device_clear_dma_mappings); 300