1 /* 2 * Copyright 2016 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: Christian König 23 */ 24 25 #include <drm/ttm/ttm_range_manager.h> 26 27 #include "amdgpu.h" 28 29 #define GART_ENTRY_WITHOUT_BO_COLOR 1 30 31 static inline struct amdgpu_gtt_mgr * 32 to_gtt_mgr(struct ttm_resource_manager *man) 33 { 34 return container_of(man, struct amdgpu_gtt_mgr, manager); 35 } 36 37 /** 38 * DOC: mem_info_gtt_total 39 * 40 * The amdgpu driver provides a sysfs API for reporting current total size of 41 * the GTT. 42 * The file mem_info_gtt_total is used for this, and returns the total size of 43 * the GTT block, in bytes 44 */ 45 static ssize_t amdgpu_mem_info_gtt_total_show(struct device *dev, 46 struct device_attribute *attr, 47 char *buf) 48 { 49 struct drm_device *ddev = dev_get_drvdata(dev); 50 struct amdgpu_device *adev = drm_to_adev(ddev); 51 struct ttm_resource_manager *man; 52 53 man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT); 54 return sysfs_emit(buf, "%llu\n", man->size); 55 } 56 57 /** 58 * DOC: mem_info_gtt_used 59 * 60 * The amdgpu driver provides a sysfs API for reporting current total amount of 61 * used GTT. 62 * The file mem_info_gtt_used is used for this, and returns the current used 63 * size of the GTT block, in bytes 64 */ 65 static ssize_t amdgpu_mem_info_gtt_used_show(struct device *dev, 66 struct device_attribute *attr, 67 char *buf) 68 { 69 struct drm_device *ddev = dev_get_drvdata(dev); 70 struct amdgpu_device *adev = drm_to_adev(ddev); 71 struct ttm_resource_manager *man = &adev->mman.gtt_mgr.manager; 72 73 return sysfs_emit(buf, "%llu\n", ttm_resource_manager_usage(man)); 74 } 75 76 static DEVICE_ATTR(mem_info_gtt_total, S_IRUGO, 77 amdgpu_mem_info_gtt_total_show, NULL); 78 static DEVICE_ATTR(mem_info_gtt_used, S_IRUGO, 79 amdgpu_mem_info_gtt_used_show, NULL); 80 81 static struct attribute *amdgpu_gtt_mgr_attributes[] = { 82 &dev_attr_mem_info_gtt_total.attr, 83 &dev_attr_mem_info_gtt_used.attr, 84 NULL 85 }; 86 87 const struct attribute_group amdgpu_gtt_mgr_attr_group = { 88 .attrs = amdgpu_gtt_mgr_attributes 89 }; 90 91 /** 92 * amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space 93 * 94 * @res: the mem object to check 95 * 96 * Check if a mem object has already address space allocated. 97 */ 98 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *res) 99 { 100 struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res); 101 102 return drm_mm_node_allocated(&node->mm_nodes[0]); 103 } 104 105 /** 106 * amdgpu_gtt_mgr_new - allocate a new node 107 * 108 * @man: TTM memory type manager 109 * @tbo: TTM BO we need this range for 110 * @place: placement flags and restrictions 111 * @res: the resulting mem object 112 * 113 * Dummy, allocate the node but no space for it yet. 114 */ 115 static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man, 116 struct ttm_buffer_object *tbo, 117 const struct ttm_place *place, 118 struct ttm_resource **res) 119 { 120 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 121 uint32_t num_pages = PFN_UP(tbo->base.size); 122 struct ttm_range_mgr_node *node; 123 int r; 124 125 node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL); 126 if (!node) 127 return -ENOMEM; 128 129 ttm_resource_init(tbo, place, &node->base); 130 if (!(place->flags & TTM_PL_FLAG_TEMPORARY) && 131 ttm_resource_manager_usage(man) > man->size) { 132 r = -ENOSPC; 133 goto err_free; 134 } 135 136 if (place->lpfn) { 137 spin_lock(&mgr->lock); 138 r = drm_mm_insert_node_in_range(&mgr->mm, &node->mm_nodes[0], 139 num_pages, tbo->page_alignment, 140 0, place->fpfn, place->lpfn, 141 DRM_MM_INSERT_BEST); 142 spin_unlock(&mgr->lock); 143 if (unlikely(r)) 144 goto err_free; 145 146 node->base.start = node->mm_nodes[0].start; 147 } else { 148 node->mm_nodes[0].start = 0; 149 node->mm_nodes[0].size = PFN_UP(node->base.size); 150 node->base.start = AMDGPU_BO_INVALID_OFFSET; 151 } 152 153 *res = &node->base; 154 return 0; 155 156 err_free: 157 ttm_resource_fini(man, &node->base); 158 kfree(node); 159 return r; 160 } 161 162 /** 163 * amdgpu_gtt_mgr_del - free ranges 164 * 165 * @man: TTM memory type manager 166 * @res: TTM memory object 167 * 168 * Free the allocated GTT again. 169 */ 170 static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man, 171 struct ttm_resource *res) 172 { 173 struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res); 174 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 175 176 spin_lock(&mgr->lock); 177 if (drm_mm_node_allocated(&node->mm_nodes[0])) 178 drm_mm_remove_node(&node->mm_nodes[0]); 179 spin_unlock(&mgr->lock); 180 181 ttm_resource_fini(man, res); 182 kfree(node); 183 } 184 185 /** 186 * amdgpu_gtt_mgr_alloc_entries - alloc GART entries without GTT bo 187 * 188 * @mgr: The GTT manager object 189 * @mm_node: The drm mm node to return the new allocation node information 190 * @num_pages: The number of pages for the new allocation 191 * @mode: The new allocation mode 192 * 193 * Helper to dynamic alloc GART entries to map memory not accociated with 194 * GTT BO, for example VRAM BO physical memory, remote physical memory. 195 */ 196 int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr, 197 struct drm_mm_node *mm_node, 198 u64 num_pages, 199 enum drm_mm_insert_mode mode) 200 { 201 struct amdgpu_device *adev = container_of(mgr, typeof(*adev), mman.gtt_mgr); 202 int r; 203 204 spin_lock(&mgr->lock); 205 r = drm_mm_insert_node_in_range(&mgr->mm, mm_node, num_pages, 206 0, GART_ENTRY_WITHOUT_BO_COLOR, 0, 207 adev->gmc.gart_size >> PAGE_SHIFT, 208 mode); 209 spin_unlock(&mgr->lock); 210 return r; 211 } 212 213 /** 214 * amdgpu_gtt_mgr_free_entries - free GART entries not accocaited with GTT bo 215 * 216 * @mgr: The GTT manager object 217 * @mm_node: The drm mm node to free 218 */ 219 void amdgpu_gtt_mgr_free_entries(struct amdgpu_gtt_mgr *mgr, 220 struct drm_mm_node *mm_node) 221 { 222 spin_lock(&mgr->lock); 223 if (drm_mm_node_allocated(mm_node)) 224 drm_mm_remove_node(mm_node); 225 spin_unlock(&mgr->lock); 226 } 227 228 /** 229 * amdgpu_gtt_mgr_recover - re-init gart 230 * 231 * @mgr: amdgpu_gtt_mgr pointer 232 * 233 * Re-init the gart for each known BO in the GTT. 234 */ 235 void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr) 236 { 237 struct ttm_range_mgr_node *node; 238 struct drm_mm_node *mm_node; 239 struct amdgpu_device *adev; 240 241 adev = container_of(mgr, typeof(*adev), mman.gtt_mgr); 242 spin_lock(&mgr->lock); 243 drm_mm_for_each_node(mm_node, &mgr->mm) { 244 if (mm_node->color == GART_ENTRY_WITHOUT_BO_COLOR) 245 continue; 246 247 node = container_of(mm_node, typeof(*node), mm_nodes[0]); 248 amdgpu_ttm_recover_gart(node->base.bo); 249 } 250 spin_unlock(&mgr->lock); 251 } 252 253 /** 254 * amdgpu_gtt_mgr_intersects - test for intersection 255 * 256 * @man: Our manager object 257 * @res: The resource to test 258 * @place: The place for the new allocation 259 * @size: The size of the new allocation 260 * 261 * Simplified intersection test, only interesting if we need GART or not. 262 */ 263 static bool amdgpu_gtt_mgr_intersects(struct ttm_resource_manager *man, 264 struct ttm_resource *res, 265 const struct ttm_place *place, 266 size_t size) 267 { 268 return !place->lpfn || amdgpu_gtt_mgr_has_gart_addr(res); 269 } 270 271 /** 272 * amdgpu_gtt_mgr_compatible - test for compatibility 273 * 274 * @man: Our manager object 275 * @res: The resource to test 276 * @place: The place for the new allocation 277 * @size: The size of the new allocation 278 * 279 * Simplified compatibility test. 280 */ 281 static bool amdgpu_gtt_mgr_compatible(struct ttm_resource_manager *man, 282 struct ttm_resource *res, 283 const struct ttm_place *place, 284 size_t size) 285 { 286 return !place->lpfn || amdgpu_gtt_mgr_has_gart_addr(res); 287 } 288 289 /** 290 * amdgpu_gtt_mgr_debug - dump VRAM table 291 * 292 * @man: TTM memory type manager 293 * @printer: DRM printer to use 294 * 295 * Dump the table content using printk. 296 */ 297 static void amdgpu_gtt_mgr_debug(struct ttm_resource_manager *man, 298 struct drm_printer *printer) 299 { 300 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 301 302 spin_lock(&mgr->lock); 303 drm_mm_print(&mgr->mm, printer); 304 spin_unlock(&mgr->lock); 305 } 306 307 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func = { 308 .alloc = amdgpu_gtt_mgr_new, 309 .free = amdgpu_gtt_mgr_del, 310 .intersects = amdgpu_gtt_mgr_intersects, 311 .compatible = amdgpu_gtt_mgr_compatible, 312 .debug = amdgpu_gtt_mgr_debug 313 }; 314 315 /** 316 * amdgpu_gtt_mgr_init - init GTT manager and DRM MM 317 * 318 * @adev: amdgpu_device pointer 319 * @gtt_size: maximum size of GTT 320 * 321 * Allocate and initialize the GTT manager. 322 */ 323 int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size) 324 { 325 struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr; 326 struct ttm_resource_manager *man = &mgr->manager; 327 uint64_t start, size; 328 329 man->use_tt = true; 330 man->func = &amdgpu_gtt_mgr_func; 331 332 ttm_resource_manager_init(man, &adev->mman.bdev, gtt_size); 333 334 start = AMDGPU_GTT_MAX_TRANSFER_SIZE * AMDGPU_GTT_NUM_TRANSFER_WINDOWS; 335 start += amdgpu_vce_required_gart_pages(adev); 336 size = (adev->gmc.gart_size >> PAGE_SHIFT) - start; 337 drm_mm_init(&mgr->mm, start, size); 338 spin_lock_init(&mgr->lock); 339 340 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, &mgr->manager); 341 ttm_resource_manager_set_used(man, true); 342 return 0; 343 } 344 345 /** 346 * amdgpu_gtt_mgr_fini - free and destroy GTT manager 347 * 348 * @adev: amdgpu_device pointer 349 * 350 * Destroy and free the GTT manager, returns -EBUSY if ranges are still 351 * allocated inside it. 352 */ 353 void amdgpu_gtt_mgr_fini(struct amdgpu_device *adev) 354 { 355 struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr; 356 struct ttm_resource_manager *man = &mgr->manager; 357 int ret; 358 359 ttm_resource_manager_set_used(man, false); 360 361 ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man); 362 if (ret) 363 return; 364 365 spin_lock(&mgr->lock); 366 drm_mm_takedown(&mgr->mm); 367 spin_unlock(&mgr->lock); 368 369 ttm_resource_manager_cleanup(man); 370 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, NULL); 371 } 372