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