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 * amdgpu_vram_mgr_init - init VRAM manager and DRM MM
38 * @man: TTM memory type manager
39 * @p_size: maximum size of VRAM
41 * Allocate and initialize the VRAM manager.
43 static int amdgpu_vram_mgr_init(struct ttm_mem_type_manager *man,
46 struct amdgpu_vram_mgr *mgr;
48 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
52 drm_mm_init(&mgr->mm, 0, p_size);
53 spin_lock_init(&mgr->lock);
59 * amdgpu_vram_mgr_fini - free and destroy VRAM manager
61 * @man: TTM memory type manager
63 * Destroy and free the VRAM manager, returns -EBUSY if ranges are still
64 * allocated inside it.
66 static int amdgpu_vram_mgr_fini(struct ttm_mem_type_manager *man)
68 struct amdgpu_vram_mgr *mgr = man->priv;
70 spin_lock(&mgr->lock);
71 drm_mm_takedown(&mgr->mm);
72 spin_unlock(&mgr->lock);
79 * amdgpu_vram_mgr_vis_size - Calculate visible node size
81 * @adev: amdgpu device structure
82 * @node: MM node structure
84 * Calculate how many bytes of the MM node are inside visible VRAM
86 static u64 amdgpu_vram_mgr_vis_size(struct amdgpu_device *adev,
87 struct drm_mm_node *node)
89 uint64_t start = node->start << PAGE_SHIFT;
90 uint64_t end = (node->size + node->start) << PAGE_SHIFT;
92 if (start >= adev->gmc.visible_vram_size)
95 return (end > adev->gmc.visible_vram_size ?
96 adev->gmc.visible_vram_size : end) - start;
100 * amdgpu_vram_mgr_bo_invisible_size - CPU invisible BO size
102 * @bo: &amdgpu_bo buffer object (must be in VRAM)
105 * How much of the given &amdgpu_bo buffer object lies in CPU invisible VRAM.
107 u64 amdgpu_vram_mgr_bo_invisible_size(struct amdgpu_bo *bo)
109 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
110 struct ttm_mem_reg *mem = &bo->tbo.mem;
111 struct drm_mm_node *nodes = mem->mm_node;
112 unsigned pages = mem->num_pages;
115 if (adev->gmc.visible_vram_size == adev->gmc.real_vram_size)
118 if (mem->start >= adev->gmc.visible_vram_size >> PAGE_SHIFT)
119 return amdgpu_bo_size(bo);
121 while (nodes && pages) {
122 usage += nodes->size << PAGE_SHIFT;
123 usage -= amdgpu_vram_mgr_vis_size(adev, nodes);
124 pages -= nodes->size;
132 * amdgpu_vram_mgr_new - allocate new ranges
134 * @man: TTM memory type manager
135 * @tbo: TTM BO we need this range for
136 * @place: placement flags and restrictions
137 * @mem: the resulting mem object
139 * Allocate VRAM for the given BO.
141 static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
142 struct ttm_buffer_object *tbo,
143 const struct ttm_place *place,
144 struct ttm_mem_reg *mem)
146 struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
147 struct amdgpu_vram_mgr *mgr = man->priv;
148 struct drm_mm *mm = &mgr->mm;
149 struct drm_mm_node *nodes;
150 enum drm_mm_insert_mode mode;
151 unsigned long lpfn, num_nodes, pages_per_node, pages_left;
152 uint64_t usage = 0, vis_usage = 0;
160 if (place->flags & TTM_PL_FLAG_CONTIGUOUS ||
161 amdgpu_vram_page_split == -1) {
162 pages_per_node = ~0ul;
165 pages_per_node = max((uint32_t)amdgpu_vram_page_split,
166 mem->page_alignment);
167 num_nodes = DIV_ROUND_UP(mem->num_pages, pages_per_node);
170 nodes = kvmalloc_array(num_nodes, sizeof(*nodes),
171 GFP_KERNEL | __GFP_ZERO);
175 mode = DRM_MM_INSERT_BEST;
176 if (place->flags & TTM_PL_FLAG_TOPDOWN)
177 mode = DRM_MM_INSERT_HIGH;
180 pages_left = mem->num_pages;
182 spin_lock(&mgr->lock);
183 for (i = 0; i < num_nodes; ++i) {
184 unsigned long pages = min(pages_left, pages_per_node);
185 uint32_t alignment = mem->page_alignment;
188 if (pages == pages_per_node)
189 alignment = pages_per_node;
191 r = drm_mm_insert_node_in_range(mm, &nodes[i],
198 usage += nodes[i].size << PAGE_SHIFT;
199 vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
201 /* Calculate a virtual BO start address to easily check if
202 * everything is CPU accessible.
204 start = nodes[i].start + nodes[i].size;
205 if (start > mem->num_pages)
206 start -= mem->num_pages;
209 mem->start = max(mem->start, start);
212 spin_unlock(&mgr->lock);
214 atomic64_add(usage, &mgr->usage);
215 atomic64_add(vis_usage, &mgr->vis_usage);
217 mem->mm_node = nodes;
223 drm_mm_remove_node(&nodes[i]);
224 spin_unlock(&mgr->lock);
227 return r == -ENOSPC ? 0 : r;
231 * amdgpu_vram_mgr_del - free ranges
233 * @man: TTM memory type manager
234 * @tbo: TTM BO we need this range for
235 * @place: placement flags and restrictions
236 * @mem: TTM memory object
238 * Free the allocated VRAM again.
240 static void amdgpu_vram_mgr_del(struct ttm_mem_type_manager *man,
241 struct ttm_mem_reg *mem)
243 struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
244 struct amdgpu_vram_mgr *mgr = man->priv;
245 struct drm_mm_node *nodes = mem->mm_node;
246 uint64_t usage = 0, vis_usage = 0;
247 unsigned pages = mem->num_pages;
252 spin_lock(&mgr->lock);
254 pages -= nodes->size;
255 drm_mm_remove_node(nodes);
256 usage += nodes->size << PAGE_SHIFT;
257 vis_usage += amdgpu_vram_mgr_vis_size(adev, nodes);
260 spin_unlock(&mgr->lock);
262 atomic64_sub(usage, &mgr->usage);
263 atomic64_sub(vis_usage, &mgr->vis_usage);
265 kvfree(mem->mm_node);
270 * amdgpu_vram_mgr_usage - how many bytes are used in this domain
272 * @man: TTM memory type manager
274 * Returns how many bytes are used in this domain.
276 uint64_t amdgpu_vram_mgr_usage(struct ttm_mem_type_manager *man)
278 struct amdgpu_vram_mgr *mgr = man->priv;
280 return atomic64_read(&mgr->usage);
284 * amdgpu_vram_mgr_vis_usage - how many bytes are used in the visible part
286 * @man: TTM memory type manager
288 * Returns how many bytes are used in the visible part of VRAM
290 uint64_t amdgpu_vram_mgr_vis_usage(struct ttm_mem_type_manager *man)
292 struct amdgpu_vram_mgr *mgr = man->priv;
294 return atomic64_read(&mgr->vis_usage);
298 * amdgpu_vram_mgr_debug - dump VRAM table
300 * @man: TTM memory type manager
301 * @printer: DRM printer to use
303 * Dump the table content using printk.
305 static void amdgpu_vram_mgr_debug(struct ttm_mem_type_manager *man,
306 struct drm_printer *printer)
308 struct amdgpu_vram_mgr *mgr = man->priv;
310 spin_lock(&mgr->lock);
311 drm_mm_print(&mgr->mm, printer);
312 spin_unlock(&mgr->lock);
314 drm_printf(printer, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
315 man->size, amdgpu_vram_mgr_usage(man) >> 20,
316 amdgpu_vram_mgr_vis_usage(man) >> 20);
319 const struct ttm_mem_type_manager_func amdgpu_vram_mgr_func = {
320 .init = amdgpu_vram_mgr_init,
321 .takedown = amdgpu_vram_mgr_fini,
322 .get_node = amdgpu_vram_mgr_new,
323 .put_node = amdgpu_vram_mgr_del,
324 .debug = amdgpu_vram_mgr_debug