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 <linux/dma-mapping.h> 26 #include <drm/ttm/ttm_range_manager.h> 27 28 #include "amdgpu.h" 29 #include "amdgpu_vm.h" 30 #include "amdgpu_res_cursor.h" 31 #include "amdgpu_atomfirmware.h" 32 #include "atom.h" 33 34 struct amdgpu_vram_reservation { 35 struct list_head node; 36 struct drm_mm_node mm_node; 37 }; 38 39 static inline struct amdgpu_vram_mgr * 40 to_vram_mgr(struct ttm_resource_manager *man) 41 { 42 return container_of(man, struct amdgpu_vram_mgr, manager); 43 } 44 45 static inline struct amdgpu_device * 46 to_amdgpu_device(struct amdgpu_vram_mgr *mgr) 47 { 48 return container_of(mgr, struct amdgpu_device, mman.vram_mgr); 49 } 50 51 /** 52 * DOC: mem_info_vram_total 53 * 54 * The amdgpu driver provides a sysfs API for reporting current total VRAM 55 * available on the device 56 * The file mem_info_vram_total is used for this and returns the total 57 * amount of VRAM in bytes 58 */ 59 static ssize_t amdgpu_mem_info_vram_total_show(struct device *dev, 60 struct device_attribute *attr, char *buf) 61 { 62 struct drm_device *ddev = dev_get_drvdata(dev); 63 struct amdgpu_device *adev = drm_to_adev(ddev); 64 65 return sysfs_emit(buf, "%llu\n", adev->gmc.real_vram_size); 66 } 67 68 /** 69 * DOC: mem_info_vis_vram_total 70 * 71 * The amdgpu driver provides a sysfs API for reporting current total 72 * visible VRAM available on the device 73 * The file mem_info_vis_vram_total is used for this and returns the total 74 * amount of visible VRAM in bytes 75 */ 76 static ssize_t amdgpu_mem_info_vis_vram_total_show(struct device *dev, 77 struct device_attribute *attr, char *buf) 78 { 79 struct drm_device *ddev = dev_get_drvdata(dev); 80 struct amdgpu_device *adev = drm_to_adev(ddev); 81 82 return sysfs_emit(buf, "%llu\n", adev->gmc.visible_vram_size); 83 } 84 85 /** 86 * DOC: mem_info_vram_used 87 * 88 * The amdgpu driver provides a sysfs API for reporting current total VRAM 89 * available on the device 90 * The file mem_info_vram_used is used for this and returns the total 91 * amount of currently used VRAM in bytes 92 */ 93 static ssize_t amdgpu_mem_info_vram_used_show(struct device *dev, 94 struct device_attribute *attr, 95 char *buf) 96 { 97 struct drm_device *ddev = dev_get_drvdata(dev); 98 struct amdgpu_device *adev = drm_to_adev(ddev); 99 100 return sysfs_emit(buf, "%llu\n", 101 amdgpu_vram_mgr_usage(&adev->mman.vram_mgr)); 102 } 103 104 /** 105 * DOC: mem_info_vis_vram_used 106 * 107 * The amdgpu driver provides a sysfs API for reporting current total of 108 * used visible VRAM 109 * The file mem_info_vis_vram_used is used for this and returns the total 110 * amount of currently used visible VRAM in bytes 111 */ 112 static ssize_t amdgpu_mem_info_vis_vram_used_show(struct device *dev, 113 struct device_attribute *attr, 114 char *buf) 115 { 116 struct drm_device *ddev = dev_get_drvdata(dev); 117 struct amdgpu_device *adev = drm_to_adev(ddev); 118 119 return sysfs_emit(buf, "%llu\n", 120 amdgpu_vram_mgr_vis_usage(&adev->mman.vram_mgr)); 121 } 122 123 /** 124 * DOC: mem_info_vram_vendor 125 * 126 * The amdgpu driver provides a sysfs API for reporting the vendor of the 127 * installed VRAM 128 * The file mem_info_vram_vendor is used for this and returns the name of the 129 * vendor. 130 */ 131 static ssize_t amdgpu_mem_info_vram_vendor(struct device *dev, 132 struct device_attribute *attr, 133 char *buf) 134 { 135 struct drm_device *ddev = dev_get_drvdata(dev); 136 struct amdgpu_device *adev = drm_to_adev(ddev); 137 138 switch (adev->gmc.vram_vendor) { 139 case SAMSUNG: 140 return sysfs_emit(buf, "samsung\n"); 141 case INFINEON: 142 return sysfs_emit(buf, "infineon\n"); 143 case ELPIDA: 144 return sysfs_emit(buf, "elpida\n"); 145 case ETRON: 146 return sysfs_emit(buf, "etron\n"); 147 case NANYA: 148 return sysfs_emit(buf, "nanya\n"); 149 case HYNIX: 150 return sysfs_emit(buf, "hynix\n"); 151 case MOSEL: 152 return sysfs_emit(buf, "mosel\n"); 153 case WINBOND: 154 return sysfs_emit(buf, "winbond\n"); 155 case ESMT: 156 return sysfs_emit(buf, "esmt\n"); 157 case MICRON: 158 return sysfs_emit(buf, "micron\n"); 159 default: 160 return sysfs_emit(buf, "unknown\n"); 161 } 162 } 163 164 static DEVICE_ATTR(mem_info_vram_total, S_IRUGO, 165 amdgpu_mem_info_vram_total_show, NULL); 166 static DEVICE_ATTR(mem_info_vis_vram_total, S_IRUGO, 167 amdgpu_mem_info_vis_vram_total_show,NULL); 168 static DEVICE_ATTR(mem_info_vram_used, S_IRUGO, 169 amdgpu_mem_info_vram_used_show, NULL); 170 static DEVICE_ATTR(mem_info_vis_vram_used, S_IRUGO, 171 amdgpu_mem_info_vis_vram_used_show, NULL); 172 static DEVICE_ATTR(mem_info_vram_vendor, S_IRUGO, 173 amdgpu_mem_info_vram_vendor, NULL); 174 175 static struct attribute *amdgpu_vram_mgr_attributes[] = { 176 &dev_attr_mem_info_vram_total.attr, 177 &dev_attr_mem_info_vis_vram_total.attr, 178 &dev_attr_mem_info_vram_used.attr, 179 &dev_attr_mem_info_vis_vram_used.attr, 180 &dev_attr_mem_info_vram_vendor.attr, 181 NULL 182 }; 183 184 const struct attribute_group amdgpu_vram_mgr_attr_group = { 185 .attrs = amdgpu_vram_mgr_attributes 186 }; 187 188 /** 189 * amdgpu_vram_mgr_vis_size - Calculate visible node size 190 * 191 * @adev: amdgpu_device pointer 192 * @node: MM node structure 193 * 194 * Calculate how many bytes of the MM node are inside visible VRAM 195 */ 196 static u64 amdgpu_vram_mgr_vis_size(struct amdgpu_device *adev, 197 struct drm_mm_node *node) 198 { 199 uint64_t start = node->start << PAGE_SHIFT; 200 uint64_t end = (node->size + node->start) << PAGE_SHIFT; 201 202 if (start >= adev->gmc.visible_vram_size) 203 return 0; 204 205 return (end > adev->gmc.visible_vram_size ? 206 adev->gmc.visible_vram_size : end) - start; 207 } 208 209 /** 210 * amdgpu_vram_mgr_bo_visible_size - CPU visible BO size 211 * 212 * @bo: &amdgpu_bo buffer object (must be in VRAM) 213 * 214 * Returns: 215 * How much of the given &amdgpu_bo buffer object lies in CPU visible VRAM. 216 */ 217 u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo) 218 { 219 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 220 struct ttm_resource *res = bo->tbo.resource; 221 unsigned pages = res->num_pages; 222 struct drm_mm_node *mm; 223 u64 usage; 224 225 if (amdgpu_gmc_vram_full_visible(&adev->gmc)) 226 return amdgpu_bo_size(bo); 227 228 if (res->start >= adev->gmc.visible_vram_size >> PAGE_SHIFT) 229 return 0; 230 231 mm = &container_of(res, struct ttm_range_mgr_node, base)->mm_nodes[0]; 232 for (usage = 0; pages; pages -= mm->size, mm++) 233 usage += amdgpu_vram_mgr_vis_size(adev, mm); 234 235 return usage; 236 } 237 238 /* Commit the reservation of VRAM pages */ 239 static void amdgpu_vram_mgr_do_reserve(struct ttm_resource_manager *man) 240 { 241 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man); 242 struct amdgpu_device *adev = to_amdgpu_device(mgr); 243 struct drm_mm *mm = &mgr->mm; 244 struct amdgpu_vram_reservation *rsv, *temp; 245 uint64_t vis_usage; 246 247 list_for_each_entry_safe(rsv, temp, &mgr->reservations_pending, node) { 248 if (drm_mm_reserve_node(mm, &rsv->mm_node)) 249 continue; 250 251 dev_dbg(adev->dev, "Reservation 0x%llx - %lld, Succeeded\n", 252 rsv->mm_node.start, rsv->mm_node.size); 253 254 vis_usage = amdgpu_vram_mgr_vis_size(adev, &rsv->mm_node); 255 atomic64_add(vis_usage, &mgr->vis_usage); 256 atomic64_add(rsv->mm_node.size << PAGE_SHIFT, &mgr->usage); 257 list_move(&rsv->node, &mgr->reserved_pages); 258 } 259 } 260 261 /** 262 * amdgpu_vram_mgr_reserve_range - Reserve a range from VRAM 263 * 264 * @mgr: amdgpu_vram_mgr pointer 265 * @start: start address of the range in VRAM 266 * @size: size of the range 267 * 268 * Reserve memory from start address with the specified size in VRAM 269 */ 270 int amdgpu_vram_mgr_reserve_range(struct amdgpu_vram_mgr *mgr, 271 uint64_t start, uint64_t size) 272 { 273 struct amdgpu_vram_reservation *rsv; 274 275 rsv = kzalloc(sizeof(*rsv), GFP_KERNEL); 276 if (!rsv) 277 return -ENOMEM; 278 279 INIT_LIST_HEAD(&rsv->node); 280 rsv->mm_node.start = start >> PAGE_SHIFT; 281 rsv->mm_node.size = size >> PAGE_SHIFT; 282 283 spin_lock(&mgr->lock); 284 list_add_tail(&mgr->reservations_pending, &rsv->node); 285 amdgpu_vram_mgr_do_reserve(&mgr->manager); 286 spin_unlock(&mgr->lock); 287 288 return 0; 289 } 290 291 /** 292 * amdgpu_vram_mgr_query_page_status - query the reservation status 293 * 294 * @mgr: amdgpu_vram_mgr pointer 295 * @start: start address of a page in VRAM 296 * 297 * Returns: 298 * -EBUSY: the page is still hold and in pending list 299 * 0: the page has been reserved 300 * -ENOENT: the input page is not a reservation 301 */ 302 int amdgpu_vram_mgr_query_page_status(struct amdgpu_vram_mgr *mgr, 303 uint64_t start) 304 { 305 struct amdgpu_vram_reservation *rsv; 306 int ret; 307 308 spin_lock(&mgr->lock); 309 310 list_for_each_entry(rsv, &mgr->reservations_pending, node) { 311 if ((rsv->mm_node.start <= start) && 312 (start < (rsv->mm_node.start + rsv->mm_node.size))) { 313 ret = -EBUSY; 314 goto out; 315 } 316 } 317 318 list_for_each_entry(rsv, &mgr->reserved_pages, node) { 319 if ((rsv->mm_node.start <= start) && 320 (start < (rsv->mm_node.start + rsv->mm_node.size))) { 321 ret = 0; 322 goto out; 323 } 324 } 325 326 ret = -ENOENT; 327 out: 328 spin_unlock(&mgr->lock); 329 return ret; 330 } 331 332 /** 333 * amdgpu_vram_mgr_virt_start - update virtual start address 334 * 335 * @mem: ttm_resource to update 336 * @node: just allocated node 337 * 338 * Calculate a virtual BO start address to easily check if everything is CPU 339 * accessible. 340 */ 341 static void amdgpu_vram_mgr_virt_start(struct ttm_resource *mem, 342 struct drm_mm_node *node) 343 { 344 unsigned long start; 345 346 start = node->start + node->size; 347 if (start > mem->num_pages) 348 start -= mem->num_pages; 349 else 350 start = 0; 351 mem->start = max(mem->start, start); 352 } 353 354 /** 355 * amdgpu_vram_mgr_new - allocate new ranges 356 * 357 * @man: TTM memory type manager 358 * @tbo: TTM BO we need this range for 359 * @place: placement flags and restrictions 360 * @res: the resulting mem object 361 * 362 * Allocate VRAM for the given BO. 363 */ 364 static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man, 365 struct ttm_buffer_object *tbo, 366 const struct ttm_place *place, 367 struct ttm_resource **res) 368 { 369 unsigned long lpfn, num_nodes, pages_per_node, pages_left, pages; 370 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man); 371 struct amdgpu_device *adev = to_amdgpu_device(mgr); 372 uint64_t vis_usage = 0, mem_bytes, max_bytes; 373 struct ttm_range_mgr_node *node; 374 struct drm_mm *mm = &mgr->mm; 375 enum drm_mm_insert_mode mode; 376 unsigned i; 377 int r; 378 379 lpfn = place->lpfn; 380 if (!lpfn) 381 lpfn = man->size; 382 383 max_bytes = adev->gmc.mc_vram_size; 384 if (tbo->type != ttm_bo_type_kernel) 385 max_bytes -= AMDGPU_VM_RESERVED_VRAM; 386 387 /* bail out quickly if there's likely not enough VRAM for this BO */ 388 mem_bytes = tbo->base.size; 389 if (atomic64_add_return(mem_bytes, &mgr->usage) > max_bytes) { 390 r = -ENOSPC; 391 goto error_sub; 392 } 393 394 if (place->flags & TTM_PL_FLAG_CONTIGUOUS) { 395 pages_per_node = ~0ul; 396 num_nodes = 1; 397 } else { 398 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 399 pages_per_node = HPAGE_PMD_NR; 400 #else 401 /* default to 2MB */ 402 pages_per_node = 2UL << (20UL - PAGE_SHIFT); 403 #endif 404 pages_per_node = max_t(uint32_t, pages_per_node, 405 tbo->page_alignment); 406 num_nodes = DIV_ROUND_UP_ULL(PFN_UP(mem_bytes), pages_per_node); 407 } 408 409 node = kvmalloc(struct_size(node, mm_nodes, num_nodes), 410 GFP_KERNEL | __GFP_ZERO); 411 if (!node) { 412 r = -ENOMEM; 413 goto error_sub; 414 } 415 416 ttm_resource_init(tbo, place, &node->base); 417 418 mode = DRM_MM_INSERT_BEST; 419 if (place->flags & TTM_PL_FLAG_TOPDOWN) 420 mode = DRM_MM_INSERT_HIGH; 421 422 pages_left = node->base.num_pages; 423 424 /* Limit maximum size to 2GB due to SG table limitations */ 425 pages = min(pages_left, 2UL << (30 - PAGE_SHIFT)); 426 427 i = 0; 428 spin_lock(&mgr->lock); 429 while (pages_left) { 430 uint32_t alignment = tbo->page_alignment; 431 432 if (pages >= pages_per_node) 433 alignment = pages_per_node; 434 435 r = drm_mm_insert_node_in_range(mm, &node->mm_nodes[i], pages, 436 alignment, 0, place->fpfn, 437 lpfn, mode); 438 if (unlikely(r)) { 439 if (pages > pages_per_node) { 440 if (is_power_of_2(pages)) 441 pages = pages / 2; 442 else 443 pages = rounddown_pow_of_two(pages); 444 continue; 445 } 446 goto error_free; 447 } 448 449 vis_usage += amdgpu_vram_mgr_vis_size(adev, &node->mm_nodes[i]); 450 amdgpu_vram_mgr_virt_start(&node->base, &node->mm_nodes[i]); 451 pages_left -= pages; 452 ++i; 453 454 if (pages > pages_left) 455 pages = pages_left; 456 } 457 spin_unlock(&mgr->lock); 458 459 if (i == 1) 460 node->base.placement |= TTM_PL_FLAG_CONTIGUOUS; 461 462 if (adev->gmc.xgmi.connected_to_cpu) 463 node->base.bus.caching = ttm_cached; 464 else 465 node->base.bus.caching = ttm_write_combined; 466 467 atomic64_add(vis_usage, &mgr->vis_usage); 468 *res = &node->base; 469 return 0; 470 471 error_free: 472 while (i--) 473 drm_mm_remove_node(&node->mm_nodes[i]); 474 spin_unlock(&mgr->lock); 475 kvfree(node); 476 477 error_sub: 478 atomic64_sub(mem_bytes, &mgr->usage); 479 return r; 480 } 481 482 /** 483 * amdgpu_vram_mgr_del - free ranges 484 * 485 * @man: TTM memory type manager 486 * @res: TTM memory object 487 * 488 * Free the allocated VRAM again. 489 */ 490 static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man, 491 struct ttm_resource *res) 492 { 493 struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res); 494 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man); 495 struct amdgpu_device *adev = to_amdgpu_device(mgr); 496 uint64_t usage = 0, vis_usage = 0; 497 unsigned i, pages; 498 499 spin_lock(&mgr->lock); 500 for (i = 0, pages = res->num_pages; pages; 501 pages -= node->mm_nodes[i].size, ++i) { 502 struct drm_mm_node *mm = &node->mm_nodes[i]; 503 504 drm_mm_remove_node(mm); 505 usage += mm->size << PAGE_SHIFT; 506 vis_usage += amdgpu_vram_mgr_vis_size(adev, mm); 507 } 508 amdgpu_vram_mgr_do_reserve(man); 509 spin_unlock(&mgr->lock); 510 511 atomic64_sub(usage, &mgr->usage); 512 atomic64_sub(vis_usage, &mgr->vis_usage); 513 514 kvfree(node); 515 } 516 517 /** 518 * amdgpu_vram_mgr_alloc_sgt - allocate and fill a sg table 519 * 520 * @adev: amdgpu device pointer 521 * @res: TTM memory object 522 * @offset: byte offset from the base of VRAM BO 523 * @length: number of bytes to export in sg_table 524 * @dev: the other device 525 * @dir: dma direction 526 * @sgt: resulting sg table 527 * 528 * Allocate and fill a sg table from a VRAM allocation. 529 */ 530 int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev, 531 struct ttm_resource *res, 532 u64 offset, u64 length, 533 struct device *dev, 534 enum dma_data_direction dir, 535 struct sg_table **sgt) 536 { 537 struct amdgpu_res_cursor cursor; 538 struct scatterlist *sg; 539 int num_entries = 0; 540 int i, r; 541 542 *sgt = kmalloc(sizeof(**sgt), GFP_KERNEL); 543 if (!*sgt) 544 return -ENOMEM; 545 546 /* Determine the number of DRM_MM nodes to export */ 547 amdgpu_res_first(res, offset, length, &cursor); 548 while (cursor.remaining) { 549 num_entries++; 550 amdgpu_res_next(&cursor, cursor.size); 551 } 552 553 r = sg_alloc_table(*sgt, num_entries, GFP_KERNEL); 554 if (r) 555 goto error_free; 556 557 /* Initialize scatterlist nodes of sg_table */ 558 for_each_sgtable_sg((*sgt), sg, i) 559 sg->length = 0; 560 561 /* 562 * Walk down DRM_MM nodes to populate scatterlist nodes 563 * @note: Use iterator api to get first the DRM_MM node 564 * and the number of bytes from it. Access the following 565 * DRM_MM node(s) if more buffer needs to exported 566 */ 567 amdgpu_res_first(res, offset, length, &cursor); 568 for_each_sgtable_sg((*sgt), sg, i) { 569 phys_addr_t phys = cursor.start + adev->gmc.aper_base; 570 size_t size = cursor.size; 571 dma_addr_t addr; 572 573 addr = dma_map_resource(dev, phys, size, dir, 574 DMA_ATTR_SKIP_CPU_SYNC); 575 r = dma_mapping_error(dev, addr); 576 if (r) 577 goto error_unmap; 578 579 sg_set_page(sg, NULL, size, 0); 580 sg_dma_address(sg) = addr; 581 sg_dma_len(sg) = size; 582 583 amdgpu_res_next(&cursor, cursor.size); 584 } 585 586 return 0; 587 588 error_unmap: 589 for_each_sgtable_sg((*sgt), sg, i) { 590 if (!sg->length) 591 continue; 592 593 dma_unmap_resource(dev, sg->dma_address, 594 sg->length, dir, 595 DMA_ATTR_SKIP_CPU_SYNC); 596 } 597 sg_free_table(*sgt); 598 599 error_free: 600 kfree(*sgt); 601 return r; 602 } 603 604 /** 605 * amdgpu_vram_mgr_free_sgt - allocate and fill a sg table 606 * 607 * @dev: device pointer 608 * @dir: data direction of resource to unmap 609 * @sgt: sg table to free 610 * 611 * Free a previously allocate sg table. 612 */ 613 void amdgpu_vram_mgr_free_sgt(struct device *dev, 614 enum dma_data_direction dir, 615 struct sg_table *sgt) 616 { 617 struct scatterlist *sg; 618 int i; 619 620 for_each_sgtable_sg(sgt, sg, i) 621 dma_unmap_resource(dev, sg->dma_address, 622 sg->length, dir, 623 DMA_ATTR_SKIP_CPU_SYNC); 624 sg_free_table(sgt); 625 kfree(sgt); 626 } 627 628 /** 629 * amdgpu_vram_mgr_usage - how many bytes are used in this domain 630 * 631 * @mgr: amdgpu_vram_mgr pointer 632 * 633 * Returns how many bytes are used in this domain. 634 */ 635 uint64_t amdgpu_vram_mgr_usage(struct amdgpu_vram_mgr *mgr) 636 { 637 return atomic64_read(&mgr->usage); 638 } 639 640 /** 641 * amdgpu_vram_mgr_vis_usage - how many bytes are used in the visible part 642 * 643 * @mgr: amdgpu_vram_mgr pointer 644 * 645 * Returns how many bytes are used in the visible part of VRAM 646 */ 647 uint64_t amdgpu_vram_mgr_vis_usage(struct amdgpu_vram_mgr *mgr) 648 { 649 return atomic64_read(&mgr->vis_usage); 650 } 651 652 /** 653 * amdgpu_vram_mgr_debug - dump VRAM table 654 * 655 * @man: TTM memory type manager 656 * @printer: DRM printer to use 657 * 658 * Dump the table content using printk. 659 */ 660 static void amdgpu_vram_mgr_debug(struct ttm_resource_manager *man, 661 struct drm_printer *printer) 662 { 663 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man); 664 665 spin_lock(&mgr->lock); 666 drm_mm_print(&mgr->mm, printer); 667 spin_unlock(&mgr->lock); 668 669 drm_printf(printer, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n", 670 man->size, amdgpu_vram_mgr_usage(mgr) >> 20, 671 amdgpu_vram_mgr_vis_usage(mgr) >> 20); 672 } 673 674 static const struct ttm_resource_manager_func amdgpu_vram_mgr_func = { 675 .alloc = amdgpu_vram_mgr_new, 676 .free = amdgpu_vram_mgr_del, 677 .debug = amdgpu_vram_mgr_debug 678 }; 679 680 /** 681 * amdgpu_vram_mgr_init - init VRAM manager and DRM MM 682 * 683 * @adev: amdgpu_device pointer 684 * 685 * Allocate and initialize the VRAM manager. 686 */ 687 int amdgpu_vram_mgr_init(struct amdgpu_device *adev) 688 { 689 struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr; 690 struct ttm_resource_manager *man = &mgr->manager; 691 692 ttm_resource_manager_init(man, adev->gmc.real_vram_size >> PAGE_SHIFT); 693 694 man->func = &amdgpu_vram_mgr_func; 695 696 drm_mm_init(&mgr->mm, 0, man->size); 697 spin_lock_init(&mgr->lock); 698 INIT_LIST_HEAD(&mgr->reservations_pending); 699 INIT_LIST_HEAD(&mgr->reserved_pages); 700 701 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, &mgr->manager); 702 ttm_resource_manager_set_used(man, true); 703 return 0; 704 } 705 706 /** 707 * amdgpu_vram_mgr_fini - free and destroy VRAM manager 708 * 709 * @adev: amdgpu_device pointer 710 * 711 * Destroy and free the VRAM manager, returns -EBUSY if ranges are still 712 * allocated inside it. 713 */ 714 void amdgpu_vram_mgr_fini(struct amdgpu_device *adev) 715 { 716 struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr; 717 struct ttm_resource_manager *man = &mgr->manager; 718 int ret; 719 struct amdgpu_vram_reservation *rsv, *temp; 720 721 ttm_resource_manager_set_used(man, false); 722 723 ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man); 724 if (ret) 725 return; 726 727 spin_lock(&mgr->lock); 728 list_for_each_entry_safe(rsv, temp, &mgr->reservations_pending, node) 729 kfree(rsv); 730 731 list_for_each_entry_safe(rsv, temp, &mgr->reserved_pages, node) { 732 drm_mm_remove_node(&rsv->mm_node); 733 kfree(rsv); 734 } 735 drm_mm_takedown(&mgr->mm); 736 spin_unlock(&mgr->lock); 737 738 ttm_resource_manager_cleanup(man); 739 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, NULL); 740 } 741