xref: /dragonfly/sys/dev/drm/radeon/radeon_sa.c (revision ec5b6af4)
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 #include <drm/drmP.h>
45926deccbSFrançois Tigeot #include "radeon.h"
46926deccbSFrançois Tigeot 
47926deccbSFrançois Tigeot static void radeon_sa_bo_remove_locked(struct radeon_sa_bo *sa_bo);
48926deccbSFrançois Tigeot static void radeon_sa_bo_try_free(struct radeon_sa_manager *sa_manager);
49926deccbSFrançois Tigeot 
radeon_sa_bo_manager_init(struct radeon_device * rdev,struct radeon_sa_manager * sa_manager,unsigned size,u32 align,u32 domain,u32 flags)50926deccbSFrançois Tigeot int radeon_sa_bo_manager_init(struct radeon_device *rdev,
51926deccbSFrançois Tigeot 			      struct radeon_sa_manager *sa_manager,
52c6f73aabSFrançois Tigeot 			      unsigned size, u32 align, u32 domain, u32 flags)
53926deccbSFrançois Tigeot {
54926deccbSFrançois Tigeot 	int i, r;
55926deccbSFrançois Tigeot 
567dcf36dcSFrançois Tigeot 	init_waitqueue_head(&sa_manager->wq);
57926deccbSFrançois Tigeot 	sa_manager->bo = NULL;
58926deccbSFrançois Tigeot 	sa_manager->size = size;
59926deccbSFrançois Tigeot 	sa_manager->domain = domain;
6057e252bfSMichael Neumann 	sa_manager->align = align;
61926deccbSFrançois Tigeot 	sa_manager->hole = &sa_manager->olist;
62926deccbSFrançois Tigeot 	INIT_LIST_HEAD(&sa_manager->olist);
63926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
64926deccbSFrançois Tigeot 		INIT_LIST_HEAD(&sa_manager->flist[i]);
65926deccbSFrançois Tigeot 	}
66926deccbSFrançois Tigeot 
6757e252bfSMichael Neumann 	r = radeon_bo_create(rdev, size, align, true,
687dcf36dcSFrançois Tigeot 			     domain, flags, NULL, NULL, &sa_manager->bo);
69926deccbSFrançois Tigeot 	if (r) {
70926deccbSFrançois Tigeot 		dev_err(rdev->dev, "(%d) failed to allocate bo for manager\n", r);
71926deccbSFrançois Tigeot 		return r;
72926deccbSFrançois Tigeot 	}
73926deccbSFrançois Tigeot 
74926deccbSFrançois Tigeot 	return r;
75926deccbSFrançois Tigeot }
76926deccbSFrançois Tigeot 
radeon_sa_bo_manager_fini(struct radeon_device * rdev,struct radeon_sa_manager * sa_manager)77926deccbSFrançois Tigeot void radeon_sa_bo_manager_fini(struct radeon_device *rdev,
78926deccbSFrançois Tigeot 			       struct radeon_sa_manager *sa_manager)
79926deccbSFrançois Tigeot {
80926deccbSFrançois Tigeot 	struct radeon_sa_bo *sa_bo, *tmp;
81926deccbSFrançois Tigeot 
82926deccbSFrançois Tigeot 	if (!list_empty(&sa_manager->olist)) {
83926deccbSFrançois Tigeot 		sa_manager->hole = &sa_manager->olist,
84926deccbSFrançois Tigeot 		radeon_sa_bo_try_free(sa_manager);
85926deccbSFrançois Tigeot 		if (!list_empty(&sa_manager->olist)) {
86926deccbSFrançois Tigeot 			dev_err(rdev->dev, "sa_manager is not empty, clearing anyway\n");
87926deccbSFrançois Tigeot 		}
88926deccbSFrançois Tigeot 	}
89926deccbSFrançois Tigeot 	list_for_each_entry_safe(sa_bo, tmp, &sa_manager->olist, olist) {
90926deccbSFrançois Tigeot 		radeon_sa_bo_remove_locked(sa_bo);
91926deccbSFrançois Tigeot 	}
92926deccbSFrançois Tigeot 	radeon_bo_unref(&sa_manager->bo);
93926deccbSFrançois Tigeot 	sa_manager->size = 0;
94926deccbSFrançois Tigeot }
95926deccbSFrançois Tigeot 
radeon_sa_bo_manager_start(struct radeon_device * rdev,struct radeon_sa_manager * sa_manager)96926deccbSFrançois Tigeot int radeon_sa_bo_manager_start(struct radeon_device *rdev,
97926deccbSFrançois Tigeot 			       struct radeon_sa_manager *sa_manager)
98926deccbSFrançois Tigeot {
99926deccbSFrançois Tigeot 	int r;
100926deccbSFrançois Tigeot 
101926deccbSFrançois Tigeot 	if (sa_manager->bo == NULL) {
102926deccbSFrançois Tigeot 		dev_err(rdev->dev, "no bo for sa manager\n");
103926deccbSFrançois Tigeot 		return -EINVAL;
104926deccbSFrançois Tigeot 	}
105926deccbSFrançois Tigeot 
106926deccbSFrançois Tigeot 	/* map the buffer */
107926deccbSFrançois Tigeot 	r = radeon_bo_reserve(sa_manager->bo, false);
108926deccbSFrançois Tigeot 	if (r) {
109926deccbSFrançois Tigeot 		dev_err(rdev->dev, "(%d) failed to reserve manager bo\n", r);
110926deccbSFrançois Tigeot 		return r;
111926deccbSFrançois Tigeot 	}
112926deccbSFrançois Tigeot 	r = radeon_bo_pin(sa_manager->bo, sa_manager->domain, &sa_manager->gpu_addr);
113926deccbSFrançois Tigeot 	if (r) {
114926deccbSFrançois Tigeot 		radeon_bo_unreserve(sa_manager->bo);
115926deccbSFrançois Tigeot 		dev_err(rdev->dev, "(%d) failed to pin manager bo\n", r);
116926deccbSFrançois Tigeot 		return r;
117926deccbSFrançois Tigeot 	}
118926deccbSFrançois Tigeot 	r = radeon_bo_kmap(sa_manager->bo, &sa_manager->cpu_ptr);
119926deccbSFrançois Tigeot 	radeon_bo_unreserve(sa_manager->bo);
120926deccbSFrançois Tigeot 	return r;
121926deccbSFrançois Tigeot }
122926deccbSFrançois Tigeot 
radeon_sa_bo_manager_suspend(struct radeon_device * rdev,struct radeon_sa_manager * sa_manager)123926deccbSFrançois Tigeot int radeon_sa_bo_manager_suspend(struct radeon_device *rdev,
124926deccbSFrançois Tigeot 				 struct radeon_sa_manager *sa_manager)
125926deccbSFrançois Tigeot {
126926deccbSFrançois Tigeot 	int r;
127926deccbSFrançois Tigeot 
128926deccbSFrançois Tigeot 	if (sa_manager->bo == NULL) {
129926deccbSFrançois Tigeot 		dev_err(rdev->dev, "no bo for sa manager\n");
130926deccbSFrançois Tigeot 		return -EINVAL;
131926deccbSFrançois Tigeot 	}
132926deccbSFrançois Tigeot 
133926deccbSFrançois Tigeot 	r = radeon_bo_reserve(sa_manager->bo, false);
134926deccbSFrançois Tigeot 	if (!r) {
135926deccbSFrançois Tigeot 		radeon_bo_kunmap(sa_manager->bo);
136926deccbSFrançois Tigeot 		radeon_bo_unpin(sa_manager->bo);
137926deccbSFrançois Tigeot 		radeon_bo_unreserve(sa_manager->bo);
138926deccbSFrançois Tigeot 	}
139926deccbSFrançois Tigeot 	return r;
140926deccbSFrançois Tigeot }
141926deccbSFrançois Tigeot 
radeon_sa_bo_remove_locked(struct radeon_sa_bo * sa_bo)142926deccbSFrançois Tigeot static void radeon_sa_bo_remove_locked(struct radeon_sa_bo *sa_bo)
143926deccbSFrançois Tigeot {
144926deccbSFrançois Tigeot 	struct radeon_sa_manager *sa_manager = sa_bo->manager;
145926deccbSFrançois Tigeot 	if (sa_manager->hole == &sa_bo->olist) {
146926deccbSFrançois Tigeot 		sa_manager->hole = sa_bo->olist.prev;
147926deccbSFrançois Tigeot 	}
148926deccbSFrançois Tigeot 	list_del_init(&sa_bo->olist);
149926deccbSFrançois Tigeot 	list_del_init(&sa_bo->flist);
150926deccbSFrançois Tigeot 	radeon_fence_unref(&sa_bo->fence);
151c4ef309bSzrj 	kfree(sa_bo);
152926deccbSFrançois Tigeot }
153926deccbSFrançois Tigeot 
radeon_sa_bo_try_free(struct radeon_sa_manager * sa_manager)154926deccbSFrançois Tigeot static void radeon_sa_bo_try_free(struct radeon_sa_manager *sa_manager)
155926deccbSFrançois Tigeot {
156926deccbSFrançois Tigeot 	struct radeon_sa_bo *sa_bo, *tmp;
157926deccbSFrançois Tigeot 
158926deccbSFrançois Tigeot 	if (sa_manager->hole->next == &sa_manager->olist)
159926deccbSFrançois Tigeot 		return;
160926deccbSFrançois Tigeot 
161926deccbSFrançois Tigeot 	sa_bo = list_entry(sa_manager->hole->next, struct radeon_sa_bo, olist);
162926deccbSFrançois Tigeot 	list_for_each_entry_safe_from(sa_bo, tmp, &sa_manager->olist, olist) {
163926deccbSFrançois Tigeot 		if (sa_bo->fence == NULL || !radeon_fence_signaled(sa_bo->fence)) {
164926deccbSFrançois Tigeot 			return;
165926deccbSFrançois Tigeot 		}
166926deccbSFrançois Tigeot 		radeon_sa_bo_remove_locked(sa_bo);
167926deccbSFrançois Tigeot 	}
168926deccbSFrançois Tigeot }
169926deccbSFrançois Tigeot 
radeon_sa_bo_hole_soffset(struct radeon_sa_manager * sa_manager)170926deccbSFrançois Tigeot static inline unsigned radeon_sa_bo_hole_soffset(struct radeon_sa_manager *sa_manager)
171926deccbSFrançois Tigeot {
172926deccbSFrançois Tigeot 	struct list_head *hole = sa_manager->hole;
173926deccbSFrançois Tigeot 
174926deccbSFrançois Tigeot 	if (hole != &sa_manager->olist) {
175926deccbSFrançois Tigeot 		return list_entry(hole, struct radeon_sa_bo, olist)->eoffset;
176926deccbSFrançois Tigeot 	}
177926deccbSFrançois Tigeot 	return 0;
178926deccbSFrançois Tigeot }
179926deccbSFrançois Tigeot 
radeon_sa_bo_hole_eoffset(struct radeon_sa_manager * sa_manager)180926deccbSFrançois Tigeot static inline unsigned radeon_sa_bo_hole_eoffset(struct radeon_sa_manager *sa_manager)
181926deccbSFrançois Tigeot {
182926deccbSFrançois Tigeot 	struct list_head *hole = sa_manager->hole;
183926deccbSFrançois Tigeot 
184926deccbSFrançois Tigeot 	if (hole->next != &sa_manager->olist) {
185926deccbSFrançois Tigeot 		return list_entry(hole->next, struct radeon_sa_bo, olist)->soffset;
186926deccbSFrançois Tigeot 	}
187926deccbSFrançois Tigeot 	return sa_manager->size;
188926deccbSFrançois Tigeot }
189926deccbSFrançois Tigeot 
radeon_sa_bo_try_alloc(struct radeon_sa_manager * sa_manager,struct radeon_sa_bo * sa_bo,unsigned size,unsigned align)190926deccbSFrançois Tigeot static bool radeon_sa_bo_try_alloc(struct radeon_sa_manager *sa_manager,
191926deccbSFrançois Tigeot 				   struct radeon_sa_bo *sa_bo,
192926deccbSFrançois Tigeot 				   unsigned size, unsigned align)
193926deccbSFrançois Tigeot {
194926deccbSFrançois Tigeot 	unsigned soffset, eoffset, wasted;
195926deccbSFrançois Tigeot 
196926deccbSFrançois Tigeot 	soffset = radeon_sa_bo_hole_soffset(sa_manager);
197926deccbSFrançois Tigeot 	eoffset = radeon_sa_bo_hole_eoffset(sa_manager);
198926deccbSFrançois Tigeot 	wasted = (align - (soffset % align)) % align;
199926deccbSFrançois Tigeot 
200926deccbSFrançois Tigeot 	if ((eoffset - soffset) >= (size + wasted)) {
201926deccbSFrançois Tigeot 		soffset += wasted;
202926deccbSFrançois Tigeot 
203926deccbSFrançois Tigeot 		sa_bo->manager = sa_manager;
204926deccbSFrançois Tigeot 		sa_bo->soffset = soffset;
205926deccbSFrançois Tigeot 		sa_bo->eoffset = soffset + size;
206926deccbSFrançois Tigeot 		list_add(&sa_bo->olist, sa_manager->hole);
207926deccbSFrançois Tigeot 		INIT_LIST_HEAD(&sa_bo->flist);
208926deccbSFrançois Tigeot 		sa_manager->hole = &sa_bo->olist;
209926deccbSFrançois Tigeot 		return true;
210926deccbSFrançois Tigeot 	}
211926deccbSFrançois Tigeot 	return false;
212926deccbSFrançois Tigeot }
213926deccbSFrançois Tigeot 
214926deccbSFrançois Tigeot /**
215926deccbSFrançois Tigeot  * radeon_sa_event - Check if we can stop waiting
216926deccbSFrançois Tigeot  *
217926deccbSFrançois Tigeot  * @sa_manager: pointer to the sa_manager
218926deccbSFrançois Tigeot  * @size: number of bytes we want to allocate
219926deccbSFrançois Tigeot  * @align: alignment we need to match
220926deccbSFrançois Tigeot  *
221926deccbSFrançois Tigeot  * Check if either there is a fence we can wait for or
222926deccbSFrançois Tigeot  * enough free memory to satisfy the allocation directly
223926deccbSFrançois Tigeot  */
radeon_sa_event(struct radeon_sa_manager * sa_manager,unsigned size,unsigned align)224926deccbSFrançois Tigeot static bool radeon_sa_event(struct radeon_sa_manager *sa_manager,
225926deccbSFrançois Tigeot 			    unsigned size, unsigned align)
226926deccbSFrançois Tigeot {
227926deccbSFrançois Tigeot 	unsigned soffset, eoffset, wasted;
228926deccbSFrançois Tigeot 	int i;
229926deccbSFrançois Tigeot 
230926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
231926deccbSFrançois Tigeot 		if (!list_empty(&sa_manager->flist[i])) {
232926deccbSFrançois Tigeot 			return true;
233926deccbSFrançois Tigeot 		}
234926deccbSFrançois Tigeot 	}
235926deccbSFrançois Tigeot 
236926deccbSFrançois Tigeot 	soffset = radeon_sa_bo_hole_soffset(sa_manager);
237926deccbSFrançois Tigeot 	eoffset = radeon_sa_bo_hole_eoffset(sa_manager);
238926deccbSFrançois Tigeot 	wasted = (align - (soffset % align)) % align;
239926deccbSFrançois Tigeot 
240926deccbSFrançois Tigeot 	if ((eoffset - soffset) >= (size + wasted)) {
241926deccbSFrançois Tigeot 		return true;
242926deccbSFrançois Tigeot 	}
243926deccbSFrançois Tigeot 
244926deccbSFrançois Tigeot 	return false;
245926deccbSFrançois Tigeot }
246926deccbSFrançois Tigeot 
radeon_sa_bo_next_hole(struct radeon_sa_manager * sa_manager,struct radeon_fence ** fences,unsigned * tries)247926deccbSFrançois Tigeot static bool radeon_sa_bo_next_hole(struct radeon_sa_manager *sa_manager,
248926deccbSFrançois Tigeot 				   struct radeon_fence **fences,
249926deccbSFrançois Tigeot 				   unsigned *tries)
250926deccbSFrançois Tigeot {
251926deccbSFrançois Tigeot 	struct radeon_sa_bo *best_bo = NULL;
252926deccbSFrançois Tigeot 	unsigned i, soffset, best, tmp;
253926deccbSFrançois Tigeot 
254926deccbSFrançois Tigeot 	/* if hole points to the end of the buffer */
255926deccbSFrançois Tigeot 	if (sa_manager->hole->next == &sa_manager->olist) {
256926deccbSFrançois Tigeot 		/* try again with its beginning */
257926deccbSFrançois Tigeot 		sa_manager->hole = &sa_manager->olist;
258926deccbSFrançois Tigeot 		return true;
259926deccbSFrançois Tigeot 	}
260926deccbSFrançois Tigeot 
261926deccbSFrançois Tigeot 	soffset = radeon_sa_bo_hole_soffset(sa_manager);
262926deccbSFrançois Tigeot 	/* to handle wrap around we add sa_manager->size */
263926deccbSFrançois Tigeot 	best = sa_manager->size * 2;
264926deccbSFrançois Tigeot 	/* go over all fence list and try to find the closest sa_bo
265926deccbSFrançois Tigeot 	 * of the current last
266926deccbSFrançois Tigeot 	 */
267926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
268926deccbSFrançois Tigeot 		struct radeon_sa_bo *sa_bo;
269926deccbSFrançois Tigeot 
270926deccbSFrançois Tigeot 		if (list_empty(&sa_manager->flist[i])) {
271926deccbSFrançois Tigeot 			continue;
272926deccbSFrançois Tigeot 		}
273926deccbSFrançois Tigeot 
274926deccbSFrançois Tigeot 		sa_bo = list_first_entry(&sa_manager->flist[i],
275926deccbSFrançois Tigeot 					 struct radeon_sa_bo, flist);
276926deccbSFrançois Tigeot 
277926deccbSFrançois Tigeot 		if (!radeon_fence_signaled(sa_bo->fence)) {
278926deccbSFrançois Tigeot 			fences[i] = sa_bo->fence;
279926deccbSFrançois Tigeot 			continue;
280926deccbSFrançois Tigeot 		}
281926deccbSFrançois Tigeot 
282926deccbSFrançois Tigeot 		/* limit the number of tries each ring gets */
283926deccbSFrançois Tigeot 		if (tries[i] > 2) {
284926deccbSFrançois Tigeot 			continue;
285926deccbSFrançois Tigeot 		}
286926deccbSFrançois Tigeot 
287926deccbSFrançois Tigeot 		tmp = sa_bo->soffset;
288926deccbSFrançois Tigeot 		if (tmp < soffset) {
289926deccbSFrançois Tigeot 			/* wrap around, pretend it's after */
290926deccbSFrançois Tigeot 			tmp += sa_manager->size;
291926deccbSFrançois Tigeot 		}
292926deccbSFrançois Tigeot 		tmp -= soffset;
293926deccbSFrançois Tigeot 		if (tmp < best) {
294926deccbSFrançois Tigeot 			/* this sa bo is the closest one */
295926deccbSFrançois Tigeot 			best = tmp;
296926deccbSFrançois Tigeot 			best_bo = sa_bo;
297926deccbSFrançois Tigeot 		}
298926deccbSFrançois Tigeot 	}
299926deccbSFrançois Tigeot 
300926deccbSFrançois Tigeot 	if (best_bo) {
301926deccbSFrançois Tigeot 		++tries[best_bo->fence->ring];
302926deccbSFrançois Tigeot 		sa_manager->hole = best_bo->olist.prev;
303926deccbSFrançois Tigeot 
304926deccbSFrançois Tigeot 		/* we knew that this one is signaled,
305926deccbSFrançois Tigeot 		   so it's save to remote it */
306926deccbSFrançois Tigeot 		radeon_sa_bo_remove_locked(best_bo);
307926deccbSFrançois Tigeot 		return true;
308926deccbSFrançois Tigeot 	}
309926deccbSFrançois Tigeot 	return false;
310926deccbSFrançois Tigeot }
311926deccbSFrançois Tigeot 
radeon_sa_bo_new(struct radeon_device * rdev,struct radeon_sa_manager * sa_manager,struct radeon_sa_bo ** sa_bo,unsigned size,unsigned align)312926deccbSFrançois Tigeot int radeon_sa_bo_new(struct radeon_device *rdev,
313926deccbSFrançois Tigeot 		     struct radeon_sa_manager *sa_manager,
314926deccbSFrançois Tigeot 		     struct radeon_sa_bo **sa_bo,
315c6f73aabSFrançois Tigeot 		     unsigned size, unsigned align)
316926deccbSFrançois Tigeot {
317926deccbSFrançois Tigeot 	struct radeon_fence *fences[RADEON_NUM_RINGS];
318926deccbSFrançois Tigeot 	unsigned tries[RADEON_NUM_RINGS];
319926deccbSFrançois Tigeot 	int i, r;
320926deccbSFrançois Tigeot 
32157e252bfSMichael Neumann 	BUG_ON(align > sa_manager->align);
32257e252bfSMichael Neumann 	BUG_ON(size > sa_manager->size);
323926deccbSFrançois Tigeot 
3247dcf36dcSFrançois Tigeot 	*sa_bo = kmalloc(sizeof(struct radeon_sa_bo), M_DRM, GFP_KERNEL);
325926deccbSFrançois Tigeot 	if ((*sa_bo) == NULL) {
326926deccbSFrançois Tigeot 		return -ENOMEM;
327926deccbSFrançois Tigeot 	}
328926deccbSFrançois Tigeot 	(*sa_bo)->manager = sa_manager;
329926deccbSFrançois Tigeot 	(*sa_bo)->fence = NULL;
330926deccbSFrançois Tigeot 	INIT_LIST_HEAD(&(*sa_bo)->olist);
331926deccbSFrançois Tigeot 	INIT_LIST_HEAD(&(*sa_bo)->flist);
332926deccbSFrançois Tigeot 
3337dcf36dcSFrançois Tigeot 	lockmgr(&sa_manager->wq.lock, LK_EXCLUSIVE);
334926deccbSFrançois Tigeot 	do {
335926deccbSFrançois Tigeot 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
336926deccbSFrançois Tigeot 			fences[i] = NULL;
337926deccbSFrançois Tigeot 			tries[i] = 0;
338926deccbSFrançois Tigeot 		}
339926deccbSFrançois Tigeot 
340926deccbSFrançois Tigeot 		do {
341926deccbSFrançois Tigeot 			radeon_sa_bo_try_free(sa_manager);
342926deccbSFrançois Tigeot 
343926deccbSFrançois Tigeot 			if (radeon_sa_bo_try_alloc(sa_manager, *sa_bo,
344926deccbSFrançois Tigeot 						   size, align)) {
3457dcf36dcSFrançois Tigeot 				lockmgr(&sa_manager->wq.lock, LK_RELEASE);
346926deccbSFrançois Tigeot 				return 0;
347926deccbSFrançois Tigeot 			}
348926deccbSFrançois Tigeot 
349926deccbSFrançois Tigeot 			/* see if we can skip over some allocations */
350926deccbSFrançois Tigeot 		} while (radeon_sa_bo_next_hole(sa_manager, fences, tries));
351926deccbSFrançois Tigeot 
352c59a5c48SFrançois Tigeot 		for (i = 0; i < RADEON_NUM_RINGS; ++i)
353c59a5c48SFrançois Tigeot 			radeon_fence_ref(fences[i]);
354c59a5c48SFrançois Tigeot 
3557dcf36dcSFrançois Tigeot 		lockmgr(&sa_manager->wq.lock, LK_RELEASE);
356926deccbSFrançois Tigeot 		r = radeon_fence_wait_any(rdev, fences, false);
357c59a5c48SFrançois Tigeot 		for (i = 0; i < RADEON_NUM_RINGS; ++i)
358c59a5c48SFrançois Tigeot 			radeon_fence_unref(&fences[i]);
3597dcf36dcSFrançois Tigeot 		lockmgr(&sa_manager->wq.lock, LK_EXCLUSIVE);
360926deccbSFrançois Tigeot 		/* if we have nothing to wait for block */
361c6f73aabSFrançois Tigeot 		if (r == -ENOENT) {
3627dcf36dcSFrançois Tigeot 			r = wait_event_interruptible_locked(
3637dcf36dcSFrançois Tigeot 				sa_manager->wq,
3647dcf36dcSFrançois Tigeot 				radeon_sa_event(sa_manager, size, align)
3657dcf36dcSFrançois Tigeot 			);
366926deccbSFrançois Tigeot 		}
367926deccbSFrançois Tigeot 
368926deccbSFrançois Tigeot 	} while (!r);
369926deccbSFrançois Tigeot 
3707dcf36dcSFrançois Tigeot 	lockmgr(&sa_manager->wq.lock, LK_RELEASE);
371c4ef309bSzrj 	kfree(*sa_bo);
372926deccbSFrançois Tigeot 	*sa_bo = NULL;
373926deccbSFrançois Tigeot 	return r;
374926deccbSFrançois Tigeot }
375926deccbSFrançois Tigeot 
radeon_sa_bo_free(struct radeon_device * rdev,struct radeon_sa_bo ** sa_bo,struct radeon_fence * fence)376926deccbSFrançois Tigeot void radeon_sa_bo_free(struct radeon_device *rdev, struct radeon_sa_bo **sa_bo,
377926deccbSFrançois Tigeot 		       struct radeon_fence *fence)
378926deccbSFrançois Tigeot {
379926deccbSFrançois Tigeot 	struct radeon_sa_manager *sa_manager;
380926deccbSFrançois Tigeot 
381926deccbSFrançois Tigeot 	if (sa_bo == NULL || *sa_bo == NULL) {
382926deccbSFrançois Tigeot 		return;
383926deccbSFrançois Tigeot 	}
384926deccbSFrançois Tigeot 
385926deccbSFrançois Tigeot 	sa_manager = (*sa_bo)->manager;
3867dcf36dcSFrançois Tigeot 	lockmgr(&sa_manager->wq.lock, LK_EXCLUSIVE);
387926deccbSFrançois Tigeot 	if (fence && !radeon_fence_signaled(fence)) {
388926deccbSFrançois Tigeot 		(*sa_bo)->fence = radeon_fence_ref(fence);
389926deccbSFrançois Tigeot 		list_add_tail(&(*sa_bo)->flist,
390926deccbSFrançois Tigeot 			      &sa_manager->flist[fence->ring]);
391926deccbSFrançois Tigeot 	} else {
392926deccbSFrançois Tigeot 		radeon_sa_bo_remove_locked(*sa_bo);
393926deccbSFrançois Tigeot 	}
3947dcf36dcSFrançois Tigeot 	wake_up_all_locked(&sa_manager->wq);
3957dcf36dcSFrançois Tigeot 	lockmgr(&sa_manager->wq.lock, LK_RELEASE);
396926deccbSFrançois Tigeot 	*sa_bo = NULL;
397926deccbSFrançois Tigeot }
398926deccbSFrançois Tigeot 
399926deccbSFrançois Tigeot #if defined(CONFIG_DEBUG_FS)
radeon_sa_bo_dump_debug_info(struct radeon_sa_manager * sa_manager,struct seq_file * m)400926deccbSFrançois Tigeot void radeon_sa_bo_dump_debug_info(struct radeon_sa_manager *sa_manager,
401926deccbSFrançois Tigeot 				  struct seq_file *m)
402926deccbSFrançois Tigeot {
403926deccbSFrançois Tigeot 	struct radeon_sa_bo *i;
404926deccbSFrançois Tigeot 
405*ec5b6af4SFrançois Tigeot 	lockmgr(&sa_manager->wq.lock, LK_EXCLUSIVE);
406926deccbSFrançois Tigeot 	list_for_each_entry(i, &sa_manager->olist, olist) {
407c6f73aabSFrançois Tigeot 		uint64_t soffset = i->soffset + sa_manager->gpu_addr;
408c6f73aabSFrançois Tigeot 		uint64_t eoffset = i->eoffset + sa_manager->gpu_addr;
409926deccbSFrançois Tigeot 		if (&i->olist == sa_manager->hole) {
410926deccbSFrançois Tigeot 			seq_printf(m, ">");
411926deccbSFrançois Tigeot 		} else {
412926deccbSFrançois Tigeot 			seq_printf(m, " ");
413926deccbSFrançois Tigeot 		}
414c6f73aabSFrançois Tigeot 		seq_printf(m, "[0x%010llx 0x%010llx] size %8lld",
415c6f73aabSFrançois Tigeot 			   soffset, eoffset, eoffset - soffset);
416926deccbSFrançois Tigeot 		if (i->fence) {
417926deccbSFrançois Tigeot 			seq_printf(m, " protected by 0x%016llx on ring %d",
418926deccbSFrançois Tigeot 				   i->fence->seq, i->fence->ring);
419926deccbSFrançois Tigeot 		}
420926deccbSFrançois Tigeot 		seq_printf(m, "\n");
421926deccbSFrançois Tigeot 	}
422*ec5b6af4SFrançois Tigeot 	lockmgr(&sa_manager->wq.lock, LK_RELEASE);
423926deccbSFrançois Tigeot }
424926deccbSFrançois Tigeot #endif
425