162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2011 Red Hat Inc.
362306a36Sopenharmony_ci * All Rights Reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
662306a36Sopenharmony_ci * copy of this software and associated documentation files (the
762306a36Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
862306a36Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
962306a36Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
1062306a36Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
1162306a36Sopenharmony_ci * the following conditions:
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1462306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1562306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
1662306a36Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
1762306a36Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
1862306a36Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
1962306a36Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the
2262306a36Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
2362306a36Sopenharmony_ci * of the Software.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_ci/*
2762306a36Sopenharmony_ci * Authors:
2862306a36Sopenharmony_ci *    Jerome Glisse <glisse@freedesktop.org>
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci/* Algorithm:
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * We store the last allocated bo in "hole", we always try to allocate
3362306a36Sopenharmony_ci * after the last allocated bo. Principle is that in a linear GPU ring
3462306a36Sopenharmony_ci * progression was is after last is the oldest bo we allocated and thus
3562306a36Sopenharmony_ci * the first one that should no longer be in use by the GPU.
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci * If it's not the case we skip over the bo after last to the closest
3862306a36Sopenharmony_ci * done bo if such one exist. If none exist and we are not asked to
3962306a36Sopenharmony_ci * block we report failure to allocate.
4062306a36Sopenharmony_ci *
4162306a36Sopenharmony_ci * If we are asked to block we wait on all the oldest fence of all
4262306a36Sopenharmony_ci * rings. We just wait for any of those fence to complete.
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#include "amdgpu.h"
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciint amdgpu_sa_bo_manager_init(struct amdgpu_device *adev,
4862306a36Sopenharmony_ci			      struct amdgpu_sa_manager *sa_manager,
4962306a36Sopenharmony_ci			      unsigned int size, u32 suballoc_align, u32 domain)
5062306a36Sopenharmony_ci{
5162306a36Sopenharmony_ci	int r;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	r = amdgpu_bo_create_kernel(adev, size, AMDGPU_GPU_PAGE_SIZE, domain,
5462306a36Sopenharmony_ci				    &sa_manager->bo, &sa_manager->gpu_addr,
5562306a36Sopenharmony_ci				    &sa_manager->cpu_ptr);
5662306a36Sopenharmony_ci	if (r) {
5762306a36Sopenharmony_ci		dev_err(adev->dev, "(%d) failed to allocate bo for manager\n", r);
5862306a36Sopenharmony_ci		return r;
5962306a36Sopenharmony_ci	}
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	memset(sa_manager->cpu_ptr, 0, size);
6262306a36Sopenharmony_ci	drm_suballoc_manager_init(&sa_manager->base, size, suballoc_align);
6362306a36Sopenharmony_ci	return r;
6462306a36Sopenharmony_ci}
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_civoid amdgpu_sa_bo_manager_fini(struct amdgpu_device *adev,
6762306a36Sopenharmony_ci			       struct amdgpu_sa_manager *sa_manager)
6862306a36Sopenharmony_ci{
6962306a36Sopenharmony_ci	if (sa_manager->bo == NULL) {
7062306a36Sopenharmony_ci		dev_err(adev->dev, "no bo for sa manager\n");
7162306a36Sopenharmony_ci		return;
7262306a36Sopenharmony_ci	}
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	drm_suballoc_manager_fini(&sa_manager->base);
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	amdgpu_bo_free_kernel(&sa_manager->bo, &sa_manager->gpu_addr, &sa_manager->cpu_ptr);
7762306a36Sopenharmony_ci}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciint amdgpu_sa_bo_new(struct amdgpu_sa_manager *sa_manager,
8062306a36Sopenharmony_ci		     struct drm_suballoc **sa_bo,
8162306a36Sopenharmony_ci		     unsigned int size)
8262306a36Sopenharmony_ci{
8362306a36Sopenharmony_ci	struct drm_suballoc *sa = drm_suballoc_new(&sa_manager->base, size,
8462306a36Sopenharmony_ci						   GFP_KERNEL, false, 0);
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	if (IS_ERR(sa)) {
8762306a36Sopenharmony_ci		*sa_bo = NULL;
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci		return PTR_ERR(sa);
9062306a36Sopenharmony_ci	}
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	*sa_bo = sa;
9362306a36Sopenharmony_ci	return 0;
9462306a36Sopenharmony_ci}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_civoid amdgpu_sa_bo_free(struct amdgpu_device *adev, struct drm_suballoc **sa_bo,
9762306a36Sopenharmony_ci		       struct dma_fence *fence)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci	if (sa_bo == NULL || *sa_bo == NULL) {
10062306a36Sopenharmony_ci		return;
10162306a36Sopenharmony_ci	}
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	drm_suballoc_free(*sa_bo, fence);
10462306a36Sopenharmony_ci	*sa_bo = NULL;
10562306a36Sopenharmony_ci}
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci#if defined(CONFIG_DEBUG_FS)
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_civoid amdgpu_sa_bo_dump_debug_info(struct amdgpu_sa_manager *sa_manager,
11062306a36Sopenharmony_ci				  struct seq_file *m)
11162306a36Sopenharmony_ci{
11262306a36Sopenharmony_ci	struct drm_printer p = drm_seq_file_printer(m);
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	drm_suballoc_dump_debug_info(&sa_manager->base, &p, sa_manager->gpu_addr);
11562306a36Sopenharmony_ci}
11662306a36Sopenharmony_ci#endif
117