|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 EPAM Systems |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <arch/arm64/hypercall.h> |
| 8 | +#include <xen/console.h> |
| 9 | +#include <xen/events.h> |
| 10 | +#include <xen/generic.h> |
| 11 | +#include <xen/hvm.h> |
| 12 | +#include <xen/public/io/console.h> |
| 13 | +#include <xen/public/xen.h> |
| 14 | + |
| 15 | +#include <device.h> |
| 16 | +#include <init.h> |
| 17 | +#include <kernel.h> |
| 18 | +#include <logging/log.h> |
| 19 | +#include <sys/device_mmio.h> |
| 20 | + |
| 21 | +LOG_MODULE_REGISTER(uart_hvc_xen); |
| 22 | + |
| 23 | +static struct hvc_xen_data hvc_data = {0}; |
| 24 | + |
| 25 | +static int read_from_ring(const struct device *dev, char *str, int len) |
| 26 | +{ |
| 27 | + int recv = 0; |
| 28 | + struct hvc_xen_data *hvc_data = dev->data; |
| 29 | + XENCONS_RING_IDX cons = hvc_data->intf->in_cons; |
| 30 | + XENCONS_RING_IDX prod = hvc_data->intf->in_prod; |
| 31 | + XENCONS_RING_IDX in_idx = 0; |
| 32 | + |
| 33 | + compiler_barrier(); |
| 34 | + __ASSERT((prod - cons) <= sizeof(hvc_data->intf->in), |
| 35 | + "Invalid input ring buffer"); |
| 36 | + |
| 37 | + while (cons != prod && recv < len) { |
| 38 | + in_idx = MASK_XENCONS_IDX(cons, hvc_data->intf->in); |
| 39 | + str[recv] = hvc_data->intf->in[in_idx]; |
| 40 | + recv++; |
| 41 | + cons++; |
| 42 | + } |
| 43 | + |
| 44 | + compiler_barrier(); |
| 45 | + hvc_data->intf->in_cons = cons; |
| 46 | + |
| 47 | + notify_evtchn(hvc_data->evtchn); |
| 48 | + return recv; |
| 49 | +} |
| 50 | + |
| 51 | +static int write_to_ring(const struct device *dev, const char *str, int len) |
| 52 | +{ |
| 53 | + int sent = 0; |
| 54 | + struct hvc_xen_data *hvc_data = dev->data; |
| 55 | + XENCONS_RING_IDX cons = hvc_data->intf->out_cons; |
| 56 | + XENCONS_RING_IDX prod = hvc_data->intf->out_prod; |
| 57 | + XENCONS_RING_IDX out_idx = 0; |
| 58 | + |
| 59 | + compiler_barrier(); |
| 60 | + __ASSERT((prod - cons) <= sizeof(hvc_data->intf->out), |
| 61 | + "Invalid output ring buffer"); |
| 62 | + |
| 63 | + while ((sent < len) && ((prod - cons) < sizeof(hvc_data->intf->out))) { |
| 64 | + out_idx = MASK_XENCONS_IDX(prod, hvc_data->intf->out); |
| 65 | + hvc_data->intf->out[out_idx] = str[sent]; |
| 66 | + prod++; |
| 67 | + sent++; |
| 68 | + } |
| 69 | + |
| 70 | + compiler_barrier(); |
| 71 | + hvc_data->intf->out_prod = prod; |
| 72 | + |
| 73 | + if (sent) { |
| 74 | + notify_evtchn(hvc_data->evtchn); |
| 75 | + } |
| 76 | + |
| 77 | + return sent; |
| 78 | +} |
| 79 | + |
| 80 | +static int xen_hvc_poll_in(const struct device *dev, |
| 81 | + unsigned char *c) |
| 82 | +{ |
| 83 | + int ret = 0; |
| 84 | + char temp; |
| 85 | + |
| 86 | + ret = read_from_ring(dev, &temp, sizeof(temp)); |
| 87 | + if (!ret) { |
| 88 | + /* Char was not received */ |
| 89 | + return -1; |
| 90 | + } |
| 91 | + |
| 92 | + *c = temp; |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | +static void xen_hvc_poll_out(const struct device *dev, |
| 97 | + unsigned char c) |
| 98 | +{ |
| 99 | + /* Not a good solution (notifying HV every time), but needed for poll_out */ |
| 100 | + (void) write_to_ring(dev, &c, sizeof(c)); |
| 101 | +} |
| 102 | + |
| 103 | +static const struct uart_driver_api xen_hvc_api = { |
| 104 | + .poll_in = xen_hvc_poll_in, |
| 105 | + .poll_out = xen_hvc_poll_out, |
| 106 | +}; |
| 107 | + |
| 108 | +int xen_console_init(const struct device *dev) |
| 109 | +{ |
| 110 | + int ret = 0; |
| 111 | + uint64_t console_pfn = 0; |
| 112 | + uintptr_t console_addr = 0; |
| 113 | + struct hvc_xen_data *data = dev->data; |
| 114 | + |
| 115 | + data->dev = dev; |
| 116 | + |
| 117 | + ret = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &data->evtchn); |
| 118 | + if (ret) { |
| 119 | + LOG_ERR("%s: failed to get Xen console evtchn, ret = %d\n", |
| 120 | + __func__, ret); |
| 121 | + return ret; |
| 122 | + } |
| 123 | + |
| 124 | + ret = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &console_pfn); |
| 125 | + if (ret) { |
| 126 | + LOG_ERR("%s: failed to get Xen console PFN, ret = %d\n", |
| 127 | + __func__, ret); |
| 128 | + return ret; |
| 129 | + } |
| 130 | + |
| 131 | + console_addr = (uintptr_t) (console_pfn << XEN_PAGE_SHIFT); |
| 132 | + device_map(DEVICE_MMIO_RAM_PTR(dev), console_addr, XEN_PAGE_SIZE, |
| 133 | + K_MEM_CACHE_WB); |
| 134 | + |
| 135 | + data->intf = (struct xencons_interface *) DEVICE_MMIO_GET(dev); |
| 136 | + |
| 137 | + LOG_INF("Xen HVC inited successfully\n"); |
| 138 | + |
| 139 | + return 0; |
| 140 | +} |
| 141 | + |
| 142 | +DEVICE_DT_DEFINE(DT_NODELABEL(xen_hvc), xen_console_init, NULL, &hvc_data, |
| 143 | + NULL, PRE_KERNEL_1, CONFIG_XEN_HVC_INIT_PRIORITY, |
| 144 | + &xen_hvc_api); |
| 145 | + |
| 146 | +#ifdef CONFIG_XEN_EARLY_CONSOLEIO |
| 147 | +extern void __printk_hook_install(int (*fn)(int)); |
| 148 | +extern void __stdout_hook_install(int (*fn)(int)); |
| 149 | + |
| 150 | +int xen_consoleio_putc(int c) |
| 151 | +{ |
| 152 | + char symbol = (char) c; |
| 153 | + |
| 154 | + HYPERVISOR_console_io(CONSOLEIO_write, sizeof(symbol), &symbol); |
| 155 | + return c; |
| 156 | +} |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +int consoleio_hooks_set(const struct device *dev) |
| 161 | +{ |
| 162 | + ARG_UNUSED(dev); |
| 163 | + |
| 164 | + /* Will be replaced with poll_in/poll_out by uart_console.c later on boot */ |
| 165 | + __stdout_hook_install(xen_consoleio_putc); |
| 166 | + __printk_hook_install(xen_consoleio_putc); |
| 167 | + |
| 168 | + return 0; |
| 169 | +} |
| 170 | + |
| 171 | +SYS_INIT(consoleio_hooks_set, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); |
| 172 | +#endif /* CONFIG_XEN_EARLY_CONSOLEIO */ |
0 commit comments