996877ef59420046c29e673221eaec1feea00333
[deb_dpdk.git] / lib / librte_eal / common / eal_common_memory.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <stdarg.h>
38 #include <unistd.h>
39 #include <inttypes.h>
40 #include <sys/mman.h>
41 #include <sys/queue.h>
42
43 #include <rte_memory.h>
44 #include <rte_memzone.h>
45 #include <rte_eal.h>
46 #include <rte_eal_memconfig.h>
47 #include <rte_log.h>
48
49 #include "eal_private.h"
50 #include "eal_internal_cfg.h"
51
52 /*
53  * Return a pointer to a read-only table of struct rte_physmem_desc
54  * elements, containing the layout of all addressable physical
55  * memory. The last element of the table contains a NULL address.
56  */
57 const struct rte_memseg *
58 rte_eal_get_physmem_layout(void)
59 {
60         return rte_eal_get_configuration()->mem_config->memseg;
61 }
62
63
64 /* get the total size of memory */
65 uint64_t
66 rte_eal_get_physmem_size(void)
67 {
68         const struct rte_mem_config *mcfg;
69         unsigned i = 0;
70         uint64_t total_len = 0;
71
72         /* get pointer to global configuration */
73         mcfg = rte_eal_get_configuration()->mem_config;
74
75         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
76                 if (mcfg->memseg[i].addr == NULL)
77                         break;
78
79                 total_len += mcfg->memseg[i].len;
80         }
81
82         return total_len;
83 }
84
85 /* Dump the physical memory layout on console */
86 void
87 rte_dump_physmem_layout(FILE *f)
88 {
89         const struct rte_mem_config *mcfg;
90         unsigned i = 0;
91
92         /* get pointer to global configuration */
93         mcfg = rte_eal_get_configuration()->mem_config;
94
95         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
96                 if (mcfg->memseg[i].addr == NULL)
97                         break;
98
99                 fprintf(f, "Segment %u: phys:0x%"PRIx64", len:%zu, "
100                        "virt:%p, socket_id:%"PRId32", "
101                        "hugepage_sz:%"PRIu64", nchannel:%"PRIx32", "
102                        "nrank:%"PRIx32"\n", i,
103                        mcfg->memseg[i].phys_addr,
104                        mcfg->memseg[i].len,
105                        mcfg->memseg[i].addr,
106                        mcfg->memseg[i].socket_id,
107                        mcfg->memseg[i].hugepage_sz,
108                        mcfg->memseg[i].nchannel,
109                        mcfg->memseg[i].nrank);
110         }
111 }
112
113 /* return the number of memory channels */
114 unsigned rte_memory_get_nchannel(void)
115 {
116         return rte_eal_get_configuration()->mem_config->nchannel;
117 }
118
119 /* return the number of memory rank */
120 unsigned rte_memory_get_nrank(void)
121 {
122         return rte_eal_get_configuration()->mem_config->nrank;
123 }
124
125 static int
126 rte_eal_memdevice_init(void)
127 {
128         struct rte_config *config;
129
130         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
131                 return 0;
132
133         config = rte_eal_get_configuration();
134         config->mem_config->nchannel = internal_config.force_nchannel;
135         config->mem_config->nrank = internal_config.force_nrank;
136
137         return 0;
138 }
139
140 /* Lock page in physical memory and prevent from swapping. */
141 int
142 rte_mem_lock_page(const void *virt)
143 {
144         unsigned long virtual = (unsigned long)virt;
145         int page_size = getpagesize();
146         unsigned long aligned = (virtual & ~(page_size - 1));
147         return mlock((void *)aligned, page_size);
148 }
149
150 /* init memory subsystem */
151 int
152 rte_eal_memory_init(void)
153 {
154         RTE_LOG(DEBUG, EAL, "Setting up physically contiguous memory...\n");
155
156         const int retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
157                         rte_eal_hugepage_init() :
158                         rte_eal_hugepage_attach();
159         if (retval < 0)
160                 return -1;
161
162         if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)
163                 return -1;
164
165         return 0;
166 }