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