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 static inline struct amdgpu_vram_mgr *
33 to_vram_mgr(struct ttm_resource_manager *man)
35 return container_of(man, struct amdgpu_vram_mgr, manager);
38 static inline struct amdgpu_device *
39 to_amdgpu_device(struct amdgpu_vram_mgr *mgr)
41 return container_of(mgr, struct amdgpu_device, mman.vram_mgr);
45 * DOC: mem_info_vram_total
47 * The amdgpu driver provides a sysfs API for reporting current total VRAM
48 * available on the device
49 * The file mem_info_vram_total is used for this and returns the total
50 * amount of VRAM in bytes
52 static ssize_t amdgpu_mem_info_vram_total_show(struct device *dev,
53 struct device_attribute *attr, char *buf)
55 struct drm_device *ddev = dev_get_drvdata(dev);
56 struct amdgpu_device *adev = drm_to_adev(ddev);
58 return sysfs_emit(buf, "%llu\n", adev->gmc.real_vram_size);
62 * DOC: mem_info_vis_vram_total
64 * The amdgpu driver provides a sysfs API for reporting current total
65 * visible VRAM available on the device
66 * The file mem_info_vis_vram_total is used for this and returns the total
67 * amount of visible VRAM in bytes
69 static ssize_t amdgpu_mem_info_vis_vram_total_show(struct device *dev,
70 struct device_attribute *attr, char *buf)
72 struct drm_device *ddev = dev_get_drvdata(dev);
73 struct amdgpu_device *adev = drm_to_adev(ddev);
75 return sysfs_emit(buf, "%llu\n", adev->gmc.visible_vram_size);
79 * DOC: mem_info_vram_used
81 * The amdgpu driver provides a sysfs API for reporting current total VRAM
82 * available on the device
83 * The file mem_info_vram_used is used for this and returns the total
84 * amount of currently used VRAM in bytes
86 static ssize_t amdgpu_mem_info_vram_used_show(struct device *dev,
87 struct device_attribute *attr,
90 struct drm_device *ddev = dev_get_drvdata(dev);
91 struct amdgpu_device *adev = drm_to_adev(ddev);
92 struct ttm_resource_manager *man;
94 man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
95 return sysfs_emit(buf, "%llu\n", amdgpu_vram_mgr_usage(man));
99 * DOC: mem_info_vis_vram_used
101 * The amdgpu driver provides a sysfs API for reporting current total of
103 * The file mem_info_vis_vram_used is used for this and returns the total
104 * amount of currently used visible VRAM in bytes
106 static ssize_t amdgpu_mem_info_vis_vram_used_show(struct device *dev,
107 struct device_attribute *attr,
110 struct drm_device *ddev = dev_get_drvdata(dev);
111 struct amdgpu_device *adev = drm_to_adev(ddev);
112 struct ttm_resource_manager *man;
114 man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
115 return sysfs_emit(buf, "%llu\n", amdgpu_vram_mgr_vis_usage(man));
119 * DOC: mem_info_vram_vendor
121 * The amdgpu driver provides a sysfs API for reporting the vendor of the
123 * The file mem_info_vram_vendor is used for this and returns the name of the
126 static ssize_t amdgpu_mem_info_vram_vendor(struct device *dev,
127 struct device_attribute *attr,
130 struct drm_device *ddev = dev_get_drvdata(dev);
131 struct amdgpu_device *adev = drm_to_adev(ddev);
133 switch (adev->gmc.vram_vendor) {
135 return sysfs_emit(buf, "samsung\n");
137 return sysfs_emit(buf, "infineon\n");
139 return sysfs_emit(buf, "elpida\n");
141 return sysfs_emit(buf, "etron\n");
143 return sysfs_emit(buf, "nanya\n");
145 return sysfs_emit(buf, "hynix\n");
147 return sysfs_emit(buf, "mosel\n");
149 return sysfs_emit(buf, "winbond\n");
151 return sysfs_emit(buf, "esmt\n");
153 return sysfs_emit(buf, "micron\n");
155 return sysfs_emit(buf, "unknown\n");
159 static DEVICE_ATTR(mem_info_vram_total, S_IRUGO,
160 amdgpu_mem_info_vram_total_show, NULL);
161 static DEVICE_ATTR(mem_info_vis_vram_total, S_IRUGO,
162 amdgpu_mem_info_vis_vram_total_show,NULL);
163 static DEVICE_ATTR(mem_info_vram_used, S_IRUGO,
164 amdgpu_mem_info_vram_used_show, NULL);
165 static DEVICE_ATTR(mem_info_vis_vram_used, S_IRUGO,
166 amdgpu_mem_info_vis_vram_used_show, NULL);
167 static DEVICE_ATTR(mem_info_vram_vendor, S_IRUGO,
168 amdgpu_mem_info_vram_vendor, NULL);
170 static const struct attribute *amdgpu_vram_mgr_attributes[] = {
171 &dev_attr_mem_info_vram_total.attr,
172 &dev_attr_mem_info_vis_vram_total.attr,
173 &dev_attr_mem_info_vram_used.attr,
174 &dev_attr_mem_info_vis_vram_used.attr,
175 &dev_attr_mem_info_vram_vendor.attr,
180 * amdgpu_vram_mgr_vis_size - Calculate visible node size
182 * @adev: amdgpu_device pointer
183 * @node: MM node structure
185 * Calculate how many bytes of the MM node are inside visible VRAM
187 static u64 amdgpu_vram_mgr_vis_size(struct amdgpu_device *adev,
188 struct drm_mm_node *node)
190 uint64_t start = node->start << PAGE_SHIFT;
191 uint64_t end = (node->size + node->start) << PAGE_SHIFT;
193 if (start >= adev->gmc.visible_vram_size)
196 return (end > adev->gmc.visible_vram_size ?
197 adev->gmc.visible_vram_size : end) - start;
201 * amdgpu_vram_mgr_bo_visible_size - CPU visible BO size
203 * @bo: &amdgpu_bo buffer object (must be in VRAM)
206 * How much of the given &amdgpu_bo buffer object lies in CPU visible VRAM.
208 u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo)
210 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
211 struct ttm_resource *mem = &bo->tbo.mem;
212 struct drm_mm_node *nodes = mem->mm_node;
213 unsigned pages = mem->num_pages;
216 if (amdgpu_gmc_vram_full_visible(&adev->gmc))
217 return amdgpu_bo_size(bo);
219 if (mem->start >= adev->gmc.visible_vram_size >> PAGE_SHIFT)
222 for (usage = 0; nodes && pages; pages -= nodes->size, nodes++)
223 usage += amdgpu_vram_mgr_vis_size(adev, nodes);
228 /* Commit the reservation of VRAM pages */
229 static void amdgpu_vram_mgr_do_reserve(struct ttm_resource_manager *man)
231 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
232 struct amdgpu_device *adev = to_amdgpu_device(mgr);
233 struct drm_mm *mm = &mgr->mm;
234 struct amdgpu_vram_reservation *rsv, *temp;
237 list_for_each_entry_safe(rsv, temp, &mgr->reservations_pending, node) {
238 if (drm_mm_reserve_node(mm, &rsv->mm_node))
241 dev_dbg(adev->dev, "Reservation 0x%llx - %lld, Succeeded\n",
242 rsv->mm_node.start, rsv->mm_node.size);
244 vis_usage = amdgpu_vram_mgr_vis_size(adev, &rsv->mm_node);
245 atomic64_add(vis_usage, &mgr->vis_usage);
246 atomic64_add(rsv->mm_node.size << PAGE_SHIFT, &mgr->usage);
247 list_move(&rsv->node, &mgr->reserved_pages);
252 * amdgpu_vram_mgr_reserve_range - Reserve a range from VRAM
254 * @man: TTM memory type manager
255 * @start: start address of the range in VRAM
256 * @size: size of the range
258 * Reserve memory from start addess with the specified size in VRAM
260 int amdgpu_vram_mgr_reserve_range(struct ttm_resource_manager *man,
261 uint64_t start, uint64_t size)
263 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
264 struct amdgpu_vram_reservation *rsv;
266 rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
270 INIT_LIST_HEAD(&rsv->node);
271 rsv->mm_node.start = start >> PAGE_SHIFT;
272 rsv->mm_node.size = size >> PAGE_SHIFT;
274 spin_lock(&mgr->lock);
275 list_add_tail(&mgr->reservations_pending, &rsv->node);
276 amdgpu_vram_mgr_do_reserve(man);
277 spin_unlock(&mgr->lock);
283 * amdgpu_vram_mgr_query_page_status - query the reservation status
285 * @man: TTM memory type manager
286 * @start: start address of a page in VRAM
289 * -EBUSY: the page is still hold and in pending list
290 * 0: the page has been reserved
291 * -ENOENT: the input page is not a reservation
293 int amdgpu_vram_mgr_query_page_status(struct ttm_resource_manager *man,
296 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
297 struct amdgpu_vram_reservation *rsv;
300 spin_lock(&mgr->lock);
302 list_for_each_entry(rsv, &mgr->reservations_pending, node) {
303 if ((rsv->mm_node.start <= start) &&
304 (start < (rsv->mm_node.start + rsv->mm_node.size))) {
310 list_for_each_entry(rsv, &mgr->reserved_pages, node) {
311 if ((rsv->mm_node.start <= start) &&
312 (start < (rsv->mm_node.start + rsv->mm_node.size))) {
320 spin_unlock(&mgr->lock);
325 * amdgpu_vram_mgr_virt_start - update virtual start address
327 * @mem: ttm_resource to update
328 * @node: just allocated node
330 * Calculate a virtual BO start address to easily check if everything is CPU
333 static void amdgpu_vram_mgr_virt_start(struct ttm_resource *mem,
334 struct drm_mm_node *node)
338 start = node->start + node->size;
339 if (start > mem->num_pages)
340 start -= mem->num_pages;
343 mem->start = max(mem->start, start);
347 * amdgpu_vram_mgr_new - allocate new ranges
349 * @man: TTM memory type manager
350 * @tbo: TTM BO we need this range for
351 * @place: placement flags and restrictions
352 * @mem: the resulting mem object
354 * Allocate VRAM for the given BO.
356 static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
357 struct ttm_buffer_object *tbo,
358 const struct ttm_place *place,
359 struct ttm_resource *mem)
361 unsigned long lpfn, num_nodes, pages_per_node, pages_left, pages;
362 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
363 struct amdgpu_device *adev = to_amdgpu_device(mgr);
364 uint64_t vis_usage = 0, mem_bytes, max_bytes;
365 struct drm_mm *mm = &mgr->mm;
366 enum drm_mm_insert_mode mode;
367 struct drm_mm_node *nodes;
375 max_bytes = adev->gmc.mc_vram_size;
376 if (tbo->type != ttm_bo_type_kernel)
377 max_bytes -= AMDGPU_VM_RESERVED_VRAM;
379 /* bail out quickly if there's likely not enough VRAM for this BO */
380 mem_bytes = (u64)mem->num_pages << PAGE_SHIFT;
381 if (atomic64_add_return(mem_bytes, &mgr->usage) > max_bytes) {
382 atomic64_sub(mem_bytes, &mgr->usage);
386 if (place->flags & TTM_PL_FLAG_CONTIGUOUS) {
387 pages_per_node = ~0ul;
390 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
391 pages_per_node = HPAGE_PMD_NR;
394 pages_per_node = 2UL << (20UL - PAGE_SHIFT);
396 pages_per_node = max_t(uint32_t, pages_per_node,
397 tbo->page_alignment);
398 num_nodes = DIV_ROUND_UP(mem->num_pages, pages_per_node);
401 nodes = kvmalloc_array((uint32_t)num_nodes, sizeof(*nodes),
402 GFP_KERNEL | __GFP_ZERO);
404 atomic64_sub(mem_bytes, &mgr->usage);
408 mode = DRM_MM_INSERT_BEST;
409 if (place->flags & TTM_PL_FLAG_TOPDOWN)
410 mode = DRM_MM_INSERT_HIGH;
413 pages_left = mem->num_pages;
415 /* Limit maximum size to 2GB due to SG table limitations */
416 pages = min(pages_left, 2UL << (30 - PAGE_SHIFT));
419 spin_lock(&mgr->lock);
421 uint32_t alignment = tbo->page_alignment;
423 if (pages >= pages_per_node)
424 alignment = pages_per_node;
426 r = drm_mm_insert_node_in_range(mm, &nodes[i], pages, alignment,
427 0, place->fpfn, lpfn, mode);
429 if (pages > pages_per_node) {
430 if (is_power_of_2(pages))
433 pages = rounddown_pow_of_two(pages);
439 vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
440 amdgpu_vram_mgr_virt_start(mem, &nodes[i]);
444 if (pages > pages_left)
447 spin_unlock(&mgr->lock);
449 atomic64_add(vis_usage, &mgr->vis_usage);
451 mem->mm_node = nodes;
457 drm_mm_remove_node(&nodes[i]);
458 spin_unlock(&mgr->lock);
459 atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
466 * amdgpu_vram_mgr_del - free ranges
468 * @man: TTM memory type manager
469 * @mem: TTM memory object
471 * Free the allocated VRAM again.
473 static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man,
474 struct ttm_resource *mem)
476 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
477 struct amdgpu_device *adev = to_amdgpu_device(mgr);
478 struct drm_mm_node *nodes = mem->mm_node;
479 uint64_t usage = 0, vis_usage = 0;
480 unsigned pages = mem->num_pages;
485 spin_lock(&mgr->lock);
487 pages -= nodes->size;
488 drm_mm_remove_node(nodes);
489 usage += nodes->size << PAGE_SHIFT;
490 vis_usage += amdgpu_vram_mgr_vis_size(adev, nodes);
493 amdgpu_vram_mgr_do_reserve(man);
494 spin_unlock(&mgr->lock);
496 atomic64_sub(usage, &mgr->usage);
497 atomic64_sub(vis_usage, &mgr->vis_usage);
499 kvfree(mem->mm_node);
504 * amdgpu_vram_mgr_alloc_sgt - allocate and fill a sg table
506 * @adev: amdgpu device pointer
507 * @mem: TTM memory object
508 * @offset: byte offset from the base of VRAM BO
509 * @length: number of bytes to export in sg_table
510 * @dev: the other device
511 * @dir: dma direction
512 * @sgt: resulting sg table
514 * Allocate and fill a sg table from a VRAM allocation.
516 int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
517 struct ttm_resource *mem,
518 u64 offset, u64 length,
520 enum dma_data_direction dir,
521 struct sg_table **sgt)
523 struct amdgpu_res_cursor cursor;
524 struct scatterlist *sg;
528 *sgt = kmalloc(sizeof(**sgt), GFP_KERNEL);
532 /* Determine the number of DRM_MM nodes to export */
533 amdgpu_res_first(mem, offset, length, &cursor);
534 while (cursor.remaining) {
536 amdgpu_res_next(&cursor, cursor.size);
539 r = sg_alloc_table(*sgt, num_entries, GFP_KERNEL);
543 /* Initialize scatterlist nodes of sg_table */
544 for_each_sgtable_sg((*sgt), sg, i)
548 * Walk down DRM_MM nodes to populate scatterlist nodes
549 * @note: Use iterator api to get first the DRM_MM node
550 * and the number of bytes from it. Access the following
551 * DRM_MM node(s) if more buffer needs to exported
553 amdgpu_res_first(mem, offset, length, &cursor);
554 for_each_sgtable_sg((*sgt), sg, i) {
555 phys_addr_t phys = cursor.start + adev->gmc.aper_base;
556 size_t size = cursor.size;
559 addr = dma_map_resource(dev, phys, size, dir,
560 DMA_ATTR_SKIP_CPU_SYNC);
561 r = dma_mapping_error(dev, addr);
565 sg_set_page(sg, NULL, size, 0);
566 sg_dma_address(sg) = addr;
567 sg_dma_len(sg) = size;
569 amdgpu_res_next(&cursor, cursor.size);
575 for_each_sgtable_sg((*sgt), sg, i) {
579 dma_unmap_resource(dev, sg->dma_address,
581 DMA_ATTR_SKIP_CPU_SYNC);
591 * amdgpu_vram_mgr_free_sgt - allocate and fill a sg table
593 * @dev: device pointer
594 * @dir: data direction of resource to unmap
595 * @sgt: sg table to free
597 * Free a previously allocate sg table.
599 void amdgpu_vram_mgr_free_sgt(struct device *dev,
600 enum dma_data_direction dir,
601 struct sg_table *sgt)
603 struct scatterlist *sg;
606 for_each_sgtable_sg(sgt, sg, i)
607 dma_unmap_resource(dev, sg->dma_address,
609 DMA_ATTR_SKIP_CPU_SYNC);
615 * amdgpu_vram_mgr_usage - how many bytes are used in this domain
617 * @man: TTM memory type manager
619 * Returns how many bytes are used in this domain.
621 uint64_t amdgpu_vram_mgr_usage(struct ttm_resource_manager *man)
623 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
625 return atomic64_read(&mgr->usage);
629 * amdgpu_vram_mgr_vis_usage - how many bytes are used in the visible part
631 * @man: TTM memory type manager
633 * Returns how many bytes are used in the visible part of VRAM
635 uint64_t amdgpu_vram_mgr_vis_usage(struct ttm_resource_manager *man)
637 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
639 return atomic64_read(&mgr->vis_usage);
643 * amdgpu_vram_mgr_debug - dump VRAM table
645 * @man: TTM memory type manager
646 * @printer: DRM printer to use
648 * Dump the table content using printk.
650 static void amdgpu_vram_mgr_debug(struct ttm_resource_manager *man,
651 struct drm_printer *printer)
653 struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
655 spin_lock(&mgr->lock);
656 drm_mm_print(&mgr->mm, printer);
657 spin_unlock(&mgr->lock);
659 drm_printf(printer, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
660 man->size, amdgpu_vram_mgr_usage(man) >> 20,
661 amdgpu_vram_mgr_vis_usage(man) >> 20);
664 static const struct ttm_resource_manager_func amdgpu_vram_mgr_func = {
665 .alloc = amdgpu_vram_mgr_new,
666 .free = amdgpu_vram_mgr_del,
667 .debug = amdgpu_vram_mgr_debug
671 * amdgpu_vram_mgr_init - init VRAM manager and DRM MM
673 * @adev: amdgpu_device pointer
675 * Allocate and initialize the VRAM manager.
677 int amdgpu_vram_mgr_init(struct amdgpu_device *adev)
679 struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;
680 struct ttm_resource_manager *man = &mgr->manager;
683 ttm_resource_manager_init(man, adev->gmc.real_vram_size >> PAGE_SHIFT);
685 man->func = &amdgpu_vram_mgr_func;
687 drm_mm_init(&mgr->mm, 0, man->size);
688 spin_lock_init(&mgr->lock);
689 INIT_LIST_HEAD(&mgr->reservations_pending);
690 INIT_LIST_HEAD(&mgr->reserved_pages);
692 /* Add the two VRAM-related sysfs files */
693 ret = sysfs_create_files(&adev->dev->kobj, amdgpu_vram_mgr_attributes);
695 DRM_ERROR("Failed to register sysfs\n");
697 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, &mgr->manager);
698 ttm_resource_manager_set_used(man, true);
703 * amdgpu_vram_mgr_fini - free and destroy VRAM manager
705 * @adev: amdgpu_device pointer
707 * Destroy and free the VRAM manager, returns -EBUSY if ranges are still
708 * allocated inside it.
710 void amdgpu_vram_mgr_fini(struct amdgpu_device *adev)
712 struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;
713 struct ttm_resource_manager *man = &mgr->manager;
715 struct amdgpu_vram_reservation *rsv, *temp;
717 ttm_resource_manager_set_used(man, false);
719 ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man);
723 spin_lock(&mgr->lock);
724 list_for_each_entry_safe(rsv, temp, &mgr->reservations_pending, node)
727 list_for_each_entry_safe(rsv, temp, &mgr->reserved_pages, node) {
728 drm_mm_remove_node(&rsv->mm_node);
731 drm_mm_takedown(&mgr->mm);
732 spin_unlock(&mgr->lock);
734 sysfs_remove_files(&adev->dev->kobj, amdgpu_vram_mgr_attributes);
736 ttm_resource_manager_cleanup(man);
737 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, NULL);