2 * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
3 * Copyright (c) 2013 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <machine/vmm.h>
35 #include <machine/vmm_dev.h>
47 #include "uart_emul.h"
49 SET_DECLARE(lpc_dsdt_set, struct lpc_dsdt);
50 SET_DECLARE(lpc_sysres_set, struct lpc_sysres);
52 static struct pci_devinst *lpc_bridge;
54 #define LPC_UART_NUM 2
55 static struct lpc_uart_softc {
56 struct uart_softc *uart_softc;
61 } lpc_uart_softc[LPC_UART_NUM];
63 static const char *lpc_uart_names[LPC_UART_NUM] = { "COM1", "COM2" };
66 * LPC device configuration is in the following form:
67 * <lpc_device_name>[,<options>]
68 * For e.g. "com1,stdio"
71 lpc_device_parse(const char *opts)
74 char *str, *cpy, *lpcdev;
77 str = cpy = strdup(opts);
78 lpcdev = strsep(&str, ",");
80 for (unit = 0; unit < LPC_UART_NUM; unit++) {
81 if (strcasecmp(lpcdev, lpc_uart_names[unit]) == 0) {
82 lpc_uart_softc[unit].opts = str;
97 lpc_uart_intr_assert(void *arg)
99 struct lpc_uart_softc *sc = arg;
101 assert(sc->irq >= 0);
103 vm_ioapic_pulse_irq(lpc_bridge->pi_vmctx, sc->irq);
107 lpc_uart_intr_deassert(void *arg)
110 * The COM devices on the LPC bus generate edge triggered interrupts,
111 * so nothing more to do here.
116 lpc_uart_io_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
117 uint32_t *eax, void *arg)
120 struct lpc_uart_softc *sc = arg;
125 offset = port - sc->iobase;
128 *eax = uart_read(sc->uart_softc, offset);
130 uart_write(sc->uart_softc, offset, *eax);
138 struct lpc_uart_softc *sc;
139 struct inout_port iop;
144 for (unit = 0; unit < LPC_UART_NUM; unit++) {
145 sc = &lpc_uart_softc[unit];
146 name = lpc_uart_names[unit];
148 if (uart_legacy_alloc(unit, &sc->iobase, &sc->irq) != 0) {
149 fprintf(stderr, "Unable to allocate resources for "
150 "LPC device %s\n", name);
154 sc->uart_softc = uart_init(lpc_uart_intr_assert,
155 lpc_uart_intr_deassert, sc);
157 if (uart_set_backend(sc->uart_softc, sc->opts) != 0) {
158 fprintf(stderr, "Unable to initialize backend '%s' "
159 "for LPC device %s\n", sc->opts, name);
163 bzero(&iop, sizeof(struct inout_port));
165 iop.port = sc->iobase;
166 iop.size = UART_IO_BAR_SIZE;
167 iop.flags = IOPORT_F_INOUT;
168 iop.handler = lpc_uart_io_handler;
171 error = register_inout(&iop);
180 pci_lpc_write_dsdt(struct pci_devinst *pi)
182 struct lpc_dsdt **ldpp, *ldp;
185 dsdt_line("Device (ISA)");
187 dsdt_line(" Name (_ADR, 0x%04X%04X)", pi->pi_slot, pi->pi_func);
188 dsdt_line(" OperationRegion (P40C, PCI_Config, 0x60, 0x04)");
191 SET_FOREACH(ldpp, lpc_dsdt_set) {
201 pci_lpc_sysres_dsdt(void)
203 struct lpc_sysres **lspp, *lsp;
206 dsdt_line("Device (SIO)");
208 dsdt_line(" Name (_HID, EisaId (\"PNP0C02\"))");
209 dsdt_line(" Name (_CRS, ResourceTemplate ()");
213 SET_FOREACH(lspp, lpc_sysres_set) {
217 dsdt_fixed_ioport(lsp->base, lsp->length);
220 dsdt_fixed_mem32(lsp->base, lsp->length);
229 LPC_DSDT(pci_lpc_sysres_dsdt);
232 pci_lpc_uart_dsdt(void)
234 struct lpc_uart_softc *sc;
237 for (unit = 0; unit < LPC_UART_NUM; unit++) {
238 sc = &lpc_uart_softc[unit];
242 dsdt_line("Device (%s)", lpc_uart_names[unit]);
244 dsdt_line(" Name (_HID, EisaId (\"PNP0501\"))");
245 dsdt_line(" Name (_UID, %d)", unit + 1);
246 dsdt_line(" Name (_CRS, ResourceTemplate ()");
249 dsdt_fixed_ioport(sc->iobase, UART_IO_BAR_SIZE);
250 dsdt_fixed_irq(sc->irq);
256 LPC_DSDT(pci_lpc_uart_dsdt);
259 pci_lpc_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
260 int baridx, uint64_t offset, int size, uint64_t value)
265 pci_lpc_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
266 int baridx, uint64_t offset, int size)
271 #define LPC_DEV 0x7000
272 #define LPC_VENDOR 0x8086
275 pci_lpc_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
278 * Do not allow more than one LPC bridge to be configured.
280 if (lpc_bridge != NULL) {
281 fprintf(stderr, "Only one LPC bridge is allowed.\n");
286 * Enforce that the LPC can only be configured on bus 0. This
287 * simplifies the ACPI DSDT because it can provide a decode for
288 * all legacy i/o ports behind bus 0.
290 if (pi->pi_bus != 0) {
291 fprintf(stderr, "LPC bridge can be present only on bus 0.\n");
298 /* initialize config space */
299 pci_set_cfgdata16(pi, PCIR_DEVICE, LPC_DEV);
300 pci_set_cfgdata16(pi, PCIR_VENDOR, LPC_VENDOR);
301 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_BRIDGE);
302 pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_BRIDGE_ISA);
309 struct pci_devemu pci_de_lpc = {
311 .pe_init = pci_lpc_init,
312 .pe_write_dsdt = pci_lpc_write_dsdt,
313 .pe_barwrite = pci_lpc_write,
314 .pe_barread = pci_lpc_read
316 PCI_EMUL_SET(pci_de_lpc);