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