xref: /dragonfly/sys/dev/drm/radeon/radeon_sa.c (revision c6f73aab)
1926deccbSFrançois Tigeot /*
2926deccbSFrançois Tigeot  * Copyright 2011 Red Hat Inc.
3926deccbSFrançois Tigeot  * All Rights Reserved.
4926deccbSFrançois Tigeot  *
5926deccbSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
6926deccbSFrançois Tigeot  * copy of this software and associated documentation files (the
7926deccbSFrançois Tigeot  * "Software"), to deal in the Software without restriction, including
8926deccbSFrançois Tigeot  * without limitation the rights to use, copy, modify, merge, publish,
9926deccbSFrançois Tigeot  * distribute, sub license, and/or sell copies of the Software, and to
10926deccbSFrançois Tigeot  * permit persons to whom the Software is furnished to do so, subject to
11926deccbSFrançois Tigeot  * the following conditions:
12926deccbSFrançois Tigeot  *
13926deccbSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14926deccbSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15926deccbSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16926deccbSFrançois Tigeot  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17926deccbSFrançois Tigeot  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18926deccbSFrançois Tigeot  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19926deccbSFrançois Tigeot  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20926deccbSFrançois Tigeot  *
21926deccbSFrançois Tigeot  * The above copyright notice and this permission notice (including the
22926deccbSFrançois Tigeot  * next paragraph) shall be included in all copies or substantial portions
23926deccbSFrançois Tigeot  * of the Software.
24926deccbSFrançois Tigeot  *
25926deccbSFrançois Tigeot  */
26926deccbSFrançois Tigeot /*
27926deccbSFrançois Tigeot  * Authors:
28926deccbSFrançois Tigeot  *    Jerome Glisse <glisse@freedesktop.org>
29926deccbSFrançois Tigeot  */
30926deccbSFrançois Tigeot /* Algorithm:
31926deccbSFrançois Tigeot  *
32926deccbSFrançois Tigeot  * We store the last allocated bo in "hole", we always try to allocate
33926deccbSFrançois Tigeot  * after the last allocated bo. Principle is that in a linear GPU ring
34926deccbSFrançois Tigeot  * progression was is after last is the oldest bo we allocated and thus
35926deccbSFrançois Tigeot  * the first one that should no longer be in use by the GPU.
36926deccbSFrançois Tigeot  *
37926deccbSFrançois Tigeot  * If it's not the case we skip over the bo after last to the closest
38926deccbSFrançois Tigeot  * done bo if such one exist. If none exist and we are not asked to
39926deccbSFrançois Tigeot  * block we report failure to allocate.
40926deccbSFrançois Tigeot  *
41926deccbSFrançois Tigeot  * If we are asked to block we wait on all the oldest fence of all
42926deccbSFrançois Tigeot  * rings. We just wait for any of those fence to complete.
43926deccbSFrançois Tigeot  *
44926deccbSFrançois Tigeot  * $FreeBSD: head/sys/dev/drm2/radeon/radeon_sa.c 254885 2013-08-25 19:37:15Z dumbbell $
45926deccbSFrançois Tigeot  */
46926deccbSFrançois Tigeot 
47926deccbSFrançois Tigeot #include <drm/drmP.h>
48926deccbSFrançois Tigeot #include "radeon.h"
49926deccbSFrançois Tigeot 
50926deccbSFrançois Tigeot static void radeon_sa_bo_remove_locked(struct radeon_sa_bo *sa_bo);
51926deccbSFrançois Tigeot static void radeon_sa_bo_try_free(struct radeon_sa_manager *sa_manager);
52926deccbSFrançois Tigeot 
53926deccbSFrançois Tigeot int radeon_sa_bo_manager_init(struct radeon_device *rdev,
54926deccbSFrançois Tigeot 			      struct radeon_sa_manager *sa_manager,
55*c6f73aabSFrançois Tigeot 			      unsigned size, u32 align, u32 domain, u32 flags)
56926deccbSFrançois Tigeot {
57926deccbSFrançois Tigeot 	int i, r;
58926deccbSFrançois Tigeot 
59926deccbSFrançois Tigeot 	lockinit(&sa_manager->wq_lock, "drm__radeon_sa_manager_wq_mtx", 0,
60926deccbSFrançois Tigeot 		 LK_CANRECURSE);
61926deccbSFrançois Tigeot 	cv_init(&sa_manager->wq, "drm__radeon_sa_manager__wq");
62926deccbSFrançois Tigeot 	sa_manager->bo = NULL;
63926deccbSFrançois Tigeot 	sa_manager->size = size;
64926deccbSFrançois Tigeot 	sa_manager->domain = domain;
6557e252bfSMichael Neumann 	sa_manager->align = align;
66926deccbSFrançois Tigeot 	sa_manager->hole = &sa_manager->olist;
67926deccbSFrançois Tigeot 	INIT_LIST_HEAD(&sa_manager->olist);
68926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
69926deccbSFrançois Tigeot 		INIT_LIST_HEAD(&sa_manager->flist[i]);
70926deccbSFrançois Tigeot 	}
71926deccbSFrançois Tigeot 
7257e252bfSMichael Neumann 	r = radeon_bo_create(rdev, size, align, true,
73*c6f73aabSFrançois Tigeot 			     domain, flags, NULL, &sa_manager->bo);
74926deccbSFrançois Tigeot 	if (r) {
75926deccbSFrançois Tigeot 		dev_err(rdev->dev, "(%d) failed to allocate bo for manager\n", r);
76926deccbSFrançois Tigeot 		return r;
77926deccbSFrançois Tigeot 	}
78926deccbSFrançois Tigeot 
79926deccbSFrançois Tigeot 	return r;
80926deccbSFrançois Tigeot }
81926deccbSFrançois Tigeot 
82926deccbSFrançois Tigeot void radeon_sa_bo_manager_fini(struct radeon_device *rdev,
83926deccbSFrançois Tigeot 			       struct radeon_sa_manager *sa_manager)
84926deccbSFrançois Tigeot {
85926deccbSFrançois Tigeot 	struct radeon_sa_bo *sa_bo, *tmp;
86926deccbSFrançois Tigeot 
87926deccbSFrançois Tigeot 	if (!list_empty(&sa_manager->olist)) {
88926deccbSFrançois Tigeot 		sa_manager->hole = &sa_manager->olist,
89926deccbSFrançois Tigeot 		radeon_sa_bo_try_free(sa_manager);
90926deccbSFrançois Tigeot 		if (!list_empty(&sa_manager->olist)) {
91926deccbSFrançois Tigeot 			dev_err(rdev->dev, "sa_manager is not empty, clearing anyway\n");
92926deccbSFrançois Tigeot 		}
93926deccbSFrançois Tigeot 	}
94926deccbSFrançois Tigeot 	list_for_each_entry_safe(sa_bo, tmp, &sa_manager->olist, olist) {
95926deccbSFrançois Tigeot 		radeon_sa_bo_remove_locked(sa_bo);
96926deccbSFrançois Tigeot 	}
97926deccbSFrançois Tigeot 	radeon_bo_unref(&sa_manager->bo);
98926deccbSFrançois Tigeot 	sa_manager->size = 0;
99926deccbSFrançois Tigeot 	cv_destroy(&sa_manager->wq);
100926deccbSFrançois Tigeot 	lockuninit(&sa_manager->wq_lock);
101926deccbSFrançois Tigeot }
102926deccbSFrançois Tigeot 
103926deccbSFrançois Tigeot int radeon_sa_bo_manager_start(struct radeon_device *rdev,
104926deccbSFrançois Tigeot 			       struct radeon_sa_manager *sa_manager)
105926deccbSFrançois Tigeot {
106926deccbSFrançois Tigeot 	int r;
107926deccbSFrançois Tigeot 
108926deccbSFrançois Tigeot 	if (sa_manager->bo == NULL) {
109926deccbSFrançois Tigeot 		dev_err(rdev->dev, "no bo for sa manager\n");
110926deccbSFrançois Tigeot 		return -EINVAL;
111926deccbSFrançois Tigeot 	}
112926deccbSFrançois Tigeot 
113926deccbSFrançois Tigeot 	/* map the buffer */
114926deccbSFrançois Tigeot 	r = radeon_bo_reserve(sa_manager->bo, false);
115926deccbSFrançois Tigeot 	if (r) {
116926deccbSFrançois Tigeot 		dev_err(rdev->dev, "(%d) failed to reserve manager bo\n", r);
117926deccbSFrançois Tigeot 		return r;
118926deccbSFrançois Tigeot 	}
119926deccbSFrançois Tigeot 	r = radeon_bo_pin(sa_manager->bo, sa_manager->domain, &sa_manager->gpu_addr);
120926deccbSFrançois Tigeot 	if (r) {
121926deccbSFrançois Tigeot 		radeon_bo_unreserve(sa_manager->bo);
122926deccbSFrançois Tigeot 		dev_err(rdev->dev, "(%d) failed to pin manager bo\n", r);
123926deccbSFrançois Tigeot 		return r;
124926deccbSFrançois Tigeot 	}
125926deccbSFrançois Tigeot 	r = radeon_bo_kmap(sa_manager->bo, &sa_manager->cpu_ptr);
126926deccbSFrançois Tigeot 	radeon_bo_unreserve(sa_manager->bo);
127926deccbSFrançois Tigeot 	return r;
128926deccbSFrançois Tigeot }
129926deccbSFrançois Tigeot 
130926deccbSFrançois Tigeot int radeon_sa_bo_manager_suspend(struct radeon_device *rdev,
131926deccbSFrançois Tigeot 				 struct radeon_sa_manager *sa_manager)
132926deccbSFrançois Tigeot {
133926deccbSFrançois Tigeot 	int r;
134926deccbSFrançois Tigeot 
135926deccbSFrançois Tigeot 	if (sa_manager->bo == NULL) {
136926deccbSFrançois Tigeot 		dev_err(rdev->dev, "no bo for sa manager\n");
137926deccbSFrançois Tigeot 		return -EINVAL;
138926deccbSFrançois Tigeot 	}
139926deccbSFrançois Tigeot 
140926deccbSFrançois Tigeot 	r = radeon_bo_reserve(sa_manager->bo, false);
141926deccbSFrançois Tigeot 	if (!r) {
142926deccbSFrançois Tigeot 		radeon_bo_kunmap(sa_manager->bo);
143926deccbSFrançois Tigeot 		radeon_bo_unpin(sa_manager->bo);
144926deccbSFrançois Tigeot 		radeon_bo_unreserve(sa_manager->bo);
145926deccbSFrançois Tigeot 	}
146926deccbSFrançois Tigeot 	return r;
147926deccbSFrançois Tigeot }
148926deccbSFrançois Tigeot 
149926deccbSFrançois Tigeot static void radeon_sa_bo_remove_locked(struct radeon_sa_bo *sa_bo)
150926deccbSFrançois Tigeot {
151926deccbSFrançois Tigeot 	struct radeon_sa_manager *sa_manager = sa_bo->manager;
152926deccbSFrançois Tigeot 	if (sa_manager->hole == &sa_bo->olist) {
153926deccbSFrançois Tigeot 		sa_manager->hole = sa_bo->olist.prev;
154926deccbSFrançois Tigeot 	}
155926deccbSFrançois Tigeot 	list_del_init(&sa_bo->olist);
156926deccbSFrançois Tigeot 	list_del_init(&sa_bo->flist);
157926deccbSFrançois Tigeot 	radeon_fence_unref(&sa_bo->fence);
158c4ef309bSzrj 	kfree(sa_bo);
159926deccbSFrançois Tigeot }
160926deccbSFrançois Tigeot 
161926deccbSFrançois Tigeot static void radeon_sa_bo_try_free(struct radeon_sa_manager *sa_manager)
162926deccbSFrançois Tigeot {
163926deccbSFrançois Tigeot 	struct radeon_sa_bo *sa_bo, *tmp;
164926deccbSFrançois Tigeot 
165926deccbSFrançois Tigeot 	if (sa_manager->hole->next == &sa_manager->olist)
166926deccbSFrançois Tigeot 		return;
167926deccbSFrançois Tigeot 
168926deccbSFrançois Tigeot 	sa_bo = list_entry(sa_manager->hole->next, struct radeon_sa_bo, olist);
169926deccbSFrançois Tigeot 	list_for_each_entry_safe_from(sa_bo, tmp, &sa_manager->olist, olist) {
170926deccbSFrançois Tigeot 		if (sa_bo->fence == NULL || !radeon_fence_signaled(sa_bo->fence)) {
171926deccbSFrançois Tigeot 			return;
172926deccbSFrançois Tigeot 		}
173926deccbSFrançois Tigeot 		radeon_sa_bo_remove_locked(sa_bo);
174926deccbSFrançois Tigeot 	}
175926deccbSFrançois Tigeot }
176926deccbSFrançois Tigeot 
177926deccbSFrançois Tigeot static inline unsigned radeon_sa_bo_hole_soffset(struct radeon_sa_manager *sa_manager)
178926deccbSFrançois Tigeot {
179926deccbSFrançois Tigeot 	struct list_head *hole = sa_manager->hole;
180926deccbSFrançois Tigeot 
181926deccbSFrançois Tigeot 	if (hole != &sa_manager->olist) {
182926deccbSFrançois Tigeot 		return list_entry(hole, struct radeon_sa_bo, olist)->eoffset;
183926deccbSFrançois Tigeot 	}
184926deccbSFrançois Tigeot 	return 0;
185926deccbSFrançois Tigeot }
186926deccbSFrançois Tigeot 
187926deccbSFrançois Tigeot static inline unsigned radeon_sa_bo_hole_eoffset(struct radeon_sa_manager *sa_manager)
188926deccbSFrançois Tigeot {
189926deccbSFrançois Tigeot 	struct list_head *hole = sa_manager->hole;
190926deccbSFrançois Tigeot 
191926deccbSFrançois Tigeot 	if (hole->next != &sa_manager->olist) {
192926deccbSFrançois Tigeot 		return list_entry(hole->next, struct radeon_sa_bo, olist)->soffset;
193926deccbSFrançois Tigeot 	}
194926deccbSFrançois Tigeot 	return sa_manager->size;
195926deccbSFrançois Tigeot }
196926deccbSFrançois Tigeot 
197926deccbSFrançois Tigeot static bool radeon_sa_bo_try_alloc(struct radeon_sa_manager *sa_manager,
198926deccbSFrançois Tigeot 				   struct radeon_sa_bo *sa_bo,
199926deccbSFrançois Tigeot 				   unsigned size, unsigned align)
200926deccbSFrançois Tigeot {
201926deccbSFrançois Tigeot 	unsigned soffset, eoffset, wasted;
202926deccbSFrançois Tigeot 
203926deccbSFrançois Tigeot 	soffset = radeon_sa_bo_hole_soffset(sa_manager);
204926deccbSFrançois Tigeot 	eoffset = radeon_sa_bo_hole_eoffset(sa_manager);
205926deccbSFrançois Tigeot 	wasted = (align - (soffset % align)) % align;
206926deccbSFrançois Tigeot 
207926deccbSFrançois Tigeot 	if ((eoffset - soffset) >= (size + wasted)) {
208926deccbSFrançois Tigeot 		soffset += wasted;
209926deccbSFrançois Tigeot 
210926deccbSFrançois Tigeot 		sa_bo->manager = sa_manager;
211926deccbSFrançois Tigeot 		sa_bo->soffset = soffset;
212926deccbSFrançois Tigeot 		sa_bo->eoffset = soffset + size;
213926deccbSFrançois Tigeot 		list_add(&sa_bo->olist, sa_manager->hole);
214926deccbSFrançois Tigeot 		INIT_LIST_HEAD(&sa_bo->flist);
215926deccbSFrançois Tigeot 		sa_manager->hole = &sa_bo->olist;
216926deccbSFrançois Tigeot 		return true;
217926deccbSFrançois Tigeot 	}
218926deccbSFrançois Tigeot 	return false;
219926deccbSFrançois Tigeot }
220926deccbSFrançois Tigeot 
221926deccbSFrançois Tigeot /**
222926deccbSFrançois Tigeot  * radeon_sa_event - Check if we can stop waiting
223926deccbSFrançois Tigeot  *
224926deccbSFrançois Tigeot  * @sa_manager: pointer to the sa_manager
225926deccbSFrançois Tigeot  * @size: number of bytes we want to allocate
226926deccbSFrançois Tigeot  * @align: alignment we need to match
227926deccbSFrançois Tigeot  *
228926deccbSFrançois Tigeot  * Check if either there is a fence we can wait for or
229926deccbSFrançois Tigeot  * enough free memory to satisfy the allocation directly
230926deccbSFrançois Tigeot  */
231926deccbSFrançois Tigeot static bool radeon_sa_event(struct radeon_sa_manager *sa_manager,
232926deccbSFrançois Tigeot 			    unsigned size, unsigned align)
233926deccbSFrançois Tigeot {
234926deccbSFrançois Tigeot 	unsigned soffset, eoffset, wasted;
235926deccbSFrançois Tigeot 	int i;
236926deccbSFrançois Tigeot 
237926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
238926deccbSFrançois Tigeot 		if (!list_empty(&sa_manager->flist[i])) {
239926deccbSFrançois Tigeot 			return true;
240926deccbSFrançois Tigeot 		}
241926deccbSFrançois Tigeot 	}
242926deccbSFrançois Tigeot 
243926deccbSFrançois Tigeot 	soffset = radeon_sa_bo_hole_soffset(sa_manager);
244926deccbSFrançois Tigeot 	eoffset = radeon_sa_bo_hole_eoffset(sa_manager);
245926deccbSFrançois Tigeot 	wasted = (align - (soffset % align)) % align;
246926deccbSFrançois Tigeot 
247926deccbSFrançois Tigeot 	if ((eoffset - soffset) >= (size + wasted)) {
248926deccbSFrançois Tigeot 		return true;
249926deccbSFrançois Tigeot 	}
250926deccbSFrançois Tigeot 
251926deccbSFrançois Tigeot 	return false;
252926deccbSFrançois Tigeot }
253926deccbSFrançois Tigeot 
254926deccbSFrançois Tigeot static bool radeon_sa_bo_next_hole(struct radeon_sa_manager *sa_manager,
255926deccbSFrançois Tigeot 				   struct radeon_fence **fences,
256926deccbSFrançois Tigeot 				   unsigned *tries)
257926deccbSFrançois Tigeot {
258926deccbSFrançois Tigeot 	struct radeon_sa_bo *best_bo = NULL;
259926deccbSFrançois Tigeot 	unsigned i, soffset, best, tmp;
260926deccbSFrançois Tigeot 
261926deccbSFrançois Tigeot 	/* if hole points to the end of the buffer */
262926deccbSFrançois Tigeot 	if (sa_manager->hole->next == &sa_manager->olist) {
263926deccbSFrançois Tigeot 		/* try again with its beginning */
264926deccbSFrançois Tigeot 		sa_manager->hole = &sa_manager->olist;
265926deccbSFrançois Tigeot 		return true;
266926deccbSFrançois Tigeot 	}
267926deccbSFrançois Tigeot 
268926deccbSFrançois Tigeot 	soffset = radeon_sa_bo_hole_soffset(sa_manager);
269926deccbSFrançois Tigeot 	/* to handle wrap around we add sa_manager->size */
270926deccbSFrançois Tigeot 	best = sa_manager->size * 2;
271926deccbSFrançois Tigeot 	/* go over all fence list and try to find the closest sa_bo
272926deccbSFrançois Tigeot 	 * of the current last
273926deccbSFrançois Tigeot 	 */
274926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
275926deccbSFrançois Tigeot 		struct radeon_sa_bo *sa_bo;
276926deccbSFrançois Tigeot 
277926deccbSFrançois Tigeot 		if (list_empty(&sa_manager->flist[i])) {
278926deccbSFrançois Tigeot 			continue;
279926deccbSFrançois Tigeot 		}
280926deccbSFrançois Tigeot 
281926deccbSFrançois Tigeot 		sa_bo = list_first_entry(&sa_manager->flist[i],
282926deccbSFrançois Tigeot 					 struct radeon_sa_bo, flist);
283926deccbSFrançois Tigeot 
284926deccbSFrançois Tigeot 		if (!radeon_fence_signaled(sa_bo->fence)) {
285926deccbSFrançois Tigeot 			fences[i] = sa_bo->fence;
286926deccbSFrançois Tigeot 			continue;
287926deccbSFrançois Tigeot 		}
288926deccbSFrançois Tigeot 
289926deccbSFrançois Tigeot 		/* limit the number of tries each ring gets */
290926deccbSFrançois Tigeot 		if (tries[i] > 2) {
291926deccbSFrançois Tigeot 			continue;
292926deccbSFrançois Tigeot 		}
293926deccbSFrançois Tigeot 
294926deccbSFrançois Tigeot 		tmp = sa_bo->soffset;
295926deccbSFrançois Tigeot 		if (tmp < soffset) {
296926deccbSFrançois Tigeot 			/* wrap around, pretend it's after */
297926deccbSFrançois Tigeot 			tmp += sa_manager->size;
298926deccbSFrançois Tigeot 		}
299926deccbSFrançois Tigeot 		tmp -= soffset;
300926deccbSFrançois Tigeot 		if (tmp < best) {
301926deccbSFrançois Tigeot 			/* this sa bo is the closest one */
302926deccbSFrançois Tigeot 			best = tmp;
303926deccbSFrançois Tigeot 			best_bo = sa_bo;
304926deccbSFrançois Tigeot 		}
305926deccbSFrançois Tigeot 	}
306926deccbSFrançois Tigeot 
307926deccbSFrançois Tigeot 	if (best_bo) {
308926deccbSFrançois Tigeot 		++tries[best_bo->fence->ring];
309926deccbSFrançois Tigeot 		sa_manager->hole = best_bo->olist.prev;
310926deccbSFrançois Tigeot 
311926deccbSFrançois Tigeot 		/* we knew that this one is signaled,
312926deccbSFrançois Tigeot 		   so it's save to remote it */
313926deccbSFrançois Tigeot 		radeon_sa_bo_remove_locked(best_bo);
314926deccbSFrançois Tigeot 		return true;
315926deccbSFrançois Tigeot 	}
316926deccbSFrançois Tigeot 	return false;
317926deccbSFrançois Tigeot }
318926deccbSFrançois Tigeot 
319926deccbSFrançois Tigeot int radeon_sa_bo_new(struct radeon_device *rdev,
320926deccbSFrançois Tigeot 		     struct radeon_sa_manager *sa_manager,
321926deccbSFrançois Tigeot 		     struct radeon_sa_bo **sa_bo,
322*c6f73aabSFrançois Tigeot 		     unsigned size, unsigned align)
323926deccbSFrançois Tigeot {
324926deccbSFrançois Tigeot 	struct radeon_fence *fences[RADEON_NUM_RINGS];
325926deccbSFrançois Tigeot 	unsigned tries[RADEON_NUM_RINGS];
326926deccbSFrançois Tigeot 	int i, r;
327926deccbSFrançois Tigeot 
32857e252bfSMichael Neumann 	BUG_ON(align > sa_manager->align);
32957e252bfSMichael Neumann 	BUG_ON(size > sa_manager->size);
330926deccbSFrançois Tigeot 
3315a3b77d5SFrançois Tigeot 	*sa_bo = kmalloc(sizeof(struct radeon_sa_bo), M_DRM,
332f6201ebfSMatthew Dillon 			 M_WAITOK | M_ZERO);
333926deccbSFrançois Tigeot 	if ((*sa_bo) == NULL) {
334926deccbSFrançois Tigeot 		return -ENOMEM;
335926deccbSFrançois Tigeot 	}
336926deccbSFrançois Tigeot 	(*sa_bo)->manager = sa_manager;
337926deccbSFrançois Tigeot 	(*sa_bo)->fence = NULL;
338926deccbSFrançois Tigeot 	INIT_LIST_HEAD(&(*sa_bo)->olist);
339926deccbSFrançois Tigeot 	INIT_LIST_HEAD(&(*sa_bo)->flist);
340926deccbSFrançois Tigeot 
341926deccbSFrançois Tigeot 	lockmgr(&sa_manager->wq_lock, LK_EXCLUSIVE);
342926deccbSFrançois Tigeot 	do {
343926deccbSFrançois Tigeot 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
344926deccbSFrançois Tigeot 			fences[i] = NULL;
345926deccbSFrançois Tigeot 			tries[i] = 0;
346926deccbSFrançois Tigeot 		}
347926deccbSFrançois Tigeot 
348926deccbSFrançois Tigeot 		do {
349926deccbSFrançois Tigeot 			radeon_sa_bo_try_free(sa_manager);
350926deccbSFrançois Tigeot 
351926deccbSFrançois Tigeot 			if (radeon_sa_bo_try_alloc(sa_manager, *sa_bo,
352926deccbSFrançois Tigeot 						   size, align)) {
353926deccbSFrançois Tigeot 				lockmgr(&sa_manager->wq_lock, LK_RELEASE);
354926deccbSFrançois Tigeot 				return 0;
355926deccbSFrançois Tigeot 			}
356926deccbSFrançois Tigeot 
357926deccbSFrançois Tigeot 			/* see if we can skip over some allocations */
358926deccbSFrançois Tigeot 		} while (radeon_sa_bo_next_hole(sa_manager, fences, tries));
359926deccbSFrançois Tigeot 
3601a05908eSMatthew Dillon 		lockmgr(&sa_manager->wq_lock, LK_RELEASE);
361926deccbSFrançois Tigeot 		r = radeon_fence_wait_any(rdev, fences, false);
362926deccbSFrançois Tigeot 		lockmgr(&sa_manager->wq_lock, LK_EXCLUSIVE);
363926deccbSFrançois Tigeot 		/* if we have nothing to wait for block */
364*c6f73aabSFrançois Tigeot 		if (r == -ENOENT) {
365926deccbSFrançois Tigeot 			while (!radeon_sa_event(sa_manager, size, align)) {
366926deccbSFrançois Tigeot 				r = -cv_wait_sig(&sa_manager->wq,
367926deccbSFrançois Tigeot 						 &sa_manager->wq_lock);
368926deccbSFrançois Tigeot 				if (r != 0)
369926deccbSFrançois Tigeot 					break;
370926deccbSFrançois Tigeot 			}
371926deccbSFrançois Tigeot 		}
372926deccbSFrançois Tigeot 
373926deccbSFrançois Tigeot 	} while (!r);
374926deccbSFrançois Tigeot 
375926deccbSFrançois Tigeot 	lockmgr(&sa_manager->wq_lock, LK_RELEASE);
376c4ef309bSzrj 	kfree(*sa_bo);
377926deccbSFrançois Tigeot 	*sa_bo = NULL;
378926deccbSFrançois Tigeot 	return r;
379926deccbSFrançois Tigeot }
380926deccbSFrançois Tigeot 
381926deccbSFrançois Tigeot void radeon_sa_bo_free(struct radeon_device *rdev, struct radeon_sa_bo **sa_bo,
382926deccbSFrançois Tigeot 		       struct radeon_fence *fence)
383926deccbSFrançois Tigeot {
384926deccbSFrançois Tigeot 	struct radeon_sa_manager *sa_manager;
385926deccbSFrançois Tigeot 
386926deccbSFrançois Tigeot 	if (sa_bo == NULL || *sa_bo == NULL) {
387926deccbSFrançois Tigeot 		return;
388926deccbSFrançois Tigeot 	}
389926deccbSFrançois Tigeot 
390926deccbSFrançois Tigeot 	sa_manager = (*sa_bo)->manager;
391926deccbSFrançois Tigeot 	lockmgr(&sa_manager->wq_lock, LK_EXCLUSIVE);
392926deccbSFrançois Tigeot 	if (fence && !radeon_fence_signaled(fence)) {
393926deccbSFrançois Tigeot 		(*sa_bo)->fence = radeon_fence_ref(fence);
394926deccbSFrançois Tigeot 		list_add_tail(&(*sa_bo)->flist,
395926deccbSFrançois Tigeot 			      &sa_manager->flist[fence->ring]);
396926deccbSFrançois Tigeot 	} else {
397926deccbSFrançois Tigeot 		radeon_sa_bo_remove_locked(*sa_bo);
398926deccbSFrançois Tigeot 	}
399926deccbSFrançois Tigeot 	cv_broadcast(&sa_manager->wq);
400926deccbSFrançois Tigeot 	lockmgr(&sa_manager->wq_lock, LK_RELEASE);
401926deccbSFrançois Tigeot 	*sa_bo = NULL;
402926deccbSFrançois Tigeot }
403926deccbSFrançois Tigeot 
404926deccbSFrançois Tigeot #if defined(CONFIG_DEBUG_FS)
405926deccbSFrançois Tigeot void radeon_sa_bo_dump_debug_info(struct radeon_sa_manager *sa_manager,
406926deccbSFrançois Tigeot 				  struct seq_file *m)
407926deccbSFrançois Tigeot {
408926deccbSFrançois Tigeot 	struct radeon_sa_bo *i;
409926deccbSFrançois Tigeot 
410926deccbSFrançois Tigeot 	spin_lock(&sa_manager->wq.lock);
411926deccbSFrançois Tigeot 	list_for_each_entry(i, &sa_manager->olist, olist) {
412*c6f73aabSFrançois Tigeot 		uint64_t soffset = i->soffset + sa_manager->gpu_addr;
413*c6f73aabSFrançois Tigeot 		uint64_t eoffset = i->eoffset + sa_manager->gpu_addr;
414926deccbSFrançois Tigeot 		if (&i->olist == sa_manager->hole) {
415926deccbSFrançois Tigeot 			seq_printf(m, ">");
416926deccbSFrançois Tigeot 		} else {
417926deccbSFrançois Tigeot 			seq_printf(m, " ");
418926deccbSFrançois Tigeot 		}
419*c6f73aabSFrançois Tigeot 		seq_printf(m, "[0x%010llx 0x%010llx] size %8lld",
420*c6f73aabSFrançois Tigeot 			   soffset, eoffset, eoffset - soffset);
421926deccbSFrançois Tigeot 		if (i->fence) {
422926deccbSFrançois Tigeot 			seq_printf(m, " protected by 0x%016llx on ring %d",
423926deccbSFrançois Tigeot 				   i->fence->seq, i->fence->ring);
424926deccbSFrançois Tigeot 		}
425926deccbSFrançois Tigeot 		seq_printf(m, "\n");
426926deccbSFrançois Tigeot 	}
427926deccbSFrançois Tigeot 	spin_unlock(&sa_manager->wq.lock);
428926deccbSFrançois Tigeot }
429926deccbSFrançois Tigeot #endif
430