1 // SPDX-License-Identifier: GPL-2.0
4 * (C) 2020 EPAM Systems Inc.
11 #include <asm/global_data.h>
13 #include <linux/bug.h>
16 #include <xen/events.h>
18 #include <xen/interface/sched.h>
19 #include <xen/interface/hvm/hvm_op.h>
20 #include <xen/interface/hvm/params.h>
21 #include <xen/interface/io/console.h>
22 #include <xen/interface/io/ring.h>
24 DECLARE_GLOBAL_DATA_PTR;
29 * struct xen_uart_priv - Structure representing a Xen UART info
30 * @intf: Console I/O interface for Xen guest OSes
31 * @evtchn: Console event channel
33 struct xen_uart_priv {
34 struct xencons_interface *intf;
38 int xen_serial_setbrg(struct udevice *dev, int baudrate)
43 static int xen_serial_probe(struct udevice *dev)
45 struct xen_uart_priv *priv = dev_get_priv(dev);
50 ret = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &val);
51 if (ret < 0 || val == 0)
57 ret = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &val);
65 priv->intf = (struct xencons_interface *)(gfn << XEN_PAGE_SHIFT);
70 static int xen_serial_pending(struct udevice *dev, bool input)
72 struct xen_uart_priv *priv = dev_get_priv(dev);
73 struct xencons_interface *intf = priv->intf;
75 if (!input || intf->in_cons == intf->in_prod)
81 static int xen_serial_getc(struct udevice *dev)
83 struct xen_uart_priv *priv = dev_get_priv(dev);
84 struct xencons_interface *intf = priv->intf;
85 XENCONS_RING_IDX cons;
88 while (intf->in_cons == intf->in_prod)
92 mb(); /* get pointers before reading ring */
94 c = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
96 mb(); /* read ring before consuming */
99 notify_remote_via_evtchn(priv->evtchn);
104 static int __write_console(struct udevice *dev, const char *data, int len)
106 struct xen_uart_priv *priv = dev_get_priv(dev);
107 struct xencons_interface *intf = priv->intf;
108 XENCONS_RING_IDX cons, prod;
111 cons = intf->out_cons;
112 prod = intf->out_prod;
113 mb(); /* Update pointer */
115 WARN_ON((prod - cons) > sizeof(intf->out));
117 while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
118 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
120 mb(); /* Update data before pointer */
121 intf->out_prod = prod;
124 notify_remote_via_evtchn(priv->evtchn);
129 static int write_console(struct udevice *dev, const char *data, int len)
132 * Make sure the whole buffer is emitted, polling if
133 * necessary. We don't ever want to rely on the hvc daemon
134 * because the most interesting console output is when the
135 * kernel is crippled.
138 int sent = __write_console(dev, data, len);
144 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
150 static int xen_serial_putc(struct udevice *dev, const char ch)
152 write_console(dev, &ch, 1);
157 static const struct dm_serial_ops xen_serial_ops = {
158 .putc = xen_serial_putc,
159 .getc = xen_serial_getc,
160 .pending = xen_serial_pending,
163 #if CONFIG_IS_ENABLED(OF_CONTROL)
164 static const struct udevice_id xen_serial_ids[] = {
165 { .compatible = "xen,xen" },
170 U_BOOT_DRIVER(serial_xen) = {
171 .name = "serial_xen",
173 #if CONFIG_IS_ENABLED(OF_CONTROL)
174 .of_match = xen_serial_ids,
176 .priv_auto = sizeof(struct xen_uart_priv),
177 .probe = xen_serial_probe,
178 .ops = &xen_serial_ops,
179 #if !CONFIG_IS_ENABLED(OF_CONTROL)
180 .flags = DM_FLAG_PRE_RELOC,