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
27 struct amdgpu_gtt_mgr {
28 struct ttm_resource_manager manager;
34 static inline struct amdgpu_gtt_mgr *to_gtt_mgr(struct ttm_resource_manager *man)
36 return container_of(man, struct amdgpu_gtt_mgr, manager);
39 struct amdgpu_gtt_node {
40 struct drm_mm_node node;
41 struct ttm_buffer_object *tbo;
45 * DOC: mem_info_gtt_total
47 * The amdgpu driver provides a sysfs API for reporting current total size of
49 * The file mem_info_gtt_total is used for this, and returns the total size of
50 * the GTT block, in bytes
52 static ssize_t amdgpu_mem_info_gtt_total_show(struct device *dev,
53 struct device_attribute *attr, char *buf)
55 struct drm_device *ddev = dev_get_drvdata(dev);
56 struct amdgpu_device *adev = ddev->dev_private;
57 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
58 return snprintf(buf, PAGE_SIZE, "%llu\n",
59 man->size * PAGE_SIZE);
63 * DOC: mem_info_gtt_used
65 * The amdgpu driver provides a sysfs API for reporting current total amount of
67 * The file mem_info_gtt_used is used for this, and returns the current used
68 * size of the GTT block, in bytes
70 static ssize_t amdgpu_mem_info_gtt_used_show(struct device *dev,
71 struct device_attribute *attr, char *buf)
73 struct drm_device *ddev = dev_get_drvdata(dev);
74 struct amdgpu_device *adev = ddev->dev_private;
75 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
76 return snprintf(buf, PAGE_SIZE, "%llu\n",
77 amdgpu_gtt_mgr_usage(man));
80 static DEVICE_ATTR(mem_info_gtt_total, S_IRUGO,
81 amdgpu_mem_info_gtt_total_show, NULL);
82 static DEVICE_ATTR(mem_info_gtt_used, S_IRUGO,
83 amdgpu_mem_info_gtt_used_show, NULL);
85 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func;
87 * amdgpu_gtt_mgr_init - init GTT manager and DRM MM
89 * @man: TTM memory type manager
90 * @p_size: maximum size of GTT
92 * Allocate and initialize the GTT manager.
94 int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size)
96 struct ttm_resource_manager *man;
97 struct amdgpu_gtt_mgr *mgr;
101 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
107 man->func = &amdgpu_gtt_mgr_func;
108 man->available_caching = TTM_PL_MASK_CACHING;
109 man->default_caching = TTM_PL_FLAG_CACHED;
111 ttm_resource_manager_init(man, gtt_size >> PAGE_SHIFT);
113 start = AMDGPU_GTT_MAX_TRANSFER_SIZE * AMDGPU_GTT_NUM_TRANSFER_WINDOWS;
114 size = (adev->gmc.gart_size >> PAGE_SHIFT) - start;
115 drm_mm_init(&mgr->mm, start, size);
116 spin_lock_init(&mgr->lock);
117 atomic64_set(&mgr->available, gtt_size >> PAGE_SHIFT);
119 ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_total);
121 DRM_ERROR("Failed to create device file mem_info_gtt_total\n");
124 ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_used);
126 DRM_ERROR("Failed to create device file mem_info_gtt_used\n");
130 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, &mgr->manager);
131 ttm_resource_manager_set_used(man, true);
136 * amdgpu_gtt_mgr_fini - free and destroy GTT manager
138 * @man: TTM memory type manager
140 * Destroy and free the GTT manager, returns -EBUSY if ranges are still
141 * allocated inside it.
143 void amdgpu_gtt_mgr_fini(struct amdgpu_device *adev)
145 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
146 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
149 ttm_resource_manager_set_used(man, false);
151 ret = ttm_resource_manager_force_list_clean(&adev->mman.bdev, man);
155 spin_lock(&mgr->lock);
156 drm_mm_takedown(&mgr->mm);
157 spin_unlock(&mgr->lock);
159 device_remove_file(adev->dev, &dev_attr_mem_info_gtt_total);
160 device_remove_file(adev->dev, &dev_attr_mem_info_gtt_used);
162 ttm_resource_manager_cleanup(man);
163 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, NULL);
168 * amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space
170 * @mem: the mem object to check
172 * Check if a mem object has already address space allocated.
174 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem)
176 return mem->mm_node != NULL;
180 * amdgpu_gtt_mgr_new - allocate a new node
182 * @man: TTM memory type manager
183 * @tbo: TTM BO we need this range for
184 * @place: placement flags and restrictions
185 * @mem: the resulting mem object
187 * Dummy, allocate the node but no space for it yet.
189 static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
190 struct ttm_buffer_object *tbo,
191 const struct ttm_place *place,
192 struct ttm_resource *mem)
194 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
195 struct amdgpu_gtt_node *node;
198 spin_lock(&mgr->lock);
199 if ((&tbo->mem == mem || tbo->mem.mem_type != TTM_PL_TT) &&
200 atomic64_read(&mgr->available) < mem->num_pages) {
201 spin_unlock(&mgr->lock);
204 atomic64_sub(mem->num_pages, &mgr->available);
205 spin_unlock(&mgr->lock);
209 mem->start = AMDGPU_BO_INVALID_OFFSET;
213 node = kzalloc(sizeof(*node), GFP_KERNEL);
221 spin_lock(&mgr->lock);
222 r = drm_mm_insert_node_in_range(&mgr->mm, &node->node, mem->num_pages,
223 mem->page_alignment, 0, place->fpfn,
224 place->lpfn, DRM_MM_INSERT_BEST);
225 spin_unlock(&mgr->lock);
231 mem->start = node->node.start;
239 atomic64_add(mem->num_pages, &mgr->available);
245 * amdgpu_gtt_mgr_del - free ranges
247 * @man: TTM memory type manager
248 * @tbo: TTM BO we need this range for
249 * @place: placement flags and restrictions
250 * @mem: TTM memory object
252 * Free the allocated GTT again.
254 static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
255 struct ttm_resource *mem)
257 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
258 struct amdgpu_gtt_node *node = mem->mm_node;
261 spin_lock(&mgr->lock);
262 drm_mm_remove_node(&node->node);
263 spin_unlock(&mgr->lock);
267 atomic64_add(mem->num_pages, &mgr->available);
271 * amdgpu_gtt_mgr_usage - return usage of GTT domain
273 * @man: TTM memory type manager
275 * Return how many bytes are used in the GTT domain
277 uint64_t amdgpu_gtt_mgr_usage(struct ttm_resource_manager *man)
279 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
280 s64 result = man->size - atomic64_read(&mgr->available);
282 return (result > 0 ? result : 0) * PAGE_SIZE;
285 int amdgpu_gtt_mgr_recover(struct ttm_resource_manager *man)
287 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
288 struct amdgpu_gtt_node *node;
289 struct drm_mm_node *mm_node;
292 spin_lock(&mgr->lock);
293 drm_mm_for_each_node(mm_node, &mgr->mm) {
294 node = container_of(mm_node, struct amdgpu_gtt_node, node);
295 r = amdgpu_ttm_recover_gart(node->tbo);
299 spin_unlock(&mgr->lock);
305 * amdgpu_gtt_mgr_debug - dump VRAM table
307 * @man: TTM memory type manager
308 * @printer: DRM printer to use
310 * Dump the table content using printk.
312 static void amdgpu_gtt_mgr_debug(struct ttm_resource_manager *man,
313 struct drm_printer *printer)
315 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
317 spin_lock(&mgr->lock);
318 drm_mm_print(&mgr->mm, printer);
319 spin_unlock(&mgr->lock);
321 drm_printf(printer, "man size:%llu pages, gtt available:%lld pages, usage:%lluMB\n",
322 man->size, (u64)atomic64_read(&mgr->available),
323 amdgpu_gtt_mgr_usage(man) >> 20);
326 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func = {
327 .get_node = amdgpu_gtt_mgr_new,
328 .put_node = amdgpu_gtt_mgr_del,
329 .debug = amdgpu_gtt_mgr_debug