]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c
drm/ttm: rename ttm_mem_reg to ttm_resource.
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_gtt_mgr.c
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 struct amdgpu_gtt_mgr {
28         struct ttm_resource_manager manager;
29         struct drm_mm mm;
30         spinlock_t lock;
31         atomic64_t available;
32 };
33
34 static inline struct amdgpu_gtt_mgr *to_gtt_mgr(struct ttm_resource_manager *man)
35 {
36         return container_of(man, struct amdgpu_gtt_mgr, manager);
37 }
38
39 struct amdgpu_gtt_node {
40         struct drm_mm_node node;
41         struct ttm_buffer_object *tbo;
42 };
43
44 /**
45  * DOC: mem_info_gtt_total
46  *
47  * The amdgpu driver provides a sysfs API for reporting current total size of
48  * the GTT.
49  * The file mem_info_gtt_total is used for this, and returns the total size of
50  * the GTT block, in bytes
51  */
52 static ssize_t amdgpu_mem_info_gtt_total_show(struct device *dev,
53                 struct device_attribute *attr, char *buf)
54 {
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);
60 }
61
62 /**
63  * DOC: mem_info_gtt_used
64  *
65  * The amdgpu driver provides a sysfs API for reporting current total amount of
66  * used GTT.
67  * The file mem_info_gtt_used is used for this, and returns the current used
68  * size of the GTT block, in bytes
69  */
70 static ssize_t amdgpu_mem_info_gtt_used_show(struct device *dev,
71                 struct device_attribute *attr, char *buf)
72 {
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));
78 }
79
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);
84
85 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func;
86 /**
87  * amdgpu_gtt_mgr_init - init GTT manager and DRM MM
88  *
89  * @man: TTM memory type manager
90  * @p_size: maximum size of GTT
91  *
92  * Allocate and initialize the GTT manager.
93  */
94 int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size)
95 {
96         struct ttm_resource_manager *man;
97         struct amdgpu_gtt_mgr *mgr;
98         uint64_t start, size;
99         int ret;
100
101         mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
102         if (!mgr)
103                 return -ENOMEM;
104
105         man = &mgr->manager;
106         man->use_tt = true;
107         man->func = &amdgpu_gtt_mgr_func;
108         man->available_caching = TTM_PL_MASK_CACHING;
109         man->default_caching = TTM_PL_FLAG_CACHED;
110
111         ttm_resource_manager_init(man, gtt_size >> PAGE_SHIFT);
112
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);
118
119         ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_total);
120         if (ret) {
121                 DRM_ERROR("Failed to create device file mem_info_gtt_total\n");
122                 return ret;
123         }
124         ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_used);
125         if (ret) {
126                 DRM_ERROR("Failed to create device file mem_info_gtt_used\n");
127                 return ret;
128         }
129
130         ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, &mgr->manager);
131         ttm_resource_manager_set_used(man, true);
132         return 0;
133 }
134
135 /**
136  * amdgpu_gtt_mgr_fini - free and destroy GTT manager
137  *
138  * @man: TTM memory type manager
139  *
140  * Destroy and free the GTT manager, returns -EBUSY if ranges are still
141  * allocated inside it.
142  */
143 void amdgpu_gtt_mgr_fini(struct amdgpu_device *adev)
144 {
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);
147         int ret;
148
149         ttm_resource_manager_set_used(man, false);
150
151         ret = ttm_resource_manager_force_list_clean(&adev->mman.bdev, man);
152         if (ret)
153                 return;
154
155         spin_lock(&mgr->lock);
156         drm_mm_takedown(&mgr->mm);
157         spin_unlock(&mgr->lock);
158
159         device_remove_file(adev->dev, &dev_attr_mem_info_gtt_total);
160         device_remove_file(adev->dev, &dev_attr_mem_info_gtt_used);
161
162         ttm_resource_manager_cleanup(man);
163         ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, NULL);
164         kfree(mgr);
165 }
166
167 /**
168  * amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space
169  *
170  * @mem: the mem object to check
171  *
172  * Check if a mem object has already address space allocated.
173  */
174 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem)
175 {
176         return mem->mm_node != NULL;
177 }
178
179 /**
180  * amdgpu_gtt_mgr_new - allocate a new node
181  *
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
186  *
187  * Dummy, allocate the node but no space for it yet.
188  */
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)
193 {
194         struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
195         struct amdgpu_gtt_node *node;
196         int r;
197
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);
202                 return -ENOSPC;
203         }
204         atomic64_sub(mem->num_pages, &mgr->available);
205         spin_unlock(&mgr->lock);
206
207         if (!place->lpfn) {
208                 mem->mm_node = NULL;
209                 mem->start = AMDGPU_BO_INVALID_OFFSET;
210                 return 0;
211         }
212
213         node = kzalloc(sizeof(*node), GFP_KERNEL);
214         if (!node) {
215                 r = -ENOMEM;
216                 goto err_out;
217         }
218
219         node->tbo = tbo;
220
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);
226
227         if (unlikely(r))
228                 goto err_free;
229
230         mem->mm_node = node;
231         mem->start = node->node.start;
232
233         return 0;
234
235 err_free:
236         kfree(node);
237
238 err_out:
239         atomic64_add(mem->num_pages, &mgr->available);
240
241         return r;
242 }
243
244 /**
245  * amdgpu_gtt_mgr_del - free ranges
246  *
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
251  *
252  * Free the allocated GTT again.
253  */
254 static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
255                                struct ttm_resource *mem)
256 {
257         struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
258         struct amdgpu_gtt_node *node = mem->mm_node;
259
260         if (node) {
261                 spin_lock(&mgr->lock);
262                 drm_mm_remove_node(&node->node);
263                 spin_unlock(&mgr->lock);
264                 kfree(node);
265         }
266
267         atomic64_add(mem->num_pages, &mgr->available);
268 }
269
270 /**
271  * amdgpu_gtt_mgr_usage - return usage of GTT domain
272  *
273  * @man: TTM memory type manager
274  *
275  * Return how many bytes are used in the GTT domain
276  */
277 uint64_t amdgpu_gtt_mgr_usage(struct ttm_resource_manager *man)
278 {
279         struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
280         s64 result = man->size - atomic64_read(&mgr->available);
281
282         return (result > 0 ? result : 0) * PAGE_SIZE;
283 }
284
285 int amdgpu_gtt_mgr_recover(struct ttm_resource_manager *man)
286 {
287         struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
288         struct amdgpu_gtt_node *node;
289         struct drm_mm_node *mm_node;
290         int r = 0;
291
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);
296                 if (r)
297                         break;
298         }
299         spin_unlock(&mgr->lock);
300
301         return r;
302 }
303
304 /**
305  * amdgpu_gtt_mgr_debug - dump VRAM table
306  *
307  * @man: TTM memory type manager
308  * @printer: DRM printer to use
309  *
310  * Dump the table content using printk.
311  */
312 static void amdgpu_gtt_mgr_debug(struct ttm_resource_manager *man,
313                                  struct drm_printer *printer)
314 {
315         struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
316
317         spin_lock(&mgr->lock);
318         drm_mm_print(&mgr->mm, printer);
319         spin_unlock(&mgr->lock);
320
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);
324 }
325
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
330 };
This page took 0.053133 seconds and 4 git commands to generate.