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 <drm/drmP.h>
26 #include "amdgpu.h"
27 
28 struct amdgpu_gtt_mgr {
29 	struct drm_mm mm;
30 	spinlock_t lock;
31 	atomic64_t available;
32 };
33 
34 struct amdgpu_gtt_node {
35 	struct drm_mm_node node;
36 	struct ttm_buffer_object *tbo;
37 };
38 
39 /**
40  * DOC: mem_info_gtt_total
41  *
42  * The amdgpu driver provides a sysfs API for reporting current total size of
43  * the GTT.
44  * The file mem_info_gtt_total is used for this, and returns the total size of
45  * the GTT block, in bytes
46  */
47 static ssize_t amdgpu_mem_info_gtt_total_show(struct device *dev,
48 		struct device_attribute *attr, char *buf)
49 {
50 	struct drm_device *ddev = dev_get_drvdata(dev);
51 	struct amdgpu_device *adev = ddev->dev_private;
52 
53 	return snprintf(buf, PAGE_SIZE, "%llu\n",
54 			(adev->mman.bdev.man[TTM_PL_TT].size) * PAGE_SIZE);
55 }
56 
57 /**
58  * DOC: mem_info_gtt_used
59  *
60  * The amdgpu driver provides a sysfs API for reporting current total amount of
61  * used GTT.
62  * The file mem_info_gtt_used is used for this, and returns the current used
63  * size of the GTT block, in bytes
64  */
65 static ssize_t amdgpu_mem_info_gtt_used_show(struct device *dev,
66 		struct device_attribute *attr, char *buf)
67 {
68 	struct drm_device *ddev = dev_get_drvdata(dev);
69 	struct amdgpu_device *adev = ddev->dev_private;
70 
71 	return snprintf(buf, PAGE_SIZE, "%llu\n",
72 			amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT]));
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 /**
81  * amdgpu_gtt_mgr_init - init GTT manager and DRM MM
82  *
83  * @man: TTM memory type manager
84  * @p_size: maximum size of GTT
85  *
86  * Allocate and initialize the GTT manager.
87  */
88 static int amdgpu_gtt_mgr_init(struct ttm_mem_type_manager *man,
89 			       unsigned long p_size)
90 {
91 	struct amdgpu_device *adev = amdgpu_ttm_adev(man->bdev);
92 	struct amdgpu_gtt_mgr *mgr;
93 	uint64_t start, size;
94 	int ret;
95 
96 	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
97 	if (!mgr)
98 		return -ENOMEM;
99 
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);
105 	man->priv = mgr;
106 
107 	ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_total);
108 	if (ret) {
109 		DRM_ERROR("Failed to create device file mem_info_gtt_total\n");
110 		return ret;
111 	}
112 	ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_used);
113 	if (ret) {
114 		DRM_ERROR("Failed to create device file mem_info_gtt_used\n");
115 		return ret;
116 	}
117 
118 	return 0;
119 }
120 
121 /**
122  * amdgpu_gtt_mgr_fini - free and destroy GTT manager
123  *
124  * @man: TTM memory type manager
125  *
126  * Destroy and free the GTT manager, returns -EBUSY if ranges are still
127  * allocated inside it.
128  */
129 static int amdgpu_gtt_mgr_fini(struct ttm_mem_type_manager *man)
130 {
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);
136 	kfree(mgr);
137 	man->priv = NULL;
138 
139 	device_remove_file(adev->dev, &dev_attr_mem_info_gtt_total);
140 	device_remove_file(adev->dev, &dev_attr_mem_info_gtt_used);
141 
142 	return 0;
143 }
144 
145 /**
146  * amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space
147  *
148  * @mem: the mem object to check
149  *
150  * Check if a mem object has already address space allocated.
151  */
152 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_mem_reg *mem)
153 {
154 	struct amdgpu_gtt_node *node = mem->mm_node;
155 
156 	return (node->node.start != AMDGPU_BO_INVALID_OFFSET);
157 }
158 
159 /**
160  * amdgpu_gtt_mgr_alloc - allocate new ranges
161  *
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
166  *
167  * Allocate the address space for a node.
168  */
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)
173 {
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;
179 	int r;
180 
181 	if (amdgpu_gtt_mgr_has_gart_addr(mem))
182 		return 0;
183 
184 	if (place)
185 		fpfn = place->fpfn;
186 	else
187 		fpfn = 0;
188 
189 	if (place && place->lpfn)
190 		lpfn = place->lpfn;
191 	else
192 		lpfn = adev->gart.num_cpu_pages;
193 
194 	mode = DRM_MM_INSERT_BEST;
195 	if (place && place->flags & TTM_PL_FLAG_TOPDOWN)
196 		mode = DRM_MM_INSERT_HIGH;
197 
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,
201 					mode);
202 	spin_unlock(&mgr->lock);
203 
204 	if (!r)
205 		mem->start = node->node.start;
206 
207 	return r;
208 }
209 
210 /**
211  * amdgpu_gtt_mgr_new - allocate a new node
212  *
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
217  *
218  * Dummy, allocate the node but no space for it yet.
219  */
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)
224 {
225 	struct amdgpu_gtt_mgr *mgr = man->priv;
226 	struct amdgpu_gtt_node *node;
227 	int r;
228 
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);
233 		return 0;
234 	}
235 	atomic64_sub(mem->num_pages, &mgr->available);
236 	spin_unlock(&mgr->lock);
237 
238 	node = kzalloc(sizeof(*node), GFP_KERNEL);
239 	if (!node) {
240 		r = -ENOMEM;
241 		goto err_out;
242 	}
243 
244 	node->node.start = AMDGPU_BO_INVALID_OFFSET;
245 	node->node.size = mem->num_pages;
246 	node->tbo = tbo;
247 	mem->mm_node = node;
248 
249 	if (place->fpfn || place->lpfn || place->flags & TTM_PL_FLAG_TOPDOWN) {
250 		r = amdgpu_gtt_mgr_alloc(man, tbo, place, mem);
251 		if (unlikely(r)) {
252 			kfree(node);
253 			mem->mm_node = NULL;
254 			r = 0;
255 			goto err_out;
256 		}
257 	} else {
258 		mem->start = node->node.start;
259 	}
260 
261 	return 0;
262 err_out:
263 	atomic64_add(mem->num_pages, &mgr->available);
264 
265 	return r;
266 }
267 
268 /**
269  * amdgpu_gtt_mgr_del - free ranges
270  *
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
275  *
276  * Free the allocated GTT again.
277  */
278 static void amdgpu_gtt_mgr_del(struct ttm_mem_type_manager *man,
279 			       struct ttm_mem_reg *mem)
280 {
281 	struct amdgpu_gtt_mgr *mgr = man->priv;
282 	struct amdgpu_gtt_node *node = mem->mm_node;
283 
284 	if (!node)
285 		return;
286 
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);
292 
293 	kfree(node);
294 	mem->mm_node = NULL;
295 }
296 
297 /**
298  * amdgpu_gtt_mgr_usage - return usage of GTT domain
299  *
300  * @man: TTM memory type manager
301  *
302  * Return how many bytes are used in the GTT domain
303  */
304 uint64_t amdgpu_gtt_mgr_usage(struct ttm_mem_type_manager *man)
305 {
306 	struct amdgpu_gtt_mgr *mgr = man->priv;
307 	s64 result = man->size - atomic64_read(&mgr->available);
308 
309 	return (result > 0 ? result : 0) * PAGE_SIZE;
310 }
311 
312 int amdgpu_gtt_mgr_recover(struct ttm_mem_type_manager *man)
313 {
314 	struct amdgpu_gtt_mgr *mgr = man->priv;
315 	struct amdgpu_gtt_node *node;
316 	struct drm_mm_node *mm_node;
317 	int r = 0;
318 
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);
323 		if (r)
324 			break;
325 	}
326 	spin_unlock(&mgr->lock);
327 
328 	return r;
329 }
330 
331 /**
332  * amdgpu_gtt_mgr_debug - dump VRAM table
333  *
334  * @man: TTM memory type manager
335  * @printer: DRM printer to use
336  *
337  * Dump the table content using printk.
338  */
339 static void amdgpu_gtt_mgr_debug(struct ttm_mem_type_manager *man,
340 				 struct drm_printer *printer)
341 {
342 	struct amdgpu_gtt_mgr *mgr = man->priv;
343 
344 	spin_lock(&mgr->lock);
345 	drm_mm_print(&mgr->mm, printer);
346 	spin_unlock(&mgr->lock);
347 
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);
351 }
352 
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
359 };
360