2 * Copyright 2016 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Christian König
25 #include <linux/dma-mapping.h>
27 #include "amdgpu_vm.h"
28 #include "amdgpu_res_cursor.h"
29 #include "amdgpu_atomfirmware.h"
32 struct amdgpu_vram_reservation {
33 struct list_head node;
34 struct drm_mm_node mm_node;
37 static inline struct amdgpu_vram_mgr *
38 to_vram_mgr(struct ttm_resource_manager *man)
40 return container_of(man, struct amdgpu_vram_mgr, manager);
43 static inline struct amdgpu_device *
44 to_amdgpu_device(struct amdgpu_vram_mgr *mgr)
46 return container_of(mgr, struct amdgpu_device, mman.vram_mgr);
50 * DOC: mem_info_vram_total
52 * The amdgpu driver provides a sysfs API for reporting current total VRAM
53 * available on the device
54 * The file mem_info_vram_total is used for this and returns the total
55 * amount of VRAM in bytes
57 static ssize_t amdgpu_mem_info_vram_total_show(struct device *dev,
58 struct device_attribute *attr, char *buf)
60 struct drm_device *ddev = dev_get_drvdata(dev);
61 struct amdgpu_device *adev = drm_to_adev(ddev);
63 return sysfs_emit(buf, "%llu\n", adev->gmc.real_vram_size);
67 * DOC: mem_info_vis_vram_total
69 * The amdgpu driver provides a sysfs API for reporting current total
70 * visible VRAM available on the device
71 * The file mem_info_vis_vram_total is used for this and returns the total
72 * amount of visible VRAM in bytes
74 static ssize_t amdgpu_mem_info_vis_vram_total_show(struct device *dev,
75 struct device_attribute *attr, char *buf)
77 struct drm_device *ddev = dev_get_drvdata(dev);
78 struct amdgpu_device *adev = drm_to_adev(ddev);
80 return sysfs_emit(buf, "%llu\n", adev->gmc.visible_vram_size);
84 * DOC: mem_info_vram_used
86 * The amdgpu driver provides a sysfs API for reporting current total VRAM
87 * available on the device
88 * The file mem_info_vram_used is used for this and returns the total
89 * amount of currently used VRAM in bytes
91 static ssize_t amdgpu_mem_info_vram_used_show(struct device *dev,
92 struct device_attribute *attr,
95 struct drm_device *ddev = dev_get_drvdata(dev);
96 struct amdgpu_device *adev = drm_to_adev(ddev);
97 struct ttm_resource_manager *man;
99 man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
100 return sysfs_emit(buf, "%llu\n", amdgpu_vram_mgr_usage(man));
104 * DOC: mem_info_vis_vram_used
106 * The amdgpu driver provides a sysfs API for reporting current total of
108 * The file mem_info_vis_vram_used is used for this and returns the total
109 * amount of currently used visible VRAM in bytes
111 static ssize_t amdgpu_mem_info_vis_vram_used_show(struct device *dev,
112 struct device_attribute *attr,
115 struct drm_device *ddev = dev_get_drvdata(dev);
116 struct amdgpu_device *adev = drm_to_adev(ddev);
117 struct ttm_resource_manager *man;
119 man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
120 return sysfs_emit(buf, "%llu\n", amdgpu_vram_mgr_vis_usage(man));
124 * DOC: mem_info_vram_vendor
126 * The amdgpu driver provides a sysfs API for reporting the vendor of the
128 * The file mem_info_vram_vendor is used for this and returns the name of the
131 static ssize_t amdgpu_mem_info_vram_vendor(struct device *dev,
132 struct device_attribute *attr,
135 struct drm_device *ddev = dev_get_drvdata(dev);
136 struct amdgpu_device *adev = drm_to_adev(ddev);
138 switch (adev->gmc.vram_vendor) {
140 return sysfs_emit(buf, "samsung\n");
142 return sysfs_emit(buf, "infineon\n");
144 return sysfs_emit(buf, "elpida\n");
146 return sysfs_emit(buf, "etron\n");
148 return sysfs_emit(buf, "nanya\n");
150 return sysfs_emit(buf, "hynix\n");
152 return sysfs_emit(buf, "mosel\n");
154 return sysfs_emit(buf, "winbond\n");
156 return sysfs_emit(buf, "esmt\n");
158 return sysfs_emit(buf, "micron\n");
160 return sysfs_emit(buf, "unknown\n");
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);
175 static const 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,
185 * amdgpu_vram_mgr_vis_size - Calculate visible node size
187 * @adev: amdgpu_device pointer
188 * @node: MM node structure
190 * Calculate how many bytes of the MM node are inside visible VRAM
192 static u64 amdgpu_vram_mgr_vis_size(struct amdgpu_device *adev,
193 struct drm_mm_node *node)
195 uint64_t start = node->start << PAGE_SHIFT;
196 uint64_t end = (node->size + node->start) << PAGE_SHIFT;
198 if (start >= adev->gmc.visible_vram_size)
201 return (end > adev->gmc.visible_vram_size ?
202 adev->gmc.visible_vram_size : end) - start;
206 * amdgpu_vram_mgr_bo_visible_size - CPU visible BO size
208 * @bo: &amdgpu_bo buffer object (must be in VRAM)
211 * How much of the given &amdgpu_bo buffer object lies in CPU visible VRAM.
213 u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo)
215 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
216 struct ttm_resource *mem = &bo->tbo.mem;
217 struct drm_mm_node *nodes = mem->mm_node;
218 unsigned pages = mem->num_pages;
221 if (amdgpu_gmc_vram_full_visible(&adev->gmc))
222 return amdgpu_bo_size(bo);
224 if (mem->start >= adev->gmc.visible_vram_size >> PAGE_SHIFT)
227 for (usage = 0; nodes && pages; pages -= nodes->size, nodes++)
228 usage += amdgpu_vram_mgr_vis_size(adev, nodes);
233 /* Commit the reservation of VRAM pages */
234 static void amdgpu_vram_mgr_do_reserve(struct ttm_resource_manager *man)
236 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
237 struct amdgpu_device *adev = to_amdgpu_device(mgr);
238 struct drm_mm *mm = &mgr->mm;
239 struct amdgpu_vram_reservation *rsv, *temp;
242 list_for_each_entry_safe(rsv, temp, &mgr->reservations_pending, node) {
243 if (drm_mm_reserve_node(mm, &rsv->mm_node))
246 dev_dbg(adev->dev, "Reservation 0x%llx - %lld, Succeeded\n",
247 rsv->mm_node.start, rsv->mm_node.size);
249 vis_usage = amdgpu_vram_mgr_vis_size(adev, &rsv->mm_node);
250 atomic64_add(vis_usage, &mgr->vis_usage);
251 atomic64_add(rsv->mm_node.size << PAGE_SHIFT, &mgr->usage);
252 list_move(&rsv->node, &mgr->reserved_pages);
257 * amdgpu_vram_mgr_reserve_range - Reserve a range from VRAM
259 * @man: TTM memory type manager
260 * @start: start address of the range in VRAM
261 * @size: size of the range
263 * Reserve memory from start addess with the specified size in VRAM
265 int amdgpu_vram_mgr_reserve_range(struct ttm_resource_manager *man,
266 uint64_t start, uint64_t size)
268 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
269 struct amdgpu_vram_reservation *rsv;
271 rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
275 INIT_LIST_HEAD(&rsv->node);
276 rsv->mm_node.start = start >> PAGE_SHIFT;
277 rsv->mm_node.size = size >> PAGE_SHIFT;
279 spin_lock(&mgr->lock);
280 list_add_tail(&mgr->reservations_pending, &rsv->node);
281 amdgpu_vram_mgr_do_reserve(man);
282 spin_unlock(&mgr->lock);
288 * amdgpu_vram_mgr_query_page_status - query the reservation status
290 * @man: TTM memory type manager
291 * @start: start address of a page in VRAM
294 * -EBUSY: the page is still hold and in pending list
295 * 0: the page has been reserved
296 * -ENOENT: the input page is not a reservation
298 int amdgpu_vram_mgr_query_page_status(struct ttm_resource_manager *man,
301 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
302 struct amdgpu_vram_reservation *rsv;
305 spin_lock(&mgr->lock);
307 list_for_each_entry(rsv, &mgr->reservations_pending, node) {
308 if ((rsv->mm_node.start <= start) &&
309 (start < (rsv->mm_node.start + rsv->mm_node.size))) {
315 list_for_each_entry(rsv, &mgr->reserved_pages, node) {
316 if ((rsv->mm_node.start <= start) &&
317 (start < (rsv->mm_node.start + rsv->mm_node.size))) {
325 spin_unlock(&mgr->lock);
330 * amdgpu_vram_mgr_virt_start - update virtual start address
332 * @mem: ttm_resource to update
333 * @node: just allocated node
335 * Calculate a virtual BO start address to easily check if everything is CPU
338 static void amdgpu_vram_mgr_virt_start(struct ttm_resource *mem,
339 struct drm_mm_node *node)
343 start = node->start + node->size;
344 if (start > mem->num_pages)
345 start -= mem->num_pages;
348 mem->start = max(mem->start, start);
352 * amdgpu_vram_mgr_new - allocate new ranges
354 * @man: TTM memory type manager
355 * @tbo: TTM BO we need this range for
356 * @place: placement flags and restrictions
357 * @mem: the resulting mem object
359 * Allocate VRAM for the given BO.
361 static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
362 struct ttm_buffer_object *tbo,
363 const struct ttm_place *place,
364 struct ttm_resource *mem)
366 unsigned long lpfn, num_nodes, pages_per_node, pages_left, pages;
367 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
368 struct amdgpu_device *adev = to_amdgpu_device(mgr);
369 uint64_t vis_usage = 0, mem_bytes, max_bytes;
370 struct drm_mm *mm = &mgr->mm;
371 enum drm_mm_insert_mode mode;
372 struct drm_mm_node *nodes;
380 max_bytes = adev->gmc.mc_vram_size;
381 if (tbo->type != ttm_bo_type_kernel)
382 max_bytes -= AMDGPU_VM_RESERVED_VRAM;
384 /* bail out quickly if there's likely not enough VRAM for this BO */
385 mem_bytes = (u64)mem->num_pages << PAGE_SHIFT;
386 if (atomic64_add_return(mem_bytes, &mgr->usage) > max_bytes) {
387 atomic64_sub(mem_bytes, &mgr->usage);
391 if (place->flags & TTM_PL_FLAG_CONTIGUOUS) {
392 pages_per_node = ~0ul;
395 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
396 pages_per_node = HPAGE_PMD_NR;
399 pages_per_node = 2UL << (20UL - PAGE_SHIFT);
401 pages_per_node = max_t(uint32_t, pages_per_node,
402 tbo->page_alignment);
403 num_nodes = DIV_ROUND_UP(mem->num_pages, pages_per_node);
406 nodes = kvmalloc_array((uint32_t)num_nodes, sizeof(*nodes),
407 GFP_KERNEL | __GFP_ZERO);
409 atomic64_sub(mem_bytes, &mgr->usage);
413 mode = DRM_MM_INSERT_BEST;
414 if (place->flags & TTM_PL_FLAG_TOPDOWN)
415 mode = DRM_MM_INSERT_HIGH;
418 pages_left = mem->num_pages;
420 /* Limit maximum size to 2GB due to SG table limitations */
421 pages = min(pages_left, 2UL << (30 - PAGE_SHIFT));
424 spin_lock(&mgr->lock);
426 uint32_t alignment = tbo->page_alignment;
428 if (pages >= pages_per_node)
429 alignment = pages_per_node;
431 r = drm_mm_insert_node_in_range(mm, &nodes[i], pages, alignment,
432 0, place->fpfn, lpfn, mode);
434 if (pages > pages_per_node) {
435 if (is_power_of_2(pages))
438 pages = rounddown_pow_of_two(pages);
444 vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
445 amdgpu_vram_mgr_virt_start(mem, &nodes[i]);
449 if (pages > pages_left)
452 spin_unlock(&mgr->lock);
455 mem->placement |= TTM_PL_FLAG_CONTIGUOUS;
457 atomic64_add(vis_usage, &mgr->vis_usage);
458 mem->mm_node = nodes;
463 drm_mm_remove_node(&nodes[i]);
464 spin_unlock(&mgr->lock);
465 atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
472 * amdgpu_vram_mgr_del - free ranges
474 * @man: TTM memory type manager
475 * @mem: TTM memory object
477 * Free the allocated VRAM again.
479 static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man,
480 struct ttm_resource *mem)
482 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
483 struct amdgpu_device *adev = to_amdgpu_device(mgr);
484 struct drm_mm_node *nodes = mem->mm_node;
485 uint64_t usage = 0, vis_usage = 0;
486 unsigned pages = mem->num_pages;
491 spin_lock(&mgr->lock);
493 pages -= nodes->size;
494 drm_mm_remove_node(nodes);
495 usage += nodes->size << PAGE_SHIFT;
496 vis_usage += amdgpu_vram_mgr_vis_size(adev, nodes);
499 amdgpu_vram_mgr_do_reserve(man);
500 spin_unlock(&mgr->lock);
502 atomic64_sub(usage, &mgr->usage);
503 atomic64_sub(vis_usage, &mgr->vis_usage);
505 kvfree(mem->mm_node);
510 * amdgpu_vram_mgr_alloc_sgt - allocate and fill a sg table
512 * @adev: amdgpu device pointer
513 * @mem: TTM memory object
514 * @offset: byte offset from the base of VRAM BO
515 * @length: number of bytes to export in sg_table
516 * @dev: the other device
517 * @dir: dma direction
518 * @sgt: resulting sg table
520 * Allocate and fill a sg table from a VRAM allocation.
522 int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
523 struct ttm_resource *mem,
524 u64 offset, u64 length,
526 enum dma_data_direction dir,
527 struct sg_table **sgt)
529 struct amdgpu_res_cursor cursor;
530 struct scatterlist *sg;
534 *sgt = kmalloc(sizeof(**sgt), GFP_KERNEL);
538 /* Determine the number of DRM_MM nodes to export */
539 amdgpu_res_first(mem, offset, length, &cursor);
540 while (cursor.remaining) {
542 amdgpu_res_next(&cursor, cursor.size);
545 r = sg_alloc_table(*sgt, num_entries, GFP_KERNEL);
549 /* Initialize scatterlist nodes of sg_table */
550 for_each_sgtable_sg((*sgt), sg, i)
554 * Walk down DRM_MM nodes to populate scatterlist nodes
555 * @note: Use iterator api to get first the DRM_MM node
556 * and the number of bytes from it. Access the following
557 * DRM_MM node(s) if more buffer needs to exported
559 amdgpu_res_first(mem, offset, length, &cursor);
560 for_each_sgtable_sg((*sgt), sg, i) {
561 phys_addr_t phys = cursor.start + adev->gmc.aper_base;
562 size_t size = cursor.size;
565 addr = dma_map_resource(dev, phys, size, dir,
566 DMA_ATTR_SKIP_CPU_SYNC);
567 r = dma_mapping_error(dev, addr);
571 sg_set_page(sg, NULL, size, 0);
572 sg_dma_address(sg) = addr;
573 sg_dma_len(sg) = size;
575 amdgpu_res_next(&cursor, cursor.size);
581 for_each_sgtable_sg((*sgt), sg, i) {
585 dma_unmap_resource(dev, sg->dma_address,
587 DMA_ATTR_SKIP_CPU_SYNC);
597 * amdgpu_vram_mgr_free_sgt - allocate and fill a sg table
599 * @dev: device pointer
600 * @dir: data direction of resource to unmap
601 * @sgt: sg table to free
603 * Free a previously allocate sg table.
605 void amdgpu_vram_mgr_free_sgt(struct device *dev,
606 enum dma_data_direction dir,
607 struct sg_table *sgt)
609 struct scatterlist *sg;
612 for_each_sgtable_sg(sgt, sg, i)
613 dma_unmap_resource(dev, sg->dma_address,
615 DMA_ATTR_SKIP_CPU_SYNC);
621 * amdgpu_vram_mgr_usage - how many bytes are used in this domain
623 * @man: TTM memory type manager
625 * Returns how many bytes are used in this domain.
627 uint64_t amdgpu_vram_mgr_usage(struct ttm_resource_manager *man)
629 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
631 return atomic64_read(&mgr->usage);
635 * amdgpu_vram_mgr_vis_usage - how many bytes are used in the visible part
637 * @man: TTM memory type manager
639 * Returns how many bytes are used in the visible part of VRAM
641 uint64_t amdgpu_vram_mgr_vis_usage(struct ttm_resource_manager *man)
643 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
645 return atomic64_read(&mgr->vis_usage);
649 * amdgpu_vram_mgr_debug - dump VRAM table
651 * @man: TTM memory type manager
652 * @printer: DRM printer to use
654 * Dump the table content using printk.
656 static void amdgpu_vram_mgr_debug(struct ttm_resource_manager *man,
657 struct drm_printer *printer)
659 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
661 spin_lock(&mgr->lock);
662 drm_mm_print(&mgr->mm, printer);
663 spin_unlock(&mgr->lock);
665 drm_printf(printer, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
666 man->size, amdgpu_vram_mgr_usage(man) >> 20,
667 amdgpu_vram_mgr_vis_usage(man) >> 20);
670 static const struct ttm_resource_manager_func amdgpu_vram_mgr_func = {
671 .alloc = amdgpu_vram_mgr_new,
672 .free = amdgpu_vram_mgr_del,
673 .debug = amdgpu_vram_mgr_debug
677 * amdgpu_vram_mgr_init - init VRAM manager and DRM MM
679 * @adev: amdgpu_device pointer
681 * Allocate and initialize the VRAM manager.
683 int amdgpu_vram_mgr_init(struct amdgpu_device *adev)
685 struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;
686 struct ttm_resource_manager *man = &mgr->manager;
689 ttm_resource_manager_init(man, adev->gmc.real_vram_size >> PAGE_SHIFT);
691 man->func = &amdgpu_vram_mgr_func;
693 drm_mm_init(&mgr->mm, 0, man->size);
694 spin_lock_init(&mgr->lock);
695 INIT_LIST_HEAD(&mgr->reservations_pending);
696 INIT_LIST_HEAD(&mgr->reserved_pages);
698 /* Add the two VRAM-related sysfs files */
699 ret = sysfs_create_files(&adev->dev->kobj, amdgpu_vram_mgr_attributes);
701 DRM_ERROR("Failed to register sysfs\n");
703 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, &mgr->manager);
704 ttm_resource_manager_set_used(man, true);
709 * amdgpu_vram_mgr_fini - free and destroy VRAM manager
711 * @adev: amdgpu_device pointer
713 * Destroy and free the VRAM manager, returns -EBUSY if ranges are still
714 * allocated inside it.
716 void amdgpu_vram_mgr_fini(struct amdgpu_device *adev)
718 struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;
719 struct ttm_resource_manager *man = &mgr->manager;
721 struct amdgpu_vram_reservation *rsv, *temp;
723 ttm_resource_manager_set_used(man, false);
725 ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man);
729 spin_lock(&mgr->lock);
730 list_for_each_entry_safe(rsv, temp, &mgr->reservations_pending, node)
733 list_for_each_entry_safe(rsv, temp, &mgr->reserved_pages, node) {
734 drm_mm_remove_node(&rsv->mm_node);
737 drm_mm_takedown(&mgr->mm);
738 spin_unlock(&mgr->lock);
740 sysfs_remove_files(&adev->dev->kobj, amdgpu_vram_mgr_attributes);
742 ttm_resource_manager_cleanup(man);
743 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, NULL);