f2415b60ca9882934081e2fdd1276a83f484f4a4
[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_2MB)
288                 size_mask |= RTE_PGSIZE_2M;
289         if (flags & RTE_MEMZONE_16MB)
290                 size_mask |= RTE_PGSIZE_16M;
291         if (flags & RTE_MEMZONE_1GB)
292                 size_mask |= RTE_PGSIZE_1G;
293         if (flags & RTE_MEMZONE_16GB)
294                 size_mask |= RTE_PGSIZE_16G;
295         if (!size_mask)
296                 size_mask = UINT64_MAX;
297
298         /* get pointer to global configuration */
299         mcfg = rte_eal_get_configuration()->mem_config;
300
301         rte_rwlock_write_lock(&mcfg->mlock);
302
303         mz = memzone_reserve_aligned_thread_unsafe(
304                 name, len, socket_id, size_mask, align, bound);
305
306         /*
307          * If we failed to allocate the requested page size, and the
308          * RTE_MEMZONE_SIZE_HINT_ONLY flag is specified, try allocating
309          * again.
310          */
311         if (!mz && rte_errno == ENOMEM && size_mask != UINT64_MAX &&
312             flags & RTE_MEMZONE_SIZE_HINT_ONLY) {
313                 mz = memzone_reserve_aligned_thread_unsafe(
314                         name, len, socket_id, UINT64_MAX, align, bound);
315         }
316
317         rte_rwlock_write_unlock(&mcfg->mlock);
318
319         return mz;
320 }
321
322 /*
323  * Return a pointer to a correctly filled memzone descriptor (with a
324  * specified alignment and boundary). If the allocation cannot be done,
325  * return NULL.
326  */
327 const struct rte_memzone *
328 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
329                             unsigned flags, unsigned align, unsigned bound)
330 {
331         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
332                                                align, bound);
333 }
334
335 /*
336  * Return a pointer to a correctly filled memzone descriptor (with a
337  * specified alignment). If the allocation cannot be done, return NULL.
338  */
339 const struct rte_memzone *
340 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
341                             unsigned flags, unsigned align)
342 {
343         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
344                                                align, 0);
345 }
346
347 /*
348  * Return a pointer to a correctly filled memzone descriptor. If the
349  * allocation cannot be done, return NULL.
350  */
351 const struct rte_memzone *
352 rte_memzone_reserve(const char *name, size_t len, int socket_id,
353                     unsigned flags)
354 {
355         return rte_memzone_reserve_thread_safe(name, len, socket_id,
356                                                flags, RTE_CACHE_LINE_SIZE, 0);
357 }
358
359 /*
360  * Lookup for the memzone identified by the given name
361  */
362 const struct rte_memzone *
363 rte_memzone_lookup(const char *name)
364 {
365         struct rte_mem_config *mcfg;
366         const struct rte_memzone *memzone = NULL;
367
368         mcfg = rte_eal_get_configuration()->mem_config;
369
370         rte_rwlock_read_lock(&mcfg->mlock);
371
372         memzone = memzone_lookup_thread_unsafe(name);
373
374         rte_rwlock_read_unlock(&mcfg->mlock);
375
376         return memzone;
377 }
378
379 /* Dump all reserved memory zones on console */
380 void
381 rte_memzone_dump(FILE *f)
382 {
383         struct rte_mem_config *mcfg;
384         unsigned i = 0;
385
386         /* get pointer to global configuration */
387         mcfg = rte_eal_get_configuration()->mem_config;
388
389         rte_rwlock_read_lock(&mcfg->mlock);
390         /* dump all zones */
391         for (i=0; i<RTE_MAX_MEMZONE; i++) {
392                 if (mcfg->memzone[i].addr == NULL)
393                         break;
394                 fprintf(f, "Zone %u: name:<%s>, phys:0x%"PRIx64", len:0x%zx"
395                        ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
396                        mcfg->memzone[i].name,
397                        mcfg->memzone[i].phys_addr,
398                        mcfg->memzone[i].len,
399                        mcfg->memzone[i].addr,
400                        mcfg->memzone[i].socket_id,
401                        mcfg->memzone[i].flags);
402         }
403         rte_rwlock_read_unlock(&mcfg->mlock);
404 }
405
406 /*
407  * called by init: modify the free memseg list to have cache-aligned
408  * addresses and cache-aligned lengths
409  */
410 static int
411 memseg_sanitize(struct rte_memseg *memseg)
412 {
413         unsigned phys_align;
414         unsigned virt_align;
415         unsigned off;
416
417         phys_align = memseg->phys_addr & RTE_CACHE_LINE_MASK;
418         virt_align = (unsigned long)memseg->addr & RTE_CACHE_LINE_MASK;
419
420         /*
421          * sanity check: phys_addr and addr must have the same
422          * alignment
423          */
424         if (phys_align != virt_align)
425                 return -1;
426
427         /* memseg is really too small, don't bother with it */
428         if (memseg->len < (2 * RTE_CACHE_LINE_SIZE)) {
429                 memseg->len = 0;
430                 return 0;
431         }
432
433         /* align start address */
434         off = (RTE_CACHE_LINE_SIZE - phys_align) & RTE_CACHE_LINE_MASK;
435         memseg->phys_addr += off;
436         memseg->addr = (char *)memseg->addr + off;
437         memseg->len -= off;
438
439         /* align end address */
440         memseg->len &= ~((uint64_t)RTE_CACHE_LINE_MASK);
441
442         return 0;
443 }
444
445 /*
446  * Init the memzone subsystem
447  */
448 int
449 rte_eal_memzone_init(void)
450 {
451         struct rte_mem_config *mcfg;
452         const struct rte_memseg *memseg;
453         unsigned i = 0;
454
455         /* get pointer to global configuration */
456         mcfg = rte_eal_get_configuration()->mem_config;
457
458         /* mirror the runtime memsegs from config */
459         free_memseg = mcfg->free_memseg;
460
461         /* secondary processes don't need to initialise anything */
462         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
463                 return 0;
464
465         memseg = rte_eal_get_physmem_layout();
466         if (memseg == NULL) {
467                 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
468                 return -1;
469         }
470
471         rte_rwlock_write_lock(&mcfg->mlock);
472
473         /* fill in uninitialized free_memsegs */
474         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
475                 if (memseg[i].addr == NULL)
476                         break;
477                 if (free_memseg[i].addr != NULL)
478                         continue;
479                 memcpy(&free_memseg[i], &memseg[i], sizeof(struct rte_memseg));
480         }
481
482         /* make all zones cache-aligned */
483         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
484                 if (free_memseg[i].addr == NULL)
485                         break;
486                 if (memseg_sanitize(&free_memseg[i]) < 0) {
487                         RTE_LOG(ERR, EAL, "%s(): Sanity check failed\n", __func__);
488                         rte_rwlock_write_unlock(&mcfg->mlock);
489                         return -1;
490                 }
491         }
492
493         /* delete all zones */
494         mcfg->memzone_idx = 0;
495         memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
496
497         rte_rwlock_write_unlock(&mcfg->mlock);
498
499         return 0;
500 }
501
502 /* Walk all reserved memory zones */
503 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
504                       void *arg)
505 {
506         struct rte_mem_config *mcfg;
507         unsigned i;
508
509         mcfg = rte_eal_get_configuration()->mem_config;
510
511         rte_rwlock_read_lock(&mcfg->mlock);
512         for (i=0; i<RTE_MAX_MEMZONE; i++) {
513                 if (mcfg->memzone[i].addr != NULL)
514                         (*func)(&mcfg->memzone[i], arg);
515         }
516         rte_rwlock_read_unlock(&mcfg->mlock);
517 }