Commit 849ee8a2 authored by Maarten Lankhorst's avatar Maarten Lankhorst Committed by Thomas Hellström

drm/suballoc: Extract amdgpu_sa.c as generic suballocation helper

Suballocating a buffer object is something that is not driver-specific
and useful for many drivers.

Use a slightly modified version of amdgpu_sa.c

v2:
- Style cleanups.
- Added / Modified documentation.
- Use u64 for the sizes and offset. The code dates back to 2012 and
  using unsigned int will probably soon come back to bite us.
  We can consider size_t as well for better 32-bit efficiency.
- Add and document gfp, intr and align arguments to drm_suballoc_new().
- Use drm_printer for debug output.

v3:
- Remove stale author info (Christian König)

v4:
- Avoid 64-bit integer divisions (kernel test robot <lkp@intel.com>)
- Use size_t rather than u64 for the managed range. (Thomas)
Signed-off-by: default avatarMaarten Lankhorst <maarten.lankhorst@linux.intel.com>
Co-developed-by: default avatarThomas Hellström <thomas.hellstrom@linux.intel.com>
Signed-off-by: default avatarThomas Hellström <thomas.hellstrom@linux.intel.com>
Reviewed-by: default avatarChristian König <christian.koenig@amd.com>
Acked-by: default avatarMaarten Lankhorst <maarten.lankhorst@linux.intel.com>
Acked-by: default avatarAlex Deucher <alexdeucher@gmail.com>
Acked-by: default avatarChristian König <christian.koenig@amd.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20230224095152.30134-2-thomas.hellstrom@linux.intel.com
parent ca62297b
......@@ -231,6 +231,10 @@ config DRM_GEM_SHMEM_HELPER
help
Choose this if you need the GEM shmem helper functions
config DRM_SUBALLOC_HELPER
tristate
depends on DRM
config DRM_SCHED
tristate
depends on DRM
......
......@@ -88,6 +88,9 @@ obj-$(CONFIG_DRM_GEM_DMA_HELPER) += drm_dma_helper.o
drm_shmem_helper-y := drm_gem_shmem_helper.o
obj-$(CONFIG_DRM_GEM_SHMEM_HELPER) += drm_shmem_helper.o
drm_suballoc_helper-y := drm_suballoc.o
obj-$(CONFIG_DRM_SUBALLOC_HELPER) += drm_suballoc_helper.o
drm_vram_helper-y := drm_gem_vram_helper.o
obj-$(CONFIG_DRM_VRAM_HELPER) += drm_vram_helper.o
......
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
/*
* Copyright 2011 Red Hat Inc.
* Copyright © 2022 Intel Corporation
*/
#ifndef _DRM_SUBALLOC_H_
#define _DRM_SUBALLOC_H_
#include <drm/drm_mm.h>
#include <linux/dma-fence.h>
#include <linux/types.h>
#define DRM_SUBALLOC_MAX_QUEUES 32
/**
* struct drm_suballoc_manager - fenced range allocations
* @wq: Wait queue for sleeping allocations on contention.
* @hole: Pointer to first hole node.
* @olist: List of allocated ranges.
* @flist: Array[fence context hash] of queues of fenced allocated ranges.
* @size: Size of the managed range.
* @align: Default alignment for the managed range.
*/
struct drm_suballoc_manager {
wait_queue_head_t wq;
struct list_head *hole;
struct list_head olist;
struct list_head flist[DRM_SUBALLOC_MAX_QUEUES];
size_t size;
size_t align;
};
/**
* struct drm_suballoc - Sub-allocated range
* @olist: List link for list of allocated ranges.
* @flist: List linkk for the manager fenced allocated ranges queues.
* @manager: The drm_suballoc_manager.
* @soffset: Start offset.
* @eoffset: End offset + 1 so that @eoffset - @soffset = size.
* @dma_fence: The fence protecting the allocation.
*/
struct drm_suballoc {
struct list_head olist;
struct list_head flist;
struct drm_suballoc_manager *manager;
size_t soffset;
size_t eoffset;
struct dma_fence *fence;
};
void drm_suballoc_manager_init(struct drm_suballoc_manager *sa_manager,
size_t size, size_t align);
void drm_suballoc_manager_fini(struct drm_suballoc_manager *sa_manager);
struct drm_suballoc *
drm_suballoc_new(struct drm_suballoc_manager *sa_manager, size_t size,
gfp_t gfp, bool intr, size_t align);
void drm_suballoc_free(struct drm_suballoc *sa, struct dma_fence *fence);
/**
* drm_suballoc_soffset - Range start.
* @sa: The struct drm_suballoc.
*
* Return: The start of the allocated range.
*/
static inline size_t drm_suballoc_soffset(struct drm_suballoc *sa)
{
return sa->soffset;
}
/**
* drm_suballoc_eoffset - Range end.
* @sa: The struct drm_suballoc.
*
* Return: The end of the allocated range + 1.
*/
static inline size_t drm_suballoc_eoffset(struct drm_suballoc *sa)
{
return sa->eoffset;
}
/**
* drm_suballoc_size - Range size.
* @sa: The struct drm_suballoc.
*
* Return: The size of the allocated range.
*/
static inline size_t drm_suballoc_size(struct drm_suballoc *sa)
{
return sa->eoffset - sa->soffset;
}
#ifdef CONFIG_DEBUG_FS
void drm_suballoc_dump_debug_info(struct drm_suballoc_manager *sa_manager,
struct drm_printer *p,
unsigned long long suballoc_base);
#else
static inline void
drm_suballoc_dump_debug_info(struct drm_suballoc_manager *sa_manager,
struct drm_printer *p,
unsigned long long suballoc_base)
{ }
#endif
#endif /* _DRM_SUBALLOC_H_ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment