New upstream version 17.11-rc3
[deb_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 EAL_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_virt2iova() when hugepages are disabled. */
58         (void)virtaddr;
59         return RTE_BAD_IOVA;
60 }
61 rte_iova_t
62 rte_mem_virt2iova(const void *virtaddr)
63 {
64         return rte_mem_virt2phy(virtaddr);
65 }
66
67 int
68 rte_eal_hugepage_init(void)
69 {
70         struct rte_mem_config *mcfg;
71         uint64_t total_mem = 0;
72         void *addr;
73         unsigned i, j, seg_idx = 0;
74
75         /* get pointer to global configuration */
76         mcfg = rte_eal_get_configuration()->mem_config;
77
78         /* for debug purposes, hugetlbfs can be disabled */
79         if (internal_config.no_hugetlbfs) {
80                 addr = malloc(internal_config.memory);
81                 mcfg->memseg[0].iova = (rte_iova_t)(uintptr_t)addr;
82                 mcfg->memseg[0].addr = addr;
83                 mcfg->memseg[0].hugepage_sz = RTE_PGSIZE_4K;
84                 mcfg->memseg[0].len = internal_config.memory;
85                 mcfg->memseg[0].socket_id = 0;
86                 return 0;
87         }
88
89         /* map all hugepages and sort them */
90         for (i = 0; i < internal_config.num_hugepage_sizes; i ++){
91                 struct hugepage_info *hpi;
92
93                 hpi = &internal_config.hugepage_info[i];
94                 for (j = 0; j < hpi->num_pages[0]; j++) {
95                         struct rte_memseg *seg;
96                         rte_iova_t physaddr;
97                         int error;
98                         size_t sysctl_size = sizeof(physaddr);
99                         char physaddr_str[64];
100
101                         addr = mmap(NULL, hpi->hugepage_sz, PROT_READ|PROT_WRITE,
102                                     MAP_SHARED, hpi->lock_descriptor,
103                                     j * EAL_PAGE_SIZE);
104                         if (addr == MAP_FAILED) {
105                                 RTE_LOG(ERR, EAL, "Failed to mmap buffer %u from %s\n",
106                                                 j, hpi->hugedir);
107                                 return -1;
108                         }
109
110                         snprintf(physaddr_str, sizeof(physaddr_str), "hw.contigmem"
111                                         ".physaddr.%d", j);
112                         error = sysctlbyname(physaddr_str, &physaddr, &sysctl_size,
113                                         NULL, 0);
114                         if (error < 0) {
115                                 RTE_LOG(ERR, EAL, "Failed to get physical addr for buffer %u "
116                                                 "from %s\n", j, hpi->hugedir);
117                                 return -1;
118                         }
119
120                         seg = &mcfg->memseg[seg_idx++];
121                         seg->addr = addr;
122                         seg->iova = physaddr;
123                         seg->hugepage_sz = hpi->hugepage_sz;
124                         seg->len = hpi->hugepage_sz;
125                         seg->nchannel = mcfg->nchannel;
126                         seg->nrank = mcfg->nrank;
127                         seg->socket_id = 0;
128
129                         RTE_LOG(INFO, EAL, "Mapped memory segment %u @ %p: physaddr:0x%"
130                                         PRIx64", len %zu\n",
131                                         seg_idx, addr, physaddr, hpi->hugepage_sz);
132                         if (total_mem >= internal_config.memory ||
133                                         seg_idx >= RTE_MAX_MEMSEG)
134                                 break;
135                 }
136         }
137         return 0;
138 }
139
140 int
141 rte_eal_hugepage_attach(void)
142 {
143         const struct hugepage_info *hpi;
144         int fd_hugepage_info, fd_hugepage = -1;
145         unsigned i = 0;
146         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
147
148         /* Obtain a file descriptor for hugepage_info */
149         fd_hugepage_info = open(eal_hugepage_info_path(), O_RDONLY);
150         if (fd_hugepage_info < 0) {
151                 RTE_LOG(ERR, EAL, "Could not open %s\n", eal_hugepage_info_path());
152                 return -1;
153         }
154
155         /* Map the shared hugepage_info into the process address spaces */
156         hpi = mmap(NULL, sizeof(struct hugepage_info), PROT_READ, MAP_PRIVATE,
157                         fd_hugepage_info, 0);
158         if (hpi == NULL) {
159                 RTE_LOG(ERR, EAL, "Could not mmap %s\n", eal_hugepage_info_path());
160                 goto error;
161         }
162
163         /* Obtain a file descriptor for contiguous memory */
164         fd_hugepage = open(hpi->hugedir, O_RDWR);
165         if (fd_hugepage < 0) {
166                 RTE_LOG(ERR, EAL, "Could not open %s\n", hpi->hugedir);
167                 goto error;
168         }
169
170         /* Map the contiguous memory into each memory segment */
171         for (i = 0; i < hpi->num_pages[0]; i++) {
172
173                 void *addr;
174                 struct rte_memseg *seg = &mcfg->memseg[i];
175
176                 addr = mmap(seg->addr, hpi->hugepage_sz, PROT_READ|PROT_WRITE,
177                             MAP_SHARED|MAP_FIXED, fd_hugepage,
178                             i * EAL_PAGE_SIZE);
179                 if (addr == MAP_FAILED || addr != seg->addr) {
180                         RTE_LOG(ERR, EAL, "Failed to mmap buffer %u from %s\n",
181                                 i, hpi->hugedir);
182                         goto error;
183                 }
184
185         }
186
187         /* hugepage_info is no longer required */
188         munmap((void *)(uintptr_t)hpi, sizeof(struct hugepage_info));
189         close(fd_hugepage_info);
190         close(fd_hugepage);
191         return 0;
192
193 error:
194         if (fd_hugepage_info >= 0)
195                 close(fd_hugepage_info);
196         if (fd_hugepage >= 0)
197                 close(fd_hugepage);
198         return -1;
199 }
200
201 int
202 rte_eal_using_phys_addrs(void)
203 {
204         return 0;
205 }