1 // SPDX-License-Identifier: GPL-2.0
4 * (C) 2020 EPAM Systems Inc.
8 #include <debug_uart.h>
13 #include <linux/bug.h>
16 #include <xen/events.h>
18 #include <xen/interface/sched.h>
19 #include <xen/interface/xen.h>
20 #include <xen/interface/hvm/hvm_op.h>
21 #include <xen/interface/hvm/params.h>
22 #include <xen/interface/io/console.h>
23 #include <xen/interface/io/ring.h>
25 #include <asm/xen/hypercall.h>
27 DECLARE_GLOBAL_DATA_PTR;
32 * struct xen_uart_priv - Structure representing a Xen UART info
33 * @intf: Console I/O interface for Xen guest OSes
34 * @evtchn: Console event channel
36 struct xen_uart_priv {
37 struct xencons_interface *intf;
41 int xen_serial_setbrg(struct udevice *dev, int baudrate)
46 static int xen_serial_probe(struct udevice *dev)
48 struct xen_uart_priv *priv = dev_get_priv(dev);
53 ret = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &val);
54 if (ret < 0 || val == 0)
60 ret = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &val);
68 priv->intf = (struct xencons_interface *)(gfn << XEN_PAGE_SHIFT);
73 static int xen_serial_pending(struct udevice *dev, bool input)
75 struct xen_uart_priv *priv = dev_get_priv(dev);
76 struct xencons_interface *intf = priv->intf;
78 if (!input || intf->in_cons == intf->in_prod)
84 static int xen_serial_getc(struct udevice *dev)
86 struct xen_uart_priv *priv = dev_get_priv(dev);
87 struct xencons_interface *intf = priv->intf;
88 XENCONS_RING_IDX cons;
91 while (intf->in_cons == intf->in_prod)
95 mb(); /* get pointers before reading ring */
97 c = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
99 mb(); /* read ring before consuming */
100 intf->in_cons = cons;
102 notify_remote_via_evtchn(priv->evtchn);
107 static int __write_console(struct udevice *dev, const char *data, int len)
109 struct xen_uart_priv *priv = dev_get_priv(dev);
110 struct xencons_interface *intf = priv->intf;
111 XENCONS_RING_IDX cons, prod;
114 cons = intf->out_cons;
115 prod = intf->out_prod;
116 mb(); /* Update pointer */
118 WARN_ON((prod - cons) > sizeof(intf->out));
120 while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
121 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
123 mb(); /* Update data before pointer */
124 intf->out_prod = prod;
127 notify_remote_via_evtchn(priv->evtchn);
132 static int write_console(struct udevice *dev, const char *data, int len)
135 * Make sure the whole buffer is emitted, polling if
136 * necessary. We don't ever want to rely on the hvc daemon
137 * because the most interesting console output is when the
138 * kernel is crippled.
141 int sent = __write_console(dev, data, len);
147 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
153 static int xen_serial_putc(struct udevice *dev, const char ch)
155 write_console(dev, &ch, 1);
160 static const struct dm_serial_ops xen_serial_ops = {
161 .putc = xen_serial_putc,
162 .getc = xen_serial_getc,
163 .pending = xen_serial_pending,
166 #if CONFIG_IS_ENABLED(OF_CONTROL)
167 static const struct udevice_id xen_serial_ids[] = {
168 { .compatible = "xen,xen" },
173 U_BOOT_DRIVER(serial_xen) = {
174 .name = "serial_xen",
176 #if CONFIG_IS_ENABLED(OF_CONTROL)
177 .of_match = xen_serial_ids,
179 .priv_auto_alloc_size = sizeof(struct xen_uart_priv),
180 .probe = xen_serial_probe,
181 .ops = &xen_serial_ops,
182 .flags = DM_FLAG_PRE_RELOC,
185 #if defined(CONFIG_DEBUG_UART_XEN)
186 static inline void _debug_uart_init(void) {}
188 static inline void _debug_uart_putc(int c)
190 #if CONFIG_IS_ENABLED(ARM)
193 /* the type cast should work on LE only */
194 HYPERVISOR_console_io(CONSOLEIO_write, 1, (char *)&ch);