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 static inline struct amdgpu_gtt_mgr *to_gtt_mgr(struct ttm_resource_manager *man)
29 return container_of(man, struct amdgpu_gtt_mgr, manager);
32 struct amdgpu_gtt_node {
33 struct drm_mm_node node;
34 struct ttm_buffer_object *tbo;
38 * DOC: mem_info_gtt_total
40 * The amdgpu driver provides a sysfs API for reporting current total size of
42 * The file mem_info_gtt_total is used for this, and returns the total size of
43 * the GTT block, in bytes
45 static ssize_t amdgpu_mem_info_gtt_total_show(struct device *dev,
46 struct device_attribute *attr, char *buf)
48 struct drm_device *ddev = dev_get_drvdata(dev);
49 struct amdgpu_device *adev = drm_to_adev(ddev);
50 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
52 return snprintf(buf, PAGE_SIZE, "%llu\n",
53 man->size * PAGE_SIZE);
57 * DOC: mem_info_gtt_used
59 * The amdgpu driver provides a sysfs API for reporting current total amount of
61 * The file mem_info_gtt_used is used for this, and returns the current used
62 * size of the GTT block, in bytes
64 static ssize_t amdgpu_mem_info_gtt_used_show(struct device *dev,
65 struct device_attribute *attr, char *buf)
67 struct drm_device *ddev = dev_get_drvdata(dev);
68 struct amdgpu_device *adev = drm_to_adev(ddev);
69 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
71 return snprintf(buf, PAGE_SIZE, "%llu\n",
72 amdgpu_gtt_mgr_usage(man));
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);
80 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func;
82 * amdgpu_gtt_mgr_init - init GTT manager and DRM MM
84 * @adev: amdgpu_device pointer
85 * @gtt_size: maximum size of GTT
87 * Allocate and initialize the GTT manager.
89 int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size)
91 struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr;
92 struct ttm_resource_manager *man = &mgr->manager;
97 man->func = &amdgpu_gtt_mgr_func;
99 ttm_resource_manager_init(man, gtt_size >> PAGE_SHIFT);
101 start = AMDGPU_GTT_MAX_TRANSFER_SIZE * AMDGPU_GTT_NUM_TRANSFER_WINDOWS;
102 size = (adev->gmc.gart_size >> PAGE_SHIFT) - start;
103 drm_mm_init(&mgr->mm, start, size);
104 spin_lock_init(&mgr->lock);
105 atomic64_set(&mgr->available, gtt_size >> PAGE_SHIFT);
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");
118 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, &mgr->manager);
119 ttm_resource_manager_set_used(man, true);
124 * amdgpu_gtt_mgr_fini - free and destroy GTT manager
126 * @adev: amdgpu_device pointer
128 * Destroy and free the GTT manager, returns -EBUSY if ranges are still
129 * allocated inside it.
131 void amdgpu_gtt_mgr_fini(struct amdgpu_device *adev)
133 struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr;
134 struct ttm_resource_manager *man = &mgr->manager;
137 ttm_resource_manager_set_used(man, false);
139 ret = ttm_resource_manager_force_list_clean(&adev->mman.bdev, man);
143 spin_lock(&mgr->lock);
144 drm_mm_takedown(&mgr->mm);
145 spin_unlock(&mgr->lock);
147 device_remove_file(adev->dev, &dev_attr_mem_info_gtt_total);
148 device_remove_file(adev->dev, &dev_attr_mem_info_gtt_used);
150 ttm_resource_manager_cleanup(man);
151 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, NULL);
155 * amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space
157 * @mem: the mem object to check
159 * Check if a mem object has already address space allocated.
161 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem)
163 return mem->mm_node != NULL;
167 * amdgpu_gtt_mgr_new - allocate a new node
169 * @man: TTM memory type manager
170 * @tbo: TTM BO we need this range for
171 * @place: placement flags and restrictions
172 * @mem: the resulting mem object
174 * Dummy, allocate the node but no space for it yet.
176 static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
177 struct ttm_buffer_object *tbo,
178 const struct ttm_place *place,
179 struct ttm_resource *mem)
181 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
182 struct amdgpu_gtt_node *node;
185 spin_lock(&mgr->lock);
186 if ((&tbo->mem == mem || tbo->mem.mem_type != TTM_PL_TT) &&
187 atomic64_read(&mgr->available) < mem->num_pages) {
188 spin_unlock(&mgr->lock);
191 atomic64_sub(mem->num_pages, &mgr->available);
192 spin_unlock(&mgr->lock);
196 mem->start = AMDGPU_BO_INVALID_OFFSET;
200 node = kzalloc(sizeof(*node), GFP_KERNEL);
208 spin_lock(&mgr->lock);
209 r = drm_mm_insert_node_in_range(&mgr->mm, &node->node, mem->num_pages,
210 mem->page_alignment, 0, place->fpfn,
211 place->lpfn, DRM_MM_INSERT_BEST);
212 spin_unlock(&mgr->lock);
218 mem->start = node->node.start;
226 atomic64_add(mem->num_pages, &mgr->available);
232 * amdgpu_gtt_mgr_del - free ranges
234 * @man: TTM memory type manager
235 * @mem: TTM memory object
237 * Free the allocated GTT again.
239 static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
240 struct ttm_resource *mem)
242 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
243 struct amdgpu_gtt_node *node = mem->mm_node;
246 spin_lock(&mgr->lock);
247 drm_mm_remove_node(&node->node);
248 spin_unlock(&mgr->lock);
252 atomic64_add(mem->num_pages, &mgr->available);
256 * amdgpu_gtt_mgr_usage - return usage of GTT domain
258 * @man: TTM memory type manager
260 * Return how many bytes are used in the GTT domain
262 uint64_t amdgpu_gtt_mgr_usage(struct ttm_resource_manager *man)
264 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
265 s64 result = man->size - atomic64_read(&mgr->available);
267 return (result > 0 ? result : 0) * PAGE_SIZE;
270 int amdgpu_gtt_mgr_recover(struct ttm_resource_manager *man)
272 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
273 struct amdgpu_gtt_node *node;
274 struct drm_mm_node *mm_node;
277 spin_lock(&mgr->lock);
278 drm_mm_for_each_node(mm_node, &mgr->mm) {
279 node = container_of(mm_node, struct amdgpu_gtt_node, node);
280 r = amdgpu_ttm_recover_gart(node->tbo);
284 spin_unlock(&mgr->lock);
290 * amdgpu_gtt_mgr_debug - dump VRAM table
292 * @man: TTM memory type manager
293 * @printer: DRM printer to use
295 * Dump the table content using printk.
297 static void amdgpu_gtt_mgr_debug(struct ttm_resource_manager *man,
298 struct drm_printer *printer)
300 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
302 spin_lock(&mgr->lock);
303 drm_mm_print(&mgr->mm, printer);
304 spin_unlock(&mgr->lock);
306 drm_printf(printer, "man size:%llu pages, gtt available:%lld pages, usage:%lluMB\n",
307 man->size, (u64)atomic64_read(&mgr->available),
308 amdgpu_gtt_mgr_usage(man) >> 20);
311 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func = {
312 .alloc = amdgpu_gtt_mgr_new,
313 .free = amdgpu_gtt_mgr_del,
314 .debug = amdgpu_gtt_mgr_debug