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.
42 #include <sys/queue.h>
45 #include <rte_memory.h>
46 #include <rte_memzone.h>
47 #include <rte_tailq.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[RTE_MAX_MEMSEG];
59 /* pointer to last reserved memzone */
60 static unsigned memzone_idx;
63 * Return a pointer to a correctly filled memzone descriptor. If the
64 * allocation cannot be done, return NULL.
66 const struct rte_memzone *
67 rte_memzone_reserve(const char *name, uint64_t len, int socket_id,
70 return rte_memzone_reserve_aligned(name,
71 len, socket_id, flags, CACHE_LINE_SIZE);
75 * Return a pointer to a correctly filled memzone descriptor (with a
76 * specified alignment). If the allocation cannot be done, return NULL.
78 const struct rte_memzone *
79 rte_memzone_reserve_aligned(const char *name, uint64_t len,
80 int socket_id, unsigned flags, unsigned align)
82 struct rte_config *config;
85 uint64_t requested_len;
86 uint64_t memseg_len = 0;
87 phys_addr_t memseg_physaddr;
89 uintptr_t addr_offset;
91 /* if secondary processes return error */
92 if (rte_eal_process_type() == RTE_PROC_SECONDARY){
93 RTE_LOG(ERR, EAL, "%s(): Not allowed in secondary process\n", __func__);
94 rte_errno = E_RTE_SECONDARY;
98 /* if alignment is not a power of two */
99 if (!rte_is_power_of_2(align)) {
100 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
106 /* alignment less than cache size is not allowed */
107 if (align < CACHE_LINE_SIZE)
108 align = CACHE_LINE_SIZE;
110 /* get pointer to global configuration */
111 config = rte_eal_get_configuration();
113 /* no more room in config */
114 if (memzone_idx >= RTE_MAX_MEMZONE) {
115 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
120 /* both sizes cannot be explicitly called for */
121 if ((flags & RTE_MEMZONE_1GB) && (flags & RTE_MEMZONE_2MB)) {
126 /* zone already exist */
127 if (rte_memzone_lookup(name) != NULL) {
128 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
134 /* align length on cache boundary */
135 len += CACHE_LINE_MASK;
136 len &= ~((uint64_t)CACHE_LINE_MASK);
140 /* save requested length */
143 /* reserve extra space for future alignment */
147 /* find the smallest segment matching requirements */
148 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
151 if (free_memseg[i].addr == NULL)
154 /* empty segment, skip it */
155 if (free_memseg[i].len == 0)
159 if (socket_id != SOCKET_ID_ANY &&
160 socket_id != free_memseg[i].socket_id)
164 if (len != 0 && len > free_memseg[i].len)
167 /* check flags for hugepage sizes */
168 if ((flags & RTE_MEMZONE_2MB) &&
169 free_memseg[i].hugepage_sz == RTE_PGSIZE_1G )
171 if ((flags & RTE_MEMZONE_1GB) &&
172 free_memseg[i].hugepage_sz == RTE_PGSIZE_2M )
175 /* this segment is the best until now */
176 if (memseg_idx == -1) {
178 memseg_len = free_memseg[i].len;
180 /* find the biggest contiguous zone */
182 if (free_memseg[i].len > memseg_len) {
184 memseg_len = free_memseg[i].len;
188 * find the smallest (we already checked that current
189 * zone length is > len
191 else if (free_memseg[i].len < memseg_len) {
193 memseg_len = free_memseg[i].len;
197 /* no segment found */
198 if (memseg_idx == -1) {
200 * If RTE_MEMZONE_SIZE_HINT_ONLY flag is specified,
201 * try allocating again without the size parameter otherwise -fail.
203 if ((flags & RTE_MEMZONE_SIZE_HINT_ONLY) &&
204 ((flags & RTE_MEMZONE_1GB) || (flags & RTE_MEMZONE_2MB)))
205 return rte_memzone_reserve_aligned(name, len - align,
206 socket_id, 0, align);
208 RTE_LOG(ERR, EAL, "%s(): No appropriate segment found\n", __func__);
213 /* get offset needed to adjust alignment */
214 addr_offset = (uintptr_t) RTE_PTR_SUB(
215 RTE_ALIGN_CEIL(free_memseg[memseg_idx].addr, (uintptr_t) align),
216 (uintptr_t) free_memseg[memseg_idx].addr);
218 /* save aligned physical and virtual addresses */
219 memseg_physaddr = free_memseg[memseg_idx].phys_addr + addr_offset;
220 memseg_addr = RTE_PTR_ADD(free_memseg[memseg_idx].addr, addr_offset);
222 /* if we are looking for a biggest memzone */
223 if (requested_len == 0)
224 requested_len = memseg_len - addr_offset;
226 /* set length to correct value */
227 len = addr_offset + requested_len;
229 /* update our internal state */
230 free_memseg[memseg_idx].len -= len;
231 free_memseg[memseg_idx].phys_addr += len;
232 free_memseg[memseg_idx].addr =
233 (char *)free_memseg[memseg_idx].addr + len;
235 /* fill the zone in config */
236 struct rte_memzone *mz = &config->mem_config->memzone[memzone_idx++];
237 rte_snprintf(mz->name, sizeof(mz->name), "%s", name);
238 mz->phys_addr = memseg_physaddr;
239 mz->addr = memseg_addr;
240 mz->len = requested_len;
241 mz->hugepage_sz = free_memseg[memseg_idx].hugepage_sz;
242 mz->socket_id = free_memseg[memseg_idx].socket_id;
249 * Lookup for the memzone identified by the given name
251 const struct rte_memzone *
252 rte_memzone_lookup(const char *name)
254 const struct rte_mem_config *mcfg;
257 /* get pointer to global configuration */
258 mcfg = rte_eal_get_configuration()->mem_config;
261 * the algorithm is not optimal (linear), but there are few
262 * zones and this function should be called at init only
264 for (i = 0; i < RTE_MAX_MEMZONE && mcfg->memzone[i].addr != NULL; i++) {
265 if (!strncmp(name, mcfg->memzone[i].name, RTE_MEMZONE_NAMESIZE))
266 return &mcfg->memzone[i];
271 /* Dump all reserved memory zones on console */
273 rte_memzone_dump(void)
275 const struct rte_mem_config *mcfg;
278 /* get pointer to global configuration */
279 mcfg = rte_eal_get_configuration()->mem_config;
282 for (i=0; i<RTE_MAX_MEMZONE; i++) {
283 if (mcfg->memzone[i].addr == NULL)
285 printf("name:<%s>, phys:0x%"PRIx64", len:0x%"PRIx64""
286 ", virt:%p, socket_id:%"PRId32"\n",
287 mcfg->memzone[i].name,
288 mcfg->memzone[i].phys_addr,
289 mcfg->memzone[i].len,
290 mcfg->memzone[i].addr,
291 mcfg->memzone[i].socket_id);
296 * called by init: modify the free memseg list to have cache-aligned
297 * addresses and cache-aligned lengths
300 memseg_sanitize(struct rte_memseg *memseg)
306 phys_align = memseg->phys_addr & CACHE_LINE_MASK;
307 virt_align = (unsigned long)memseg->addr & CACHE_LINE_MASK;
310 * sanity check: phys_addr and addr must have the same
313 if (phys_align != virt_align)
316 /* memseg is really too small, don't bother with it */
317 if (memseg->len < (2 * CACHE_LINE_SIZE)) {
322 /* align start address */
323 off = (CACHE_LINE_SIZE - phys_align) & CACHE_LINE_MASK;
324 memseg->phys_addr += off;
325 memseg->addr = (char *)memseg->addr + off;
328 /* align end address */
329 memseg->len &= ~((uint64_t)CACHE_LINE_MASK);
335 * Init the memzone subsystem
338 rte_eal_memzone_init(void)
340 struct rte_config *config;
341 const struct rte_memseg *memseg;
344 /* secondary processes don't need to initialise anything */
345 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
348 /* get pointer to global configuration */
349 config = rte_eal_get_configuration();
351 memseg = rte_eal_get_physmem_layout();
352 if (memseg == NULL) {
353 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
357 /* duplicate the memsegs from config */
358 memcpy(free_memseg, memseg, sizeof(free_memseg));
360 /* make all zones cache-aligned */
361 for (i=0; i<RTE_MAX_MEMSEG; i++) {
362 if (free_memseg[i].addr == NULL)
364 if (memseg_sanitize(&free_memseg[i]) < 0) {
365 RTE_LOG(ERR, EAL, "%s(): Sanity check failed\n", __func__);
370 /* delete all zones */
372 memset(config->mem_config->memzone, 0, sizeof(config->mem_config->memzone));