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