1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * vio driver interface to hvc_console.c
4 *
5 * This code was moved here to allow the remaining code to be reused as a
6 * generic polling mode with semi-reliable transport driver core to the
7 * console and tty subsystems.
8 *
9 *
10 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
11 * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
12 * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
13 * Copyright (C) 2004 IBM Corporation
14 *
15 * Additional Author(s):
16 * Ryan S. Arnold <rsa@us.ibm.com>
17 *
18 * TODO:
19 *
20 * - handle error in sending hvsi protocol packets
21 * - retry nego on subsequent sends ?
22 */
23
24 #undef DEBUG
25
26 #include <linux/types.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/slab.h>
30 #include <linux/console.h>
31 #include <linux/of.h>
32
33 #include <asm/hvconsole.h>
34 #include <asm/vio.h>
35 #include <asm/hvsi.h>
36 #include <asm/udbg.h>
37 #include <asm/machdep.h>
38
39 #include "hvc_console.h"
40
41 static const char hvc_driver_name[] = "hvc_console";
42
43 static const struct vio_device_id hvc_driver_table[] = {
44 {"serial", "hvterm1"},
45 #ifndef HVC_OLD_HVSI
46 {"serial", "hvterm-protocol"},
47 #endif
48 { "", "" }
49 };
50
51 typedef enum hv_protocol {
52 HV_PROTOCOL_RAW,
53 HV_PROTOCOL_HVSI
54 } hv_protocol_t;
55
56 struct hvterm_priv {
57 u32 termno; /* HV term number */
58 hv_protocol_t proto; /* Raw data or HVSI packets */
59 struct hvsi_priv hvsi; /* HVSI specific data */
60 spinlock_t buf_lock;
61 char buf[SIZE_VIO_GET_CHARS];
62 int left;
63 int offset;
64 };
65 static struct hvterm_priv *hvterm_privs[MAX_NR_HVC_CONSOLES];
66 /* For early boot console */
67 static struct hvterm_priv hvterm_priv0;
68
hvterm_raw_get_chars(uint32_t vtermno,char * buf,int count)69 static int hvterm_raw_get_chars(uint32_t vtermno, char *buf, int count)
70 {
71 struct hvterm_priv *pv = hvterm_privs[vtermno];
72 unsigned long i;
73 unsigned long flags;
74 int got;
75
76 if (WARN_ON(!pv))
77 return 0;
78
79 spin_lock_irqsave(&pv->buf_lock, flags);
80
81 if (pv->left == 0) {
82 pv->offset = 0;
83 pv->left = hvc_get_chars(pv->termno, pv->buf, count);
84
85 /*
86 * Work around a HV bug where it gives us a null
87 * after every \r. -- paulus
88 */
89 for (i = 1; i < pv->left; ++i) {
90 if (pv->buf[i] == 0 && pv->buf[i-1] == '\r') {
91 --pv->left;
92 if (i < pv->left) {
93 memmove(&pv->buf[i], &pv->buf[i+1],
94 pv->left - i);
95 }
96 }
97 }
98 }
99
100 got = min(count, pv->left);
101 memcpy(buf, &pv->buf[pv->offset], got);
102 pv->offset += got;
103 pv->left -= got;
104
105 spin_unlock_irqrestore(&pv->buf_lock, flags);
106
107 return got;
108 }
109
110 /**
111 * hvterm_raw_put_chars: send characters to firmware for given vterm adapter
112 * @vtermno: The virtual terminal number.
113 * @buf: The characters to send. Because of the underlying hypercall in
114 * hvc_put_chars(), this buffer must be at least 16 bytes long, even if
115 * you are sending fewer chars.
116 * @count: number of chars to send.
117 */
hvterm_raw_put_chars(uint32_t vtermno,const char * buf,int count)118 static int hvterm_raw_put_chars(uint32_t vtermno, const char *buf, int count)
119 {
120 struct hvterm_priv *pv = hvterm_privs[vtermno];
121
122 if (WARN_ON(!pv))
123 return 0;
124
125 return hvc_put_chars(pv->termno, buf, count);
126 }
127
128 static const struct hv_ops hvterm_raw_ops = {
129 .get_chars = hvterm_raw_get_chars,
130 .put_chars = hvterm_raw_put_chars,
131 .notifier_add = notifier_add_irq,
132 .notifier_del = notifier_del_irq,
133 .notifier_hangup = notifier_hangup_irq,
134 };
135
hvterm_hvsi_get_chars(uint32_t vtermno,char * buf,int count)136 static int hvterm_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
137 {
138 struct hvterm_priv *pv = hvterm_privs[vtermno];
139
140 if (WARN_ON(!pv))
141 return 0;
142
143 return hvsilib_get_chars(&pv->hvsi, buf, count);
144 }
145
hvterm_hvsi_put_chars(uint32_t vtermno,const char * buf,int count)146 static int hvterm_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
147 {
148 struct hvterm_priv *pv = hvterm_privs[vtermno];
149
150 if (WARN_ON(!pv))
151 return 0;
152
153 return hvsilib_put_chars(&pv->hvsi, buf, count);
154 }
155
hvterm_hvsi_open(struct hvc_struct * hp,int data)156 static int hvterm_hvsi_open(struct hvc_struct *hp, int data)
157 {
158 struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
159 int rc;
160
161 pr_devel("HVSI@%x: open !\n", pv->termno);
162
163 rc = notifier_add_irq(hp, data);
164 if (rc)
165 return rc;
166
167 return hvsilib_open(&pv->hvsi, hp);
168 }
169
hvterm_hvsi_close(struct hvc_struct * hp,int data)170 static void hvterm_hvsi_close(struct hvc_struct *hp, int data)
171 {
172 struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
173
174 pr_devel("HVSI@%x: do close !\n", pv->termno);
175
176 hvsilib_close(&pv->hvsi, hp);
177
178 notifier_del_irq(hp, data);
179 }
180
hvterm_hvsi_hangup(struct hvc_struct * hp,int data)181 static void hvterm_hvsi_hangup(struct hvc_struct *hp, int data)
182 {
183 struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
184
185 pr_devel("HVSI@%x: do hangup !\n", pv->termno);
186
187 hvsilib_close(&pv->hvsi, hp);
188
189 notifier_hangup_irq(hp, data);
190 }
191
hvterm_hvsi_tiocmget(struct hvc_struct * hp)192 static int hvterm_hvsi_tiocmget(struct hvc_struct *hp)
193 {
194 struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
195
196 if (!pv)
197 return -EINVAL;
198 return pv->hvsi.mctrl;
199 }
200
hvterm_hvsi_tiocmset(struct hvc_struct * hp,unsigned int set,unsigned int clear)201 static int hvterm_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
202 unsigned int clear)
203 {
204 struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
205
206 pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
207 pv->termno, set, clear);
208
209 if (set & TIOCM_DTR)
210 hvsilib_write_mctrl(&pv->hvsi, 1);
211 else if (clear & TIOCM_DTR)
212 hvsilib_write_mctrl(&pv->hvsi, 0);
213
214 return 0;
215 }
216
217 static const struct hv_ops hvterm_hvsi_ops = {
218 .get_chars = hvterm_hvsi_get_chars,
219 .put_chars = hvterm_hvsi_put_chars,
220 .notifier_add = hvterm_hvsi_open,
221 .notifier_del = hvterm_hvsi_close,
222 .notifier_hangup = hvterm_hvsi_hangup,
223 .tiocmget = hvterm_hvsi_tiocmget,
224 .tiocmset = hvterm_hvsi_tiocmset,
225 };
226
udbg_hvc_putc(char c)227 static void udbg_hvc_putc(char c)
228 {
229 int count = -1;
230 unsigned char bounce_buffer[16];
231
232 if (!hvterm_privs[0])
233 return;
234
235 if (c == '\n')
236 udbg_hvc_putc('\r');
237
238 do {
239 switch(hvterm_privs[0]->proto) {
240 case HV_PROTOCOL_RAW:
241 /*
242 * hvterm_raw_put_chars requires at least a 16-byte
243 * buffer, so go via the bounce buffer
244 */
245 bounce_buffer[0] = c;
246 count = hvterm_raw_put_chars(0, bounce_buffer, 1);
247 break;
248 case HV_PROTOCOL_HVSI:
249 count = hvterm_hvsi_put_chars(0, &c, 1);
250 break;
251 }
252 } while (count == 0 || count == -EAGAIN);
253 }
254
udbg_hvc_getc_poll(void)255 static int udbg_hvc_getc_poll(void)
256 {
257 int rc = 0;
258 char c;
259
260 if (!hvterm_privs[0])
261 return -1;
262
263 switch(hvterm_privs[0]->proto) {
264 case HV_PROTOCOL_RAW:
265 rc = hvterm_raw_get_chars(0, &c, 1);
266 break;
267 case HV_PROTOCOL_HVSI:
268 rc = hvterm_hvsi_get_chars(0, &c, 1);
269 break;
270 }
271 if (!rc)
272 return -1;
273 return c;
274 }
275
udbg_hvc_getc(void)276 static int udbg_hvc_getc(void)
277 {
278 int ch;
279
280 if (!hvterm_privs[0])
281 return -1;
282
283 for (;;) {
284 ch = udbg_hvc_getc_poll();
285 if (ch == -1) {
286 /* This shouldn't be needed...but... */
287 volatile unsigned long delay;
288 for (delay=0; delay < 2000000; delay++)
289 ;
290 } else {
291 return ch;
292 }
293 }
294 }
295
hvc_vio_probe(struct vio_dev * vdev,const struct vio_device_id * id)296 static int hvc_vio_probe(struct vio_dev *vdev,
297 const struct vio_device_id *id)
298 {
299 const struct hv_ops *ops;
300 struct hvc_struct *hp;
301 struct hvterm_priv *pv;
302 hv_protocol_t proto;
303 int i, termno = -1;
304
305 /* probed with invalid parameters. */
306 if (!vdev || !id)
307 return -EPERM;
308
309 if (of_device_is_compatible(vdev->dev.of_node, "hvterm1")) {
310 proto = HV_PROTOCOL_RAW;
311 ops = &hvterm_raw_ops;
312 } else if (of_device_is_compatible(vdev->dev.of_node, "hvterm-protocol")) {
313 proto = HV_PROTOCOL_HVSI;
314 ops = &hvterm_hvsi_ops;
315 } else {
316 pr_err("hvc_vio: Unknown protocol for %pOF\n", vdev->dev.of_node);
317 return -ENXIO;
318 }
319
320 pr_devel("hvc_vio_probe() device %pOF, using %s protocol\n",
321 vdev->dev.of_node,
322 proto == HV_PROTOCOL_RAW ? "raw" : "hvsi");
323
324 /* Is it our boot one ? */
325 if (hvterm_privs[0] == &hvterm_priv0 &&
326 vdev->unit_address == hvterm_priv0.termno) {
327 pv = hvterm_privs[0];
328 termno = 0;
329 pr_devel("->boot console, using termno 0\n");
330 }
331 /* nope, allocate a new one */
332 else {
333 for (i = 0; i < MAX_NR_HVC_CONSOLES && termno < 0; i++)
334 if (!hvterm_privs[i])
335 termno = i;
336 pr_devel("->non-boot console, using termno %d\n", termno);
337 if (termno < 0)
338 return -ENODEV;
339 pv = kzalloc(sizeof(struct hvterm_priv), GFP_KERNEL);
340 if (!pv)
341 return -ENOMEM;
342 pv->termno = vdev->unit_address;
343 pv->proto = proto;
344 spin_lock_init(&pv->buf_lock);
345 hvterm_privs[termno] = pv;
346 hvsilib_init(&pv->hvsi, hvc_get_chars, hvc_put_chars,
347 pv->termno, 0);
348 }
349
350 hp = hvc_alloc(termno, vdev->irq, ops, MAX_VIO_PUT_CHARS);
351 if (IS_ERR(hp))
352 return PTR_ERR(hp);
353 dev_set_drvdata(&vdev->dev, hp);
354
355 /* register udbg if it's not there already for console 0 */
356 if (hp->index == 0 && !udbg_putc) {
357 udbg_putc = udbg_hvc_putc;
358 udbg_getc = udbg_hvc_getc;
359 udbg_getc_poll = udbg_hvc_getc_poll;
360 }
361
362 return 0;
363 }
364
365 static struct vio_driver hvc_vio_driver = {
366 .id_table = hvc_driver_table,
367 .probe = hvc_vio_probe,
368 .name = hvc_driver_name,
369 .driver = {
370 .suppress_bind_attrs = true,
371 },
372 };
373
hvc_vio_init(void)374 static int __init hvc_vio_init(void)
375 {
376 int rc;
377
378 /* Register as a vio device to receive callbacks */
379 rc = vio_register_driver(&hvc_vio_driver);
380
381 return rc;
382 }
383 device_initcall(hvc_vio_init); /* after drivers/tty/hvc/hvc_console.c */
384
hvc_vio_init_early(void)385 void __init hvc_vio_init_early(void)
386 {
387 const __be32 *termno;
388 const struct hv_ops *ops;
389
390 /* find the boot console from /chosen/stdout */
391 /* Check if it's a virtual terminal */
392 if (!of_node_name_prefix(of_stdout, "vty"))
393 return;
394 termno = of_get_property(of_stdout, "reg", NULL);
395 if (termno == NULL)
396 return;
397 hvterm_priv0.termno = of_read_number(termno, 1);
398 spin_lock_init(&hvterm_priv0.buf_lock);
399 hvterm_privs[0] = &hvterm_priv0;
400
401 /* Check the protocol */
402 if (of_device_is_compatible(of_stdout, "hvterm1")) {
403 hvterm_priv0.proto = HV_PROTOCOL_RAW;
404 ops = &hvterm_raw_ops;
405 }
406 else if (of_device_is_compatible(of_stdout, "hvterm-protocol")) {
407 hvterm_priv0.proto = HV_PROTOCOL_HVSI;
408 ops = &hvterm_hvsi_ops;
409 hvsilib_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
410 hvterm_priv0.termno, 1);
411 /* HVSI, perform the handshake now */
412 hvsilib_establish(&hvterm_priv0.hvsi);
413 } else
414 return;
415 udbg_putc = udbg_hvc_putc;
416 udbg_getc = udbg_hvc_getc;
417 udbg_getc_poll = udbg_hvc_getc_poll;
418 #ifdef HVC_OLD_HVSI
419 /* When using the old HVSI driver don't register the HVC
420 * backend for HVSI, only do udbg
421 */
422 if (hvterm_priv0.proto == HV_PROTOCOL_HVSI)
423 return;
424 #endif
425 /* Check whether the user has requested a different console. */
426 if (!strstr(boot_command_line, "console="))
427 add_preferred_console("hvc", 0, NULL);
428 hvc_instantiate(0, 0, ops);
429 }
430
431 /* call this from early_init() for a working debug console on
432 * vterm capable LPAR machines
433 */
434 #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR
udbg_init_debug_lpar(void)435 void __init udbg_init_debug_lpar(void)
436 {
437 /*
438 * If we're running as a hypervisor then we definitely can't call the
439 * hypervisor to print debug output (we *are* the hypervisor), so don't
440 * register if we detect that MSR_HV=1.
441 */
442 if (mfmsr() & MSR_HV)
443 return;
444
445 hvterm_privs[0] = &hvterm_priv0;
446 hvterm_priv0.termno = 0;
447 hvterm_priv0.proto = HV_PROTOCOL_RAW;
448 spin_lock_init(&hvterm_priv0.buf_lock);
449 udbg_putc = udbg_hvc_putc;
450 udbg_getc = udbg_hvc_getc;
451 udbg_getc_poll = udbg_hvc_getc_poll;
452 }
453 #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR */
454
455 #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI
udbg_init_debug_lpar_hvsi(void)456 void __init udbg_init_debug_lpar_hvsi(void)
457 {
458 /* See comment above in udbg_init_debug_lpar() */
459 if (mfmsr() & MSR_HV)
460 return;
461
462 hvterm_privs[0] = &hvterm_priv0;
463 hvterm_priv0.termno = CONFIG_PPC_EARLY_DEBUG_HVSI_VTERMNO;
464 hvterm_priv0.proto = HV_PROTOCOL_HVSI;
465 spin_lock_init(&hvterm_priv0.buf_lock);
466 udbg_putc = udbg_hvc_putc;
467 udbg_getc = udbg_hvc_getc;
468 udbg_getc_poll = udbg_hvc_getc_poll;
469 hvsilib_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
470 hvterm_priv0.termno, 1);
471 hvsilib_establish(&hvterm_priv0.hvsi);
472 }
473 #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI */
474