2599bce944db040e921264c8a39d08af4e8b3e76
[vpp.git] / src / vlib / physmem.c
1 /*
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>
20 #include <sys/fcntl.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23
24 #include <vppinfra/linux/syscall.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>
31
32 clib_error_t *
33 vlib_physmem_shared_map_create (vlib_main_t * vm, char *name, uword size,
34                                 u32 numa_node, u32 * map_index)
35 {
36   clib_pmalloc_main_t *pm = vm->physmem_main.pmalloc_main;
37   vlib_physmem_main_t *vpm = &vm->physmem_main;
38   vlib_physmem_map_t *map;
39   clib_pmalloc_arena_t *a;
40   clib_error_t *error = 0;
41   void *va;
42   uword i;
43
44   va = clib_pmalloc_create_shared_arena (pm, name, size, numa_node);
45
46   if (va == 0)
47     return clib_error_return (0, "%U", format_clib_error,
48                               clib_pmalloc_last_error (pm));
49
50   a = clib_pmalloc_get_arena (pm, va);
51
52   pool_get (vpm->maps, map);
53   *map_index = map->index = map - vpm->maps;
54   map->base = va;
55   map->fd = a->fd;
56   map->n_pages = a->n_pages;
57   map->log2_page_size = a->log2_page_sz;
58
59   for (i = 0; i < a->n_pages; i++)
60     {
61       uword pa = clib_pmalloc_get_pa (pm, (u8 *) va + (i << a->log2_page_sz));
62
63       /* maybe iova */
64       if (pa == 0)
65         pa = pointer_to_uword (va);
66
67       vec_add1 (map->page_table, pa);
68     }
69
70   return error;
71 }
72
73 vlib_physmem_map_t *
74 vlib_physmem_get_map (vlib_main_t * vm, u32 index)
75 {
76   vlib_physmem_main_t *vpm = &vm->physmem_main;
77   return pool_elt_at_index (vpm->maps, index);
78 }
79
80 clib_error_t *
81 vlib_physmem_init (vlib_main_t * vm)
82 {
83   vlib_physmem_main_t *vpm = &vm->physmem_main;
84   clib_error_t *error = 0;
85   u64 *pt = 0;
86   void *p;
87
88   /* check if pagemap is accessible */
89   pt = clib_mem_vm_get_paddr (&pt, min_log2 (sysconf (_SC_PAGESIZE)), 1);
90   if (pt && pt[0])
91     vpm->flags |= VLIB_PHYSMEM_MAIN_F_HAVE_PAGEMAP;
92   vec_free (pt);
93
94   if ((error = linux_vfio_init (vm)))
95     return error;
96
97   p = clib_mem_alloc_aligned (sizeof (clib_pmalloc_main_t),
98                               CLIB_CACHE_LINE_BYTES);
99   memset (p, 0, sizeof (clib_pmalloc_main_t));
100   vpm->pmalloc_main = (clib_pmalloc_main_t *) p;
101   clib_pmalloc_init (vpm->pmalloc_main, 0);
102
103   return error;
104 }
105
106 static clib_error_t *
107 show_physmem (vlib_main_t * vm,
108               unformat_input_t * input, vlib_cli_command_t * cmd)
109 {
110   vlib_physmem_main_t *vpm = &vm->physmem_main;
111   unformat_input_t _line_input, *line_input = &_line_input;
112   u32 verbose = 0;
113
114   if (unformat_user (input, unformat_line_input, line_input))
115     {
116       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
117         {
118           if (unformat (line_input, "verbose"))
119             verbose = 1;
120           else if (unformat (line_input, "v"))
121             verbose = 1;
122           else if (unformat (line_input, "detail"))
123             verbose = 2;
124           else if (unformat (line_input, "d"))
125             verbose = 2;
126           else
127             break;
128         }
129       unformat_free (line_input);
130     }
131
132   vlib_cli_output (vm, "  %U", format_pmalloc, vpm->pmalloc_main, verbose);
133   return 0;
134 }
135
136 /* *INDENT-OFF* */
137 VLIB_CLI_COMMAND (show_physmem_command, static) = {
138   .path = "show physmem",
139   .short_help = "Show physical memory allocation",
140   .function = show_physmem,
141 };
142 /* *INDENT-ON* */
143
144 /*
145  * fd.io coding-style-patch-verification: ON
146  *
147  * Local Variables:
148  * eval: (c-set-style "gnu")
149  * End:
150  */