1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright (c) 2010, 2014 The Linux Foundation. All rights reserved. */
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include <linux/console.h>
5*4882a593Smuzhiyun #include <linux/init.h>
6*4882a593Smuzhiyun #include <linux/kfifo.h>
7*4882a593Smuzhiyun #include <linux/moduleparam.h>
8*4882a593Smuzhiyun #include <linux/serial.h>
9*4882a593Smuzhiyun #include <linux/serial_core.h>
10*4882a593Smuzhiyun #include <linux/spinlock.h>
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <asm/dcc.h>
13*4882a593Smuzhiyun #include <asm/processor.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include "hvc_console.h"
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /*
18*4882a593Smuzhiyun * Disable DCC driver at runtime. Want driver enabled for GKI, but some devices
19*4882a593Smuzhiyun * do not support the registers and crash when driver pokes the registers
20*4882a593Smuzhiyun */
21*4882a593Smuzhiyun static bool enable;
22*4882a593Smuzhiyun module_param(enable, bool, 0444);
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /* DCC Status Bits */
25*4882a593Smuzhiyun #define DCC_STATUS_RX (1 << 30)
26*4882a593Smuzhiyun #define DCC_STATUS_TX (1 << 29)
27*4882a593Smuzhiyun
dcc_uart_console_putchar(struct uart_port * port,int ch)28*4882a593Smuzhiyun static void dcc_uart_console_putchar(struct uart_port *port, int ch)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun while (__dcc_getstatus() & DCC_STATUS_TX)
31*4882a593Smuzhiyun cpu_relax();
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun __dcc_putchar(ch);
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun
dcc_early_write(struct console * con,const char * s,unsigned n)36*4882a593Smuzhiyun static void dcc_early_write(struct console *con, const char *s, unsigned n)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun struct earlycon_device *dev = con->data;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun uart_console_write(&dev->port, s, n, dcc_uart_console_putchar);
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun
dcc_early_console_setup(struct earlycon_device * device,const char * opt)43*4882a593Smuzhiyun static int __init dcc_early_console_setup(struct earlycon_device *device,
44*4882a593Smuzhiyun const char *opt)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun device->con->write = dcc_early_write;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun return 0;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun EARLYCON_DECLARE(dcc, dcc_early_console_setup);
52*4882a593Smuzhiyun
hvc_dcc_put_chars(uint32_t vt,const char * buf,int count)53*4882a593Smuzhiyun static int hvc_dcc_put_chars(uint32_t vt, const char *buf, int count)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun int i;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun for (i = 0; i < count; i++) {
58*4882a593Smuzhiyun while (__dcc_getstatus() & DCC_STATUS_TX)
59*4882a593Smuzhiyun cpu_relax();
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun __dcc_putchar(buf[i]);
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun return count;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
hvc_dcc_get_chars(uint32_t vt,char * buf,int count)67*4882a593Smuzhiyun static int hvc_dcc_get_chars(uint32_t vt, char *buf, int count)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun int i;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun for (i = 0; i < count; ++i)
72*4882a593Smuzhiyun if (__dcc_getstatus() & DCC_STATUS_RX)
73*4882a593Smuzhiyun buf[i] = __dcc_getchar();
74*4882a593Smuzhiyun else
75*4882a593Smuzhiyun break;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun return i;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun * Check if the DCC is enabled. If CONFIG_HVC_DCC_SERIALIZE_SMP is enabled,
82*4882a593Smuzhiyun * then we assume then this function will be called first on core 0. That
83*4882a593Smuzhiyun * way, dcc_core0_available will be true only if it's available on core 0.
84*4882a593Smuzhiyun */
hvc_dcc_check(void)85*4882a593Smuzhiyun static bool hvc_dcc_check(void)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun unsigned long time = jiffies + (HZ / 10);
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun #ifdef CONFIG_HVC_DCC_SERIALIZE_SMP
90*4882a593Smuzhiyun static bool dcc_core0_available;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun * If we're not on core 0, but we previously confirmed that DCC is
94*4882a593Smuzhiyun * active, then just return true.
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun if (smp_processor_id() && dcc_core0_available)
97*4882a593Smuzhiyun return true;
98*4882a593Smuzhiyun #endif
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /* Write a test character to check if it is handled */
101*4882a593Smuzhiyun __dcc_putchar('\n');
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun while (time_is_after_jiffies(time)) {
104*4882a593Smuzhiyun if (!(__dcc_getstatus() & DCC_STATUS_TX)) {
105*4882a593Smuzhiyun #ifdef CONFIG_HVC_DCC_SERIALIZE_SMP
106*4882a593Smuzhiyun dcc_core0_available = true;
107*4882a593Smuzhiyun #endif
108*4882a593Smuzhiyun return true;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun return false;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun #ifdef CONFIG_HVC_DCC_SERIALIZE_SMP
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun static void dcc_put_work_fn(struct work_struct *work);
118*4882a593Smuzhiyun static void dcc_get_work_fn(struct work_struct *work);
119*4882a593Smuzhiyun static DECLARE_WORK(dcc_pwork, dcc_put_work_fn);
120*4882a593Smuzhiyun static DECLARE_WORK(dcc_gwork, dcc_get_work_fn);
121*4882a593Smuzhiyun static DEFINE_SPINLOCK(dcc_lock);
122*4882a593Smuzhiyun static DEFINE_KFIFO(inbuf, unsigned char, 128);
123*4882a593Smuzhiyun static DEFINE_KFIFO(outbuf, unsigned char, 1024);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /*
126*4882a593Smuzhiyun * Workqueue function that writes the output FIFO to the DCC on core 0.
127*4882a593Smuzhiyun */
dcc_put_work_fn(struct work_struct * work)128*4882a593Smuzhiyun static void dcc_put_work_fn(struct work_struct *work)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun unsigned char ch;
131*4882a593Smuzhiyun unsigned long irqflags;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun spin_lock_irqsave(&dcc_lock, irqflags);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /* While there's data in the output FIFO, write it to the DCC */
136*4882a593Smuzhiyun while (kfifo_get(&outbuf, &ch))
137*4882a593Smuzhiyun hvc_dcc_put_chars(0, &ch, 1);
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /* While we're at it, check for any input characters */
140*4882a593Smuzhiyun while (!kfifo_is_full(&inbuf)) {
141*4882a593Smuzhiyun if (!hvc_dcc_get_chars(0, &ch, 1))
142*4882a593Smuzhiyun break;
143*4882a593Smuzhiyun kfifo_put(&inbuf, ch);
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun spin_unlock_irqrestore(&dcc_lock, irqflags);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /*
150*4882a593Smuzhiyun * Workqueue function that reads characters from DCC and puts them into the
151*4882a593Smuzhiyun * input FIFO.
152*4882a593Smuzhiyun */
dcc_get_work_fn(struct work_struct * work)153*4882a593Smuzhiyun static void dcc_get_work_fn(struct work_struct *work)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun unsigned char ch;
156*4882a593Smuzhiyun unsigned long irqflags;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun * Read characters from DCC and put them into the input FIFO, as
160*4882a593Smuzhiyun * long as there is room and we have characters to read.
161*4882a593Smuzhiyun */
162*4882a593Smuzhiyun spin_lock_irqsave(&dcc_lock, irqflags);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun while (!kfifo_is_full(&inbuf)) {
165*4882a593Smuzhiyun if (!hvc_dcc_get_chars(0, &ch, 1))
166*4882a593Smuzhiyun break;
167*4882a593Smuzhiyun kfifo_put(&inbuf, ch);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun spin_unlock_irqrestore(&dcc_lock, irqflags);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /*
173*4882a593Smuzhiyun * Write characters directly to the DCC if we're on core 0 and the FIFO
174*4882a593Smuzhiyun * is empty, or write them to the FIFO if we're not.
175*4882a593Smuzhiyun */
hvc_dcc0_put_chars(uint32_t vt,const char * buf,int count)176*4882a593Smuzhiyun static int hvc_dcc0_put_chars(uint32_t vt, const char *buf,
177*4882a593Smuzhiyun int count)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun int len;
180*4882a593Smuzhiyun unsigned long irqflags;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun spin_lock_irqsave(&dcc_lock, irqflags);
183*4882a593Smuzhiyun if (smp_processor_id() || (!kfifo_is_empty(&outbuf))) {
184*4882a593Smuzhiyun len = kfifo_in(&outbuf, buf, count);
185*4882a593Smuzhiyun spin_unlock_irqrestore(&dcc_lock, irqflags);
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun * We just push data to the output FIFO, so schedule the
188*4882a593Smuzhiyun * workqueue that will actually write that data to DCC.
189*4882a593Smuzhiyun */
190*4882a593Smuzhiyun schedule_work_on(0, &dcc_pwork);
191*4882a593Smuzhiyun return len;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /*
195*4882a593Smuzhiyun * If we're already on core 0, and the FIFO is empty, then just
196*4882a593Smuzhiyun * write the data to DCC.
197*4882a593Smuzhiyun */
198*4882a593Smuzhiyun len = hvc_dcc_put_chars(vt, buf, count);
199*4882a593Smuzhiyun spin_unlock_irqrestore(&dcc_lock, irqflags);
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun return len;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun /*
205*4882a593Smuzhiyun * Read characters directly from the DCC if we're on core 0 and the FIFO
206*4882a593Smuzhiyun * is empty, or read them from the FIFO if we're not.
207*4882a593Smuzhiyun */
hvc_dcc0_get_chars(uint32_t vt,char * buf,int count)208*4882a593Smuzhiyun static int hvc_dcc0_get_chars(uint32_t vt, char *buf, int count)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun int len;
211*4882a593Smuzhiyun unsigned long irqflags;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun spin_lock_irqsave(&dcc_lock, irqflags);
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun if (smp_processor_id() || (!kfifo_is_empty(&inbuf))) {
216*4882a593Smuzhiyun len = kfifo_out(&inbuf, buf, count);
217*4882a593Smuzhiyun spin_unlock_irqrestore(&dcc_lock, irqflags);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /*
220*4882a593Smuzhiyun * If the FIFO was empty, there may be characters in the DCC
221*4882a593Smuzhiyun * that we haven't read yet. Schedule a workqueue to fill
222*4882a593Smuzhiyun * the input FIFO, so that the next time this function is
223*4882a593Smuzhiyun * called, we'll have data.
224*4882a593Smuzhiyun */
225*4882a593Smuzhiyun if (!len)
226*4882a593Smuzhiyun schedule_work_on(0, &dcc_gwork);
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun return len;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /*
232*4882a593Smuzhiyun * If we're already on core 0, and the FIFO is empty, then just
233*4882a593Smuzhiyun * read the data from DCC.
234*4882a593Smuzhiyun */
235*4882a593Smuzhiyun len = hvc_dcc_get_chars(vt, buf, count);
236*4882a593Smuzhiyun spin_unlock_irqrestore(&dcc_lock, irqflags);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun return len;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun static const struct hv_ops hvc_dcc_get_put_ops = {
242*4882a593Smuzhiyun .get_chars = hvc_dcc0_get_chars,
243*4882a593Smuzhiyun .put_chars = hvc_dcc0_put_chars,
244*4882a593Smuzhiyun };
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun #else
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun static const struct hv_ops hvc_dcc_get_put_ops = {
249*4882a593Smuzhiyun .get_chars = hvc_dcc_get_chars,
250*4882a593Smuzhiyun .put_chars = hvc_dcc_put_chars,
251*4882a593Smuzhiyun };
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun #endif
254*4882a593Smuzhiyun
hvc_dcc_console_init(void)255*4882a593Smuzhiyun static int __init hvc_dcc_console_init(void)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun int ret;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun if (!enable || !hvc_dcc_check())
260*4882a593Smuzhiyun return -ENODEV;
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun /* Returns -1 if error */
263*4882a593Smuzhiyun ret = hvc_instantiate(0, 0, &hvc_dcc_get_put_ops);
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun return ret < 0 ? -ENODEV : 0;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun console_initcall(hvc_dcc_console_init);
268*4882a593Smuzhiyun
hvc_dcc_init(void)269*4882a593Smuzhiyun static int __init hvc_dcc_init(void)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun struct hvc_struct *p;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun if (!enable || !hvc_dcc_check())
274*4882a593Smuzhiyun return -ENODEV;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun p = hvc_alloc(0, 0, &hvc_dcc_get_put_ops, 128);
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun return PTR_ERR_OR_ZERO(p);
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun device_initcall(hvc_dcc_init);
281