Lines Matching +full:dma +full:- +full:pool
1 // SPDX-License-Identifier: GPL-2.0-only
5 * kmalloc/kfree interface. Uses for this includes on-device special
16 * available. If new memory is added to the pool a lock has to be
21 * On architectures that don't have NMI-safe cmpxchg implementation,
26 * Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
40 return chunk->end_addr - chunk->start_addr + 1; in chunk_size()
51 return -EBUSY; in set_bits_ll()
66 return -EBUSY; in clear_bits_ll()
74 * bitmap_set_ll - set the specified number of bits at the specified position
79 * Set @nr bits start from @start in @map lock-lessly. Several users
88 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); in bitmap_set_ll()
94 nr -= bits_to_set; in bitmap_set_ll()
109 * bitmap_clear_ll - clear the specified number of bits at the specified position
114 * Clear @nr bits start from @start in @map lock-lessly. Several users
124 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); in bitmap_clear_ll()
130 nr -= bits_to_clear; in bitmap_clear_ll()
145 * gen_pool_create - create a new special memory pool
147 * @nid: node id of the node the pool structure should be allocated on, or -1
149 * Create a new special memory pool that can be used to manage special purpose
154 struct gen_pool *pool; in gen_pool_create() local
156 pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid); in gen_pool_create()
157 if (pool != NULL) { in gen_pool_create()
158 spin_lock_init(&pool->lock); in gen_pool_create()
159 INIT_LIST_HEAD(&pool->chunks); in gen_pool_create()
160 pool->min_alloc_order = min_alloc_order; in gen_pool_create()
161 pool->algo = gen_pool_first_fit; in gen_pool_create()
162 pool->data = NULL; in gen_pool_create()
163 pool->name = NULL; in gen_pool_create()
165 return pool; in gen_pool_create()
170 * gen_pool_add_owner- add a new chunk of special memory to the pool
171 * @pool: pool to add new memory chunk to
172 * @virt: virtual starting address of memory chunk to add to pool
173 * @phys: physical starting address of memory chunk to add to pool
174 * @size: size in bytes of the memory chunk to add to pool
176 * allocated on, or -1
179 * Add a new chunk of special memory to the specified pool.
181 * Returns 0 on success or a -ve errno on failure.
183 int gen_pool_add_owner(struct gen_pool *pool, unsigned long virt, phys_addr_t phys, in gen_pool_add_owner() argument
187 unsigned long nbits = size >> pool->min_alloc_order; in gen_pool_add_owner()
193 return -ENOMEM; in gen_pool_add_owner()
195 chunk->phys_addr = phys; in gen_pool_add_owner()
196 chunk->start_addr = virt; in gen_pool_add_owner()
197 chunk->end_addr = virt + size - 1; in gen_pool_add_owner()
198 chunk->owner = owner; in gen_pool_add_owner()
199 atomic_long_set(&chunk->avail, size); in gen_pool_add_owner()
201 spin_lock(&pool->lock); in gen_pool_add_owner()
202 list_add_rcu(&chunk->next_chunk, &pool->chunks); in gen_pool_add_owner()
203 spin_unlock(&pool->lock); in gen_pool_add_owner()
210 * gen_pool_virt_to_phys - return the physical address of memory
211 * @pool: pool to allocate from
214 * Returns the physical address on success, or -1 on error.
216 phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long addr) in gen_pool_virt_to_phys() argument
219 phys_addr_t paddr = -1; in gen_pool_virt_to_phys()
222 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) { in gen_pool_virt_to_phys()
223 if (addr >= chunk->start_addr && addr <= chunk->end_addr) { in gen_pool_virt_to_phys()
224 paddr = chunk->phys_addr + (addr - chunk->start_addr); in gen_pool_virt_to_phys()
235 * gen_pool_destroy - destroy a special memory pool
236 * @pool: pool to destroy
238 * Destroy the specified special memory pool. Verifies that there are no
241 void gen_pool_destroy(struct gen_pool *pool) in gen_pool_destroy() argument
245 int order = pool->min_alloc_order; in gen_pool_destroy()
248 list_for_each_safe(_chunk, _next_chunk, &pool->chunks) { in gen_pool_destroy()
250 list_del(&chunk->next_chunk); in gen_pool_destroy()
253 bit = find_next_bit(chunk->bits, end_bit, 0); in gen_pool_destroy()
258 kfree_const(pool->name); in gen_pool_destroy()
259 kfree(pool); in gen_pool_destroy()
264 * gen_pool_alloc_algo_owner - allocate special memory from the pool
265 * @pool: pool to allocate from
266 * @size: number of bytes to allocate from the pool
271 * Allocate the requested number of bytes from the specified pool.
272 * Uses the pool allocation function (with first-fit algorithm by default).
274 * NMI-safe cmpxchg implementation.
276 unsigned long gen_pool_alloc_algo_owner(struct gen_pool *pool, size_t size, in gen_pool_alloc_algo_owner() argument
281 int order = pool->min_alloc_order; in gen_pool_alloc_algo_owner()
294 nbits = (size + (1UL << order) - 1) >> order; in gen_pool_alloc_algo_owner()
296 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) { in gen_pool_alloc_algo_owner()
297 if (size > atomic_long_read(&chunk->avail)) in gen_pool_alloc_algo_owner()
303 start_bit = algo(chunk->bits, end_bit, start_bit, in gen_pool_alloc_algo_owner()
304 nbits, data, pool, chunk->start_addr); in gen_pool_alloc_algo_owner()
307 remain = bitmap_set_ll(chunk->bits, start_bit, nbits); in gen_pool_alloc_algo_owner()
309 remain = bitmap_clear_ll(chunk->bits, start_bit, in gen_pool_alloc_algo_owner()
310 nbits - remain); in gen_pool_alloc_algo_owner()
315 addr = chunk->start_addr + ((unsigned long)start_bit << order); in gen_pool_alloc_algo_owner()
317 atomic_long_sub(size, &chunk->avail); in gen_pool_alloc_algo_owner()
319 *owner = chunk->owner; in gen_pool_alloc_algo_owner()
328 * gen_pool_dma_alloc - allocate special memory from the pool for DMA usage
329 * @pool: pool to allocate from
330 * @size: number of bytes to allocate from the pool
331 * @dma: dma-view physical address return value. Use %NULL if unneeded.
333 * Allocate the requested number of bytes from the specified pool.
334 * Uses the pool allocation function (with first-fit algorithm by default).
336 * NMI-safe cmpxchg implementation.
340 void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size, dma_addr_t *dma) in gen_pool_dma_alloc() argument
342 return gen_pool_dma_alloc_algo(pool, size, dma, pool->algo, pool->data); in gen_pool_dma_alloc()
347 * gen_pool_dma_alloc_algo - allocate special memory from the pool for DMA
348 * usage with the given pool algorithm
349 * @pool: pool to allocate from
350 * @size: number of bytes to allocate from the pool
351 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
355 * Allocate the requested number of bytes from the specified pool. Uses the
356 * given pool allocation function. Can not be used in NMI handler on
357 * architectures without NMI-safe cmpxchg implementation.
361 void *gen_pool_dma_alloc_algo(struct gen_pool *pool, size_t size, in gen_pool_dma_alloc_algo() argument
362 dma_addr_t *dma, genpool_algo_t algo, void *data) in gen_pool_dma_alloc_algo() argument
366 if (!pool) in gen_pool_dma_alloc_algo()
369 vaddr = gen_pool_alloc_algo(pool, size, algo, data); in gen_pool_dma_alloc_algo()
373 if (dma) in gen_pool_dma_alloc_algo()
374 *dma = gen_pool_virt_to_phys(pool, vaddr); in gen_pool_dma_alloc_algo()
381 * gen_pool_dma_alloc_align - allocate special memory from the pool for DMA
383 * @pool: pool to allocate from
384 * @size: number of bytes to allocate from the pool
385 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
388 * Allocate the requested number bytes from the specified pool, with the given
390 * without NMI-safe cmpxchg implementation.
394 void *gen_pool_dma_alloc_align(struct gen_pool *pool, size_t size, in gen_pool_dma_alloc_align() argument
395 dma_addr_t *dma, int align) in gen_pool_dma_alloc_align() argument
399 return gen_pool_dma_alloc_algo(pool, size, dma, in gen_pool_dma_alloc_align()
405 * gen_pool_dma_zalloc - allocate special zeroed memory from the pool for
406 * DMA usage
407 * @pool: pool to allocate from
408 * @size: number of bytes to allocate from the pool
409 * @dma: dma-view physical address return value. Use %NULL if unneeded.
411 * Allocate the requested number of zeroed bytes from the specified pool.
412 * Uses the pool allocation function (with first-fit algorithm by default).
414 * NMI-safe cmpxchg implementation.
418 void *gen_pool_dma_zalloc(struct gen_pool *pool, size_t size, dma_addr_t *dma) in gen_pool_dma_zalloc() argument
420 return gen_pool_dma_zalloc_algo(pool, size, dma, pool->algo, pool->data); in gen_pool_dma_zalloc()
425 * gen_pool_dma_zalloc_algo - allocate special zeroed memory from the pool for
426 * DMA usage with the given pool algorithm
427 * @pool: pool to allocate from
428 * @size: number of bytes to allocate from the pool
429 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
433 * Allocate the requested number of zeroed bytes from the specified pool. Uses
434 * the given pool allocation function. Can not be used in NMI handler on
435 * architectures without NMI-safe cmpxchg implementation.
439 void *gen_pool_dma_zalloc_algo(struct gen_pool *pool, size_t size, in gen_pool_dma_zalloc_algo() argument
440 dma_addr_t *dma, genpool_algo_t algo, void *data) in gen_pool_dma_zalloc_algo() argument
442 void *vaddr = gen_pool_dma_alloc_algo(pool, size, dma, algo, data); in gen_pool_dma_zalloc_algo()
452 * gen_pool_dma_zalloc_align - allocate special zeroed memory from the pool for
453 * DMA usage with the given alignment
454 * @pool: pool to allocate from
455 * @size: number of bytes to allocate from the pool
456 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
459 * Allocate the requested number of zeroed bytes from the specified pool,
461 * architectures without NMI-safe cmpxchg implementation.
465 void *gen_pool_dma_zalloc_align(struct gen_pool *pool, size_t size, in gen_pool_dma_zalloc_align() argument
466 dma_addr_t *dma, int align) in gen_pool_dma_zalloc_align() argument
470 return gen_pool_dma_zalloc_algo(pool, size, dma, in gen_pool_dma_zalloc_align()
476 * gen_pool_free_owner - free allocated special memory back to the pool
477 * @pool: pool to free to
478 * @addr: starting address of memory to free back to pool
483 * pool. Can not be used in NMI handler on architectures without
484 * NMI-safe cmpxchg implementation.
486 void gen_pool_free_owner(struct gen_pool *pool, unsigned long addr, size_t size, in gen_pool_free_owner() argument
490 int order = pool->min_alloc_order; in gen_pool_free_owner()
500 nbits = (size + (1UL << order) - 1) >> order; in gen_pool_free_owner()
502 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) { in gen_pool_free_owner()
503 if (addr >= chunk->start_addr && addr <= chunk->end_addr) { in gen_pool_free_owner()
504 BUG_ON(addr + size - 1 > chunk->end_addr); in gen_pool_free_owner()
505 start_bit = (addr - chunk->start_addr) >> order; in gen_pool_free_owner()
506 remain = bitmap_clear_ll(chunk->bits, start_bit, nbits); in gen_pool_free_owner()
509 atomic_long_add(size, &chunk->avail); in gen_pool_free_owner()
511 *owner = chunk->owner; in gen_pool_free_owner()
522 * gen_pool_for_each_chunk - call func for every chunk of generic memory pool
523 * @pool: the generic memory pool
527 * Call @func for every chunk of generic memory pool. The @func is
530 void gen_pool_for_each_chunk(struct gen_pool *pool, in gen_pool_for_each_chunk() argument
531 void (*func)(struct gen_pool *pool, struct gen_pool_chunk *chunk, void *data), in gen_pool_for_each_chunk() argument
537 list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk) in gen_pool_for_each_chunk()
538 func(pool, chunk, data); in gen_pool_for_each_chunk()
544 * gen_pool_has_addr - checks if an address falls within the range of a pool
545 * @pool: the generic memory pool
549 * Check if the range of addresses falls within the specified pool. Returns
550 * true if the entire range is contained in the pool and false otherwise.
552 bool gen_pool_has_addr(struct gen_pool *pool, unsigned long start, in gen_pool_has_addr() argument
556 unsigned long end = start + size - 1; in gen_pool_has_addr()
560 list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk) { in gen_pool_has_addr()
561 if (start >= chunk->start_addr && start <= chunk->end_addr) { in gen_pool_has_addr()
562 if (end <= chunk->end_addr) { in gen_pool_has_addr()
574 * gen_pool_avail - get available free space of the pool
575 * @pool: pool to get available free space
577 * Return available free space of the specified pool.
579 size_t gen_pool_avail(struct gen_pool *pool) in gen_pool_avail() argument
585 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) in gen_pool_avail()
586 avail += atomic_long_read(&chunk->avail); in gen_pool_avail()
593 * gen_pool_size - get size in bytes of memory managed by the pool
594 * @pool: pool to get size
596 * Return size in bytes of memory managed by the pool.
598 size_t gen_pool_size(struct gen_pool *pool) in gen_pool_size() argument
604 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) in gen_pool_size()
612 * gen_pool_set_algo - set the allocation algorithm
613 * @pool: pool to change allocation algorithm
617 * Call @algo for each memory allocation in the pool.
621 void gen_pool_set_algo(struct gen_pool *pool, genpool_algo_t algo, void *data) in gen_pool_set_algo() argument
625 pool->algo = algo; in gen_pool_set_algo()
626 if (!pool->algo) in gen_pool_set_algo()
627 pool->algo = gen_pool_first_fit; in gen_pool_set_algo()
629 pool->data = data; in gen_pool_set_algo()
636 * gen_pool_first_fit - find the first available region
642 * @data: additional data - unused
643 * @pool: pool to find the fit region memory from
647 struct gen_pool *pool, unsigned long start_addr) in gen_pool_first_fit() argument
654 * gen_pool_first_fit_align - find the first available region
661 * @pool: pool to get order from
665 struct gen_pool *pool, unsigned long start_addr) in gen_pool_first_fit_align() argument
672 order = pool->min_alloc_order; in gen_pool_first_fit_align()
673 align_mask = ((alignment->align + (1UL << order) - 1) >> order) - 1; in gen_pool_first_fit_align()
674 align_off = (start_addr & (alignment->align - 1)) >> order; in gen_pool_first_fit_align()
682 * gen_pool_fixed_alloc - reserve a specific region
688 * @pool: pool to get order from
692 struct gen_pool *pool, unsigned long start_addr) in gen_pool_fixed_alloc() argument
700 order = pool->min_alloc_order; in gen_pool_fixed_alloc()
701 offset_bit = fixed_data->offset >> order; in gen_pool_fixed_alloc()
702 if (WARN_ON(fixed_data->offset & ((1UL << order) - 1))) in gen_pool_fixed_alloc()
714 * gen_pool_first_fit_order_align - find the first available region
721 * @data: additional data - unused
722 * @pool: pool to find the fit region memory from
726 unsigned int nr, void *data, struct gen_pool *pool, in gen_pool_first_fit_order_align() argument
729 unsigned long align_mask = roundup_pow_of_two(nr) - 1; in gen_pool_first_fit_order_align()
736 * gen_pool_best_fit - find the best fitting region of memory
742 * @data: additional data - unused
743 * @pool: pool to find the fit region memory from
750 struct gen_pool *pool, unsigned long start_addr) in gen_pool_best_fit() argument
760 if ((next_bit - index) < len) { in gen_pool_best_fit()
761 len = next_bit - index; in gen_pool_best_fit()
783 /* NULL data matches only a pool without an assigned name */ in devm_gen_pool_match()
784 if (!data && !(*p)->name) in devm_gen_pool_match()
787 if (!data || !(*p)->name) in devm_gen_pool_match()
790 return !strcmp((*p)->name, data); in devm_gen_pool_match()
794 * gen_pool_get - Obtain the gen_pool (if any) for a device
813 * devm_gen_pool_create - managed gen_pool_create
819 * Create a new special memory pool that can be used to manage special purpose
820 * memory not managed by the regular kmalloc/kfree interface. The pool will be
826 struct gen_pool **ptr, *pool; in devm_gen_pool_create() local
831 return ERR_PTR(-EINVAL); in devm_gen_pool_create()
836 return ERR_PTR(-ENOMEM); in devm_gen_pool_create()
843 pool = gen_pool_create(min_alloc_order, nid); in devm_gen_pool_create()
844 if (!pool) in devm_gen_pool_create()
847 *ptr = pool; in devm_gen_pool_create()
848 pool->name = pool_name; in devm_gen_pool_create()
851 return pool; in devm_gen_pool_create()
858 return ERR_PTR(-ENOMEM); in devm_gen_pool_create()
864 * of_gen_pool_get - find a pool by phandle property
869 * Returns the pool that contains the chunk starting at the physical
879 struct gen_pool *pool = NULL; in of_gen_pool_get() local
894 name = np_pool->name; in of_gen_pool_get()
897 pool = gen_pool_get(&pdev->dev, name); in of_gen_pool_get()
900 return pool; in of_gen_pool_get()