Skip to content

Commit 3c8f370

Browse files
plagniojtorvalds
authored andcommitted
lib/genalloc.c: add support for specifying the physical address
So we can specify the virtual address as the base of the pool chunk and then get physical addresses for hardware IP. For example on at91 we will use this on spi, uart or macb Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> Cc: Nicolas Ferre <nicolas.ferre@atmel.com> Cc: Patrice VILCHEZ <patrice.vilchez@atmel.com> Cc: Jes Sorensen <jes@wildopensource.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 6aae6e0 commit 3c8f370

2 files changed

Lines changed: 58 additions & 9 deletions

File tree

‎include/linux/genalloc.h‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,33 @@ struct gen_pool {
2626
struct gen_pool_chunk {
2727
spinlock_t lock;
2828
struct list_head next_chunk; /* next chunk in pool */
29+
phys_addr_t phys_addr; /* physical starting address of memory chunk */
2930
unsigned long start_addr; /* starting address of memory chunk */
3031
unsigned long end_addr; /* ending address of memory chunk */
3132
unsigned long bits[0]; /* bitmap for allocating memory chunk */
3233
};
3334

3435
extern struct gen_pool *gen_pool_create(int, int);
35-
extern int gen_pool_add(struct gen_pool *, unsigned long, size_t, int);
36+
extern phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long);
37+
extern int gen_pool_add_virt(struct gen_pool *, unsigned long, phys_addr_t,
38+
size_t, int);
39+
/**
40+
* gen_pool_add - add a new chunk of special memory to the pool
41+
* @pool: pool to add new memory chunk to
42+
* @addr: starting address of memory chunk to add to pool
43+
* @size: size in bytes of the memory chunk to add to pool
44+
* @nid: node id of the node the chunk structure and bitmap should be
45+
* allocated on, or -1
46+
*
47+
* Add a new chunk of special memory to the specified pool.
48+
*
49+
* Returns 0 on success or a -ve errno on failure.
50+
*/
51+
static inline int gen_pool_add(struct gen_pool *pool, unsigned long addr,
52+
size_t size, int nid)
53+
{
54+
return gen_pool_add_virt(pool, addr, -1, size, nid);
55+
}
3656
extern void gen_pool_destroy(struct gen_pool *);
3757
extern unsigned long gen_pool_alloc(struct gen_pool *, size_t);
3858
extern void gen_pool_free(struct gen_pool *, unsigned long, size_t);

‎lib/genalloc.c‎

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,20 @@ struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
3939
EXPORT_SYMBOL(gen_pool_create);
4040

4141
/**
42-
* gen_pool_add - add a new chunk of special memory to the pool
42+
* gen_pool_add_virt - add a new chunk of special memory to the pool
4343
* @pool: pool to add new memory chunk to
44-
* @addr: starting address of memory chunk to add to pool
44+
* @virt: virtual starting address of memory chunk to add to pool
45+
* @phys: physical starting address of memory chunk to add to pool
4546
* @size: size in bytes of the memory chunk to add to pool
4647
* @nid: node id of the node the chunk structure and bitmap should be
4748
* allocated on, or -1
4849
*
4950
* Add a new chunk of special memory to the specified pool.
51+
*
52+
* Returns 0 on success or a -ve errno on failure.
5053
*/
51-
int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size,
52-
int nid)
54+
int gen_pool_add_virt(struct gen_pool *pool, unsigned long virt, phys_addr_t phys,
55+
size_t size, int nid)
5356
{
5457
struct gen_pool_chunk *chunk;
5558
int nbits = size >> pool->min_alloc_order;
@@ -58,19 +61,45 @@ int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size,
5861

5962
chunk = kmalloc_node(nbytes, GFP_KERNEL | __GFP_ZERO, nid);
6063
if (unlikely(chunk == NULL))
61-
return -1;
64+
return -ENOMEM;
6265

6366
spin_lock_init(&chunk->lock);
64-
chunk->start_addr = addr;
65-
chunk->end_addr = addr + size;
67+
chunk->phys_addr = phys;
68+
chunk->start_addr = virt;
69+
chunk->end_addr = virt + size;
6670

6771
write_lock(&pool->lock);
6872
list_add(&chunk->next_chunk, &pool->chunks);
6973
write_unlock(&pool->lock);
7074

7175
return 0;
7276
}
73-
EXPORT_SYMBOL(gen_pool_add);
77+
EXPORT_SYMBOL(gen_pool_add_virt);
78+
79+
/**
80+
* gen_pool_virt_to_phys - return the physical address of memory
81+
* @pool: pool to allocate from
82+
* @addr: starting address of memory
83+
*
84+
* Returns the physical address on success, or -1 on error.
85+
*/
86+
phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long addr)
87+
{
88+
struct list_head *_chunk;
89+
struct gen_pool_chunk *chunk;
90+
91+
read_lock(&pool->lock);
92+
list_for_each(_chunk, &pool->chunks) {
93+
chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
94+
95+
if (addr >= chunk->start_addr && addr < chunk->end_addr)
96+
return chunk->phys_addr + addr - chunk->start_addr;
97+
}
98+
read_unlock(&pool->lock);
99+
100+
return -1;
101+
}
102+
EXPORT_SYMBOL(gen_pool_virt_to_phys);
74103

75104
/**
76105
* gen_pool_destroy - destroy a special memory pool

0 commit comments

Comments
 (0)