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_gtt_mgr {
34 struct amdgpu_gtt_node {
35 struct drm_mm_node node;
36 struct ttm_buffer_object *tbo;
40 * DOC: mem_info_gtt_total
42 * The amdgpu driver provides a sysfs API for reporting current total size of
44 * The file mem_info_gtt_total is used for this, and returns the total size of
45 * the GTT block, in bytes
47 static ssize_t amdgpu_mem_info_gtt_total_show(struct device *dev,
48 struct device_attribute *attr, char *buf)
50 struct drm_device *ddev = dev_get_drvdata(dev);
51 struct amdgpu_device *adev = ddev->dev_private;
53 return snprintf(buf, PAGE_SIZE, "%llu\n",
54 (adev->mman.bdev.man[TTM_PL_TT].size) * PAGE_SIZE);
58 * DOC: mem_info_gtt_used
60 * The amdgpu driver provides a sysfs API for reporting current total amount of
62 * The file mem_info_gtt_used is used for this, and returns the current used
63 * size of the GTT block, in bytes
65 static ssize_t amdgpu_mem_info_gtt_used_show(struct device *dev,
66 struct device_attribute *attr, char *buf)
68 struct drm_device *ddev = dev_get_drvdata(dev);
69 struct amdgpu_device *adev = ddev->dev_private;
71 return snprintf(buf, PAGE_SIZE, "%llu\n",
72 amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT]));
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);
81 * amdgpu_gtt_mgr_init - init GTT manager and DRM MM
83 * @man: TTM memory type manager
84 * @p_size: maximum size of GTT
86 * Allocate and initialize the GTT manager.
88 static int amdgpu_gtt_mgr_init(struct ttm_mem_type_manager *man,
91 struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
92 struct amdgpu_gtt_mgr *mgr;
96 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
100 start = AMDGPU_GTT_MAX_TRANSFER_SIZE * AMDGPU_GTT_NUM_TRANSFER_WINDOWS;
101 size = (adev->gmc.gart_size >> PAGE_SHIFT) - start;
102 drm_mm_init(&mgr->mm, start, size);
103 spin_lock_init(&mgr->lock);
104 atomic64_set(&mgr->available, p_size);
107 ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_total);
109 DRM_ERROR("Failed to create device file mem_info_gtt_total\n");
112 ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_used);
114 DRM_ERROR("Failed to create device file mem_info_gtt_used\n");
122 * amdgpu_gtt_mgr_fini - free and destroy GTT manager
124 * @man: TTM memory type manager
126 * Destroy and free the GTT manager, returns -EBUSY if ranges are still
127 * allocated inside it.
129 static int amdgpu_gtt_mgr_fini(struct ttm_mem_type_manager *man)
131 struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
132 struct amdgpu_gtt_mgr *mgr = man->priv;
133 spin_lock(&mgr->lock);
134 drm_mm_takedown(&mgr->mm);
135 spin_unlock(&mgr->lock);
139 device_remove_file(adev->dev, &dev_attr_mem_info_gtt_total);
140 device_remove_file(adev->dev, &dev_attr_mem_info_gtt_used);
146 * amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space
148 * @mem: the mem object to check
150 * Check if a mem object has already address space allocated.
152 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_mem_reg *mem)
154 struct amdgpu_gtt_node *node = mem->mm_node;
156 return (node->node.start != AMDGPU_BO_INVALID_OFFSET);
160 * amdgpu_gtt_mgr_alloc - allocate new ranges
162 * @man: TTM memory type manager
163 * @tbo: TTM BO we need this range for
164 * @place: placement flags and restrictions
165 * @mem: the resulting mem object
167 * Allocate the address space for a node.
169 static int amdgpu_gtt_mgr_alloc(struct ttm_mem_type_manager *man,
170 struct ttm_buffer_object *tbo,
171 const struct ttm_place *place,
172 struct ttm_mem_reg *mem)
174 struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
175 struct amdgpu_gtt_mgr *mgr = man->priv;
176 struct amdgpu_gtt_node *node = mem->mm_node;
177 enum drm_mm_insert_mode mode;
178 unsigned long fpfn, lpfn;
181 if (amdgpu_gtt_mgr_has_gart_addr(mem))
189 if (place && place->lpfn)
192 lpfn = adev->gart.num_cpu_pages;
194 mode = DRM_MM_INSERT_BEST;
195 if (place && place->flags & TTM_PL_FLAG_TOPDOWN)
196 mode = DRM_MM_INSERT_HIGH;
198 spin_lock(&mgr->lock);
199 r = drm_mm_insert_node_in_range(&mgr->mm, &node->node, mem->num_pages,
200 mem->page_alignment, 0, fpfn, lpfn,
202 spin_unlock(&mgr->lock);
205 mem->start = node->node.start;
211 * amdgpu_gtt_mgr_new - allocate a new node
213 * @man: TTM memory type manager
214 * @tbo: TTM BO we need this range for
215 * @place: placement flags and restrictions
216 * @mem: the resulting mem object
218 * Dummy, allocate the node but no space for it yet.
220 static int amdgpu_gtt_mgr_new(struct ttm_mem_type_manager *man,
221 struct ttm_buffer_object *tbo,
222 const struct ttm_place *place,
223 struct ttm_mem_reg *mem)
225 struct amdgpu_gtt_mgr *mgr = man->priv;
226 struct amdgpu_gtt_node *node;
229 spin_lock(&mgr->lock);
230 if ((&tbo->mem == mem || tbo->mem.mem_type != TTM_PL_TT) &&
231 atomic64_read(&mgr->available) < mem->num_pages) {
232 spin_unlock(&mgr->lock);
235 atomic64_sub(mem->num_pages, &mgr->available);
236 spin_unlock(&mgr->lock);
238 node = kzalloc(sizeof(*node), GFP_KERNEL);
244 node->node.start = AMDGPU_BO_INVALID_OFFSET;
245 node->node.size = mem->num_pages;
249 if (place->fpfn || place->lpfn || place->flags & TTM_PL_FLAG_TOPDOWN) {
250 r = amdgpu_gtt_mgr_alloc(man, tbo, place, mem);
258 mem->start = node->node.start;
263 atomic64_add(mem->num_pages, &mgr->available);
269 * amdgpu_gtt_mgr_del - free ranges
271 * @man: TTM memory type manager
272 * @tbo: TTM BO we need this range for
273 * @place: placement flags and restrictions
274 * @mem: TTM memory object
276 * Free the allocated GTT again.
278 static void amdgpu_gtt_mgr_del(struct ttm_mem_type_manager *man,
279 struct ttm_mem_reg *mem)
281 struct amdgpu_gtt_mgr *mgr = man->priv;
282 struct amdgpu_gtt_node *node = mem->mm_node;
287 spin_lock(&mgr->lock);
288 if (node->node.start != AMDGPU_BO_INVALID_OFFSET)
289 drm_mm_remove_node(&node->node);
290 spin_unlock(&mgr->lock);
291 atomic64_add(mem->num_pages, &mgr->available);
298 * amdgpu_gtt_mgr_usage - return usage of GTT domain
300 * @man: TTM memory type manager
302 * Return how many bytes are used in the GTT domain
304 uint64_t amdgpu_gtt_mgr_usage(struct ttm_mem_type_manager *man)
306 struct amdgpu_gtt_mgr *mgr = man->priv;
307 s64 result = man->size - atomic64_read(&mgr->available);
309 return (result > 0 ? result : 0) * PAGE_SIZE;
312 int amdgpu_gtt_mgr_recover(struct ttm_mem_type_manager *man)
314 struct amdgpu_gtt_mgr *mgr = man->priv;
315 struct amdgpu_gtt_node *node;
316 struct drm_mm_node *mm_node;
319 spin_lock(&mgr->lock);
320 drm_mm_for_each_node(mm_node, &mgr->mm) {
321 node = container_of(mm_node, struct amdgpu_gtt_node, node);
322 r = amdgpu_ttm_recover_gart(node->tbo);
326 spin_unlock(&mgr->lock);
332 * amdgpu_gtt_mgr_debug - dump VRAM table
334 * @man: TTM memory type manager
335 * @printer: DRM printer to use
337 * Dump the table content using printk.
339 static void amdgpu_gtt_mgr_debug(struct ttm_mem_type_manager *man,
340 struct drm_printer *printer)
342 struct amdgpu_gtt_mgr *mgr = man->priv;
344 spin_lock(&mgr->lock);
345 drm_mm_print(&mgr->mm, printer);
346 spin_unlock(&mgr->lock);
348 drm_printf(printer, "man size:%llu pages, gtt available:%lld pages, usage:%lluMB\n",
349 man->size, (u64)atomic64_read(&mgr->available),
350 amdgpu_gtt_mgr_usage(man) >> 20);
353 const struct ttm_mem_type_manager_func amdgpu_gtt_mgr_func = {
354 .init = amdgpu_gtt_mgr_init,
355 .takedown = amdgpu_gtt_mgr_fini,
356 .get_node = amdgpu_gtt_mgr_new,
357 .put_node = amdgpu_gtt_mgr_del,
358 .debug = amdgpu_gtt_mgr_debug