2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
5 * Copyright (c) 2013 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <machine/vmm.h>
37 #include <machine/vmm_snapshot.h>
52 #include "pctestdev.h"
53 #include "uart_emul.h"
58 SET_DECLARE(lpc_dsdt_set, struct lpc_dsdt);
59 SET_DECLARE(lpc_sysres_set, struct lpc_sysres);
61 #define ELCR_PORT 0x4d0
62 SYSRES_IO(ELCR_PORT, 2);
64 #define IO_TIMER1_PORT 0x40
66 #define NMISC_PORT 0x61
67 SYSRES_IO(NMISC_PORT, 1);
69 static struct pci_devinst *lpc_bridge;
71 static const char *romfile;
73 #define LPC_UART_NUM 2
74 static struct lpc_uart_softc {
75 struct uart_softc *uart_softc;
80 } lpc_uart_softc[LPC_UART_NUM];
82 static const char *lpc_uart_names[LPC_UART_NUM] = { "COM1", "COM2" };
84 static bool pctestdev_present;
87 * LPC device configuration is in the following form:
88 * <lpc_device_name>[,<options>]
89 * For e.g. "com1,stdio" or "bootrom,/var/romfile"
92 lpc_device_parse(const char *opts)
95 char *str, *cpy, *lpcdev;
98 str = cpy = strdup(opts);
99 lpcdev = strsep(&str, ",");
100 if (lpcdev != NULL) {
101 if (strcasecmp(lpcdev, "bootrom") == 0) {
106 for (unit = 0; unit < LPC_UART_NUM; unit++) {
107 if (strcasecmp(lpcdev, lpc_uart_names[unit]) == 0) {
108 lpc_uart_softc[unit].opts = str;
113 if (strcasecmp(lpcdev, pctestdev_getname()) == 0) {
114 if (pctestdev_present) {
115 EPRINTLN("More than one %s device conf is "
116 "specified; only one is allowed.",
117 pctestdev_getname());
118 } else if (pctestdev_parse(str) == 0) {
119 pctestdev_present = true;
135 lpc_print_supported_devices()
140 for (i = 0; i < LPC_UART_NUM; i++)
141 printf("%s\n", lpc_uart_names[i]);
142 printf("%s\n", pctestdev_getname());
153 lpc_uart_intr_assert(void *arg)
155 struct lpc_uart_softc *sc = arg;
157 assert(sc->irq >= 0);
159 vm_isa_pulse_irq(lpc_bridge->pi_vmctx, sc->irq, sc->irq);
163 lpc_uart_intr_deassert(void *arg)
166 * The COM devices on the LPC bus generate edge triggered interrupts,
167 * so nothing more to do here.
172 lpc_uart_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
173 uint32_t *eax, void *arg)
176 struct lpc_uart_softc *sc = arg;
178 offset = port - sc->iobase;
183 *eax = uart_read(sc->uart_softc, offset);
185 uart_write(sc->uart_softc, offset, *eax);
189 *eax = uart_read(sc->uart_softc, offset);
190 *eax |= uart_read(sc->uart_softc, offset + 1) << 8;
192 uart_write(sc->uart_softc, offset, *eax);
193 uart_write(sc->uart_softc, offset + 1, *eax >> 8);
204 lpc_init(struct vmctx *ctx)
206 struct lpc_uart_softc *sc;
207 struct inout_port iop;
211 if (romfile != NULL) {
212 error = bootrom_loadrom(ctx, romfile);
218 for (unit = 0; unit < LPC_UART_NUM; unit++) {
219 sc = &lpc_uart_softc[unit];
220 name = lpc_uart_names[unit];
222 if (uart_legacy_alloc(unit, &sc->iobase, &sc->irq) != 0) {
223 EPRINTLN("Unable to allocate resources for "
224 "LPC device %s", name);
227 pci_irq_reserve(sc->irq);
229 sc->uart_softc = uart_init(lpc_uart_intr_assert,
230 lpc_uart_intr_deassert, sc);
232 if (uart_set_backend(sc->uart_softc, sc->opts) != 0) {
233 EPRINTLN("Unable to initialize backend '%s' "
234 "for LPC device %s", sc->opts, name);
238 bzero(&iop, sizeof(struct inout_port));
240 iop.port = sc->iobase;
241 iop.size = UART_IO_BAR_SIZE;
242 iop.flags = IOPORT_F_INOUT;
243 iop.handler = lpc_uart_io_handler;
246 error = register_inout(&iop);
252 if (pctestdev_present) {
253 error = pctestdev_init(ctx);
262 pci_lpc_write_dsdt(struct pci_devinst *pi)
264 struct lpc_dsdt **ldpp, *ldp;
267 dsdt_line("Device (ISA)");
269 dsdt_line(" Name (_ADR, 0x%04X%04X)", pi->pi_slot, pi->pi_func);
270 dsdt_line(" OperationRegion (LPCR, PCI_Config, 0x00, 0x100)");
271 dsdt_line(" Field (LPCR, AnyAcc, NoLock, Preserve)");
273 dsdt_line(" Offset (0x60),");
274 dsdt_line(" PIRA, 8,");
275 dsdt_line(" PIRB, 8,");
276 dsdt_line(" PIRC, 8,");
277 dsdt_line(" PIRD, 8,");
278 dsdt_line(" Offset (0x68),");
279 dsdt_line(" PIRE, 8,");
280 dsdt_line(" PIRF, 8,");
281 dsdt_line(" PIRG, 8,");
282 dsdt_line(" PIRH, 8");
287 SET_FOREACH(ldpp, lpc_dsdt_set) {
293 dsdt_line("Device (PIC)");
295 dsdt_line(" Name (_HID, EisaId (\"PNP0000\"))");
296 dsdt_line(" Name (_CRS, ResourceTemplate ()");
299 dsdt_fixed_ioport(IO_ICU1, 2);
300 dsdt_fixed_ioport(IO_ICU2, 2);
307 dsdt_line("Device (TIMR)");
309 dsdt_line(" Name (_HID, EisaId (\"PNP0100\"))");
310 dsdt_line(" Name (_CRS, ResourceTemplate ()");
313 dsdt_fixed_ioport(IO_TIMER1_PORT, 4);
324 pci_lpc_sysres_dsdt(void)
326 struct lpc_sysres **lspp, *lsp;
329 dsdt_line("Device (SIO)");
331 dsdt_line(" Name (_HID, EisaId (\"PNP0C02\"))");
332 dsdt_line(" Name (_CRS, ResourceTemplate ()");
336 SET_FOREACH(lspp, lpc_sysres_set) {
340 dsdt_fixed_ioport(lsp->base, lsp->length);
343 dsdt_fixed_mem32(lsp->base, lsp->length);
352 LPC_DSDT(pci_lpc_sysres_dsdt);
355 pci_lpc_uart_dsdt(void)
357 struct lpc_uart_softc *sc;
360 for (unit = 0; unit < LPC_UART_NUM; unit++) {
361 sc = &lpc_uart_softc[unit];
365 dsdt_line("Device (%s)", lpc_uart_names[unit]);
367 dsdt_line(" Name (_HID, EisaId (\"PNP0501\"))");
368 dsdt_line(" Name (_UID, %d)", unit + 1);
369 dsdt_line(" Name (_CRS, ResourceTemplate ()");
372 dsdt_fixed_ioport(sc->iobase, UART_IO_BAR_SIZE);
373 dsdt_fixed_irq(sc->irq);
379 LPC_DSDT(pci_lpc_uart_dsdt);
382 pci_lpc_cfgwrite(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
383 int coff, int bytes, uint32_t val)
389 if (coff >= 0x60 && coff <= 0x63)
390 pirq_pin = coff - 0x60 + 1;
391 if (coff >= 0x68 && coff <= 0x6b)
392 pirq_pin = coff - 0x68 + 5;
394 pirq_write(ctx, pirq_pin, val);
395 pci_set_cfgdata8(pi, coff, pirq_read(pirq_pin));
403 pci_lpc_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
404 int baridx, uint64_t offset, int size, uint64_t value)
409 pci_lpc_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
410 int baridx, uint64_t offset, int size)
415 #define LPC_DEV 0x7000
416 #define LPC_VENDOR 0x8086
419 pci_lpc_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
423 * Do not allow more than one LPC bridge to be configured.
425 if (lpc_bridge != NULL) {
426 EPRINTLN("Only one LPC bridge is allowed.");
431 * Enforce that the LPC can only be configured on bus 0. This
432 * simplifies the ACPI DSDT because it can provide a decode for
433 * all legacy i/o ports behind bus 0.
435 if (pi->pi_bus != 0) {
436 EPRINTLN("LPC bridge can be present only on bus 0.");
440 if (lpc_init(ctx) != 0)
443 /* initialize config space */
444 pci_set_cfgdata16(pi, PCIR_DEVICE, LPC_DEV);
445 pci_set_cfgdata16(pi, PCIR_VENDOR, LPC_VENDOR);
446 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_BRIDGE);
447 pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_BRIDGE_ISA);
455 lpc_pirq_name(int pin)
459 if (lpc_bridge == NULL)
461 asprintf(&name, "\\_SB.PC00.ISA.LNK%c,", 'A' + pin - 1);
466 lpc_pirq_routed(void)
470 if (lpc_bridge == NULL)
473 for (pin = 0; pin < 4; pin++)
474 pci_set_cfgdata8(lpc_bridge, 0x60 + pin, pirq_read(pin + 1));
475 for (pin = 0; pin < 4; pin++)
476 pci_set_cfgdata8(lpc_bridge, 0x68 + pin, pirq_read(pin + 5));
479 #ifdef BHYVE_SNAPSHOT
481 pci_lpc_snapshot(struct vm_snapshot_meta *meta)
484 struct uart_softc *sc;
486 for (unit = 0; unit < LPC_UART_NUM; unit++) {
487 sc = lpc_uart_softc[unit].uart_softc;
489 ret = uart_snapshot(sc, meta);
499 struct pci_devemu pci_de_lpc = {
501 .pe_init = pci_lpc_init,
502 .pe_write_dsdt = pci_lpc_write_dsdt,
503 .pe_cfgwrite = pci_lpc_cfgwrite,
504 .pe_barwrite = pci_lpc_write,
505 .pe_barread = pci_lpc_read,
506 #ifdef BHYVE_SNAPSHOT
507 .pe_snapshot = pci_lpc_snapshot,
510 PCI_EMUL_SET(pci_de_lpc);