New upstream version 18.05
[deb_dpdk.git] / lib / librte_eal / common / eal_common_memory.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <inttypes.h>
13 #include <sys/mman.h>
14 #include <sys/queue.h>
15
16 #include <rte_fbarray.h>
17 #include <rte_memory.h>
18 #include <rte_eal.h>
19 #include <rte_eal_memconfig.h>
20 #include <rte_errno.h>
21 #include <rte_log.h>
22
23 #include "eal_memalloc.h"
24 #include "eal_private.h"
25 #include "eal_internal_cfg.h"
26
27 /*
28  * Try to mmap *size bytes in /dev/zero. If it is successful, return the
29  * pointer to the mmap'd area and keep *size unmodified. Else, retry
30  * with a smaller zone: decrease *size by hugepage_sz until it reaches
31  * 0. In this case, return NULL. Note: this function returns an address
32  * which is a multiple of hugepage size.
33  */
34
35 #define MEMSEG_LIST_FMT "memseg-%" PRIu64 "k-%i-%i"
36
37 static uint64_t baseaddr_offset;
38 static uint64_t system_page_sz;
39
40 void *
41 eal_get_virtual_area(void *requested_addr, size_t *size,
42                 size_t page_sz, int flags, int mmap_flags)
43 {
44         bool addr_is_hint, allow_shrink, unmap, no_align;
45         uint64_t map_sz;
46         void *mapped_addr, *aligned_addr;
47
48         if (system_page_sz == 0)
49                 system_page_sz = sysconf(_SC_PAGESIZE);
50
51         mmap_flags |= MAP_PRIVATE | MAP_ANONYMOUS;
52
53         RTE_LOG(DEBUG, EAL, "Ask a virtual area of 0x%zx bytes\n", *size);
54
55         addr_is_hint = (flags & EAL_VIRTUAL_AREA_ADDR_IS_HINT) > 0;
56         allow_shrink = (flags & EAL_VIRTUAL_AREA_ALLOW_SHRINK) > 0;
57         unmap = (flags & EAL_VIRTUAL_AREA_UNMAP) > 0;
58
59         if (requested_addr == NULL && internal_config.base_virtaddr != 0) {
60                 requested_addr = (void *) (internal_config.base_virtaddr +
61                                 (size_t)baseaddr_offset);
62                 requested_addr = RTE_PTR_ALIGN(requested_addr, page_sz);
63                 addr_is_hint = true;
64         }
65
66         /* if requested address is not aligned by page size, or if requested
67          * address is NULL, add page size to requested length as we may get an
68          * address that's aligned by system page size, which can be smaller than
69          * our requested page size. additionally, we shouldn't try to align if
70          * system page size is the same as requested page size.
71          */
72         no_align = (requested_addr != NULL &&
73                 ((uintptr_t)requested_addr & (page_sz - 1)) == 0) ||
74                 page_sz == system_page_sz;
75
76         do {
77                 map_sz = no_align ? *size : *size + page_sz;
78                 if (map_sz > SIZE_MAX) {
79                         RTE_LOG(ERR, EAL, "Map size too big\n");
80                         rte_errno = E2BIG;
81                         return NULL;
82                 }
83
84                 mapped_addr = mmap(requested_addr, (size_t)map_sz, PROT_READ,
85                                 mmap_flags, -1, 0);
86                 if (mapped_addr == MAP_FAILED && allow_shrink)
87                         *size -= page_sz;
88         } while (allow_shrink && mapped_addr == MAP_FAILED && *size > 0);
89
90         /* align resulting address - if map failed, we will ignore the value
91          * anyway, so no need to add additional checks.
92          */
93         aligned_addr = no_align ? mapped_addr :
94                         RTE_PTR_ALIGN(mapped_addr, page_sz);
95
96         if (*size == 0) {
97                 RTE_LOG(ERR, EAL, "Cannot get a virtual area of any size: %s\n",
98                         strerror(errno));
99                 rte_errno = errno;
100                 return NULL;
101         } else if (mapped_addr == MAP_FAILED) {
102                 RTE_LOG(ERR, EAL, "Cannot get a virtual area: %s\n",
103                         strerror(errno));
104                 /* pass errno up the call chain */
105                 rte_errno = errno;
106                 return NULL;
107         } else if (requested_addr != NULL && !addr_is_hint &&
108                         aligned_addr != requested_addr) {
109                 RTE_LOG(ERR, EAL, "Cannot get a virtual area at requested address: %p (got %p)\n",
110                         requested_addr, aligned_addr);
111                 munmap(mapped_addr, map_sz);
112                 rte_errno = EADDRNOTAVAIL;
113                 return NULL;
114         } else if (requested_addr != NULL && addr_is_hint &&
115                         aligned_addr != requested_addr) {
116                 RTE_LOG(WARNING, EAL, "WARNING! Base virtual address hint (%p != %p) not respected!\n",
117                         requested_addr, aligned_addr);
118                 RTE_LOG(WARNING, EAL, "   This may cause issues with mapping memory into secondary processes\n");
119         }
120
121         RTE_LOG(DEBUG, EAL, "Virtual area found at %p (size = 0x%zx)\n",
122                 aligned_addr, *size);
123
124         if (unmap) {
125                 munmap(mapped_addr, map_sz);
126         } else if (!no_align) {
127                 void *map_end, *aligned_end;
128                 size_t before_len, after_len;
129
130                 /* when we reserve space with alignment, we add alignment to
131                  * mapping size. On 32-bit, if 1GB alignment was requested, this
132                  * would waste 1GB of address space, which is a luxury we cannot
133                  * afford. so, if alignment was performed, check if any unneeded
134                  * address space can be unmapped back.
135                  */
136
137                 map_end = RTE_PTR_ADD(mapped_addr, (size_t)map_sz);
138                 aligned_end = RTE_PTR_ADD(aligned_addr, *size);
139
140                 /* unmap space before aligned mmap address */
141                 before_len = RTE_PTR_DIFF(aligned_addr, mapped_addr);
142                 if (before_len > 0)
143                         munmap(mapped_addr, before_len);
144
145                 /* unmap space after aligned end mmap address */
146                 after_len = RTE_PTR_DIFF(map_end, aligned_end);
147                 if (after_len > 0)
148                         munmap(aligned_end, after_len);
149         }
150
151         baseaddr_offset += *size;
152
153         return aligned_addr;
154 }
155
156 static uint64_t
157 get_mem_amount(uint64_t page_sz, uint64_t max_mem)
158 {
159         uint64_t area_sz, max_pages;
160
161         /* limit to RTE_MAX_MEMSEG_PER_LIST pages or RTE_MAX_MEM_MB_PER_LIST */
162         max_pages = RTE_MAX_MEMSEG_PER_LIST;
163         max_mem = RTE_MIN((uint64_t)RTE_MAX_MEM_MB_PER_LIST << 20, max_mem);
164
165         area_sz = RTE_MIN(page_sz * max_pages, max_mem);
166
167         /* make sure the list isn't smaller than the page size */
168         area_sz = RTE_MAX(area_sz, page_sz);
169
170         return RTE_ALIGN(area_sz, page_sz);
171 }
172
173 static int
174 free_memseg_list(struct rte_memseg_list *msl)
175 {
176         if (rte_fbarray_destroy(&msl->memseg_arr)) {
177                 RTE_LOG(ERR, EAL, "Cannot destroy memseg list\n");
178                 return -1;
179         }
180         memset(msl, 0, sizeof(*msl));
181         return 0;
182 }
183
184 static int
185 alloc_memseg_list(struct rte_memseg_list *msl, uint64_t page_sz,
186                 uint64_t max_mem, int socket_id, int type_msl_idx)
187 {
188         char name[RTE_FBARRAY_NAME_LEN];
189         uint64_t mem_amount;
190         int max_segs;
191
192         mem_amount = get_mem_amount(page_sz, max_mem);
193         max_segs = mem_amount / page_sz;
194
195         snprintf(name, sizeof(name), MEMSEG_LIST_FMT, page_sz >> 10, socket_id,
196                  type_msl_idx);
197         if (rte_fbarray_init(&msl->memseg_arr, name, max_segs,
198                         sizeof(struct rte_memseg))) {
199                 RTE_LOG(ERR, EAL, "Cannot allocate memseg list: %s\n",
200                         rte_strerror(rte_errno));
201                 return -1;
202         }
203
204         msl->page_sz = page_sz;
205         msl->socket_id = socket_id;
206         msl->base_va = NULL;
207
208         RTE_LOG(DEBUG, EAL, "Memseg list allocated: 0x%zxkB at socket %i\n",
209                         (size_t)page_sz >> 10, socket_id);
210
211         return 0;
212 }
213
214 static int
215 alloc_va_space(struct rte_memseg_list *msl)
216 {
217         uint64_t page_sz;
218         size_t mem_sz;
219         void *addr;
220         int flags = 0;
221
222 #ifdef RTE_ARCH_PPC_64
223         flags |= MAP_HUGETLB;
224 #endif
225
226         page_sz = msl->page_sz;
227         mem_sz = page_sz * msl->memseg_arr.len;
228
229         addr = eal_get_virtual_area(msl->base_va, &mem_sz, page_sz, 0, flags);
230         if (addr == NULL) {
231                 if (rte_errno == EADDRNOTAVAIL)
232                         RTE_LOG(ERR, EAL, "Could not mmap %llu bytes at [%p] - please use '--base-virtaddr' option\n",
233                                 (unsigned long long)mem_sz, msl->base_va);
234                 else
235                         RTE_LOG(ERR, EAL, "Cannot reserve memory\n");
236                 return -1;
237         }
238         msl->base_va = addr;
239
240         return 0;
241 }
242
243 static int __rte_unused
244 memseg_primary_init_32(void)
245 {
246         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
247         int active_sockets, hpi_idx, msl_idx = 0;
248         unsigned int socket_id, i;
249         struct rte_memseg_list *msl;
250         uint64_t extra_mem_per_socket, total_extra_mem, total_requested_mem;
251         uint64_t max_mem;
252
253         /* no-huge does not need this at all */
254         if (internal_config.no_hugetlbfs)
255                 return 0;
256
257         /* this is a giant hack, but desperate times call for desperate
258          * measures. in legacy 32-bit mode, we cannot preallocate VA space,
259          * because having upwards of 2 gigabytes of VA space already mapped will
260          * interfere with our ability to map and sort hugepages.
261          *
262          * therefore, in legacy 32-bit mode, we will be initializing memseg
263          * lists much later - in eal_memory.c, right after we unmap all the
264          * unneeded pages. this will not affect secondary processes, as those
265          * should be able to mmap the space without (too many) problems.
266          */
267         if (internal_config.legacy_mem)
268                 return 0;
269
270         /* 32-bit mode is a very special case. we cannot know in advance where
271          * the user will want to allocate their memory, so we have to do some
272          * heuristics.
273          */
274         active_sockets = 0;
275         total_requested_mem = 0;
276         if (internal_config.force_sockets)
277                 for (i = 0; i < rte_socket_count(); i++) {
278                         uint64_t mem;
279
280                         socket_id = rte_socket_id_by_idx(i);
281                         mem = internal_config.socket_mem[socket_id];
282
283                         if (mem == 0)
284                                 continue;
285
286                         active_sockets++;
287                         total_requested_mem += mem;
288                 }
289         else
290                 total_requested_mem = internal_config.memory;
291
292         max_mem = (uint64_t)RTE_MAX_MEM_MB << 20;
293         if (total_requested_mem > max_mem) {
294                 RTE_LOG(ERR, EAL, "Invalid parameters: 32-bit process can at most use %uM of memory\n",
295                                 (unsigned int)(max_mem >> 20));
296                 return -1;
297         }
298         total_extra_mem = max_mem - total_requested_mem;
299         extra_mem_per_socket = active_sockets == 0 ? total_extra_mem :
300                         total_extra_mem / active_sockets;
301
302         /* the allocation logic is a little bit convoluted, but here's how it
303          * works, in a nutshell:
304          *  - if user hasn't specified on which sockets to allocate memory via
305          *    --socket-mem, we allocate all of our memory on master core socket.
306          *  - if user has specified sockets to allocate memory on, there may be
307          *    some "unused" memory left (e.g. if user has specified --socket-mem
308          *    such that not all memory adds up to 2 gigabytes), so add it to all
309          *    sockets that are in use equally.
310          *
311          * page sizes are sorted by size in descending order, so we can safely
312          * assume that we dispense with bigger page sizes first.
313          */
314
315         /* create memseg lists */
316         for (i = 0; i < rte_socket_count(); i++) {
317                 int hp_sizes = (int) internal_config.num_hugepage_sizes;
318                 uint64_t max_socket_mem, cur_socket_mem;
319                 unsigned int master_lcore_socket;
320                 struct rte_config *cfg = rte_eal_get_configuration();
321                 bool skip;
322
323                 socket_id = rte_socket_id_by_idx(i);
324
325 #ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
326                 if (socket_id > 0)
327                         break;
328 #endif
329
330                 /* if we didn't specifically request memory on this socket */
331                 skip = active_sockets != 0 &&
332                                 internal_config.socket_mem[socket_id] == 0;
333                 /* ...or if we didn't specifically request memory on *any*
334                  * socket, and this is not master lcore
335                  */
336                 master_lcore_socket = rte_lcore_to_socket_id(cfg->master_lcore);
337                 skip |= active_sockets == 0 && socket_id != master_lcore_socket;
338
339                 if (skip) {
340                         RTE_LOG(DEBUG, EAL, "Will not preallocate memory on socket %u\n",
341                                         socket_id);
342                         continue;
343                 }
344
345                 /* max amount of memory on this socket */
346                 max_socket_mem = (active_sockets != 0 ?
347                                         internal_config.socket_mem[socket_id] :
348                                         internal_config.memory) +
349                                         extra_mem_per_socket;
350                 cur_socket_mem = 0;
351
352                 for (hpi_idx = 0; hpi_idx < hp_sizes; hpi_idx++) {
353                         uint64_t max_pagesz_mem, cur_pagesz_mem = 0;
354                         uint64_t hugepage_sz;
355                         struct hugepage_info *hpi;
356                         int type_msl_idx, max_segs, total_segs = 0;
357
358                         hpi = &internal_config.hugepage_info[hpi_idx];
359                         hugepage_sz = hpi->hugepage_sz;
360
361                         /* check if pages are actually available */
362                         if (hpi->num_pages[socket_id] == 0)
363                                 continue;
364
365                         max_segs = RTE_MAX_MEMSEG_PER_TYPE;
366                         max_pagesz_mem = max_socket_mem - cur_socket_mem;
367
368                         /* make it multiple of page size */
369                         max_pagesz_mem = RTE_ALIGN_FLOOR(max_pagesz_mem,
370                                         hugepage_sz);
371
372                         RTE_LOG(DEBUG, EAL, "Attempting to preallocate "
373                                         "%" PRIu64 "M on socket %i\n",
374                                         max_pagesz_mem >> 20, socket_id);
375
376                         type_msl_idx = 0;
377                         while (cur_pagesz_mem < max_pagesz_mem &&
378                                         total_segs < max_segs) {
379                                 if (msl_idx >= RTE_MAX_MEMSEG_LISTS) {
380                                         RTE_LOG(ERR, EAL,
381                                                 "No more space in memseg lists, please increase %s\n",
382                                                 RTE_STR(CONFIG_RTE_MAX_MEMSEG_LISTS));
383                                         return -1;
384                                 }
385
386                                 msl = &mcfg->memsegs[msl_idx];
387
388                                 if (alloc_memseg_list(msl, hugepage_sz,
389                                                 max_pagesz_mem, socket_id,
390                                                 type_msl_idx)) {
391                                         /* failing to allocate a memseg list is
392                                          * a serious error.
393                                          */
394                                         RTE_LOG(ERR, EAL, "Cannot allocate memseg list\n");
395                                         return -1;
396                                 }
397
398                                 if (alloc_va_space(msl)) {
399                                         /* if we couldn't allocate VA space, we
400                                          * can try with smaller page sizes.
401                                          */
402                                         RTE_LOG(ERR, EAL, "Cannot allocate VA space for memseg list, retrying with different page size\n");
403                                         /* deallocate memseg list */
404                                         if (free_memseg_list(msl))
405                                                 return -1;
406                                         break;
407                                 }
408
409                                 total_segs += msl->memseg_arr.len;
410                                 cur_pagesz_mem = total_segs * hugepage_sz;
411                                 type_msl_idx++;
412                                 msl_idx++;
413                         }
414                         cur_socket_mem += cur_pagesz_mem;
415                 }
416                 if (cur_socket_mem == 0) {
417                         RTE_LOG(ERR, EAL, "Cannot allocate VA space on socket %u\n",
418                                 socket_id);
419                         return -1;
420                 }
421         }
422
423         return 0;
424 }
425
426 static int __rte_unused
427 memseg_primary_init(void)
428 {
429         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
430         int i, socket_id, hpi_idx, msl_idx = 0;
431         struct rte_memseg_list *msl;
432         uint64_t max_mem, total_mem;
433
434         /* no-huge does not need this at all */
435         if (internal_config.no_hugetlbfs)
436                 return 0;
437
438         max_mem = (uint64_t)RTE_MAX_MEM_MB << 20;
439         total_mem = 0;
440
441         /* create memseg lists */
442         for (hpi_idx = 0; hpi_idx < (int) internal_config.num_hugepage_sizes;
443                         hpi_idx++) {
444                 struct hugepage_info *hpi;
445                 uint64_t hugepage_sz;
446
447                 hpi = &internal_config.hugepage_info[hpi_idx];
448                 hugepage_sz = hpi->hugepage_sz;
449
450                 for (i = 0; i < (int) rte_socket_count(); i++) {
451                         uint64_t max_type_mem, total_type_mem = 0;
452                         int type_msl_idx, max_segs, total_segs = 0;
453
454                         socket_id = rte_socket_id_by_idx(i);
455
456 #ifndef RTE_EAL_NUMA_AWARE_HUGEPAGES
457                         if (socket_id > 0)
458                                 break;
459 #endif
460
461                         if (total_mem >= max_mem)
462                                 break;
463
464                         max_type_mem = RTE_MIN(max_mem - total_mem,
465                                 (uint64_t)RTE_MAX_MEM_MB_PER_TYPE << 20);
466                         max_segs = RTE_MAX_MEMSEG_PER_TYPE;
467
468                         type_msl_idx = 0;
469                         while (total_type_mem < max_type_mem &&
470                                         total_segs < max_segs) {
471                                 uint64_t cur_max_mem;
472                                 if (msl_idx >= RTE_MAX_MEMSEG_LISTS) {
473                                         RTE_LOG(ERR, EAL,
474                                                 "No more space in memseg lists, please increase %s\n",
475                                                 RTE_STR(CONFIG_RTE_MAX_MEMSEG_LISTS));
476                                         return -1;
477                                 }
478
479                                 msl = &mcfg->memsegs[msl_idx++];
480
481                                 cur_max_mem = max_type_mem - total_type_mem;
482                                 if (alloc_memseg_list(msl, hugepage_sz,
483                                                 cur_max_mem, socket_id,
484                                                 type_msl_idx))
485                                         return -1;
486
487                                 total_segs += msl->memseg_arr.len;
488                                 total_type_mem = total_segs * hugepage_sz;
489                                 type_msl_idx++;
490
491                                 if (alloc_va_space(msl)) {
492                                         RTE_LOG(ERR, EAL, "Cannot allocate VA space for memseg list\n");
493                                         return -1;
494                                 }
495                         }
496                         total_mem += total_type_mem;
497                 }
498         }
499         return 0;
500 }
501
502 static int
503 memseg_secondary_init(void)
504 {
505         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
506         int msl_idx = 0;
507         struct rte_memseg_list *msl;
508
509         for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
510
511                 msl = &mcfg->memsegs[msl_idx];
512
513                 /* skip empty memseg lists */
514                 if (msl->memseg_arr.len == 0)
515                         continue;
516
517                 if (rte_fbarray_attach(&msl->memseg_arr)) {
518                         RTE_LOG(ERR, EAL, "Cannot attach to primary process memseg lists\n");
519                         return -1;
520                 }
521
522                 /* preallocate VA space */
523                 if (alloc_va_space(msl)) {
524                         RTE_LOG(ERR, EAL, "Cannot preallocate VA space for hugepage memory\n");
525                         return -1;
526                 }
527         }
528
529         return 0;
530 }
531
532 static struct rte_memseg *
533 virt2memseg(const void *addr, const struct rte_memseg_list *msl)
534 {
535         const struct rte_fbarray *arr;
536         void *start, *end;
537         int ms_idx;
538
539         /* a memseg list was specified, check if it's the right one */
540         start = msl->base_va;
541         end = RTE_PTR_ADD(start, (size_t)msl->page_sz * msl->memseg_arr.len);
542
543         if (addr < start || addr >= end)
544                 return NULL;
545
546         /* now, calculate index */
547         arr = &msl->memseg_arr;
548         ms_idx = RTE_PTR_DIFF(addr, msl->base_va) / msl->page_sz;
549         return rte_fbarray_get(arr, ms_idx);
550 }
551
552 static struct rte_memseg_list *
553 virt2memseg_list(const void *addr)
554 {
555         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
556         struct rte_memseg_list *msl;
557         int msl_idx;
558
559         for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
560                 void *start, *end;
561                 msl = &mcfg->memsegs[msl_idx];
562
563                 start = msl->base_va;
564                 end = RTE_PTR_ADD(start,
565                                 (size_t)msl->page_sz * msl->memseg_arr.len);
566                 if (addr >= start && addr < end)
567                         break;
568         }
569         /* if we didn't find our memseg list */
570         if (msl_idx == RTE_MAX_MEMSEG_LISTS)
571                 return NULL;
572         return msl;
573 }
574
575 __rte_experimental struct rte_memseg_list *
576 rte_mem_virt2memseg_list(const void *addr)
577 {
578         return virt2memseg_list(addr);
579 }
580
581 struct virtiova {
582         rte_iova_t iova;
583         void *virt;
584 };
585 static int
586 find_virt(const struct rte_memseg_list *msl __rte_unused,
587                 const struct rte_memseg *ms, void *arg)
588 {
589         struct virtiova *vi = arg;
590         if (vi->iova >= ms->iova && vi->iova < (ms->iova + ms->len)) {
591                 size_t offset = vi->iova - ms->iova;
592                 vi->virt = RTE_PTR_ADD(ms->addr, offset);
593                 /* stop the walk */
594                 return 1;
595         }
596         return 0;
597 }
598 static int
599 find_virt_legacy(const struct rte_memseg_list *msl __rte_unused,
600                 const struct rte_memseg *ms, size_t len, void *arg)
601 {
602         struct virtiova *vi = arg;
603         if (vi->iova >= ms->iova && vi->iova < (ms->iova + len)) {
604                 size_t offset = vi->iova - ms->iova;
605                 vi->virt = RTE_PTR_ADD(ms->addr, offset);
606                 /* stop the walk */
607                 return 1;
608         }
609         return 0;
610 }
611
612 __rte_experimental void *
613 rte_mem_iova2virt(rte_iova_t iova)
614 {
615         struct virtiova vi;
616
617         memset(&vi, 0, sizeof(vi));
618
619         vi.iova = iova;
620         /* for legacy mem, we can get away with scanning VA-contiguous segments,
621          * as we know they are PA-contiguous as well
622          */
623         if (internal_config.legacy_mem)
624                 rte_memseg_contig_walk(find_virt_legacy, &vi);
625         else
626                 rte_memseg_walk(find_virt, &vi);
627
628         return vi.virt;
629 }
630
631 __rte_experimental struct rte_memseg *
632 rte_mem_virt2memseg(const void *addr, const struct rte_memseg_list *msl)
633 {
634         return virt2memseg(addr, msl != NULL ? msl :
635                         rte_mem_virt2memseg_list(addr));
636 }
637
638 static int
639 physmem_size(const struct rte_memseg_list *msl, void *arg)
640 {
641         uint64_t *total_len = arg;
642
643         *total_len += msl->memseg_arr.count * msl->page_sz;
644
645         return 0;
646 }
647
648 /* get the total size of memory */
649 uint64_t
650 rte_eal_get_physmem_size(void)
651 {
652         uint64_t total_len = 0;
653
654         rte_memseg_list_walk(physmem_size, &total_len);
655
656         return total_len;
657 }
658
659 static int
660 dump_memseg(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
661                 void *arg)
662 {
663         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
664         int msl_idx, ms_idx;
665         FILE *f = arg;
666
667         msl_idx = msl - mcfg->memsegs;
668         if (msl_idx < 0 || msl_idx >= RTE_MAX_MEMSEG_LISTS)
669                 return -1;
670
671         ms_idx = rte_fbarray_find_idx(&msl->memseg_arr, ms);
672         if (ms_idx < 0)
673                 return -1;
674
675         fprintf(f, "Segment %i-%i: IOVA:0x%"PRIx64", len:%zu, "
676                         "virt:%p, socket_id:%"PRId32", "
677                         "hugepage_sz:%"PRIu64", nchannel:%"PRIx32", "
678                         "nrank:%"PRIx32"\n",
679                         msl_idx, ms_idx,
680                         ms->iova,
681                         ms->len,
682                         ms->addr,
683                         ms->socket_id,
684                         ms->hugepage_sz,
685                         ms->nchannel,
686                         ms->nrank);
687
688         return 0;
689 }
690
691 /*
692  * Defining here because declared in rte_memory.h, but the actual implementation
693  * is in eal_common_memalloc.c, like all other memalloc internals.
694  */
695 int __rte_experimental
696 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
697                 void *arg)
698 {
699         /* FreeBSD boots with legacy mem enabled by default */
700         if (internal_config.legacy_mem) {
701                 RTE_LOG(DEBUG, EAL, "Registering mem event callbacks not supported\n");
702                 rte_errno = ENOTSUP;
703                 return -1;
704         }
705         return eal_memalloc_mem_event_callback_register(name, clb, arg);
706 }
707
708 int __rte_experimental
709 rte_mem_event_callback_unregister(const char *name, void *arg)
710 {
711         /* FreeBSD boots with legacy mem enabled by default */
712         if (internal_config.legacy_mem) {
713                 RTE_LOG(DEBUG, EAL, "Registering mem event callbacks not supported\n");
714                 rte_errno = ENOTSUP;
715                 return -1;
716         }
717         return eal_memalloc_mem_event_callback_unregister(name, arg);
718 }
719
720 int __rte_experimental
721 rte_mem_alloc_validator_register(const char *name,
722                 rte_mem_alloc_validator_t clb, int socket_id, size_t limit)
723 {
724         /* FreeBSD boots with legacy mem enabled by default */
725         if (internal_config.legacy_mem) {
726                 RTE_LOG(DEBUG, EAL, "Registering mem alloc validators not supported\n");
727                 rte_errno = ENOTSUP;
728                 return -1;
729         }
730         return eal_memalloc_mem_alloc_validator_register(name, clb, socket_id,
731                         limit);
732 }
733
734 int __rte_experimental
735 rte_mem_alloc_validator_unregister(const char *name, int socket_id)
736 {
737         /* FreeBSD boots with legacy mem enabled by default */
738         if (internal_config.legacy_mem) {
739                 RTE_LOG(DEBUG, EAL, "Registering mem alloc validators not supported\n");
740                 rte_errno = ENOTSUP;
741                 return -1;
742         }
743         return eal_memalloc_mem_alloc_validator_unregister(name, socket_id);
744 }
745
746 /* Dump the physical memory layout on console */
747 void
748 rte_dump_physmem_layout(FILE *f)
749 {
750         rte_memseg_walk(dump_memseg, f);
751 }
752
753 /* return the number of memory channels */
754 unsigned rte_memory_get_nchannel(void)
755 {
756         return rte_eal_get_configuration()->mem_config->nchannel;
757 }
758
759 /* return the number of memory rank */
760 unsigned rte_memory_get_nrank(void)
761 {
762         return rte_eal_get_configuration()->mem_config->nrank;
763 }
764
765 static int
766 rte_eal_memdevice_init(void)
767 {
768         struct rte_config *config;
769
770         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
771                 return 0;
772
773         config = rte_eal_get_configuration();
774         config->mem_config->nchannel = internal_config.force_nchannel;
775         config->mem_config->nrank = internal_config.force_nrank;
776
777         return 0;
778 }
779
780 /* Lock page in physical memory and prevent from swapping. */
781 int
782 rte_mem_lock_page(const void *virt)
783 {
784         unsigned long virtual = (unsigned long)virt;
785         int page_size = getpagesize();
786         unsigned long aligned = (virtual & ~(page_size - 1));
787         return mlock((void *)aligned, page_size);
788 }
789
790 int __rte_experimental
791 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg)
792 {
793         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
794         int i, ms_idx, ret = 0;
795
796         /* do not allow allocations/frees/init while we iterate */
797         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
798
799         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
800                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
801                 const struct rte_memseg *ms;
802                 struct rte_fbarray *arr;
803
804                 if (msl->memseg_arr.count == 0)
805                         continue;
806
807                 arr = &msl->memseg_arr;
808
809                 ms_idx = rte_fbarray_find_next_used(arr, 0);
810                 while (ms_idx >= 0) {
811                         int n_segs;
812                         size_t len;
813
814                         ms = rte_fbarray_get(arr, ms_idx);
815
816                         /* find how many more segments there are, starting with
817                          * this one.
818                          */
819                         n_segs = rte_fbarray_find_contig_used(arr, ms_idx);
820                         len = n_segs * msl->page_sz;
821
822                         ret = func(msl, ms, len, arg);
823                         if (ret < 0) {
824                                 ret = -1;
825                                 goto out;
826                         } else if (ret > 0) {
827                                 ret = 1;
828                                 goto out;
829                         }
830                         ms_idx = rte_fbarray_find_next_used(arr,
831                                         ms_idx + n_segs);
832                 }
833         }
834 out:
835         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
836         return ret;
837 }
838
839 int __rte_experimental
840 rte_memseg_walk(rte_memseg_walk_t func, void *arg)
841 {
842         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
843         int i, ms_idx, ret = 0;
844
845         /* do not allow allocations/frees/init while we iterate */
846         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
847
848         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
849                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
850                 const struct rte_memseg *ms;
851                 struct rte_fbarray *arr;
852
853                 if (msl->memseg_arr.count == 0)
854                         continue;
855
856                 arr = &msl->memseg_arr;
857
858                 ms_idx = rte_fbarray_find_next_used(arr, 0);
859                 while (ms_idx >= 0) {
860                         ms = rte_fbarray_get(arr, ms_idx);
861                         ret = func(msl, ms, arg);
862                         if (ret < 0) {
863                                 ret = -1;
864                                 goto out;
865                         } else if (ret > 0) {
866                                 ret = 1;
867                                 goto out;
868                         }
869                         ms_idx = rte_fbarray_find_next_used(arr, ms_idx + 1);
870                 }
871         }
872 out:
873         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
874         return ret;
875 }
876
877 int __rte_experimental
878 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg)
879 {
880         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
881         int i, ret = 0;
882
883         /* do not allow allocations/frees/init while we iterate */
884         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
885
886         for (i = 0; i < RTE_MAX_MEMSEG_LISTS; i++) {
887                 struct rte_memseg_list *msl = &mcfg->memsegs[i];
888
889                 if (msl->base_va == NULL)
890                         continue;
891
892                 ret = func(msl, arg);
893                 if (ret < 0) {
894                         ret = -1;
895                         goto out;
896                 }
897                 if (ret > 0) {
898                         ret = 1;
899                         goto out;
900                 }
901         }
902 out:
903         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
904         return ret;
905 }
906
907 /* init memory subsystem */
908 int
909 rte_eal_memory_init(void)
910 {
911         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
912         int retval;
913         RTE_LOG(DEBUG, EAL, "Setting up physically contiguous memory...\n");
914
915         if (!mcfg)
916                 return -1;
917
918         /* lock mem hotplug here, to prevent races while we init */
919         rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
920
921         retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
922 #ifndef RTE_ARCH_64
923                         memseg_primary_init_32() :
924 #else
925                         memseg_primary_init() :
926 #endif
927                         memseg_secondary_init();
928
929         if (retval < 0)
930                 goto fail;
931
932         if (eal_memalloc_init() < 0)
933                 goto fail;
934
935         retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
936                         rte_eal_hugepage_init() :
937                         rte_eal_hugepage_attach();
938         if (retval < 0)
939                 goto fail;
940
941         if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)
942                 goto fail;
943
944         return 0;
945 fail:
946         rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
947         return -1;
948 }