• Home
  • Raw
  • Download

Lines Matching +full:dma +full:- +full:safe +full:- +full:map

1 // SPDX-License-Identifier: GPL-2.0-only
5 * kmalloc/kfree interface. Uses for this includes on-device special
8 * It is safe to use the allocator in NMI handlers and other 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
75 * @map: pointer to a bitmap
76 * @start: a bit position in @map
79 * Set @nr bits start from @start in @map lock-lessly. Several users
84 static int bitmap_set_ll(unsigned long *map, unsigned long start, unsigned long nr) in bitmap_set_ll() argument
86 unsigned long *p = map + BIT_WORD(start); in bitmap_set_ll()
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
110 * @map: pointer to a bitmap
111 * @start: a bit position in @map
114 * Clear @nr bits start from @start in @map lock-lessly. Several users
120 bitmap_clear_ll(unsigned long *map, unsigned long start, unsigned long nr) in bitmap_clear_ll() argument
122 unsigned long *p = map + BIT_WORD(start); in bitmap_clear_ll()
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
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
176 * allocated on, or -1
181 * Returns 0 on success or a -ve errno on failure.
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
272 * Uses the pool allocation function (with first-fit algorithm by default).
274 * NMI-safe cmpxchg implementation.
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
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
351 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
357 * architectures without NMI-safe cmpxchg implementation.
362 dma_addr_t *dma, genpool_algo_t algo, void *data) in gen_pool_dma_alloc_algo() argument
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
385 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
390 * without NMI-safe cmpxchg implementation.
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
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
426 * DMA usage with the given pool algorithm
429 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
435 * architectures without NMI-safe cmpxchg implementation.
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
456 * @dma: DMA-view physical address return value. Use %NULL if unneeded.
461 * architectures without NMI-safe cmpxchg implementation.
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
484 * NMI-safe cmpxchg implementation.
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
537 list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk) in gen_pool_for_each_chunk()
544 * gen_pool_has_addr - checks if an address falls within the range of a pool
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
604 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) in gen_pool_size()
612 * gen_pool_set_algo - set the allocation algorithm
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
638 * @map: The address to base the search on
642 * @data: additional data - unused
645 unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size, 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
656 * @map: The address to base the search on
663 unsigned long gen_pool_first_fit_align(unsigned long *map, unsigned long size, 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()
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
683 * @map: The address to base the search on
690 unsigned long gen_pool_fixed_alloc(unsigned long *map, unsigned long size, 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()
705 start_bit = bitmap_find_next_zero_area(map, size, in gen_pool_fixed_alloc()
714 * gen_pool_first_fit_order_align - find the first available region
717 * @map: The address to base the search on
721 * @data: additional data - unused
724 unsigned long gen_pool_first_fit_order_align(unsigned long *map, 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
738 * @map: The address to base the search on
742 * @data: additional data - unused
748 unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size, in gen_pool_best_fit() argument
756 index = bitmap_find_next_zero_area(map, size, start, nr, 0); 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()
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()