blob: 15e912fd9e77564f506362afb4f9f023f4ed8ddc [file] [log] [blame]
Damjan Marion68b4da62018-09-30 18:26:20 +02001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <unistd.h>
17#include <sys/types.h>
18#include <sys/mount.h>
19#include <sys/mman.h>
Guillaume Solignacaf5744c2023-01-10 15:37:18 +010020#include <fcntl.h>
Damjan Marion68b4da62018-09-30 18:26:20 +020021#include <sys/stat.h>
22#include <unistd.h>
23
Benoît Ganne6531cf52022-09-30 17:13:33 +020024#include <vppinfra/clib.h>
Damjan Marion68b4da62018-09-30 18:26:20 +020025#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>
31
Benoît Ganne9fb6d402019-04-15 15:28:21 +020032#if defined(__x86_64__) && !defined(CLIB_SANITIZE_ADDR)
Damjan Marion5a6c8092019-02-21 14:44:59 +010033/* 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)
36#else
37/* let kernel decide */
38#define VLIB_PHYSMEM_DEFAULT_BASE_ADDDR 0
39#endif
40
Damjan Marion68b4da62018-09-30 18:26:20 +020041clib_error_t *
42vlib_physmem_shared_map_create (vlib_main_t * vm, char *name, uword size,
Damjan Marion567e61d2018-10-24 17:08:26 +020043 u32 log2_page_sz, u32 numa_node,
44 u32 * map_index)
Damjan Marion68b4da62018-09-30 18:26:20 +020045{
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;
51 void *va;
Damjan Marion8e8d3c82018-10-23 22:54:40 +020052 uword i;
Damjan Marion68b4da62018-09-30 18:26:20 +020053
Damjan Marion567e61d2018-10-24 17:08:26 +020054 va = clib_pmalloc_create_shared_arena (pm, name, size, log2_page_sz,
55 numa_node);
Damjan Marion68b4da62018-09-30 18:26:20 +020056
57 if (va == 0)
58 return clib_error_return (0, "%U", format_clib_error,
59 clib_pmalloc_last_error (pm));
60
61 a = clib_pmalloc_get_arena (pm, va);
62
63 pool_get (vpm->maps, map);
64 *map_index = map->index = map - vpm->maps;
65 map->base = va;
66 map->fd = a->fd;
Damjan Marion567e61d2018-10-24 17:08:26 +020067 map->n_pages = a->n_pages * a->subpages_per_page;
68 map->log2_page_size = a->log2_subpage_sz;
Damjan Mariond2bfb782019-01-07 20:56:04 +010069 map->numa_node = a->numa_node;
Damjan Marion68b4da62018-09-30 18:26:20 +020070
71 for (i = 0; i < a->n_pages; i++)
72 {
Damjan Marion567e61d2018-10-24 17:08:26 +020073 uword pa =
74 clib_pmalloc_get_pa (pm, (u8 *) va + (i << a->log2_subpage_sz));
Damjan Marion68b4da62018-09-30 18:26:20 +020075
76 /* maybe iova */
77 if (pa == 0)
78 pa = pointer_to_uword (va);
79
80 vec_add1 (map->page_table, pa);
81 }
82
83 return error;
84}
85
86vlib_physmem_map_t *
87vlib_physmem_get_map (vlib_main_t * vm, u32 index)
88{
89 vlib_physmem_main_t *vpm = &vm->physmem_main;
90 return pool_elt_at_index (vpm->maps, index);
91}
92
93clib_error_t *
94vlib_physmem_init (vlib_main_t * vm)
95{
96 vlib_physmem_main_t *vpm = &vm->physmem_main;
97 clib_error_t *error = 0;
98 u64 *pt = 0;
99 void *p;
100
101 /* check if pagemap is accessible */
102 pt = clib_mem_vm_get_paddr (&pt, min_log2 (sysconf (_SC_PAGESIZE)), 1);
Damjan Marion8e8d3c82018-10-23 22:54:40 +0200103 if (pt && pt[0])
Damjan Marion68b4da62018-09-30 18:26:20 +0200104 vpm->flags |= VLIB_PHYSMEM_MAIN_F_HAVE_PAGEMAP;
105 vec_free (pt);
106
Tom Jonesaaa2a892024-01-30 13:54:12 +0000107#ifdef __linux__
Damjan Marion68b4da62018-09-30 18:26:20 +0200108 if ((error = linux_vfio_init (vm)))
109 return error;
Tom Jonesaaa2a892024-01-30 13:54:12 +0000110#endif /* __linux__ */
Damjan Marion68b4da62018-09-30 18:26:20 +0200111
112 p = clib_mem_alloc_aligned (sizeof (clib_pmalloc_main_t),
113 CLIB_CACHE_LINE_BYTES);
114 memset (p, 0, sizeof (clib_pmalloc_main_t));
115 vpm->pmalloc_main = (clib_pmalloc_main_t *) p;
Damjan Marion5a6c8092019-02-21 14:44:59 +0100116
117 if (vpm->base_addr == 0)
118 vpm->base_addr = VLIB_PHYSMEM_DEFAULT_BASE_ADDDR;
119
Lijian.Zhangba0da572019-08-21 17:51:16 +0800120 clib_pmalloc_init (vpm->pmalloc_main, vpm->base_addr, vpm->max_size);
121
122 /* update base_addr and max_size per actual allocation */
123 vpm->base_addr = (uword) vpm->pmalloc_main->base;
124 vpm->max_size = (uword) vpm->pmalloc_main->max_pages <<
125 vpm->pmalloc_main->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +0200126
127 return error;
128}
129
130static clib_error_t *
131show_physmem (vlib_main_t * vm,
132 unformat_input_t * input, vlib_cli_command_t * cmd)
133{
134 vlib_physmem_main_t *vpm = &vm->physmem_main;
135 unformat_input_t _line_input, *line_input = &_line_input;
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100136 u32 verbose = 0, map = 0;
Damjan Marion68b4da62018-09-30 18:26:20 +0200137
138 if (unformat_user (input, unformat_line_input, line_input))
139 {
140 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
141 {
142 if (unformat (line_input, "verbose"))
143 verbose = 1;
144 else if (unformat (line_input, "v"))
145 verbose = 1;
146 else if (unformat (line_input, "detail"))
147 verbose = 2;
148 else if (unformat (line_input, "d"))
149 verbose = 2;
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100150 else if (unformat (line_input, "map"))
151 map = 1;
Damjan Marion68b4da62018-09-30 18:26:20 +0200152 else
153 break;
154 }
155 unformat_free (line_input);
156 }
157
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100158 if (map)
159 vlib_cli_output (vm, " %U", format_pmalloc_map, vpm->pmalloc_main);
160 else
161 vlib_cli_output (vm, " %U", format_pmalloc, vpm->pmalloc_main, verbose);
162
Damjan Marion68b4da62018-09-30 18:26:20 +0200163 return 0;
164}
165
166/* *INDENT-OFF* */
167VLIB_CLI_COMMAND (show_physmem_command, static) = {
168 .path = "show physmem",
Mohsin Kazmi6ec99c32018-11-07 16:55:18 +0100169 .short_help = "show physmem [verbose | detail | map]",
Damjan Marion68b4da62018-09-30 18:26:20 +0200170 .function = show_physmem,
171};
172/* *INDENT-ON* */
173
Damjan Marion5a6c8092019-02-21 14:44:59 +0100174static clib_error_t *
175vlib_physmem_config (vlib_main_t * vm, unformat_input_t * input)
176{
177 vlib_physmem_main_t *vpm = &vm->physmem_main;
178
179 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
180 {
181 if (unformat (input, "base-addr 0x%lx", &vpm->base_addr))
182 ;
jieqiang wang842506f2019-10-22 15:54:40 +0800183 else if (unformat (input, "max-size %U",
184 unformat_memory_size, &vpm->max_size))
185 ;
Damjan Marion5a6c8092019-02-21 14:44:59 +0100186 else
187 return unformat_parse_error (input);
188 }
189
190 unformat_free (input);
191 return 0;
192}
193
194VLIB_EARLY_CONFIG_FUNCTION (vlib_physmem_config, "physmem");
195
Damjan Marion68b4da62018-09-30 18:26:20 +0200196/*
197 * fd.io coding-style-patch-verification: ON
198 *
199 * Local Variables:
200 * eval: (c-set-style "gnu")
201 * End:
202 */