xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_sa.c (revision 63f2ec2a80f7867420ac39c966d68633ffde2b3a)
1*63f2ec2aSriastradh /*	$NetBSD: amdgpu_sa.c,v 1.5 2022/10/08 19:06:30 riastradh Exp $	*/
2efa246c0Sriastradh 
3efa246c0Sriastradh /*
4efa246c0Sriastradh  * Copyright 2011 Red Hat Inc.
5efa246c0Sriastradh  * All Rights Reserved.
6efa246c0Sriastradh  *
7efa246c0Sriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
8efa246c0Sriastradh  * copy of this software and associated documentation files (the
9efa246c0Sriastradh  * "Software"), to deal in the Software without restriction, including
10efa246c0Sriastradh  * without limitation the rights to use, copy, modify, merge, publish,
11efa246c0Sriastradh  * distribute, sub license, and/or sell copies of the Software, and to
12efa246c0Sriastradh  * permit persons to whom the Software is furnished to do so, subject to
13efa246c0Sriastradh  * the following conditions:
14efa246c0Sriastradh  *
15efa246c0Sriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16efa246c0Sriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17efa246c0Sriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18efa246c0Sriastradh  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
19efa246c0Sriastradh  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20efa246c0Sriastradh  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21efa246c0Sriastradh  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22efa246c0Sriastradh  *
23efa246c0Sriastradh  * The above copyright notice and this permission notice (including the
24efa246c0Sriastradh  * next paragraph) shall be included in all copies or substantial portions
25efa246c0Sriastradh  * of the Software.
26efa246c0Sriastradh  *
27efa246c0Sriastradh  */
28efa246c0Sriastradh /*
29efa246c0Sriastradh  * Authors:
30efa246c0Sriastradh  *    Jerome Glisse <glisse@freedesktop.org>
31efa246c0Sriastradh  */
32efa246c0Sriastradh /* Algorithm:
33efa246c0Sriastradh  *
34efa246c0Sriastradh  * We store the last allocated bo in "hole", we always try to allocate
35efa246c0Sriastradh  * after the last allocated bo. Principle is that in a linear GPU ring
36efa246c0Sriastradh  * progression was is after last is the oldest bo we allocated and thus
37efa246c0Sriastradh  * the first one that should no longer be in use by the GPU.
38efa246c0Sriastradh  *
39efa246c0Sriastradh  * If it's not the case we skip over the bo after last to the closest
40efa246c0Sriastradh  * done bo if such one exist. If none exist and we are not asked to
41efa246c0Sriastradh  * block we report failure to allocate.
42efa246c0Sriastradh  *
43efa246c0Sriastradh  * If we are asked to block we wait on all the oldest fence of all
44efa246c0Sriastradh  * rings. We just wait for any of those fence to complete.
45efa246c0Sriastradh  */
46efa246c0Sriastradh 
4741ec0267Sriastradh #include <sys/cdefs.h>
48*63f2ec2aSriastradh __KERNEL_RCSID(0, "$NetBSD: amdgpu_sa.c,v 1.5 2022/10/08 19:06:30 riastradh Exp $");
4941ec0267Sriastradh 
50efa246c0Sriastradh #include "amdgpu.h"
51efa246c0Sriastradh 
52efa246c0Sriastradh static void amdgpu_sa_bo_remove_locked(struct amdgpu_sa_bo *sa_bo);
53efa246c0Sriastradh static void amdgpu_sa_bo_try_free(struct amdgpu_sa_manager *sa_manager);
54efa246c0Sriastradh 
amdgpu_sa_bo_manager_init(struct amdgpu_device * adev,struct amdgpu_sa_manager * sa_manager,unsigned size,u32 align,u32 domain)55efa246c0Sriastradh int amdgpu_sa_bo_manager_init(struct amdgpu_device *adev,
56efa246c0Sriastradh 			      struct amdgpu_sa_manager *sa_manager,
57efa246c0Sriastradh 			      unsigned size, u32 align, u32 domain)
58efa246c0Sriastradh {
59efa246c0Sriastradh 	int i, r;
60efa246c0Sriastradh 
610d50c49dSriastradh 	spin_lock_init(&sa_manager->wq_lock);
620d50c49dSriastradh 	DRM_INIT_WAITQUEUE(&sa_manager->wq, "amdsabom");
63efa246c0Sriastradh 	sa_manager->bo = NULL;
64efa246c0Sriastradh 	sa_manager->size = size;
65efa246c0Sriastradh 	sa_manager->domain = domain;
66efa246c0Sriastradh 	sa_manager->align = align;
67efa246c0Sriastradh 	sa_manager->hole = &sa_manager->olist;
68efa246c0Sriastradh 	INIT_LIST_HEAD(&sa_manager->olist);
6941ec0267Sriastradh 	for (i = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i)
70efa246c0Sriastradh 		INIT_LIST_HEAD(&sa_manager->flist[i]);
71efa246c0Sriastradh 
7241ec0267Sriastradh 	r = amdgpu_bo_create_kernel(adev, size, align, domain, &sa_manager->bo,
7341ec0267Sriastradh 				&sa_manager->gpu_addr, &sa_manager->cpu_ptr);
74efa246c0Sriastradh 	if (r) {
75efa246c0Sriastradh 		dev_err(adev->dev, "(%d) failed to allocate bo for manager\n", r);
76efa246c0Sriastradh 		return r;
77efa246c0Sriastradh 	}
78efa246c0Sriastradh 
7941ec0267Sriastradh 	memset(sa_manager->cpu_ptr, 0, sa_manager->size);
80efa246c0Sriastradh 	return r;
81efa246c0Sriastradh }
82efa246c0Sriastradh 
amdgpu_sa_bo_manager_fini(struct amdgpu_device * adev,struct amdgpu_sa_manager * sa_manager)83efa246c0Sriastradh void amdgpu_sa_bo_manager_fini(struct amdgpu_device *adev,
84efa246c0Sriastradh                               struct amdgpu_sa_manager *sa_manager)
85efa246c0Sriastradh {
86efa246c0Sriastradh 	struct amdgpu_sa_bo *sa_bo, *tmp;
87efa246c0Sriastradh 
8841ec0267Sriastradh 	if (sa_manager->bo == NULL) {
8941ec0267Sriastradh 		dev_err(adev->dev, "no bo for sa manager\n");
9041ec0267Sriastradh 		return;
9141ec0267Sriastradh 	}
9241ec0267Sriastradh 
93efa246c0Sriastradh 	if (!list_empty(&sa_manager->olist)) {
94efa246c0Sriastradh 		sa_manager->hole = &sa_manager->olist,
95efa246c0Sriastradh 		amdgpu_sa_bo_try_free(sa_manager);
96efa246c0Sriastradh 		if (!list_empty(&sa_manager->olist)) {
97efa246c0Sriastradh 			dev_err(adev->dev, "sa_manager is not empty, clearing anyway\n");
98efa246c0Sriastradh 		}
99efa246c0Sriastradh 	}
100efa246c0Sriastradh 	list_for_each_entry_safe(sa_bo, tmp, &sa_manager->olist, olist) {
101efa246c0Sriastradh 		amdgpu_sa_bo_remove_locked(sa_bo);
102efa246c0Sriastradh 	}
10341ec0267Sriastradh 
10441ec0267Sriastradh 	amdgpu_bo_free_kernel(&sa_manager->bo, &sa_manager->gpu_addr, &sa_manager->cpu_ptr);
105efa246c0Sriastradh 	sa_manager->size = 0;
1060d50c49dSriastradh 	DRM_DESTROY_WAITQUEUE(&sa_manager->wq);
1070d50c49dSriastradh 	spin_lock_destroy(&sa_manager->wq_lock);
108efa246c0Sriastradh }
109efa246c0Sriastradh 
amdgpu_sa_bo_remove_locked(struct amdgpu_sa_bo * sa_bo)110efa246c0Sriastradh static void amdgpu_sa_bo_remove_locked(struct amdgpu_sa_bo *sa_bo)
111efa246c0Sriastradh {
112efa246c0Sriastradh 	struct amdgpu_sa_manager *sa_manager = sa_bo->manager;
113efa246c0Sriastradh 	if (sa_manager->hole == &sa_bo->olist) {
114efa246c0Sriastradh 		sa_manager->hole = sa_bo->olist.prev;
115efa246c0Sriastradh 	}
116efa246c0Sriastradh 	list_del_init(&sa_bo->olist);
117efa246c0Sriastradh 	list_del_init(&sa_bo->flist);
11841ec0267Sriastradh 	dma_fence_put(sa_bo->fence);
119efa246c0Sriastradh 	kfree(sa_bo);
120efa246c0Sriastradh }
121efa246c0Sriastradh 
amdgpu_sa_bo_try_free(struct amdgpu_sa_manager * sa_manager)122efa246c0Sriastradh static void amdgpu_sa_bo_try_free(struct amdgpu_sa_manager *sa_manager)
123efa246c0Sriastradh {
124efa246c0Sriastradh 	struct amdgpu_sa_bo *sa_bo, *tmp;
125efa246c0Sriastradh 
126efa246c0Sriastradh 	if (sa_manager->hole->next == &sa_manager->olist)
127efa246c0Sriastradh 		return;
128efa246c0Sriastradh 
129efa246c0Sriastradh 	sa_bo = list_entry(sa_manager->hole->next, struct amdgpu_sa_bo, olist);
130efa246c0Sriastradh 	list_for_each_entry_safe_from(sa_bo, tmp, &sa_manager->olist, olist) {
131efa246c0Sriastradh 		if (sa_bo->fence == NULL ||
13241ec0267Sriastradh 		    !dma_fence_is_signaled(sa_bo->fence)) {
133efa246c0Sriastradh 			return;
134efa246c0Sriastradh 		}
135efa246c0Sriastradh 		amdgpu_sa_bo_remove_locked(sa_bo);
136efa246c0Sriastradh 	}
137efa246c0Sriastradh }
138efa246c0Sriastradh 
amdgpu_sa_bo_hole_soffset(struct amdgpu_sa_manager * sa_manager)139efa246c0Sriastradh static inline unsigned amdgpu_sa_bo_hole_soffset(struct amdgpu_sa_manager *sa_manager)
140efa246c0Sriastradh {
141efa246c0Sriastradh 	struct list_head *hole = sa_manager->hole;
142efa246c0Sriastradh 
143efa246c0Sriastradh 	if (hole != &sa_manager->olist) {
144efa246c0Sriastradh 		return list_entry(hole, struct amdgpu_sa_bo, olist)->eoffset;
145efa246c0Sriastradh 	}
146efa246c0Sriastradh 	return 0;
147efa246c0Sriastradh }
148efa246c0Sriastradh 
amdgpu_sa_bo_hole_eoffset(struct amdgpu_sa_manager * sa_manager)149efa246c0Sriastradh static inline unsigned amdgpu_sa_bo_hole_eoffset(struct amdgpu_sa_manager *sa_manager)
150efa246c0Sriastradh {
151efa246c0Sriastradh 	struct list_head *hole = sa_manager->hole;
152efa246c0Sriastradh 
153efa246c0Sriastradh 	if (hole->next != &sa_manager->olist) {
154efa246c0Sriastradh 		return list_entry(hole->next, struct amdgpu_sa_bo, olist)->soffset;
155efa246c0Sriastradh 	}
156efa246c0Sriastradh 	return sa_manager->size;
157efa246c0Sriastradh }
158efa246c0Sriastradh 
amdgpu_sa_bo_try_alloc(struct amdgpu_sa_manager * sa_manager,struct amdgpu_sa_bo * sa_bo,unsigned size,unsigned align)159efa246c0Sriastradh static bool amdgpu_sa_bo_try_alloc(struct amdgpu_sa_manager *sa_manager,
160efa246c0Sriastradh 				   struct amdgpu_sa_bo *sa_bo,
161efa246c0Sriastradh 				   unsigned size, unsigned align)
162efa246c0Sriastradh {
163efa246c0Sriastradh 	unsigned soffset, eoffset, wasted;
164efa246c0Sriastradh 
165efa246c0Sriastradh 	soffset = amdgpu_sa_bo_hole_soffset(sa_manager);
166efa246c0Sriastradh 	eoffset = amdgpu_sa_bo_hole_eoffset(sa_manager);
167efa246c0Sriastradh 	wasted = (align - (soffset % align)) % align;
168efa246c0Sriastradh 
169efa246c0Sriastradh 	if ((eoffset - soffset) >= (size + wasted)) {
170efa246c0Sriastradh 		soffset += wasted;
171efa246c0Sriastradh 
172efa246c0Sriastradh 		sa_bo->manager = sa_manager;
173efa246c0Sriastradh 		sa_bo->soffset = soffset;
174efa246c0Sriastradh 		sa_bo->eoffset = soffset + size;
175efa246c0Sriastradh 		list_add(&sa_bo->olist, sa_manager->hole);
176efa246c0Sriastradh 		INIT_LIST_HEAD(&sa_bo->flist);
177efa246c0Sriastradh 		sa_manager->hole = &sa_bo->olist;
178efa246c0Sriastradh 		return true;
179efa246c0Sriastradh 	}
180efa246c0Sriastradh 	return false;
181efa246c0Sriastradh }
182efa246c0Sriastradh 
183efa246c0Sriastradh /**
184efa246c0Sriastradh  * amdgpu_sa_event - Check if we can stop waiting
185efa246c0Sriastradh  *
186efa246c0Sriastradh  * @sa_manager: pointer to the sa_manager
187efa246c0Sriastradh  * @size: number of bytes we want to allocate
188efa246c0Sriastradh  * @align: alignment we need to match
189efa246c0Sriastradh  *
190efa246c0Sriastradh  * Check if either there is a fence we can wait for or
191efa246c0Sriastradh  * enough free memory to satisfy the allocation directly
192efa246c0Sriastradh  */
amdgpu_sa_event(struct amdgpu_sa_manager * sa_manager,unsigned size,unsigned align)193efa246c0Sriastradh static bool amdgpu_sa_event(struct amdgpu_sa_manager *sa_manager,
194efa246c0Sriastradh 			    unsigned size, unsigned align)
195efa246c0Sriastradh {
196efa246c0Sriastradh 	unsigned soffset, eoffset, wasted;
197efa246c0Sriastradh 	int i;
198efa246c0Sriastradh 
19941ec0267Sriastradh 	for (i = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i)
20041ec0267Sriastradh 		if (!list_empty(&sa_manager->flist[i]))
201efa246c0Sriastradh 			return true;
202efa246c0Sriastradh 
203efa246c0Sriastradh 	soffset = amdgpu_sa_bo_hole_soffset(sa_manager);
204efa246c0Sriastradh 	eoffset = amdgpu_sa_bo_hole_eoffset(sa_manager);
205efa246c0Sriastradh 	wasted = (align - (soffset % align)) % align;
206efa246c0Sriastradh 
207efa246c0Sriastradh 	if ((eoffset - soffset) >= (size + wasted)) {
208efa246c0Sriastradh 		return true;
209efa246c0Sriastradh 	}
210efa246c0Sriastradh 
211efa246c0Sriastradh 	return false;
212efa246c0Sriastradh }
213efa246c0Sriastradh 
amdgpu_sa_bo_next_hole(struct amdgpu_sa_manager * sa_manager,struct dma_fence ** fences,unsigned * tries)214efa246c0Sriastradh static bool amdgpu_sa_bo_next_hole(struct amdgpu_sa_manager *sa_manager,
21541ec0267Sriastradh 				   struct dma_fence **fences,
216efa246c0Sriastradh 				   unsigned *tries)
217efa246c0Sriastradh {
218efa246c0Sriastradh 	struct amdgpu_sa_bo *best_bo = NULL;
219efa246c0Sriastradh 	unsigned i, soffset, best, tmp;
220efa246c0Sriastradh 
221efa246c0Sriastradh 	/* if hole points to the end of the buffer */
222efa246c0Sriastradh 	if (sa_manager->hole->next == &sa_manager->olist) {
223efa246c0Sriastradh 		/* try again with its beginning */
224efa246c0Sriastradh 		sa_manager->hole = &sa_manager->olist;
225efa246c0Sriastradh 		return true;
226efa246c0Sriastradh 	}
227efa246c0Sriastradh 
228efa246c0Sriastradh 	soffset = amdgpu_sa_bo_hole_soffset(sa_manager);
229efa246c0Sriastradh 	/* to handle wrap around we add sa_manager->size */
230efa246c0Sriastradh 	best = sa_manager->size * 2;
231efa246c0Sriastradh 	/* go over all fence list and try to find the closest sa_bo
232efa246c0Sriastradh 	 * of the current last
233efa246c0Sriastradh 	 */
23441ec0267Sriastradh 	for (i = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i) {
235efa246c0Sriastradh 		struct amdgpu_sa_bo *sa_bo;
236efa246c0Sriastradh 
23741ec0267Sriastradh 		fences[i] = NULL;
23841ec0267Sriastradh 
23941ec0267Sriastradh 		if (list_empty(&sa_manager->flist[i]))
240efa246c0Sriastradh 			continue;
241efa246c0Sriastradh 
242efa246c0Sriastradh 		sa_bo = list_first_entry(&sa_manager->flist[i],
243efa246c0Sriastradh 					 struct amdgpu_sa_bo, flist);
244efa246c0Sriastradh 
24541ec0267Sriastradh 		if (!dma_fence_is_signaled(sa_bo->fence)) {
246efa246c0Sriastradh 			fences[i] = sa_bo->fence;
247efa246c0Sriastradh 			continue;
248efa246c0Sriastradh 		}
249efa246c0Sriastradh 
250efa246c0Sriastradh 		/* limit the number of tries each ring gets */
251efa246c0Sriastradh 		if (tries[i] > 2) {
252efa246c0Sriastradh 			continue;
253efa246c0Sriastradh 		}
254efa246c0Sriastradh 
255efa246c0Sriastradh 		tmp = sa_bo->soffset;
256efa246c0Sriastradh 		if (tmp < soffset) {
257efa246c0Sriastradh 			/* wrap around, pretend it's after */
258efa246c0Sriastradh 			tmp += sa_manager->size;
259efa246c0Sriastradh 		}
260efa246c0Sriastradh 		tmp -= soffset;
261efa246c0Sriastradh 		if (tmp < best) {
262efa246c0Sriastradh 			/* this sa bo is the closest one */
263efa246c0Sriastradh 			best = tmp;
264efa246c0Sriastradh 			best_bo = sa_bo;
265efa246c0Sriastradh 		}
266efa246c0Sriastradh 	}
267efa246c0Sriastradh 
268efa246c0Sriastradh 	if (best_bo) {
26941ec0267Sriastradh 		uint32_t idx = best_bo->fence->context;
27041ec0267Sriastradh 
27141ec0267Sriastradh 		idx %= AMDGPU_SA_NUM_FENCE_LISTS;
272efa246c0Sriastradh 		++tries[idx];
273efa246c0Sriastradh 		sa_manager->hole = best_bo->olist.prev;
274efa246c0Sriastradh 
275efa246c0Sriastradh 		/* we knew that this one is signaled,
276efa246c0Sriastradh 		   so it's save to remote it */
277efa246c0Sriastradh 		amdgpu_sa_bo_remove_locked(best_bo);
278efa246c0Sriastradh 		return true;
279efa246c0Sriastradh 	}
280efa246c0Sriastradh 	return false;
281efa246c0Sriastradh }
282efa246c0Sriastradh 
amdgpu_sa_bo_new(struct amdgpu_sa_manager * sa_manager,struct amdgpu_sa_bo ** sa_bo,unsigned size,unsigned align)283efa246c0Sriastradh int amdgpu_sa_bo_new(struct amdgpu_sa_manager *sa_manager,
284efa246c0Sriastradh 		     struct amdgpu_sa_bo **sa_bo,
285efa246c0Sriastradh 		     unsigned size, unsigned align)
286efa246c0Sriastradh {
28741ec0267Sriastradh 	struct dma_fence *fences[AMDGPU_SA_NUM_FENCE_LISTS];
28841ec0267Sriastradh 	unsigned tries[AMDGPU_SA_NUM_FENCE_LISTS];
289efa246c0Sriastradh 	unsigned count;
290efa246c0Sriastradh 	int i, r;
291efa246c0Sriastradh 	signed long t;
292efa246c0Sriastradh 
29341ec0267Sriastradh 	if (WARN_ON_ONCE(align > sa_manager->align))
29441ec0267Sriastradh 		return -EINVAL;
29541ec0267Sriastradh 
29641ec0267Sriastradh 	if (WARN_ON_ONCE(size > sa_manager->size))
29741ec0267Sriastradh 		return -EINVAL;
298efa246c0Sriastradh 
299efa246c0Sriastradh 	*sa_bo = kmalloc(sizeof(struct amdgpu_sa_bo), GFP_KERNEL);
30041ec0267Sriastradh 	if (!(*sa_bo))
301efa246c0Sriastradh 		return -ENOMEM;
302efa246c0Sriastradh 	(*sa_bo)->manager = sa_manager;
303efa246c0Sriastradh 	(*sa_bo)->fence = NULL;
304efa246c0Sriastradh 	INIT_LIST_HEAD(&(*sa_bo)->olist);
305efa246c0Sriastradh 	INIT_LIST_HEAD(&(*sa_bo)->flist);
306efa246c0Sriastradh 
3070d50c49dSriastradh 	spin_lock(&sa_manager->wq_lock);
308efa246c0Sriastradh 	do {
30941ec0267Sriastradh 		for (i = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i)
310efa246c0Sriastradh 			tries[i] = 0;
311efa246c0Sriastradh 
312efa246c0Sriastradh 		do {
313efa246c0Sriastradh 			amdgpu_sa_bo_try_free(sa_manager);
314efa246c0Sriastradh 
315efa246c0Sriastradh 			if (amdgpu_sa_bo_try_alloc(sa_manager, *sa_bo,
316efa246c0Sriastradh 						   size, align)) {
3170d50c49dSriastradh 				spin_unlock(&sa_manager->wq_lock);
318efa246c0Sriastradh 				return 0;
319efa246c0Sriastradh 			}
320efa246c0Sriastradh 
321efa246c0Sriastradh 			/* see if we can skip over some allocations */
322efa246c0Sriastradh 		} while (amdgpu_sa_bo_next_hole(sa_manager, fences, tries));
323efa246c0Sriastradh 
32441ec0267Sriastradh 		for (i = 0, count = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i)
325efa246c0Sriastradh 			if (fences[i])
32641ec0267Sriastradh 				fences[count++] = dma_fence_get(fences[i]);
327efa246c0Sriastradh 
328efa246c0Sriastradh 		if (count) {
3290d50c49dSriastradh 			spin_unlock(&sa_manager->wq_lock);
33041ec0267Sriastradh 			t = dma_fence_wait_any_timeout(fences, count, false,
33141ec0267Sriastradh 						       MAX_SCHEDULE_TIMEOUT,
33241ec0267Sriastradh 						       NULL);
333efa246c0Sriastradh 			for (i = 0; i < count; ++i)
33441ec0267Sriastradh 				dma_fence_put(fences[i]);
335efa246c0Sriastradh 
336efa246c0Sriastradh 			r = (t > 0) ? 0 : t;
3370d50c49dSriastradh 			spin_lock(&sa_manager->wq_lock);
338efa246c0Sriastradh 		} else {
339efa246c0Sriastradh 			/* if we have nothing to wait for block */
3400d50c49dSriastradh 			DRM_SPIN_WAIT_UNTIL(r, &sa_manager->wq,
3410d50c49dSriastradh 			    &sa_manager->wq_lock,
3420d50c49dSriastradh 			    amdgpu_sa_event(sa_manager, size, align));
343efa246c0Sriastradh 		}
344efa246c0Sriastradh 
345efa246c0Sriastradh 	} while (!r);
346efa246c0Sriastradh 
3470d50c49dSriastradh 	spin_unlock(&sa_manager->wq_lock);
348efa246c0Sriastradh 	kfree(*sa_bo);
349efa246c0Sriastradh 	*sa_bo = NULL;
350efa246c0Sriastradh 	return r;
351efa246c0Sriastradh }
352efa246c0Sriastradh 
amdgpu_sa_bo_free(struct amdgpu_device * adev,struct amdgpu_sa_bo ** sa_bo,struct dma_fence * fence)353efa246c0Sriastradh void amdgpu_sa_bo_free(struct amdgpu_device *adev, struct amdgpu_sa_bo **sa_bo,
35441ec0267Sriastradh 		       struct dma_fence *fence)
355efa246c0Sriastradh {
356efa246c0Sriastradh 	struct amdgpu_sa_manager *sa_manager;
357efa246c0Sriastradh 
358efa246c0Sriastradh 	if (sa_bo == NULL || *sa_bo == NULL) {
359efa246c0Sriastradh 		return;
360efa246c0Sriastradh 	}
361efa246c0Sriastradh 
362efa246c0Sriastradh 	sa_manager = (*sa_bo)->manager;
3630d50c49dSriastradh 	spin_lock(&sa_manager->wq_lock);
36441ec0267Sriastradh 	if (fence && !dma_fence_is_signaled(fence)) {
365efa246c0Sriastradh 		uint32_t idx;
36641ec0267Sriastradh 
36741ec0267Sriastradh 		(*sa_bo)->fence = dma_fence_get(fence);
36841ec0267Sriastradh 		idx = fence->context % AMDGPU_SA_NUM_FENCE_LISTS;
369efa246c0Sriastradh 		list_add_tail(&(*sa_bo)->flist, &sa_manager->flist[idx]);
370efa246c0Sriastradh 	} else {
371efa246c0Sriastradh 		amdgpu_sa_bo_remove_locked(*sa_bo);
372efa246c0Sriastradh 	}
3730d50c49dSriastradh 	DRM_SPIN_WAKEUP_ALL(&sa_manager->wq, &sa_manager->wq_lock);
3740d50c49dSriastradh 	spin_unlock(&sa_manager->wq_lock);
375efa246c0Sriastradh 	*sa_bo = NULL;
376efa246c0Sriastradh }
377efa246c0Sriastradh 
378efa246c0Sriastradh #if defined(CONFIG_DEBUG_FS)
379efa246c0Sriastradh 
amdgpu_sa_bo_dump_debug_info(struct amdgpu_sa_manager * sa_manager,struct seq_file * m)380efa246c0Sriastradh void amdgpu_sa_bo_dump_debug_info(struct amdgpu_sa_manager *sa_manager,
381efa246c0Sriastradh 				  struct seq_file *m)
382efa246c0Sriastradh {
383efa246c0Sriastradh 	struct amdgpu_sa_bo *i;
384efa246c0Sriastradh 
385efa246c0Sriastradh 	spin_lock(&sa_manager->wq.lock);
386efa246c0Sriastradh 	list_for_each_entry(i, &sa_manager->olist, olist) {
387efa246c0Sriastradh 		uint64_t soffset = i->soffset + sa_manager->gpu_addr;
388efa246c0Sriastradh 		uint64_t eoffset = i->eoffset + sa_manager->gpu_addr;
389efa246c0Sriastradh 		if (&i->olist == sa_manager->hole) {
390efa246c0Sriastradh 			seq_printf(m, ">");
391efa246c0Sriastradh 		} else {
392efa246c0Sriastradh 			seq_printf(m, " ");
393efa246c0Sriastradh 		}
394efa246c0Sriastradh 		seq_printf(m, "[0x%010llx 0x%010llx] size %8lld",
395efa246c0Sriastradh 			   soffset, eoffset, eoffset - soffset);
39641ec0267Sriastradh 
397efa246c0Sriastradh 		if (i->fence)
39841ec0267Sriastradh 			seq_printf(m, " protected by 0x%016llx on context %llu",
39941ec0267Sriastradh 				   i->fence->seqno, i->fence->context);
40041ec0267Sriastradh 
401efa246c0Sriastradh 		seq_printf(m, "\n");
402efa246c0Sriastradh 	}
403efa246c0Sriastradh 	spin_unlock(&sa_manager->wq.lock);
404efa246c0Sriastradh }
405efa246c0Sriastradh #endif
406