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_mark_bo_teardown - exclude a BO from GART recovery 107 * 108 * @tbo: TTM BO whose TT backing is about to be destroyed 109 * 110 * Keep the GART range allocated until the resource is freed, but make recovery 111 * treat it like a range without a BO so it isn't touched after TT teardown has 112 * started. 113 */ 114 void amdgpu_gtt_mgr_mark_bo_teardown(struct ttm_buffer_object *tbo) 115 { 116 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev); 117 struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(tbo->resource); 118 struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr; 119 120 dma_resv_assert_held(tbo->base.resv); 121 122 spin_lock(&mgr->lock); 123 if (drm_mm_node_allocated(&node->mm_nodes[0])) 124 node->mm_nodes[0].color = GART_ENTRY_WITHOUT_BO_COLOR; 125 spin_unlock(&mgr->lock); 126 } 127 128 /** 129 * amdgpu_gtt_mgr_new - allocate a new node 130 * 131 * @man: TTM memory type manager 132 * @tbo: TTM BO we need this range for 133 * @place: placement flags and restrictions 134 * @res: the resulting mem object 135 * 136 * Dummy, allocate the node but no space for it yet. 137 */ 138 static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man, 139 struct ttm_buffer_object *tbo, 140 const struct ttm_place *place, 141 struct ttm_resource **res) 142 { 143 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 144 uint32_t num_pages = PFN_UP(tbo->base.size); 145 struct ttm_range_mgr_node *node; 146 int r; 147 148 node = kzalloc_flex(*node, mm_nodes, 1); 149 if (!node) 150 return -ENOMEM; 151 152 ttm_resource_init(tbo, place, &node->base); 153 if (!(place->flags & TTM_PL_FLAG_TEMPORARY) && 154 ttm_resource_manager_usage(man) > man->size) { 155 r = -ENOSPC; 156 goto err_free; 157 } 158 159 if (place->lpfn) { 160 spin_lock(&mgr->lock); 161 r = drm_mm_insert_node_in_range(&mgr->mm, &node->mm_nodes[0], 162 num_pages, tbo->page_alignment, 163 0, place->fpfn, place->lpfn, 164 DRM_MM_INSERT_BEST); 165 spin_unlock(&mgr->lock); 166 if (unlikely(r)) 167 goto err_free; 168 169 node->base.start = node->mm_nodes[0].start; 170 } else { 171 node->mm_nodes[0].start = 0; 172 node->mm_nodes[0].size = PFN_UP(node->base.size); 173 node->base.start = AMDGPU_BO_INVALID_OFFSET; 174 } 175 176 *res = &node->base; 177 return 0; 178 179 err_free: 180 ttm_resource_fini(man, &node->base); 181 kfree(node); 182 return r; 183 } 184 185 /** 186 * amdgpu_gtt_mgr_del - free ranges 187 * 188 * @man: TTM memory type manager 189 * @res: TTM memory object 190 * 191 * Free the allocated GTT again. 192 */ 193 static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man, 194 struct ttm_resource *res) 195 { 196 struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res); 197 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 198 199 spin_lock(&mgr->lock); 200 if (drm_mm_node_allocated(&node->mm_nodes[0])) 201 drm_mm_remove_node(&node->mm_nodes[0]); 202 spin_unlock(&mgr->lock); 203 204 ttm_resource_fini(man, res); 205 kfree(node); 206 } 207 208 /** 209 * amdgpu_gtt_mgr_alloc_entries - alloc GART entries without GTT bo 210 * 211 * @mgr: The GTT manager object 212 * @mm_node: The drm mm node to return the new allocation node information 213 * @num_pages: The number of pages for the new allocation 214 * @mode: The new allocation mode 215 * 216 * Helper to dynamic alloc GART entries to map memory not accociated with 217 * GTT BO, for example VRAM BO physical memory, remote physical memory. 218 */ 219 int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr, 220 struct drm_mm_node *mm_node, 221 u64 num_pages, 222 enum drm_mm_insert_mode mode) 223 { 224 struct amdgpu_device *adev = container_of(mgr, typeof(*adev), mman.gtt_mgr); 225 u32 alignment = 0; 226 int r; 227 228 /* Align to TLB L2 cache entry size to work around "V bit HW bug" */ 229 if (adev->family == AMDGPU_FAMILY_SI) { 230 alignment = 32 * 1024 / AMDGPU_GPU_PAGE_SIZE; 231 num_pages = ALIGN(num_pages, alignment); 232 } 233 234 spin_lock(&mgr->lock); 235 r = drm_mm_insert_node_in_range(&mgr->mm, mm_node, num_pages, 236 alignment, GART_ENTRY_WITHOUT_BO_COLOR, 0, 237 adev->gmc.gart_size >> PAGE_SHIFT, 238 mode); 239 spin_unlock(&mgr->lock); 240 return r; 241 } 242 243 /** 244 * amdgpu_gtt_mgr_free_entries - free GART entries not accocaited with GTT bo 245 * 246 * @mgr: The GTT manager object 247 * @mm_node: The drm mm node to free 248 */ 249 void amdgpu_gtt_mgr_free_entries(struct amdgpu_gtt_mgr *mgr, 250 struct drm_mm_node *mm_node) 251 { 252 spin_lock(&mgr->lock); 253 if (drm_mm_node_allocated(mm_node)) 254 drm_mm_remove_node(mm_node); 255 spin_unlock(&mgr->lock); 256 } 257 258 /** 259 * amdgpu_gtt_mgr_recover - re-init gart 260 * 261 * @mgr: amdgpu_gtt_mgr pointer 262 * 263 * Re-init the gart for each known BO in the GTT. 264 */ 265 void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr) 266 { 267 struct ttm_range_mgr_node *node; 268 struct drm_mm_node *mm_node; 269 struct amdgpu_device *adev; 270 271 adev = container_of(mgr, typeof(*adev), mman.gtt_mgr); 272 spin_lock(&mgr->lock); 273 drm_mm_for_each_node(mm_node, &mgr->mm) { 274 if (mm_node->color == GART_ENTRY_WITHOUT_BO_COLOR) 275 continue; 276 277 node = container_of(mm_node, typeof(*node), mm_nodes[0]); 278 amdgpu_ttm_recover_gart(node->base.bo); 279 } 280 spin_unlock(&mgr->lock); 281 } 282 283 /** 284 * amdgpu_gtt_mgr_intersects - test for intersection 285 * 286 * @man: Our manager object 287 * @res: The resource to test 288 * @place: The place for the new allocation 289 * @size: The size of the new allocation 290 * 291 * Simplified intersection test, only interesting if we need GART or not. 292 */ 293 static bool amdgpu_gtt_mgr_intersects(struct ttm_resource_manager *man, 294 struct ttm_resource *res, 295 const struct ttm_place *place, 296 size_t size) 297 { 298 const struct drm_mm_node *const node = &to_ttm_range_mgr_node(res)->mm_nodes[0]; 299 const u32 num_pages = PFN_UP(size); 300 301 if (!place->lpfn) 302 return true; 303 304 if (!amdgpu_gtt_mgr_has_gart_addr(res)) 305 return false; 306 307 if (place->fpfn >= (node->start + num_pages) || 308 (place->lpfn && place->lpfn <= node->start)) 309 return false; 310 311 return true; 312 } 313 314 /** 315 * amdgpu_gtt_mgr_compatible - test for compatibility 316 * 317 * @man: Our manager object 318 * @res: The resource to test 319 * @place: The place for the new allocation 320 * @size: The size of the new allocation 321 * 322 * Simplified compatibility test. 323 */ 324 static bool amdgpu_gtt_mgr_compatible(struct ttm_resource_manager *man, 325 struct ttm_resource *res, 326 const struct ttm_place *place, 327 size_t size) 328 { 329 const struct drm_mm_node *const node = &to_ttm_range_mgr_node(res)->mm_nodes[0]; 330 const u32 num_pages = PFN_UP(size); 331 332 if (!place->lpfn) 333 return true; 334 335 if (!amdgpu_gtt_mgr_has_gart_addr(res)) 336 return false; 337 338 if (node->start < place->fpfn || 339 (place->lpfn && (node->start + num_pages) > place->lpfn)) 340 return false; 341 342 return true; 343 } 344 345 /** 346 * amdgpu_gtt_mgr_debug - dump VRAM table 347 * 348 * @man: TTM memory type manager 349 * @printer: DRM printer to use 350 * 351 * Dump the table content using printk. 352 */ 353 static void amdgpu_gtt_mgr_debug(struct ttm_resource_manager *man, 354 struct drm_printer *printer) 355 { 356 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 357 358 spin_lock(&mgr->lock); 359 drm_mm_print(&mgr->mm, printer); 360 spin_unlock(&mgr->lock); 361 } 362 363 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func = { 364 .alloc = amdgpu_gtt_mgr_new, 365 .free = amdgpu_gtt_mgr_del, 366 .intersects = amdgpu_gtt_mgr_intersects, 367 .compatible = amdgpu_gtt_mgr_compatible, 368 .debug = amdgpu_gtt_mgr_debug 369 }; 370 371 /** 372 * amdgpu_gtt_mgr_init - init GTT manager and DRM MM 373 * 374 * @adev: amdgpu_device pointer 375 * @gtt_size: maximum size of GTT 376 * 377 * Allocate and initialize the GTT manager. 378 */ 379 int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size) 380 { 381 struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr; 382 struct ttm_resource_manager *man = &mgr->manager; 383 384 man->use_tt = true; 385 man->func = &amdgpu_gtt_mgr_func; 386 387 ttm_resource_manager_init(man, &adev->mman.bdev, gtt_size); 388 389 drm_mm_init(&mgr->mm, 0, adev->gmc.gart_size >> PAGE_SHIFT); 390 spin_lock_init(&mgr->lock); 391 392 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, &mgr->manager); 393 ttm_resource_manager_set_used(man, true); 394 return 0; 395 } 396 397 /** 398 * amdgpu_gtt_mgr_fini - free and destroy GTT manager 399 * 400 * @adev: amdgpu_device pointer 401 * 402 * Destroy and free the GTT manager, returns -EBUSY if ranges are still 403 * allocated inside it. 404 */ 405 void amdgpu_gtt_mgr_fini(struct amdgpu_device *adev) 406 { 407 struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr; 408 struct ttm_resource_manager *man = &mgr->manager; 409 int ret; 410 411 ttm_resource_manager_set_used(man, false); 412 413 ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man); 414 if (ret) 415 return; 416 417 spin_lock(&mgr->lock); 418 drm_mm_takedown(&mgr->mm); 419 spin_unlock(&mgr->lock); 420 421 ttm_resource_manager_cleanup(man); 422 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, NULL); 423 } 424