xref: /linux-6.15/include/linux/genalloc.h (revision de2dd0eb)
1 /*
2  * Basic general purpose allocator for managing special purpose
3  * memory, for example, memory that is not managed by the regular
4  * kmalloc/kfree interface.  Uses for this includes on-device special
5  * memory, uncached memory etc.
6  *
7  * It is safe to use the allocator in NMI handlers and other special
8  * unblockable contexts that could otherwise deadlock on locks.  This
9  * is implemented by using atomic operations and retries on any
10  * conflicts.  The disadvantage is that there may be livelocks in
11  * extreme cases.  For better scalability, one allocator can be used
12  * for each CPU.
13  *
14  * The lockless operation only works if there is enough memory
15  * available.  If new memory is added to the pool a lock has to be
16  * still taken.  So any user relying on locklessness has to ensure
17  * that sufficient memory is preallocated.
18  *
19  * The basic atomic operation of this allocator is cmpxchg on long.
20  * On architectures that don't have NMI-safe cmpxchg implementation,
21  * the allocator can NOT be used in NMI handler.  So code uses the
22  * allocator in NMI handler should depend on
23  * CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
24  *
25  * This source code is licensed under the GNU General Public License,
26  * Version 2.  See the file COPYING for more details.
27  */
28 
29 
30 #ifndef __GENALLOC_H__
31 #define __GENALLOC_H__
32 
33 #include <linux/types.h>
34 #include <linux/spinlock_types.h>
35 
36 struct device;
37 struct device_node;
38 struct gen_pool;
39 
40 /**
41  * Allocation callback function type definition
42  * @map: Pointer to bitmap
43  * @size: The bitmap size in bits
44  * @start: The bitnumber to start searching at
45  * @nr: The number of zeroed bits we're looking for
46  * @data: optional additional data used by @genpool_algo_t
47  */
48 typedef unsigned long (*genpool_algo_t)(unsigned long *map,
49 			unsigned long size,
50 			unsigned long start,
51 			unsigned int nr,
52 			void *data, struct gen_pool *pool);
53 
54 /*
55  *  General purpose special memory pool descriptor.
56  */
57 struct gen_pool {
58 	spinlock_t lock;
59 	struct list_head chunks;	/* list of chunks in this pool */
60 	int min_alloc_order;		/* minimum allocation order */
61 
62 	genpool_algo_t algo;		/* allocation function */
63 	void *data;
64 
65 	const char *name;
66 };
67 
68 /*
69  *  General purpose special memory pool chunk descriptor.
70  */
71 struct gen_pool_chunk {
72 	struct list_head next_chunk;	/* next chunk in pool */
73 	atomic_t avail;
74 	phys_addr_t phys_addr;		/* physical starting address of memory chunk */
75 	unsigned long start_addr;	/* start address of memory chunk */
76 	unsigned long end_addr;		/* end address of memory chunk (inclusive) */
77 	unsigned long bits[0];		/* bitmap for allocating memory chunk */
78 };
79 
80 /*
81  *  gen_pool data descriptor for gen_pool_first_fit_align.
82  */
83 struct genpool_data_align {
84 	int align;		/* alignment by bytes for starting address */
85 };
86 
87 extern struct gen_pool *gen_pool_create(int, int);
88 extern phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long);
89 extern int gen_pool_add_virt(struct gen_pool *, unsigned long, phys_addr_t,
90 			     size_t, int);
91 /**
92  * gen_pool_add - add a new chunk of special memory to the pool
93  * @pool: pool to add new memory chunk to
94  * @addr: starting address of memory chunk to add to pool
95  * @size: size in bytes of the memory chunk to add to pool
96  * @nid: node id of the node the chunk structure and bitmap should be
97  *       allocated on, or -1
98  *
99  * Add a new chunk of special memory to the specified pool.
100  *
101  * Returns 0 on success or a -ve errno on failure.
102  */
103 static inline int gen_pool_add(struct gen_pool *pool, unsigned long addr,
104 			       size_t size, int nid)
105 {
106 	return gen_pool_add_virt(pool, addr, -1, size, nid);
107 }
108 extern void gen_pool_destroy(struct gen_pool *);
109 extern unsigned long gen_pool_alloc(struct gen_pool *, size_t);
110 extern unsigned long gen_pool_alloc_algo(struct gen_pool *, size_t,
111 		genpool_algo_t algo, void *data);
112 extern void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size,
113 		dma_addr_t *dma);
114 extern void gen_pool_free(struct gen_pool *, unsigned long, size_t);
115 extern void gen_pool_for_each_chunk(struct gen_pool *,
116 	void (*)(struct gen_pool *, struct gen_pool_chunk *, void *), void *);
117 extern size_t gen_pool_avail(struct gen_pool *);
118 extern size_t gen_pool_size(struct gen_pool *);
119 
120 extern void gen_pool_set_algo(struct gen_pool *pool, genpool_algo_t algo,
121 		void *data);
122 
123 extern unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size,
124 		unsigned long start, unsigned int nr, void *data,
125 		struct gen_pool *pool);
126 
127 extern unsigned long gen_pool_first_fit_align(unsigned long *map,
128 		unsigned long size, unsigned long start, unsigned int nr,
129 		void *data, struct gen_pool *pool);
130 
131 
132 extern unsigned long gen_pool_first_fit_order_align(unsigned long *map,
133 		unsigned long size, unsigned long start, unsigned int nr,
134 		void *data, struct gen_pool *pool);
135 
136 extern unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size,
137 		unsigned long start, unsigned int nr, void *data,
138 		struct gen_pool *pool);
139 
140 
141 extern struct gen_pool *devm_gen_pool_create(struct device *dev,
142 		int min_alloc_order, int nid, const char *name);
143 extern struct gen_pool *gen_pool_get(struct device *dev, const char *name);
144 
145 bool addr_in_gen_pool(struct gen_pool *pool, unsigned long start,
146 			size_t size);
147 
148 #ifdef CONFIG_OF
149 extern struct gen_pool *of_gen_pool_get(struct device_node *np,
150 	const char *propname, int index);
151 #else
152 static inline struct gen_pool *of_gen_pool_get(struct device_node *np,
153 	const char *propname, int index)
154 {
155 	return NULL;
156 }
157 #endif
158 #endif /* __GENALLOC_H__ */
159