1 /*
2  * Copyright 2016 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Authors: Christian König
23  */
24 
25 #include "amdgpu.h"
26 
27 static inline struct amdgpu_gtt_mgr *to_gtt_mgr(struct ttm_resource_manager *man)
28 {
29 	return container_of(man, struct amdgpu_gtt_mgr, manager);
30 }
31 
32 struct amdgpu_gtt_node {
33 	struct drm_mm_node node;
34 	struct ttm_buffer_object *tbo;
35 };
36 
37 /**
38  * DOC: mem_info_gtt_total
39  *
40  * The amdgpu driver provides a sysfs API for reporting current total size of
41  * the GTT.
42  * The file mem_info_gtt_total is used for this, and returns the total size of
43  * the GTT block, in bytes
44  */
45 static ssize_t amdgpu_mem_info_gtt_total_show(struct device *dev,
46 		struct device_attribute *attr, char *buf)
47 {
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);
51 
52 	return snprintf(buf, PAGE_SIZE, "%llu\n",
53 			man->size * PAGE_SIZE);
54 }
55 
56 /**
57  * DOC: mem_info_gtt_used
58  *
59  * The amdgpu driver provides a sysfs API for reporting current total amount of
60  * used GTT.
61  * The file mem_info_gtt_used is used for this, and returns the current used
62  * size of the GTT block, in bytes
63  */
64 static ssize_t amdgpu_mem_info_gtt_used_show(struct device *dev,
65 		struct device_attribute *attr, char *buf)
66 {
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);
70 
71 	return snprintf(buf, PAGE_SIZE, "%llu\n",
72 			amdgpu_gtt_mgr_usage(man));
73 }
74 
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);
79 
80 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func;
81 /**
82  * amdgpu_gtt_mgr_init - init GTT manager and DRM MM
83  *
84  * @adev: amdgpu_device pointer
85  * @gtt_size: maximum size of GTT
86  *
87  * Allocate and initialize the GTT manager.
88  */
89 int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size)
90 {
91 	struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr;
92 	struct ttm_resource_manager *man = &mgr->manager;
93 	uint64_t start, size;
94 	int ret;
95 
96 	man->use_tt = true;
97 	man->func = &amdgpu_gtt_mgr_func;
98 	man->available_caching = TTM_PL_MASK_CACHING;
99 	man->default_caching = TTM_PL_FLAG_CACHED;
100 
101 	ttm_resource_manager_init(man, gtt_size >> PAGE_SHIFT);
102 
103 	start = AMDGPU_GTT_MAX_TRANSFER_SIZE * AMDGPU_GTT_NUM_TRANSFER_WINDOWS;
104 	size = (adev->gmc.gart_size >> PAGE_SHIFT) - start;
105 	drm_mm_init(&mgr->mm, start, size);
106 	spin_lock_init(&mgr->lock);
107 	atomic64_set(&mgr->available, gtt_size >> PAGE_SHIFT);
108 
109 	ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_total);
110 	if (ret) {
111 		DRM_ERROR("Failed to create device file mem_info_gtt_total\n");
112 		return ret;
113 	}
114 	ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_used);
115 	if (ret) {
116 		DRM_ERROR("Failed to create device file mem_info_gtt_used\n");
117 		return ret;
118 	}
119 
120 	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, &mgr->manager);
121 	ttm_resource_manager_set_used(man, true);
122 	return 0;
123 }
124 
125 /**
126  * amdgpu_gtt_mgr_fini - free and destroy GTT manager
127  *
128  * @adev: amdgpu_device pointer
129  *
130  * Destroy and free the GTT manager, returns -EBUSY if ranges are still
131  * allocated inside it.
132  */
133 void amdgpu_gtt_mgr_fini(struct amdgpu_device *adev)
134 {
135 	struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr;
136 	struct ttm_resource_manager *man = &mgr->manager;
137 	int ret;
138 
139 	ttm_resource_manager_set_used(man, false);
140 
141 	ret = ttm_resource_manager_force_list_clean(&adev->mman.bdev, man);
142 	if (ret)
143 		return;
144 
145 	spin_lock(&mgr->lock);
146 	drm_mm_takedown(&mgr->mm);
147 	spin_unlock(&mgr->lock);
148 
149 	device_remove_file(adev->dev, &dev_attr_mem_info_gtt_total);
150 	device_remove_file(adev->dev, &dev_attr_mem_info_gtt_used);
151 
152 	ttm_resource_manager_cleanup(man);
153 	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, NULL);
154 }
155 
156 /**
157  * amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space
158  *
159  * @mem: the mem object to check
160  *
161  * Check if a mem object has already address space allocated.
162  */
163 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem)
164 {
165 	return mem->mm_node != NULL;
166 }
167 
168 /**
169  * amdgpu_gtt_mgr_new - allocate a new node
170  *
171  * @man: TTM memory type manager
172  * @tbo: TTM BO we need this range for
173  * @place: placement flags and restrictions
174  * @mem: the resulting mem object
175  *
176  * Dummy, allocate the node but no space for it yet.
177  */
178 static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
179 			      struct ttm_buffer_object *tbo,
180 			      const struct ttm_place *place,
181 			      struct ttm_resource *mem)
182 {
183 	struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
184 	struct amdgpu_gtt_node *node;
185 	int r;
186 
187 	spin_lock(&mgr->lock);
188 	if ((&tbo->mem == mem || tbo->mem.mem_type != TTM_PL_TT) &&
189 	    atomic64_read(&mgr->available) < mem->num_pages) {
190 		spin_unlock(&mgr->lock);
191 		return -ENOSPC;
192 	}
193 	atomic64_sub(mem->num_pages, &mgr->available);
194 	spin_unlock(&mgr->lock);
195 
196 	if (!place->lpfn) {
197 		mem->mm_node = NULL;
198 		mem->start = AMDGPU_BO_INVALID_OFFSET;
199 		return 0;
200 	}
201 
202 	node = kzalloc(sizeof(*node), GFP_KERNEL);
203 	if (!node) {
204 		r = -ENOMEM;
205 		goto err_out;
206 	}
207 
208 	node->tbo = tbo;
209 
210 	spin_lock(&mgr->lock);
211 	r = drm_mm_insert_node_in_range(&mgr->mm, &node->node, mem->num_pages,
212 					mem->page_alignment, 0, place->fpfn,
213 					place->lpfn, DRM_MM_INSERT_BEST);
214 	spin_unlock(&mgr->lock);
215 
216 	if (unlikely(r))
217 		goto err_free;
218 
219 	mem->mm_node = node;
220 	mem->start = node->node.start;
221 
222 	return 0;
223 
224 err_free:
225 	kfree(node);
226 
227 err_out:
228 	atomic64_add(mem->num_pages, &mgr->available);
229 
230 	return r;
231 }
232 
233 /**
234  * amdgpu_gtt_mgr_del - free ranges
235  *
236  * @man: TTM memory type manager
237  * @mem: TTM memory object
238  *
239  * Free the allocated GTT again.
240  */
241 static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
242 			       struct ttm_resource *mem)
243 {
244 	struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
245 	struct amdgpu_gtt_node *node = mem->mm_node;
246 
247 	if (node) {
248 		spin_lock(&mgr->lock);
249 		drm_mm_remove_node(&node->node);
250 		spin_unlock(&mgr->lock);
251 		kfree(node);
252 	}
253 
254 	atomic64_add(mem->num_pages, &mgr->available);
255 }
256 
257 /**
258  * amdgpu_gtt_mgr_usage - return usage of GTT domain
259  *
260  * @man: TTM memory type manager
261  *
262  * Return how many bytes are used in the GTT domain
263  */
264 uint64_t amdgpu_gtt_mgr_usage(struct ttm_resource_manager *man)
265 {
266 	struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
267 	s64 result = man->size - atomic64_read(&mgr->available);
268 
269 	return (result > 0 ? result : 0) * PAGE_SIZE;
270 }
271 
272 int amdgpu_gtt_mgr_recover(struct ttm_resource_manager *man)
273 {
274 	struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
275 	struct amdgpu_gtt_node *node;
276 	struct drm_mm_node *mm_node;
277 	int r = 0;
278 
279 	spin_lock(&mgr->lock);
280 	drm_mm_for_each_node(mm_node, &mgr->mm) {
281 		node = container_of(mm_node, struct amdgpu_gtt_node, node);
282 		r = amdgpu_ttm_recover_gart(node->tbo);
283 		if (r)
284 			break;
285 	}
286 	spin_unlock(&mgr->lock);
287 
288 	return r;
289 }
290 
291 /**
292  * amdgpu_gtt_mgr_debug - dump VRAM table
293  *
294  * @man: TTM memory type manager
295  * @printer: DRM printer to use
296  *
297  * Dump the table content using printk.
298  */
299 static void amdgpu_gtt_mgr_debug(struct ttm_resource_manager *man,
300 				 struct drm_printer *printer)
301 {
302 	struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
303 
304 	spin_lock(&mgr->lock);
305 	drm_mm_print(&mgr->mm, printer);
306 	spin_unlock(&mgr->lock);
307 
308 	drm_printf(printer, "man size:%llu pages, gtt available:%lld pages, usage:%lluMB\n",
309 		   man->size, (u64)atomic64_read(&mgr->available),
310 		   amdgpu_gtt_mgr_usage(man) >> 20);
311 }
312 
313 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func = {
314 	.alloc = amdgpu_gtt_mgr_new,
315 	.free = amdgpu_gtt_mgr_del,
316 	.debug = amdgpu_gtt_mgr_debug
317 };
318