1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev * Copyright 2011 Red Hat Inc.
3b843c749SSergey Zigachev * All Rights Reserved.
4b843c749SSergey Zigachev *
5b843c749SSergey Zigachev * Permission is hereby granted, free of charge, to any person obtaining a
6b843c749SSergey Zigachev * copy of this software and associated documentation files (the
7b843c749SSergey Zigachev * "Software"), to deal in the Software without restriction, including
8b843c749SSergey Zigachev * without limitation the rights to use, copy, modify, merge, publish,
9b843c749SSergey Zigachev * distribute, sub license, and/or sell copies of the Software, and to
10b843c749SSergey Zigachev * permit persons to whom the Software is furnished to do so, subject to
11b843c749SSergey Zigachev * the following conditions:
12b843c749SSergey Zigachev *
13b843c749SSergey Zigachev * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14b843c749SSergey Zigachev * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15b843c749SSergey Zigachev * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16b843c749SSergey Zigachev * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17b843c749SSergey Zigachev * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18b843c749SSergey Zigachev * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19b843c749SSergey Zigachev * USE OR OTHER DEALINGS IN THE SOFTWARE.
20b843c749SSergey Zigachev *
21b843c749SSergey Zigachev * The above copyright notice and this permission notice (including the
22b843c749SSergey Zigachev * next paragraph) shall be included in all copies or substantial portions
23b843c749SSergey Zigachev * of the Software.
24b843c749SSergey Zigachev *
25b843c749SSergey Zigachev */
26b843c749SSergey Zigachev /*
27b843c749SSergey Zigachev * Authors:
28b843c749SSergey Zigachev * Jerome Glisse <glisse@freedesktop.org>
29b843c749SSergey Zigachev */
30b843c749SSergey Zigachev /* Algorithm:
31b843c749SSergey Zigachev *
32b843c749SSergey Zigachev * We store the last allocated bo in "hole", we always try to allocate
33b843c749SSergey Zigachev * after the last allocated bo. Principle is that in a linear GPU ring
34b843c749SSergey Zigachev * progression was is after last is the oldest bo we allocated and thus
35b843c749SSergey Zigachev * the first one that should no longer be in use by the GPU.
36b843c749SSergey Zigachev *
37b843c749SSergey Zigachev * If it's not the case we skip over the bo after last to the closest
38b843c749SSergey Zigachev * done bo if such one exist. If none exist and we are not asked to
39b843c749SSergey Zigachev * block we report failure to allocate.
40b843c749SSergey Zigachev *
41b843c749SSergey Zigachev * If we are asked to block we wait on all the oldest fence of all
42b843c749SSergey Zigachev * rings. We just wait for any of those fence to complete.
43b843c749SSergey Zigachev */
44b843c749SSergey Zigachev #include <drm/drmP.h>
45b843c749SSergey Zigachev #include "amdgpu.h"
46b843c749SSergey Zigachev
47b843c749SSergey Zigachev static void amdgpu_sa_bo_remove_locked(struct amdgpu_sa_bo *sa_bo);
48b843c749SSergey Zigachev static void amdgpu_sa_bo_try_free(struct amdgpu_sa_manager *sa_manager);
49b843c749SSergey Zigachev
amdgpu_sa_bo_manager_init(struct amdgpu_device * adev,struct amdgpu_sa_manager * sa_manager,unsigned size,u32 align,u32 domain)50b843c749SSergey Zigachev int amdgpu_sa_bo_manager_init(struct amdgpu_device *adev,
51b843c749SSergey Zigachev struct amdgpu_sa_manager *sa_manager,
52b843c749SSergey Zigachev unsigned size, u32 align, u32 domain)
53b843c749SSergey Zigachev {
54b843c749SSergey Zigachev int i, r;
55b843c749SSergey Zigachev
56b843c749SSergey Zigachev init_waitqueue_head(&sa_manager->wq);
57b843c749SSergey Zigachev sa_manager->bo = NULL;
58b843c749SSergey Zigachev sa_manager->size = size;
59b843c749SSergey Zigachev sa_manager->domain = domain;
60b843c749SSergey Zigachev sa_manager->align = align;
61b843c749SSergey Zigachev sa_manager->hole = &sa_manager->olist;
62b843c749SSergey Zigachev INIT_LIST_HEAD(&sa_manager->olist);
63b843c749SSergey Zigachev for (i = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i)
64b843c749SSergey Zigachev INIT_LIST_HEAD(&sa_manager->flist[i]);
65b843c749SSergey Zigachev
66b843c749SSergey Zigachev r = amdgpu_bo_create_kernel(adev, size, align, domain, &sa_manager->bo,
67*78973132SSergey Zigachev (u64 *)&sa_manager->gpu_addr, &sa_manager->cpu_ptr);
68b843c749SSergey Zigachev if (r) {
69b843c749SSergey Zigachev dev_err(adev->dev, "(%d) failed to allocate bo for manager\n", r);
70b843c749SSergey Zigachev return r;
71b843c749SSergey Zigachev }
72b843c749SSergey Zigachev
73b843c749SSergey Zigachev memset(sa_manager->cpu_ptr, 0, sa_manager->size);
74b843c749SSergey Zigachev return r;
75b843c749SSergey Zigachev }
76b843c749SSergey Zigachev
amdgpu_sa_bo_manager_fini(struct amdgpu_device * adev,struct amdgpu_sa_manager * sa_manager)77b843c749SSergey Zigachev void amdgpu_sa_bo_manager_fini(struct amdgpu_device *adev,
78b843c749SSergey Zigachev struct amdgpu_sa_manager *sa_manager)
79b843c749SSergey Zigachev {
80b843c749SSergey Zigachev struct amdgpu_sa_bo *sa_bo, *tmp;
81b843c749SSergey Zigachev
82b843c749SSergey Zigachev if (sa_manager->bo == NULL) {
83b843c749SSergey Zigachev dev_err(adev->dev, "no bo for sa manager\n");
84b843c749SSergey Zigachev return;
85b843c749SSergey Zigachev }
86b843c749SSergey Zigachev
87b843c749SSergey Zigachev if (!list_empty(&sa_manager->olist)) {
88b843c749SSergey Zigachev sa_manager->hole = &sa_manager->olist,
89b843c749SSergey Zigachev amdgpu_sa_bo_try_free(sa_manager);
90b843c749SSergey Zigachev if (!list_empty(&sa_manager->olist)) {
91b843c749SSergey Zigachev dev_err(adev->dev, "sa_manager is not empty, clearing anyway\n");
92b843c749SSergey Zigachev }
93b843c749SSergey Zigachev }
94b843c749SSergey Zigachev list_for_each_entry_safe(sa_bo, tmp, &sa_manager->olist, olist) {
95b843c749SSergey Zigachev amdgpu_sa_bo_remove_locked(sa_bo);
96b843c749SSergey Zigachev }
97b843c749SSergey Zigachev
98*78973132SSergey Zigachev amdgpu_bo_free_kernel(&sa_manager->bo, (u64 *)&sa_manager->gpu_addr, &sa_manager->cpu_ptr);
99b843c749SSergey Zigachev sa_manager->size = 0;
100b843c749SSergey Zigachev }
101b843c749SSergey Zigachev
amdgpu_sa_bo_remove_locked(struct amdgpu_sa_bo * sa_bo)102b843c749SSergey Zigachev static void amdgpu_sa_bo_remove_locked(struct amdgpu_sa_bo *sa_bo)
103b843c749SSergey Zigachev {
104b843c749SSergey Zigachev struct amdgpu_sa_manager *sa_manager = sa_bo->manager;
105b843c749SSergey Zigachev if (sa_manager->hole == &sa_bo->olist) {
106b843c749SSergey Zigachev sa_manager->hole = sa_bo->olist.prev;
107b843c749SSergey Zigachev }
108b843c749SSergey Zigachev list_del_init(&sa_bo->olist);
109b843c749SSergey Zigachev list_del_init(&sa_bo->flist);
110b843c749SSergey Zigachev dma_fence_put(sa_bo->fence);
111b843c749SSergey Zigachev kfree(sa_bo);
112b843c749SSergey Zigachev }
113b843c749SSergey Zigachev
amdgpu_sa_bo_try_free(struct amdgpu_sa_manager * sa_manager)114b843c749SSergey Zigachev static void amdgpu_sa_bo_try_free(struct amdgpu_sa_manager *sa_manager)
115b843c749SSergey Zigachev {
116b843c749SSergey Zigachev struct amdgpu_sa_bo *sa_bo, *tmp;
117b843c749SSergey Zigachev
118b843c749SSergey Zigachev if (sa_manager->hole->next == &sa_manager->olist)
119b843c749SSergey Zigachev return;
120b843c749SSergey Zigachev
121b843c749SSergey Zigachev sa_bo = list_entry(sa_manager->hole->next, struct amdgpu_sa_bo, olist);
122b843c749SSergey Zigachev list_for_each_entry_safe_from(sa_bo, tmp, &sa_manager->olist, olist) {
123b843c749SSergey Zigachev if (sa_bo->fence == NULL ||
124b843c749SSergey Zigachev !dma_fence_is_signaled(sa_bo->fence)) {
125b843c749SSergey Zigachev return;
126b843c749SSergey Zigachev }
127b843c749SSergey Zigachev amdgpu_sa_bo_remove_locked(sa_bo);
128b843c749SSergey Zigachev }
129b843c749SSergey Zigachev }
130b843c749SSergey Zigachev
amdgpu_sa_bo_hole_soffset(struct amdgpu_sa_manager * sa_manager)131b843c749SSergey Zigachev static inline unsigned amdgpu_sa_bo_hole_soffset(struct amdgpu_sa_manager *sa_manager)
132b843c749SSergey Zigachev {
133b843c749SSergey Zigachev struct list_head *hole = sa_manager->hole;
134b843c749SSergey Zigachev
135b843c749SSergey Zigachev if (hole != &sa_manager->olist) {
136b843c749SSergey Zigachev return list_entry(hole, struct amdgpu_sa_bo, olist)->eoffset;
137b843c749SSergey Zigachev }
138b843c749SSergey Zigachev return 0;
139b843c749SSergey Zigachev }
140b843c749SSergey Zigachev
amdgpu_sa_bo_hole_eoffset(struct amdgpu_sa_manager * sa_manager)141b843c749SSergey Zigachev static inline unsigned amdgpu_sa_bo_hole_eoffset(struct amdgpu_sa_manager *sa_manager)
142b843c749SSergey Zigachev {
143b843c749SSergey Zigachev struct list_head *hole = sa_manager->hole;
144b843c749SSergey Zigachev
145b843c749SSergey Zigachev if (hole->next != &sa_manager->olist) {
146b843c749SSergey Zigachev return list_entry(hole->next, struct amdgpu_sa_bo, olist)->soffset;
147b843c749SSergey Zigachev }
148b843c749SSergey Zigachev return sa_manager->size;
149b843c749SSergey Zigachev }
150b843c749SSergey Zigachev
amdgpu_sa_bo_try_alloc(struct amdgpu_sa_manager * sa_manager,struct amdgpu_sa_bo * sa_bo,unsigned size,unsigned align)151b843c749SSergey Zigachev static bool amdgpu_sa_bo_try_alloc(struct amdgpu_sa_manager *sa_manager,
152b843c749SSergey Zigachev struct amdgpu_sa_bo *sa_bo,
153b843c749SSergey Zigachev unsigned size, unsigned align)
154b843c749SSergey Zigachev {
155b843c749SSergey Zigachev unsigned soffset, eoffset, wasted;
156b843c749SSergey Zigachev
157b843c749SSergey Zigachev soffset = amdgpu_sa_bo_hole_soffset(sa_manager);
158b843c749SSergey Zigachev eoffset = amdgpu_sa_bo_hole_eoffset(sa_manager);
159b843c749SSergey Zigachev wasted = (align - (soffset % align)) % align;
160b843c749SSergey Zigachev
161b843c749SSergey Zigachev if ((eoffset - soffset) >= (size + wasted)) {
162b843c749SSergey Zigachev soffset += wasted;
163b843c749SSergey Zigachev
164b843c749SSergey Zigachev sa_bo->manager = sa_manager;
165b843c749SSergey Zigachev sa_bo->soffset = soffset;
166b843c749SSergey Zigachev sa_bo->eoffset = soffset + size;
167b843c749SSergey Zigachev list_add(&sa_bo->olist, sa_manager->hole);
168b843c749SSergey Zigachev INIT_LIST_HEAD(&sa_bo->flist);
169b843c749SSergey Zigachev sa_manager->hole = &sa_bo->olist;
170b843c749SSergey Zigachev return true;
171b843c749SSergey Zigachev }
172b843c749SSergey Zigachev return false;
173b843c749SSergey Zigachev }
174b843c749SSergey Zigachev
175b843c749SSergey Zigachev /**
176b843c749SSergey Zigachev * amdgpu_sa_event - Check if we can stop waiting
177b843c749SSergey Zigachev *
178b843c749SSergey Zigachev * @sa_manager: pointer to the sa_manager
179b843c749SSergey Zigachev * @size: number of bytes we want to allocate
180b843c749SSergey Zigachev * @align: alignment we need to match
181b843c749SSergey Zigachev *
182b843c749SSergey Zigachev * Check if either there is a fence we can wait for or
183b843c749SSergey Zigachev * enough free memory to satisfy the allocation directly
184b843c749SSergey Zigachev */
amdgpu_sa_event(struct amdgpu_sa_manager * sa_manager,unsigned size,unsigned align)185b843c749SSergey Zigachev static bool amdgpu_sa_event(struct amdgpu_sa_manager *sa_manager,
186b843c749SSergey Zigachev unsigned size, unsigned align)
187b843c749SSergey Zigachev {
188b843c749SSergey Zigachev unsigned soffset, eoffset, wasted;
189b843c749SSergey Zigachev int i;
190b843c749SSergey Zigachev
191b843c749SSergey Zigachev for (i = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i)
192b843c749SSergey Zigachev if (!list_empty(&sa_manager->flist[i]))
193b843c749SSergey Zigachev return true;
194b843c749SSergey Zigachev
195b843c749SSergey Zigachev soffset = amdgpu_sa_bo_hole_soffset(sa_manager);
196b843c749SSergey Zigachev eoffset = amdgpu_sa_bo_hole_eoffset(sa_manager);
197b843c749SSergey Zigachev wasted = (align - (soffset % align)) % align;
198b843c749SSergey Zigachev
199b843c749SSergey Zigachev if ((eoffset - soffset) >= (size + wasted)) {
200b843c749SSergey Zigachev return true;
201b843c749SSergey Zigachev }
202b843c749SSergey Zigachev
203b843c749SSergey Zigachev return false;
204b843c749SSergey Zigachev }
205b843c749SSergey Zigachev
amdgpu_sa_bo_next_hole(struct amdgpu_sa_manager * sa_manager,struct dma_fence ** fences,unsigned * tries)206b843c749SSergey Zigachev static bool amdgpu_sa_bo_next_hole(struct amdgpu_sa_manager *sa_manager,
207b843c749SSergey Zigachev struct dma_fence **fences,
208b843c749SSergey Zigachev unsigned *tries)
209b843c749SSergey Zigachev {
210b843c749SSergey Zigachev struct amdgpu_sa_bo *best_bo = NULL;
211b843c749SSergey Zigachev unsigned i, soffset, best, tmp;
212b843c749SSergey Zigachev
213b843c749SSergey Zigachev /* if hole points to the end of the buffer */
214b843c749SSergey Zigachev if (sa_manager->hole->next == &sa_manager->olist) {
215b843c749SSergey Zigachev /* try again with its beginning */
216b843c749SSergey Zigachev sa_manager->hole = &sa_manager->olist;
217b843c749SSergey Zigachev return true;
218b843c749SSergey Zigachev }
219b843c749SSergey Zigachev
220b843c749SSergey Zigachev soffset = amdgpu_sa_bo_hole_soffset(sa_manager);
221b843c749SSergey Zigachev /* to handle wrap around we add sa_manager->size */
222b843c749SSergey Zigachev best = sa_manager->size * 2;
223b843c749SSergey Zigachev /* go over all fence list and try to find the closest sa_bo
224b843c749SSergey Zigachev * of the current last
225b843c749SSergey Zigachev */
226b843c749SSergey Zigachev for (i = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i) {
227b843c749SSergey Zigachev struct amdgpu_sa_bo *sa_bo;
228b843c749SSergey Zigachev
229b843c749SSergey Zigachev if (list_empty(&sa_manager->flist[i]))
230b843c749SSergey Zigachev continue;
231b843c749SSergey Zigachev
232b843c749SSergey Zigachev sa_bo = list_first_entry(&sa_manager->flist[i],
233b843c749SSergey Zigachev struct amdgpu_sa_bo, flist);
234b843c749SSergey Zigachev
235b843c749SSergey Zigachev if (!dma_fence_is_signaled(sa_bo->fence)) {
236b843c749SSergey Zigachev fences[i] = sa_bo->fence;
237b843c749SSergey Zigachev continue;
238b843c749SSergey Zigachev }
239b843c749SSergey Zigachev
240b843c749SSergey Zigachev /* limit the number of tries each ring gets */
241b843c749SSergey Zigachev if (tries[i] > 2) {
242b843c749SSergey Zigachev continue;
243b843c749SSergey Zigachev }
244b843c749SSergey Zigachev
245b843c749SSergey Zigachev tmp = sa_bo->soffset;
246b843c749SSergey Zigachev if (tmp < soffset) {
247b843c749SSergey Zigachev /* wrap around, pretend it's after */
248b843c749SSergey Zigachev tmp += sa_manager->size;
249b843c749SSergey Zigachev }
250b843c749SSergey Zigachev tmp -= soffset;
251b843c749SSergey Zigachev if (tmp < best) {
252b843c749SSergey Zigachev /* this sa bo is the closest one */
253b843c749SSergey Zigachev best = tmp;
254b843c749SSergey Zigachev best_bo = sa_bo;
255b843c749SSergey Zigachev }
256b843c749SSergey Zigachev }
257b843c749SSergey Zigachev
258b843c749SSergey Zigachev if (best_bo) {
259b843c749SSergey Zigachev uint32_t idx = best_bo->fence->context;
260b843c749SSergey Zigachev
261b843c749SSergey Zigachev idx %= AMDGPU_SA_NUM_FENCE_LISTS;
262b843c749SSergey Zigachev ++tries[idx];
263b843c749SSergey Zigachev sa_manager->hole = best_bo->olist.prev;
264b843c749SSergey Zigachev
265b843c749SSergey Zigachev /* we knew that this one is signaled,
266b843c749SSergey Zigachev so it's save to remote it */
267b843c749SSergey Zigachev amdgpu_sa_bo_remove_locked(best_bo);
268b843c749SSergey Zigachev return true;
269b843c749SSergey Zigachev }
270b843c749SSergey Zigachev return false;
271b843c749SSergey Zigachev }
272b843c749SSergey Zigachev
amdgpu_sa_bo_new(struct amdgpu_sa_manager * sa_manager,struct amdgpu_sa_bo ** sa_bo,unsigned size,unsigned align)273b843c749SSergey Zigachev int amdgpu_sa_bo_new(struct amdgpu_sa_manager *sa_manager,
274b843c749SSergey Zigachev struct amdgpu_sa_bo **sa_bo,
275b843c749SSergey Zigachev unsigned size, unsigned align)
276b843c749SSergey Zigachev {
277b843c749SSergey Zigachev struct dma_fence *fences[AMDGPU_SA_NUM_FENCE_LISTS];
278b843c749SSergey Zigachev unsigned tries[AMDGPU_SA_NUM_FENCE_LISTS];
279b843c749SSergey Zigachev unsigned count;
280b843c749SSergey Zigachev int i, r;
281b843c749SSergey Zigachev signed long t;
282b843c749SSergey Zigachev
283b843c749SSergey Zigachev if (WARN_ON_ONCE(align > sa_manager->align))
284b843c749SSergey Zigachev return -EINVAL;
285b843c749SSergey Zigachev
286b843c749SSergey Zigachev if (WARN_ON_ONCE(size > sa_manager->size))
287b843c749SSergey Zigachev return -EINVAL;
288b843c749SSergey Zigachev
289*78973132SSergey Zigachev *sa_bo = kmalloc(sizeof(struct amdgpu_sa_bo), M_DRM, GFP_KERNEL);
290b843c749SSergey Zigachev if (!(*sa_bo))
291b843c749SSergey Zigachev return -ENOMEM;
292b843c749SSergey Zigachev (*sa_bo)->manager = sa_manager;
293b843c749SSergey Zigachev (*sa_bo)->fence = NULL;
294b843c749SSergey Zigachev INIT_LIST_HEAD(&(*sa_bo)->olist);
295b843c749SSergey Zigachev INIT_LIST_HEAD(&(*sa_bo)->flist);
296b843c749SSergey Zigachev
297*78973132SSergey Zigachev lockmgr(&sa_manager->wq.lock, LK_EXCLUSIVE);
298b843c749SSergey Zigachev do {
299b843c749SSergey Zigachev for (i = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i) {
300b843c749SSergey Zigachev fences[i] = NULL;
301b843c749SSergey Zigachev tries[i] = 0;
302b843c749SSergey Zigachev }
303b843c749SSergey Zigachev
304b843c749SSergey Zigachev do {
305b843c749SSergey Zigachev amdgpu_sa_bo_try_free(sa_manager);
306b843c749SSergey Zigachev
307b843c749SSergey Zigachev if (amdgpu_sa_bo_try_alloc(sa_manager, *sa_bo,
308b843c749SSergey Zigachev size, align)) {
309*78973132SSergey Zigachev lockmgr(&sa_manager->wq.lock, LK_RELEASE);
310b843c749SSergey Zigachev return 0;
311b843c749SSergey Zigachev }
312b843c749SSergey Zigachev
313b843c749SSergey Zigachev /* see if we can skip over some allocations */
314b843c749SSergey Zigachev } while (amdgpu_sa_bo_next_hole(sa_manager, fences, tries));
315b843c749SSergey Zigachev
316b843c749SSergey Zigachev for (i = 0, count = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i)
317b843c749SSergey Zigachev if (fences[i])
318b843c749SSergey Zigachev fences[count++] = dma_fence_get(fences[i]);
319b843c749SSergey Zigachev
320b843c749SSergey Zigachev if (count) {
321*78973132SSergey Zigachev lockmgr(&sa_manager->wq.lock, LK_RELEASE);
322b843c749SSergey Zigachev t = dma_fence_wait_any_timeout(fences, count, false,
323b843c749SSergey Zigachev MAX_SCHEDULE_TIMEOUT,
324b843c749SSergey Zigachev NULL);
325b843c749SSergey Zigachev for (i = 0; i < count; ++i)
326b843c749SSergey Zigachev dma_fence_put(fences[i]);
327b843c749SSergey Zigachev
328b843c749SSergey Zigachev r = (t > 0) ? 0 : t;
329*78973132SSergey Zigachev lockmgr(&sa_manager->wq.lock, LK_EXCLUSIVE);
330b843c749SSergey Zigachev } else {
331b843c749SSergey Zigachev /* if we have nothing to wait for block */
332b843c749SSergey Zigachev r = wait_event_interruptible_locked(
333b843c749SSergey Zigachev sa_manager->wq,
334b843c749SSergey Zigachev amdgpu_sa_event(sa_manager, size, align)
335b843c749SSergey Zigachev );
336b843c749SSergey Zigachev }
337b843c749SSergey Zigachev
338b843c749SSergey Zigachev } while (!r);
339b843c749SSergey Zigachev
340*78973132SSergey Zigachev lockmgr(&sa_manager->wq.lock, LK_RELEASE);
341b843c749SSergey Zigachev kfree(*sa_bo);
342b843c749SSergey Zigachev *sa_bo = NULL;
343b843c749SSergey Zigachev return r;
344b843c749SSergey Zigachev }
345b843c749SSergey Zigachev
amdgpu_sa_bo_free(struct amdgpu_device * adev,struct amdgpu_sa_bo ** sa_bo,struct dma_fence * fence)346b843c749SSergey Zigachev void amdgpu_sa_bo_free(struct amdgpu_device *adev, struct amdgpu_sa_bo **sa_bo,
347b843c749SSergey Zigachev struct dma_fence *fence)
348b843c749SSergey Zigachev {
349b843c749SSergey Zigachev struct amdgpu_sa_manager *sa_manager;
350b843c749SSergey Zigachev
351b843c749SSergey Zigachev if (sa_bo == NULL || *sa_bo == NULL) {
352b843c749SSergey Zigachev return;
353b843c749SSergey Zigachev }
354b843c749SSergey Zigachev
355b843c749SSergey Zigachev sa_manager = (*sa_bo)->manager;
356*78973132SSergey Zigachev lockmgr(&sa_manager->wq.lock, LK_EXCLUSIVE);
357b843c749SSergey Zigachev if (fence && !dma_fence_is_signaled(fence)) {
358b843c749SSergey Zigachev uint32_t idx;
359b843c749SSergey Zigachev
360b843c749SSergey Zigachev (*sa_bo)->fence = dma_fence_get(fence);
361b843c749SSergey Zigachev idx = fence->context % AMDGPU_SA_NUM_FENCE_LISTS;
362b843c749SSergey Zigachev list_add_tail(&(*sa_bo)->flist, &sa_manager->flist[idx]);
363b843c749SSergey Zigachev } else {
364b843c749SSergey Zigachev amdgpu_sa_bo_remove_locked(*sa_bo);
365b843c749SSergey Zigachev }
366b843c749SSergey Zigachev wake_up_all_locked(&sa_manager->wq);
367*78973132SSergey Zigachev lockmgr(&sa_manager->wq.lock, LK_RELEASE);
368b843c749SSergey Zigachev *sa_bo = NULL;
369b843c749SSergey Zigachev }
370b843c749SSergey Zigachev
371b843c749SSergey Zigachev #if defined(CONFIG_DEBUG_FS)
372b843c749SSergey Zigachev
amdgpu_sa_bo_dump_debug_info(struct amdgpu_sa_manager * sa_manager,struct seq_file * m)373b843c749SSergey Zigachev void amdgpu_sa_bo_dump_debug_info(struct amdgpu_sa_manager *sa_manager,
374b843c749SSergey Zigachev struct seq_file *m)
375b843c749SSergey Zigachev {
376b843c749SSergey Zigachev struct amdgpu_sa_bo *i;
377b843c749SSergey Zigachev
378*78973132SSergey Zigachev lockmgr(&sa_manager->wq.lock, LK_EXCLUSIVE);
379b843c749SSergey Zigachev list_for_each_entry(i, &sa_manager->olist, olist) {
380b843c749SSergey Zigachev uint64_t soffset = i->soffset + sa_manager->gpu_addr;
381b843c749SSergey Zigachev uint64_t eoffset = i->eoffset + sa_manager->gpu_addr;
382b843c749SSergey Zigachev if (&i->olist == sa_manager->hole) {
383b843c749SSergey Zigachev seq_printf(m, ">");
384b843c749SSergey Zigachev } else {
385b843c749SSergey Zigachev seq_printf(m, " ");
386b843c749SSergey Zigachev }
387b843c749SSergey Zigachev seq_printf(m, "[0x%010llx 0x%010llx] size %8lld",
388b843c749SSergey Zigachev soffset, eoffset, eoffset - soffset);
389b843c749SSergey Zigachev
390b843c749SSergey Zigachev if (i->fence)
391b843c749SSergey Zigachev seq_printf(m, " protected by 0x%08x on context %llu",
392b843c749SSergey Zigachev i->fence->seqno, i->fence->context);
393b843c749SSergey Zigachev
394b843c749SSergey Zigachev seq_printf(m, "\n");
395b843c749SSergey Zigachev }
396*78973132SSergey Zigachev lockmgr(&sa_manager->wq.lock, LK_RELEASE);
397b843c749SSergey Zigachev }
398b843c749SSergey Zigachev #endif
399