From e29ad45703b2cc67d2cfad1b0813d7c07ab76b28 Mon Sep 17 00:00:00 2001 From: Didier Pallard Date: Tue, 19 Mar 2013 17:00:02 +0100 Subject: [PATCH] mem: get physical address of any rte_malloc buffer Get physical address of any rte_malloc allocated buffer using function rte_malloc_virt2phy(addr). The rte_memzone pointer is now stored in each allocated memory block header to allow simple computation of physical address of a block using the memzone it comes from. The function rte_malloc_virt2phy has a dependency on rte_memory.h: phys_addr_t must be defined. Signed-off-by: Didier Pallard Acked-by: Thomas Monjalon --- lib/librte_malloc/malloc_elem.c | 7 ++++--- lib/librte_malloc/malloc_elem.h | 2 ++ lib/librte_malloc/malloc_heap.c | 2 +- lib/librte_malloc/rte_malloc.c | 12 ++++++++++++ lib/librte_malloc/rte_malloc.h | 14 ++++++++++++++ 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/librte_malloc/malloc_elem.c b/lib/librte_malloc/malloc_elem.c index 6a92253058..f0da6408f3 100644 --- a/lib/librte_malloc/malloc_elem.c +++ b/lib/librte_malloc/malloc_elem.c @@ -56,9 +56,10 @@ */ void malloc_elem_init(struct malloc_elem *elem, - struct malloc_heap *heap, size_t size) + struct malloc_heap *heap, const struct rte_memzone *mz, size_t size) { elem->heap = heap; + elem->mz = mz; elem->prev = elem->next_free = NULL; elem->state = ELEM_FREE; elem->size = size; @@ -73,7 +74,7 @@ malloc_elem_init(struct malloc_elem *elem, void malloc_elem_mkend(struct malloc_elem *elem, struct malloc_elem *prev) { - malloc_elem_init(elem, prev->heap, 0); + malloc_elem_init(elem, prev->heap, prev->mz, 0); elem->prev = prev; elem->state = ELEM_BUSY; /* mark busy so its never merged */ } @@ -116,7 +117,7 @@ split_elem(struct malloc_elem *elem, struct malloc_elem *split_pt) const unsigned old_elem_size = (uintptr_t)split_pt - (uintptr_t)elem; const unsigned new_elem_size = elem->size - old_elem_size; - malloc_elem_init(split_pt, elem->heap, new_elem_size); + malloc_elem_init(split_pt, elem->heap, elem->mz, new_elem_size); split_pt->prev = elem; next_elem->prev = split_pt; elem->size = old_elem_size; diff --git a/lib/librte_malloc/malloc_elem.h b/lib/librte_malloc/malloc_elem.h index 1997109e23..eadecf9c9f 100644 --- a/lib/librte_malloc/malloc_elem.h +++ b/lib/librte_malloc/malloc_elem.h @@ -47,6 +47,7 @@ struct malloc_elem { struct malloc_heap *heap; struct malloc_elem *volatile prev; /* points to prev elem in memzone */ struct malloc_elem *volatile next_free; /* to make list of free elements */ + const struct rte_memzone *mz; volatile enum elem_state state; uint32_t pad; size_t size; @@ -133,6 +134,7 @@ malloc_elem_from_data(const void *data) void malloc_elem_init(struct malloc_elem *elem, struct malloc_heap *heap, + const struct rte_memzone *mz, size_t size); /* diff --git a/lib/librte_malloc/malloc_heap.c b/lib/librte_malloc/malloc_heap.c index dec7ab1eb1..f4a029491d 100644 --- a/lib/librte_malloc/malloc_heap.c +++ b/lib/librte_malloc/malloc_heap.c @@ -110,7 +110,7 @@ malloc_heap_add_memzone(struct malloc_heap *heap, size_t size, unsigned align) end_elem = RTE_PTR_ALIGN_FLOOR(end_elem, CACHE_LINE_SIZE); const unsigned elem_size = (uintptr_t)end_elem - (uintptr_t)start_elem; - malloc_elem_init(start_elem, heap, elem_size); + malloc_elem_init(start_elem, heap, mz, elem_size); malloc_elem_mkend(end_elem, start_elem); start_elem->next_free = heap->free_head; diff --git a/lib/librte_malloc/rte_malloc.c b/lib/librte_malloc/rte_malloc.c index 53b24da86c..2bdec29985 100644 --- a/lib/librte_malloc/rte_malloc.c +++ b/lib/librte_malloc/rte_malloc.c @@ -227,3 +227,15 @@ rte_malloc_set_limit(__rte_unused const char *type, { return 0; } + +/* + * Return the physical address of a virtual address obtained through rte_malloc + */ +phys_addr_t +rte_malloc_virt2phy(const void *addr) +{ + const struct malloc_elem *elem = malloc_elem_from_data(addr); + if (elem == NULL) + return 0; + return elem->mz->phys_addr + ((uintptr_t)addr - (uintptr_t)elem->mz->addr); +} diff --git a/lib/librte_malloc/rte_malloc.h b/lib/librte_malloc/rte_malloc.h index dc80d72b68..14a78b0b7a 100644 --- a/lib/librte_malloc/rte_malloc.h +++ b/lib/librte_malloc/rte_malloc.h @@ -41,6 +41,7 @@ */ #include +#include #ifdef __cplusplus extern "C" { @@ -318,6 +319,19 @@ rte_malloc_dump_stats(const char *type); int rte_malloc_set_limit(const char *type, size_t max); +/** + * Return the physical address of a virtual address obtained through + * rte_malloc + * + * @param addr + * Adress obtained from a previous rte_malloc call + * @return + * NULL on error + * otherwise return physical address of the buffer + */ +phys_addr_t +rte_malloc_virt2phy(const void *addr); + #ifdef __cplusplus } #endif -- 2.20.1