]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/powernv/opal_console.c
PowerNV: add buffer for OPAL console
[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/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 };
74
75 static struct uart_opal_softc   *console_sc = NULL;
76
77 #if defined(KDB)
78 static int                      alt_break_state;
79 #endif
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 cn_probe_t uart_opal_cnprobe;
116 static cn_init_t uart_opal_cninit;
117 static cn_term_t uart_opal_cnterm;
118 static cn_getc_t uart_opal_cngetc;
119 static cn_putc_t uart_opal_cnputc;
120 static cn_grab_t uart_opal_cngrab;
121 static cn_ungrab_t uart_opal_cnungrab;
122
123 CONSOLE_DRIVER(uart_opal);
124
125 static void uart_opal_ttyoutwakeup(struct tty *tp);
126
127 static struct ttydevsw uart_opal_tty_class = {
128         .tsw_flags      = TF_INITLOCK|TF_CALLOUT,
129         .tsw_outwakeup  = uart_opal_ttyoutwakeup,
130 };
131
132 static struct {
133         char tmpbuf[16];
134         uint64_t size;
135         struct mtx mtx;
136 } escapehatch;
137
138 static void
139 uart_opal_real_map_outbuffer(uint64_t *bufferp, uint64_t *lenp)
140 {
141
142         if (!mtx_initialized(&escapehatch.mtx))
143                 mtx_init(&escapehatch.mtx, "uart_opal", NULL, MTX_SPIN | MTX_QUIET |
144                     MTX_NOWITNESS);
145
146         if (!pmap_bootstrapped)
147                 return;
148
149         if (TD_IS_IDLETHREAD(curthread)) {
150                 escapehatch.size = *(uint64_t *)(*lenp) =
151                     min(sizeof(escapehatch.tmpbuf), *(uint64_t *)(*lenp));
152                 mtx_lock_spin(&escapehatch.mtx);
153                 memcpy(escapehatch.tmpbuf, (void *)(*bufferp), *(uint64_t *)(*lenp));
154                 *bufferp = (uint64_t)escapehatch.tmpbuf;
155                 *lenp = (uint64_t)&escapehatch.size;
156         }
157
158         *bufferp = vtophys(*bufferp);
159         *lenp = vtophys(*lenp);
160 }
161         
162 static void
163 uart_opal_real_unmap_outbuffer(uint64_t lenp, uint64_t *origlen)
164 {
165
166         if (!pmap_bootstrapped || !TD_IS_IDLETHREAD(curthread))
167                 return;
168
169         mtx_assert(&escapehatch.mtx, MA_OWNED);
170         *origlen = escapehatch.size;
171         mtx_unlock_spin(&escapehatch.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_getprop(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         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->dev = dev;
269         sc->node = ofw_bus_get_node(dev);
270         uart_opal_probe_node(sc);
271
272         unit = device_get_unit(dev);
273         sc->tp = tty_alloc(&uart_opal_tty_class, sc);
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                 sc->outseqno = console_sc->outseqno;
280                 console_sc = sc;
281                 sprintf(uart_opal_consdev.cn_name, "ttyu%r", unit);
282                 tty_init_console(sc->tp, 0);
283         }
284
285         sc->irqrid = 0;
286         sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqrid,
287             RF_ACTIVE | RF_SHAREABLE);
288         if (sc->irqres != NULL) {
289                 bus_setup_intr(dev, sc->irqres, INTR_TYPE_TTY | INTR_MPSAFE,
290                     NULL, uart_opal_intr, sc, &sc->sc_icookie);
291         } else {
292                 callout_init(&sc->callout, CALLOUT_MPSAFE);
293                 sc->polltime = hz / 20;
294                 if (sc->polltime < 1)
295                         sc->polltime = 1;
296                 callout_reset(&sc->callout, sc->polltime, uart_opal_intr, sc);
297         }
298
299         tty_makedev(sc->tp, NULL, "u%r", unit);
300
301         return (0);
302 }
303
304 static void
305 uart_opal_cninit(struct consdev *cp)
306 {
307
308         strcpy(cp->cn_name, "opalcons");
309 }
310
311 static void
312 uart_opal_cnterm(struct consdev *cp)
313 {
314 }
315
316 static int
317 uart_opal_get(struct uart_opal_softc *sc, void *buffer, size_t bufsize)
318 {
319         int err;
320         int hdr = 0;
321
322         if (sc->protocol == OPAL_RAW) {
323                 uint64_t len = bufsize;
324                 uint64_t olen = (uint64_t)&len;
325                 uint64_t obuf = (uint64_t)buffer;
326
327                 if (pmap_bootstrapped) {
328                         olen = vtophys(&len);
329                         obuf = vtophys(buffer);
330                 }
331
332                 err = opal_call(OPAL_CONSOLE_READ, sc->vtermid, olen, obuf);
333                 if (err != OPAL_SUCCESS)
334                         return (-1);
335
336                 bufsize = len;
337         } else {
338                 uart_lock(&sc->sc_mtx);
339                 if (sc->inbuflen == 0) {
340                         err = opal_call(OPAL_CONSOLE_READ, sc->vtermid,
341                             &sc->inbuflen, sc->opal_inbuf);
342                         if (err != OPAL_SUCCESS) {
343                                 uart_unlock(&sc->sc_mtx);
344                                 return (-1);
345                         }
346                         hdr = 1; 
347                 }
348
349                 if (sc->inbuflen == 0) {
350                         uart_unlock(&sc->sc_mtx);
351                         return (0);
352                 }
353
354                 if (bufsize > sc->inbuflen)
355                         bufsize = sc->inbuflen;
356
357                 if (hdr == 1) {
358                         sc->inbuflen = sc->inbuflen - 4;
359                         /* The HVSI protocol has a 4 byte header, skip it */
360                         memmove(&sc->opal_inbuf[0], &sc->opal_inbuf[4],
361                             sc->inbuflen);
362                 }
363
364                 memcpy(buffer, sc->opal_inbuf, bufsize);
365                 sc->inbuflen -= bufsize;
366                 if (sc->inbuflen > 0)
367                         memmove(&sc->opal_inbuf[0], &sc->opal_inbuf[bufsize],
368                             sc->inbuflen);
369
370                 uart_unlock(&sc->sc_mtx);
371         }
372
373         return (bufsize);
374 }
375
376 static int
377 uart_opal_put(struct uart_opal_softc *sc, void *buffer, size_t bufsize)
378 {
379         uint16_t seqno;
380         uint64_t len;
381         char    cbuf[16];
382         int     err;
383         uint64_t olen = (uint64_t)&len;
384         uint64_t obuf = (uint64_t)cbuf;
385
386         if (sc->protocol == OPAL_RAW) {
387                 obuf = (uint64_t)buffer;
388                 len = bufsize;
389
390                 uart_opal_real_map_outbuffer(&obuf, &olen);
391                 err = opal_call(OPAL_CONSOLE_WRITE, sc->vtermid, olen, obuf);
392                 uart_opal_real_unmap_outbuffer(olen, &len);
393         } else {
394                 uart_lock(&sc->sc_mtx);
395                 if (bufsize > 12)
396                         bufsize = 12;
397                 seqno = sc->outseqno++;
398                 cbuf[0] = VS_DATA_PACKET_HEADER;
399                 cbuf[1] = 4 + bufsize; /* total length */
400                 cbuf[2] = (seqno >> 8) & 0xff;
401                 cbuf[3] = seqno & 0xff;
402                 memcpy(&cbuf[4], buffer, bufsize);
403                 len = 4 + bufsize;
404
405                 uart_opal_real_map_outbuffer(&obuf, &olen);
406                 err = opal_call(OPAL_CONSOLE_WRITE, sc->vtermid, olen, obuf);
407                 uart_opal_real_unmap_outbuffer(olen, &len);
408
409                 uart_unlock(&sc->sc_mtx);
410
411                 len -= 4;
412         }
413
414 #if 0
415         if (err != OPAL_SUCCESS)
416                 len = 0;
417 #endif
418
419         return (len);
420 }
421
422 static int
423 uart_opal_cngetc(struct consdev *cp)
424 {
425         unsigned char c;
426         int retval;
427
428         retval = uart_opal_get(console_sc, &c, 1);
429         if (retval != 1)
430                 return (-1);
431 #if defined(KDB)
432         kdb_alt_break(c, &alt_break_state);
433 #endif
434
435         return (c);
436 }
437
438 static void
439 uart_opal_cnputc(struct consdev *cp, int c)
440 {
441         unsigned char ch = c;
442         uart_opal_put(console_sc, &ch, 1);
443 }
444
445 static void
446 uart_opal_cngrab(struct consdev *cp)
447 {
448 }
449
450 static void
451 uart_opal_cnungrab(struct consdev *cp)
452 {
453 }
454
455 static void
456 uart_opal_ttyoutwakeup(struct tty *tp)
457 {
458         struct uart_opal_softc *sc;
459         char buffer[8];
460         int len;
461
462         sc = tty_softc(tp);
463         
464         while ((len = ttydisc_getc(tp, buffer, sizeof(buffer))) != 0)
465                 uart_opal_put(sc, buffer, len);
466 }
467
468 static void
469 uart_opal_intr(void *v)
470 {
471         struct uart_opal_softc *sc = v;
472         struct tty *tp = sc->tp;
473         unsigned char c;
474         int len;
475
476         tty_lock(tp);
477         while ((len = uart_opal_get(sc, &c, 1)) > 0)
478                 ttydisc_rint(tp, c, 0);
479         ttydisc_rint_done(tp);
480         tty_unlock(tp);
481
482         if (sc->irqres == NULL)
483                 callout_reset(&sc->callout, sc->polltime, uart_opal_intr, sc);
484 }
485
486 static int
487 opalcons_probe(device_t dev)
488 {
489         const char *name;
490
491         name = ofw_bus_get_name(dev);
492         if (name == NULL || strcmp(name, "consoles") != 0)
493                 return (ENXIO);
494
495         device_set_desc(dev, "OPAL Consoles");
496         return (BUS_PROBE_SPECIFIC);
497 }
498
499 static int 
500 opalcons_attach(device_t dev)
501 {
502         phandle_t child;
503         device_t cdev;
504         struct ofw_bus_devinfo *dinfo;
505
506         for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
507             child = OF_peer(child)) {
508                 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
509                 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
510                         free(dinfo, M_DEVBUF);
511                         continue;
512                 }
513                 cdev = device_add_child(dev, NULL, -1);
514                 if (cdev == NULL) {
515                         device_printf(dev, "<%s>: device_add_child failed\n",
516                             dinfo->obd_name);
517                         ofw_bus_gen_destroy_devinfo(dinfo);
518                         free(dinfo, M_DEVBUF);
519                         continue;
520                 }
521                 device_set_ivars(cdev, dinfo);
522         }
523
524         return (bus_generic_attach(dev));
525 }
526
527 static const struct ofw_bus_devinfo *
528 opalcons_get_devinfo(device_t dev, device_t child)
529 {
530         return (device_get_ivars(child));
531 }
532
533 static device_method_t opalcons_methods[] = {
534         /* Device interface */
535         DEVMETHOD(device_probe,         opalcons_probe),
536         DEVMETHOD(device_attach,        opalcons_attach),
537
538         /* ofw_bus interface */
539         DEVMETHOD(ofw_bus_get_devinfo,  opalcons_get_devinfo),
540         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
541         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
542         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
543         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
544         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
545
546         DEVMETHOD_END
547 };
548
549 static driver_t opalcons_driver = {
550         "opalcons",
551         opalcons_methods,
552         0
553 };
554
555 static devclass_t opalcons_devclass;
556
557 DRIVER_MODULE(opalcons, opal, opalcons_driver, opalcons_devclass, 0, 0);
558