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