From 482dc8035cfae1c792ca1fa1a2d79e7ea938ee14 Mon Sep 17 00:00:00 2001
From: wuchangsheng <wuchangsheng2@huawei.com>
Date: Tue, 30 Mar 2021 16:26:22 +0800
Subject: [PATCH] dpdk-support-gazelle-03-memory

---
 lib/librte_eal/common/eal_common_memory.c | 88 ++++++++++++++++++-----
 lib/librte_eal/common/eal_memalloc.h      |  7 ++
 2 files changed, 79 insertions(+), 16 deletions(-)

diff --git a/lib/librte_eal/common/eal_common_memory.c b/lib/librte_eal/common/eal_common_memory.c
index 4a9cc1f..842fc9b 100644
--- a/lib/librte_eal/common/eal_common_memory.c
+++ b/lib/librte_eal/common/eal_common_memory.c
@@ -206,9 +206,9 @@ virt2memseg(const void *addr, const struct rte_memseg_list *msl)
 }
 
 static struct rte_memseg_list *
-virt2memseg_list(const void *addr)
+virt2memseg_list(const void *addr, const struct rte_config *rte_cfg)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+	struct rte_mem_config *mcfg = rte_cfg->mem_config;
 	struct rte_memseg_list *msl;
 	int msl_idx;
 
@@ -230,7 +230,13 @@ virt2memseg_list(const void *addr)
 struct rte_memseg_list *
 rte_mem_virt2memseg_list(const void *addr)
 {
-	return virt2memseg_list(addr);
+	return virt2memseg_list(addr, rte_eal_get_configuration());
+}
+
+struct rte_memseg_list *
+rte_sec_mem_virt2memseg_list(const void *addr, const struct rte_config *rte_cfg)
+{
+	return virt2memseg_list(addr, rte_cfg);
 }
 
 struct virtiova {
@@ -283,11 +289,25 @@ rte_mem_iova2virt(rte_iova_t iova)
 	return vi.virt;
 }
 
+static struct rte_memseg *
+__rte_mem_virt2memseg(const void *addr, const struct rte_memseg_list *msl, 
+		const struct rte_config *rte_cfg)
+{
+	return virt2memseg(addr, msl != NULL ? msl :
+			rte_sec_mem_virt2memseg_list(addr, rte_cfg));
+}
+
 struct rte_memseg *
 rte_mem_virt2memseg(const void *addr, const struct rte_memseg_list *msl)
 {
-	return virt2memseg(addr, msl != NULL ? msl :
-			rte_mem_virt2memseg_list(addr));
+	return __rte_mem_virt2memseg(addr, msl, rte_eal_get_configuration());
+}
+
+struct rte_memseg *
+rte_sec_mem_virt2memseg(const void *addr, const struct rte_memseg_list *msl, 
+		const struct rte_config *rte_cfg)
+{
+	return __rte_mem_virt2memseg(addr, msl, rte_cfg);
 }
 
 static int
@@ -889,10 +909,14 @@ rte_extmem_detach(void *va_addr, size_t len)
 }
 
 /* init memory subsystem */
-int
-rte_eal_memory_init(void)
+static int
+__rte_eal_memory_init(__attribute__((__unused__)) const char *runtime_dir,
+		const struct internal_config *internal_cfg,
+		struct rte_config *rte_cfg,
+		const int switch_pri_and_sec,
+		const int sec_idx)
 {
-	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+	struct rte_mem_config *mcfg = rte_cfg->mem_config;
 	int retval;
 	RTE_LOG(DEBUG, EAL, "Setting up physically contiguous memory...\n");
 
@@ -900,25 +924,57 @@ rte_eal_memory_init(void)
 		return -1;
 
 	/* lock mem hotplug here, to prevent races while we init */
-	rte_mcfg_mem_read_lock();
+	rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
 
-	if (rte_eal_memseg_init() < 0)
+	if (rte_eal_memseg_init(switch_pri_and_sec, sec_idx) < 0)
 		goto fail;
 
-	if (eal_memalloc_init() < 0)
-		goto fail;
+	if (!internal_cfg->pri_and_sec)
+		if (eal_memalloc_init() < 0)
+			goto fail;
 
-	retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
+	retval = rte_cfg->process_type == RTE_PROC_PRIMARY ?
 			rte_eal_hugepage_init() :
-			rte_eal_hugepage_attach();
+			rte_eal_hugepage_attach(switch_pri_and_sec, sec_idx);
 	if (retval < 0)
 		goto fail;
 
-	if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)
+	if (internal_cfg->no_shconf == 0 && rte_eal_memdevice_init() < 0)
 		goto fail;
 
 	return 0;
 fail:
-	rte_mcfg_mem_read_unlock();
+	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
 	return -1;
 }
+
+int
+rte_eal_memory_init(void)
+{
+	const int unused_idx = -1;
+
+	return __rte_eal_memory_init(rte_eal_get_runtime_dir(),
+			&internal_config, rte_eal_get_configuration(),
+			false, unused_idx);
+}
+
+int
+rte_eal_sec_memory_init(const int sec_idx)
+{
+	int ret;
+	struct rte_config *rte_cfg = rte_eal_sec_get_configuration(sec_idx);
+
+	ret = __rte_eal_memory_init(rte_eal_sec_get_runtime_dir(sec_idx),
+			rte_eal_sec_get_internal_config(sec_idx), rte_cfg,
+			true, sec_idx);
+
+	rte_rwlock_read_unlock(&rte_cfg->mem_config->memory_hotplug_lock);
+
+	return ret;
+}
+
+int
+rte_eal_sec_memory_cleanup(const int sec_idx)
+{
+	return eal_memalloc_destroy(sec_idx);
+}
diff --git a/lib/librte_eal/common/eal_memalloc.h b/lib/librte_eal/common/eal_memalloc.h
index e953cd8..d5ea6e1 100644
--- a/lib/librte_eal/common/eal_memalloc.h
+++ b/lib/librte_eal/common/eal_memalloc.h
@@ -83,6 +83,10 @@ eal_memalloc_get_seg_fd(int list_idx, int seg_idx);
 int
 eal_memalloc_set_seg_fd(int list_idx, int seg_idx, int fd);
 
+int
+eal_sec_memalloc_set_seg_fd(int list_idx, int seg_idx, int fd, 
+		const int switch_pri_and_sec, const int sec_idx);
+
 /* returns 0 or -errno */
 int
 eal_memalloc_set_seg_list_fd(int list_idx, int fd);
@@ -93,4 +97,7 @@ eal_memalloc_get_seg_fd_offset(int list_idx, int seg_idx, size_t *offset);
 int
 eal_memalloc_init(void);
 
+int
+eal_memalloc_destroy(const int sec_idx);
+
 #endif /* EAL_MEMALLOC_H */
-- 
2.23.0

