Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 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, |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 34 | u32 log2_page_sz, u32 numa_node, |
| 35 | u32 * map_index) |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 36 | { |
| 37 | clib_pmalloc_main_t *pm = vm->physmem_main.pmalloc_main; |
| 38 | vlib_physmem_main_t *vpm = &vm->physmem_main; |
| 39 | vlib_physmem_map_t *map; |
| 40 | clib_pmalloc_arena_t *a; |
| 41 | clib_error_t *error = 0; |
| 42 | void *va; |
Damjan Marion | 8e8d3c8 | 2018-10-23 22:54:40 +0200 | [diff] [blame] | 43 | uword i; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 44 | |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 45 | va = clib_pmalloc_create_shared_arena (pm, name, size, log2_page_sz, |
| 46 | numa_node); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 47 | |
| 48 | if (va == 0) |
| 49 | return clib_error_return (0, "%U", format_clib_error, |
| 50 | clib_pmalloc_last_error (pm)); |
| 51 | |
| 52 | a = clib_pmalloc_get_arena (pm, va); |
| 53 | |
| 54 | pool_get (vpm->maps, map); |
| 55 | *map_index = map->index = map - vpm->maps; |
| 56 | map->base = va; |
| 57 | map->fd = a->fd; |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 58 | map->n_pages = a->n_pages * a->subpages_per_page; |
| 59 | map->log2_page_size = a->log2_subpage_sz; |
Damjan Marion | d2bfb78 | 2019-01-07 20:56:04 +0100 | [diff] [blame] | 60 | map->numa_node = a->numa_node; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 61 | |
| 62 | for (i = 0; i < a->n_pages; i++) |
| 63 | { |
Damjan Marion | 567e61d | 2018-10-24 17:08:26 +0200 | [diff] [blame] | 64 | uword pa = |
| 65 | clib_pmalloc_get_pa (pm, (u8 *) va + (i << a->log2_subpage_sz)); |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 66 | |
| 67 | /* maybe iova */ |
| 68 | if (pa == 0) |
| 69 | pa = pointer_to_uword (va); |
| 70 | |
| 71 | vec_add1 (map->page_table, pa); |
| 72 | } |
| 73 | |
| 74 | return error; |
| 75 | } |
| 76 | |
| 77 | vlib_physmem_map_t * |
| 78 | vlib_physmem_get_map (vlib_main_t * vm, u32 index) |
| 79 | { |
| 80 | vlib_physmem_main_t *vpm = &vm->physmem_main; |
| 81 | return pool_elt_at_index (vpm->maps, index); |
| 82 | } |
| 83 | |
| 84 | clib_error_t * |
| 85 | vlib_physmem_init (vlib_main_t * vm) |
| 86 | { |
| 87 | vlib_physmem_main_t *vpm = &vm->physmem_main; |
| 88 | clib_error_t *error = 0; |
| 89 | u64 *pt = 0; |
| 90 | void *p; |
| 91 | |
| 92 | /* check if pagemap is accessible */ |
| 93 | pt = clib_mem_vm_get_paddr (&pt, min_log2 (sysconf (_SC_PAGESIZE)), 1); |
Damjan Marion | 8e8d3c8 | 2018-10-23 22:54:40 +0200 | [diff] [blame] | 94 | if (pt && pt[0]) |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 95 | vpm->flags |= VLIB_PHYSMEM_MAIN_F_HAVE_PAGEMAP; |
| 96 | vec_free (pt); |
| 97 | |
| 98 | if ((error = linux_vfio_init (vm))) |
| 99 | return error; |
| 100 | |
| 101 | p = clib_mem_alloc_aligned (sizeof (clib_pmalloc_main_t), |
| 102 | CLIB_CACHE_LINE_BYTES); |
| 103 | memset (p, 0, sizeof (clib_pmalloc_main_t)); |
| 104 | vpm->pmalloc_main = (clib_pmalloc_main_t *) p; |
| 105 | clib_pmalloc_init (vpm->pmalloc_main, 0); |
| 106 | |
| 107 | return error; |
| 108 | } |
| 109 | |
| 110 | static clib_error_t * |
| 111 | show_physmem (vlib_main_t * vm, |
| 112 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 113 | { |
| 114 | vlib_physmem_main_t *vpm = &vm->physmem_main; |
| 115 | unformat_input_t _line_input, *line_input = &_line_input; |
Mohsin Kazmi | 6ec99c3 | 2018-11-07 16:55:18 +0100 | [diff] [blame] | 116 | u32 verbose = 0, map = 0; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 117 | |
| 118 | if (unformat_user (input, unformat_line_input, line_input)) |
| 119 | { |
| 120 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 121 | { |
| 122 | if (unformat (line_input, "verbose")) |
| 123 | verbose = 1; |
| 124 | else if (unformat (line_input, "v")) |
| 125 | verbose = 1; |
| 126 | else if (unformat (line_input, "detail")) |
| 127 | verbose = 2; |
| 128 | else if (unformat (line_input, "d")) |
| 129 | verbose = 2; |
Mohsin Kazmi | 6ec99c3 | 2018-11-07 16:55:18 +0100 | [diff] [blame] | 130 | else if (unformat (line_input, "map")) |
| 131 | map = 1; |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 132 | else |
| 133 | break; |
| 134 | } |
| 135 | unformat_free (line_input); |
| 136 | } |
| 137 | |
Mohsin Kazmi | 6ec99c3 | 2018-11-07 16:55:18 +0100 | [diff] [blame] | 138 | if (map) |
| 139 | vlib_cli_output (vm, " %U", format_pmalloc_map, vpm->pmalloc_main); |
| 140 | else |
| 141 | vlib_cli_output (vm, " %U", format_pmalloc, vpm->pmalloc_main, verbose); |
| 142 | |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /* *INDENT-OFF* */ |
| 147 | VLIB_CLI_COMMAND (show_physmem_command, static) = { |
| 148 | .path = "show physmem", |
Mohsin Kazmi | 6ec99c3 | 2018-11-07 16:55:18 +0100 | [diff] [blame] | 149 | .short_help = "show physmem [verbose | detail | map]", |
Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 150 | .function = show_physmem, |
| 151 | }; |
| 152 | /* *INDENT-ON* */ |
| 153 | |
| 154 | /* |
| 155 | * fd.io coding-style-patch-verification: ON |
| 156 | * |
| 157 | * Local Variables: |
| 158 | * eval: (c-set-style "gnu") |
| 159 | * End: |
| 160 | */ |