Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | /* * Copyright (c) 2020 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/shell/shell.h> #include <stdlib.h> #include <zephyr/drivers/virtualization/ivshmem.h> static const struct device *ivshmem; #ifdef CONFIG_IVSHMEM_DOORBELL #define STACK_SIZE 512 static struct k_poll_signal doorbell_sig = K_POLL_SIGNAL_INITIALIZER(doorbell_sig); static struct k_poll_event doorbell_evt = K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY, &doorbell_sig); K_THREAD_STACK_DEFINE(doorbell_stack, STACK_SIZE); static bool doorbell_started; static struct k_thread doorbell_thread; static void doorbell_notification_thread(const struct shell *sh) { while (1) { unsigned int signaled; int vector; k_poll(&doorbell_evt, 1, K_FOREVER); k_poll_signal_check(&doorbell_sig, &signaled, &vector); if (signaled == 0) { continue; } shell_fprintf(sh, SHELL_NORMAL, "Received a notification on vector %u\n", (unsigned int)vector); k_poll_signal_init(&doorbell_sig); } } #endif /* CONFIG_IVSHMEM_DOORBELL */ static bool get_ivshmem(const struct shell *sh) { if (ivshmem == NULL) { ivshmem = DEVICE_DT_GET_ONE(qemu_ivshmem); if (!device_is_ready(ivshmem)) { shell_error(sh, "IVshmem device is not ready"); } } return ivshmem != NULL ? true : false; } static int cmd_ivshmem_shmem(const struct shell *sh, size_t argc, char **argv) { uintptr_t mem; size_t size; uint32_t id; uint16_t vectors; if (!get_ivshmem(sh)) { return 0; } size = ivshmem_get_mem(ivshmem, &mem); id = ivshmem_get_id(ivshmem); vectors = ivshmem_get_vectors(ivshmem); shell_fprintf(sh, SHELL_NORMAL, "IVshmem up and running: \n" "\tShared memory: 0x%x of size %u bytes\n" "\tPeer id: %u\n" "\tNotification vectors: %u\n", mem, size, id, vectors); return 0; } static int cmd_ivshmem_dump(const struct shell *sh, size_t argc, char **argv) { uintptr_t dump_pos; size_t dump_size; uintptr_t mem; size_t size; if (!get_ivshmem(sh)) { return 0; } dump_pos = strtol(argv[1], NULL, 10); dump_size = strtol(argv[2], NULL, 10); size = ivshmem_get_mem(ivshmem, &mem); if (dump_size > size) { shell_error(sh, "Size is too big"); } else if (dump_pos > size) { shell_error(sh, "Position is out of the shared memory"); } else if ((mem + dump_pos + dump_size) > (mem + size)) { shell_error(sh, "Position and size overflow"); } else { shell_hexdump(sh, (const uint8_t *)mem+dump_pos, dump_size); } return 0; } static int cmd_ivshmem_int(const struct shell *sh, size_t argc, char **argv) { int peer_id; int vector; int ret; if (!IS_ENABLED(CONFIG_IVSHMEM_DOORBELL)) { shell_error(sh, "CONFIG_IVSHMEM_DOORBELL is not enabled"); return 0; } if (!get_ivshmem(sh)) { return 0; } peer_id = strtol(argv[1], NULL, 10); vector = strtol(argv[2], NULL, 10); ret = ivshmem_int_peer(ivshmem, (uint16_t)peer_id, (uint16_t)vector); if (ret != 0) { shell_error(sh, "Could not notify peer %u on %u. status %d", peer_id, vector, ret); return -EIO; } shell_fprintf(sh, SHELL_NORMAL, "Notification sent to peer %u on vector %u\n", peer_id, vector); return 0; } static int cmd_ivshmem_get_notified(const struct shell *sh, size_t argc, char **argv) { #ifdef CONFIG_IVSHMEM_DOORBELL int vector; if (!get_ivshmem(sh)) { return 0; } vector = strtol(argv[1], NULL, 10); if (ivshmem_register_handler(ivshmem, &doorbell_sig, (uint16_t)vector)) { shell_error(sh, "Could not get notifications on vector %u", vector); return -EIO; } shell_fprintf(sh, SHELL_NORMAL, "Notifications enabled for vector %u\n", vector); if (!doorbell_started) { k_tid_t tid; tid = k_thread_create( &doorbell_thread, doorbell_stack, STACK_SIZE, (k_thread_entry_t)doorbell_notification_thread, (void *)sh, NULL, NULL, K_PRIO_COOP(2), 0, K_NO_WAIT); if (!tid) { shell_error(sh, "Cannot start notification thread"); return -ENOEXEC; } k_thread_name_set(tid, "notification_thread"); k_thread_start(tid); doorbell_started = true; } #else shell_error(sh, "CONFIG_IVSHMEM_DOORBELL is not enabled"); #endif return 0; } SHELL_STATIC_SUBCMD_SET_CREATE(sub_ivshmem_cmds, SHELL_CMD(shmem, NULL, "Show shared memory info", cmd_ivshmem_shmem), SHELL_CMD_ARG(dump, NULL, "Dump shared memory content", cmd_ivshmem_dump, 3, 0), SHELL_CMD_ARG(int_peer, NULL, "Notify a vector on a peer", cmd_ivshmem_int, 3, 0), SHELL_CMD_ARG(get_notified, NULL, "Get notification on vector", cmd_ivshmem_get_notified, 2, 0), SHELL_SUBCMD_SET_END ); SHELL_CMD_REGISTER(ivshmem, &sub_ivshmem_cmds, "IVshmem information", cmd_ivshmem_shmem); SHELL_CMD_ARG_REGISTER(ivshmem_dump, &sub_ivshmem_cmds, "Dump shared memory content", cmd_ivshmem_dump, 3, 0); SHELL_CMD_ARG_REGISTER(ivshmem_int, &sub_ivshmem_cmds, "Notify a vector on an ivshmem peer", cmd_ivshmem_int, 3, 0); SHELL_CMD_ARG_REGISTER(ivshmem_get_notified, &sub_ivshmem_cmds, "Get notification on vector", cmd_ivshmem_get_notified, 2, 0); |