7ef47081b705aa2103ed93e0e018ec616dfcbf62
[dpdk.git] / lib / librte_eal / common / eal_common_memzone.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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_eal_memconfig.h>
50 #include <rte_per_lcore.h>
51 #include <rte_errno.h>
52 #include <rte_string_fns.h>
53 #include <rte_common.h>
54
55 #include "eal_private.h"
56
57 /* internal copy of free memory segments */
58 static struct rte_memseg *free_memseg = NULL;
59
60 static inline const struct rte_memzone *
61 memzone_lookup_thread_unsafe(const char *name)
62 {
63         const struct rte_mem_config *mcfg;
64         unsigned i = 0;
65
66         /* get pointer to global configuration */
67         mcfg = rte_eal_get_configuration()->mem_config;
68
69         /*
70          * the algorithm is not optimal (linear), but there are few
71          * zones and this function should be called at init only
72          */
73         for (i = 0; i < RTE_MAX_MEMZONE && mcfg->memzone[i].addr != NULL; i++) {
74                 if (!strncmp(name, mcfg->memzone[i].name, RTE_MEMZONE_NAMESIZE)) 
75                         return &mcfg->memzone[i];
76         }
77
78         return NULL;
79 }
80
81 /*
82  * Return a pointer to a correctly filled memzone descriptor. If the
83  * allocation cannot be done, return NULL.
84  */
85 const struct rte_memzone *
86 rte_memzone_reserve(const char *name, size_t len, int socket_id,
87                       unsigned flags)
88 {
89         return rte_memzone_reserve_aligned(name,
90                         len, socket_id, flags, CACHE_LINE_SIZE);
91 }
92
93 static const struct rte_memzone *
94 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
95                 int socket_id, unsigned flags, unsigned align)
96 {
97         struct rte_mem_config *mcfg;
98         unsigned i = 0;
99         int memseg_idx = -1;
100         uint64_t addr_offset;
101         size_t requested_len;
102         size_t memseg_len = 0;
103         phys_addr_t memseg_physaddr;
104         void *memseg_addr;
105
106         /* get pointer to global configuration */
107         mcfg = rte_eal_get_configuration()->mem_config;
108
109         /* no more room in config */
110         if (mcfg->memzone_idx >= RTE_MAX_MEMZONE) {
111                 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
112                 rte_errno = ENOSPC;
113                 return NULL;
114         }
115
116         /* zone already exist */
117         if ((memzone_lookup_thread_unsafe(name)) != NULL) {
118                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
119                         __func__, name);
120                 rte_errno = EEXIST;
121                 return NULL;
122         }
123
124         /* align length on cache boundary. Check for overflow before doing so */
125         if (len > SIZE_MAX - CACHE_LINE_MASK) {
126                 rte_errno = EINVAL; /* requested size too big */
127                 return NULL;
128         }
129         len += CACHE_LINE_MASK;
130         len &= ~((size_t) CACHE_LINE_MASK);
131
132         /* save original length */
133         requested_len = len;
134
135         /* reserve extra space for future alignment */
136         if (len)
137                 len += align;
138
139         /* save requested length */
140         requested_len = len;
141
142         /* reserve extra space for future alignment */
143         if (len)
144                 len += align;
145
146         /* find the smallest segment matching requirements */
147         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
148                 /* last segment */
149                 if (free_memseg[i].addr == NULL)
150                         break;
151
152                 /* empty segment, skip it */
153                 if (free_memseg[i].len == 0)
154                         continue;
155
156                 /* bad socket ID */
157                 if (socket_id != SOCKET_ID_ANY &&
158                     socket_id != free_memseg[i].socket_id)
159                         continue;
160
161                 /* check len */
162                 if (len != 0 && len > free_memseg[i].len)
163                         continue;
164
165                 /* check flags for hugepage sizes */
166                 if ((flags & RTE_MEMZONE_2MB) &&
167                                 free_memseg[i].hugepage_sz == RTE_PGSIZE_1G )
168                         continue;
169                 if ((flags & RTE_MEMZONE_1GB) &&
170                                 free_memseg[i].hugepage_sz == RTE_PGSIZE_2M )
171                         continue;
172
173                 /* this segment is the best until now */
174                 if (memseg_idx == -1) {
175                         memseg_idx = i;
176                         memseg_len = free_memseg[i].len;
177                 }
178                 /* find the biggest contiguous zone */
179                 else if (len == 0) {
180                         if (free_memseg[i].len > memseg_len) {
181                                 memseg_idx = i;
182                                 memseg_len = free_memseg[i].len;
183                         }
184                 }
185                 /*
186                  * find the smallest (we already checked that current
187                  * zone length is > len
188                  */
189                 else if (free_memseg[i].len < memseg_len) {
190                         memseg_idx = i;
191                         memseg_len = free_memseg[i].len;
192                 }
193         }
194
195         /* no segment found */
196         if (memseg_idx == -1) {
197                 /*
198                  * If RTE_MEMZONE_SIZE_HINT_ONLY flag is specified,
199                  * try allocating again without the size parameter otherwise -fail.
200                  */
201                 if ((flags & RTE_MEMZONE_SIZE_HINT_ONLY)  &&
202                     ((flags & RTE_MEMZONE_1GB) || (flags & RTE_MEMZONE_2MB)))
203                         return memzone_reserve_aligned_thread_unsafe(name, len - align,
204                                         socket_id, 0, align);
205
206                 RTE_LOG(ERR, EAL, "%s(%s, %zu, %d): "
207                         "No appropriate segment found\n",
208                         __func__, name, requested_len, socket_id);
209                 rte_errno = ENOMEM;
210                 return NULL;
211         }
212
213         /* get offset needed to adjust alignment */
214         addr_offset = RTE_ALIGN_CEIL(free_memseg[memseg_idx].phys_addr, align) -
215                         free_memseg[memseg_idx].phys_addr;
216
217         /* save aligned physical and virtual addresses */
218         memseg_physaddr = free_memseg[memseg_idx].phys_addr + addr_offset;
219         memseg_addr = RTE_PTR_ADD(free_memseg[memseg_idx].addr,
220                         (uintptr_t) 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 = (size_t)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 = &mcfg->memzone[mcfg->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  * Return a pointer to a correctly filled memzone descriptor (with a
250  * specified alignment). If the allocation cannot be done, return NULL.
251  */
252 const struct rte_memzone *
253 rte_memzone_reserve_aligned(const char *name, size_t len,
254                 int socket_id, unsigned flags, unsigned align)
255 {
256         struct rte_mem_config *mcfg;
257         const struct rte_memzone *mz = NULL;
258
259         /* both sizes cannot be explicitly called for */
260         if ((flags & RTE_MEMZONE_1GB) && (flags & RTE_MEMZONE_2MB)) {
261                 rte_errno = EINVAL;
262                 return NULL;
263         }
264
265         /* if alignment is not a power of two */
266         if (!rte_is_power_of_2(align)) {
267                 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
268                                 align);
269                 rte_errno = EINVAL;
270                 return NULL;
271         }
272
273         /* alignment less than cache size is not allowed */
274         if (align < CACHE_LINE_SIZE)
275                 align = CACHE_LINE_SIZE;
276
277         /* get pointer to global configuration */
278         mcfg = rte_eal_get_configuration()->mem_config;
279
280         rte_rwlock_write_lock(&mcfg->mlock);
281
282         mz = memzone_reserve_aligned_thread_unsafe(
283                 name, len, socket_id, flags, align);
284
285         rte_rwlock_write_unlock(&mcfg->mlock);
286
287         return mz;
288 }
289
290 /*
291  * Lookup for the memzone identified by the given name
292  */
293 const struct rte_memzone *
294 rte_memzone_lookup(const char *name)
295 {
296         struct rte_mem_config *mcfg;
297         const struct rte_memzone *memzone = NULL;
298
299         mcfg = rte_eal_get_configuration()->mem_config;
300         
301         rte_rwlock_read_lock(&mcfg->mlock);
302
303         memzone = memzone_lookup_thread_unsafe(name);
304
305         rte_rwlock_read_unlock(&mcfg->mlock);
306
307         return memzone;
308 }
309
310 /* Dump all reserved memory zones on console */
311 void
312 rte_memzone_dump(void)
313 {
314         struct rte_mem_config *mcfg;
315         unsigned i = 0;
316
317         /* get pointer to global configuration */
318         mcfg = rte_eal_get_configuration()->mem_config;
319
320         rte_rwlock_read_lock(&mcfg->mlock);
321         /* dump all zones */
322         for (i=0; i<RTE_MAX_MEMZONE; i++) {
323                 if (mcfg->memzone[i].addr == NULL)
324                         break;
325                 printf("Zone %o: name:<%s>, phys:0x%"PRIx64", len:0x%zx"
326                        ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
327                        mcfg->memzone[i].name,
328                        mcfg->memzone[i].phys_addr,
329                        mcfg->memzone[i].len,
330                        mcfg->memzone[i].addr,
331                        mcfg->memzone[i].socket_id,
332                        mcfg->memzone[i].flags);
333         }
334         rte_rwlock_read_unlock(&mcfg->mlock);
335 }
336
337 /*
338  * called by init: modify the free memseg list to have cache-aligned
339  * addresses and cache-aligned lengths
340  */
341 static int
342 memseg_sanitize(struct rte_memseg *memseg)
343 {
344         unsigned phys_align;
345         unsigned virt_align;
346         unsigned off;
347
348         phys_align = memseg->phys_addr & CACHE_LINE_MASK;
349         virt_align = (unsigned long)memseg->addr & CACHE_LINE_MASK;
350
351         /*
352          * sanity check: phys_addr and addr must have the same
353          * alignment
354          */
355         if (phys_align != virt_align)
356                 return -1;
357
358         /* memseg is really too small, don't bother with it */
359         if (memseg->len < (2 * CACHE_LINE_SIZE)) {
360                 memseg->len = 0;
361                 return 0;
362         }
363
364         /* align start address */
365         off = (CACHE_LINE_SIZE - phys_align) & CACHE_LINE_MASK;
366         memseg->phys_addr += off;
367         memseg->addr = (char *)memseg->addr + off;
368         memseg->len -= off;
369
370         /* align end address */
371         memseg->len &= ~((uint64_t)CACHE_LINE_MASK);
372
373         return 0;
374 }
375
376 /*
377  * Init the memzone subsystem
378  */
379 int
380 rte_eal_memzone_init(void)
381 {
382         struct rte_mem_config *mcfg;
383         const struct rte_memseg *memseg;
384         unsigned i = 0;
385
386         /* get pointer to global configuration */
387         mcfg = rte_eal_get_configuration()->mem_config;
388
389         /* mirror the runtime memsegs from config */
390         free_memseg = mcfg->free_memseg;
391         
392         /* secondary processes don't need to initialise anything */
393         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
394                 return 0;
395
396         memseg = rte_eal_get_physmem_layout();
397         if (memseg == NULL) {
398                 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
399                 return -1;
400         }
401
402         rte_rwlock_write_lock(&mcfg->mlock);
403
404         /* duplicate the memsegs from config */
405         memcpy(free_memseg, memseg, sizeof(struct rte_memseg) * RTE_MAX_MEMSEG);
406
407         /* make all zones cache-aligned */
408         for (i=0; i<RTE_MAX_MEMSEG; i++) {
409                 if (free_memseg[i].addr == NULL)
410                         break;
411                 if (memseg_sanitize(&free_memseg[i]) < 0) {
412                         RTE_LOG(ERR, EAL, "%s(): Sanity check failed\n", __func__);
413                         rte_rwlock_write_unlock(&mcfg->mlock);
414                         return -1;
415                 }
416         }
417
418         /* delete all zones */
419         mcfg->memzone_idx = 0;
420         memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
421
422         rte_rwlock_write_unlock(&mcfg->mlock);
423
424         return 0;
425 }