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
28 struct amdgpu_vram_mgr {
36 * DOC: mem_info_vram_total
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
43 static ssize_t amdgpu_mem_info_vram_total_show(struct device *dev,
44 struct device_attribute *attr, char *buf)
46 struct drm_device *ddev = dev_get_drvdata(dev);
47 struct amdgpu_device *adev = ddev->dev_private;
49 return snprintf(buf, PAGE_SIZE, "%llu\n", adev->gmc.real_vram_size);
53 * DOC: mem_info_vis_vram_total
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
60 static ssize_t amdgpu_mem_info_vis_vram_total_show(struct device *dev,
61 struct device_attribute *attr, char *buf)
63 struct drm_device *ddev = dev_get_drvdata(dev);
64 struct amdgpu_device *adev = ddev->dev_private;
66 return snprintf(buf, PAGE_SIZE, "%llu\n", adev->gmc.visible_vram_size);
70 * DOC: mem_info_vram_used
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
77 static ssize_t amdgpu_mem_info_vram_used_show(struct device *dev,
78 struct device_attribute *attr, char *buf)
80 struct drm_device *ddev = dev_get_drvdata(dev);
81 struct amdgpu_device *adev = ddev->dev_private;
83 return snprintf(buf, PAGE_SIZE, "%llu\n",
84 amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]));
88 * DOC: mem_info_vis_vram_used
90 * The amdgpu driver provides a sysfs API for reporting current total of
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
95 static ssize_t amdgpu_mem_info_vis_vram_used_show(struct device *dev,
96 struct device_attribute *attr, char *buf)
98 struct drm_device *ddev = dev_get_drvdata(dev);
99 struct amdgpu_device *adev = ddev->dev_private;
101 return snprintf(buf, PAGE_SIZE, "%llu\n",
102 amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]));
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);
115 * amdgpu_vram_mgr_init - init VRAM manager and DRM MM
117 * @man: TTM memory type manager
118 * @p_size: maximum size of VRAM
120 * Allocate and initialize the VRAM manager.
122 static int amdgpu_vram_mgr_init(struct ttm_mem_type_manager *man,
123 unsigned long p_size)
125 struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
126 struct amdgpu_vram_mgr *mgr;
129 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
133 drm_mm_init(&mgr->mm, 0, p_size);
134 spin_lock_init(&mgr->lock);
137 /* Add the two VRAM-related sysfs files */
138 ret = device_create_file(adev->dev, &dev_attr_mem_info_vram_total);
140 DRM_ERROR("Failed to create device file mem_info_vram_total\n");
143 ret = device_create_file(adev->dev, &dev_attr_mem_info_vis_vram_total);
145 DRM_ERROR("Failed to create device file mem_info_vis_vram_total\n");
148 ret = device_create_file(adev->dev, &dev_attr_mem_info_vram_used);
150 DRM_ERROR("Failed to create device file mem_info_vram_used\n");
153 ret = device_create_file(adev->dev, &dev_attr_mem_info_vis_vram_used);
155 DRM_ERROR("Failed to create device file mem_info_vis_vram_used\n");
163 * amdgpu_vram_mgr_fini - free and destroy VRAM manager
165 * @man: TTM memory type manager
167 * Destroy and free the VRAM manager, returns -EBUSY if ranges are still
168 * allocated inside it.
170 static int amdgpu_vram_mgr_fini(struct ttm_mem_type_manager *man)
172 struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
173 struct amdgpu_vram_mgr *mgr = man->priv;
175 spin_lock(&mgr->lock);
176 drm_mm_takedown(&mgr->mm);
177 spin_unlock(&mgr->lock);
180 device_remove_file(adev->dev, &dev_attr_mem_info_vram_total);
181 device_remove_file(adev->dev, &dev_attr_mem_info_vis_vram_total);
182 device_remove_file(adev->dev, &dev_attr_mem_info_vram_used);
183 device_remove_file(adev->dev, &dev_attr_mem_info_vis_vram_used);
188 * amdgpu_vram_mgr_vis_size - Calculate visible node size
190 * @adev: amdgpu device structure
191 * @node: MM node structure
193 * Calculate how many bytes of the MM node are inside visible VRAM
195 static u64 amdgpu_vram_mgr_vis_size(struct amdgpu_device *adev,
196 struct drm_mm_node *node)
198 uint64_t start = node->start << PAGE_SHIFT;
199 uint64_t end = (node->size + node->start) << PAGE_SHIFT;
201 if (start >= adev->gmc.visible_vram_size)
204 return (end > adev->gmc.visible_vram_size ?
205 adev->gmc.visible_vram_size : end) - start;
209 * amdgpu_vram_mgr_bo_visible_size - CPU visible BO size
211 * @bo: &amdgpu_bo buffer object (must be in VRAM)
214 * How much of the given &amdgpu_bo buffer object lies in CPU visible VRAM.
216 u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo)
218 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
219 struct ttm_mem_reg *mem = &bo->tbo.mem;
220 struct drm_mm_node *nodes = mem->mm_node;
221 unsigned pages = mem->num_pages;
224 if (amdgpu_gmc_vram_full_visible(&adev->gmc))
225 return amdgpu_bo_size(bo);
227 if (mem->start >= adev->gmc.visible_vram_size >> PAGE_SHIFT)
230 for (usage = 0; nodes && pages; pages -= nodes->size, nodes++)
231 usage += amdgpu_vram_mgr_vis_size(adev, nodes);
237 * amdgpu_vram_mgr_virt_start - update virtual start address
239 * @mem: ttm_mem_reg to update
240 * @node: just allocated node
242 * Calculate a virtual BO start address to easily check if everything is CPU
245 static void amdgpu_vram_mgr_virt_start(struct ttm_mem_reg *mem,
246 struct drm_mm_node *node)
250 start = node->start + node->size;
251 if (start > mem->num_pages)
252 start -= mem->num_pages;
255 mem->start = max(mem->start, start);
259 * amdgpu_vram_mgr_new - allocate new ranges
261 * @man: TTM memory type manager
262 * @tbo: TTM BO we need this range for
263 * @place: placement flags and restrictions
264 * @mem: the resulting mem object
266 * Allocate VRAM for the given BO.
268 static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
269 struct ttm_buffer_object *tbo,
270 const struct ttm_place *place,
271 struct ttm_mem_reg *mem)
273 struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
274 struct amdgpu_vram_mgr *mgr = man->priv;
275 struct drm_mm *mm = &mgr->mm;
276 struct drm_mm_node *nodes;
277 enum drm_mm_insert_mode mode;
278 unsigned long lpfn, num_nodes, pages_per_node, pages_left;
279 uint64_t vis_usage = 0, mem_bytes;
287 /* bail out quickly if there's likely not enough VRAM for this BO */
288 mem_bytes = (u64)mem->num_pages << PAGE_SHIFT;
289 if (atomic64_add_return(mem_bytes, &mgr->usage) > adev->gmc.mc_vram_size) {
290 atomic64_sub(mem_bytes, &mgr->usage);
295 if (place->flags & TTM_PL_FLAG_CONTIGUOUS) {
296 pages_per_node = ~0ul;
299 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
300 pages_per_node = HPAGE_PMD_NR;
303 pages_per_node = (2UL << (20UL - PAGE_SHIFT));
305 pages_per_node = max((uint32_t)pages_per_node, mem->page_alignment);
306 num_nodes = DIV_ROUND_UP(mem->num_pages, pages_per_node);
309 nodes = kvmalloc_array((uint32_t)num_nodes, sizeof(*nodes),
310 GFP_KERNEL | __GFP_ZERO);
312 atomic64_sub(mem_bytes, &mgr->usage);
316 mode = DRM_MM_INSERT_BEST;
317 if (place->flags & TTM_PL_FLAG_TOPDOWN)
318 mode = DRM_MM_INSERT_HIGH;
321 pages_left = mem->num_pages;
323 spin_lock(&mgr->lock);
324 for (i = 0; pages_left >= pages_per_node; ++i) {
325 unsigned long pages = rounddown_pow_of_two(pages_left);
327 r = drm_mm_insert_node_in_range(mm, &nodes[i], pages,
334 vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
335 amdgpu_vram_mgr_virt_start(mem, &nodes[i]);
339 for (; pages_left; ++i) {
340 unsigned long pages = min(pages_left, pages_per_node);
341 uint32_t alignment = mem->page_alignment;
343 if (pages == pages_per_node)
344 alignment = pages_per_node;
346 r = drm_mm_insert_node_in_range(mm, &nodes[i],
353 vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
354 amdgpu_vram_mgr_virt_start(mem, &nodes[i]);
357 spin_unlock(&mgr->lock);
359 atomic64_add(vis_usage, &mgr->vis_usage);
361 mem->mm_node = nodes;
367 drm_mm_remove_node(&nodes[i]);
368 spin_unlock(&mgr->lock);
369 atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
372 return r == -ENOSPC ? 0 : r;
376 * amdgpu_vram_mgr_del - free ranges
378 * @man: TTM memory type manager
379 * @tbo: TTM BO we need this range for
380 * @place: placement flags and restrictions
381 * @mem: TTM memory object
383 * Free the allocated VRAM again.
385 static void amdgpu_vram_mgr_del(struct ttm_mem_type_manager *man,
386 struct ttm_mem_reg *mem)
388 struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
389 struct amdgpu_vram_mgr *mgr = man->priv;
390 struct drm_mm_node *nodes = mem->mm_node;
391 uint64_t usage = 0, vis_usage = 0;
392 unsigned pages = mem->num_pages;
397 spin_lock(&mgr->lock);
399 pages -= nodes->size;
400 drm_mm_remove_node(nodes);
401 usage += nodes->size << PAGE_SHIFT;
402 vis_usage += amdgpu_vram_mgr_vis_size(adev, nodes);
405 spin_unlock(&mgr->lock);
407 atomic64_sub(usage, &mgr->usage);
408 atomic64_sub(vis_usage, &mgr->vis_usage);
410 kvfree(mem->mm_node);
415 * amdgpu_vram_mgr_usage - how many bytes are used in this domain
417 * @man: TTM memory type manager
419 * Returns how many bytes are used in this domain.
421 uint64_t amdgpu_vram_mgr_usage(struct ttm_mem_type_manager *man)
423 struct amdgpu_vram_mgr *mgr = man->priv;
425 return atomic64_read(&mgr->usage);
429 * amdgpu_vram_mgr_vis_usage - how many bytes are used in the visible part
431 * @man: TTM memory type manager
433 * Returns how many bytes are used in the visible part of VRAM
435 uint64_t amdgpu_vram_mgr_vis_usage(struct ttm_mem_type_manager *man)
437 struct amdgpu_vram_mgr *mgr = man->priv;
439 return atomic64_read(&mgr->vis_usage);
443 * amdgpu_vram_mgr_debug - dump VRAM table
445 * @man: TTM memory type manager
446 * @printer: DRM printer to use
448 * Dump the table content using printk.
450 static void amdgpu_vram_mgr_debug(struct ttm_mem_type_manager *man,
451 struct drm_printer *printer)
453 struct amdgpu_vram_mgr *mgr = man->priv;
455 spin_lock(&mgr->lock);
456 drm_mm_print(&mgr->mm, printer);
457 spin_unlock(&mgr->lock);
459 drm_printf(printer, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
460 man->size, amdgpu_vram_mgr_usage(man) >> 20,
461 amdgpu_vram_mgr_vis_usage(man) >> 20);
464 const struct ttm_mem_type_manager_func amdgpu_vram_mgr_func = {
465 .init = amdgpu_vram_mgr_init,
466 .takedown = amdgpu_vram_mgr_fini,
467 .get_node = amdgpu_vram_mgr_new,
468 .put_node = amdgpu_vram_mgr_del,
469 .debug = amdgpu_vram_mgr_debug