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