4 * Copyright(c) 2010-2012 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.
33 * version: DPDK.L.1.2.3-3
43 #include <sys/queue.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_tailq.h>
50 #include <rte_per_lcore.h>
51 #include <rte_errno.h>
52 #include <rte_string_fns.h>
53 #include <rte_common.h>
55 #include "eal_private.h"
57 /* internal copy of free memory segments */
58 static struct rte_memseg free_memseg[RTE_MAX_MEMSEG];
60 /* pointer to last reserved memzone */
61 static unsigned memzone_idx;
64 * Return a pointer to a correctly filled memzone descriptor. If the
65 * allocation cannot be done, return NULL.
67 const struct rte_memzone *
68 rte_memzone_reserve(const char *name, uint64_t len, int socket_id,
71 return rte_memzone_reserve_aligned(name,
72 len, socket_id, flags, CACHE_LINE_SIZE);
76 * Return a pointer to a correctly filled memzone descriptor (with a
77 * specified alignment). If the allocation cannot be done, return NULL.
79 const struct rte_memzone *
80 rte_memzone_reserve_aligned(const char *name, uint64_t len,
81 int socket_id, unsigned flags, unsigned align)
83 struct rte_config *config;
86 uint64_t requested_len;
87 uint64_t memseg_len = 0;
88 phys_addr_t memseg_physaddr;
90 uintptr_t addr_offset;
92 /* if secondary processes return error */
93 if (rte_eal_process_type() == RTE_PROC_SECONDARY){
94 RTE_LOG(ERR, EAL, "%s(): Not allowed in secondary process\n", __func__);
95 rte_errno = E_RTE_SECONDARY;
99 /* if alignment is not a power of two */
100 if (!rte_is_power_of_2(align)) {
101 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
107 /* alignment less than cache size is not allowed */
108 if (align < CACHE_LINE_SIZE)
109 align = CACHE_LINE_SIZE;
111 /* get pointer to global configuration */
112 config = rte_eal_get_configuration();
114 /* no more room in config */
115 if (memzone_idx >= RTE_MAX_MEMZONE) {
116 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
121 /* both sizes cannot be explicitly called for */
122 if ((flags & RTE_MEMZONE_1GB) && (flags & RTE_MEMZONE_2MB)) {
127 /* zone already exist */
128 if (rte_memzone_lookup(name) != NULL) {
129 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
135 /* align length on cache boundary */
136 len += CACHE_LINE_MASK;
137 len &= ~((uint64_t)CACHE_LINE_MASK);
141 /* save requested length */
144 /* reserve extra space for future alignment */
148 /* find the smallest segment matching requirements */
149 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
152 if (free_memseg[i].addr == NULL)
155 /* empty segment, skip it */
156 if (free_memseg[i].len == 0)
160 if (socket_id != SOCKET_ID_ANY &&
161 socket_id != free_memseg[i].socket_id)
165 if (len != 0 && len > free_memseg[i].len)
168 /* check flags for hugepage sizes */
169 if ((flags & RTE_MEMZONE_2MB) &&
170 free_memseg[i].hugepage_sz == RTE_PGSIZE_1G )
172 if ((flags & RTE_MEMZONE_1GB) &&
173 free_memseg[i].hugepage_sz == RTE_PGSIZE_2M )
176 /* this segment is the best until now */
177 if (memseg_idx == -1) {
179 memseg_len = free_memseg[i].len;
181 /* find the biggest contiguous zone */
183 if (free_memseg[i].len > memseg_len) {
185 memseg_len = free_memseg[i].len;
189 * find the smallest (we already checked that current
190 * zone length is > len
192 else if (free_memseg[i].len < memseg_len) {
194 memseg_len = free_memseg[i].len;
198 /* no segment found */
199 if (memseg_idx == -1) {
201 * If RTE_MEMZONE_SIZE_HINT_ONLY flag is specified,
202 * try allocating again without the size parameter otherwise -fail.
204 if ((flags & RTE_MEMZONE_SIZE_HINT_ONLY) &&
205 ((flags & RTE_MEMZONE_1GB) || (flags & RTE_MEMZONE_2MB)))
206 return rte_memzone_reserve_aligned(name, len - align,
207 socket_id, 0, align);
209 RTE_LOG(ERR, EAL, "%s(): No appropriate segment found\n", __func__);
214 /* get offset needed to adjust alignment */
215 addr_offset = (uintptr_t) RTE_PTR_SUB(
216 RTE_ALIGN_CEIL(free_memseg[memseg_idx].addr, (uintptr_t) align),
217 (uintptr_t) free_memseg[memseg_idx].addr);
219 /* save aligned physical and virtual addresses */
220 memseg_physaddr = free_memseg[memseg_idx].phys_addr + addr_offset;
221 memseg_addr = RTE_PTR_ADD(free_memseg[memseg_idx].addr, addr_offset);
223 /* if we are looking for a biggest memzone */
224 if (requested_len == 0)
225 requested_len = memseg_len - addr_offset;
227 /* set length to correct value */
228 len = addr_offset + requested_len;
230 /* update our internal state */
231 free_memseg[memseg_idx].len -= len;
232 free_memseg[memseg_idx].phys_addr += len;
233 free_memseg[memseg_idx].addr =
234 (char *)free_memseg[memseg_idx].addr + len;
236 /* fill the zone in config */
237 struct rte_memzone *mz = &config->mem_config->memzone[memzone_idx++];
238 rte_snprintf(mz->name, sizeof(mz->name), "%s", name);
239 mz->phys_addr = memseg_physaddr;
240 mz->addr = memseg_addr;
241 mz->len = requested_len;
242 mz->hugepage_sz = free_memseg[memseg_idx].hugepage_sz;
243 mz->socket_id = free_memseg[memseg_idx].socket_id;
250 * Lookup for the memzone identified by the given name
252 const struct rte_memzone *
253 rte_memzone_lookup(const char *name)
255 const struct rte_mem_config *mcfg;
258 /* get pointer to global configuration */
259 mcfg = rte_eal_get_configuration()->mem_config;
262 * the algorithm is not optimal (linear), but there are few
263 * zones and this function should be called at init only
265 for (i = 0; i < RTE_MAX_MEMZONE && mcfg->memzone[i].addr != NULL; i++) {
266 if (!strncmp(name, mcfg->memzone[i].name, RTE_MEMZONE_NAMESIZE))
267 return &mcfg->memzone[i];
272 /* Dump all reserved memory zones on console */
274 rte_memzone_dump(void)
276 const struct rte_mem_config *mcfg;
279 /* get pointer to global configuration */
280 mcfg = rte_eal_get_configuration()->mem_config;
283 for (i=0; i<RTE_MAX_MEMZONE; i++) {
284 if (mcfg->memzone[i].addr == NULL)
286 printf("name:<%s>, phys:0x%"PRIx64", len:0x%"PRIx64""
287 ", virt:%p, socket_id:%"PRId32"\n",
288 mcfg->memzone[i].name,
289 mcfg->memzone[i].phys_addr,
290 mcfg->memzone[i].len,
291 mcfg->memzone[i].addr,
292 mcfg->memzone[i].socket_id);
297 * called by init: modify the free memseg list to have cache-aligned
298 * addresses and cache-aligned lengths
301 memseg_sanitize(struct rte_memseg *memseg)
307 phys_align = memseg->phys_addr & CACHE_LINE_MASK;
308 virt_align = (unsigned long)memseg->addr & CACHE_LINE_MASK;
311 * sanity check: phys_addr and addr must have the same
314 if (phys_align != virt_align)
317 /* memseg is really too small, don't bother with it */
318 if (memseg->len < (2 * CACHE_LINE_SIZE)) {
323 /* align start address */
324 off = (CACHE_LINE_SIZE - phys_align) & CACHE_LINE_MASK;
325 memseg->phys_addr += off;
326 memseg->addr = (char *)memseg->addr + off;
329 /* align end address */
330 memseg->len &= ~((uint64_t)CACHE_LINE_MASK);
336 * Init the memzone subsystem
339 rte_eal_memzone_init(void)
341 struct rte_config *config;
342 const struct rte_memseg *memseg;
345 /* secondary processes don't need to initialise anything */
346 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
349 /* get pointer to global configuration */
350 config = rte_eal_get_configuration();
352 memseg = rte_eal_get_physmem_layout();
353 if (memseg == NULL) {
354 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
358 /* duplicate the memsegs from config */
359 memcpy(free_memseg, memseg, sizeof(free_memseg));
361 /* make all zones cache-aligned */
362 for (i=0; i<RTE_MAX_MEMSEG; i++) {
363 if (free_memseg[i].addr == NULL)
365 if (memseg_sanitize(&free_memseg[i]) < 0) {
366 RTE_LOG(ERR, EAL, "%s(): Sanity check failed\n", __func__);
371 /* delete all zones */
373 memset(config->mem_config->memzone, 0, sizeof(config->mem_config->memzone));