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.
41 #include <sys/queue.h>
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_tailq.h>
48 #include <rte_eal_memconfig.h>
49 #include <rte_per_lcore.h>
50 #include <rte_errno.h>
51 #include <rte_string_fns.h>
52 #include <rte_common.h>
54 #include "eal_private.h"
56 /* internal copy of free memory segments */
57 static struct rte_memseg *free_memseg = NULL;
59 static inline const struct rte_memzone *
60 memzone_lookup_thread_unsafe(const char *name)
62 const struct rte_mem_config *mcfg;
65 /* get pointer to global configuration */
66 mcfg = rte_eal_get_configuration()->mem_config;
69 * the algorithm is not optimal (linear), but there are few
70 * zones and this function should be called at init only
72 for (i = 0; i < RTE_MAX_MEMZONE && mcfg->memzone[i].addr != NULL; i++) {
73 if (!strncmp(name, mcfg->memzone[i].name, RTE_MEMZONE_NAMESIZE))
74 return &mcfg->memzone[i];
81 * Return a pointer to a correctly filled memzone descriptor. If the
82 * allocation cannot be done, return NULL.
84 const struct rte_memzone *
85 rte_memzone_reserve(const char *name, size_t len, int socket_id,
88 return rte_memzone_reserve_aligned(name,
89 len, socket_id, flags, CACHE_LINE_SIZE);
93 * Helper function for memzone_reserve_aligned_thread_unsafe().
94 * Calculate address offset from the start of the segment.
95 * Align offset in that way that it satisfy istart alignmnet and
96 * buffer of the requested length would not cross specified boundary.
98 static inline phys_addr_t
99 align_phys_boundary(const struct rte_memseg *ms, size_t len, size_t align,
102 phys_addr_t addr_offset, bmask, end, start;
105 step = RTE_MAX(align, bound);
106 bmask = ~((phys_addr_t)bound - 1);
108 /* calculate offset to closest alignment */
109 start = RTE_ALIGN_CEIL(ms->phys_addr, align);
110 addr_offset = start - ms->phys_addr;
112 while (addr_offset + len < ms->len) {
114 /* check, do we meet boundary condition */
115 end = start + len - (len != 0);
116 if ((start & bmask) == (end & bmask))
119 /* calculate next offset */
120 start = RTE_ALIGN_CEIL(start + 1, step);
121 addr_offset = start - ms->phys_addr;
124 return (addr_offset);
127 static const struct rte_memzone *
128 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
129 int socket_id, unsigned flags, unsigned align, unsigned bound)
131 struct rte_mem_config *mcfg;
134 uint64_t addr_offset, seg_offset = 0;
135 size_t requested_len;
136 size_t memseg_len = 0;
137 phys_addr_t memseg_physaddr;
140 /* get pointer to global configuration */
141 mcfg = rte_eal_get_configuration()->mem_config;
143 /* no more room in config */
144 if (mcfg->memzone_idx >= RTE_MAX_MEMZONE) {
145 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
150 /* zone already exist */
151 if ((memzone_lookup_thread_unsafe(name)) != NULL) {
152 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
158 /* if alignment is not a power of two */
159 if (!rte_is_power_of_2(align)) {
160 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
166 /* alignment less than cache size is not allowed */
167 if (align < CACHE_LINE_SIZE)
168 align = CACHE_LINE_SIZE;
171 /* align length on cache boundary. Check for overflow before doing so */
172 if (len > SIZE_MAX - CACHE_LINE_MASK) {
173 rte_errno = EINVAL; /* requested size too big */
177 len += CACHE_LINE_MASK;
178 len &= ~((size_t) CACHE_LINE_MASK);
180 /* save minimal requested length */
181 requested_len = RTE_MAX((size_t)CACHE_LINE_SIZE, len);
183 /* check that boundary condition is valid */
185 (requested_len > bound || !rte_is_power_of_2(bound))) {
190 /* find the smallest segment matching requirements */
191 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
193 if (free_memseg[i].addr == NULL)
196 /* empty segment, skip it */
197 if (free_memseg[i].len == 0)
201 if (socket_id != SOCKET_ID_ANY &&
202 free_memseg[i].socket_id != SOCKET_ID_ANY &&
203 socket_id != free_memseg[i].socket_id)
207 * calculate offset to closest alignment that
208 * meets boundary conditions.
210 addr_offset = align_phys_boundary(free_memseg + i,
211 requested_len, align, bound);
214 if ((requested_len + addr_offset) > free_memseg[i].len)
217 /* check flags for hugepage sizes */
218 if ((flags & RTE_MEMZONE_2MB) &&
219 free_memseg[i].hugepage_sz == RTE_PGSIZE_1G )
221 if ((flags & RTE_MEMZONE_1GB) &&
222 free_memseg[i].hugepage_sz == RTE_PGSIZE_2M )
225 /* this segment is the best until now */
226 if (memseg_idx == -1) {
228 memseg_len = free_memseg[i].len;
229 seg_offset = addr_offset;
231 /* find the biggest contiguous zone */
233 if (free_memseg[i].len > memseg_len) {
235 memseg_len = free_memseg[i].len;
236 seg_offset = addr_offset;
240 * find the smallest (we already checked that current
241 * zone length is > len
243 else if (free_memseg[i].len + align < memseg_len ||
244 (free_memseg[i].len <= memseg_len + align &&
245 addr_offset < seg_offset)) {
247 memseg_len = free_memseg[i].len;
248 seg_offset = addr_offset;
252 /* no segment found */
253 if (memseg_idx == -1) {
255 * If RTE_MEMZONE_SIZE_HINT_ONLY flag is specified,
256 * try allocating again without the size parameter otherwise -fail.
258 if ((flags & RTE_MEMZONE_SIZE_HINT_ONLY) &&
259 ((flags & RTE_MEMZONE_1GB) || (flags & RTE_MEMZONE_2MB)))
260 return memzone_reserve_aligned_thread_unsafe(name,
261 len, socket_id, 0, align, bound);
267 /* save aligned physical and virtual addresses */
268 memseg_physaddr = free_memseg[memseg_idx].phys_addr + seg_offset;
269 memseg_addr = RTE_PTR_ADD(free_memseg[memseg_idx].addr,
270 (uintptr_t) seg_offset);
272 /* if we are looking for a biggest memzone */
275 requested_len = memseg_len - seg_offset;
277 requested_len = RTE_ALIGN_CEIL(memseg_physaddr + 1,
278 bound) - memseg_physaddr;
281 /* set length to correct value */
282 len = (size_t)seg_offset + requested_len;
284 /* update our internal state */
285 free_memseg[memseg_idx].len -= len;
286 free_memseg[memseg_idx].phys_addr += len;
287 free_memseg[memseg_idx].addr =
288 (char *)free_memseg[memseg_idx].addr + len;
290 /* fill the zone in config */
291 struct rte_memzone *mz = &mcfg->memzone[mcfg->memzone_idx++];
292 snprintf(mz->name, sizeof(mz->name), "%s", name);
293 mz->phys_addr = memseg_physaddr;
294 mz->addr = memseg_addr;
295 mz->len = requested_len;
296 mz->hugepage_sz = free_memseg[memseg_idx].hugepage_sz;
297 mz->socket_id = free_memseg[memseg_idx].socket_id;
299 mz->memseg_id = memseg_idx;
305 * Return a pointer to a correctly filled memzone descriptor (with a
306 * specified alignment). If the allocation cannot be done, return NULL.
308 const struct rte_memzone *
309 rte_memzone_reserve_aligned(const char *name, size_t len,
310 int socket_id, unsigned flags, unsigned align)
312 struct rte_mem_config *mcfg;
313 const struct rte_memzone *mz = NULL;
315 /* both sizes cannot be explicitly called for */
316 if ((flags & RTE_MEMZONE_1GB) && (flags & RTE_MEMZONE_2MB)) {
321 /* get pointer to global configuration */
322 mcfg = rte_eal_get_configuration()->mem_config;
324 rte_rwlock_write_lock(&mcfg->mlock);
326 mz = memzone_reserve_aligned_thread_unsafe(
327 name, len, socket_id, flags, align, 0);
329 rte_rwlock_write_unlock(&mcfg->mlock);
335 * Return a pointer to a correctly filled memzone descriptor (with a
336 * specified alignment and boundary).
337 * If the allocation cannot be done, return NULL.
339 const struct rte_memzone *
340 rte_memzone_reserve_bounded(const char *name, size_t len,
341 int socket_id, unsigned flags, unsigned align, unsigned bound)
343 struct rte_mem_config *mcfg;
344 const struct rte_memzone *mz = NULL;
346 /* both sizes cannot be explicitly called for */
347 if ((flags & RTE_MEMZONE_1GB) && (flags & RTE_MEMZONE_2MB)) {
352 /* get pointer to global configuration */
353 mcfg = rte_eal_get_configuration()->mem_config;
355 rte_rwlock_write_lock(&mcfg->mlock);
357 mz = memzone_reserve_aligned_thread_unsafe(
358 name, len, socket_id, flags, align, bound);
360 rte_rwlock_write_unlock(&mcfg->mlock);
367 * Lookup for the memzone identified by the given name
369 const struct rte_memzone *
370 rte_memzone_lookup(const char *name)
372 struct rte_mem_config *mcfg;
373 const struct rte_memzone *memzone = NULL;
375 mcfg = rte_eal_get_configuration()->mem_config;
377 rte_rwlock_read_lock(&mcfg->mlock);
379 memzone = memzone_lookup_thread_unsafe(name);
381 rte_rwlock_read_unlock(&mcfg->mlock);
386 /* Dump all reserved memory zones on console */
388 rte_memzone_dump(FILE *f)
390 struct rte_mem_config *mcfg;
393 /* get pointer to global configuration */
394 mcfg = rte_eal_get_configuration()->mem_config;
396 rte_rwlock_read_lock(&mcfg->mlock);
398 for (i=0; i<RTE_MAX_MEMZONE; i++) {
399 if (mcfg->memzone[i].addr == NULL)
401 fprintf(f, "Zone %u: name:<%s>, phys:0x%"PRIx64", len:0x%zx"
402 ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
403 mcfg->memzone[i].name,
404 mcfg->memzone[i].phys_addr,
405 mcfg->memzone[i].len,
406 mcfg->memzone[i].addr,
407 mcfg->memzone[i].socket_id,
408 mcfg->memzone[i].flags);
410 rte_rwlock_read_unlock(&mcfg->mlock);
414 * called by init: modify the free memseg list to have cache-aligned
415 * addresses and cache-aligned lengths
418 memseg_sanitize(struct rte_memseg *memseg)
424 phys_align = memseg->phys_addr & CACHE_LINE_MASK;
425 virt_align = (unsigned long)memseg->addr & CACHE_LINE_MASK;
428 * sanity check: phys_addr and addr must have the same
431 if (phys_align != virt_align)
434 /* memseg is really too small, don't bother with it */
435 if (memseg->len < (2 * CACHE_LINE_SIZE)) {
440 /* align start address */
441 off = (CACHE_LINE_SIZE - phys_align) & CACHE_LINE_MASK;
442 memseg->phys_addr += off;
443 memseg->addr = (char *)memseg->addr + off;
446 /* align end address */
447 memseg->len &= ~((uint64_t)CACHE_LINE_MASK);
453 * Init the memzone subsystem
456 rte_eal_memzone_init(void)
458 struct rte_mem_config *mcfg;
459 const struct rte_memseg *memseg;
462 /* get pointer to global configuration */
463 mcfg = rte_eal_get_configuration()->mem_config;
465 /* mirror the runtime memsegs from config */
466 free_memseg = mcfg->free_memseg;
468 /* secondary processes don't need to initialise anything */
469 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
472 memseg = rte_eal_get_physmem_layout();
473 if (memseg == NULL) {
474 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
478 rte_rwlock_write_lock(&mcfg->mlock);
480 /* fill in uninitialized free_memsegs */
481 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
482 if (memseg[i].addr == NULL)
484 if (free_memseg[i].addr != NULL)
486 memcpy(&free_memseg[i], &memseg[i], sizeof(struct rte_memseg));
489 /* make all zones cache-aligned */
490 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
491 if (free_memseg[i].addr == NULL)
493 if (memseg_sanitize(&free_memseg[i]) < 0) {
494 RTE_LOG(ERR, EAL, "%s(): Sanity check failed\n", __func__);
495 rte_rwlock_write_unlock(&mcfg->mlock);
500 /* delete all zones */
501 mcfg->memzone_idx = 0;
502 memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
504 rte_rwlock_write_unlock(&mcfg->mlock);
509 /* Walk all reserved memory zones */
510 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
513 struct rte_mem_config *mcfg;
516 mcfg = rte_eal_get_configuration()->mem_config;
518 rte_rwlock_read_lock(&mcfg->mlock);
519 for (i=0; i<RTE_MAX_MEMZONE; i++) {
520 if (mcfg->memzone[i].addr != NULL)
521 (*func)(&mcfg->memzone[i], arg);
523 rte_rwlock_read_unlock(&mcfg->mlock);