提交 3c8f370d 编写于 作者: J Jean-Christophe PLAGNIOL-VILLARD 提交者: Linus Torvalds

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: NJean-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: NAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
上级 6aae6e03
...@@ -26,13 +26,33 @@ struct gen_pool { ...@@ -26,13 +26,33 @@ struct gen_pool {
struct gen_pool_chunk { struct gen_pool_chunk {
spinlock_t lock; spinlock_t lock;
struct list_head next_chunk; /* next chunk in pool */ struct list_head next_chunk; /* next chunk in pool */
phys_addr_t phys_addr; /* physical starting address of memory chunk */
unsigned long start_addr; /* starting address of memory chunk */ unsigned long start_addr; /* starting address of memory chunk */
unsigned long end_addr; /* ending address of memory chunk */ unsigned long end_addr; /* ending address of memory chunk */
unsigned long bits[0]; /* bitmap for allocating memory chunk */ unsigned long bits[0]; /* bitmap for allocating memory chunk */
}; };
extern struct gen_pool *gen_pool_create(int, int); extern struct gen_pool *gen_pool_create(int, int);
extern int gen_pool_add(struct gen_pool *, unsigned long, size_t, int); extern phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long);
extern int gen_pool_add_virt(struct gen_pool *, unsigned long, phys_addr_t,
size_t, int);
/**
* gen_pool_add - add a new chunk of special memory to the pool
* @pool: pool to add new memory chunk to
* @addr: starting address of memory chunk to add to pool
* @size: size in bytes of the memory chunk to add to pool
* @nid: node id of the node the chunk structure and bitmap should be
* allocated on, or -1
*
* Add a new chunk of special memory to the specified pool.
*
* Returns 0 on success or a -ve errno on failure.
*/
static inline int gen_pool_add(struct gen_pool *pool, unsigned long addr,
size_t size, int nid)
{
return gen_pool_add_virt(pool, addr, -1, size, nid);
}
extern void gen_pool_destroy(struct gen_pool *); extern void gen_pool_destroy(struct gen_pool *);
extern unsigned long gen_pool_alloc(struct gen_pool *, size_t); extern unsigned long gen_pool_alloc(struct gen_pool *, size_t);
extern void gen_pool_free(struct gen_pool *, unsigned long, size_t); extern void gen_pool_free(struct gen_pool *, unsigned long, size_t);
......
...@@ -39,17 +39,20 @@ struct gen_pool *gen_pool_create(int min_alloc_order, int nid) ...@@ -39,17 +39,20 @@ struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
EXPORT_SYMBOL(gen_pool_create); EXPORT_SYMBOL(gen_pool_create);
/** /**
* gen_pool_add - add a new chunk of special memory to the pool * gen_pool_add_virt - add a new chunk of special memory to the pool
* @pool: pool to add new memory chunk to * @pool: pool to add new memory chunk to
* @addr: starting address of memory chunk to add to pool * @virt: virtual starting address of memory chunk to add to pool
* @phys: physical starting address of memory chunk to add to pool
* @size: size in bytes of the memory chunk to add to pool * @size: size in bytes of the memory chunk to add to pool
* @nid: node id of the node the chunk structure and bitmap should be * @nid: node id of the node the chunk structure and bitmap should be
* allocated on, or -1 * allocated on, or -1
* *
* Add a new chunk of special memory to the specified pool. * Add a new chunk of special memory to the specified pool.
*
* Returns 0 on success or a -ve errno on failure.
*/ */
int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size, int gen_pool_add_virt(struct gen_pool *pool, unsigned long virt, phys_addr_t phys,
int nid) size_t size, int nid)
{ {
struct gen_pool_chunk *chunk; struct gen_pool_chunk *chunk;
int nbits = size >> pool->min_alloc_order; int nbits = size >> pool->min_alloc_order;
...@@ -58,11 +61,12 @@ int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size, ...@@ -58,11 +61,12 @@ int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size,
chunk = kmalloc_node(nbytes, GFP_KERNEL | __GFP_ZERO, nid); chunk = kmalloc_node(nbytes, GFP_KERNEL | __GFP_ZERO, nid);
if (unlikely(chunk == NULL)) if (unlikely(chunk == NULL))
return -1; return -ENOMEM;
spin_lock_init(&chunk->lock); spin_lock_init(&chunk->lock);
chunk->start_addr = addr; chunk->phys_addr = phys;
chunk->end_addr = addr + size; chunk->start_addr = virt;
chunk->end_addr = virt + size;
write_lock(&pool->lock); write_lock(&pool->lock);
list_add(&chunk->next_chunk, &pool->chunks); list_add(&chunk->next_chunk, &pool->chunks);
...@@ -70,7 +74,32 @@ int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size, ...@@ -70,7 +74,32 @@ int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size,
return 0; return 0;
} }
EXPORT_SYMBOL(gen_pool_add); EXPORT_SYMBOL(gen_pool_add_virt);
/**
* gen_pool_virt_to_phys - return the physical address of memory
* @pool: pool to allocate from
* @addr: starting address of memory
*
* Returns the physical address on success, or -1 on error.
*/
phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long addr)
{
struct list_head *_chunk;
struct gen_pool_chunk *chunk;
read_lock(&pool->lock);
list_for_each(_chunk, &pool->chunks) {
chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
if (addr >= chunk->start_addr && addr < chunk->end_addr)
return chunk->phys_addr + addr - chunk->start_addr;
}
read_unlock(&pool->lock);
return -1;
}
EXPORT_SYMBOL(gen_pool_virt_to_phys);
/** /**
* gen_pool_destroy - destroy a special memory pool * gen_pool_destroy - destroy a special memory pool
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册