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>
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
87 const unsigned long size = start + nr; in bitmap_set_ll() local
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()
100 mask_to_set &= BITMAP_LAST_WORD_MASK(size); 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
123 const unsigned long size = start + nr; in bitmap_clear_ll() local
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()
136 mask_to_clear &= BITMAP_LAST_WORD_MASK(size); 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
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()
170 * gen_pool_add_owner- add a new chunk of special memory to the pool
174 * @size: size in bytes of the memory chunk to add to pool
176 * allocated on, or -1
177 * @owner: private data the publisher would like to recall at alloc time
181 * Returns 0 on success or a -ve errno on failure.
184 size_t size, int nid, void *owner) 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
214 * Returns the physical address on success, or -1 on error.
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
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()
264 * gen_pool_alloc_algo_owner - allocate special memory from the pool
266 * @size: number of bytes to allocate from the pool
268 * @data: data passed to algorithm
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
277 genpool_algo_t algo, void *data, void **owner) in gen_pool_alloc_algo_owner() argument
281 int order = pool->min_alloc_order; in gen_pool_alloc_algo_owner()
291 if (size == 0) 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()
316 size = nbits << 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
330 * @size: number of bytes to allocate from the pool
331 * @dma: dma-view physical address return value. Use %NULL if unneeded.
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
350 * @size: number of bytes to allocate from the pool
351 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
353 * @data: data passed to algorithm
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
369 vaddr = gen_pool_alloc_algo(pool, size, algo, data); in gen_pool_dma_alloc_algo()
381 * gen_pool_dma_alloc_align - allocate special memory from the pool for DMA
384 * @size: number of bytes to allocate from the pool
385 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
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
397 struct genpool_data_align data = { .align = align }; in gen_pool_dma_alloc_align() local
399 return gen_pool_dma_alloc_algo(pool, size, dma, in gen_pool_dma_alloc_align()
400 gen_pool_first_fit_align, &data); in gen_pool_dma_alloc_align()
405 * gen_pool_dma_zalloc - allocate special zeroed memory from the pool for
408 * @size: number of bytes to allocate from the pool
409 * @dma: dma-view physical address return value. Use %NULL if unneeded.
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
428 * @size: number of bytes to allocate from the pool
429 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
431 * @data: data passed to algorithm
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()
445 memset(vaddr, 0, size); in gen_pool_dma_zalloc_algo()
452 * gen_pool_dma_zalloc_align - allocate special zeroed memory from the pool for
455 * @size: number of bytes to allocate from the pool
456 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
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
468 struct genpool_data_align data = { .align = align }; in gen_pool_dma_zalloc_align() local
470 return gen_pool_dma_zalloc_algo(pool, size, dma, in gen_pool_dma_zalloc_align()
471 gen_pool_first_fit_align, &data); in gen_pool_dma_zalloc_align()
476 * gen_pool_free_owner - free allocated special memory back to the pool
479 * @size: size in bytes of memory to free
480 * @owner: private data stashed at gen_pool_add() time
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()
508 size = nbits << order; 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
525 * @data: additional data used by @func
531 void (*func)(struct gen_pool *pool, struct gen_pool_chunk *chunk, void *data), in gen_pool_for_each_chunk() argument
532 void *data) in gen_pool_for_each_chunk()
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
547 * @size: size of the region
553 size_t size) 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
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.
601 size_t size = 0; in gen_pool_size() local
604 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) in gen_pool_size()
605 size += chunk_size(chunk); in gen_pool_size()
607 return size; in gen_pool_size()
612 * gen_pool_set_algo - set the allocation algorithm
615 * @data: additional data used by @algo
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
637 * of memory matching the size requirement (no alignment constraint)
639 * @size: The bitmap size in bits
642 * @data: additional data - unused
645 unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size, in gen_pool_first_fit() argument
646 unsigned long start, unsigned int nr, void *data, in gen_pool_first_fit() argument
649 return bitmap_find_next_zero_area(map, size, start, nr, 0); in gen_pool_first_fit()
654 * gen_pool_first_fit_align - find the first available region
655 * of memory matching the size requirement (alignment constraint)
657 * @size: The bitmap size in bits
660 * @data: data for alignment
663 unsigned long gen_pool_first_fit_align(unsigned long *map, unsigned long size, in gen_pool_first_fit_align() argument
664 unsigned long start, unsigned int nr, void *data, in gen_pool_first_fit_align() argument
671 alignment = data; in gen_pool_first_fit_align()
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()
676 return bitmap_find_next_zero_area_off(map, size, start, nr, in gen_pool_first_fit_align()
682 * gen_pool_fixed_alloc - reserve a specific region
684 * @size: The bitmap size in bits
687 * @data: data for alignment
690 unsigned long gen_pool_fixed_alloc(unsigned long *map, unsigned long size, in gen_pool_fixed_alloc() argument
691 unsigned long start, unsigned int nr, void *data, in gen_pool_fixed_alloc() argument
699 fixed_data = data; in gen_pool_fixed_alloc()
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()
703 return size; in gen_pool_fixed_alloc()
705 start_bit = bitmap_find_next_zero_area(map, size, in gen_pool_fixed_alloc()
708 start_bit = size; in gen_pool_fixed_alloc()
714 * gen_pool_first_fit_order_align - find the first available region
715 * of memory matching the size requirement. The region will be aligned
716 * to the order of the size specified.
718 * @size: The bitmap size in bits
721 * @data: additional data - unused
725 unsigned long size, unsigned long start, in gen_pool_first_fit_order_align() argument
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()
731 return bitmap_find_next_zero_area(map, size, start, nr, align_mask); in gen_pool_first_fit_order_align()
736 * gen_pool_best_fit - find the best fitting region of memory
737 * macthing the size requirement (no alignment constraint)
739 * @size: The bitmap size in bits
742 * @data: additional data - unused
748 unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size, in gen_pool_best_fit() argument
749 unsigned long start, unsigned int nr, void *data, in gen_pool_best_fit() argument
752 unsigned long start_bit = size; in gen_pool_best_fit()
753 unsigned long len = size + 1; in gen_pool_best_fit()
756 index = bitmap_find_next_zero_area(map, size, start, nr, 0); in gen_pool_best_fit()
758 while (index < size) { in gen_pool_best_fit()
759 unsigned long next_bit = find_next_bit(map, size, index + nr); in gen_pool_best_fit()
760 if ((next_bit - index) < len) { in gen_pool_best_fit()
761 len = next_bit - index; in gen_pool_best_fit()
766 index = bitmap_find_next_zero_area(map, size, in gen_pool_best_fit()
779 static int devm_gen_pool_match(struct device *dev, void *res, void *data) in devm_gen_pool_match() argument
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
831 return ERR_PTR(-EINVAL); in devm_gen_pool_create()
836 return ERR_PTR(-ENOMEM); in devm_gen_pool_create()
848 pool->name = pool_name; 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
894 name = np_pool->name; in of_gen_pool_get()
897 pool = gen_pool_get(&pdev->dev, name); in of_gen_pool_get()