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/mm.h> 31 32 #include <drm/ttm/ttm_device.h> 33 #include <drm/ttm/ttm_tt.h> 34 #include <drm/ttm/ttm_placement.h> 35 #include <drm/ttm/ttm_bo_api.h> 36 37 #include "ttm_module.h" 38 39 /* 40 * ttm_global_mutex - protecting the global state 41 */ 42 static DEFINE_MUTEX(ttm_global_mutex); 43 static unsigned ttm_glob_use_count; 44 struct ttm_global ttm_glob; 45 EXPORT_SYMBOL(ttm_glob); 46 47 struct dentry *ttm_debugfs_root; 48 49 static void ttm_global_release(void) 50 { 51 struct ttm_global *glob = &ttm_glob; 52 53 mutex_lock(&ttm_global_mutex); 54 if (--ttm_glob_use_count > 0) 55 goto out; 56 57 ttm_pool_mgr_fini(); 58 debugfs_remove(ttm_debugfs_root); 59 60 __free_page(glob->dummy_read_page); 61 memset(glob, 0, sizeof(*glob)); 62 out: 63 mutex_unlock(&ttm_global_mutex); 64 } 65 66 static int ttm_global_init(void) 67 { 68 struct ttm_global *glob = &ttm_glob; 69 unsigned long num_pages, num_dma32; 70 struct sysinfo si; 71 int ret = 0; 72 73 mutex_lock(&ttm_global_mutex); 74 if (++ttm_glob_use_count > 1) 75 goto out; 76 77 si_meminfo(&si); 78 79 ttm_debugfs_root = debugfs_create_dir("ttm", NULL); 80 if (IS_ERR(ttm_debugfs_root)) { 81 ret = PTR_ERR(ttm_debugfs_root); 82 ttm_debugfs_root = NULL; 83 goto out; 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 102 if (unlikely(glob->dummy_read_page == NULL)) { 103 ret = -ENOMEM; 104 goto out; 105 } 106 107 INIT_LIST_HEAD(&glob->device_list); 108 atomic_set(&glob->bo_count, 0); 109 110 debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root, 111 &glob->bo_count); 112 out: 113 if (ret && ttm_debugfs_root) 114 debugfs_remove(ttm_debugfs_root); 115 if (ret) 116 --ttm_glob_use_count; 117 mutex_unlock(&ttm_global_mutex); 118 return ret; 119 } 120 121 /* 122 * A buffer object shrink method that tries to swap out the first 123 * buffer object on the global::swap_lru list. 124 */ 125 int ttm_global_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags) 126 { 127 struct ttm_global *glob = &ttm_glob; 128 struct ttm_device *bdev; 129 int ret = 0; 130 131 mutex_lock(&ttm_global_mutex); 132 list_for_each_entry(bdev, &glob->device_list, device_list) { 133 ret = ttm_device_swapout(bdev, ctx, gfp_flags); 134 if (ret > 0) { 135 list_move_tail(&bdev->device_list, &glob->device_list); 136 break; 137 } 138 } 139 mutex_unlock(&ttm_global_mutex); 140 return ret; 141 } 142 EXPORT_SYMBOL(ttm_global_swapout); 143 144 int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx, 145 gfp_t gfp_flags) 146 { 147 struct ttm_resource_manager *man; 148 struct ttm_buffer_object *bo; 149 unsigned i, j; 150 int ret; 151 152 spin_lock(&bdev->lru_lock); 153 for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) { 154 man = ttm_manager_type(bdev, i); 155 if (!man || !man->use_tt) 156 continue; 157 158 for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j) { 159 list_for_each_entry(bo, &man->lru[j], lru) { 160 uint32_t num_pages = PFN_UP(bo->base.size); 161 162 ret = ttm_bo_swapout(bo, ctx, gfp_flags); 163 /* ttm_bo_swapout has dropped the lru_lock */ 164 if (!ret) 165 return num_pages; 166 if (ret != -EBUSY) 167 return ret; 168 } 169 } 170 } 171 spin_unlock(&bdev->lru_lock); 172 return 0; 173 } 174 EXPORT_SYMBOL(ttm_device_swapout); 175 176 static void ttm_device_delayed_workqueue(struct work_struct *work) 177 { 178 struct ttm_device *bdev = 179 container_of(work, struct ttm_device, wq.work); 180 181 if (!ttm_bo_delayed_delete(bdev, false)) 182 schedule_delayed_work(&bdev->wq, 183 ((HZ / 100) < 1) ? 1 : HZ / 100); 184 } 185 186 /** 187 * ttm_device_init 188 * 189 * @bdev: A pointer to a struct ttm_device to initialize. 190 * @funcs: Function table for the device. 191 * @dev: The core kernel device pointer for DMA mappings and allocations. 192 * @mapping: The address space to use for this bo. 193 * @vma_manager: A pointer to a vma manager. 194 * @use_dma_alloc: If coherent DMA allocation API should be used. 195 * @use_dma32: If we should use GFP_DMA32 for device memory allocations. 196 * 197 * Initializes a struct ttm_device: 198 * Returns: 199 * !0: Failure. 200 */ 201 int ttm_device_init(struct ttm_device *bdev, struct ttm_device_funcs *funcs, 202 struct device *dev, struct address_space *mapping, 203 struct drm_vma_offset_manager *vma_manager, 204 bool use_dma_alloc, bool use_dma32) 205 { 206 struct ttm_global *glob = &ttm_glob; 207 int ret; 208 209 if (WARN_ON(vma_manager == NULL)) 210 return -EINVAL; 211 212 ret = ttm_global_init(); 213 if (ret) 214 return ret; 215 216 bdev->funcs = funcs; 217 218 ttm_sys_man_init(bdev); 219 ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32); 220 221 bdev->vma_manager = vma_manager; 222 INIT_DELAYED_WORK(&bdev->wq, ttm_device_delayed_workqueue); 223 spin_lock_init(&bdev->lru_lock); 224 INIT_LIST_HEAD(&bdev->ddestroy); 225 bdev->dev_mapping = mapping; 226 mutex_lock(&ttm_global_mutex); 227 list_add_tail(&bdev->device_list, &glob->device_list); 228 mutex_unlock(&ttm_global_mutex); 229 230 return 0; 231 } 232 EXPORT_SYMBOL(ttm_device_init); 233 234 void ttm_device_fini(struct ttm_device *bdev) 235 { 236 struct ttm_resource_manager *man; 237 unsigned i; 238 239 man = ttm_manager_type(bdev, TTM_PL_SYSTEM); 240 ttm_resource_manager_set_used(man, false); 241 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL); 242 243 mutex_lock(&ttm_global_mutex); 244 list_del(&bdev->device_list); 245 mutex_unlock(&ttm_global_mutex); 246 247 cancel_delayed_work_sync(&bdev->wq); 248 249 if (ttm_bo_delayed_delete(bdev, true)) 250 pr_debug("Delayed destroy list was clean\n"); 251 252 spin_lock(&bdev->lru_lock); 253 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) 254 if (list_empty(&man->lru[0])) 255 pr_debug("Swap list %d was clean\n", i); 256 spin_unlock(&bdev->lru_lock); 257 258 ttm_pool_fini(&bdev->pool); 259 ttm_global_release(); 260 } 261 EXPORT_SYMBOL(ttm_device_fini); 262