New upstream version 18.02
[deb_dpdk.git] / lib / librte_eal / common / eal_common_memory.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <unistd.h>
10 #include <inttypes.h>
11 #include <sys/mman.h>
12 #include <sys/queue.h>
13
14 #include <rte_memory.h>
15 #include <rte_eal.h>
16 #include <rte_eal_memconfig.h>
17 #include <rte_log.h>
18
19 #include "eal_private.h"
20 #include "eal_internal_cfg.h"
21
22 /*
23  * Return a pointer to a read-only table of struct rte_physmem_desc
24  * elements, containing the layout of all addressable physical
25  * memory. The last element of the table contains a NULL address.
26  */
27 const struct rte_memseg *
28 rte_eal_get_physmem_layout(void)
29 {
30         return rte_eal_get_configuration()->mem_config->memseg;
31 }
32
33
34 /* get the total size of memory */
35 uint64_t
36 rte_eal_get_physmem_size(void)
37 {
38         const struct rte_mem_config *mcfg;
39         unsigned i = 0;
40         uint64_t total_len = 0;
41
42         /* get pointer to global configuration */
43         mcfg = rte_eal_get_configuration()->mem_config;
44
45         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
46                 if (mcfg->memseg[i].addr == NULL)
47                         break;
48
49                 total_len += mcfg->memseg[i].len;
50         }
51
52         return total_len;
53 }
54
55 /* Dump the physical memory layout on console */
56 void
57 rte_dump_physmem_layout(FILE *f)
58 {
59         const struct rte_mem_config *mcfg;
60         unsigned i = 0;
61
62         /* get pointer to global configuration */
63         mcfg = rte_eal_get_configuration()->mem_config;
64
65         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
66                 if (mcfg->memseg[i].addr == NULL)
67                         break;
68
69                 fprintf(f, "Segment %u: IOVA:0x%"PRIx64", len:%zu, "
70                        "virt:%p, socket_id:%"PRId32", "
71                        "hugepage_sz:%"PRIu64", nchannel:%"PRIx32", "
72                        "nrank:%"PRIx32"\n", i,
73                        mcfg->memseg[i].iova,
74                        mcfg->memseg[i].len,
75                        mcfg->memseg[i].addr,
76                        mcfg->memseg[i].socket_id,
77                        mcfg->memseg[i].hugepage_sz,
78                        mcfg->memseg[i].nchannel,
79                        mcfg->memseg[i].nrank);
80         }
81 }
82
83 /* return the number of memory channels */
84 unsigned rte_memory_get_nchannel(void)
85 {
86         return rte_eal_get_configuration()->mem_config->nchannel;
87 }
88
89 /* return the number of memory rank */
90 unsigned rte_memory_get_nrank(void)
91 {
92         return rte_eal_get_configuration()->mem_config->nrank;
93 }
94
95 static int
96 rte_eal_memdevice_init(void)
97 {
98         struct rte_config *config;
99
100         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
101                 return 0;
102
103         config = rte_eal_get_configuration();
104         config->mem_config->nchannel = internal_config.force_nchannel;
105         config->mem_config->nrank = internal_config.force_nrank;
106
107         return 0;
108 }
109
110 /* Lock page in physical memory and prevent from swapping. */
111 int
112 rte_mem_lock_page(const void *virt)
113 {
114         unsigned long virtual = (unsigned long)virt;
115         int page_size = getpagesize();
116         unsigned long aligned = (virtual & ~(page_size - 1));
117         return mlock((void *)aligned, page_size);
118 }
119
120 /* init memory subsystem */
121 int
122 rte_eal_memory_init(void)
123 {
124         RTE_LOG(DEBUG, EAL, "Setting up physically contiguous memory...\n");
125
126         const int retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
127                         rte_eal_hugepage_init() :
128                         rte_eal_hugepage_attach();
129         if (retval < 0)
130                 return -1;
131
132         if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)
133                 return -1;
134
135         return 0;
136 }