2 * Copyright 2011 Red Hat Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
28 * Jerome Glisse <glisse@freedesktop.org>
32 * We store the last allocated bo in "hole", we always try to allocate
33 * after the last allocated bo. Principle is that in a linear GPU ring
34 * progression was is after last is the oldest bo we allocated and thus
35 * the first one that should no longer be in use by the GPU.
37 * If it's not the case we skip over the bo after last to the closest
38 * done bo if such one exist. If none exist and we are not asked to
39 * block we report failure to allocate.
41 * If we are asked to block we wait on all the oldest fence of all
42 * rings. We just wait for any of those fence to complete.
47 static void amdgpu_sa_bo_remove_locked(struct amdgpu_sa_bo *sa_bo);
48 static void amdgpu_sa_bo_try_free(struct amdgpu_sa_manager *sa_manager);
50 int amdgpu_sa_bo_manager_init(struct amdgpu_device *adev,
51 struct amdgpu_sa_manager *sa_manager,
52 unsigned size, u32 align, u32 domain)
56 init_waitqueue_head(&sa_manager->wq);
57 sa_manager->bo = NULL;
58 sa_manager->size = size;
59 sa_manager->domain = domain;
60 sa_manager->align = align;
61 sa_manager->hole = &sa_manager->olist;
62 INIT_LIST_HEAD(&sa_manager->olist);
63 for (i = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i)
64 INIT_LIST_HEAD(&sa_manager->flist[i]);
66 r = amdgpu_bo_create(adev, size, align, true, domain,
67 0, NULL, NULL, &sa_manager->bo);
69 dev_err(adev->dev, "(%d) failed to allocate bo for manager\n", r);
76 void amdgpu_sa_bo_manager_fini(struct amdgpu_device *adev,
77 struct amdgpu_sa_manager *sa_manager)
79 struct amdgpu_sa_bo *sa_bo, *tmp;
81 if (!list_empty(&sa_manager->olist)) {
82 sa_manager->hole = &sa_manager->olist,
83 amdgpu_sa_bo_try_free(sa_manager);
84 if (!list_empty(&sa_manager->olist)) {
85 dev_err(adev->dev, "sa_manager is not empty, clearing anyway\n");
88 list_for_each_entry_safe(sa_bo, tmp, &sa_manager->olist, olist) {
89 amdgpu_sa_bo_remove_locked(sa_bo);
91 amdgpu_bo_unref(&sa_manager->bo);
95 int amdgpu_sa_bo_manager_start(struct amdgpu_device *adev,
96 struct amdgpu_sa_manager *sa_manager)
100 if (sa_manager->bo == NULL) {
101 dev_err(adev->dev, "no bo for sa manager\n");
106 r = amdgpu_bo_reserve(sa_manager->bo, false);
108 dev_err(adev->dev, "(%d) failed to reserve manager bo\n", r);
111 r = amdgpu_bo_pin(sa_manager->bo, sa_manager->domain, &sa_manager->gpu_addr);
113 amdgpu_bo_unreserve(sa_manager->bo);
114 dev_err(adev->dev, "(%d) failed to pin manager bo\n", r);
117 r = amdgpu_bo_kmap(sa_manager->bo, &sa_manager->cpu_ptr);
118 amdgpu_bo_unreserve(sa_manager->bo);
122 int amdgpu_sa_bo_manager_suspend(struct amdgpu_device *adev,
123 struct amdgpu_sa_manager *sa_manager)
127 if (sa_manager->bo == NULL) {
128 dev_err(adev->dev, "no bo for sa manager\n");
132 r = amdgpu_bo_reserve(sa_manager->bo, false);
134 amdgpu_bo_kunmap(sa_manager->bo);
135 amdgpu_bo_unpin(sa_manager->bo);
136 amdgpu_bo_unreserve(sa_manager->bo);
141 static void amdgpu_sa_bo_remove_locked(struct amdgpu_sa_bo *sa_bo)
143 struct amdgpu_sa_manager *sa_manager = sa_bo->manager;
144 if (sa_manager->hole == &sa_bo->olist) {
145 sa_manager->hole = sa_bo->olist.prev;
147 list_del_init(&sa_bo->olist);
148 list_del_init(&sa_bo->flist);
149 fence_put(sa_bo->fence);
153 static void amdgpu_sa_bo_try_free(struct amdgpu_sa_manager *sa_manager)
155 struct amdgpu_sa_bo *sa_bo, *tmp;
157 if (sa_manager->hole->next == &sa_manager->olist)
160 sa_bo = list_entry(sa_manager->hole->next, struct amdgpu_sa_bo, olist);
161 list_for_each_entry_safe_from(sa_bo, tmp, &sa_manager->olist, olist) {
162 if (sa_bo->fence == NULL ||
163 !fence_is_signaled(sa_bo->fence)) {
166 amdgpu_sa_bo_remove_locked(sa_bo);
170 static inline unsigned amdgpu_sa_bo_hole_soffset(struct amdgpu_sa_manager *sa_manager)
172 struct list_head *hole = sa_manager->hole;
174 if (hole != &sa_manager->olist) {
175 return list_entry(hole, struct amdgpu_sa_bo, olist)->eoffset;
180 static inline unsigned amdgpu_sa_bo_hole_eoffset(struct amdgpu_sa_manager *sa_manager)
182 struct list_head *hole = sa_manager->hole;
184 if (hole->next != &sa_manager->olist) {
185 return list_entry(hole->next, struct amdgpu_sa_bo, olist)->soffset;
187 return sa_manager->size;
190 static bool amdgpu_sa_bo_try_alloc(struct amdgpu_sa_manager *sa_manager,
191 struct amdgpu_sa_bo *sa_bo,
192 unsigned size, unsigned align)
194 unsigned soffset, eoffset, wasted;
196 soffset = amdgpu_sa_bo_hole_soffset(sa_manager);
197 eoffset = amdgpu_sa_bo_hole_eoffset(sa_manager);
198 wasted = (align - (soffset % align)) % align;
200 if ((eoffset - soffset) >= (size + wasted)) {
203 sa_bo->manager = sa_manager;
204 sa_bo->soffset = soffset;
205 sa_bo->eoffset = soffset + size;
206 list_add(&sa_bo->olist, sa_manager->hole);
207 INIT_LIST_HEAD(&sa_bo->flist);
208 sa_manager->hole = &sa_bo->olist;
215 * amdgpu_sa_event - Check if we can stop waiting
217 * @sa_manager: pointer to the sa_manager
218 * @size: number of bytes we want to allocate
219 * @align: alignment we need to match
221 * Check if either there is a fence we can wait for or
222 * enough free memory to satisfy the allocation directly
224 static bool amdgpu_sa_event(struct amdgpu_sa_manager *sa_manager,
225 unsigned size, unsigned align)
227 unsigned soffset, eoffset, wasted;
230 for (i = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i)
231 if (!list_empty(&sa_manager->flist[i]))
234 soffset = amdgpu_sa_bo_hole_soffset(sa_manager);
235 eoffset = amdgpu_sa_bo_hole_eoffset(sa_manager);
236 wasted = (align - (soffset % align)) % align;
238 if ((eoffset - soffset) >= (size + wasted)) {
245 static bool amdgpu_sa_bo_next_hole(struct amdgpu_sa_manager *sa_manager,
246 struct fence **fences,
249 struct amdgpu_sa_bo *best_bo = NULL;
250 unsigned i, soffset, best, tmp;
252 /* if hole points to the end of the buffer */
253 if (sa_manager->hole->next == &sa_manager->olist) {
254 /* try again with its beginning */
255 sa_manager->hole = &sa_manager->olist;
259 soffset = amdgpu_sa_bo_hole_soffset(sa_manager);
260 /* to handle wrap around we add sa_manager->size */
261 best = sa_manager->size * 2;
262 /* go over all fence list and try to find the closest sa_bo
263 * of the current last
265 for (i = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i) {
266 struct amdgpu_sa_bo *sa_bo;
268 if (list_empty(&sa_manager->flist[i]))
271 sa_bo = list_first_entry(&sa_manager->flist[i],
272 struct amdgpu_sa_bo, flist);
274 if (!fence_is_signaled(sa_bo->fence)) {
275 fences[i] = sa_bo->fence;
279 /* limit the number of tries each ring gets */
284 tmp = sa_bo->soffset;
286 /* wrap around, pretend it's after */
287 tmp += sa_manager->size;
291 /* this sa bo is the closest one */
298 uint32_t idx = best_bo->fence->context;
300 idx %= AMDGPU_SA_NUM_FENCE_LISTS;
302 sa_manager->hole = best_bo->olist.prev;
304 /* we knew that this one is signaled,
305 so it's save to remote it */
306 amdgpu_sa_bo_remove_locked(best_bo);
312 int amdgpu_sa_bo_new(struct amdgpu_sa_manager *sa_manager,
313 struct amdgpu_sa_bo **sa_bo,
314 unsigned size, unsigned align)
316 struct fence *fences[AMDGPU_SA_NUM_FENCE_LISTS];
317 unsigned tries[AMDGPU_SA_NUM_FENCE_LISTS];
322 if (WARN_ON_ONCE(align > sa_manager->align))
325 if (WARN_ON_ONCE(size > sa_manager->size))
328 *sa_bo = kmalloc(sizeof(struct amdgpu_sa_bo), GFP_KERNEL);
329 if ((*sa_bo) == NULL) {
332 (*sa_bo)->manager = sa_manager;
333 (*sa_bo)->fence = NULL;
334 INIT_LIST_HEAD(&(*sa_bo)->olist);
335 INIT_LIST_HEAD(&(*sa_bo)->flist);
337 spin_lock(&sa_manager->wq.lock);
339 for (i = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i) {
345 amdgpu_sa_bo_try_free(sa_manager);
347 if (amdgpu_sa_bo_try_alloc(sa_manager, *sa_bo,
349 spin_unlock(&sa_manager->wq.lock);
353 /* see if we can skip over some allocations */
354 } while (amdgpu_sa_bo_next_hole(sa_manager, fences, tries));
356 for (i = 0, count = 0; i < AMDGPU_SA_NUM_FENCE_LISTS; ++i)
358 fences[count++] = fence_get(fences[i]);
361 spin_unlock(&sa_manager->wq.lock);
362 t = fence_wait_any_timeout(fences, count, false,
363 MAX_SCHEDULE_TIMEOUT);
364 for (i = 0; i < count; ++i)
365 fence_put(fences[i]);
368 spin_lock(&sa_manager->wq.lock);
370 /* if we have nothing to wait for block */
371 r = wait_event_interruptible_locked(
373 amdgpu_sa_event(sa_manager, size, align)
379 spin_unlock(&sa_manager->wq.lock);
385 void amdgpu_sa_bo_free(struct amdgpu_device *adev, struct amdgpu_sa_bo **sa_bo,
388 struct amdgpu_sa_manager *sa_manager;
390 if (sa_bo == NULL || *sa_bo == NULL) {
394 sa_manager = (*sa_bo)->manager;
395 spin_lock(&sa_manager->wq.lock);
396 if (fence && !fence_is_signaled(fence)) {
399 (*sa_bo)->fence = fence_get(fence);
400 idx = fence->context % AMDGPU_SA_NUM_FENCE_LISTS;
401 list_add_tail(&(*sa_bo)->flist, &sa_manager->flist[idx]);
403 amdgpu_sa_bo_remove_locked(*sa_bo);
405 wake_up_all_locked(&sa_manager->wq);
406 spin_unlock(&sa_manager->wq.lock);
410 #if defined(CONFIG_DEBUG_FS)
412 void amdgpu_sa_bo_dump_debug_info(struct amdgpu_sa_manager *sa_manager,
415 struct amdgpu_sa_bo *i;
417 spin_lock(&sa_manager->wq.lock);
418 list_for_each_entry(i, &sa_manager->olist, olist) {
419 uint64_t soffset = i->soffset + sa_manager->gpu_addr;
420 uint64_t eoffset = i->eoffset + sa_manager->gpu_addr;
421 if (&i->olist == sa_manager->hole) {
426 seq_printf(m, "[0x%010llx 0x%010llx] size %8lld",
427 soffset, eoffset, eoffset - soffset);
430 seq_printf(m, " protected by 0x%08x on context %d",
431 i->fence->seqno, i->fence->context);
435 spin_unlock(&sa_manager->wq.lock);