tailq: remove unneeded inclusions
[dpdk.git] / lib / librte_eal / common / eal_common_memzone.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <stdarg.h>
38 #include <inttypes.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_log.h>
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_eal.h>
47 #include <rte_eal_memconfig.h>
48 #include <rte_per_lcore.h>
49 #include <rte_errno.h>
50 #include <rte_string_fns.h>
51 #include <rte_common.h>
52
53 #include "eal_private.h"
54
55 /* internal copy of free memory segments */
56 static struct rte_memseg *free_memseg = NULL;
57
58 static inline const struct rte_memzone *
59 memzone_lookup_thread_unsafe(const char *name)
60 {
61         const struct rte_mem_config *mcfg;
62         unsigned i = 0;
63
64         /* get pointer to global configuration */
65         mcfg = rte_eal_get_configuration()->mem_config;
66
67         /*
68          * the algorithm is not optimal (linear), but there are few
69          * zones and this function should be called at init only
70          */
71         for (i = 0; i < RTE_MAX_MEMZONE && mcfg->memzone[i].addr != NULL; i++) {
72                 if (!strncmp(name, mcfg->memzone[i].name, RTE_MEMZONE_NAMESIZE))
73                         return &mcfg->memzone[i];
74         }
75
76         return NULL;
77 }
78
79 /*
80  * Return a pointer to a correctly filled memzone descriptor. If the
81  * allocation cannot be done, return NULL.
82  */
83 const struct rte_memzone *
84 rte_memzone_reserve(const char *name, size_t len, int socket_id,
85                       unsigned flags)
86 {
87         return rte_memzone_reserve_aligned(name,
88                         len, socket_id, flags, RTE_CACHE_LINE_SIZE);
89 }
90
91 /*
92  * Helper function for memzone_reserve_aligned_thread_unsafe().
93  * Calculate address offset from the start of the segment.
94  * Align offset in that way that it satisfy istart alignmnet and
95  * buffer of the  requested length would not cross specified boundary.
96  */
97 static inline phys_addr_t
98 align_phys_boundary(const struct rte_memseg *ms, size_t len, size_t align,
99         size_t bound)
100 {
101         phys_addr_t addr_offset, bmask, end, start;
102         size_t step;
103
104         step = RTE_MAX(align, bound);
105         bmask = ~((phys_addr_t)bound - 1);
106
107         /* calculate offset to closest alignment */
108         start = RTE_ALIGN_CEIL(ms->phys_addr, align);
109         addr_offset = start - ms->phys_addr;
110
111         while (addr_offset + len < ms->len) {
112
113                 /* check, do we meet boundary condition */
114                 end = start + len - (len != 0);
115                 if ((start & bmask) == (end & bmask))
116                         break;
117
118                 /* calculate next offset */
119                 start = RTE_ALIGN_CEIL(start + 1, step);
120                 addr_offset = start - ms->phys_addr;
121         }
122
123         return (addr_offset);
124 }
125
126 static const struct rte_memzone *
127 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
128                 int socket_id, unsigned flags, unsigned align, unsigned bound)
129 {
130         struct rte_mem_config *mcfg;
131         unsigned i = 0;
132         int memseg_idx = -1;
133         uint64_t addr_offset, seg_offset = 0;
134         size_t requested_len;
135         size_t memseg_len = 0;
136         phys_addr_t memseg_physaddr;
137         void *memseg_addr;
138
139         /* get pointer to global configuration */
140         mcfg = rte_eal_get_configuration()->mem_config;
141
142         /* no more room in config */
143         if (mcfg->memzone_idx >= RTE_MAX_MEMZONE) {
144                 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
145                 rte_errno = ENOSPC;
146                 return NULL;
147         }
148
149         /* zone already exist */
150         if ((memzone_lookup_thread_unsafe(name)) != NULL) {
151                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
152                         __func__, name);
153                 rte_errno = EEXIST;
154                 return NULL;
155         }
156
157         /* if alignment is not a power of two */
158         if (align && !rte_is_power_of_2(align)) {
159                 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
160                                 align);
161                 rte_errno = EINVAL;
162                 return NULL;
163         }
164
165         /* alignment less than cache size is not allowed */
166         if (align < RTE_CACHE_LINE_SIZE)
167                 align = RTE_CACHE_LINE_SIZE;
168
169
170         /* align length on cache boundary. Check for overflow before doing so */
171         if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
172                 rte_errno = EINVAL; /* requested size too big */
173                 return NULL;
174         }
175
176         len += RTE_CACHE_LINE_MASK;
177         len &= ~((size_t) RTE_CACHE_LINE_MASK);
178
179         /* save minimal requested  length */
180         requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE,  len);
181
182         /* check that boundary condition is valid */
183         if (bound != 0 &&
184                         (requested_len > bound || !rte_is_power_of_2(bound))) {
185                 rte_errno = EINVAL;
186                 return NULL;
187         }
188
189         /* find the smallest segment matching requirements */
190         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
191                 /* last segment */
192                 if (free_memseg[i].addr == NULL)
193                         break;
194
195                 /* empty segment, skip it */
196                 if (free_memseg[i].len == 0)
197                         continue;
198
199                 /* bad socket ID */
200                 if (socket_id != SOCKET_ID_ANY &&
201                     free_memseg[i].socket_id != SOCKET_ID_ANY &&
202                     socket_id != free_memseg[i].socket_id)
203                         continue;
204
205                 /*
206                  * calculate offset to closest alignment that
207                  * meets boundary conditions.
208                  */
209                 addr_offset = align_phys_boundary(free_memseg + i,
210                         requested_len, align, bound);
211
212                 /* check len */
213                 if ((requested_len + addr_offset) > free_memseg[i].len)
214                         continue;
215
216                 /* check flags for hugepage sizes */
217                 if ((flags & RTE_MEMZONE_2MB) &&
218                                 free_memseg[i].hugepage_sz == RTE_PGSIZE_1G)
219                         continue;
220                 if ((flags & RTE_MEMZONE_1GB) &&
221                                 free_memseg[i].hugepage_sz == RTE_PGSIZE_2M)
222                         continue;
223                 if ((flags & RTE_MEMZONE_16MB) &&
224                                 free_memseg[i].hugepage_sz == RTE_PGSIZE_16G)
225                         continue;
226                 if ((flags & RTE_MEMZONE_16GB) &&
227                                 free_memseg[i].hugepage_sz == RTE_PGSIZE_16M)
228                         continue;
229
230                 /* this segment is the best until now */
231                 if (memseg_idx == -1) {
232                         memseg_idx = i;
233                         memseg_len = free_memseg[i].len;
234                         seg_offset = addr_offset;
235                 }
236                 /* find the biggest contiguous zone */
237                 else if (len == 0) {
238                         if (free_memseg[i].len > memseg_len) {
239                                 memseg_idx = i;
240                                 memseg_len = free_memseg[i].len;
241                                 seg_offset = addr_offset;
242                         }
243                 }
244                 /*
245                  * find the smallest (we already checked that current
246                  * zone length is > len
247                  */
248                 else if (free_memseg[i].len + align < memseg_len ||
249                                 (free_memseg[i].len <= memseg_len + align &&
250                                 addr_offset < seg_offset)) {
251                         memseg_idx = i;
252                         memseg_len = free_memseg[i].len;
253                         seg_offset = addr_offset;
254                 }
255         }
256
257         /* no segment found */
258         if (memseg_idx == -1) {
259                 /*
260                  * If RTE_MEMZONE_SIZE_HINT_ONLY flag is specified,
261                  * try allocating again without the size parameter otherwise -fail.
262                  */
263                 if ((flags & RTE_MEMZONE_SIZE_HINT_ONLY)  &&
264                     ((flags & RTE_MEMZONE_1GB) || (flags & RTE_MEMZONE_2MB)
265                 || (flags & RTE_MEMZONE_16MB) || (flags & RTE_MEMZONE_16GB)))
266                         return memzone_reserve_aligned_thread_unsafe(name,
267                                 len, socket_id, 0, align, bound);
268
269                 rte_errno = ENOMEM;
270                 return NULL;
271         }
272
273         /* save aligned physical and virtual addresses */
274         memseg_physaddr = free_memseg[memseg_idx].phys_addr + seg_offset;
275         memseg_addr = RTE_PTR_ADD(free_memseg[memseg_idx].addr,
276                         (uintptr_t) seg_offset);
277
278         /* if we are looking for a biggest memzone */
279         if (len == 0) {
280                 if (bound == 0)
281                         requested_len = memseg_len - seg_offset;
282                 else
283                         requested_len = RTE_ALIGN_CEIL(memseg_physaddr + 1,
284                                 bound) - memseg_physaddr;
285         }
286
287         /* set length to correct value */
288         len = (size_t)seg_offset + requested_len;
289
290         /* update our internal state */
291         free_memseg[memseg_idx].len -= len;
292         free_memseg[memseg_idx].phys_addr += len;
293         free_memseg[memseg_idx].addr =
294                 (char *)free_memseg[memseg_idx].addr + len;
295
296         /* fill the zone in config */
297         struct rte_memzone *mz = &mcfg->memzone[mcfg->memzone_idx++];
298         snprintf(mz->name, sizeof(mz->name), "%s", name);
299         mz->phys_addr = memseg_physaddr;
300         mz->addr = memseg_addr;
301         mz->len = requested_len;
302         mz->hugepage_sz = free_memseg[memseg_idx].hugepage_sz;
303         mz->socket_id = free_memseg[memseg_idx].socket_id;
304         mz->flags = 0;
305         mz->memseg_id = memseg_idx;
306
307         return mz;
308 }
309
310 /*
311  * Return a pointer to a correctly filled memzone descriptor (with a
312  * specified alignment). If the allocation cannot be done, return NULL.
313  */
314 const struct rte_memzone *
315 rte_memzone_reserve_aligned(const char *name, size_t len,
316                 int socket_id, unsigned flags, unsigned align)
317 {
318         struct rte_mem_config *mcfg;
319         const struct rte_memzone *mz = NULL;
320
321         /* both sizes cannot be explicitly called for */
322         if (((flags & RTE_MEMZONE_1GB) && (flags & RTE_MEMZONE_2MB))
323                 || ((flags & RTE_MEMZONE_16MB) && (flags & RTE_MEMZONE_16GB))) {
324                 rte_errno = EINVAL;
325                 return NULL;
326         }
327
328         /* get pointer to global configuration */
329         mcfg = rte_eal_get_configuration()->mem_config;
330
331         rte_rwlock_write_lock(&mcfg->mlock);
332
333         mz = memzone_reserve_aligned_thread_unsafe(
334                 name, len, socket_id, flags, align, 0);
335
336         rte_rwlock_write_unlock(&mcfg->mlock);
337
338         return mz;
339 }
340
341 /*
342  * Return a pointer to a correctly filled memzone descriptor (with a
343  * specified alignment and boundary).
344  * If the allocation cannot be done, return NULL.
345  */
346 const struct rte_memzone *
347 rte_memzone_reserve_bounded(const char *name, size_t len,
348                 int socket_id, unsigned flags, unsigned align, unsigned bound)
349 {
350         struct rte_mem_config *mcfg;
351         const struct rte_memzone *mz = NULL;
352
353         /* both sizes cannot be explicitly called for */
354         if (((flags & RTE_MEMZONE_1GB) && (flags & RTE_MEMZONE_2MB))
355                 || ((flags & RTE_MEMZONE_16MB) && (flags & RTE_MEMZONE_16GB))) {
356                 rte_errno = EINVAL;
357                 return NULL;
358         }
359
360         /* get pointer to global configuration */
361         mcfg = rte_eal_get_configuration()->mem_config;
362
363         rte_rwlock_write_lock(&mcfg->mlock);
364
365         mz = memzone_reserve_aligned_thread_unsafe(
366                 name, len, socket_id, flags, align, bound);
367
368         rte_rwlock_write_unlock(&mcfg->mlock);
369
370         return mz;
371 }
372
373
374 /*
375  * Lookup for the memzone identified by the given name
376  */
377 const struct rte_memzone *
378 rte_memzone_lookup(const char *name)
379 {
380         struct rte_mem_config *mcfg;
381         const struct rte_memzone *memzone = NULL;
382
383         mcfg = rte_eal_get_configuration()->mem_config;
384
385         rte_rwlock_read_lock(&mcfg->mlock);
386
387         memzone = memzone_lookup_thread_unsafe(name);
388
389         rte_rwlock_read_unlock(&mcfg->mlock);
390
391         return memzone;
392 }
393
394 /* Dump all reserved memory zones on console */
395 void
396 rte_memzone_dump(FILE *f)
397 {
398         struct rte_mem_config *mcfg;
399         unsigned i = 0;
400
401         /* get pointer to global configuration */
402         mcfg = rte_eal_get_configuration()->mem_config;
403
404         rte_rwlock_read_lock(&mcfg->mlock);
405         /* dump all zones */
406         for (i=0; i<RTE_MAX_MEMZONE; i++) {
407                 if (mcfg->memzone[i].addr == NULL)
408                         break;
409                 fprintf(f, "Zone %u: name:<%s>, phys:0x%"PRIx64", len:0x%zx"
410                        ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
411                        mcfg->memzone[i].name,
412                        mcfg->memzone[i].phys_addr,
413                        mcfg->memzone[i].len,
414                        mcfg->memzone[i].addr,
415                        mcfg->memzone[i].socket_id,
416                        mcfg->memzone[i].flags);
417         }
418         rte_rwlock_read_unlock(&mcfg->mlock);
419 }
420
421 /*
422  * called by init: modify the free memseg list to have cache-aligned
423  * addresses and cache-aligned lengths
424  */
425 static int
426 memseg_sanitize(struct rte_memseg *memseg)
427 {
428         unsigned phys_align;
429         unsigned virt_align;
430         unsigned off;
431
432         phys_align = memseg->phys_addr & RTE_CACHE_LINE_MASK;
433         virt_align = (unsigned long)memseg->addr & RTE_CACHE_LINE_MASK;
434
435         /*
436          * sanity check: phys_addr and addr must have the same
437          * alignment
438          */
439         if (phys_align != virt_align)
440                 return -1;
441
442         /* memseg is really too small, don't bother with it */
443         if (memseg->len < (2 * RTE_CACHE_LINE_SIZE)) {
444                 memseg->len = 0;
445                 return 0;
446         }
447
448         /* align start address */
449         off = (RTE_CACHE_LINE_SIZE - phys_align) & RTE_CACHE_LINE_MASK;
450         memseg->phys_addr += off;
451         memseg->addr = (char *)memseg->addr + off;
452         memseg->len -= off;
453
454         /* align end address */
455         memseg->len &= ~((uint64_t)RTE_CACHE_LINE_MASK);
456
457         return 0;
458 }
459
460 /*
461  * Init the memzone subsystem
462  */
463 int
464 rte_eal_memzone_init(void)
465 {
466         struct rte_mem_config *mcfg;
467         const struct rte_memseg *memseg;
468         unsigned i = 0;
469
470         /* get pointer to global configuration */
471         mcfg = rte_eal_get_configuration()->mem_config;
472
473         /* mirror the runtime memsegs from config */
474         free_memseg = mcfg->free_memseg;
475
476         /* secondary processes don't need to initialise anything */
477         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
478                 return 0;
479
480         memseg = rte_eal_get_physmem_layout();
481         if (memseg == NULL) {
482                 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
483                 return -1;
484         }
485
486         rte_rwlock_write_lock(&mcfg->mlock);
487
488         /* fill in uninitialized free_memsegs */
489         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
490                 if (memseg[i].addr == NULL)
491                         break;
492                 if (free_memseg[i].addr != NULL)
493                         continue;
494                 memcpy(&free_memseg[i], &memseg[i], sizeof(struct rte_memseg));
495         }
496
497         /* make all zones cache-aligned */
498         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
499                 if (free_memseg[i].addr == NULL)
500                         break;
501                 if (memseg_sanitize(&free_memseg[i]) < 0) {
502                         RTE_LOG(ERR, EAL, "%s(): Sanity check failed\n", __func__);
503                         rte_rwlock_write_unlock(&mcfg->mlock);
504                         return -1;
505                 }
506         }
507
508         /* delete all zones */
509         mcfg->memzone_idx = 0;
510         memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
511
512         rte_rwlock_write_unlock(&mcfg->mlock);
513
514         return 0;
515 }
516
517 /* Walk all reserved memory zones */
518 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
519                       void *arg)
520 {
521         struct rte_mem_config *mcfg;
522         unsigned i;
523
524         mcfg = rte_eal_get_configuration()->mem_config;
525
526         rte_rwlock_read_lock(&mcfg->mlock);
527         for (i=0; i<RTE_MAX_MEMZONE; i++) {
528                 if (mcfg->memzone[i].addr != NULL)
529                         (*func)(&mcfg->memzone[i], arg);
530         }
531         rte_rwlock_read_unlock(&mcfg->mlock);
532 }