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