Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/amdgpu: Add sysfs files for returning VRAM/GTT info v2

Add 6 files that return (in bytes):
The total amount of VRAM/visible VRAM/GTT
and the current total used VRAM/visible VRAM/GTT

v2: Split used and total into separate files

Reviewed-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Kent Russell <kent.russell@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>

authored by

Kent Russell and committed by
Alex Deucher
55c374e9 07740adc

+168
+59
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c
··· 37 37 }; 38 38 39 39 /** 40 + * DOC: mem_info_gtt_total 41 + * 42 + * The amdgpu driver provides a sysfs API for reporting current total size of 43 + * the GTT. 44 + * The file mem_info_gtt_total is used for this, and returns the total size of 45 + * the GTT block, in bytes 46 + */ 47 + static ssize_t amdgpu_mem_info_gtt_total_show(struct device *dev, 48 + struct device_attribute *attr, char *buf) 49 + { 50 + struct drm_device *ddev = dev_get_drvdata(dev); 51 + struct amdgpu_device *adev = ddev->dev_private; 52 + 53 + return snprintf(buf, PAGE_SIZE, "%llu\n", 54 + (adev->mman.bdev.man[TTM_PL_TT].size) * PAGE_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, char *buf) 67 + { 68 + struct drm_device *ddev = dev_get_drvdata(dev); 69 + struct amdgpu_device *adev = ddev->dev_private; 70 + 71 + return snprintf(buf, PAGE_SIZE, "%llu\n", 72 + amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT])); 73 + } 74 + 75 + static DEVICE_ATTR(mem_info_gtt_total, S_IRUGO, 76 + amdgpu_mem_info_gtt_total_show, NULL); 77 + static DEVICE_ATTR(mem_info_gtt_used, S_IRUGO, 78 + amdgpu_mem_info_gtt_used_show, NULL); 79 + 80 + /** 40 81 * amdgpu_gtt_mgr_init - init GTT manager and DRM MM 41 82 * 42 83 * @man: TTM memory type manager ··· 91 50 struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev); 92 51 struct amdgpu_gtt_mgr *mgr; 93 52 uint64_t start, size; 53 + int ret; 94 54 95 55 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL); 96 56 if (!mgr) ··· 103 61 spin_lock_init(&mgr->lock); 104 62 atomic64_set(&mgr->available, p_size); 105 63 man->priv = mgr; 64 + 65 + ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_total); 66 + if (ret) { 67 + DRM_ERROR("Failed to create device file mem_info_gtt_total\n"); 68 + return ret; 69 + } 70 + ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_used); 71 + if (ret) { 72 + DRM_ERROR("Failed to create device file mem_info_gtt_used\n"); 73 + return ret; 74 + } 75 + 106 76 return 0; 107 77 } 108 78 ··· 128 74 */ 129 75 static int amdgpu_gtt_mgr_fini(struct ttm_mem_type_manager *man) 130 76 { 77 + struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev); 131 78 struct amdgpu_gtt_mgr *mgr = man->priv; 132 79 spin_lock(&mgr->lock); 133 80 drm_mm_takedown(&mgr->mm); 134 81 spin_unlock(&mgr->lock); 135 82 kfree(mgr); 136 83 man->priv = NULL; 84 + 85 + device_remove_file(adev->dev, &dev_attr_mem_info_gtt_total); 86 + device_remove_file(adev->dev, &dev_attr_mem_info_gtt_used); 87 + 137 88 return 0; 138 89 } 139 90
+109
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
··· 33 33 }; 34 34 35 35 /** 36 + * DOC: mem_info_vram_total 37 + * 38 + * The amdgpu driver provides a sysfs API for reporting current total VRAM 39 + * available on the device 40 + * The file mem_info_vram_total is used for this and returns the total 41 + * amount of VRAM in bytes 42 + */ 43 + static ssize_t amdgpu_mem_info_vram_total_show(struct device *dev, 44 + struct device_attribute *attr, char *buf) 45 + { 46 + struct drm_device *ddev = dev_get_drvdata(dev); 47 + struct amdgpu_device *adev = ddev->dev_private; 48 + 49 + return snprintf(buf, PAGE_SIZE, "%llu\n", adev->gmc.real_vram_size); 50 + } 51 + 52 + /** 53 + * DOC: mem_info_vis_vram_total 54 + * 55 + * The amdgpu driver provides a sysfs API for reporting current total 56 + * visible VRAM available on the device 57 + * The file mem_info_vis_vram_total is used for this and returns the total 58 + * amount of visible VRAM in bytes 59 + */ 60 + static ssize_t amdgpu_mem_info_vis_vram_total_show(struct device *dev, 61 + struct device_attribute *attr, char *buf) 62 + { 63 + struct drm_device *ddev = dev_get_drvdata(dev); 64 + struct amdgpu_device *adev = ddev->dev_private; 65 + 66 + return snprintf(buf, PAGE_SIZE, "%llu\n", adev->gmc.visible_vram_size); 67 + } 68 + 69 + /** 70 + * DOC: mem_info_vram_used 71 + * 72 + * The amdgpu driver provides a sysfs API for reporting current total VRAM 73 + * available on the device 74 + * The file mem_info_vram_used is used for this and returns the total 75 + * amount of currently used VRAM in bytes 76 + */ 77 + static ssize_t amdgpu_mem_info_vram_used_show(struct device *dev, 78 + struct device_attribute *attr, char *buf) 79 + { 80 + struct drm_device *ddev = dev_get_drvdata(dev); 81 + struct amdgpu_device *adev = ddev->dev_private; 82 + 83 + return snprintf(buf, PAGE_SIZE, "%llu\n", 84 + amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM])); 85 + } 86 + 87 + /** 88 + * DOC: mem_info_vis_vram_used 89 + * 90 + * The amdgpu driver provides a sysfs API for reporting current total of 91 + * used visible VRAM 92 + * The file mem_info_vis_vram_used is used for this and returns the total 93 + * amount of currently used visible VRAM in bytes 94 + */ 95 + static ssize_t amdgpu_mem_info_vis_vram_used_show(struct device *dev, 96 + struct device_attribute *attr, char *buf) 97 + { 98 + struct drm_device *ddev = dev_get_drvdata(dev); 99 + struct amdgpu_device *adev = ddev->dev_private; 100 + 101 + return snprintf(buf, PAGE_SIZE, "%llu\n", 102 + amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM])); 103 + } 104 + 105 + static DEVICE_ATTR(mem_info_vram_total, S_IRUGO, 106 + amdgpu_mem_info_vram_total_show, NULL); 107 + static DEVICE_ATTR(mem_info_vis_vram_total, S_IRUGO, 108 + amdgpu_mem_info_vis_vram_total_show,NULL); 109 + static DEVICE_ATTR(mem_info_vram_used, S_IRUGO, 110 + amdgpu_mem_info_vram_used_show, NULL); 111 + static DEVICE_ATTR(mem_info_vis_vram_used, S_IRUGO, 112 + amdgpu_mem_info_vis_vram_used_show, NULL); 113 + 114 + /** 36 115 * amdgpu_vram_mgr_init - init VRAM manager and DRM MM 37 116 * 38 117 * @man: TTM memory type manager ··· 122 43 static int amdgpu_vram_mgr_init(struct ttm_mem_type_manager *man, 123 44 unsigned long p_size) 124 45 { 46 + struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev); 125 47 struct amdgpu_vram_mgr *mgr; 48 + int ret; 126 49 127 50 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL); 128 51 if (!mgr) ··· 133 52 drm_mm_init(&mgr->mm, 0, p_size); 134 53 spin_lock_init(&mgr->lock); 135 54 man->priv = mgr; 55 + 56 + /* Add the two VRAM-related sysfs files */ 57 + ret = device_create_file(adev->dev, &dev_attr_mem_info_vram_total); 58 + if (ret) { 59 + DRM_ERROR("Failed to create device file mem_info_vram_total\n"); 60 + return ret; 61 + } 62 + ret = device_create_file(adev->dev, &dev_attr_mem_info_vis_vram_total); 63 + if (ret) { 64 + DRM_ERROR("Failed to create device file mem_info_vis_vram_total\n"); 65 + return ret; 66 + } 67 + ret = device_create_file(adev->dev, &dev_attr_mem_info_vram_used); 68 + if (ret) { 69 + DRM_ERROR("Failed to create device file mem_info_vram_used\n"); 70 + return ret; 71 + } 72 + ret = device_create_file(adev->dev, &dev_attr_mem_info_vis_vram_used); 73 + if (ret) { 74 + DRM_ERROR("Failed to create device file mem_info_vis_vram_used\n"); 75 + return ret; 76 + } 77 + 136 78 return 0; 137 79 } 138 80 ··· 169 65 */ 170 66 static int amdgpu_vram_mgr_fini(struct ttm_mem_type_manager *man) 171 67 { 68 + struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev); 172 69 struct amdgpu_vram_mgr *mgr = man->priv; 173 70 174 71 spin_lock(&mgr->lock); ··· 177 72 spin_unlock(&mgr->lock); 178 73 kfree(mgr); 179 74 man->priv = NULL; 75 + device_remove_file(adev->dev, &dev_attr_mem_info_vram_total); 76 + device_remove_file(adev->dev, &dev_attr_mem_info_vis_vram_total); 77 + device_remove_file(adev->dev, &dev_attr_mem_info_vram_used); 78 + device_remove_file(adev->dev, &dev_attr_mem_info_vis_vram_used); 180 79 return 0; 181 80 } 182 81