add FreeBSD support
[dpdk.git] / lib / librte_eal / bsdapp / eal / eal_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 #include <sys/mman.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
37 #include <inttypes.h>
38 #include <fcntl.h>
39
40 #include <rte_eal.h>
41 #include <rte_eal_memconfig.h>
42 #include <rte_log.h>
43 #include <rte_string_fns.h>
44 #include "eal_private.h"
45 #include "eal_internal_cfg.h"
46 #include "eal_filesystem.h"
47
48 #define PAGE_SIZE (sysconf(_SC_PAGESIZE))
49
50 static int
51 rte_eal_contigmem_init(void)
52 {
53         struct rte_mem_config *mcfg;
54         uint64_t total_mem = 0;
55         void *addr;
56         unsigned i, j, seg_idx = 0;
57
58         /* get pointer to global configuration */
59         mcfg = rte_eal_get_configuration()->mem_config;
60
61         /* for debug purposes, hugetlbfs can be disabled */
62         if (internal_config.no_hugetlbfs) {
63                 addr = malloc(internal_config.memory);
64                 mcfg->memseg[0].phys_addr = (phys_addr_t)(uintptr_t)addr;
65                 mcfg->memseg[0].addr = addr;
66                 mcfg->memseg[0].len = internal_config.memory;
67                 mcfg->memseg[0].socket_id = 0;
68                 return 0;
69         }
70
71         /* map all hugepages and sort them */
72         for (i = 0; i < internal_config.num_hugepage_sizes; i ++){
73                 struct hugepage_info *hpi;
74
75                 hpi = &internal_config.hugepage_info[i];
76                 for (j = 0; j < hpi->num_pages[0]; j++) {
77                         struct rte_memseg *seg;
78                         uint64_t physaddr;
79                         int error;
80                         size_t sysctl_size = sizeof(physaddr);
81                         char physaddr_str[64];
82
83                         addr = mmap(NULL, hpi->hugepage_sz, PROT_READ|PROT_WRITE,
84                                         MAP_SHARED, hpi->lock_descriptor, j * PAGE_SIZE);
85                         if (addr == MAP_FAILED) {
86                                 RTE_LOG(ERR, EAL, "Failed to mmap buffer %u from %s\n",
87                                                 j, hpi->hugedir);
88                                 return -1;
89                         }
90
91                         rte_snprintf(physaddr_str, sizeof(physaddr_str), "hw.contigmem"
92                                         ".physaddr.%d", j);
93                         error = sysctlbyname(physaddr_str, &physaddr, &sysctl_size,
94                                         NULL, 0);
95                         if (error < 0) {
96                                 RTE_LOG(ERR, EAL, "Failed to get physical addr for buffer %u "
97                                                 "from %s\n", j, hpi->hugedir);
98                                 return -1;
99                         }
100
101                         seg = &mcfg->memseg[seg_idx++];
102                         seg->addr = addr;
103                         seg->phys_addr = physaddr;
104                         seg->hugepage_sz = hpi->hugepage_sz;
105                         seg->len = hpi->hugepage_sz;
106                         seg->nchannel = mcfg->nchannel;
107                         seg->nrank = mcfg->nrank;
108                         seg->socket_id = 0;
109
110                         RTE_LOG(INFO, EAL, "Mapped memory segment %u @ %p: physaddr:0x%"
111                                         PRIx64", len %zu\n",
112                                         seg_idx, addr, physaddr, hpi->hugepage_sz);
113                         if (total_mem >= internal_config.memory ||
114                                         seg_idx >= RTE_MAX_MEMSEG)
115                                 break;
116                 }
117         }
118         return 0;
119 }
120
121 static int
122 rte_eal_contigmem_attach(void)
123 {
124         const struct hugepage_info *hpi;
125         int fd_hugepage_info, fd_hugepage = -1;
126         unsigned i = 0;
127         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
128     
129         /* Obtain a file descriptor for hugepage_info */
130         fd_hugepage_info = open(eal_hugepage_info_path(), O_RDONLY);
131         if (fd_hugepage_info < 0) {
132                 RTE_LOG(ERR, EAL, "Could not open %s\n", eal_hugepage_info_path());
133                 return -1;
134         }
135
136         /* Map the shared hugepage_info into the process address spaces */
137         hpi = mmap(NULL, sizeof(struct hugepage_info), PROT_READ, MAP_PRIVATE,
138                         fd_hugepage_info, 0);
139         if (hpi == NULL) {
140                 RTE_LOG(ERR, EAL, "Could not mmap %s\n", eal_hugepage_info_path());
141                 goto error;
142         }
143
144         /* Obtain a file descriptor for contiguous memory */
145         fd_hugepage = open(hpi->hugedir, O_RDWR);
146         if (fd_hugepage < 0) {
147                 RTE_LOG(ERR, EAL, "Could not open %s\n", hpi->hugedir);
148                 goto error;
149         }
150
151         /* Map the contiguous memory into each memory segment */
152         for (i = 0; i < hpi->num_pages[0]; i++) {
153
154                 void *addr;
155                 struct rte_memseg *seg = &mcfg->memseg[i];
156
157                 addr = mmap(seg->addr, hpi->hugepage_sz, PROT_READ|PROT_WRITE,
158                                 MAP_SHARED|MAP_FIXED, fd_hugepage, i * PAGE_SIZE);
159                 if (addr == MAP_FAILED || addr != seg->addr) {
160                         RTE_LOG(ERR, EAL, "Failed to mmap buffer %u from %s\n",
161                                 i, hpi->hugedir);
162                         goto error;
163                 }
164         
165         }
166
167         /* hugepage_info is no longer required */
168         munmap((void *)(uintptr_t)hpi, sizeof(struct hugepage_info));
169         close(fd_hugepage_info);
170         close(fd_hugepage);
171         return 0;
172
173 error:
174         if (fd_hugepage_info >= 0)
175                 close(fd_hugepage_info);
176         if (fd_hugepage >= 0)
177                 close(fd_hugepage);
178         return -1;
179 }
180
181
182 static int
183 rte_eal_memdevice_init(void)
184 {
185         struct rte_config *config;
186
187         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
188                 return 0;
189
190         config = rte_eal_get_configuration();
191         config->mem_config->nchannel = internal_config.force_nchannel;
192         config->mem_config->nrank = internal_config.force_nrank;
193
194         return 0;
195 }
196
197 /* init memory subsystem */
198 int
199 rte_eal_memory_init(void)
200 {
201         RTE_LOG(INFO, EAL, "Setting up physically contiguous memory...\n");
202         const int retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
203                         rte_eal_contigmem_init() :
204                         rte_eal_contigmem_attach();
205         if (retval < 0)
206                 return -1;
207
208         if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)
209                 return -1;
210
211         return 0;
212 }