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 if (!drm_mm_clean(&mgr->mm)) {
72 spin_unlock(&mgr->lock);
76 drm_mm_takedown(&mgr->mm);
77 spin_unlock(&mgr->lock);
84 * amdgpu_vram_mgr_vis_size - Calculate visible node size
86 * @adev: amdgpu device structure
87 * @node: MM node structure
89 * Calculate how many bytes of the MM node are inside visible VRAM
91 static u64 amdgpu_vram_mgr_vis_size(struct amdgpu_device *adev,
92 struct drm_mm_node *node)
94 uint64_t start = node->start << PAGE_SHIFT;
95 uint64_t end = (node->size + node->start) << PAGE_SHIFT;
97 if (start >= adev->mc.visible_vram_size)
100 return (end > adev->mc.visible_vram_size ?
101 adev->mc.visible_vram_size : end) - start;
105 * amdgpu_vram_mgr_bo_invisible_size - CPU invisible BO size
107 * @bo: &amdgpu_bo buffer object (must be in VRAM)
110 * How much of the given &amdgpu_bo buffer object lies in CPU invisible VRAM.
112 u64 amdgpu_vram_mgr_bo_invisible_size(struct amdgpu_bo *bo)
114 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
115 return amdgpu_bo_size(bo);
121 * amdgpu_vram_mgr_new - allocate new ranges
123 * @man: TTM memory type manager
124 * @tbo: TTM BO we need this range for
125 * @place: placement flags and restrictions
126 * @mem: the resulting mem object
128 * Allocate VRAM for the given BO.
130 static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
131 struct ttm_buffer_object *tbo,
132 const struct ttm_place *place,
133 struct ttm_mem_reg *mem)
135 struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
136 struct amdgpu_vram_mgr *mgr = man->priv;
137 struct drm_mm *mm = &mgr->mm;
138 struct drm_mm_node *nodes;
139 enum drm_mm_insert_mode mode;
140 unsigned long lpfn, num_nodes, pages_per_node, pages_left;
141 uint64_t usage = 0, vis_usage = 0;
149 if (place->flags & TTM_PL_FLAG_CONTIGUOUS ||
150 amdgpu_vram_page_split == -1) {
151 pages_per_node = ~0ul;
154 pages_per_node = max((uint32_t)amdgpu_vram_page_split,
155 mem->page_alignment);
156 num_nodes = DIV_ROUND_UP(mem->num_pages, pages_per_node);
159 nodes = kvmalloc_array(num_nodes, sizeof(*nodes),
160 GFP_KERNEL | __GFP_ZERO);
164 mode = DRM_MM_INSERT_BEST;
165 if (place->flags & TTM_PL_FLAG_TOPDOWN)
166 mode = DRM_MM_INSERT_HIGH;
169 pages_left = mem->num_pages;
171 spin_lock(&mgr->lock);
172 for (i = 0; i < num_nodes; ++i) {
173 unsigned long pages = min(pages_left, pages_per_node);
174 uint32_t alignment = mem->page_alignment;
177 if (pages == pages_per_node)
178 alignment = pages_per_node;
180 r = drm_mm_insert_node_in_range(mm, &nodes[i],
187 usage += nodes[i].size << PAGE_SHIFT;
188 vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
190 /* Calculate a virtual BO start address to easily check if
191 * everything is CPU accessible.
193 start = nodes[i].start + nodes[i].size;
194 if (start > mem->num_pages)
195 start -= mem->num_pages;
198 mem->start = max(mem->start, start);
201 spin_unlock(&mgr->lock);
203 atomic64_add(usage, &mgr->usage);
204 atomic64_add(vis_usage, &mgr->vis_usage);
206 mem->mm_node = nodes;
212 drm_mm_remove_node(&nodes[i]);
213 spin_unlock(&mgr->lock);
216 return r == -ENOSPC ? 0 : r;
220 * amdgpu_vram_mgr_del - free ranges
222 * @man: TTM memory type manager
223 * @tbo: TTM BO we need this range for
224 * @place: placement flags and restrictions
225 * @mem: TTM memory object
227 * Free the allocated VRAM again.
229 static void amdgpu_vram_mgr_del(struct ttm_mem_type_manager *man,
230 struct ttm_mem_reg *mem)
232 struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
233 struct amdgpu_vram_mgr *mgr = man->priv;
234 struct drm_mm_node *nodes = mem->mm_node;
235 uint64_t usage = 0, vis_usage = 0;
236 unsigned pages = mem->num_pages;
241 spin_lock(&mgr->lock);
243 pages -= nodes->size;
244 drm_mm_remove_node(nodes);
245 usage += nodes->size << PAGE_SHIFT;
246 vis_usage += amdgpu_vram_mgr_vis_size(adev, nodes);
249 spin_unlock(&mgr->lock);
251 atomic64_sub(usage, &mgr->usage);
252 atomic64_sub(vis_usage, &mgr->vis_usage);
254 kvfree(mem->mm_node);
259 * amdgpu_vram_mgr_usage - how many bytes are used in this domain
261 * @man: TTM memory type manager
263 * Returns how many bytes are used in this domain.
265 uint64_t amdgpu_vram_mgr_usage(struct ttm_mem_type_manager *man)
267 struct amdgpu_vram_mgr *mgr = man->priv;
269 return atomic64_read(&mgr->usage);
273 * amdgpu_vram_mgr_vis_usage - how many bytes are used in the visible part
275 * @man: TTM memory type manager
277 * Returns how many bytes are used in the visible part of VRAM
279 uint64_t amdgpu_vram_mgr_vis_usage(struct ttm_mem_type_manager *man)
281 struct amdgpu_vram_mgr *mgr = man->priv;
283 return atomic64_read(&mgr->vis_usage);
287 * amdgpu_vram_mgr_debug - dump VRAM table
289 * @man: TTM memory type manager
290 * @printer: DRM printer to use
292 * Dump the table content using printk.
294 static void amdgpu_vram_mgr_debug(struct ttm_mem_type_manager *man,
295 struct drm_printer *printer)
297 struct amdgpu_vram_mgr *mgr = man->priv;
299 spin_lock(&mgr->lock);
300 drm_mm_print(&mgr->mm, printer);
301 spin_unlock(&mgr->lock);
303 drm_printf(printer, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
304 man->size, amdgpu_vram_mgr_usage(man) >> 20,
305 amdgpu_vram_mgr_vis_usage(man) >> 20);
308 const struct ttm_mem_type_manager_func amdgpu_vram_mgr_func = {
309 .init = amdgpu_vram_mgr_init,
310 .takedown = amdgpu_vram_mgr_fini,
311 .get_node = amdgpu_vram_mgr_new,
312 .put_node = amdgpu_vram_mgr_del,
313 .debug = amdgpu_vram_mgr_debug