2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 #include <sys/types.h>
18 #include <sys/mount.h>
24 #include <vppinfra/clib.h>
25 #include <vppinfra/linux/sysfs.h>
26 #include <vlib/vlib.h>
27 #include <vlib/physmem.h>
28 #include <vlib/unix/unix.h>
29 #include <vlib/pci/pci.h>
30 #include <vlib/linux/vfio.h>
32 #if defined(__x86_64__) && !defined(CLIB_SANITIZE_ADDR)
33 /* we keep physmem in low 38 bits of VA address space as some
34 IOMMU implamentation cannot map above that range */
35 #define VLIB_PHYSMEM_DEFAULT_BASE_ADDDR (1ULL << 36)
37 /* let kernel decide */
38 #define VLIB_PHYSMEM_DEFAULT_BASE_ADDDR 0
42 vlib_physmem_shared_map_create (vlib_main_t * vm, char *name, uword size,
43 u32 log2_page_sz, u32 numa_node,
46 clib_pmalloc_main_t *pm = vm->physmem_main.pmalloc_main;
47 vlib_physmem_main_t *vpm = &vm->physmem_main;
48 vlib_physmem_map_t *map;
49 clib_pmalloc_arena_t *a;
50 clib_error_t *error = 0;
54 va = clib_pmalloc_create_shared_arena (pm, name, size, log2_page_sz,
58 return clib_error_return (0, "%U", format_clib_error,
59 clib_pmalloc_last_error (pm));
61 a = clib_pmalloc_get_arena (pm, va);
63 pool_get (vpm->maps, map);
64 *map_index = map->index = map - vpm->maps;
67 map->n_pages = a->n_pages * a->subpages_per_page;
68 map->log2_page_size = a->log2_subpage_sz;
69 map->numa_node = a->numa_node;
71 for (i = 0; i < a->n_pages; i++)
74 clib_pmalloc_get_pa (pm, (u8 *) va + (i << a->log2_subpage_sz));
78 pa = pointer_to_uword (va);
80 vec_add1 (map->page_table, pa);
87 vlib_physmem_get_map (vlib_main_t * vm, u32 index)
89 vlib_physmem_main_t *vpm = &vm->physmem_main;
90 return pool_elt_at_index (vpm->maps, index);
94 vlib_physmem_init (vlib_main_t * vm)
96 vlib_physmem_main_t *vpm = &vm->physmem_main;
97 clib_error_t *error = 0;
101 /* check if pagemap is accessible */
102 pt = clib_mem_vm_get_paddr (&pt, min_log2 (sysconf (_SC_PAGESIZE)), 1);
104 vpm->flags |= VLIB_PHYSMEM_MAIN_F_HAVE_PAGEMAP;
107 if ((error = linux_vfio_init (vm)))
110 p = clib_mem_alloc_aligned (sizeof (clib_pmalloc_main_t),
111 CLIB_CACHE_LINE_BYTES);
112 memset (p, 0, sizeof (clib_pmalloc_main_t));
113 vpm->pmalloc_main = (clib_pmalloc_main_t *) p;
115 if (vpm->base_addr == 0)
116 vpm->base_addr = VLIB_PHYSMEM_DEFAULT_BASE_ADDDR;
118 clib_pmalloc_init (vpm->pmalloc_main, vpm->base_addr, vpm->max_size);
120 /* update base_addr and max_size per actual allocation */
121 vpm->base_addr = (uword) vpm->pmalloc_main->base;
122 vpm->max_size = (uword) vpm->pmalloc_main->max_pages <<
123 vpm->pmalloc_main->def_log2_page_sz;
128 static clib_error_t *
129 show_physmem (vlib_main_t * vm,
130 unformat_input_t * input, vlib_cli_command_t * cmd)
132 vlib_physmem_main_t *vpm = &vm->physmem_main;
133 unformat_input_t _line_input, *line_input = &_line_input;
134 u32 verbose = 0, map = 0;
136 if (unformat_user (input, unformat_line_input, line_input))
138 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
140 if (unformat (line_input, "verbose"))
142 else if (unformat (line_input, "v"))
144 else if (unformat (line_input, "detail"))
146 else if (unformat (line_input, "d"))
148 else if (unformat (line_input, "map"))
153 unformat_free (line_input);
157 vlib_cli_output (vm, " %U", format_pmalloc_map, vpm->pmalloc_main);
159 vlib_cli_output (vm, " %U", format_pmalloc, vpm->pmalloc_main, verbose);
165 VLIB_CLI_COMMAND (show_physmem_command, static) = {
166 .path = "show physmem",
167 .short_help = "show physmem [verbose | detail | map]",
168 .function = show_physmem,
172 static clib_error_t *
173 vlib_physmem_config (vlib_main_t * vm, unformat_input_t * input)
175 vlib_physmem_main_t *vpm = &vm->physmem_main;
177 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
179 if (unformat (input, "base-addr 0x%lx", &vpm->base_addr))
181 else if (unformat (input, "max-size %U",
182 unformat_memory_size, &vpm->max_size))
185 return unformat_parse_error (input);
188 unformat_free (input);
192 VLIB_EARLY_CONFIG_FUNCTION (vlib_physmem_config, "physmem");
195 * fd.io coding-style-patch-verification: ON
198 * eval: (c-set-style "gnu")