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