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:
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.
16 * physmem.c: Unix physical memory
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
41 #include <sys/types.h>
42 #include <sys/mount.h>
44 #include <sys/fcntl.h>
48 #include <vppinfra/linux/syscall.h>
49 #include <vppinfra/linux/sysfs.h>
50 #include <vlib/vlib.h>
51 #include <vlib/physmem.h>
52 #include <vlib/unix/unix.h>
53 #include <vlib/pci/pci.h>
54 #include <vlib/linux/vfio.h>
57 unix_physmem_alloc_aligned (vlib_main_t * vm, vlib_physmem_region_index_t idx,
58 uword n_bytes, uword alignment)
60 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx);
61 uword lo_offset, hi_offset;
67 /* IO memory is always at least cache aligned. */
68 alignment = clib_max (alignment, CLIB_CACHE_LINE_BYTES);
74 mheap_get_aligned (pr->heap, n_bytes,
75 /* align */ alignment,
79 lo_offset = (uword) mspace_get_aligned (pr->heap, n_bytes,
80 alignment, ~0ULL /* offset */ );
85 /* Allocation failed? */
89 /* Make sure allocation does not span DMA physical chunk boundary. */
90 hi_offset = lo_offset + n_bytes - 1;
92 if (((pointer_to_uword (pr->heap) + lo_offset) >> pr->log2_page_size) ==
93 ((pointer_to_uword (pr->heap) + hi_offset) >> pr->log2_page_size))
96 /* Allocation would span chunk boundary, queue it to be freed as soon as
97 we find suitable chunk. */
98 vec_add1 (to_free, lo_offset);
104 for (i = 0; i < vec_len (to_free); i++)
106 #if USE_DLMALLOC == 0
107 mheap_put (pr->heap, to_free[i]);
109 mspace_put_no_offset (pr->heap, (void *) to_free[i]);
115 #if USE_DLMALLOC == 0
116 return lo_offset != ~0 ? (void *) (pr->heap + lo_offset) : 0;
118 return lo_offset != ~0 ? (void *) lo_offset : 0;
123 unix_physmem_free (vlib_main_t * vm, vlib_physmem_region_index_t idx, void *x)
125 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx);
126 /* Return object to region's heap. */
127 #if USE_DLMALLOC == 0
128 mheap_put (pr->heap, x - pr->heap);
130 mspace_put_no_offset (pr->heap, x);
134 static clib_error_t *
135 unix_physmem_region_alloc (vlib_main_t * vm, char *name, u32 size,
136 u8 numa_node, u32 flags,
137 vlib_physmem_region_index_t * idx)
139 vlib_physmem_main_t *vpm = &physmem_main;
140 vlib_physmem_region_t *pr;
141 clib_error_t *error = 0;
142 clib_mem_vm_alloc_t alloc = { 0 };
145 pool_get (vpm->regions, pr);
147 if ((pr - vpm->regions) >= 256)
149 error = clib_error_return (0, "maximum number of regions reached");
155 alloc.numa_node = numa_node;
157 alloc.flags = (flags & VLIB_PHYSMEM_F_SHARED) ?
158 CLIB_MEM_VM_F_SHARED : CLIB_MEM_VM_F_LOCKED;
160 if ((flags & VLIB_PHYSMEM_F_HUGETLB))
162 alloc.flags |= CLIB_MEM_VM_F_HUGETLB;
163 alloc.flags |= CLIB_MEM_VM_F_HUGETLB_PREALLOC;
164 alloc.flags |= CLIB_MEM_VM_F_NUMA_FORCE;
168 alloc.flags |= CLIB_MEM_VM_F_NUMA_PREFER;
171 error = clib_mem_vm_ext_alloc (&alloc);
175 pr->index = pr - vpm->regions;
178 pr->mem = alloc.addr;
179 pr->log2_page_size = alloc.log2_page_size;
180 pr->n_pages = alloc.n_pages;
181 pr->size = (u64) pr->n_pages << (u64) pr->log2_page_size;
182 pr->page_mask = (1ull << pr->log2_page_size) - 1;
183 pr->numa_node = numa_node;
184 pr->name = format (0, "%s%c", name, 0);
186 for (i = 0; i < pr->n_pages; i++)
188 void *ptr = pr->mem + ((u64) i << pr->log2_page_size);
190 if ((move_pages (0, 1, &ptr, 0, &node, 0) == 0) && (numa_node != node))
192 clib_warning ("physmem page for region \'%s\' allocated on the"
193 " wrong numa node (requested %u actual %u)",
194 pr->name, pr->numa_node, node, i);
199 pr->page_table = clib_mem_vm_get_paddr (pr->mem, pr->log2_page_size,
202 linux_vfio_dma_map_regions (vm);
204 if (flags & VLIB_PHYSMEM_F_INIT_MHEAP)
206 #if USE_DLMALLOC == 0
207 pr->heap = mheap_alloc_with_flags (pr->mem, pr->size,
208 /* Don't want mheap mmap/munmap with IO memory. */
209 MHEAP_FLAG_DISABLE_VM |
210 MHEAP_FLAG_THREAD_SAFE);
212 pr->heap = create_mspace_with_base (pr->mem, pr->size, 1 /* locked */ );
213 mspace_disable_expand (pr->heap);
222 clib_memset (pr, 0, sizeof (*pr));
223 pool_put (vpm->regions, pr);
230 unix_physmem_region_free (vlib_main_t * vm, vlib_physmem_region_index_t idx)
232 vlib_physmem_main_t *vpm = &physmem_main;
233 vlib_physmem_region_t *pr = vlib_physmem_get_region (vm, idx);
237 munmap (pr->mem, pr->size);
239 pool_put (vpm->regions, pr);
243 unix_physmem_init (vlib_main_t * vm)
245 vlib_physmem_main_t *vpm = &physmem_main;
246 clib_error_t *error = 0;
249 /* Avoid multiple calls. */
250 if (vm->os_physmem_alloc_aligned)
253 /* check if pagemap is accessible */
254 pt = clib_mem_vm_get_paddr (&pt, min_log2 (sysconf (_SC_PAGESIZE)), 1);
256 vpm->flags |= VLIB_PHYSMEM_MAIN_F_HAVE_PAGEMAP;
259 if ((error = linux_vfio_init (vm)))
262 vm->os_physmem_alloc_aligned = unix_physmem_alloc_aligned;
263 vm->os_physmem_free = unix_physmem_free;
264 vm->os_physmem_region_alloc = unix_physmem_region_alloc;
265 vm->os_physmem_region_free = unix_physmem_region_free;
270 static clib_error_t *
271 show_physmem (vlib_main_t * vm,
272 unformat_input_t * input, vlib_cli_command_t * cmd)
274 vlib_physmem_main_t *vpm = &physmem_main;
275 vlib_physmem_region_t *pr;
278 pool_foreach (pr, vpm->regions, (
280 vlib_cli_output (vm, "index %u name '%s' page-size %uKB num-pages %d "
281 "numa-node %u fd %d\n",
282 pr->index, pr->name, (1 << (pr->log2_page_size -10)),
283 pr->n_pages, pr->numa_node, pr->fd);
285 vlib_cli_output (vm, " %U", format_mheap, pr->heap, /* verbose */ 1);
287 vlib_cli_output (vm, " no heap\n");
294 VLIB_CLI_COMMAND (show_physmem_command, static) = {
295 .path = "show physmem",
296 .short_help = "Show physical memory allocation",
297 .function = show_physmem,
302 * fd.io coding-style-patch-verification: ON
305 * eval: (c-set-style "gnu")