]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c
drm/amdgpu: flush gart changes after all BO recovery
[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 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 /**
84  * amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space
85  *
86  * @mem: the mem object to check
87  *
88  * Check if a mem object has already address space allocated.
89  */
90 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem)
91 {
92         return mem->mm_node != NULL;
93 }
94
95 /**
96  * amdgpu_gtt_mgr_new - allocate a new node
97  *
98  * @man: TTM memory type manager
99  * @tbo: TTM BO we need this range for
100  * @place: placement flags and restrictions
101  * @mem: the resulting mem object
102  *
103  * Dummy, allocate the node but no space for it yet.
104  */
105 static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
106                               struct ttm_buffer_object *tbo,
107                               const struct ttm_place *place,
108                               struct ttm_resource *mem)
109 {
110         struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
111         struct amdgpu_gtt_node *node;
112         int r;
113
114         spin_lock(&mgr->lock);
115         if ((&tbo->mem == mem || tbo->mem.mem_type != TTM_PL_TT) &&
116             atomic64_read(&mgr->available) < mem->num_pages) {
117                 spin_unlock(&mgr->lock);
118                 return -ENOSPC;
119         }
120         atomic64_sub(mem->num_pages, &mgr->available);
121         spin_unlock(&mgr->lock);
122
123         if (!place->lpfn) {
124                 mem->mm_node = NULL;
125                 mem->start = AMDGPU_BO_INVALID_OFFSET;
126                 return 0;
127         }
128
129         node = kzalloc(sizeof(*node), GFP_KERNEL);
130         if (!node) {
131                 r = -ENOMEM;
132                 goto err_out;
133         }
134
135         node->tbo = tbo;
136
137         spin_lock(&mgr->lock);
138         r = drm_mm_insert_node_in_range(&mgr->mm, &node->node, mem->num_pages,
139                                         tbo->page_alignment, 0, place->fpfn,
140                                         place->lpfn, DRM_MM_INSERT_BEST);
141         spin_unlock(&mgr->lock);
142
143         if (unlikely(r))
144                 goto err_free;
145
146         mem->mm_node = node;
147         mem->start = node->node.start;
148
149         return 0;
150
151 err_free:
152         kfree(node);
153
154 err_out:
155         atomic64_add(mem->num_pages, &mgr->available);
156
157         return r;
158 }
159
160 /**
161  * amdgpu_gtt_mgr_del - free ranges
162  *
163  * @man: TTM memory type manager
164  * @mem: TTM memory object
165  *
166  * Free the allocated GTT again.
167  */
168 static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
169                                struct ttm_resource *mem)
170 {
171         struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
172         struct amdgpu_gtt_node *node = mem->mm_node;
173
174         if (node) {
175                 spin_lock(&mgr->lock);
176                 drm_mm_remove_node(&node->node);
177                 spin_unlock(&mgr->lock);
178                 kfree(node);
179         }
180
181         atomic64_add(mem->num_pages, &mgr->available);
182 }
183
184 /**
185  * amdgpu_gtt_mgr_usage - return usage of GTT domain
186  *
187  * @man: TTM memory type manager
188  *
189  * Return how many bytes are used in the GTT domain
190  */
191 uint64_t amdgpu_gtt_mgr_usage(struct ttm_resource_manager *man)
192 {
193         struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
194         s64 result = man->size - atomic64_read(&mgr->available);
195
196         return (result > 0 ? result : 0) * PAGE_SIZE;
197 }
198
199 /**
200  * amdgpu_gtt_mgr_recover - re-init gart
201  *
202  * @man: TTM memory type manager
203  *
204  * Re-init the gart for each known BO in the GTT.
205  */
206 int amdgpu_gtt_mgr_recover(struct ttm_resource_manager *man)
207 {
208         struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
209         struct amdgpu_device *adev;
210         struct amdgpu_gtt_node *node;
211         struct drm_mm_node *mm_node;
212         int r = 0;
213
214         adev = container_of(mgr, typeof(*adev), mman.gtt_mgr);
215         spin_lock(&mgr->lock);
216         drm_mm_for_each_node(mm_node, &mgr->mm) {
217                 node = container_of(mm_node, struct amdgpu_gtt_node, node);
218                 r = amdgpu_ttm_recover_gart(node->tbo);
219                 if (r)
220                         break;
221         }
222         spin_unlock(&mgr->lock);
223
224         amdgpu_gart_invalidate_tlb(adev);
225
226         return r;
227 }
228
229 /**
230  * amdgpu_gtt_mgr_debug - dump VRAM table
231  *
232  * @man: TTM memory type manager
233  * @printer: DRM printer to use
234  *
235  * Dump the table content using printk.
236  */
237 static void amdgpu_gtt_mgr_debug(struct ttm_resource_manager *man,
238                                  struct drm_printer *printer)
239 {
240         struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
241
242         spin_lock(&mgr->lock);
243         drm_mm_print(&mgr->mm, printer);
244         spin_unlock(&mgr->lock);
245
246         drm_printf(printer, "man size:%llu pages, gtt available:%lld pages, usage:%lluMB\n",
247                    man->size, (u64)atomic64_read(&mgr->available),
248                    amdgpu_gtt_mgr_usage(man) >> 20);
249 }
250
251 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func = {
252         .alloc = amdgpu_gtt_mgr_new,
253         .free = amdgpu_gtt_mgr_del,
254         .debug = amdgpu_gtt_mgr_debug
255 };
256
257 /**
258  * amdgpu_gtt_mgr_init - init GTT manager and DRM MM
259  *
260  * @adev: amdgpu_device pointer
261  * @gtt_size: maximum size of GTT
262  *
263  * Allocate and initialize the GTT manager.
264  */
265 int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size)
266 {
267         struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr;
268         struct ttm_resource_manager *man = &mgr->manager;
269         uint64_t start, size;
270         int ret;
271
272         man->use_tt = true;
273         man->func = &amdgpu_gtt_mgr_func;
274
275         ttm_resource_manager_init(man, gtt_size >> PAGE_SHIFT);
276
277         start = AMDGPU_GTT_MAX_TRANSFER_SIZE * AMDGPU_GTT_NUM_TRANSFER_WINDOWS;
278         size = (adev->gmc.gart_size >> PAGE_SHIFT) - start;
279         drm_mm_init(&mgr->mm, start, size);
280         spin_lock_init(&mgr->lock);
281         atomic64_set(&mgr->available, gtt_size >> PAGE_SHIFT);
282
283         ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_total);
284         if (ret) {
285                 DRM_ERROR("Failed to create device file mem_info_gtt_total\n");
286                 return ret;
287         }
288         ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_used);
289         if (ret) {
290                 DRM_ERROR("Failed to create device file mem_info_gtt_used\n");
291                 return ret;
292         }
293
294         ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, &mgr->manager);
295         ttm_resource_manager_set_used(man, true);
296         return 0;
297 }
298
299 /**
300  * amdgpu_gtt_mgr_fini - free and destroy GTT manager
301  *
302  * @adev: amdgpu_device pointer
303  *
304  * Destroy and free the GTT manager, returns -EBUSY if ranges are still
305  * allocated inside it.
306  */
307 void amdgpu_gtt_mgr_fini(struct amdgpu_device *adev)
308 {
309         struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr;
310         struct ttm_resource_manager *man = &mgr->manager;
311         int ret;
312
313         ttm_resource_manager_set_used(man, false);
314
315         ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man);
316         if (ret)
317                 return;
318
319         spin_lock(&mgr->lock);
320         drm_mm_takedown(&mgr->mm);
321         spin_unlock(&mgr->lock);
322
323         device_remove_file(adev->dev, &dev_attr_mem_info_gtt_total);
324         device_remove_file(adev->dev, &dev_attr_mem_info_gtt_used);
325
326         ttm_resource_manager_cleanup(man);
327         ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, NULL);
328 }
This page took 0.05436 seconds and 4 git commands to generate.