]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/pci_lpc.c
Merge commit '7087c8de43b0d5d27c52da6ba2ba4957b7e336ff' into new_merge
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / pci_lpc.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
5  * Copyright (c) 2013 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/types.h>
36 #include <machine/vmm.h>
37 #include <machine/vmm_snapshot.h>
38
39 #include <err.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include <vmmapi.h>
45
46 #include "acpi.h"
47 #include "debug.h"
48 #include "bootrom.h"
49 #include "config.h"
50 #include "inout.h"
51 #include "pci_emul.h"
52 #include "pci_irq.h"
53 #include "pci_lpc.h"
54 #include "pctestdev.h"
55 #include "uart_emul.h"
56
57 #define IO_ICU1         0x20
58 #define IO_ICU2         0xA0
59
60 SET_DECLARE(lpc_dsdt_set, struct lpc_dsdt);
61 SET_DECLARE(lpc_sysres_set, struct lpc_sysres);
62
63 #define ELCR_PORT       0x4d0
64 SYSRES_IO(ELCR_PORT, 2);
65
66 #define IO_TIMER1_PORT  0x40
67
68 #define NMISC_PORT      0x61
69 SYSRES_IO(NMISC_PORT, 1);
70
71 static struct pci_devinst *lpc_bridge;
72
73 #define LPC_UART_NUM    4
74 static struct lpc_uart_softc {
75         struct uart_softc *uart_softc;
76         int     iobase;
77         int     irq;
78         int     enabled;
79 } lpc_uart_softc[LPC_UART_NUM];
80
81 static const char *lpc_uart_names[LPC_UART_NUM] = {
82         "com1", "com2", "com3", "com4"
83 };
84
85 static const char *lpc_uart_acpi_names[LPC_UART_NUM] = {
86         "COM1", "COM2", "COM3", "COM4"
87 };
88
89 /*
90  * LPC device configuration is in the following form:
91  * <lpc_device_name>[,<options>]
92  * For e.g. "com1,stdio" or "bootrom,/var/romfile"
93  */
94 int
95 lpc_device_parse(const char *opts)
96 {
97         int unit, error;
98         char *str, *cpy, *lpcdev, *node_name;
99         const char *romfile, *varfile;
100
101         error = -1;
102         str = cpy = strdup(opts);
103         lpcdev = strsep(&str, ",");
104         if (lpcdev != NULL) {
105                 if (strcasecmp(lpcdev, "bootrom") == 0) {
106                         romfile = strsep(&str, ",");
107                         if (romfile == NULL) {
108                                 errx(4, "invalid bootrom option \"%s\"", opts);
109                         }
110                         set_config_value("lpc.bootrom", romfile);
111
112                         varfile = strsep(&str, ",");
113                         if (varfile != NULL) {
114                                 set_config_value("lpc.bootvars", varfile);
115                         }
116
117                         error = 0;
118                         goto done;
119                 }
120                 for (unit = 0; unit < LPC_UART_NUM; unit++) {
121                         if (strcasecmp(lpcdev, lpc_uart_names[unit]) == 0) {
122                                 asprintf(&node_name, "lpc.%s.path",
123                                     lpc_uart_names[unit]);
124                                 set_config_value(node_name, str);
125                                 free(node_name);
126                                 error = 0;
127                                 goto done;
128                         }
129                 }
130                 if (strcasecmp(lpcdev, pctestdev_getname()) == 0) {
131                         asprintf(&node_name, "lpc.%s", pctestdev_getname());
132                         set_config_bool(node_name, true);
133                         free(node_name);
134                         error = 0;
135                         goto done;
136                 }
137         }
138
139 done:
140         free(cpy);
141
142         return (error);
143 }
144
145 void
146 lpc_print_supported_devices(void)
147 {
148         size_t i;
149
150         printf("bootrom\n");
151         for (i = 0; i < LPC_UART_NUM; i++)
152                 printf("%s\n", lpc_uart_names[i]);
153         printf("%s\n", pctestdev_getname());
154 }
155
156 const char *
157 lpc_bootrom(void)
158 {
159
160         return (get_config_value("lpc.bootrom"));
161 }
162
163 static void
164 lpc_uart_intr_assert(void *arg)
165 {
166         struct lpc_uart_softc *sc = arg;
167
168         assert(sc->irq >= 0);
169
170         vm_isa_pulse_irq(lpc_bridge->pi_vmctx, sc->irq, sc->irq);
171 }
172
173 static void
174 lpc_uart_intr_deassert(void *arg __unused)
175 {
176         /*
177          * The COM devices on the LPC bus generate edge triggered interrupts,
178          * so nothing more to do here.
179          */
180 }
181
182 static int
183 lpc_uart_io_handler(struct vmctx *ctx __unused, int in,
184     int port, int bytes, uint32_t *eax, void *arg)
185 {
186         int offset;
187         struct lpc_uart_softc *sc = arg;
188
189         offset = port - sc->iobase;
190
191         switch (bytes) {
192         case 1:
193                 if (in)
194                         *eax = uart_read(sc->uart_softc, offset);
195                 else
196                         uart_write(sc->uart_softc, offset, *eax);
197                 break;
198         case 2:
199                 if (in) {
200                         *eax = uart_read(sc->uart_softc, offset);
201                         *eax |= uart_read(sc->uart_softc, offset + 1) << 8;
202                 } else {
203                         uart_write(sc->uart_softc, offset, *eax);
204                         uart_write(sc->uart_softc, offset + 1, *eax >> 8);
205                 }
206                 break;
207         default:
208                 return (-1);
209         }
210
211         return (0);
212 }
213
214 static int
215 lpc_init(struct vmctx *ctx)
216 {
217         struct lpc_uart_softc *sc;
218         struct inout_port iop;
219         const char *backend, *name;
220         char *node_name;
221         int unit, error;
222         const nvlist_t *nvl;
223
224         nvl = find_config_node("lpc");
225         if (nvl != NULL && nvlist_exists(nvl, "bootrom")) {
226                 error = bootrom_loadrom(ctx, nvl);
227                 if (error)
228                         return (error);
229         }
230
231         /* COM1 and COM2 */
232         for (unit = 0; unit < LPC_UART_NUM; unit++) {
233                 sc = &lpc_uart_softc[unit];
234                 name = lpc_uart_names[unit];
235
236                 if (uart_legacy_alloc(unit, &sc->iobase, &sc->irq) != 0) {
237                         EPRINTLN("Unable to allocate resources for "
238                             "LPC device %s", name);
239                         return (-1);
240                 }
241                 pci_irq_reserve(sc->irq);
242
243                 sc->uart_softc = uart_init(lpc_uart_intr_assert,
244                                     lpc_uart_intr_deassert, sc);
245
246                 asprintf(&node_name, "lpc.%s.path", name);
247                 backend = get_config_value(node_name);
248                 free(node_name);
249                 if (uart_set_backend(sc->uart_softc, backend) != 0) {
250                         EPRINTLN("Unable to initialize backend '%s' "
251                             "for LPC device %s", backend, name);
252                         return (-1);
253                 }
254
255                 bzero(&iop, sizeof(struct inout_port));
256                 iop.name = name;
257                 iop.port = sc->iobase;
258                 iop.size = UART_IO_BAR_SIZE;
259                 iop.flags = IOPORT_F_INOUT;
260                 iop.handler = lpc_uart_io_handler;
261                 iop.arg = sc;
262
263                 error = register_inout(&iop);
264                 assert(error == 0);
265                 sc->enabled = 1;
266         }
267
268         /* pc-testdev */
269         asprintf(&node_name, "lpc.%s", pctestdev_getname());
270         if (get_config_bool_default(node_name, false)) {
271                 error = pctestdev_init(ctx);
272                 if (error)
273                         return (error);
274         }
275         free(node_name);
276
277         return (0);
278 }
279
280 static void
281 pci_lpc_write_dsdt(struct pci_devinst *pi)
282 {
283         struct lpc_dsdt **ldpp, *ldp;
284
285         dsdt_line("");
286         dsdt_line("Device (ISA)");
287         dsdt_line("{");
288         dsdt_line("  Name (_ADR, 0x%04X%04X)", pi->pi_slot, pi->pi_func);
289         dsdt_line("  OperationRegion (LPCR, PCI_Config, 0x00, 0x100)");
290         dsdt_line("  Field (LPCR, AnyAcc, NoLock, Preserve)");
291         dsdt_line("  {");
292         dsdt_line("    Offset (0x60),");
293         dsdt_line("    PIRA,   8,");
294         dsdt_line("    PIRB,   8,");
295         dsdt_line("    PIRC,   8,");
296         dsdt_line("    PIRD,   8,");
297         dsdt_line("    Offset (0x68),");
298         dsdt_line("    PIRE,   8,");
299         dsdt_line("    PIRF,   8,");
300         dsdt_line("    PIRG,   8,");
301         dsdt_line("    PIRH,   8");
302         dsdt_line("  }");
303         dsdt_line("");
304
305         dsdt_indent(1);
306         SET_FOREACH(ldpp, lpc_dsdt_set) {
307                 ldp = *ldpp;
308                 ldp->handler();
309         }
310
311         dsdt_line("");
312         dsdt_line("Device (PIC)");
313         dsdt_line("{");
314         dsdt_line("  Name (_HID, EisaId (\"PNP0000\"))");
315         dsdt_line("  Name (_CRS, ResourceTemplate ()");
316         dsdt_line("  {");
317         dsdt_indent(2);
318         dsdt_fixed_ioport(IO_ICU1, 2);
319         dsdt_fixed_ioport(IO_ICU2, 2);
320         dsdt_fixed_irq(2);
321         dsdt_unindent(2);
322         dsdt_line("  })");
323         dsdt_line("}");
324
325         dsdt_line("");
326         dsdt_line("Device (TIMR)");
327         dsdt_line("{");
328         dsdt_line("  Name (_HID, EisaId (\"PNP0100\"))");
329         dsdt_line("  Name (_CRS, ResourceTemplate ()");
330         dsdt_line("  {");
331         dsdt_indent(2);
332         dsdt_fixed_ioport(IO_TIMER1_PORT, 4);
333         dsdt_fixed_irq(0);
334         dsdt_unindent(2);
335         dsdt_line("  })");
336         dsdt_line("}");
337         dsdt_unindent(1);
338
339         dsdt_line("}");
340 }
341
342 static void
343 pci_lpc_sysres_dsdt(void)
344 {
345         struct lpc_sysres **lspp, *lsp;
346
347         dsdt_line("");
348         dsdt_line("Device (SIO)");
349         dsdt_line("{");
350         dsdt_line("  Name (_HID, EisaId (\"PNP0C02\"))");
351         dsdt_line("  Name (_CRS, ResourceTemplate ()");
352         dsdt_line("  {");
353
354         dsdt_indent(2);
355         SET_FOREACH(lspp, lpc_sysres_set) {
356                 lsp = *lspp;
357                 switch (lsp->type) {
358                 case LPC_SYSRES_IO:
359                         dsdt_fixed_ioport(lsp->base, lsp->length);
360                         break;
361                 case LPC_SYSRES_MEM:
362                         dsdt_fixed_mem32(lsp->base, lsp->length);
363                         break;
364                 }
365         }
366         dsdt_unindent(2);
367
368         dsdt_line("  })");
369         dsdt_line("}");
370 }
371 LPC_DSDT(pci_lpc_sysres_dsdt);
372
373 static void
374 pci_lpc_uart_dsdt(void)
375 {
376         struct lpc_uart_softc *sc;
377         int unit;
378
379         for (unit = 0; unit < LPC_UART_NUM; unit++) {
380                 sc = &lpc_uart_softc[unit];
381                 if (!sc->enabled)
382                         continue;
383                 dsdt_line("");
384                 dsdt_line("Device (%s)", lpc_uart_acpi_names[unit]);
385                 dsdt_line("{");
386                 dsdt_line("  Name (_HID, EisaId (\"PNP0501\"))");
387                 dsdt_line("  Name (_UID, %d)", unit + 1);
388                 dsdt_line("  Name (_CRS, ResourceTemplate ()");
389                 dsdt_line("  {");
390                 dsdt_indent(2);
391                 dsdt_fixed_ioport(sc->iobase, UART_IO_BAR_SIZE);
392                 dsdt_fixed_irq(sc->irq);
393                 dsdt_unindent(2);
394                 dsdt_line("  })");
395                 dsdt_line("}");
396         }
397 }
398 LPC_DSDT(pci_lpc_uart_dsdt);
399
400 static int
401 pci_lpc_cfgwrite(struct pci_devinst *pi, int coff, int bytes, uint32_t val)
402 {
403         int pirq_pin;
404
405         if (bytes == 1) {
406                 pirq_pin = 0;
407                 if (coff >= 0x60 && coff <= 0x63)
408                         pirq_pin = coff - 0x60 + 1;
409                 if (coff >= 0x68 && coff <= 0x6b)
410                         pirq_pin = coff - 0x68 + 5;
411                 if (pirq_pin != 0) {
412                         pirq_write(pi->pi_vmctx, pirq_pin, val);
413                         pci_set_cfgdata8(pi, coff, pirq_read(pirq_pin));
414                         return (0);
415                 }
416         }
417         return (-1);
418 }
419
420 static void
421 pci_lpc_write(struct pci_devinst *pi __unused, int baridx __unused,
422     uint64_t offset __unused, int size __unused, uint64_t value __unused)
423 {
424 }
425
426 static uint64_t
427 pci_lpc_read(struct pci_devinst *pi __unused, int baridx __unused,
428     uint64_t offset __unused, int size __unused)
429 {
430         return (0);
431 }
432
433 #define LPC_DEV         0x7000
434 #define LPC_VENDOR      0x8086
435
436 static int
437 pci_lpc_init(struct pci_devinst *pi, nvlist_t *nvl __unused)
438 {
439         /*
440          * Do not allow more than one LPC bridge to be configured.
441          */
442         if (lpc_bridge != NULL) {
443                 EPRINTLN("Only one LPC bridge is allowed.");
444                 return (-1);
445         }
446
447         /*
448          * Enforce that the LPC can only be configured on bus 0. This
449          * simplifies the ACPI DSDT because it can provide a decode for
450          * all legacy i/o ports behind bus 0.
451          */
452         if (pi->pi_bus != 0) {
453                 EPRINTLN("LPC bridge can be present only on bus 0.");
454                 return (-1);
455         }
456
457         if (lpc_init(pi->pi_vmctx) != 0)
458                 return (-1);
459
460         /* initialize config space */
461         pci_set_cfgdata16(pi, PCIR_DEVICE, LPC_DEV);
462         pci_set_cfgdata16(pi, PCIR_VENDOR, LPC_VENDOR);
463         pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_BRIDGE);
464         pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_BRIDGE_ISA);
465
466         lpc_bridge = pi;
467
468         return (0);
469 }
470
471 char *
472 lpc_pirq_name(int pin)
473 {
474         char *name;
475
476         if (lpc_bridge == NULL)
477                 return (NULL);
478         asprintf(&name, "\\_SB.PC00.ISA.LNK%c,", 'A' + pin - 1);
479         return (name);
480 }
481
482 void
483 lpc_pirq_routed(void)
484 {
485         int pin;
486
487         if (lpc_bridge == NULL)
488                 return;
489
490         for (pin = 0; pin < 4; pin++)
491                 pci_set_cfgdata8(lpc_bridge, 0x60 + pin, pirq_read(pin + 1));
492         for (pin = 0; pin < 4; pin++)
493                 pci_set_cfgdata8(lpc_bridge, 0x68 + pin, pirq_read(pin + 5));
494 }
495
496 #ifdef BHYVE_SNAPSHOT
497 static int
498 pci_lpc_snapshot(struct vm_snapshot_meta *meta)
499 {
500         int unit, ret;
501         struct uart_softc *sc;
502
503         for (unit = 0; unit < LPC_UART_NUM; unit++) {
504                 sc = lpc_uart_softc[unit].uart_softc;
505
506                 ret = uart_snapshot(sc, meta);
507                 if (ret != 0)
508                         goto done;
509         }
510
511 done:
512         return (ret);
513 }
514 #endif
515
516 static const struct pci_devemu pci_de_lpc = {
517         .pe_emu =       "lpc",
518         .pe_init =      pci_lpc_init,
519         .pe_write_dsdt = pci_lpc_write_dsdt,
520         .pe_cfgwrite =  pci_lpc_cfgwrite,
521         .pe_barwrite =  pci_lpc_write,
522         .pe_barread =   pci_lpc_read,
523 #ifdef BHYVE_SNAPSHOT
524         .pe_snapshot =  pci_lpc_snapshot,
525 #endif
526 };
527 PCI_EMUL_SET(pci_de_lpc);