Skip to content

Commit cf394fc

Browse files
frno7Christoph Hellwig
authored andcommitted
lib/genalloc.c: Add algorithm, align and zeroed family of DMA allocators
Provide the algorithm option to DMA allocators as well, along with convenience variants for zeroed and aligned memory. The following four functions are added: - gen_pool_dma_alloc_algo() - gen_pool_dma_alloc_align() - gen_pool_dma_zalloc_algo() - gen_pool_dma_zalloc_align() Signed-off-by: Fredrik Noring <noring@nocrew.org> Tested-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent b1acd4b commit cf394fc

2 files changed

Lines changed: 105 additions & 5 deletions

File tree

‎include/linux/genalloc.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,15 @@ extern unsigned long gen_pool_alloc_algo(struct gen_pool *, size_t,
121121
genpool_algo_t algo, void *data);
122122
extern void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size,
123123
dma_addr_t *dma);
124-
void *gen_pool_dma_zalloc(struct gen_pool *pool, size_t size, dma_addr_t *dma);
124+
extern void *gen_pool_dma_alloc_algo(struct gen_pool *pool, size_t size,
125+
dma_addr_t *dma, genpool_algo_t algo, void *data);
126+
extern void *gen_pool_dma_alloc_align(struct gen_pool *pool, size_t size,
127+
dma_addr_t *dma, int align);
128+
extern void *gen_pool_dma_zalloc(struct gen_pool *pool, size_t size, dma_addr_t *dma);
129+
extern void *gen_pool_dma_zalloc_algo(struct gen_pool *pool, size_t size,
130+
dma_addr_t *dma, genpool_algo_t algo, void *data);
131+
extern void *gen_pool_dma_zalloc_align(struct gen_pool *pool, size_t size,
132+
dma_addr_t *dma, int align);
125133
extern void gen_pool_free(struct gen_pool *, unsigned long, size_t);
126134
extern void gen_pool_for_each_chunk(struct gen_pool *,
127135
void (*)(struct gen_pool *, struct gen_pool_chunk *, void *), void *);

‎lib/genalloc.c‎

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,35 @@ EXPORT_SYMBOL(gen_pool_alloc_algo);
347347
* Return: virtual address of the allocated memory, or %NULL on failure
348348
*/
349349
void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
350+
{
351+
return gen_pool_dma_alloc_algo(pool, size, dma, pool->algo, pool->data);
352+
}
353+
EXPORT_SYMBOL(gen_pool_dma_alloc);
354+
355+
/**
356+
* gen_pool_dma_alloc_algo - allocate special memory from the pool for DMA
357+
* usage with the given pool algorithm
358+
* @pool: pool to allocate from
359+
* @size: number of bytes to allocate from the pool
360+
* @dma: DMA-view physical address return value. Use %NULL if unneeded.
361+
* @algo: algorithm passed from caller
362+
* @data: data passed to algorithm
363+
*
364+
* Allocate the requested number of bytes from the specified pool. Uses the
365+
* given pool allocation function. Can not be used in NMI handler on
366+
* architectures without NMI-safe cmpxchg implementation.
367+
*
368+
* Return: virtual address of the allocated memory, or %NULL on failure
369+
*/
370+
void *gen_pool_dma_alloc_algo(struct gen_pool *pool, size_t size,
371+
dma_addr_t *dma, genpool_algo_t algo, void *data)
350372
{
351373
unsigned long vaddr;
352374

353375
if (!pool)
354376
return NULL;
355377

356-
vaddr = gen_pool_alloc(pool, size);
378+
vaddr = gen_pool_alloc_algo(pool, size, algo, data);
357379
if (!vaddr)
358380
return NULL;
359381

@@ -362,7 +384,31 @@ void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
362384

363385
return (void *)vaddr;
364386
}
365-
EXPORT_SYMBOL(gen_pool_dma_alloc);
387+
EXPORT_SYMBOL(gen_pool_dma_alloc_algo);
388+
389+
/**
390+
* gen_pool_dma_alloc_align - allocate special memory from the pool for DMA
391+
* usage with the given alignment
392+
* @pool: pool to allocate from
393+
* @size: number of bytes to allocate from the pool
394+
* @dma: DMA-view physical address return value. Use %NULL if unneeded.
395+
* @align: alignment in bytes for starting address
396+
*
397+
* Allocate the requested number bytes from the specified pool, with the given
398+
* alignment restriction. Can not be used in NMI handler on architectures
399+
* without NMI-safe cmpxchg implementation.
400+
*
401+
* Return: virtual address of the allocated memory, or %NULL on failure
402+
*/
403+
void *gen_pool_dma_alloc_align(struct gen_pool *pool, size_t size,
404+
dma_addr_t *dma, int align)
405+
{
406+
struct genpool_data_align data = { .align = align };
407+
408+
return gen_pool_dma_alloc_algo(pool, size, dma,
409+
gen_pool_first_fit_align, &data);
410+
}
411+
EXPORT_SYMBOL(gen_pool_dma_alloc_align);
366412

367413
/**
368414
* gen_pool_dma_zalloc - allocate special zeroed memory from the pool for
@@ -380,14 +426,60 @@ EXPORT_SYMBOL(gen_pool_dma_alloc);
380426
*/
381427
void *gen_pool_dma_zalloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
382428
{
383-
void *vaddr = gen_pool_dma_alloc(pool, size, dma);
429+
return gen_pool_dma_zalloc_algo(pool, size, dma, pool->algo, pool->data);
430+
}
431+
EXPORT_SYMBOL(gen_pool_dma_zalloc);
432+
433+
/**
434+
* gen_pool_dma_zalloc_algo - allocate special zeroed memory from the pool for
435+
* DMA usage with the given pool algorithm
436+
* @pool: pool to allocate from
437+
* @size: number of bytes to allocate from the pool
438+
* @dma: DMA-view physical address return value. Use %NULL if unneeded.
439+
* @algo: algorithm passed from caller
440+
* @data: data passed to algorithm
441+
*
442+
* Allocate the requested number of zeroed bytes from the specified pool. Uses
443+
* the given pool allocation function. Can not be used in NMI handler on
444+
* architectures without NMI-safe cmpxchg implementation.
445+
*
446+
* Return: virtual address of the allocated zeroed memory, or %NULL on failure
447+
*/
448+
void *gen_pool_dma_zalloc_algo(struct gen_pool *pool, size_t size,
449+
dma_addr_t *dma, genpool_algo_t algo, void *data)
450+
{
451+
void *vaddr = gen_pool_dma_alloc_algo(pool, size, dma, algo, data);
384452

385453
if (vaddr)
386454
memset(vaddr, 0, size);
387455

388456
return vaddr;
389457
}
390-
EXPORT_SYMBOL(gen_pool_dma_zalloc);
458+
EXPORT_SYMBOL(gen_pool_dma_zalloc_algo);
459+
460+
/**
461+
* gen_pool_dma_zalloc_align - allocate special zeroed memory from the pool for
462+
* DMA usage with the given alignment
463+
* @pool: pool to allocate from
464+
* @size: number of bytes to allocate from the pool
465+
* @dma: DMA-view physical address return value. Use %NULL if unneeded.
466+
* @align: alignment in bytes for starting address
467+
*
468+
* Allocate the requested number of zeroed bytes from the specified pool,
469+
* with the given alignment restriction. Can not be used in NMI handler on
470+
* architectures without NMI-safe cmpxchg implementation.
471+
*
472+
* Return: virtual address of the allocated zeroed memory, or %NULL on failure
473+
*/
474+
void *gen_pool_dma_zalloc_align(struct gen_pool *pool, size_t size,
475+
dma_addr_t *dma, int align)
476+
{
477+
struct genpool_data_align data = { .align = align };
478+
479+
return gen_pool_dma_zalloc_algo(pool, size, dma,
480+
gen_pool_first_fit_align, &data);
481+
}
482+
EXPORT_SYMBOL(gen_pool_dma_zalloc_align);
391483

392484
/**
393485
* gen_pool_free - free allocated special memory back to the pool

0 commit comments

Comments
 (0)