140b0b3f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f14f75b8SJes Sorensen /*
37f184275SHuang Ying * Basic general purpose allocator for managing special purpose
47f184275SHuang Ying * memory, for example, memory that is not managed by the regular
57f184275SHuang Ying * kmalloc/kfree interface. Uses for this includes on-device special
67f184275SHuang Ying * memory, uncached memory etc.
77f184275SHuang Ying *
87f184275SHuang Ying * It is safe to use the allocator in NMI handlers and other special
97f184275SHuang Ying * unblockable contexts that could otherwise deadlock on locks. This
107f184275SHuang Ying * is implemented by using atomic operations and retries on any
117f184275SHuang Ying * conflicts. The disadvantage is that there may be livelocks in
127f184275SHuang Ying * extreme cases. For better scalability, one allocator can be used
137f184275SHuang Ying * for each CPU.
147f184275SHuang Ying *
157f184275SHuang Ying * The lockless operation only works if there is enough memory
167f184275SHuang Ying * available. If new memory is added to the pool a lock has to be
177f184275SHuang Ying * still taken. So any user relying on locklessness has to ensure
187f184275SHuang Ying * that sufficient memory is preallocated.
197f184275SHuang Ying *
207f184275SHuang Ying * The basic atomic operation of this allocator is cmpxchg on long.
217f184275SHuang Ying * On architectures that don't have NMI-safe cmpxchg implementation,
227f184275SHuang Ying * the allocator can NOT be used in NMI handler. So code uses the
237f184275SHuang Ying * allocator in NMI handler should depend on
247f184275SHuang Ying * CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
25f14f75b8SJes Sorensen *
26f14f75b8SJes Sorensen * Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
27f14f75b8SJes Sorensen */
28f14f75b8SJes Sorensen
295a0e3ad6STejun Heo #include <linux/slab.h>
308bc3bcc9SPaul Gortmaker #include <linux/export.h>
31243797f5SAkinobu Mita #include <linux/bitmap.h>
327f184275SHuang Ying #include <linux/rculist.h>
337f184275SHuang Ying #include <linux/interrupt.h>
34f14f75b8SJes Sorensen #include <linux/genalloc.h>
35*077ca040SRob Herring #include <linux/of.h>
36*077ca040SRob Herring #include <linux/of_platform.h>
37*077ca040SRob Herring #include <linux/platform_device.h>
3835004f2eSOlof Johansson #include <linux/vmalloc.h>
39f14f75b8SJes Sorensen
chunk_size(const struct gen_pool_chunk * chunk)40674470d9SJoonyoung Shim static inline size_t chunk_size(const struct gen_pool_chunk *chunk)
41674470d9SJoonyoung Shim {
42674470d9SJoonyoung Shim return chunk->end_addr - chunk->start_addr + 1;
43674470d9SJoonyoung Shim }
44674470d9SJoonyoung Shim
45030c6ff6SUros Bizjak static inline int
set_bits_ll(unsigned long * addr,unsigned long mask_to_set)46030c6ff6SUros Bizjak set_bits_ll(unsigned long *addr, unsigned long mask_to_set)
477f184275SHuang Ying {
48030c6ff6SUros Bizjak unsigned long val = READ_ONCE(*addr);
497f184275SHuang Ying
507f184275SHuang Ying do {
517f184275SHuang Ying if (val & mask_to_set)
527f184275SHuang Ying return -EBUSY;
537f184275SHuang Ying cpu_relax();
54030c6ff6SUros Bizjak } while (!try_cmpxchg(addr, &val, val | mask_to_set));
557f184275SHuang Ying
567f184275SHuang Ying return 0;
577f184275SHuang Ying }
587f184275SHuang Ying
59030c6ff6SUros Bizjak static inline int
clear_bits_ll(unsigned long * addr,unsigned long mask_to_clear)60030c6ff6SUros Bizjak clear_bits_ll(unsigned long *addr, unsigned long mask_to_clear)
617f184275SHuang Ying {
62030c6ff6SUros Bizjak unsigned long val = READ_ONCE(*addr);
637f184275SHuang Ying
647f184275SHuang Ying do {
657f184275SHuang Ying if ((val & mask_to_clear) != mask_to_clear)
667f184275SHuang Ying return -EBUSY;
677f184275SHuang Ying cpu_relax();
68030c6ff6SUros Bizjak } while (!try_cmpxchg(addr, &val, val & ~mask_to_clear));
697f184275SHuang Ying
707f184275SHuang Ying return 0;
717f184275SHuang Ying }
727f184275SHuang Ying
737f184275SHuang Ying /*
747f184275SHuang Ying * bitmap_set_ll - set the specified number of bits at the specified position
757f184275SHuang Ying * @map: pointer to a bitmap
767f184275SHuang Ying * @start: a bit position in @map
777f184275SHuang Ying * @nr: number of bits to set
787f184275SHuang Ying *
797f184275SHuang Ying * Set @nr bits start from @start in @map lock-lessly. Several users
807f184275SHuang Ying * can set/clear the same bitmap simultaneously without lock. If two
817f184275SHuang Ying * users set the same bit, one user will return remain bits, otherwise
827f184275SHuang Ying * return 0.
837f184275SHuang Ying */
840e24465dSHuang Shijie static unsigned long
bitmap_set_ll(unsigned long * map,unsigned long start,unsigned long nr)850e24465dSHuang Shijie bitmap_set_ll(unsigned long *map, unsigned long start, unsigned long nr)
867f184275SHuang Ying {
877f184275SHuang Ying unsigned long *p = map + BIT_WORD(start);
8836845663SHuang Shijie const unsigned long size = start + nr;
897f184275SHuang Ying int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
907f184275SHuang Ying unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
917f184275SHuang Ying
9236845663SHuang Shijie while (nr >= bits_to_set) {
937f184275SHuang Ying if (set_bits_ll(p, mask_to_set))
947f184275SHuang Ying return nr;
957f184275SHuang Ying nr -= bits_to_set;
967f184275SHuang Ying bits_to_set = BITS_PER_LONG;
977f184275SHuang Ying mask_to_set = ~0UL;
987f184275SHuang Ying p++;
997f184275SHuang Ying }
1007f184275SHuang Ying if (nr) {
1017f184275SHuang Ying mask_to_set &= BITMAP_LAST_WORD_MASK(size);
1027f184275SHuang Ying if (set_bits_ll(p, mask_to_set))
1037f184275SHuang Ying return nr;
1047f184275SHuang Ying }
1057f184275SHuang Ying
1067f184275SHuang Ying return 0;
1077f184275SHuang Ying }
1087f184275SHuang Ying
1097f184275SHuang Ying /*
1107f184275SHuang Ying * bitmap_clear_ll - clear the specified number of bits at the specified position
1117f184275SHuang Ying * @map: pointer to a bitmap
1127f184275SHuang Ying * @start: a bit position in @map
1137f184275SHuang Ying * @nr: number of bits to set
1147f184275SHuang Ying *
1157f184275SHuang Ying * Clear @nr bits start from @start in @map lock-lessly. Several users
1167f184275SHuang Ying * can set/clear the same bitmap simultaneously without lock. If two
1177f184275SHuang Ying * users clear the same bit, one user will return remain bits,
1187f184275SHuang Ying * otherwise return 0.
1197f184275SHuang Ying */
12036845663SHuang Shijie static unsigned long
bitmap_clear_ll(unsigned long * map,unsigned long start,unsigned long nr)12136845663SHuang Shijie bitmap_clear_ll(unsigned long *map, unsigned long start, unsigned long nr)
1227f184275SHuang Ying {
1237f184275SHuang Ying unsigned long *p = map + BIT_WORD(start);
12436845663SHuang Shijie const unsigned long size = start + nr;
1257f184275SHuang Ying int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
1267f184275SHuang Ying unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
1277f184275SHuang Ying
12836845663SHuang Shijie while (nr >= bits_to_clear) {
1297f184275SHuang Ying if (clear_bits_ll(p, mask_to_clear))
1307f184275SHuang Ying return nr;
1317f184275SHuang Ying nr -= bits_to_clear;
1327f184275SHuang Ying bits_to_clear = BITS_PER_LONG;
1337f184275SHuang Ying mask_to_clear = ~0UL;
1347f184275SHuang Ying p++;
1357f184275SHuang Ying }
1367f184275SHuang Ying if (nr) {
1377f184275SHuang Ying mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
1387f184275SHuang Ying if (clear_bits_ll(p, mask_to_clear))
1397f184275SHuang Ying return nr;
1407f184275SHuang Ying }
1417f184275SHuang Ying
1427f184275SHuang Ying return 0;
1437f184275SHuang Ying }
144f14f75b8SJes Sorensen
145a58cbd7cSDean Nelson /**
146a58cbd7cSDean Nelson * gen_pool_create - create a new special memory pool
147929f9727SDean Nelson * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
148929f9727SDean Nelson * @nid: node id of the node the pool structure should be allocated on, or -1
149a58cbd7cSDean Nelson *
150a58cbd7cSDean Nelson * Create a new special memory pool that can be used to manage special purpose
151a58cbd7cSDean Nelson * memory not managed by the regular kmalloc/kfree interface.
152f14f75b8SJes Sorensen */
gen_pool_create(int min_alloc_order,int nid)153929f9727SDean Nelson struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
154929f9727SDean Nelson {
155929f9727SDean Nelson struct gen_pool *pool;
156f14f75b8SJes Sorensen
157929f9727SDean Nelson pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
158929f9727SDean Nelson if (pool != NULL) {
1597f184275SHuang Ying spin_lock_init(&pool->lock);
160929f9727SDean Nelson INIT_LIST_HEAD(&pool->chunks);
161929f9727SDean Nelson pool->min_alloc_order = min_alloc_order;
162ca279cf1SBenjamin Gaignard pool->algo = gen_pool_first_fit;
163ca279cf1SBenjamin Gaignard pool->data = NULL;
164c98c3635SVladimir Zapolskiy pool->name = NULL;
165f14f75b8SJes Sorensen }
166929f9727SDean Nelson return pool;
167f14f75b8SJes Sorensen }
168f14f75b8SJes Sorensen EXPORT_SYMBOL(gen_pool_create);
169f14f75b8SJes Sorensen
170a58cbd7cSDean Nelson /**
171795ee306SDan Williams * gen_pool_add_owner- add a new chunk of special memory to the pool
172929f9727SDean Nelson * @pool: pool to add new memory chunk to
1733c8f370dSJean-Christophe PLAGNIOL-VILLARD * @virt: virtual starting address of memory chunk to add to pool
1743c8f370dSJean-Christophe PLAGNIOL-VILLARD * @phys: physical starting address of memory chunk to add to pool
175929f9727SDean Nelson * @size: size in bytes of the memory chunk to add to pool
176929f9727SDean Nelson * @nid: node id of the node the chunk structure and bitmap should be
177929f9727SDean Nelson * allocated on, or -1
178795ee306SDan Williams * @owner: private data the publisher would like to recall at alloc time
179a58cbd7cSDean Nelson *
180a58cbd7cSDean Nelson * Add a new chunk of special memory to the specified pool.
1813c8f370dSJean-Christophe PLAGNIOL-VILLARD *
1823c8f370dSJean-Christophe PLAGNIOL-VILLARD * Returns 0 on success or a -ve errno on failure.
183f14f75b8SJes Sorensen */
gen_pool_add_owner(struct gen_pool * pool,unsigned long virt,phys_addr_t phys,size_t size,int nid,void * owner)184795ee306SDan Williams int gen_pool_add_owner(struct gen_pool *pool, unsigned long virt, phys_addr_t phys,
185795ee306SDan Williams size_t size, int nid, void *owner)
186f14f75b8SJes Sorensen {
187929f9727SDean Nelson struct gen_pool_chunk *chunk;
18836845663SHuang Shijie unsigned long nbits = size >> pool->min_alloc_order;
18936845663SHuang Shijie unsigned long nbytes = sizeof(struct gen_pool_chunk) +
190eedce141SThadeu Lima de Souza Cascardo BITS_TO_LONGS(nbits) * sizeof(long);
191f14f75b8SJes Sorensen
1926862d2fcSHuang Shijie chunk = vzalloc_node(nbytes, nid);
193929f9727SDean Nelson if (unlikely(chunk == NULL))
1943c8f370dSJean-Christophe PLAGNIOL-VILLARD return -ENOMEM;
195f14f75b8SJes Sorensen
1963c8f370dSJean-Christophe PLAGNIOL-VILLARD chunk->phys_addr = phys;
1973c8f370dSJean-Christophe PLAGNIOL-VILLARD chunk->start_addr = virt;
198674470d9SJoonyoung Shim chunk->end_addr = virt + size - 1;
199795ee306SDan Williams chunk->owner = owner;
20036a3d1ddSStephen Bates atomic_long_set(&chunk->avail, size);
201929f9727SDean Nelson
2027f184275SHuang Ying spin_lock(&pool->lock);
2037f184275SHuang Ying list_add_rcu(&chunk->next_chunk, &pool->chunks);
2047f184275SHuang Ying spin_unlock(&pool->lock);
205929f9727SDean Nelson
206929f9727SDean Nelson return 0;
207929f9727SDean Nelson }
208795ee306SDan Williams EXPORT_SYMBOL(gen_pool_add_owner);
2093c8f370dSJean-Christophe PLAGNIOL-VILLARD
2103c8f370dSJean-Christophe PLAGNIOL-VILLARD /**
2113c8f370dSJean-Christophe PLAGNIOL-VILLARD * gen_pool_virt_to_phys - return the physical address of memory
2123c8f370dSJean-Christophe PLAGNIOL-VILLARD * @pool: pool to allocate from
2133c8f370dSJean-Christophe PLAGNIOL-VILLARD * @addr: starting address of memory
2143c8f370dSJean-Christophe PLAGNIOL-VILLARD *
2153c8f370dSJean-Christophe PLAGNIOL-VILLARD * Returns the physical address on success, or -1 on error.
2163c8f370dSJean-Christophe PLAGNIOL-VILLARD */
gen_pool_virt_to_phys(struct gen_pool * pool,unsigned long addr)2173c8f370dSJean-Christophe PLAGNIOL-VILLARD phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long addr)
2183c8f370dSJean-Christophe PLAGNIOL-VILLARD {
2193c8f370dSJean-Christophe PLAGNIOL-VILLARD struct gen_pool_chunk *chunk;
2207f184275SHuang Ying phys_addr_t paddr = -1;
2213c8f370dSJean-Christophe PLAGNIOL-VILLARD
2227f184275SHuang Ying rcu_read_lock();
2237f184275SHuang Ying list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
224674470d9SJoonyoung Shim if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
2257f184275SHuang Ying paddr = chunk->phys_addr + (addr - chunk->start_addr);
2267f184275SHuang Ying break;
2273c8f370dSJean-Christophe PLAGNIOL-VILLARD }
2287f184275SHuang Ying }
2297f184275SHuang Ying rcu_read_unlock();
2303c8f370dSJean-Christophe PLAGNIOL-VILLARD
2317f184275SHuang Ying return paddr;
2323c8f370dSJean-Christophe PLAGNIOL-VILLARD }
2333c8f370dSJean-Christophe PLAGNIOL-VILLARD EXPORT_SYMBOL(gen_pool_virt_to_phys);
234929f9727SDean Nelson
235a58cbd7cSDean Nelson /**
236a58cbd7cSDean Nelson * gen_pool_destroy - destroy a special memory pool
237322acc96SSteve Wise * @pool: pool to destroy
238a58cbd7cSDean Nelson *
239a58cbd7cSDean Nelson * Destroy the specified special memory pool. Verifies that there are no
240a58cbd7cSDean Nelson * outstanding allocations.
241322acc96SSteve Wise */
gen_pool_destroy(struct gen_pool * pool)242322acc96SSteve Wise void gen_pool_destroy(struct gen_pool *pool)
243322acc96SSteve Wise {
244322acc96SSteve Wise struct list_head *_chunk, *_next_chunk;
245322acc96SSteve Wise struct gen_pool_chunk *chunk;
246322acc96SSteve Wise int order = pool->min_alloc_order;
24736845663SHuang Shijie unsigned long bit, end_bit;
248322acc96SSteve Wise
249322acc96SSteve Wise list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
250322acc96SSteve Wise chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
251322acc96SSteve Wise list_del(&chunk->next_chunk);
252322acc96SSteve Wise
253674470d9SJoonyoung Shim end_bit = chunk_size(chunk) >> order;
254b5c7e7ecSYury Norov bit = find_first_bit(chunk->bits, end_bit);
255322acc96SSteve Wise BUG_ON(bit < end_bit);
256322acc96SSteve Wise
2576862d2fcSHuang Shijie vfree(chunk);
258322acc96SSteve Wise }
259c98c3635SVladimir Zapolskiy kfree_const(pool->name);
260322acc96SSteve Wise kfree(pool);
261322acc96SSteve Wise }
262322acc96SSteve Wise EXPORT_SYMBOL(gen_pool_destroy);
263322acc96SSteve Wise
264a58cbd7cSDean Nelson /**
265795ee306SDan Williams * gen_pool_alloc_algo_owner - allocate special memory from the pool
266de2dd0ebSZhao Qiang * @pool: pool to allocate from
267de2dd0ebSZhao Qiang * @size: number of bytes to allocate from the pool
268de2dd0ebSZhao Qiang * @algo: algorithm passed from caller
269de2dd0ebSZhao Qiang * @data: data passed to algorithm
270795ee306SDan Williams * @owner: optionally retrieve the chunk owner
271de2dd0ebSZhao Qiang *
272de2dd0ebSZhao Qiang * Allocate the requested number of bytes from the specified pool.
273de2dd0ebSZhao Qiang * Uses the pool allocation function (with first-fit algorithm by default).
274de2dd0ebSZhao Qiang * Can not be used in NMI handler on architectures without
275de2dd0ebSZhao Qiang * NMI-safe cmpxchg implementation.
276de2dd0ebSZhao Qiang */
gen_pool_alloc_algo_owner(struct gen_pool * pool,size_t size,genpool_algo_t algo,void * data,void ** owner)277795ee306SDan Williams unsigned long gen_pool_alloc_algo_owner(struct gen_pool *pool, size_t size,
278795ee306SDan Williams genpool_algo_t algo, void *data, void **owner)
279de2dd0ebSZhao Qiang {
280929f9727SDean Nelson struct gen_pool_chunk *chunk;
2817f184275SHuang Ying unsigned long addr = 0;
282929f9727SDean Nelson int order = pool->min_alloc_order;
28336845663SHuang Shijie unsigned long nbits, start_bit, end_bit, remain;
2847f184275SHuang Ying
2857f184275SHuang Ying #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
2867f184275SHuang Ying BUG_ON(in_nmi());
2877f184275SHuang Ying #endif
288929f9727SDean Nelson
289795ee306SDan Williams if (owner)
290795ee306SDan Williams *owner = NULL;
291795ee306SDan Williams
292929f9727SDean Nelson if (size == 0)
293f14f75b8SJes Sorensen return 0;
294f14f75b8SJes Sorensen
295929f9727SDean Nelson nbits = (size + (1UL << order) - 1) >> order;
2967f184275SHuang Ying rcu_read_lock();
2977f184275SHuang Ying list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
29836a3d1ddSStephen Bates if (size > atomic_long_read(&chunk->avail))
2997f184275SHuang Ying continue;
300929f9727SDean Nelson
30162e931faSDaniel Mentz start_bit = 0;
302674470d9SJoonyoung Shim end_bit = chunk_size(chunk) >> order;
3037f184275SHuang Ying retry:
304de2dd0ebSZhao Qiang start_bit = algo(chunk->bits, end_bit, start_bit,
30552fbf113SAlexey Skidanov nbits, data, pool, chunk->start_addr);
3067f184275SHuang Ying if (start_bit >= end_bit)
307929f9727SDean Nelson continue;
3087f184275SHuang Ying remain = bitmap_set_ll(chunk->bits, start_bit, nbits);
3097f184275SHuang Ying if (remain) {
3107f184275SHuang Ying remain = bitmap_clear_ll(chunk->bits, start_bit,
3117f184275SHuang Ying nbits - remain);
3127f184275SHuang Ying BUG_ON(remain);
3137f184275SHuang Ying goto retry;
314f14f75b8SJes Sorensen }
315929f9727SDean Nelson
316243797f5SAkinobu Mita addr = chunk->start_addr + ((unsigned long)start_bit << order);
3177f184275SHuang Ying size = nbits << order;
31836a3d1ddSStephen Bates atomic_long_sub(size, &chunk->avail);
319795ee306SDan Williams if (owner)
320795ee306SDan Williams *owner = chunk->owner;
3217f184275SHuang Ying break;
322f14f75b8SJes Sorensen }
3237f184275SHuang Ying rcu_read_unlock();
3247f184275SHuang Ying return addr;
325f14f75b8SJes Sorensen }
326795ee306SDan Williams EXPORT_SYMBOL(gen_pool_alloc_algo_owner);
327f14f75b8SJes Sorensen
328a58cbd7cSDean Nelson /**
329684f0d3dSNicolin Chen * gen_pool_dma_alloc - allocate special memory from the pool for DMA usage
330684f0d3dSNicolin Chen * @pool: pool to allocate from
331684f0d3dSNicolin Chen * @size: number of bytes to allocate from the pool
332da83a722SFredrik Noring * @dma: dma-view physical address return value. Use %NULL if unneeded.
333684f0d3dSNicolin Chen *
334684f0d3dSNicolin Chen * Allocate the requested number of bytes from the specified pool.
335684f0d3dSNicolin Chen * Uses the pool allocation function (with first-fit algorithm by default).
336684f0d3dSNicolin Chen * Can not be used in NMI handler on architectures without
337684f0d3dSNicolin Chen * NMI-safe cmpxchg implementation.
338da83a722SFredrik Noring *
339da83a722SFredrik Noring * Return: virtual address of the allocated memory, or %NULL on failure
340684f0d3dSNicolin Chen */
gen_pool_dma_alloc(struct gen_pool * pool,size_t size,dma_addr_t * dma)341684f0d3dSNicolin Chen void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
342684f0d3dSNicolin Chen {
343cf394fc5SFredrik Noring return gen_pool_dma_alloc_algo(pool, size, dma, pool->algo, pool->data);
344cf394fc5SFredrik Noring }
345cf394fc5SFredrik Noring EXPORT_SYMBOL(gen_pool_dma_alloc);
346cf394fc5SFredrik Noring
347cf394fc5SFredrik Noring /**
348cf394fc5SFredrik Noring * gen_pool_dma_alloc_algo - allocate special memory from the pool for DMA
349cf394fc5SFredrik Noring * usage with the given pool algorithm
350cf394fc5SFredrik Noring * @pool: pool to allocate from
351cf394fc5SFredrik Noring * @size: number of bytes to allocate from the pool
352cf394fc5SFredrik Noring * @dma: DMA-view physical address return value. Use %NULL if unneeded.
353cf394fc5SFredrik Noring * @algo: algorithm passed from caller
354cf394fc5SFredrik Noring * @data: data passed to algorithm
355cf394fc5SFredrik Noring *
356cf394fc5SFredrik Noring * Allocate the requested number of bytes from the specified pool. Uses the
357cf394fc5SFredrik Noring * given pool allocation function. Can not be used in NMI handler on
358cf394fc5SFredrik Noring * architectures without NMI-safe cmpxchg implementation.
359cf394fc5SFredrik Noring *
360cf394fc5SFredrik Noring * Return: virtual address of the allocated memory, or %NULL on failure
361cf394fc5SFredrik Noring */
gen_pool_dma_alloc_algo(struct gen_pool * pool,size_t size,dma_addr_t * dma,genpool_algo_t algo,void * data)362cf394fc5SFredrik Noring void *gen_pool_dma_alloc_algo(struct gen_pool *pool, size_t size,
363cf394fc5SFredrik Noring dma_addr_t *dma, genpool_algo_t algo, void *data)
364cf394fc5SFredrik Noring {
365684f0d3dSNicolin Chen unsigned long vaddr;
366684f0d3dSNicolin Chen
367684f0d3dSNicolin Chen if (!pool)
368684f0d3dSNicolin Chen return NULL;
369684f0d3dSNicolin Chen
370cf394fc5SFredrik Noring vaddr = gen_pool_alloc_algo(pool, size, algo, data);
371684f0d3dSNicolin Chen if (!vaddr)
372684f0d3dSNicolin Chen return NULL;
373684f0d3dSNicolin Chen
3740368dfd0SLad, Prabhakar if (dma)
375684f0d3dSNicolin Chen *dma = gen_pool_virt_to_phys(pool, vaddr);
376684f0d3dSNicolin Chen
377684f0d3dSNicolin Chen return (void *)vaddr;
378684f0d3dSNicolin Chen }
379cf394fc5SFredrik Noring EXPORT_SYMBOL(gen_pool_dma_alloc_algo);
380cf394fc5SFredrik Noring
381cf394fc5SFredrik Noring /**
382cf394fc5SFredrik Noring * gen_pool_dma_alloc_align - allocate special memory from the pool for DMA
383cf394fc5SFredrik Noring * usage with the given alignment
384cf394fc5SFredrik Noring * @pool: pool to allocate from
385cf394fc5SFredrik Noring * @size: number of bytes to allocate from the pool
386cf394fc5SFredrik Noring * @dma: DMA-view physical address return value. Use %NULL if unneeded.
387cf394fc5SFredrik Noring * @align: alignment in bytes for starting address
388cf394fc5SFredrik Noring *
389cf394fc5SFredrik Noring * Allocate the requested number bytes from the specified pool, with the given
390cf394fc5SFredrik Noring * alignment restriction. Can not be used in NMI handler on architectures
391cf394fc5SFredrik Noring * without NMI-safe cmpxchg implementation.
392cf394fc5SFredrik Noring *
393cf394fc5SFredrik Noring * Return: virtual address of the allocated memory, or %NULL on failure
394cf394fc5SFredrik Noring */
gen_pool_dma_alloc_align(struct gen_pool * pool,size_t size,dma_addr_t * dma,int align)395cf394fc5SFredrik Noring void *gen_pool_dma_alloc_align(struct gen_pool *pool, size_t size,
396cf394fc5SFredrik Noring dma_addr_t *dma, int align)
397cf394fc5SFredrik Noring {
398cf394fc5SFredrik Noring struct genpool_data_align data = { .align = align };
399cf394fc5SFredrik Noring
400cf394fc5SFredrik Noring return gen_pool_dma_alloc_algo(pool, size, dma,
401cf394fc5SFredrik Noring gen_pool_first_fit_align, &data);
402cf394fc5SFredrik Noring }
403cf394fc5SFredrik Noring EXPORT_SYMBOL(gen_pool_dma_alloc_align);
404684f0d3dSNicolin Chen
405684f0d3dSNicolin Chen /**
406da83a722SFredrik Noring * gen_pool_dma_zalloc - allocate special zeroed memory from the pool for
407da83a722SFredrik Noring * DMA usage
408da83a722SFredrik Noring * @pool: pool to allocate from
409da83a722SFredrik Noring * @size: number of bytes to allocate from the pool
410da83a722SFredrik Noring * @dma: dma-view physical address return value. Use %NULL if unneeded.
411da83a722SFredrik Noring *
412da83a722SFredrik Noring * Allocate the requested number of zeroed bytes from the specified pool.
413da83a722SFredrik Noring * Uses the pool allocation function (with first-fit algorithm by default).
414da83a722SFredrik Noring * Can not be used in NMI handler on architectures without
415da83a722SFredrik Noring * NMI-safe cmpxchg implementation.
416da83a722SFredrik Noring *
417da83a722SFredrik Noring * Return: virtual address of the allocated zeroed memory, or %NULL on failure
418da83a722SFredrik Noring */
gen_pool_dma_zalloc(struct gen_pool * pool,size_t size,dma_addr_t * dma)419da83a722SFredrik Noring void *gen_pool_dma_zalloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
420da83a722SFredrik Noring {
421cf394fc5SFredrik Noring return gen_pool_dma_zalloc_algo(pool, size, dma, pool->algo, pool->data);
422cf394fc5SFredrik Noring }
423cf394fc5SFredrik Noring EXPORT_SYMBOL(gen_pool_dma_zalloc);
424cf394fc5SFredrik Noring
425cf394fc5SFredrik Noring /**
426cf394fc5SFredrik Noring * gen_pool_dma_zalloc_algo - allocate special zeroed memory from the pool for
427cf394fc5SFredrik Noring * DMA usage with the given pool algorithm
428cf394fc5SFredrik Noring * @pool: pool to allocate from
429cf394fc5SFredrik Noring * @size: number of bytes to allocate from the pool
430cf394fc5SFredrik Noring * @dma: DMA-view physical address return value. Use %NULL if unneeded.
431cf394fc5SFredrik Noring * @algo: algorithm passed from caller
432cf394fc5SFredrik Noring * @data: data passed to algorithm
433cf394fc5SFredrik Noring *
434cf394fc5SFredrik Noring * Allocate the requested number of zeroed bytes from the specified pool. Uses
435cf394fc5SFredrik Noring * the given pool allocation function. Can not be used in NMI handler on
436cf394fc5SFredrik Noring * architectures without NMI-safe cmpxchg implementation.
437cf394fc5SFredrik Noring *
438cf394fc5SFredrik Noring * Return: virtual address of the allocated zeroed memory, or %NULL on failure
439cf394fc5SFredrik Noring */
gen_pool_dma_zalloc_algo(struct gen_pool * pool,size_t size,dma_addr_t * dma,genpool_algo_t algo,void * data)440cf394fc5SFredrik Noring void *gen_pool_dma_zalloc_algo(struct gen_pool *pool, size_t size,
441cf394fc5SFredrik Noring dma_addr_t *dma, genpool_algo_t algo, void *data)
442cf394fc5SFredrik Noring {
443cf394fc5SFredrik Noring void *vaddr = gen_pool_dma_alloc_algo(pool, size, dma, algo, data);
444da83a722SFredrik Noring
445da83a722SFredrik Noring if (vaddr)
446da83a722SFredrik Noring memset(vaddr, 0, size);
447da83a722SFredrik Noring
448da83a722SFredrik Noring return vaddr;
449da83a722SFredrik Noring }
450cf394fc5SFredrik Noring EXPORT_SYMBOL(gen_pool_dma_zalloc_algo);
451cf394fc5SFredrik Noring
452cf394fc5SFredrik Noring /**
453cf394fc5SFredrik Noring * gen_pool_dma_zalloc_align - allocate special zeroed memory from the pool for
454cf394fc5SFredrik Noring * DMA usage with the given alignment
455cf394fc5SFredrik Noring * @pool: pool to allocate from
456cf394fc5SFredrik Noring * @size: number of bytes to allocate from the pool
457cf394fc5SFredrik Noring * @dma: DMA-view physical address return value. Use %NULL if unneeded.
458cf394fc5SFredrik Noring * @align: alignment in bytes for starting address
459cf394fc5SFredrik Noring *
460cf394fc5SFredrik Noring * Allocate the requested number of zeroed bytes from the specified pool,
461cf394fc5SFredrik Noring * with the given alignment restriction. Can not be used in NMI handler on
462cf394fc5SFredrik Noring * architectures without NMI-safe cmpxchg implementation.
463cf394fc5SFredrik Noring *
464cf394fc5SFredrik Noring * Return: virtual address of the allocated zeroed memory, or %NULL on failure
465cf394fc5SFredrik Noring */
gen_pool_dma_zalloc_align(struct gen_pool * pool,size_t size,dma_addr_t * dma,int align)466cf394fc5SFredrik Noring void *gen_pool_dma_zalloc_align(struct gen_pool *pool, size_t size,
467cf394fc5SFredrik Noring dma_addr_t *dma, int align)
468cf394fc5SFredrik Noring {
469cf394fc5SFredrik Noring struct genpool_data_align data = { .align = align };
470cf394fc5SFredrik Noring
471cf394fc5SFredrik Noring return gen_pool_dma_zalloc_algo(pool, size, dma,
472cf394fc5SFredrik Noring gen_pool_first_fit_align, &data);
473cf394fc5SFredrik Noring }
474cf394fc5SFredrik Noring EXPORT_SYMBOL(gen_pool_dma_zalloc_align);
4757f184275SHuang Ying
4767f184275SHuang Ying /**
477ea83df73SJonathan Corbet * gen_pool_free_owner - free allocated special memory back to the pool
478a58cbd7cSDean Nelson * @pool: pool to free to
479929f9727SDean Nelson * @addr: starting address of memory to free back to pool
480929f9727SDean Nelson * @size: size in bytes of memory to free
481795ee306SDan Williams * @owner: private data stashed at gen_pool_add() time
482929f9727SDean Nelson *
483a58cbd7cSDean Nelson * Free previously allocated special memory back to the specified
484a58cbd7cSDean Nelson * pool. Can not be used in NMI handler on architectures without
485f14f75b8SJes Sorensen * NMI-safe cmpxchg implementation.
486929f9727SDean Nelson */
gen_pool_free_owner(struct gen_pool * pool,unsigned long addr,size_t size,void ** owner)487795ee306SDan Williams void gen_pool_free_owner(struct gen_pool *pool, unsigned long addr, size_t size,
488795ee306SDan Williams void **owner)
489929f9727SDean Nelson {
490929f9727SDean Nelson struct gen_pool_chunk *chunk;
491929f9727SDean Nelson int order = pool->min_alloc_order;
49236845663SHuang Shijie unsigned long start_bit, nbits, remain;
493929f9727SDean Nelson
494f14f75b8SJes Sorensen #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
495929f9727SDean Nelson BUG_ON(in_nmi());
496f14f75b8SJes Sorensen #endif
497929f9727SDean Nelson
498795ee306SDan Williams if (owner)
499795ee306SDan Williams *owner = NULL;
500795ee306SDan Williams
501929f9727SDean Nelson nbits = (size + (1UL << order) - 1) >> order;
502929f9727SDean Nelson rcu_read_lock();
503f14f75b8SJes Sorensen list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
504929f9727SDean Nelson if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
505929f9727SDean Nelson BUG_ON(addr + size - 1 > chunk->end_addr);
5067f184275SHuang Ying start_bit = (addr - chunk->start_addr) >> order;
5077f184275SHuang Ying remain = bitmap_clear_ll(chunk->bits, start_bit, nbits);
5087f184275SHuang Ying BUG_ON(remain);
5097f184275SHuang Ying size = nbits << order;
51036a3d1ddSStephen Bates atomic_long_add(size, &chunk->avail);
511795ee306SDan Williams if (owner)
512795ee306SDan Williams *owner = chunk->owner;
5137f184275SHuang Ying rcu_read_unlock();
5147f184275SHuang Ying return;
515f14f75b8SJes Sorensen }
516f14f75b8SJes Sorensen }
5177f184275SHuang Ying rcu_read_unlock();
5187f184275SHuang Ying BUG();
519f14f75b8SJes Sorensen }
520795ee306SDan Williams EXPORT_SYMBOL(gen_pool_free_owner);
5217f184275SHuang Ying
5227f184275SHuang Ying /**
5237f184275SHuang Ying * gen_pool_for_each_chunk - call func for every chunk of generic memory pool
5247f184275SHuang Ying * @pool: the generic memory pool
5257f184275SHuang Ying * @func: func to call
5267f184275SHuang Ying * @data: additional data used by @func
5277f184275SHuang Ying *
5287f184275SHuang Ying * Call @func for every chunk of generic memory pool. The @func is
5297f184275SHuang Ying * called with rcu_read_lock held.
5307f184275SHuang Ying */
gen_pool_for_each_chunk(struct gen_pool * pool,void (* func)(struct gen_pool * pool,struct gen_pool_chunk * chunk,void * data),void * data)5317f184275SHuang Ying void gen_pool_for_each_chunk(struct gen_pool *pool,
5327f184275SHuang Ying void (*func)(struct gen_pool *pool, struct gen_pool_chunk *chunk, void *data),
5337f184275SHuang Ying void *data)
5347f184275SHuang Ying {
5357f184275SHuang Ying struct gen_pool_chunk *chunk;
5367f184275SHuang Ying
5377f184275SHuang Ying rcu_read_lock();
5387f184275SHuang Ying list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk)
5397f184275SHuang Ying func(pool, chunk, data);
5407f184275SHuang Ying rcu_read_unlock();
5417f184275SHuang Ying }
5427f184275SHuang Ying EXPORT_SYMBOL(gen_pool_for_each_chunk);
5437f184275SHuang Ying
5447f184275SHuang Ying /**
545964975acSHuang Shijie * gen_pool_has_addr - checks if an address falls within the range of a pool
5469efb3a42SLaura Abbott * @pool: the generic memory pool
5479efb3a42SLaura Abbott * @start: start address
5489efb3a42SLaura Abbott * @size: size of the region
5499efb3a42SLaura Abbott *
5509efb3a42SLaura Abbott * Check if the range of addresses falls within the specified pool. Returns
5519efb3a42SLaura Abbott * true if the entire range is contained in the pool and false otherwise.
5529efb3a42SLaura Abbott */
gen_pool_has_addr(struct gen_pool * pool,unsigned long start,size_t size)553964975acSHuang Shijie bool gen_pool_has_addr(struct gen_pool *pool, unsigned long start,
5549efb3a42SLaura Abbott size_t size)
5559efb3a42SLaura Abbott {
5569efb3a42SLaura Abbott bool found = false;
557ad3d5d2fSToshi Kikuchi unsigned long end = start + size - 1;
5589efb3a42SLaura Abbott struct gen_pool_chunk *chunk;
5599efb3a42SLaura Abbott
5609efb3a42SLaura Abbott rcu_read_lock();
5619efb3a42SLaura Abbott list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk) {
5629efb3a42SLaura Abbott if (start >= chunk->start_addr && start <= chunk->end_addr) {
5639efb3a42SLaura Abbott if (end <= chunk->end_addr) {
5649efb3a42SLaura Abbott found = true;
5659efb3a42SLaura Abbott break;
5669efb3a42SLaura Abbott }
5679efb3a42SLaura Abbott }
5689efb3a42SLaura Abbott }
5699efb3a42SLaura Abbott rcu_read_unlock();
5709efb3a42SLaura Abbott return found;
5719efb3a42SLaura Abbott }
572964975acSHuang Shijie EXPORT_SYMBOL(gen_pool_has_addr);
5739efb3a42SLaura Abbott
5749efb3a42SLaura Abbott /**
5757f184275SHuang Ying * gen_pool_avail - get available free space of the pool
5767f184275SHuang Ying * @pool: pool to get available free space
5777f184275SHuang Ying *
5787f184275SHuang Ying * Return available free space of the specified pool.
5797f184275SHuang Ying */
gen_pool_avail(struct gen_pool * pool)5807f184275SHuang Ying size_t gen_pool_avail(struct gen_pool *pool)
5817f184275SHuang Ying {
5827f184275SHuang Ying struct gen_pool_chunk *chunk;
5837f184275SHuang Ying size_t avail = 0;
5847f184275SHuang Ying
5857f184275SHuang Ying rcu_read_lock();
5867f184275SHuang Ying list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
58736a3d1ddSStephen Bates avail += atomic_long_read(&chunk->avail);
5887f184275SHuang Ying rcu_read_unlock();
5897f184275SHuang Ying return avail;
5907f184275SHuang Ying }
5917f184275SHuang Ying EXPORT_SYMBOL_GPL(gen_pool_avail);
5927f184275SHuang Ying
5937f184275SHuang Ying /**
5947f184275SHuang Ying * gen_pool_size - get size in bytes of memory managed by the pool
5957f184275SHuang Ying * @pool: pool to get size
5967f184275SHuang Ying *
5977f184275SHuang Ying * Return size in bytes of memory managed by the pool.
5987f184275SHuang Ying */
gen_pool_size(struct gen_pool * pool)5997f184275SHuang Ying size_t gen_pool_size(struct gen_pool *pool)
6007f184275SHuang Ying {
6017f184275SHuang Ying struct gen_pool_chunk *chunk;
6027f184275SHuang Ying size_t size = 0;
6037f184275SHuang Ying
6047f184275SHuang Ying rcu_read_lock();
6057f184275SHuang Ying list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
606674470d9SJoonyoung Shim size += chunk_size(chunk);
6077f184275SHuang Ying rcu_read_unlock();
6087f184275SHuang Ying return size;
6097f184275SHuang Ying }
6107f184275SHuang Ying EXPORT_SYMBOL_GPL(gen_pool_size);
611ca279cf1SBenjamin Gaignard
612ca279cf1SBenjamin Gaignard /**
613ca279cf1SBenjamin Gaignard * gen_pool_set_algo - set the allocation algorithm
614ca279cf1SBenjamin Gaignard * @pool: pool to change allocation algorithm
615ca279cf1SBenjamin Gaignard * @algo: custom algorithm function
616ca279cf1SBenjamin Gaignard * @data: additional data used by @algo
617ca279cf1SBenjamin Gaignard *
618ca279cf1SBenjamin Gaignard * Call @algo for each memory allocation in the pool.
619ca279cf1SBenjamin Gaignard * If @algo is NULL use gen_pool_first_fit as default
620ca279cf1SBenjamin Gaignard * memory allocation function.
621ca279cf1SBenjamin Gaignard */
gen_pool_set_algo(struct gen_pool * pool,genpool_algo_t algo,void * data)622ca279cf1SBenjamin Gaignard void gen_pool_set_algo(struct gen_pool *pool, genpool_algo_t algo, void *data)
623ca279cf1SBenjamin Gaignard {
624ca279cf1SBenjamin Gaignard rcu_read_lock();
625ca279cf1SBenjamin Gaignard
626ca279cf1SBenjamin Gaignard pool->algo = algo;
627ca279cf1SBenjamin Gaignard if (!pool->algo)
628ca279cf1SBenjamin Gaignard pool->algo = gen_pool_first_fit;
629ca279cf1SBenjamin Gaignard
630ca279cf1SBenjamin Gaignard pool->data = data;
631ca279cf1SBenjamin Gaignard
632ca279cf1SBenjamin Gaignard rcu_read_unlock();
633ca279cf1SBenjamin Gaignard }
634ca279cf1SBenjamin Gaignard EXPORT_SYMBOL(gen_pool_set_algo);
635ca279cf1SBenjamin Gaignard
636ca279cf1SBenjamin Gaignard /**
637ca279cf1SBenjamin Gaignard * gen_pool_first_fit - find the first available region
638ca279cf1SBenjamin Gaignard * of memory matching the size requirement (no alignment constraint)
639ca279cf1SBenjamin Gaignard * @map: The address to base the search on
640ca279cf1SBenjamin Gaignard * @size: The bitmap size in bits
641ca279cf1SBenjamin Gaignard * @start: The bitnumber to start searching at
642ca279cf1SBenjamin Gaignard * @nr: The number of zeroed bits we're looking for
643ca279cf1SBenjamin Gaignard * @data: additional data - unused
644de2dd0ebSZhao Qiang * @pool: pool to find the fit region memory from
6459d6ecac0SAlex Shi * @start_addr: not used in this function
646ca279cf1SBenjamin Gaignard */
gen_pool_first_fit(unsigned long * map,unsigned long size,unsigned long start,unsigned int nr,void * data,struct gen_pool * pool,unsigned long start_addr)647ca279cf1SBenjamin Gaignard unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size,
648de2dd0ebSZhao Qiang unsigned long start, unsigned int nr, void *data,
64952fbf113SAlexey Skidanov struct gen_pool *pool, unsigned long start_addr)
650ca279cf1SBenjamin Gaignard {
651ca279cf1SBenjamin Gaignard return bitmap_find_next_zero_area(map, size, start, nr, 0);
652ca279cf1SBenjamin Gaignard }
653ca279cf1SBenjamin Gaignard EXPORT_SYMBOL(gen_pool_first_fit);
654ca279cf1SBenjamin Gaignard
655ca279cf1SBenjamin Gaignard /**
656de2dd0ebSZhao Qiang * gen_pool_first_fit_align - find the first available region
657de2dd0ebSZhao Qiang * of memory matching the size requirement (alignment constraint)
658de2dd0ebSZhao Qiang * @map: The address to base the search on
659de2dd0ebSZhao Qiang * @size: The bitmap size in bits
660de2dd0ebSZhao Qiang * @start: The bitnumber to start searching at
661de2dd0ebSZhao Qiang * @nr: The number of zeroed bits we're looking for
662de2dd0ebSZhao Qiang * @data: data for alignment
663de2dd0ebSZhao Qiang * @pool: pool to get order from
6649d6ecac0SAlex Shi * @start_addr: start addr of alloction chunk
665de2dd0ebSZhao Qiang */
gen_pool_first_fit_align(unsigned long * map,unsigned long size,unsigned long start,unsigned int nr,void * data,struct gen_pool * pool,unsigned long start_addr)666de2dd0ebSZhao Qiang unsigned long gen_pool_first_fit_align(unsigned long *map, unsigned long size,
667de2dd0ebSZhao Qiang unsigned long start, unsigned int nr, void *data,
66852fbf113SAlexey Skidanov struct gen_pool *pool, unsigned long start_addr)
669de2dd0ebSZhao Qiang {
670de2dd0ebSZhao Qiang struct genpool_data_align *alignment;
67152fbf113SAlexey Skidanov unsigned long align_mask, align_off;
672de2dd0ebSZhao Qiang int order;
673de2dd0ebSZhao Qiang
674de2dd0ebSZhao Qiang alignment = data;
675de2dd0ebSZhao Qiang order = pool->min_alloc_order;
676de2dd0ebSZhao Qiang align_mask = ((alignment->align + (1UL << order) - 1) >> order) - 1;
67752fbf113SAlexey Skidanov align_off = (start_addr & (alignment->align - 1)) >> order;
67852fbf113SAlexey Skidanov
67952fbf113SAlexey Skidanov return bitmap_find_next_zero_area_off(map, size, start, nr,
68052fbf113SAlexey Skidanov align_mask, align_off);
681de2dd0ebSZhao Qiang }
682de2dd0ebSZhao Qiang EXPORT_SYMBOL(gen_pool_first_fit_align);
683de2dd0ebSZhao Qiang
684de2dd0ebSZhao Qiang /**
685b26981c8SZhao Qiang * gen_pool_fixed_alloc - reserve a specific region
686b26981c8SZhao Qiang * @map: The address to base the search on
687b26981c8SZhao Qiang * @size: The bitmap size in bits
688b26981c8SZhao Qiang * @start: The bitnumber to start searching at
689b26981c8SZhao Qiang * @nr: The number of zeroed bits we're looking for
690b26981c8SZhao Qiang * @data: data for alignment
691b26981c8SZhao Qiang * @pool: pool to get order from
6929d6ecac0SAlex Shi * @start_addr: not used in this function
693b26981c8SZhao Qiang */
gen_pool_fixed_alloc(unsigned long * map,unsigned long size,unsigned long start,unsigned int nr,void * data,struct gen_pool * pool,unsigned long start_addr)694b26981c8SZhao Qiang unsigned long gen_pool_fixed_alloc(unsigned long *map, unsigned long size,
695b26981c8SZhao Qiang unsigned long start, unsigned int nr, void *data,
69652fbf113SAlexey Skidanov struct gen_pool *pool, unsigned long start_addr)
697b26981c8SZhao Qiang {
698b26981c8SZhao Qiang struct genpool_data_fixed *fixed_data;
699b26981c8SZhao Qiang int order;
700b26981c8SZhao Qiang unsigned long offset_bit;
701b26981c8SZhao Qiang unsigned long start_bit;
702b26981c8SZhao Qiang
703b26981c8SZhao Qiang fixed_data = data;
704b26981c8SZhao Qiang order = pool->min_alloc_order;
705b26981c8SZhao Qiang offset_bit = fixed_data->offset >> order;
7060e6e01ffSZhao Qiang if (WARN_ON(fixed_data->offset & ((1UL << order) - 1)))
707b26981c8SZhao Qiang return size;
708b26981c8SZhao Qiang
709b26981c8SZhao Qiang start_bit = bitmap_find_next_zero_area(map, size,
710b26981c8SZhao Qiang start + offset_bit, nr, 0);
711b26981c8SZhao Qiang if (start_bit != offset_bit)
712b26981c8SZhao Qiang start_bit = size;
713b26981c8SZhao Qiang return start_bit;
714b26981c8SZhao Qiang }
715b26981c8SZhao Qiang EXPORT_SYMBOL(gen_pool_fixed_alloc);
716b26981c8SZhao Qiang
717b26981c8SZhao Qiang /**
718505e3be6SLaura Abbott * gen_pool_first_fit_order_align - find the first available region
719505e3be6SLaura Abbott * of memory matching the size requirement. The region will be aligned
720505e3be6SLaura Abbott * to the order of the size specified.
721505e3be6SLaura Abbott * @map: The address to base the search on
722505e3be6SLaura Abbott * @size: The bitmap size in bits
723505e3be6SLaura Abbott * @start: The bitnumber to start searching at
724505e3be6SLaura Abbott * @nr: The number of zeroed bits we're looking for
725505e3be6SLaura Abbott * @data: additional data - unused
726de2dd0ebSZhao Qiang * @pool: pool to find the fit region memory from
7279d6ecac0SAlex Shi * @start_addr: not used in this function
728505e3be6SLaura Abbott */
gen_pool_first_fit_order_align(unsigned long * map,unsigned long size,unsigned long start,unsigned int nr,void * data,struct gen_pool * pool,unsigned long start_addr)729505e3be6SLaura Abbott unsigned long gen_pool_first_fit_order_align(unsigned long *map,
730505e3be6SLaura Abbott unsigned long size, unsigned long start,
73152fbf113SAlexey Skidanov unsigned int nr, void *data, struct gen_pool *pool,
73252fbf113SAlexey Skidanov unsigned long start_addr)
733505e3be6SLaura Abbott {
734505e3be6SLaura Abbott unsigned long align_mask = roundup_pow_of_two(nr) - 1;
735505e3be6SLaura Abbott
736505e3be6SLaura Abbott return bitmap_find_next_zero_area(map, size, start, nr, align_mask);
737505e3be6SLaura Abbott }
738505e3be6SLaura Abbott EXPORT_SYMBOL(gen_pool_first_fit_order_align);
739505e3be6SLaura Abbott
740505e3be6SLaura Abbott /**
741ca279cf1SBenjamin Gaignard * gen_pool_best_fit - find the best fitting region of memory
742ade29d4fSBhaskar Chowdhury * matching the size requirement (no alignment constraint)
743ca279cf1SBenjamin Gaignard * @map: The address to base the search on
744ca279cf1SBenjamin Gaignard * @size: The bitmap size in bits
745ca279cf1SBenjamin Gaignard * @start: The bitnumber to start searching at
746ca279cf1SBenjamin Gaignard * @nr: The number of zeroed bits we're looking for
747ca279cf1SBenjamin Gaignard * @data: additional data - unused
748de2dd0ebSZhao Qiang * @pool: pool to find the fit region memory from
7499d6ecac0SAlex Shi * @start_addr: not used in this function
750ca279cf1SBenjamin Gaignard *
751ca279cf1SBenjamin Gaignard * Iterate over the bitmap to find the smallest free region
752ca279cf1SBenjamin Gaignard * which we can allocate the memory.
753ca279cf1SBenjamin Gaignard */
gen_pool_best_fit(unsigned long * map,unsigned long size,unsigned long start,unsigned int nr,void * data,struct gen_pool * pool,unsigned long start_addr)754ca279cf1SBenjamin Gaignard unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size,
755de2dd0ebSZhao Qiang unsigned long start, unsigned int nr, void *data,
75652fbf113SAlexey Skidanov struct gen_pool *pool, unsigned long start_addr)
757ca279cf1SBenjamin Gaignard {
758ca279cf1SBenjamin Gaignard unsigned long start_bit = size;
759ca279cf1SBenjamin Gaignard unsigned long len = size + 1;
760ca279cf1SBenjamin Gaignard unsigned long index;
761ca279cf1SBenjamin Gaignard
762ca279cf1SBenjamin Gaignard index = bitmap_find_next_zero_area(map, size, start, nr, 0);
763ca279cf1SBenjamin Gaignard
764ca279cf1SBenjamin Gaignard while (index < size) {
76536845663SHuang Shijie unsigned long next_bit = find_next_bit(map, size, index + nr);
766ca279cf1SBenjamin Gaignard if ((next_bit - index) < len) {
767ca279cf1SBenjamin Gaignard len = next_bit - index;
768ca279cf1SBenjamin Gaignard start_bit = index;
769ca279cf1SBenjamin Gaignard if (len == nr)
770ca279cf1SBenjamin Gaignard return start_bit;
771ca279cf1SBenjamin Gaignard }
772ca279cf1SBenjamin Gaignard index = bitmap_find_next_zero_area(map, size,
773ca279cf1SBenjamin Gaignard next_bit + 1, nr, 0);
774ca279cf1SBenjamin Gaignard }
775ca279cf1SBenjamin Gaignard
776ca279cf1SBenjamin Gaignard return start_bit;
777ca279cf1SBenjamin Gaignard }
778ca279cf1SBenjamin Gaignard EXPORT_SYMBOL(gen_pool_best_fit);
7799375db07SPhilipp Zabel
devm_gen_pool_release(struct device * dev,void * res)7809375db07SPhilipp Zabel static void devm_gen_pool_release(struct device *dev, void *res)
7819375db07SPhilipp Zabel {
7829375db07SPhilipp Zabel gen_pool_destroy(*(struct gen_pool **)res);
7839375db07SPhilipp Zabel }
7849375db07SPhilipp Zabel
devm_gen_pool_match(struct device * dev,void * res,void * data)785c98c3635SVladimir Zapolskiy static int devm_gen_pool_match(struct device *dev, void *res, void *data)
786c98c3635SVladimir Zapolskiy {
787c98c3635SVladimir Zapolskiy struct gen_pool **p = res;
788c98c3635SVladimir Zapolskiy
789c98c3635SVladimir Zapolskiy /* NULL data matches only a pool without an assigned name */
790c98c3635SVladimir Zapolskiy if (!data && !(*p)->name)
791c98c3635SVladimir Zapolskiy return 1;
792c98c3635SVladimir Zapolskiy
793c98c3635SVladimir Zapolskiy if (!data || !(*p)->name)
794c98c3635SVladimir Zapolskiy return 0;
795c98c3635SVladimir Zapolskiy
796c98c3635SVladimir Zapolskiy return !strcmp((*p)->name, data);
797c98c3635SVladimir Zapolskiy }
798c98c3635SVladimir Zapolskiy
7999375db07SPhilipp Zabel /**
80073858173SVladimir Zapolskiy * gen_pool_get - Obtain the gen_pool (if any) for a device
80173858173SVladimir Zapolskiy * @dev: device to retrieve the gen_pool from
80273858173SVladimir Zapolskiy * @name: name of a gen_pool or NULL, identifies a particular gen_pool on device
80373858173SVladimir Zapolskiy *
80473858173SVladimir Zapolskiy * Returns the gen_pool for the device if one is present, or NULL.
80573858173SVladimir Zapolskiy */
gen_pool_get(struct device * dev,const char * name)80673858173SVladimir Zapolskiy struct gen_pool *gen_pool_get(struct device *dev, const char *name)
80773858173SVladimir Zapolskiy {
80873858173SVladimir Zapolskiy struct gen_pool **p;
80973858173SVladimir Zapolskiy
810c98c3635SVladimir Zapolskiy p = devres_find(dev, devm_gen_pool_release, devm_gen_pool_match,
811c98c3635SVladimir Zapolskiy (void *)name);
81273858173SVladimir Zapolskiy if (!p)
81373858173SVladimir Zapolskiy return NULL;
81473858173SVladimir Zapolskiy return *p;
81573858173SVladimir Zapolskiy }
81673858173SVladimir Zapolskiy EXPORT_SYMBOL_GPL(gen_pool_get);
81773858173SVladimir Zapolskiy
81873858173SVladimir Zapolskiy /**
8199375db07SPhilipp Zabel * devm_gen_pool_create - managed gen_pool_create
8209375db07SPhilipp Zabel * @dev: device that provides the gen_pool
8219375db07SPhilipp Zabel * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
82273858173SVladimir Zapolskiy * @nid: node selector for allocated gen_pool, %NUMA_NO_NODE for all nodes
82373858173SVladimir Zapolskiy * @name: name of a gen_pool or NULL, identifies a particular gen_pool on device
8249375db07SPhilipp Zabel *
8259375db07SPhilipp Zabel * Create a new special memory pool that can be used to manage special purpose
8269375db07SPhilipp Zabel * memory not managed by the regular kmalloc/kfree interface. The pool will be
8279375db07SPhilipp Zabel * automatically destroyed by the device management code.
8289375db07SPhilipp Zabel */
devm_gen_pool_create(struct device * dev,int min_alloc_order,int nid,const char * name)8299375db07SPhilipp Zabel struct gen_pool *devm_gen_pool_create(struct device *dev, int min_alloc_order,
83073858173SVladimir Zapolskiy int nid, const char *name)
8319375db07SPhilipp Zabel {
8329375db07SPhilipp Zabel struct gen_pool **ptr, *pool;
833c98c3635SVladimir Zapolskiy const char *pool_name = NULL;
8349375db07SPhilipp Zabel
83573858173SVladimir Zapolskiy /* Check that genpool to be created is uniquely addressed on device */
83673858173SVladimir Zapolskiy if (gen_pool_get(dev, name))
83773858173SVladimir Zapolskiy return ERR_PTR(-EINVAL);
83873858173SVladimir Zapolskiy
839c98c3635SVladimir Zapolskiy if (name) {
840c98c3635SVladimir Zapolskiy pool_name = kstrdup_const(name, GFP_KERNEL);
841c98c3635SVladimir Zapolskiy if (!pool_name)
84273858173SVladimir Zapolskiy return ERR_PTR(-ENOMEM);
8439375db07SPhilipp Zabel }
8449375db07SPhilipp Zabel
845c98c3635SVladimir Zapolskiy ptr = devres_alloc(devm_gen_pool_release, sizeof(*ptr), GFP_KERNEL);
846c98c3635SVladimir Zapolskiy if (!ptr)
847c98c3635SVladimir Zapolskiy goto free_pool_name;
848c98c3635SVladimir Zapolskiy
849c98c3635SVladimir Zapolskiy pool = gen_pool_create(min_alloc_order, nid);
850c98c3635SVladimir Zapolskiy if (!pool)
851c98c3635SVladimir Zapolskiy goto free_devres;
852c98c3635SVladimir Zapolskiy
853c98c3635SVladimir Zapolskiy *ptr = pool;
854c98c3635SVladimir Zapolskiy pool->name = pool_name;
855c98c3635SVladimir Zapolskiy devres_add(dev, ptr);
856c98c3635SVladimir Zapolskiy
8579375db07SPhilipp Zabel return pool;
858c98c3635SVladimir Zapolskiy
859c98c3635SVladimir Zapolskiy free_devres:
860c98c3635SVladimir Zapolskiy devres_free(ptr);
861c98c3635SVladimir Zapolskiy free_pool_name:
862c98c3635SVladimir Zapolskiy kfree_const(pool_name);
863c98c3635SVladimir Zapolskiy
864c98c3635SVladimir Zapolskiy return ERR_PTR(-ENOMEM);
8659375db07SPhilipp Zabel }
866b724aa21SMichal Simek EXPORT_SYMBOL(devm_gen_pool_create);
8679375db07SPhilipp Zabel
8689375db07SPhilipp Zabel #ifdef CONFIG_OF
8699375db07SPhilipp Zabel /**
870abdd4a70SVladimir Zapolskiy * of_gen_pool_get - find a pool by phandle property
8719375db07SPhilipp Zabel * @np: device node
8729375db07SPhilipp Zabel * @propname: property name containing phandle(s)
8739375db07SPhilipp Zabel * @index: index into the phandle array
8749375db07SPhilipp Zabel *
8759375db07SPhilipp Zabel * Returns the pool that contains the chunk starting at the physical
8769375db07SPhilipp Zabel * address of the device tree node pointed at by the phandle property,
8779375db07SPhilipp Zabel * or NULL if not found.
8789375db07SPhilipp Zabel */
of_gen_pool_get(struct device_node * np,const char * propname,int index)879abdd4a70SVladimir Zapolskiy struct gen_pool *of_gen_pool_get(struct device_node *np,
8809375db07SPhilipp Zabel const char *propname, int index)
8819375db07SPhilipp Zabel {
8829375db07SPhilipp Zabel struct platform_device *pdev;
883c98c3635SVladimir Zapolskiy struct device_node *np_pool, *parent;
884c98c3635SVladimir Zapolskiy const char *name = NULL;
885c98c3635SVladimir Zapolskiy struct gen_pool *pool = NULL;
8869375db07SPhilipp Zabel
8879375db07SPhilipp Zabel np_pool = of_parse_phandle(np, propname, index);
8889375db07SPhilipp Zabel if (!np_pool)
8899375db07SPhilipp Zabel return NULL;
890c98c3635SVladimir Zapolskiy
8919375db07SPhilipp Zabel pdev = of_find_device_by_node(np_pool);
892c98c3635SVladimir Zapolskiy if (!pdev) {
893c98c3635SVladimir Zapolskiy /* Check if named gen_pool is created by parent node device */
894c98c3635SVladimir Zapolskiy parent = of_get_parent(np_pool);
895c98c3635SVladimir Zapolskiy pdev = of_find_device_by_node(parent);
896c98c3635SVladimir Zapolskiy of_node_put(parent);
897c98c3635SVladimir Zapolskiy
898c98c3635SVladimir Zapolskiy of_property_read_string(np_pool, "label", &name);
899c98c3635SVladimir Zapolskiy if (!name)
900f8ea9502SLinus Walleij name = of_node_full_name(np_pool);
901c98c3635SVladimir Zapolskiy }
902c98c3635SVladimir Zapolskiy if (pdev)
903c98c3635SVladimir Zapolskiy pool = gen_pool_get(&pdev->dev, name);
9046f3aabd1SVladimir Zapolskiy of_node_put(np_pool);
905c98c3635SVladimir Zapolskiy
906c98c3635SVladimir Zapolskiy return pool;
9079375db07SPhilipp Zabel }
908abdd4a70SVladimir Zapolskiy EXPORT_SYMBOL_GPL(of_gen_pool_get);
9099375db07SPhilipp Zabel #endif /* CONFIG_OF */
910