Lines Matching +full:data +full:- +full:size
1 // SPDX-License-Identifier: GPL-2.0-only
5 * kmalloc/kfree interface. Uses for this includes on-device special
21 * On architectures that don't have NMI-safe cmpxchg implementation,
26 * Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
42 return chunk->end_addr - chunk->start_addr + 1; in chunk_size()
52 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 const unsigned long size = start + nr; in bitmap_set_ll() local
89 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); in bitmap_set_ll()
95 nr -= bits_to_set; in bitmap_set_ll()
101 mask_to_set &= BITMAP_LAST_WORD_MASK(size); in bitmap_set_ll()
110 * bitmap_clear_ll - clear the specified number of bits at the specified position
115 * Clear @nr bits start from @start in @map lock-lessly. Several users
124 const unsigned long size = start + nr; in bitmap_clear_ll() local
125 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); in bitmap_clear_ll()
131 nr -= bits_to_clear; in bitmap_clear_ll()
137 mask_to_clear &= BITMAP_LAST_WORD_MASK(size); in bitmap_clear_ll()
146 * gen_pool_create - create a new special memory pool
148 * @nid: node id of the node the pool structure should be allocated on, or -1
159 spin_lock_init(&pool->lock); in gen_pool_create()
160 INIT_LIST_HEAD(&pool->chunks); in gen_pool_create()
161 pool->min_alloc_order = min_alloc_order; in gen_pool_create()
162 pool->algo = gen_pool_first_fit; in gen_pool_create()
163 pool->data = NULL; in gen_pool_create()
164 pool->name = NULL; in gen_pool_create()
171 * gen_pool_add_owner- add a new chunk of special memory to the pool
175 * @size: size in bytes of the memory chunk to add to pool
177 * allocated on, or -1
178 * @owner: private data the publisher would like to recall at alloc time
182 * Returns 0 on success or a -ve errno on failure.
185 size_t size, int nid, void *owner) in gen_pool_add_owner() argument
188 unsigned long nbits = size >> pool->min_alloc_order; in gen_pool_add_owner()
194 return -ENOMEM; in gen_pool_add_owner()
196 chunk->phys_addr = phys; in gen_pool_add_owner()
197 chunk->start_addr = virt; in gen_pool_add_owner()
198 chunk->end_addr = virt + size - 1; in gen_pool_add_owner()
199 chunk->owner = owner; in gen_pool_add_owner()
200 atomic_long_set(&chunk->avail, size); in gen_pool_add_owner()
202 spin_lock(&pool->lock); in gen_pool_add_owner()
203 list_add_rcu(&chunk->next_chunk, &pool->chunks); in gen_pool_add_owner()
204 spin_unlock(&pool->lock); in gen_pool_add_owner()
211 * gen_pool_virt_to_phys - return the physical address of memory
215 * Returns the physical address on success, or -1 on error.
220 phys_addr_t paddr = -1; in gen_pool_virt_to_phys()
223 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) { in gen_pool_virt_to_phys()
224 if (addr >= chunk->start_addr && addr <= chunk->end_addr) { in gen_pool_virt_to_phys()
225 paddr = chunk->phys_addr + (addr - chunk->start_addr); in gen_pool_virt_to_phys()
236 * gen_pool_destroy - destroy a special memory pool
246 int order = pool->min_alloc_order; in gen_pool_destroy()
249 list_for_each_safe(_chunk, _next_chunk, &pool->chunks) { in gen_pool_destroy()
251 list_del(&chunk->next_chunk); in gen_pool_destroy()
254 bit = find_first_bit(chunk->bits, end_bit); in gen_pool_destroy()
259 kfree_const(pool->name); in gen_pool_destroy()
265 * gen_pool_alloc_algo_owner - allocate special memory from the pool
267 * @size: number of bytes to allocate from the pool
269 * @data: data passed to algorithm
273 * Uses the pool allocation function (with first-fit algorithm by default).
275 * NMI-safe cmpxchg implementation.
277 unsigned long gen_pool_alloc_algo_owner(struct gen_pool *pool, size_t size, in gen_pool_alloc_algo_owner() argument
278 genpool_algo_t algo, void *data, void **owner) in gen_pool_alloc_algo_owner() argument
282 int order = pool->min_alloc_order; in gen_pool_alloc_algo_owner()
292 if (size == 0) in gen_pool_alloc_algo_owner()
295 nbits = (size + (1UL << order) - 1) >> order; in gen_pool_alloc_algo_owner()
297 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) { in gen_pool_alloc_algo_owner()
298 if (size > atomic_long_read(&chunk->avail)) in gen_pool_alloc_algo_owner()
304 start_bit = algo(chunk->bits, end_bit, start_bit, in gen_pool_alloc_algo_owner()
305 nbits, data, pool, chunk->start_addr); in gen_pool_alloc_algo_owner()
308 remain = bitmap_set_ll(chunk->bits, start_bit, nbits); in gen_pool_alloc_algo_owner()
310 remain = bitmap_clear_ll(chunk->bits, start_bit, in gen_pool_alloc_algo_owner()
311 nbits - remain); in gen_pool_alloc_algo_owner()
316 addr = chunk->start_addr + ((unsigned long)start_bit << order); in gen_pool_alloc_algo_owner()
317 size = nbits << order; in gen_pool_alloc_algo_owner()
318 atomic_long_sub(size, &chunk->avail); in gen_pool_alloc_algo_owner()
320 *owner = chunk->owner; in gen_pool_alloc_algo_owner()
329 * gen_pool_dma_alloc - allocate special memory from the pool for DMA usage
331 * @size: number of bytes to allocate from the pool
332 * @dma: dma-view physical address return value. Use %NULL if unneeded.
335 * Uses the pool allocation function (with first-fit algorithm by default).
337 * NMI-safe cmpxchg implementation.
341 void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size, dma_addr_t *dma) in gen_pool_dma_alloc() argument
343 return gen_pool_dma_alloc_algo(pool, size, dma, pool->algo, pool->data); in gen_pool_dma_alloc()
348 * gen_pool_dma_alloc_algo - allocate special memory from the pool for DMA
351 * @size: number of bytes to allocate from the pool
352 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
354 * @data: data passed to algorithm
358 * architectures without NMI-safe cmpxchg implementation.
362 void *gen_pool_dma_alloc_algo(struct gen_pool *pool, size_t size, in gen_pool_dma_alloc_algo() argument
363 dma_addr_t *dma, genpool_algo_t algo, void *data) in gen_pool_dma_alloc_algo() argument
370 vaddr = gen_pool_alloc_algo(pool, size, algo, data); in gen_pool_dma_alloc_algo()
382 * gen_pool_dma_alloc_align - allocate special memory from the pool for DMA
385 * @size: number of bytes to allocate from the pool
386 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
391 * without NMI-safe cmpxchg implementation.
395 void *gen_pool_dma_alloc_align(struct gen_pool *pool, size_t size, in gen_pool_dma_alloc_align() argument
398 struct genpool_data_align data = { .align = align }; in gen_pool_dma_alloc_align() local
400 return gen_pool_dma_alloc_algo(pool, size, dma, in gen_pool_dma_alloc_align()
401 gen_pool_first_fit_align, &data); in gen_pool_dma_alloc_align()
406 * gen_pool_dma_zalloc - allocate special zeroed memory from the pool for
409 * @size: number of bytes to allocate from the pool
410 * @dma: dma-view physical address return value. Use %NULL if unneeded.
413 * Uses the pool allocation function (with first-fit algorithm by default).
415 * NMI-safe cmpxchg implementation.
419 void *gen_pool_dma_zalloc(struct gen_pool *pool, size_t size, dma_addr_t *dma) in gen_pool_dma_zalloc() argument
421 return gen_pool_dma_zalloc_algo(pool, size, dma, pool->algo, pool->data); in gen_pool_dma_zalloc()
426 * gen_pool_dma_zalloc_algo - allocate special zeroed memory from the pool for
429 * @size: number of bytes to allocate from the pool
430 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
432 * @data: data passed to algorithm
436 * architectures without NMI-safe cmpxchg implementation.
440 void *gen_pool_dma_zalloc_algo(struct gen_pool *pool, size_t size, in gen_pool_dma_zalloc_algo() argument
441 dma_addr_t *dma, genpool_algo_t algo, void *data) in gen_pool_dma_zalloc_algo() argument
443 void *vaddr = gen_pool_dma_alloc_algo(pool, size, dma, algo, data); in gen_pool_dma_zalloc_algo()
446 memset(vaddr, 0, size); in gen_pool_dma_zalloc_algo()
453 * gen_pool_dma_zalloc_align - allocate special zeroed memory from the pool for
456 * @size: number of bytes to allocate from the pool
457 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
462 * architectures without NMI-safe cmpxchg implementation.
466 void *gen_pool_dma_zalloc_align(struct gen_pool *pool, size_t size, in gen_pool_dma_zalloc_align() argument
469 struct genpool_data_align data = { .align = align }; in gen_pool_dma_zalloc_align() local
471 return gen_pool_dma_zalloc_algo(pool, size, dma, in gen_pool_dma_zalloc_align()
472 gen_pool_first_fit_align, &data); in gen_pool_dma_zalloc_align()
477 * gen_pool_free_owner - free allocated special memory back to the pool
480 * @size: size in bytes of memory to free
481 * @owner: private data stashed at gen_pool_add() time
485 * NMI-safe cmpxchg implementation.
487 void gen_pool_free_owner(struct gen_pool *pool, unsigned long addr, size_t size, in gen_pool_free_owner() argument
491 int order = pool->min_alloc_order; in gen_pool_free_owner()
501 nbits = (size + (1UL << order) - 1) >> order; in gen_pool_free_owner()
503 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) { in gen_pool_free_owner()
504 if (addr >= chunk->start_addr && addr <= chunk->end_addr) { in gen_pool_free_owner()
505 BUG_ON(addr + size - 1 > chunk->end_addr); in gen_pool_free_owner()
506 start_bit = (addr - chunk->start_addr) >> order; in gen_pool_free_owner()
507 remain = bitmap_clear_ll(chunk->bits, start_bit, nbits); in gen_pool_free_owner()
509 size = nbits << order; in gen_pool_free_owner()
510 atomic_long_add(size, &chunk->avail); in gen_pool_free_owner()
512 *owner = chunk->owner; in gen_pool_free_owner()
523 * gen_pool_for_each_chunk - call func for every chunk of generic memory pool
526 * @data: additional data used by @func
532 void (*func)(struct gen_pool *pool, struct gen_pool_chunk *chunk, void *data), in gen_pool_for_each_chunk() argument
533 void *data) in gen_pool_for_each_chunk()
538 list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk) in gen_pool_for_each_chunk()
539 func(pool, chunk, data); in gen_pool_for_each_chunk()
545 * gen_pool_has_addr - checks if an address falls within the range of a pool
548 * @size: size of the region
554 size_t size) in gen_pool_has_addr() argument
557 unsigned long end = start + size - 1; in gen_pool_has_addr()
561 list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk) { in gen_pool_has_addr()
562 if (start >= chunk->start_addr && start <= chunk->end_addr) { in gen_pool_has_addr()
563 if (end <= chunk->end_addr) { in gen_pool_has_addr()
575 * gen_pool_avail - get available free space of the pool
586 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) in gen_pool_avail()
587 avail += atomic_long_read(&chunk->avail); in gen_pool_avail()
594 * gen_pool_size - get size in bytes of memory managed by the pool
595 * @pool: pool to get size
597 * Return size in bytes of memory managed by the pool.
602 size_t size = 0; in gen_pool_size() local
605 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) in gen_pool_size()
606 size += chunk_size(chunk); in gen_pool_size()
608 return size; in gen_pool_size()
613 * gen_pool_set_algo - set the allocation algorithm
616 * @data: additional data used by @algo
622 void gen_pool_set_algo(struct gen_pool *pool, genpool_algo_t algo, void *data) in gen_pool_set_algo() argument
626 pool->algo = algo; in gen_pool_set_algo()
627 if (!pool->algo) in gen_pool_set_algo()
628 pool->algo = gen_pool_first_fit; in gen_pool_set_algo()
630 pool->data = data; in gen_pool_set_algo()
637 * gen_pool_first_fit - find the first available region
638 * of memory matching the size requirement (no alignment constraint)
640 * @size: The bitmap size in bits
643 * @data: additional data - unused
647 unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size, in gen_pool_first_fit() argument
648 unsigned long start, unsigned int nr, void *data, in gen_pool_first_fit() argument
651 return bitmap_find_next_zero_area(map, size, start, nr, 0); in gen_pool_first_fit()
656 * gen_pool_first_fit_align - find the first available region
657 * of memory matching the size requirement (alignment constraint)
659 * @size: The bitmap size in bits
662 * @data: data for alignment
666 unsigned long gen_pool_first_fit_align(unsigned long *map, unsigned long size, in gen_pool_first_fit_align() argument
667 unsigned long start, unsigned int nr, void *data, in gen_pool_first_fit_align() argument
674 alignment = data; in gen_pool_first_fit_align()
675 order = pool->min_alloc_order; in gen_pool_first_fit_align()
676 align_mask = ((alignment->align + (1UL << order) - 1) >> order) - 1; in gen_pool_first_fit_align()
677 align_off = (start_addr & (alignment->align - 1)) >> order; in gen_pool_first_fit_align()
679 return bitmap_find_next_zero_area_off(map, size, start, nr, in gen_pool_first_fit_align()
685 * gen_pool_fixed_alloc - reserve a specific region
687 * @size: The bitmap size in bits
690 * @data: data for alignment
694 unsigned long gen_pool_fixed_alloc(unsigned long *map, unsigned long size, in gen_pool_fixed_alloc() argument
695 unsigned long start, unsigned int nr, void *data, in gen_pool_fixed_alloc() argument
703 fixed_data = data; in gen_pool_fixed_alloc()
704 order = pool->min_alloc_order; in gen_pool_fixed_alloc()
705 offset_bit = fixed_data->offset >> order; in gen_pool_fixed_alloc()
706 if (WARN_ON(fixed_data->offset & ((1UL << order) - 1))) in gen_pool_fixed_alloc()
707 return size; in gen_pool_fixed_alloc()
709 start_bit = bitmap_find_next_zero_area(map, size, in gen_pool_fixed_alloc()
712 start_bit = size; in gen_pool_fixed_alloc()
718 * gen_pool_first_fit_order_align - find the first available region
719 * of memory matching the size requirement. The region will be aligned
720 * to the order of the size specified.
722 * @size: The bitmap size in bits
725 * @data: additional data - unused
730 unsigned long size, unsigned long start, in gen_pool_first_fit_order_align() argument
731 unsigned int nr, void *data, struct gen_pool *pool, in gen_pool_first_fit_order_align() argument
734 unsigned long align_mask = roundup_pow_of_two(nr) - 1; in gen_pool_first_fit_order_align()
736 return bitmap_find_next_zero_area(map, size, start, nr, align_mask); in gen_pool_first_fit_order_align()
741 * gen_pool_best_fit - find the best fitting region of memory
742 * matching the size requirement (no alignment constraint)
744 * @size: The bitmap size in bits
747 * @data: additional data - unused
754 unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size, in gen_pool_best_fit() argument
755 unsigned long start, unsigned int nr, void *data, in gen_pool_best_fit() argument
758 unsigned long start_bit = size; in gen_pool_best_fit()
759 unsigned long len = size + 1; in gen_pool_best_fit()
762 index = bitmap_find_next_zero_area(map, size, start, nr, 0); in gen_pool_best_fit()
764 while (index < size) { in gen_pool_best_fit()
765 unsigned long next_bit = find_next_bit(map, size, index + nr); in gen_pool_best_fit()
766 if ((next_bit - index) < len) { in gen_pool_best_fit()
767 len = next_bit - index; in gen_pool_best_fit()
772 index = bitmap_find_next_zero_area(map, size, in gen_pool_best_fit()
785 static int devm_gen_pool_match(struct device *dev, void *res, void *data) in devm_gen_pool_match() argument
789 /* NULL data matches only a pool without an assigned name */ in devm_gen_pool_match()
790 if (!data && !(*p)->name) in devm_gen_pool_match()
793 if (!data || !(*p)->name) in devm_gen_pool_match()
796 return !strcmp((*p)->name, data); in devm_gen_pool_match()
800 * gen_pool_get - Obtain the gen_pool (if any) for a device
819 * devm_gen_pool_create - managed gen_pool_create
837 return ERR_PTR(-EINVAL); in devm_gen_pool_create()
842 return ERR_PTR(-ENOMEM); in devm_gen_pool_create()
854 pool->name = pool_name; in devm_gen_pool_create()
864 return ERR_PTR(-ENOMEM); in devm_gen_pool_create()
870 * of_gen_pool_get - find a pool by phandle property
903 pool = gen_pool_get(&pdev->dev, name); in of_gen_pool_get()