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