summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDidier Pallard <didier.pallard@6wind.com>2013-03-19 17:00:02 +0100
committerThomas Monjalon <thomas.monjalon@6wind.com>2013-09-17 14:16:11 +0200
commit962ce023f76044b489530f9c2b6652d3831f9032 (patch)
tree0db0292ed6d6693d187b45b221ba345e8412e649
parent945fa0e651e0bed03d05eeda36a7676fbf793691 (diff)
downloaddpdk-962ce023f76044b489530f9c2b6652d3831f9032.zip
dpdk-962ce023f76044b489530f9c2b6652d3831f9032.tar.gz
dpdk-962ce023f76044b489530f9c2b6652d3831f9032.tar.xz
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 <didier.pallard@6wind.com> Acked-by: Thomas Monjalon <thomas.monjalon@6wind.com>
-rw-r--r--lib/librte_malloc/malloc_elem.c7
-rw-r--r--lib/librte_malloc/malloc_elem.h2
-rw-r--r--lib/librte_malloc/malloc_heap.c2
-rw-r--r--lib/librte_malloc/rte_malloc.c12
-rw-r--r--lib/librte_malloc/rte_malloc.h14
5 files changed, 33 insertions, 4 deletions
diff --git a/lib/librte_malloc/malloc_elem.c b/lib/librte_malloc/malloc_elem.c
index 919d474..8da517f 100644
--- a/lib/librte_malloc/malloc_elem.c
+++ b/lib/librte_malloc/malloc_elem.c
@@ -57,9 +57,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;
@@ -74,7 +75,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 */
}
@@ -117,7 +118,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 fadf0a5..f9475df 100644
--- a/lib/librte_malloc/malloc_elem.h
+++ b/lib/librte_malloc/malloc_elem.h
@@ -48,6 +48,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;
@@ -134,6 +135,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 3f8f8e2..e18b725 100644
--- a/lib/librte_malloc/malloc_heap.c
+++ b/lib/librte_malloc/malloc_heap.c
@@ -100,7 +100,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 108c76e..6d43221 100644
--- a/lib/librte_malloc/rte_malloc.c
+++ b/lib/librte_malloc/rte_malloc.c
@@ -228,3 +228,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 acf2ae0..fe85689 100644
--- a/lib/librte_malloc/rte_malloc.h
+++ b/lib/librte_malloc/rte_malloc.h
@@ -42,6 +42,7 @@
*/
#include <stddef.h>
+#include <rte_memory.h>
#ifdef __cplusplus
extern "C" {
@@ -319,6 +320,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