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