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);
92 static const struct rte_memzone *
93 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
94 int socket_id, unsigned flags, unsigned align)
96 struct rte_mem_config *mcfg;
100 size_t requested_len;
101 size_t memseg_len = 0;
102 phys_addr_t memseg_physaddr;
105 /* get pointer to global configuration */
106 mcfg = rte_eal_get_configuration()->mem_config;
108 /* no more room in config */
109 if (mcfg->memzone_idx >= RTE_MAX_MEMZONE) {
110 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
115 /* zone already exist */
116 if ((memzone_lookup_thread_unsafe(name)) != NULL) {
117 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
123 /* align length on cache boundary. Check for overflow before doing so */
124 if (len > SIZE_MAX - CACHE_LINE_MASK) {
125 rte_errno = EINVAL; /* requested size too big */
128 len += CACHE_LINE_MASK;
129 len &= ~((size_t) CACHE_LINE_MASK);
131 /* save requested length */
134 /* reserve extra space for future alignment */
138 /* find the smallest segment matching requirements */
139 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
141 if (free_memseg[i].addr == NULL)
144 /* empty segment, skip it */
145 if (free_memseg[i].len == 0)
149 if (socket_id != SOCKET_ID_ANY &&
150 socket_id != free_memseg[i].socket_id)
154 if (len != 0 && len > free_memseg[i].len)
157 /* check flags for hugepage sizes */
158 if ((flags & RTE_MEMZONE_2MB) &&
159 free_memseg[i].hugepage_sz == RTE_PGSIZE_1G )
161 if ((flags & RTE_MEMZONE_1GB) &&
162 free_memseg[i].hugepage_sz == RTE_PGSIZE_2M )
165 /* this segment is the best until now */
166 if (memseg_idx == -1) {
168 memseg_len = free_memseg[i].len;
170 /* find the biggest contiguous zone */
172 if (free_memseg[i].len > memseg_len) {
174 memseg_len = free_memseg[i].len;
178 * find the smallest (we already checked that current
179 * zone length is > len
181 else if (free_memseg[i].len < memseg_len) {
183 memseg_len = free_memseg[i].len;
187 /* no segment found */
188 if (memseg_idx == -1) {
190 * If RTE_MEMZONE_SIZE_HINT_ONLY flag is specified,
191 * try allocating again without the size parameter otherwise -fail.
193 if ((flags & RTE_MEMZONE_SIZE_HINT_ONLY) &&
194 ((flags & RTE_MEMZONE_1GB) || (flags & RTE_MEMZONE_2MB)))
195 return memzone_reserve_aligned_thread_unsafe(name, len - align,
196 socket_id, 0, align);
198 RTE_LOG(ERR, EAL, "%s(%s, %zu, %d): "
199 "No appropriate segment found\n",
200 __func__, name, requested_len, socket_id);
205 /* get offset needed to adjust alignment */
206 addr_offset = RTE_ALIGN_CEIL(free_memseg[memseg_idx].phys_addr, align) -
207 free_memseg[memseg_idx].phys_addr;
209 /* save aligned physical and virtual addresses */
210 memseg_physaddr = free_memseg[memseg_idx].phys_addr + addr_offset;
211 memseg_addr = RTE_PTR_ADD(free_memseg[memseg_idx].addr,
212 (uintptr_t) addr_offset);
214 /* if we are looking for a biggest memzone */
215 if (requested_len == 0)
216 requested_len = memseg_len - addr_offset;
218 /* set length to correct value */
219 len = (size_t)addr_offset + requested_len;
221 /* update our internal state */
222 free_memseg[memseg_idx].len -= len;
223 free_memseg[memseg_idx].phys_addr += len;
224 free_memseg[memseg_idx].addr =
225 (char *)free_memseg[memseg_idx].addr + len;
227 /* fill the zone in config */
228 struct rte_memzone *mz = &mcfg->memzone[mcfg->memzone_idx++];
229 rte_snprintf(mz->name, sizeof(mz->name), "%s", name);
230 mz->phys_addr = memseg_physaddr;
231 mz->addr = memseg_addr;
232 mz->len = requested_len;
233 mz->hugepage_sz = free_memseg[memseg_idx].hugepage_sz;
234 mz->socket_id = free_memseg[memseg_idx].socket_id;
241 * Return a pointer to a correctly filled memzone descriptor (with a
242 * specified alignment). If the allocation cannot be done, return NULL.
244 const struct rte_memzone *
245 rte_memzone_reserve_aligned(const char *name, size_t len,
246 int socket_id, unsigned flags, unsigned align)
248 struct rte_mem_config *mcfg;
249 const struct rte_memzone *mz = NULL;
251 /* both sizes cannot be explicitly called for */
252 if ((flags & RTE_MEMZONE_1GB) && (flags & RTE_MEMZONE_2MB)) {
257 /* if alignment is not a power of two */
258 if (!rte_is_power_of_2(align)) {
259 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
265 /* alignment less than cache size is not allowed */
266 if (align < CACHE_LINE_SIZE)
267 align = CACHE_LINE_SIZE;
269 /* get pointer to global configuration */
270 mcfg = rte_eal_get_configuration()->mem_config;
272 rte_rwlock_write_lock(&mcfg->mlock);
274 mz = memzone_reserve_aligned_thread_unsafe(
275 name, len, socket_id, flags, align);
277 rte_rwlock_write_unlock(&mcfg->mlock);
283 * Lookup for the memzone identified by the given name
285 const struct rte_memzone *
286 rte_memzone_lookup(const char *name)
288 struct rte_mem_config *mcfg;
289 const struct rte_memzone *memzone = NULL;
291 mcfg = rte_eal_get_configuration()->mem_config;
293 rte_rwlock_read_lock(&mcfg->mlock);
295 memzone = memzone_lookup_thread_unsafe(name);
297 rte_rwlock_read_unlock(&mcfg->mlock);
302 /* Dump all reserved memory zones on console */
304 rte_memzone_dump(void)
306 struct rte_mem_config *mcfg;
309 /* get pointer to global configuration */
310 mcfg = rte_eal_get_configuration()->mem_config;
312 rte_rwlock_read_lock(&mcfg->mlock);
314 for (i=0; i<RTE_MAX_MEMZONE; i++) {
315 if (mcfg->memzone[i].addr == NULL)
317 printf("Zone %u: name:<%s>, phys:0x%"PRIx64", len:0x%zx"
318 ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
319 mcfg->memzone[i].name,
320 mcfg->memzone[i].phys_addr,
321 mcfg->memzone[i].len,
322 mcfg->memzone[i].addr,
323 mcfg->memzone[i].socket_id,
324 mcfg->memzone[i].flags);
326 rte_rwlock_read_unlock(&mcfg->mlock);
330 * called by init: modify the free memseg list to have cache-aligned
331 * addresses and cache-aligned lengths
334 memseg_sanitize(struct rte_memseg *memseg)
340 phys_align = memseg->phys_addr & CACHE_LINE_MASK;
341 virt_align = (unsigned long)memseg->addr & CACHE_LINE_MASK;
344 * sanity check: phys_addr and addr must have the same
347 if (phys_align != virt_align)
350 /* memseg is really too small, don't bother with it */
351 if (memseg->len < (2 * CACHE_LINE_SIZE)) {
356 /* align start address */
357 off = (CACHE_LINE_SIZE - phys_align) & CACHE_LINE_MASK;
358 memseg->phys_addr += off;
359 memseg->addr = (char *)memseg->addr + off;
362 /* align end address */
363 memseg->len &= ~((uint64_t)CACHE_LINE_MASK);
369 * Init the memzone subsystem
372 rte_eal_memzone_init(void)
374 struct rte_mem_config *mcfg;
375 const struct rte_memseg *memseg;
378 /* get pointer to global configuration */
379 mcfg = rte_eal_get_configuration()->mem_config;
381 /* mirror the runtime memsegs from config */
382 free_memseg = mcfg->free_memseg;
384 /* secondary processes don't need to initialise anything */
385 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
388 memseg = rte_eal_get_physmem_layout();
389 if (memseg == NULL) {
390 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
394 rte_rwlock_write_lock(&mcfg->mlock);
396 /* duplicate the memsegs from config */
397 memcpy(free_memseg, memseg, sizeof(struct rte_memseg) * RTE_MAX_MEMSEG);
399 /* make all zones cache-aligned */
400 for (i=0; i<RTE_MAX_MEMSEG; i++) {
401 if (free_memseg[i].addr == NULL)
403 if (memseg_sanitize(&free_memseg[i]) < 0) {
404 RTE_LOG(ERR, EAL, "%s(): Sanity check failed\n", __func__);
405 rte_rwlock_write_unlock(&mcfg->mlock);
410 /* delete all zones */
411 mcfg->memzone_idx = 0;
412 memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
414 rte_rwlock_write_unlock(&mcfg->mlock);