]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/powernv/opal_console.c
Update to bmake-20201101
[FreeBSD/FreeBSD.git] / sys / powerpc / powernv / opal_console.c
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
57 struct 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
80 static struct uart_opal_softc   *console_sc = NULL;
81 static struct consdev *stdout_cp;
82
83 enum {
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
97 static int uart_opal_probe(device_t dev);
98 static int uart_opal_attach(device_t dev);
99 static void uart_opal_intr(void *v);
100
101 static 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
109 static driver_t uart_opal_driver = {
110         "uart",
111         uart_opal_methods,
112         sizeof(struct uart_opal_softc),
113 };
114
115 DRIVER_MODULE(uart_opal, opalcons, uart_opal_driver, uart_devclass, 0, 0);
116
117 static int uart_opal_getc(struct uart_opal_softc *sc);
118 static cn_probe_t uart_opal_cnprobe;
119 static cn_init_t uart_opal_cninit;
120 static cn_term_t uart_opal_cnterm;
121 static cn_getc_t uart_opal_cngetc;
122 static cn_putc_t uart_opal_cnputc;
123 static cn_grab_t uart_opal_cngrab;
124 static cn_ungrab_t uart_opal_cnungrab;
125
126 CONSOLE_DRIVER(uart_opal);
127
128 static void uart_opal_ttyoutwakeup(struct tty *tp);
129
130 static struct ttydevsw uart_opal_tty_class = {
131         .tsw_flags      = TF_INITLOCK|TF_CALLOUT,
132         .tsw_outwakeup  = uart_opal_ttyoutwakeup,
133 };
134
135 static struct {
136         char tmpbuf[16];
137         uint64_t size;
138         struct mtx mtx;
139 } opalcons_buffer;
140
141 static void
142 uart_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
162 static void
163 uart_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
174 static int
175 uart_opal_probe_node(struct uart_opal_softc *sc)
176 {
177         phandle_t node = sc->node;
178         uint32_t reg;
179         char buf[64];
180
181         sc->inbuflen = 0;
182         sc->outseqno = 0;
183
184         if (OF_getprop(node, "device_type", buf, sizeof(buf)) <= 0)
185                 return (ENXIO);
186         if (strcmp(buf, "serial") != 0)
187                 return (ENXIO);
188
189         reg = -1;
190         OF_getencprop(node, "reg", &reg, sizeof(reg));
191         if (reg == -1)
192                 return (ENXIO);
193         sc->vtermid = reg;
194         sc->node = node;
195
196         if (OF_getprop(node, "compatible", buf, sizeof(buf)) <= 0)
197                 return (ENXIO);
198         if (strcmp(buf, "ibm,opal-console-raw") == 0) {
199                 sc->protocol = OPAL_RAW;
200                 return (0);
201         } else if (strcmp(buf, "ibm,opal-console-hvsi") == 0) {
202                 sc->protocol = OPAL_HVSI;
203                 return (0);
204         }
205
206         return (ENXIO);
207 }
208
209 static int
210 uart_opal_probe(device_t dev)
211 {
212         struct uart_opal_softc sc;
213         int err;
214
215         sc.node = ofw_bus_get_node(dev);
216         err = uart_opal_probe_node(&sc);
217         if (err != 0)
218                 return (err);
219
220         device_set_desc(dev, "OPAL Serial Port");
221
222         return (err);
223 }
224
225 static void
226 uart_opal_cnprobe(struct consdev *cp)
227 {
228         char buf[64];
229         phandle_t input, chosen;
230         static struct uart_opal_softc sc;
231
232         if (opal_check() != 0)
233                 goto fail;
234
235         if ((chosen = OF_finddevice("/chosen")) == -1)
236                 goto fail;
237
238         /* Check if OF has an active stdin/stdout */
239         if (OF_getprop(chosen, "linux,stdout-path", buf, sizeof(buf)) <= 0)
240                 goto fail;
241
242         input = OF_finddevice(buf);
243         if (input == -1)
244                 goto fail;
245
246         sc.node = input;
247         if (uart_opal_probe_node(&sc) != 0)
248                 goto fail;
249         mtx_init(&sc.sc_mtx, "uart_opal", NULL, MTX_SPIN | MTX_QUIET |
250             MTX_NOWITNESS);
251
252         cp->cn_pri = CN_NORMAL;
253         console_sc = &sc;
254         cp->cn_arg = console_sc;
255         stdout_cp = cp;
256         return;
257
258 fail:
259         cp->cn_pri = CN_DEAD;
260         return;
261 }
262
263 static int
264 uart_opal_attach(device_t dev)
265 {
266         struct uart_opal_softc *sc;
267         int unit;
268
269         sc = device_get_softc(dev);
270         sc->node = ofw_bus_get_node(dev);
271         uart_opal_probe_node(sc);
272
273         unit = device_get_unit(dev);
274         mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL,
275             MTX_SPIN | MTX_QUIET | MTX_NOWITNESS);
276
277         if (console_sc != NULL && console_sc->vtermid == sc->vtermid) {
278                 device_printf(dev, "console\n");
279                 device_set_softc(dev, console_sc);
280                 sc = console_sc;
281                 sprintf(uart_opal_consdev.cn_name, "ttyu%r", unit);
282         }
283         sc->tp = tty_alloc(&uart_opal_tty_class, sc);
284
285         if (console_sc == sc)
286                 tty_init_console(sc->tp, 0);
287
288         sc->dev = dev;
289         sc->irqrid = 0;
290         sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqrid,
291             RF_ACTIVE | RF_SHAREABLE);
292         if (sc->irqres != NULL) {
293                 bus_setup_intr(dev, sc->irqres, INTR_TYPE_TTY | INTR_MPSAFE,
294                     NULL, uart_opal_intr, sc, &sc->sc_icookie);
295         } else {
296                 callout_init(&sc->callout, CALLOUT_MPSAFE);
297                 sc->polltime = hz / 20;
298                 if (sc->polltime < 1)
299                         sc->polltime = 1;
300                 callout_reset(&sc->callout, sc->polltime, uart_opal_intr, sc);
301         }
302
303         tty_makedev(sc->tp, NULL, "u%r", unit);
304
305         return (0);
306 }
307
308 static void
309 uart_opal_cninit(struct consdev *cp)
310 {
311
312         strcpy(cp->cn_name, "opalcons");
313 }
314
315 static void
316 uart_opal_cnterm(struct consdev *cp)
317 {
318 }
319
320 static int
321 uart_opal_get(struct uart_opal_softc *sc, void *buffer, size_t bufsize)
322 {
323         int err;
324         int hdr = 0;
325
326         if (sc->protocol == OPAL_RAW) {
327                 uint64_t len = htobe64(bufsize);
328                 uint64_t olen = (uint64_t)&len;
329                 uint64_t obuf = (uint64_t)buffer;
330
331                 if (pmap_bootstrapped) {
332                         olen = vtophys(&len);
333                         obuf = vtophys(buffer);
334                 }
335
336                 err = opal_call(OPAL_CONSOLE_READ, sc->vtermid, olen, obuf);
337                 if (err != OPAL_SUCCESS)
338                         return (-1);
339
340                 bufsize = be64toh(len);
341         } else {
342                 uart_lock(&sc->sc_mtx);
343                 if (sc->inbuflen == 0) {
344                         err = opal_call(OPAL_CONSOLE_READ, sc->vtermid,
345                             &sc->inbuflen, sc->opal_inbuf);
346                         if (err != OPAL_SUCCESS) {
347                                 uart_unlock(&sc->sc_mtx);
348                                 return (-1);
349                         }
350                         hdr = 1; 
351                         sc->inbuflen = be64toh(sc->inbuflen);
352                 }
353
354                 if (sc->inbuflen == 0) {
355                         uart_unlock(&sc->sc_mtx);
356                         return (0);
357                 }
358
359                 if (bufsize > sc->inbuflen)
360                         bufsize = sc->inbuflen;
361
362                 if (hdr == 1) {
363                         sc->inbuflen = sc->inbuflen - 4;
364                         /* The HVSI protocol has a 4 byte header, skip it */
365                         memmove(&sc->opal_inbuf[0], &sc->opal_inbuf[4],
366                             sc->inbuflen);
367                 }
368
369                 memcpy(buffer, sc->opal_inbuf, bufsize);
370                 sc->inbuflen -= bufsize;
371                 if (sc->inbuflen > 0)
372                         memmove(&sc->opal_inbuf[0], &sc->opal_inbuf[bufsize],
373                             sc->inbuflen);
374
375                 uart_unlock(&sc->sc_mtx);
376         }
377
378         return (bufsize);
379 }
380
381 static int
382 uart_opal_put(struct uart_opal_softc *sc, void *buffer, size_t bufsize)
383 {
384         uint16_t seqno;
385         uint64_t len;
386         char    cbuf[16];
387         int     err;
388         uint64_t olen = (uint64_t)&len;
389         uint64_t obuf = (uint64_t)cbuf;
390
391         if (sc->protocol == OPAL_RAW) {
392                 obuf = (uint64_t)buffer;
393                 len = bufsize;
394
395                 uart_opal_real_map_outbuffer(&obuf, &olen);
396                 *(uint64_t*)olen = htobe64(*(uint64_t*)olen);
397                 err = opal_call(OPAL_CONSOLE_WRITE, sc->vtermid, olen, obuf);
398                 *(uint64_t*)olen = be64toh(*(uint64_t*)olen);
399                 uart_opal_real_unmap_outbuffer(&len);
400         } else {
401                 uart_lock(&sc->sc_mtx);
402                 if (bufsize > 12)
403                         bufsize = 12;
404                 seqno = sc->outseqno++;
405                 cbuf[0] = VS_DATA_PACKET_HEADER;
406                 cbuf[1] = 4 + bufsize; /* total length */
407                 cbuf[2] = (seqno >> 8) & 0xff;
408                 cbuf[3] = seqno & 0xff;
409                 memcpy(&cbuf[4], buffer, bufsize);
410                 len = 4 + bufsize;
411
412                 uart_opal_real_map_outbuffer(&obuf, &olen);
413                 *(uint64_t*)olen = htobe64(*(uint64_t*)olen);
414                 err = opal_call(OPAL_CONSOLE_WRITE, sc->vtermid, olen, obuf);
415                 *(uint64_t*)olen = be64toh(*(uint64_t*)olen);
416                 uart_opal_real_unmap_outbuffer(&len);
417
418                 uart_unlock(&sc->sc_mtx);
419
420                 len -= 4;
421         }
422
423 #if 0
424         if (err != OPAL_SUCCESS)
425                 len = 0;
426 #endif
427
428         return (len);
429 }
430
431 static int
432 uart_opal_cngetc(struct consdev *cp)
433 {
434         return (uart_opal_getc(cp->cn_arg));
435 }
436
437 static int
438 uart_opal_getc(struct uart_opal_softc *sc)
439 {
440         unsigned char c;
441         int retval;
442
443         retval = uart_opal_get(sc, &c, 1);
444         if (retval != 1)
445                 return (-1);
446 #if defined(KDB)
447         kdb_alt_break(c, &sc->alt_break_state);
448 #endif
449
450         return (c);
451 }
452
453 static void
454 uart_opal_cnputc(struct consdev *cp, int c)
455 {
456         unsigned char ch = c;
457         int a;
458
459         if (1) {
460                 /* Clear FIFO if needed. Must be repeated few times. */
461                 for (a = 0; a < 20; a++) {
462                         opal_call(OPAL_POLL_EVENTS, NULL);
463                 }
464         }
465         uart_opal_put(cp->cn_arg, &ch, 1);
466 }
467
468 static void
469 uart_opal_cngrab(struct consdev *cp)
470 {
471 }
472
473 static void
474 uart_opal_cnungrab(struct consdev *cp)
475 {
476 }
477
478 static void
479 uart_opal_ttyoutwakeup(struct tty *tp)
480 {
481         struct uart_opal_softc *sc;
482         char buffer[8];
483         int len;
484
485         sc = tty_softc(tp);
486
487         while ((len = ttydisc_getc(tp, buffer, sizeof(buffer))) != 0)
488                 uart_opal_put(sc, buffer, len);
489 }
490
491 static void
492 uart_opal_intr(void *v)
493 {
494         struct uart_opal_softc *sc = v;
495         struct tty *tp = sc->tp;
496         int c;
497
498         tty_lock(tp);
499         while ((c = uart_opal_getc(sc)) > 0)
500                 ttydisc_rint(tp, c, 0);
501         ttydisc_rint_done(tp);
502         tty_unlock(tp);
503
504         opal_call(OPAL_POLL_EVENTS, NULL);
505
506         if (sc->irqres == NULL)
507                 callout_reset(&sc->callout, sc->polltime, uart_opal_intr, sc);
508 }
509
510 static int
511 opalcons_probe(device_t dev)
512 {
513         const char *name;
514
515         name = ofw_bus_get_name(dev);
516         if (name == NULL || strcmp(name, "consoles") != 0)
517                 return (ENXIO);
518
519         device_set_desc(dev, "OPAL Consoles");
520         return (BUS_PROBE_SPECIFIC);
521 }
522
523 static int 
524 opalcons_attach(device_t dev)
525 {
526         phandle_t child;
527         device_t cdev;
528         struct ofw_bus_devinfo *dinfo;
529
530         for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
531             child = OF_peer(child)) {
532                 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
533                 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
534                         free(dinfo, M_DEVBUF);
535                         continue;
536                 }
537                 cdev = device_add_child(dev, NULL, -1);
538                 if (cdev == NULL) {
539                         device_printf(dev, "<%s>: device_add_child failed\n",
540                             dinfo->obd_name);
541                         ofw_bus_gen_destroy_devinfo(dinfo);
542                         free(dinfo, M_DEVBUF);
543                         continue;
544                 }
545                 device_set_ivars(cdev, dinfo);
546         }
547
548         return (bus_generic_attach(dev));
549 }
550
551 static const struct ofw_bus_devinfo *
552 opalcons_get_devinfo(device_t dev, device_t child)
553 {
554         return (device_get_ivars(child));
555 }
556
557 static device_method_t opalcons_methods[] = {
558         /* Device interface */
559         DEVMETHOD(device_probe,         opalcons_probe),
560         DEVMETHOD(device_attach,        opalcons_attach),
561
562         /* ofw_bus interface */
563         DEVMETHOD(ofw_bus_get_devinfo,  opalcons_get_devinfo),
564         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
565         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
566         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
567         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
568         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
569
570         DEVMETHOD_END
571 };
572
573 static driver_t opalcons_driver = {
574         "opalcons",
575         opalcons_methods,
576         0
577 };
578
579 static devclass_t opalcons_devclass;
580
581 DRIVER_MODULE(opalcons, opal, opalcons_driver, opalcons_devclass, 0, 0);