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