first public release
[dpdk.git] / lib / librte_eal / common / eal_common_memzone.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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.
20  * 
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.
32  * 
33  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <stdarg.h>
40 #include <inttypes.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <sys/queue.h>
44
45 #include <rte_log.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_tailq.h>
49 #include <rte_eal.h>
50 #include <rte_per_lcore.h>
51 #include <rte_errno.h>
52 #include <rte_string_fns.h>
53 #include <rte_common.h>
54
55 #include "eal_private.h"
56
57 /* internal copy of free memory segments */
58 static struct rte_memseg free_memseg[RTE_MAX_MEMSEG];
59
60 /* pointer to last reserved memzone */
61 static unsigned memzone_idx;
62
63 /*
64  * Return a pointer to a correctly filled memzone descriptor. If the
65  * allocation cannot be done, return NULL.
66  */
67 const struct rte_memzone *
68 rte_memzone_reserve(const char *name, uint64_t len, int socket_id,
69                       unsigned flags)
70 {
71         return rte_memzone_reserve_aligned(name,
72                         len, socket_id, flags, CACHE_LINE_SIZE);
73 }
74
75 /*
76  * Return a pointer to a correctly filled memzone descriptor (with a
77  * specified alignment). If the allocation cannot be done, return NULL.
78  */
79 const struct rte_memzone *
80 rte_memzone_reserve_aligned(const char *name, uint64_t len,
81                 int socket_id, unsigned flags, unsigned align)
82 {
83         struct rte_config *config;
84         unsigned i = 0;
85         int memseg_idx = -1;
86         uint64_t requested_len;
87         uint64_t memseg_len = 0;
88         phys_addr_t memseg_physaddr;
89         void *memseg_addr;
90         uintptr_t addr_offset;
91
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;
96                 return NULL;
97         }
98
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__,
102                                 align);
103                 rte_errno = EINVAL;
104                 return NULL;
105         }
106
107         /* alignment less than cache size is not allowed */
108         if (align < CACHE_LINE_SIZE)
109                 align = CACHE_LINE_SIZE;
110
111         /* get pointer to global configuration */
112         config = rte_eal_get_configuration();
113
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__);
117                 rte_errno = ENOSPC;
118                 return NULL;
119         }
120
121         /* both sizes cannot be explicitly called for */
122         if ((flags & RTE_MEMZONE_1GB) && (flags & RTE_MEMZONE_2MB)) {
123                 rte_errno = EINVAL;
124                 return NULL;
125         }
126
127         /* zone already exist */
128         if (rte_memzone_lookup(name) != NULL) {
129                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
130                         __func__, name);
131                 rte_errno = EEXIST;
132                 return NULL;
133         }
134
135         /* align length on cache boundary */
136         len += CACHE_LINE_MASK;
137         len &= ~((uint64_t)CACHE_LINE_MASK);
138
139
140
141         /* save requested length */
142         requested_len = len;
143
144         /* reserve extra space for future alignment */
145         if (len)
146                 len += align;
147
148         /* find the smallest segment matching requirements */
149         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
150
151                 /* last segment */
152                 if (free_memseg[i].addr == NULL)
153                         break;
154
155                 /* empty segment, skip it */
156                 if (free_memseg[i].len == 0)
157                         continue;
158
159                 /* bad socket ID */
160                 if (socket_id != SOCKET_ID_ANY &&
161                     socket_id != free_memseg[i].socket_id)
162                         continue;
163
164                 /* check len */
165                 if (len != 0 && len > free_memseg[i].len)
166                         continue;
167
168                 /* check flags for hugepage sizes */
169                 if ((flags & RTE_MEMZONE_2MB) &&
170                                 free_memseg[i].hugepage_sz == RTE_PGSIZE_1G )
171                         continue;
172                 if ((flags & RTE_MEMZONE_1GB) &&
173                                 free_memseg[i].hugepage_sz == RTE_PGSIZE_2M )
174                         continue;
175
176                 /* this segment is the best until now */
177                 if (memseg_idx == -1) {
178                         memseg_idx = i;
179                         memseg_len = free_memseg[i].len;
180                 }
181                 /* find the biggest contiguous zone */
182                 else if (len == 0) {
183                         if (free_memseg[i].len > memseg_len) {
184                                 memseg_idx = i;
185                                 memseg_len = free_memseg[i].len;
186                         }
187                 }
188                 /*
189                  * find the smallest (we already checked that current
190                  * zone length is > len
191                  */
192                 else if (free_memseg[i].len < memseg_len) {
193                         memseg_idx = i;
194                         memseg_len = free_memseg[i].len;
195                 }
196         }
197
198         /* no segment found */
199         if (memseg_idx == -1) {
200                 /*
201                  * If RTE_MEMZONE_SIZE_HINT_ONLY flag is specified,
202                  * try allocating again without the size parameter otherwise -fail.
203                  */
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);
208
209                 RTE_LOG(ERR, EAL, "%s(): No appropriate segment found\n", __func__);
210                 rte_errno = ENOMEM;
211                 return NULL;
212         }
213
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);
218
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);
222
223         /* if we are looking for a biggest memzone */
224         if (requested_len == 0)
225                 requested_len = memseg_len - addr_offset;
226
227         /* set length to correct value */
228         len = addr_offset + requested_len;
229
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;
235
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;
244         mz->flags = 0;
245
246         return mz;
247 }
248
249 /*
250  * Lookup for the memzone identified by the given name
251  */
252 const struct rte_memzone *
253 rte_memzone_lookup(const char *name)
254 {
255         const struct rte_mem_config *mcfg;
256         unsigned i = 0;
257
258         /* get pointer to global configuration */
259         mcfg = rte_eal_get_configuration()->mem_config;
260
261         /*
262          * the algorithm is not optimal (linear), but there are few
263          * zones and this function should be called at init only
264          */
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];
268         }
269         return NULL;
270 }
271
272 /* Dump all reserved memory zones on console */
273 void
274 rte_memzone_dump(void)
275 {
276         const struct rte_mem_config *mcfg;
277         unsigned i = 0;
278
279         /* get pointer to global configuration */
280         mcfg = rte_eal_get_configuration()->mem_config;
281
282         /* dump all zones */
283         for (i=0; i<RTE_MAX_MEMZONE; i++) {
284                 if (mcfg->memzone[i].addr == NULL)
285                         break;
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);
293         }
294 }
295
296 /*
297  * called by init: modify the free memseg list to have cache-aligned
298  * addresses and cache-aligned lengths
299  */
300 static int
301 memseg_sanitize(struct rte_memseg *memseg)
302 {
303         unsigned phys_align;
304         unsigned virt_align;
305         unsigned off;
306
307         phys_align = memseg->phys_addr & CACHE_LINE_MASK;
308         virt_align = (unsigned long)memseg->addr & CACHE_LINE_MASK;
309
310         /*
311          * sanity check: phys_addr and addr must have the same
312          * alignment
313          */
314         if (phys_align != virt_align)
315                 return -1;
316
317         /* memseg is really too small, don't bother with it */
318         if (memseg->len < (2 * CACHE_LINE_SIZE)) {
319                 memseg->len = 0;
320                 return 0;
321         }
322
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;
327         memseg->len -= off;
328
329         /* align end address */
330         memseg->len &= ~((uint64_t)CACHE_LINE_MASK);
331
332         return 0;
333 }
334
335 /*
336  * Init the memzone subsystem
337  */
338 int
339 rte_eal_memzone_init(void)
340 {
341         struct rte_config *config;
342         const struct rte_memseg *memseg;
343         unsigned i = 0;
344
345         /* secondary processes don't need to initialise anything */
346         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
347                 return 0;
348
349         /* get pointer to global configuration */
350         config = rte_eal_get_configuration();
351
352         memseg = rte_eal_get_physmem_layout();
353         if (memseg == NULL) {
354                 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
355                 return -1;
356         }
357
358         /* duplicate the memsegs from config */
359         memcpy(free_memseg, memseg, sizeof(free_memseg));
360
361         /* make all zones cache-aligned */
362         for (i=0; i<RTE_MAX_MEMSEG; i++) {
363                 if (free_memseg[i].addr == NULL)
364                         break;
365                 if (memseg_sanitize(&free_memseg[i]) < 0) {
366                         RTE_LOG(ERR, EAL, "%s(): Sanity check failed\n", __func__);
367                         return -1;
368                 }
369         }
370
371         /* delete all zones */
372         memzone_idx = 0;
373         memset(config->mem_config->memzone, 0, sizeof(config->mem_config->memzone));
374
375         return 0;
376 }