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