summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDidier Pallard <didier.pallard@6wind.com>2013-03-19 17:00:02 +0100
committerThomas Monjalon <thomas.monjalon@6wind.com>2014-01-15 18:08:44 +0100
commit0ec3da126ac5013b7016ddfe0072d36caf04b8d9 (patch)
tree5a7e02a84779fa362fa357a8a4fb1156356a93a8 /lib
parent9ad73b32805b90905b19370a8c620713a659949b (diff)
downloaddpdk-0ec3da126ac5013b7016ddfe0072d36caf04b8d9.zip
dpdk-0ec3da126ac5013b7016ddfe0072d36caf04b8d9.tar.gz
dpdk-0ec3da126ac5013b7016ddfe0072d36caf04b8d9.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>
Diffstat (limited to 'lib')
-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 33145e6..ed90fcc 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 d99e72d..95de377 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 2d3dfb3..1bf7d4b 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 ea70eb4..92f16b8 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 5d38bc2..8c6e7c7 100644
--- a/lib/librte_malloc/rte_malloc.h
+++ b/lib/librte_malloc/rte_malloc.h
@@ -41,6 +41,7 @@
*/
#include <stddef.h>
+#include <rte_memory.h>
#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