4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
41 #include <rte_eal_memconfig.h>
43 #include <rte_string_fns.h>
44 #include "eal_private.h"
45 #include "eal_internal_cfg.h"
46 #include "eal_filesystem.h"
48 #define EAL_PAGE_SIZE (sysconf(_SC_PAGESIZE))
51 * Get physical address of any mapped virtual address in the current process.
54 rte_mem_virt2phy(const void *virtaddr)
56 /* XXX not implemented. This function is only used by
57 * rte_mempool_virt2phy() when hugepages are disabled. */
62 rte_mem_virt2iova(const void *virtaddr)
64 return rte_mem_virt2phy(virtaddr);
68 rte_eal_hugepage_init(void)
70 struct rte_mem_config *mcfg;
71 uint64_t total_mem = 0;
73 unsigned i, j, seg_idx = 0;
75 /* get pointer to global configuration */
76 mcfg = rte_eal_get_configuration()->mem_config;
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;
89 /* map all hugepages and sort them */
90 for (i = 0; i < internal_config.num_hugepage_sizes; i ++){
91 struct hugepage_info *hpi;
93 hpi = &internal_config.hugepage_info[i];
94 for (j = 0; j < hpi->num_pages[0]; j++) {
95 struct rte_memseg *seg;
98 size_t sysctl_size = sizeof(physaddr);
99 char physaddr_str[64];
101 addr = mmap(NULL, hpi->hugepage_sz, PROT_READ|PROT_WRITE,
102 MAP_SHARED, hpi->lock_descriptor,
104 if (addr == MAP_FAILED) {
105 RTE_LOG(ERR, EAL, "Failed to mmap buffer %u from %s\n",
110 snprintf(physaddr_str, sizeof(physaddr_str), "hw.contigmem"
112 error = sysctlbyname(physaddr_str, &physaddr, &sysctl_size,
115 RTE_LOG(ERR, EAL, "Failed to get physical addr for buffer %u "
116 "from %s\n", j, hpi->hugedir);
120 seg = &mcfg->memseg[seg_idx++];
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;
129 RTE_LOG(INFO, EAL, "Mapped memory segment %u @ %p: physaddr:0x%"
131 seg_idx, addr, physaddr, hpi->hugepage_sz);
132 if (total_mem >= internal_config.memory ||
133 seg_idx >= RTE_MAX_MEMSEG)
141 rte_eal_hugepage_attach(void)
143 const struct hugepage_info *hpi;
144 int fd_hugepage_info, fd_hugepage = -1;
146 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
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());
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);
159 RTE_LOG(ERR, EAL, "Could not mmap %s\n", eal_hugepage_info_path());
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);
170 /* Map the contiguous memory into each memory segment */
171 for (i = 0; i < hpi->num_pages[0]; i++) {
174 struct rte_memseg *seg = &mcfg->memseg[i];
176 addr = mmap(seg->addr, hpi->hugepage_sz, PROT_READ|PROT_WRITE,
177 MAP_SHARED|MAP_FIXED, fd_hugepage,
179 if (addr == MAP_FAILED || addr != seg->addr) {
180 RTE_LOG(ERR, EAL, "Failed to mmap buffer %u from %s\n",
187 /* hugepage_info is no longer required */
188 munmap((void *)(uintptr_t)hpi, sizeof(struct hugepage_info));
189 close(fd_hugepage_info);
194 if (fd_hugepage_info >= 0)
195 close(fd_hugepage_info);
196 if (fd_hugepage >= 0)
202 rte_eal_using_phys_addrs(void)