blob: b247efc9a853caecf9d194c5bddcc097d0a659c8 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 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 * physmem.c: Unix physical memory
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
Damjan Marion49d66f12017-07-20 18:10:35 +020040#include <unistd.h>
41#include <sys/types.h>
42#include <sys/mount.h>
43#include <sys/mman.h>
44#include <sys/fcntl.h>
45#include <sys/stat.h>
Damjan Marion2ca8ced2017-10-30 22:38:47 +010046#include <unistd.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070047
Damjan Marion01914ce2017-09-14 19:04:50 +020048#include <vppinfra/linux/syscall.h>
49#include <vppinfra/linux/sysfs.h>
Damjan Marion49d66f12017-07-20 18:10:35 +020050#include <vlib/vlib.h>
51#include <vlib/physmem.h>
52#include <vlib/unix/unix.h>
Damjan Marion1ba0fa42018-03-04 17:19:08 +010053#include <vlib/pci/pci.h>
54#include <vlib/linux/vfio.h>
Damjan Marion2ca8ced2017-10-30 22:38:47 +010055
Ed Warnickecb9cada2015-12-08 15:45:58 -070056static void *
Damjan Marion49d66f12017-07-20 18:10:35 +020057unix_physmem_alloc_aligned (vlib_main_t * vm, vlib_physmem_region_index_t idx,
58 uword n_bytes, uword alignment)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
Damjan Marion49d66f12017-07-20 18:10:35 +020060 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx);
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 uword lo_offset, hi_offset;
Dave Barach9b8ffd92016-07-08 08:13:45 -040062 uword *to_free = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Damjan Marion49d66f12017-07-20 18:10:35 +020064 if (pr->heap == 0)
65 return 0;
66
Ed Warnickecb9cada2015-12-08 15:45:58 -070067 /* IO memory is always at least cache aligned. */
68 alignment = clib_max (alignment, CLIB_CACHE_LINE_BYTES);
69
70 while (1)
71 {
Damjan Marion49d66f12017-07-20 18:10:35 +020072 mheap_get_aligned (pr->heap, n_bytes,
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 /* align */ alignment,
74 /* align offset */ 0,
75 &lo_offset);
76
77 /* Allocation failed? */
78 if (lo_offset == ~0)
79 break;
80
81 /* Make sure allocation does not span DMA physical chunk boundary. */
82 hi_offset = lo_offset + n_bytes - 1;
83
Damjan Marion52014c62017-12-13 13:51:13 +010084 if (((pointer_to_uword (pr->heap) + lo_offset) >> pr->log2_page_size) ==
85 ((pointer_to_uword (pr->heap) + hi_offset) >> pr->log2_page_size))
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 break;
87
88 /* Allocation would span chunk boundary, queue it to be freed as soon as
Dave Barach9b8ffd92016-07-08 08:13:45 -040089 we find suitable chunk. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070090 vec_add1 (to_free, lo_offset);
91 }
92
93 if (to_free != 0)
94 {
95 uword i;
96 for (i = 0; i < vec_len (to_free); i++)
Damjan Marion49d66f12017-07-20 18:10:35 +020097 mheap_put (pr->heap, to_free[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 vec_free (to_free);
99 }
100
Damjan Marion49d66f12017-07-20 18:10:35 +0200101 return lo_offset != ~0 ? pr->heap + lo_offset : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102}
103
Dave Barach9b8ffd92016-07-08 08:13:45 -0400104static void
Damjan Marion49d66f12017-07-20 18:10:35 +0200105unix_physmem_free (vlib_main_t * vm, vlib_physmem_region_index_t idx, void *x)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106{
Damjan Marion49d66f12017-07-20 18:10:35 +0200107 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 /* Return object to region's heap. */
Damjan Marion49d66f12017-07-20 18:10:35 +0200109 mheap_put (pr->heap, x - pr->heap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110}
111
Damjan Marion49d66f12017-07-20 18:10:35 +0200112static clib_error_t *
113unix_physmem_region_alloc (vlib_main_t * vm, char *name, u32 size,
114 u8 numa_node, u32 flags,
115 vlib_physmem_region_index_t * idx)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116{
Damjan Marion88c06212018-03-05 20:08:28 +0100117 vlib_physmem_main_t *vpm = &physmem_main;
Damjan Marion49d66f12017-07-20 18:10:35 +0200118 vlib_physmem_region_t *pr;
119 clib_error_t *error = 0;
Damjan Marion01914ce2017-09-14 19:04:50 +0200120 clib_mem_vm_alloc_t alloc = { 0 };
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100121 int i;
Damjan Marion49d66f12017-07-20 18:10:35 +0200122
123 pool_get (vpm->regions, pr);
124
125 if ((pr - vpm->regions) >= 256)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 {
Damjan Marion49d66f12017-07-20 18:10:35 +0200127 error = clib_error_return (0, "maximum number of regions reached");
128 goto error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 }
130
Damjan Marion01914ce2017-09-14 19:04:50 +0200131 alloc.name = name;
132 alloc.size = size;
133 alloc.numa_node = numa_node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100135 alloc.flags = (flags & VLIB_PHYSMEM_F_SHARED) ?
136 CLIB_MEM_VM_F_SHARED : CLIB_MEM_VM_F_LOCKED;
137
138 if ((flags & VLIB_PHYSMEM_F_HUGETLB))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 {
Damjan Marion01914ce2017-09-14 19:04:50 +0200140 alloc.flags |= CLIB_MEM_VM_F_HUGETLB;
141 alloc.flags |= CLIB_MEM_VM_F_HUGETLB_PREALLOC;
142 alloc.flags |= CLIB_MEM_VM_F_NUMA_FORCE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 }
Damjan Marion49d66f12017-07-20 18:10:35 +0200144 else
145 {
Damjan Marion01914ce2017-09-14 19:04:50 +0200146 alloc.flags |= CLIB_MEM_VM_F_NUMA_PREFER;
Damjan Marion49d66f12017-07-20 18:10:35 +0200147 }
148
Damjan Marion01914ce2017-09-14 19:04:50 +0200149 error = clib_mem_vm_ext_alloc (&alloc);
150 if (error)
151 goto error;
Damjan Marion49d66f12017-07-20 18:10:35 +0200152
Damjan Marion01914ce2017-09-14 19:04:50 +0200153 pr->index = pr - vpm->regions;
154 pr->flags = flags;
155 pr->fd = alloc.fd;
156 pr->mem = alloc.addr;
157 pr->log2_page_size = alloc.log2_page_size;
158 pr->n_pages = alloc.n_pages;
Chris Lukeb2bcad62017-09-18 08:51:22 -0400159 pr->size = (u64) pr->n_pages << (u64) pr->log2_page_size;
Damjan Marion49d66f12017-07-20 18:10:35 +0200160 pr->page_mask = (1 << pr->log2_page_size) - 1;
161 pr->numa_node = numa_node;
162 pr->name = format (0, "%s", name);
163
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100164 for (i = 0; i < pr->n_pages; i++)
Damjan Marion49d66f12017-07-20 18:10:35 +0200165 {
Lee Roberts45a09462018-03-07 19:47:00 -0700166 void *ptr = pr->mem + ((u64) i << pr->log2_page_size);
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100167 int node;
168 if ((move_pages (0, 1, &ptr, 0, &node, 0) == 0) && (numa_node != node))
Damjan Marion49d66f12017-07-20 18:10:35 +0200169 {
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100170 clib_warning ("physmem page for region \'%s\' allocated on the"
171 " wrong numa node (requested %u actual %u)",
172 pr->name, pr->numa_node, node, i);
173 break;
Damjan Marion49d66f12017-07-20 18:10:35 +0200174 }
175 }
176
Damjan Marion51c52c02018-03-09 16:05:56 +0100177 pr->page_table = clib_mem_vm_get_paddr (pr->mem, pr->log2_page_size,
178 pr->n_pages);
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100179
180 linux_vfio_dma_map_regions (vm);
181
Damjan Marion49d66f12017-07-20 18:10:35 +0200182 if (flags & VLIB_PHYSMEM_F_INIT_MHEAP)
183 {
184 pr->heap = mheap_alloc_with_flags (pr->mem, pr->size,
185 /* Don't want mheap mmap/munmap with IO memory. */
186 MHEAP_FLAG_DISABLE_VM |
187 MHEAP_FLAG_THREAD_SAFE);
Damjan Marion49d66f12017-07-20 18:10:35 +0200188 }
189
Damjan Marion49d66f12017-07-20 18:10:35 +0200190 *idx = pr->index;
191
Damjan Marion49d66f12017-07-20 18:10:35 +0200192 goto done;
193
194error:
Damjan Marion49d66f12017-07-20 18:10:35 +0200195 memset (pr, 0, sizeof (*pr));
196 pool_put (vpm->regions, pr);
197
198done:
Damjan Marion49d66f12017-07-20 18:10:35 +0200199 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200}
201
Damjan Marion49d66f12017-07-20 18:10:35 +0200202static void
203unix_physmem_region_free (vlib_main_t * vm, vlib_physmem_region_index_t idx)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204{
Damjan Marion88c06212018-03-05 20:08:28 +0100205 vlib_physmem_main_t *vpm = &physmem_main;
Damjan Marion49d66f12017-07-20 18:10:35 +0200206 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx);
207
208 if (pr->fd > 0)
209 close (pr->fd);
210 munmap (pr->mem, pr->size);
211 vec_free (pr->name);
212 pool_put (vpm->regions, pr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213}
214
Dave Barach9b8ffd92016-07-08 08:13:45 -0400215clib_error_t *
Damjan Marion49d66f12017-07-20 18:10:35 +0200216unix_physmem_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217{
Damjan Marion88c06212018-03-05 20:08:28 +0100218 vlib_physmem_main_t *vpm = &physmem_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400219 clib_error_t *error = 0;
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100220 u64 *pt = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
222 /* Avoid multiple calls. */
223 if (vm->os_physmem_alloc_aligned)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400224 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100226 /* check if pagemap is accessible */
227 pt = clib_mem_vm_get_paddr (&pt, min_log2 (sysconf (_SC_PAGESIZE)), 1);
228 if (pt[0])
229 vpm->flags |= VLIB_PHYSMEM_MAIN_F_HAVE_PAGEMAP;
230 vec_free (pt);
231
232 if ((error = linux_vfio_init (vm)))
233 return error;
234
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 vm->os_physmem_alloc_aligned = unix_physmem_alloc_aligned;
236 vm->os_physmem_free = unix_physmem_free;
Damjan Marion49d66f12017-07-20 18:10:35 +0200237 vm->os_physmem_region_alloc = unix_physmem_region_alloc;
238 vm->os_physmem_region_free = unix_physmem_region_free;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240 return error;
241}
242
243static clib_error_t *
244show_physmem (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400245 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246{
Damjan Marion88c06212018-03-05 20:08:28 +0100247 vlib_physmem_main_t *vpm = &physmem_main;
Damjan Marion49d66f12017-07-20 18:10:35 +0200248 vlib_physmem_region_t *pr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
Damjan Marion49d66f12017-07-20 18:10:35 +0200250 /* *INDENT-OFF* */
251 pool_foreach (pr, vpm->regions, (
252 {
253 vlib_cli_output (vm, "index %u name '%s' page-size %uKB num-pages %d "
254 "numa-node %u fd %d\n",
255 pr->index, pr->name, (1 << (pr->log2_page_size -10)),
256 pr->n_pages, pr->numa_node, pr->fd);
257 if (pr->heap)
258 vlib_cli_output (vm, " %U", format_mheap, pr->heap, /* verbose */ 1);
259 else
260 vlib_cli_output (vm, " no heap\n");
261 }));
262 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263 return 0;
264}
265
Dave Barach9b8ffd92016-07-08 08:13:45 -0400266/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267VLIB_CLI_COMMAND (show_physmem_command, static) = {
268 .path = "show physmem",
269 .short_help = "Show physical memory allocation",
270 .function = show_physmem,
271};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400272/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273
Dave Barach9b8ffd92016-07-08 08:13:45 -0400274/*
275 * fd.io coding-style-patch-verification: ON
276 *
277 * Local Variables:
278 * eval: (c-set-style "gnu")
279 * End:
280 */