@@ -19,7 +19,7 @@ extern "C" {
#endif
void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
- int type);
+ int type, odp_shm_e flag);
uintptr_t _odp_shm_get_paddr(void *vaddr);
#ifdef __cplusplus
@@ -96,7 +96,8 @@ int odp_buffer_pool_init_global(void)
pool_tbl = odp_shm_reserve("odp_buffer_pools",
sizeof(pool_table_t),
- sizeof(pool_entry_t));
+ sizeof(pool_entry_t),
+ ODP_SHM_THREAD);
if (pool_tbl == NULL)
return -1;
@@ -71,7 +71,8 @@ int odp_pktio_init_global(void)
pktio_tbl = odp_shm_reserve("odp_pktio_entries",
sizeof(pktio_table_t),
- sizeof(pktio_entry_t));
+ sizeof(pktio_entry_t),
+ ODP_SHM_THREAD);
if (pktio_tbl == NULL)
return -1;
@@ -99,7 +99,8 @@ int odp_queue_init_global(void)
queue_tbl = odp_shm_reserve("odp_queues",
sizeof(queue_table_t),
- sizeof(queue_entry_t));
+ sizeof(queue_entry_t),
+ ODP_SHM_THREAD);
if (queue_tbl == NULL)
return -1;
@@ -99,9 +99,11 @@ enum {
};
void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
- int type)
+ int type, odp_shm_e flag)
{
- int i;
+ int i, ret, shm_open_flags;
+ int shm = -1;
+ int mmap_flags = MAP_SHARED;
odp_shm_block_t *block;
#ifdef MAP_HUGETLB
uint64_t huge_sz, page_sz;
@@ -110,6 +112,7 @@ void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
huge_sz = odp_sys_huge_page_size();
page_sz = odp_sys_page_size();
#endif
+ void *addr = MAP_FAILED;
odp_spinlock_lock(&odp_shm_tbl->lock);
@@ -134,6 +137,31 @@ void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
block = &odp_shm_tbl->block[i];
+ if (flag != ODP_SHM_THREAD) {
+ shm_open_flags = O_RDWR;
+ if (flag == ODP_SHM_PROC)
+ shm_open_flags |= O_CREAT;
+
+ shm = shm_open(name, shm_open_flags, S_IRUSR | S_IWUSR);
+ if (shm == -1) {
+ odp_spinlock_unlock(&odp_shm_tbl->lock);
+ ODP_ERR("shm_open failed");
+ return NULL;
+ }
+
+ ret = ftruncate(shm, size + align);
+ if (ret == -1) {
+ odp_spinlock_unlock(&odp_shm_tbl->lock);
+ if (flag != ODP_SHM_PROC_NOCREAT)
+ shm_unlink(name);
+ ODP_ERR("ftruncate failed");
+ return NULL;
+ }
+ } else {
+ mmap_flags |= MAP_ANONYMOUS;
+ }
+
+
/* Allocate memory */
mem_config.size = size + align;
mem_config.flags = TI_EM_OSAL_MEM_CACHED;
@@ -156,23 +184,31 @@ void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
align);
return NULL;
}
+ /* remap cma memory to shm mem */
+ if (flag == ODP_SHM_PROC || flag == ODP_SHM_PROC_NOCREAT) {
+ addr = mmap((void *)mem_config.vaddr, size + align,
+ PROT_READ | PROT_WRITE,
+ mmap_flags, shm, 0);
+ } else {
+ addr = (void *)mem_config.vaddr;
+ }
+
} else if (type == ODP_SHM_MMAP) {
- void *addr = MAP_FAILED;
block->huge = 0;
#ifdef MAP_HUGETLB
/* Try first huge pages */
if (huge_sz && (size + align) > page_sz) {
addr = mmap(NULL, size + align, PROT_READ | PROT_WRITE,
- SHM_FLAGS | MAP_HUGETLB, -1, 0);
+ mmap_flags | MAP_HUGETLB, shm, 0);
}
#endif
/* Use normal pages for small or failed huge page allocations */
if (addr == MAP_FAILED) {
addr = mmap(NULL, size + align, PROT_READ | PROT_WRITE,
- SHM_FLAGS, -1, 0);
+ mmap_flags, shm, 0);
} else {
block->huge = 1;
}
@@ -180,17 +216,22 @@ void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
if (addr == MAP_FAILED) {
/* Alloc failed */
odp_spinlock_unlock(&odp_shm_tbl->lock);
+ if (flag != ODP_SHM_PROC_NOCREAT)
+ shm_unlink(name);
return NULL;
}
- mem_config.vaddr = (uintptr_t)addr;
} else {
+ odp_spinlock_unlock(&odp_shm_tbl->lock);
ODP_ERR("Unknown shared memory type: %d\n", type);
+ if (flag != ODP_SHM_PROC_NOCREAT)
+ shm_unlink(name);
+ return NULL;
}
block->mem_config = mem_config;
/* move to correct alignment */
- block->addr = ODP_ALIGN_ROUNDUP_PTR(mem_config.vaddr, align);
+ block->addr = ODP_ALIGN_ROUNDUP_PTR(addr, align);
strncpy(block->name, name, ODP_SHM_NAME_LEN - 1);
block->name[ODP_SHM_NAME_LEN - 1] = 0;
@@ -208,9 +249,10 @@ void *_odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
return block->addr;
}
-void *odp_shm_reserve(const char *name, uint64_t size, uint64_t align)
+void *odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
+ odp_shm_e flag)
{
- return _odp_shm_reserve(name, size, align, ODP_SHM_CMA);
+ return _odp_shm_reserve(name, size, align, ODP_SHM_CMA, flag);
}
uintptr_t _odp_shm_get_paddr(void *vaddr)
Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org> --- v2: shm add CMA support ****Patch not tested******. .../include/odp_shared_memory_internal.h | 2 +- platform/linux-keystone2/odp_buffer_pool.c | 3 +- platform/linux-keystone2/odp_packet_io.c | 3 +- platform/linux-keystone2/odp_queue.c | 3 +- platform/linux-keystone2/odp_shared_memory.c | 60 ++++++++++++++++++---- 5 files changed, 58 insertions(+), 13 deletions(-)