xref: /freebsd/sys/powerpc/powernv/opal_console.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*-
2  * Copyright (C) 2011,2015 by Nathan Whitehorn. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
19  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
21  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
27 
28 #include <sys/param.h>
29 #include <sys/kdb.h>
30 #include <sys/kernel.h>
31 #include <sys/priv.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/types.h>
35 #include <sys/conf.h>
36 #include <sys/cons.h>
37 #include <sys/proc.h>
38 #include <sys/tty.h>
39 
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 
43 #include <machine/bus.h>
44 
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 #include <dev/uart/uart.h>
49 #include <dev/uart/uart_cpu.h>
50 #include <dev/uart/uart_bus.h>
51 
52 #include "opal.h"
53 #include "uart_if.h"
54 
55 struct uart_opal_softc {
56 	device_t dev;
57 	phandle_t node;
58 	int vtermid;
59 
60 	struct tty *tp;
61 	struct resource *irqres;
62 	int irqrid;
63 	struct callout callout;
64 	void *sc_icookie;
65 	int polltime;
66 
67 	struct mtx sc_mtx;
68 	int protocol;
69 
70 	char opal_inbuf[16];
71 	uint64_t inbuflen;
72 	uint8_t outseqno;
73 #if defined(KDB)
74 	int alt_break_state;
75 #endif
76 };
77 
78 static struct uart_opal_softc	*console_sc = NULL;
79 static struct consdev *stdout_cp;
80 
81 enum {
82 	OPAL_RAW, OPAL_HVSI
83 };
84 
85 #define VS_DATA_PACKET_HEADER		0xff
86 #define VS_CONTROL_PACKET_HEADER	0xfe
87 #define  VSV_SET_MODEM_CTL		0x01
88 #define  VSV_MODEM_CTL_UPDATE		0x02
89 #define  VSV_RENEGOTIATE_CONNECTION	0x03
90 #define VS_QUERY_PACKET_HEADER		0xfd
91 #define  VSV_SEND_VERSION_NUMBER	0x01
92 #define  VSV_SEND_MODEM_CTL_STATUS	0x02
93 #define VS_QUERY_RESPONSE_PACKET_HEADER	0xfc
94 
95 static int uart_opal_probe(device_t dev);
96 static int uart_opal_attach(device_t dev);
97 static void uart_opal_intr(void *v);
98 
99 static device_method_t uart_opal_methods[] = {
100 	/* Device interface */
101 	DEVMETHOD(device_probe,		uart_opal_probe),
102 	DEVMETHOD(device_attach,	uart_opal_attach),
103 
104 	DEVMETHOD_END
105 };
106 
107 static driver_t uart_opal_driver = {
108 	"uart",
109 	uart_opal_methods,
110 	sizeof(struct uart_opal_softc),
111 };
112 
113 DRIVER_MODULE(uart_opal, opalcons, uart_opal_driver, uart_devclass, 0, 0);
114 
115 static int uart_opal_getc(struct uart_opal_softc *sc);
116 static cn_probe_t uart_opal_cnprobe;
117 static cn_init_t uart_opal_cninit;
118 static cn_term_t uart_opal_cnterm;
119 static cn_getc_t uart_opal_cngetc;
120 static cn_putc_t uart_opal_cnputc;
121 static cn_grab_t uart_opal_cngrab;
122 static cn_ungrab_t uart_opal_cnungrab;
123 
124 CONSOLE_DRIVER(uart_opal);
125 
126 static void uart_opal_ttyoutwakeup(struct tty *tp);
127 
128 static struct ttydevsw uart_opal_tty_class = {
129 	.tsw_flags	= TF_INITLOCK|TF_CALLOUT,
130 	.tsw_outwakeup	= uart_opal_ttyoutwakeup,
131 };
132 
133 static struct {
134 	char tmpbuf[16];
135 	uint64_t size;
136 	struct mtx mtx;
137 } opalcons_buffer;
138 
139 static void
140 uart_opal_real_map_outbuffer(uint64_t *bufferp, uint64_t *lenp)
141 {
142 
143 	if (!mtx_initialized(&opalcons_buffer.mtx))
144 		mtx_init(&opalcons_buffer.mtx, "uart_opal", NULL,
145 		    MTX_SPIN | MTX_QUIET | MTX_NOWITNESS);
146 
147 	if (!pmap_bootstrapped)
148 		return;
149 
150 	mtx_lock_spin(&opalcons_buffer.mtx);
151 
152 	opalcons_buffer.size = *(uint64_t *)(*lenp) =
153 	    min(sizeof(opalcons_buffer.tmpbuf), *(uint64_t *)(*lenp));
154 	memcpy(opalcons_buffer.tmpbuf, (void *)(*bufferp),
155 	    *(uint64_t *)(*lenp));
156 	*bufferp = (uint64_t)opalcons_buffer.tmpbuf;
157 	*lenp = (uint64_t)&opalcons_buffer.size;
158 }
159 
160 static void
161 uart_opal_real_unmap_outbuffer(uint64_t *len)
162 {
163 
164 	if (!pmap_bootstrapped)
165 		return;
166 
167 	mtx_assert(&opalcons_buffer.mtx, MA_OWNED);
168 	*len = opalcons_buffer.size;
169 	mtx_unlock_spin(&opalcons_buffer.mtx);
170 }
171 
172 static int
173 uart_opal_probe_node(struct uart_opal_softc *sc)
174 {
175 	phandle_t node = sc->node;
176 	uint32_t reg;
177 	char buf[64];
178 
179 	sc->inbuflen = 0;
180 	sc->outseqno = 0;
181 
182 	if (OF_getprop(node, "device_type", buf, sizeof(buf)) <= 0)
183 		return (ENXIO);
184 	if (strcmp(buf, "serial") != 0)
185 		return (ENXIO);
186 
187 	reg = -1;
188 	OF_getencprop(node, "reg", &reg, sizeof(reg));
189 	if (reg == -1)
190 		return (ENXIO);
191 	sc->vtermid = reg;
192 	sc->node = node;
193 
194 	if (OF_getprop(node, "compatible", buf, sizeof(buf)) <= 0)
195 		return (ENXIO);
196 	if (strcmp(buf, "ibm,opal-console-raw") == 0) {
197 		sc->protocol = OPAL_RAW;
198 		return (0);
199 	} else if (strcmp(buf, "ibm,opal-console-hvsi") == 0) {
200 		sc->protocol = OPAL_HVSI;
201 		return (0);
202 	}
203 
204 	return (ENXIO);
205 }
206 
207 static int
208 uart_opal_probe(device_t dev)
209 {
210 	struct uart_opal_softc sc;
211 	int err;
212 
213 	sc.node = ofw_bus_get_node(dev);
214 	err = uart_opal_probe_node(&sc);
215 	if (err != 0)
216 		return (err);
217 
218 	device_set_desc(dev, "OPAL Serial Port");
219 
220 	return (err);
221 }
222 
223 static void
224 uart_opal_cnprobe(struct consdev *cp)
225 {
226 	char buf[64];
227 	phandle_t input, chosen;
228 	static struct uart_opal_softc sc;
229 
230 	if (opal_check() != 0)
231 		goto fail;
232 
233 	if ((chosen = OF_finddevice("/chosen")) == -1)
234 		goto fail;
235 
236 	/* Check if OF has an active stdin/stdout */
237 	if (OF_getprop(chosen, "linux,stdout-path", buf, sizeof(buf)) <= 0)
238 		goto fail;
239 
240 	input = OF_finddevice(buf);
241 	if (input == -1)
242 		goto fail;
243 
244 	sc.node = input;
245 	if (uart_opal_probe_node(&sc) != 0)
246 		goto fail;
247 	mtx_init(&sc.sc_mtx, "uart_opal", NULL, MTX_SPIN | MTX_QUIET |
248 	    MTX_NOWITNESS);
249 
250 	cp->cn_pri = CN_NORMAL;
251 	console_sc = &sc;
252 	cp->cn_arg = console_sc;
253 	stdout_cp = cp;
254 	return;
255 
256 fail:
257 	cp->cn_pri = CN_DEAD;
258 	return;
259 }
260 
261 static int
262 uart_opal_attach(device_t dev)
263 {
264 	struct uart_opal_softc *sc;
265 	int unit;
266 
267 	sc = device_get_softc(dev);
268 	sc->node = ofw_bus_get_node(dev);
269 	uart_opal_probe_node(sc);
270 
271 	unit = device_get_unit(dev);
272 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL,
273 	    MTX_SPIN | MTX_QUIET | MTX_NOWITNESS);
274 
275 	if (console_sc != NULL && console_sc->vtermid == sc->vtermid) {
276 		device_printf(dev, "console\n");
277 		device_set_softc(dev, console_sc);
278 		sc = console_sc;
279 		sprintf(uart_opal_consdev.cn_name, "ttyu%r", unit);
280 	}
281 	sc->tp = tty_alloc(&uart_opal_tty_class, sc);
282 
283 	if (console_sc == sc)
284 		tty_init_console(sc->tp, 0);
285 
286 	sc->dev = dev;
287 	sc->irqrid = 0;
288 	sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqrid,
289 	    RF_ACTIVE | RF_SHAREABLE);
290 	if (sc->irqres != NULL) {
291 		bus_setup_intr(dev, sc->irqres, INTR_TYPE_TTY | INTR_MPSAFE,
292 		    NULL, uart_opal_intr, sc, &sc->sc_icookie);
293 	} else {
294 		callout_init(&sc->callout, CALLOUT_MPSAFE);
295 		sc->polltime = hz / 20;
296 		if (sc->polltime < 1)
297 			sc->polltime = 1;
298 		callout_reset(&sc->callout, sc->polltime, uart_opal_intr, sc);
299 	}
300 
301 	tty_makedev(sc->tp, NULL, "u%r", unit);
302 
303 	return (0);
304 }
305 
306 static void
307 uart_opal_cninit(struct consdev *cp)
308 {
309 
310 	strcpy(cp->cn_name, "opalcons");
311 }
312 
313 static void
314 uart_opal_cnterm(struct consdev *cp)
315 {
316 }
317 
318 static int
319 uart_opal_get(struct uart_opal_softc *sc, void *buffer, size_t bufsize)
320 {
321 	int err;
322 	int hdr = 0;
323 
324 	if (sc->protocol == OPAL_RAW) {
325 		uint64_t len = bufsize;
326 		uint64_t olen = (uint64_t)&len;
327 		uint64_t obuf = (uint64_t)buffer;
328 
329 		if (pmap_bootstrapped) {
330 			olen = vtophys(&len);
331 			obuf = vtophys(buffer);
332 		}
333 
334 		err = opal_call(OPAL_CONSOLE_READ, sc->vtermid, olen, obuf);
335 		if (err != OPAL_SUCCESS)
336 			return (-1);
337 
338 		bufsize = len;
339 	} else {
340 		uart_lock(&sc->sc_mtx);
341 		if (sc->inbuflen == 0) {
342 			err = opal_call(OPAL_CONSOLE_READ, sc->vtermid,
343 			    &sc->inbuflen, sc->opal_inbuf);
344 			if (err != OPAL_SUCCESS) {
345 				uart_unlock(&sc->sc_mtx);
346 				return (-1);
347 			}
348 			hdr = 1;
349 		}
350 
351 		if (sc->inbuflen == 0) {
352 			uart_unlock(&sc->sc_mtx);
353 			return (0);
354 		}
355 
356 		if (bufsize > sc->inbuflen)
357 			bufsize = sc->inbuflen;
358 
359 		if (hdr == 1) {
360 			sc->inbuflen = sc->inbuflen - 4;
361 			/* The HVSI protocol has a 4 byte header, skip it */
362 			memmove(&sc->opal_inbuf[0], &sc->opal_inbuf[4],
363 			    sc->inbuflen);
364 		}
365 
366 		memcpy(buffer, sc->opal_inbuf, bufsize);
367 		sc->inbuflen -= bufsize;
368 		if (sc->inbuflen > 0)
369 			memmove(&sc->opal_inbuf[0], &sc->opal_inbuf[bufsize],
370 			    sc->inbuflen);
371 
372 		uart_unlock(&sc->sc_mtx);
373 	}
374 
375 	return (bufsize);
376 }
377 
378 static int
379 uart_opal_put(struct uart_opal_softc *sc, void *buffer, size_t bufsize)
380 {
381 	uint16_t seqno;
382 	uint64_t len;
383 	char	cbuf[16];
384 	int	err;
385 	uint64_t olen = (uint64_t)&len;
386 	uint64_t obuf = (uint64_t)cbuf;
387 
388 	if (sc->protocol == OPAL_RAW) {
389 		obuf = (uint64_t)buffer;
390 		len = bufsize;
391 
392 		uart_opal_real_map_outbuffer(&obuf, &olen);
393 		err = opal_call(OPAL_CONSOLE_WRITE, sc->vtermid, olen, obuf);
394 		uart_opal_real_unmap_outbuffer(&len);
395 	} else {
396 		uart_lock(&sc->sc_mtx);
397 		if (bufsize > 12)
398 			bufsize = 12;
399 		seqno = sc->outseqno++;
400 		cbuf[0] = VS_DATA_PACKET_HEADER;
401 		cbuf[1] = 4 + bufsize; /* total length */
402 		cbuf[2] = (seqno >> 8) & 0xff;
403 		cbuf[3] = seqno & 0xff;
404 		memcpy(&cbuf[4], buffer, bufsize);
405 		len = 4 + bufsize;
406 
407 		uart_opal_real_map_outbuffer(&obuf, &olen);
408 		err = opal_call(OPAL_CONSOLE_WRITE, sc->vtermid, olen, obuf);
409 		uart_opal_real_unmap_outbuffer(&len);
410 
411 		uart_unlock(&sc->sc_mtx);
412 
413 		len -= 4;
414 	}
415 
416 #if 0
417 	if (err != OPAL_SUCCESS)
418 		len = 0;
419 #endif
420 
421 	return (len);
422 }
423 
424 static int
425 uart_opal_cngetc(struct consdev *cp)
426 {
427 	return (uart_opal_getc(cp->cn_arg));
428 }
429 
430 static int
431 uart_opal_getc(struct uart_opal_softc *sc)
432 {
433 	unsigned char c;
434 	int retval;
435 
436 	retval = uart_opal_get(sc, &c, 1);
437 	if (retval != 1)
438 		return (-1);
439 #if defined(KDB)
440 	kdb_alt_break(c, &sc->alt_break_state);
441 #endif
442 
443 	return (c);
444 }
445 
446 static void
447 uart_opal_cnputc(struct consdev *cp, int c)
448 {
449 	unsigned char ch = c;
450 	int a;
451 
452 	if (1) {
453 		/* Clear FIFO if needed. Must be repeated few times. */
454 		for (a = 0; a < 20; a++) {
455 			opal_call(OPAL_POLL_EVENTS, NULL);
456 		}
457 	}
458 	uart_opal_put(cp->cn_arg, &ch, 1);
459 }
460 
461 static void
462 uart_opal_cngrab(struct consdev *cp)
463 {
464 }
465 
466 static void
467 uart_opal_cnungrab(struct consdev *cp)
468 {
469 }
470 
471 static void
472 uart_opal_ttyoutwakeup(struct tty *tp)
473 {
474 	struct uart_opal_softc *sc;
475 	char buffer[8];
476 	int len;
477 
478 	sc = tty_softc(tp);
479 
480 	while ((len = ttydisc_getc(tp, buffer, sizeof(buffer))) != 0)
481 		uart_opal_put(sc, buffer, len);
482 }
483 
484 static void
485 uart_opal_intr(void *v)
486 {
487 	struct uart_opal_softc *sc = v;
488 	struct tty *tp = sc->tp;
489 	int c;
490 
491 	tty_lock(tp);
492 	while ((c = uart_opal_getc(sc)) > 0)
493 		ttydisc_rint(tp, c, 0);
494 	ttydisc_rint_done(tp);
495 	tty_unlock(tp);
496 
497 	opal_call(OPAL_POLL_EVENTS, NULL);
498 
499 	if (sc->irqres == NULL)
500 		callout_reset(&sc->callout, sc->polltime, uart_opal_intr, sc);
501 }
502 
503 static int
504 opalcons_probe(device_t dev)
505 {
506 	const char *name;
507 
508 	name = ofw_bus_get_name(dev);
509 	if (name == NULL || strcmp(name, "consoles") != 0)
510 		return (ENXIO);
511 
512 	device_set_desc(dev, "OPAL Consoles");
513 	return (BUS_PROBE_SPECIFIC);
514 }
515 
516 static int
517 opalcons_attach(device_t dev)
518 {
519 	phandle_t child;
520 	device_t cdev;
521 	struct ofw_bus_devinfo *dinfo;
522 
523 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
524 	    child = OF_peer(child)) {
525 		dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
526 		if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
527 			free(dinfo, M_DEVBUF);
528 			continue;
529 		}
530 		cdev = device_add_child(dev, NULL, -1);
531 		if (cdev == NULL) {
532 			device_printf(dev, "<%s>: device_add_child failed\n",
533 			    dinfo->obd_name);
534 			ofw_bus_gen_destroy_devinfo(dinfo);
535 			free(dinfo, M_DEVBUF);
536 			continue;
537 		}
538 		device_set_ivars(cdev, dinfo);
539 	}
540 
541 	return (bus_generic_attach(dev));
542 }
543 
544 static const struct ofw_bus_devinfo *
545 opalcons_get_devinfo(device_t dev, device_t child)
546 {
547         return (device_get_ivars(child));
548 }
549 
550 static device_method_t opalcons_methods[] = {
551 	/* Device interface */
552 	DEVMETHOD(device_probe,		opalcons_probe),
553 	DEVMETHOD(device_attach,	opalcons_attach),
554 
555 	/* ofw_bus interface */
556 	DEVMETHOD(ofw_bus_get_devinfo,	opalcons_get_devinfo),
557 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
558 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
559 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
560 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
561 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
562 
563 	DEVMETHOD_END
564 };
565 
566 static driver_t opalcons_driver = {
567         "opalcons",
568         opalcons_methods,
569         0
570 };
571 
572 static devclass_t opalcons_devclass;
573 
574 DRIVER_MODULE(opalcons, opal, opalcons_driver, opalcons_devclass, 0, 0);
575 
576